(function(){'use strict';document.addEventListener('DOMContentLoaded',init);function init(){initThemeToggle();initMobileSidebar();initLanguageDropdown();initStickyHeader();}
function initThemeToggle(){var toggle=document.querySelector('.ap-theme-toggle');if(!toggle)return;toggle.addEventListener('click',function(){var html=document.documentElement;var isLight=html.classList.contains('light-theme');if(isLight){html.classList.remove('light-theme');localStorage.setItem('ap-theme','dark');}else{html.classList.add('light-theme');localStorage.setItem('ap-theme','light');}});}
function initMobileSidebar(){const hamburger=document.querySelector('.ap-hamburger');const sidebar=document.querySelector('.ap-sidebar');const overlay=document.querySelector('.ap-sidebar-overlay');const closeBtn=document.querySelector('.ap-sidebar__close');if(!hamburger||!sidebar)return;function openSidebar(){sidebar.classList.add('is-open');overlay&&overlay.classList.add('is-visible');hamburger.classList.add('is-open');hamburger.setAttribute('aria-expanded','true');document.body.style.overflow='hidden';const firstLink=sidebar.querySelector('a, button');if(firstLink)firstLink.focus();}
function closeSidebar(){sidebar.classList.remove('is-open');overlay&&overlay.classList.remove('is-visible');hamburger.classList.remove('is-open');hamburger.setAttribute('aria-expanded','false');document.body.style.overflow='';hamburger.focus();}
hamburger.addEventListener('click',function(){const isOpen=sidebar.classList.contains('is-open');isOpen?closeSidebar():openSidebar();});closeBtn&&closeBtn.addEventListener('click',closeSidebar);overlay&&overlay.addEventListener('click',closeSidebar);document.addEventListener('keydown',function(e){if(e.key==='Escape'&&sidebar.classList.contains('is-open')){closeSidebar();}});}
function initLanguageDropdown(){
const langWidget=document.querySelector('.ap-lang');if(!langWidget)return;
const trigger=langWidget.querySelector('.ap-lang__trigger');if(!trigger)return;
const currentLang=langWidget.getAttribute('data-current-lang')||'';
let anchorMap=[];try{anchorMap=JSON.parse(langWidget.getAttribute('data-anchor-map')||'[]');}catch(e){anchorMap=[];}
const options=langWidget.querySelectorAll('.ap-lang__option');
options.forEach(function(o){if(o.getAttribute('data-base')===null)o.setAttribute('data-base',o.getAttribute('href')||'');});
function norm(u){if(!u)return'';let hash='';const h=u.indexOf('#');if(h>=0){hash=u.slice(h);u=u.slice(0,h);}const q=u.indexOf('?');if(q>=0)u=u.slice(0,q);if(u.length>1&&u.charAt(u.length-1)==='/')u=u.slice(0,-1);return u+hash;}
function decorate(){
const cur=norm(window.location.pathname+window.location.hash);
const curHash=window.location.hash;
options.forEach(function(o){
const base=o.getAttribute('data-base')||'';
const target=o.getAttribute('data-lang')||'';
let matched=null;
for(let i=0;i<anchorMap.length;i++){const entry=anchorMap[i];if(entry&&entry[currentLang]&&norm(entry[currentLang])===cur){matched=entry;break;}}
let href=base;
if(matched&&matched[target]){href=matched[target];}
else if(curHash){href=base.split('#')[0]+curHash;}
o.setAttribute('href',href);
});
}
decorate();
trigger.addEventListener('click',function(e){e.stopPropagation();decorate();langWidget.classList.toggle('is-open');});
document.addEventListener('click',function(e){if(!langWidget.contains(e.target)){langWidget.classList.remove('is-open');}});
document.addEventListener('keydown',function(e){if(e.key==='Escape'){langWidget.classList.remove('is-open');}});
}
function initStickyHeader(){const header=document.querySelector('.ap-header');if(!header)return;header.classList.toggle('is-scrolled',window.scrollY>10);let ticking=false;window.addEventListener('scroll',function(){if(!ticking){window.requestAnimationFrame(function(){header.classList.toggle('is-scrolled',window.scrollY>10);ticking=false;});ticking=true;}},{passive:true});}})();