// @ts-check
(()=>{
  'use strict';
  const App=window.IUS_TEMPLATE,{utils,state}=App;let sensorHandler=null;
  function reveal(){const items=utils.$$('.reveal');if(!('IntersectionObserver'in window)){items.forEach(i=>i.classList.add('is-visible'));return}const ob=new IntersectionObserver(entries=>entries.forEach(entry=>{if(entry.isIntersecting){entry.target.classList.add('is-visible');ob.unobserve(entry.target)}}),{rootMargin:'0px 0px -8%',threshold:.08});items.forEach(i=>ob.observe(i))}
  function initTilt(){utils.$$('[data-tilt]').forEach(el=>{el.addEventListener('pointermove',e=>{if(!state.motion||matchMedia('(prefers-reduced-motion: reduce)').matches||matchMedia('(pointer:coarse)').matches)return;const r=el.getBoundingClientRect(),rx=((e.clientY-r.top)/r.height-.5)*-2.2,ry=((e.clientX-r.left)/r.width-.5)*2.2;el.style.transform=`perspective(1200px) rotateX(${rx}deg) rotateY(${ry}deg) translate3d(${ry*1.5}px,${-rx*1.5}px,0)`});el.addEventListener('pointerleave',()=>el.style.transform='')})}
  function enableSensor(){if(sensorHandler||!state.sensorMotion||typeof DeviceOrientationEvent==='undefined')return;sensorHandler=e=>{const x=Math.max(-1,Math.min(1,(e.gamma||0)/30)),y=Math.max(-1,Math.min(1,(e.beta||0)/45));utils.$$('[data-tilt]').forEach(el=>el.style.transform=`perspective(1200px) rotateX(${-y*.7}deg) rotateY(${x*.7}deg) translate3d(${x*2}px,${y*2}px,0)`)};window.addEventListener('deviceorientation',sensorHandler,{passive:true})}
  function disableSensor(){if(sensorHandler){window.removeEventListener('deviceorientation',sensorHandler);sensorHandler=null}utils.$$('[data-tilt]').forEach(el=>el.style.transform='')}
  function viewport(){const root=document.documentElement;let frame=0;const update=()=>{frame=0;const vp=visualViewport,h=Math.round(vp?.height||innerHeight||root.clientHeight);if(h>0)root.style.setProperty('--viewport-h',`${h}px`);App.modules.lightbox?.reset()};const schedule=()=>{if(frame)cancelAnimationFrame(frame);frame=requestAnimationFrame(update)};update();addEventListener('resize',schedule,{passive:true});addEventListener('orientationchange',()=>{schedule();setTimeout(schedule,120);setTimeout(schedule,360)},{passive:true});visualViewport?.addEventListener('resize',schedule,{passive:true})}
  function header(){const h=utils.$('[data-header]'),top=utils.$('[data-back-to-top]');const onScroll=()=>{h?.classList.toggle('is-scrolled',scrollY>16);top?.classList.toggle('is-visible',scrollY>680)};onScroll();addEventListener('scroll',onScroll,{passive:true});top?.addEventListener('click',()=>scrollTo({top:0,behavior:matchMedia('(prefers-reduced-motion: reduce)').matches?'auto':'smooth'}));const links=utils.$$('.desktop-nav a[href^="#"]'),sections=links.map(a=>document.querySelector(a.getAttribute('href'))).filter(Boolean);if('IntersectionObserver'in window){const ob=new IntersectionObserver(entries=>{const v=entries.filter(e=>e.isIntersecting).sort((a,b)=>b.intersectionRatio-a.intersectionRatio)[0];if(v)links.forEach(a=>a.classList.toggle('is-active',a.getAttribute('href')===`#${v.target.id}`))},{rootMargin:'-25% 0px -65%',threshold:[0,.2,.5]});sections.forEach(s=>ob.observe(s))}}
  function menu(){const btn=utils.$('[data-menu-toggle]'),nav=utils.$('[data-mobile-nav]');const set=open=>{btn?.setAttribute('aria-expanded',String(open));if(nav)nav.hidden=!open;document.body.classList.toggle('nav-open',open);const sr=utils.$('.sr-only',btn);if(sr)sr.textContent=App.modules.i18n.t(open?'menu.close':'menu.open')};btn?.addEventListener('click',()=>set(btn.getAttribute('aria-expanded')!=='true'));utils.$$('a',nav).forEach(a=>a.addEventListener('click',()=>set(false)));matchMedia('(min-width:1201px)').addEventListener?.('change',e=>{if(e.matches)set(false)})}
  function init(){reveal();initTilt();viewport();header();menu();if(state.sensorMotion)enableSensor();document.addEventListener('ius:motionchange',e=>e.detail.enabled?enableSensor():disableSensor())}
  App.modules.motion={init,enableSensor,disableSensor};
})();
