
(function(){
  const menuBtn=document.querySelector('.menu-toggle');
  const nav=document.querySelector('.main-nav');
  if(menuBtn&&nav){menuBtn.addEventListener('click',()=>{const open=nav.classList.toggle('open');menuBtn.setAttribute('aria-expanded',String(open));});nav.querySelectorAll('a').forEach(a=>a.addEventListener('click',()=>nav.classList.remove('open')))}
  const year=document.getElementById('year');if(year)year.textContent=new Date().getFullYear();
  const observer=new IntersectionObserver(entries=>entries.forEach(e=>{if(e.isIntersecting){e.target.classList.add('visible');observer.unobserve(e.target)}}),{threshold:.08});document.querySelectorAll('.reveal').forEach(el=>observer.observe(el));
  const filters=document.querySelectorAll('.filter');const items=document.querySelectorAll('.gallery-item');filters.forEach(btn=>btn.addEventListener('click',()=>{filters.forEach(b=>b.classList.remove('active'));btn.classList.add('active');const f=btn.dataset.filter;items.forEach(item=>item.classList.toggle('hidden',f!=='all'&&item.dataset.category!==f));}));

  const form=document.getElementById('bookingForm');if(!form)return;
  const requiredDate=document.getElementById('requiredDate');
  const min=new Date();min.setDate(min.getDate()+7);requiredDate.min=min.toISOString().split('T')[0];
  const basePrices={6:{1:70,2:105,3:140,4:175},7:{1:85,2:125,3:165,4:205},8:{1:100,2:145,3:190,4:235},9:{1:115,2:165,3:215,4:265}};
  const orderType=document.getElementById('orderType'),cakeOptions=document.getElementById('cakeOptions'),cupcakeOptions=document.getElementById('cupcakeOptions');
  function toggleOrder(){const v=orderType.value;cakeOptions.style.display=v==='cupcakes'?'none':'block';cupcakeOptions.classList.toggle('show',v==='cupcakes'||v==='both');calculate();}
  function money(n){return '$'+Math.round(n).toLocaleString('en-AU')}
  function calculate(){
    let total=0,lines=[];const order=orderType.value;
    if(order==='cake'||order==='both'){
      const size=document.getElementById('cakeSize').value,layers=document.getElementById('cakeLayers').value;base=basePrices[size][layers];total+=base;lines.push([`${size} inch · ${layers} layer${layers==='1'?'':'s'}`,base]);
      const style=Number(document.getElementById('cakeStyle').value);if(style){total+=style;lines.push(['Cake style',style]);}
    }
    if(order==='cupcakes'||order==='both'){
      const pkg=Number(document.getElementById('cupcakePackage').value),dozens=Math.max(1,Number(document.getElementById('cupcakeDozens').value||1));const cp=pkg*dozens;total+=cp;lines.push([`${dozens} dozen cupcakes`,cp]);
    }
    document.querySelectorAll('[data-price]:checked').forEach(c=>{const p=Number(c.dataset.price);total+=p;lines.push([c.value.split(' — ')[0],p]);});
    if(document.getElementById('fulfilment').value==='delivery'){total+=20;lines.push(['Delivery & setup',20]);}
    document.getElementById('estimateTotal').textContent=money(total);document.getElementById('estimatedTotalInput').value=money(total);
    const deposit=total/2;document.getElementById('depositEstimate').textContent=money(deposit);document.getElementById('depositEstimateInput').value=money(deposit);
    document.getElementById('estimateBreakdown').innerHTML=lines.map(([n,p])=>`<div><span>${n}</span><b>${money(p)}</b></div>`).join('');
  }
  function updateDate(){if(!requiredDate.value){document.getElementById('finalPaymentDue').textContent='Select a date';return}const d=new Date(requiredDate.value+'T12:00:00');d.setDate(d.getDate()-3);const formatted=d.toLocaleDateString('en-AU',{day:'numeric',month:'short',year:'numeric'});document.getElementById('finalPaymentDue').textContent=formatted;document.getElementById('finalPaymentDueInput').value=formatted;}
  form.addEventListener('input',calculate);form.addEventListener('change',calculate);orderType.addEventListener('change',toggleOrder);requiredDate.addEventListener('change',updateDate);toggleOrder();updateDate();calculate();
})();
