(()=>{
  const reduced=window.matchMedia('(prefers-reduced-motion: reduce)').matches;
  const loader=document.querySelector('.page-loader');
  const header=document.querySelector('[data-header]');
  const menuBtn=document.querySelector('.menu-toggle');
  const mobile=document.querySelector('.mobile-menu');
  const updateMobileTop=()=>{
    if(!header||!mobile)return;
    const bottom=Math.max(0,Math.round(header.getBoundingClientRect().bottom));
    mobile.style.setProperty('--mobile-menu-top',`${bottom}px`);
  };
  const updateHeader=()=>{header?.classList.toggle('scrolled',window.scrollY>12);updateMobileTop();};
  updateHeader();
  addEventListener('scroll',updateHeader,{passive:true});
  addEventListener('resize',updateMobileTop,{passive:true});
  addEventListener('load',()=>setTimeout(()=>loader?.classList.add('done'),220));
  setTimeout(()=>loader?.classList.add('done'),1600);
  document.querySelectorAll('[data-year]').forEach(el=>el.textContent=new Date().getFullYear());
  const file=location.pathname.split('/').pop()||'index.html';
  document.querySelectorAll('.desktop-nav a,.mobile-menu nav a').forEach(a=>{if(a.getAttribute('href')===file)a.setAttribute('aria-current','page')});
  const setMenu=(open)=>{
    if(!menuBtn||!mobile)return;
    updateMobileTop();
    menuBtn.setAttribute('aria-expanded',String(open));
    menuBtn.setAttribute('aria-label',open?'Menu sluiten':'Menu openen');
    mobile.classList.toggle('open',open);
    mobile.setAttribute('aria-hidden',String(!open));
    document.body.classList.toggle('menu-open',open);
    if(open) mobile.querySelector('a')?.focus({preventScroll:true});
  };
  menuBtn?.addEventListener('click',()=>setMenu(menuBtn.getAttribute('aria-expanded')!=='true'));
  mobile?.querySelectorAll('a').forEach(a=>a.addEventListener('click',()=>setMenu(false)));
  addEventListener('keydown',e=>{if(e.key==='Escape'&&menuBtn?.getAttribute('aria-expanded')==='true'){setMenu(false);menuBtn.focus();}});
  const reveal=[...document.querySelectorAll('[data-reveal],.hero-visual')];
  if(reduced){reveal.forEach(el=>el.classList.add('visible'))}else{
    const io=new IntersectionObserver(entries=>entries.forEach(e=>{if(e.isIntersecting){e.target.classList.add('visible');io.unobserve(e.target)}}),{threshold:.12,rootMargin:'0px 0px -5%'});
    reveal.forEach(el=>io.observe(el));
  }
  const rail=document.querySelector('[data-rail]');
  document.querySelector('[data-prev]')?.addEventListener('click',()=>rail?.scrollBy({left:-250,behavior:'smooth'}));
  document.querySelector('[data-next]')?.addEventListener('click',()=>rail?.scrollBy({left:250,behavior:'smooth'}));
  if(!reduced){
    let ticking=false;
    const parallax=()=>{
      document.querySelectorAll('[data-parallax] img').forEach(img=>{
        const r=img.parentElement.getBoundingClientRect(); if(r.bottom<0||r.top>innerHeight)return;
        const p=(innerHeight/2-(r.top+r.height/2))/innerHeight;
        img.style.transform=`translateY(${p*18}px) scale(1.035)`;
      }); ticking=false;
    };
    addEventListener('scroll',()=>{if(!ticking){requestAnimationFrame(parallax);ticking=true}},{passive:true}); parallax();
    document.querySelectorAll('a[href]').forEach(a=>a.addEventListener('click',e=>{
      const href=a.getAttribute('href');
      if(!href||href.startsWith('#')||href.startsWith('http')||href.startsWith('mailto:')||a.target==='_blank')return;
      const u=new URL(a.href,location.href); if(u.origin!==location.origin)return;
      e.preventDefault(); document.querySelector('.page-wipe')?.classList.add('active'); setTimeout(()=>location.href=u.href,340);
    }));
  }
})();
