(function(){
  const body=document.body;
  const toggle=document.querySelector('[data-front-menu-toggle]');
  const nav=document.getElementById('front-nav');
  const overlay=document.querySelector('[data-front-menu-overlay]');
  const closeFrontMenu=()=>{body.classList.remove('front-menu-open'); if(toggle) toggle.setAttribute('aria-expanded','false');};
  if(toggle && nav){
    toggle.addEventListener('click',()=>{const open=!body.classList.contains('front-menu-open');body.classList.toggle('front-menu-open',open);toggle.setAttribute('aria-expanded',open?'true':'false');});
    nav.querySelectorAll('a').forEach(a=>a.addEventListener('click',closeFrontMenu));
  }
  if(overlay) overlay.addEventListener('click',closeFrontMenu);
  const preloader=document.querySelector('[data-preloader]');
  const showLoader=()=>{ if(preloader){ preloader.classList.add('is-active'); preloader.setAttribute('aria-hidden','false'); } };
  const hideLoader=()=>{ if(preloader){ preloader.classList.remove('is-active'); preloader.setAttribute('aria-hidden','true'); } };
  window.addEventListener('pageshow',hideLoader);
  document.querySelectorAll('form').forEach(form=>form.addEventListener('submit',()=>showLoader()));
  document.addEventListener('click',(event)=>{const link=event.target.closest('a'); if(!link) return; const href=link.getAttribute('href')||''; if(!href || href.startsWith('#') || href.startsWith('mailto:') || href.startsWith('tel:') || link.target==='_blank' || link.hasAttribute('download')) return; showLoader();});

  const consentBox=document.querySelector('[data-cookie-consent]');
  const settingsButtons=document.querySelectorAll('[data-cookie-settings]');
  const statisticsCheckbox=document.querySelector('[data-cookie-category="statistics"]');
  const consentCookie='qrator_cookie_consent';
  const consentMaxAge=60*60*24*180;
  const defaultConsent={necessary:true,statistics:false,version:1};
  const readCookie=(name)=>document.cookie.split('; ').find(row=>row.startsWith(name+'='))?.split('=')[1]||'';
  const writeCookie=(value)=>{document.cookie=consentCookie+'='+encodeURIComponent(JSON.stringify(value))+'; Max-Age='+consentMaxAge+'; Path=/; SameSite=Lax';};
  const readConsent=()=>{
    try{
      const raw=readCookie(consentCookie) || window.localStorage.getItem(consentCookie) || '';
      return raw ? Object.assign({},defaultConsent,JSON.parse(decodeURIComponent(raw))) : null;
    }catch(error){return null;}
  };
  const saveConsent=(value)=>{
    const consent=Object.assign({},defaultConsent,value,{necessary:true,version:1});
    writeCookie(consent);
    try{window.localStorage.setItem(consentCookie,JSON.stringify(consent));}catch(error){}
    applyConsent(consent);
    if(consentBox) consentBox.hidden=true;
  };
  const activateScripts=(category)=>{
    document.querySelectorAll('script[type="text/plain"][data-cookie-script="'+category+'"]').forEach(script=>{
      const active=document.createElement('script');
      Array.from(script.attributes).forEach(attr=>{if(!['type','data-cookie-script'].includes(attr.name)) active.setAttribute(attr.name,attr.value);});
      active.text=script.textContent;
      script.replaceWith(active);
    });
    window.dispatchEvent(new CustomEvent('qrator:cookie-consent',{detail:{category:category,allowed:true}}));
  };
  const applyConsent=(consent)=>{
    body.classList.toggle('cookies-statistics-enabled',!!consent.statistics);
    if(statisticsCheckbox) statisticsCheckbox.checked=!!consent.statistics;
    if(consent.statistics) activateScripts('statistics');
  };
  const openConsent=()=>{const consent=readConsent()||defaultConsent; applyConsent(consent); if(consentBox) consentBox.hidden=false;};
  const existing=readConsent();
  if(existing){applyConsent(existing);} else if(consentBox){consentBox.hidden=false;}
  settingsButtons.forEach(button=>button.addEventListener('click',openConsent));
  document.querySelector('[data-cookie-accept-all]')?.addEventListener('click',()=>saveConsent({statistics:true}));
  document.querySelector('[data-cookie-essential]')?.addEventListener('click',()=>saveConsent({statistics:false}));
  document.querySelector('[data-cookie-save]')?.addEventListener('click',()=>saveConsent({statistics:!!(statisticsCheckbox && statisticsCheckbox.checked)}));
})();
