/* AI Project Universe V6.31/V6.34 — legal page language and theme control */
(()=>{
  'use strict';
  if(window.__APU_LEGAL_LANGUAGE_V631__)return;
  window.__APU_LEGAL_LANGUAGE_V631__=true;
  const langs={id:'Indonesia',en:'English',ms:'Melayu',de:'Deutsch',vi:'Tiếng Việt',zh:'中文',ja:'日本語'};
  const labels={
    id:{theme:'Tema',light:'Terang',dark:'Gelap',language:'Bahasa'},en:{theme:'Theme',light:'Light',dark:'Dark',language:'Language'},ms:{theme:'Tema',light:'Cerah',dark:'Gelap',language:'Bahasa'},de:{theme:'Design',light:'Hell',dark:'Dunkel',language:'Sprache'},vi:{theme:'Giao diện',light:'Sáng',dark:'Tối',language:'Ngôn ngữ'},zh:{theme:'主题',light:'浅色',dark:'深色',language:'语言'},ja:{theme:'テーマ',light:'ライト',dark:'ダーク',language:'言語'}
  };
  const norm=v=>Object.hasOwn(langs,String(v||'').split('-')[0])?String(v).split('-')[0]:'id';
  const getLang=()=>{try{return norm(localStorage.getItem('apu-ui-language')||document.documentElement.lang)}catch(_){return'id'}};
  const getTheme=()=>{try{return localStorage.getItem('apu-ui-theme')==='dark'?'dark':'light'}catch(_){return'light'}};
  function applyTheme(theme){theme=theme==='dark'?'dark':'light';document.documentElement.dataset.apuTheme=theme;document.documentElement.style.colorScheme=theme;try{localStorage.setItem('apu-ui-theme',theme)}catch(_){};document.querySelector('[data-legal-theme-label]')?.replaceChildren(document.createTextNode((labels[getLang()]||labels.en)[theme]));}
  function applyLanguage(lang,announce=true){lang=norm(lang);document.documentElement.lang=lang;document.documentElement.dataset.apuLanguage=lang;try{localStorage.setItem('apu-ui-language',lang)}catch(_){};const ui=labels[lang]||labels.en;const select=document.querySelector('[data-legal-language]');if(select)select.value=lang;const theme=document.querySelector('[data-legal-theme]');if(theme)theme.setAttribute('aria-label',ui.theme);document.querySelector('[data-legal-theme-label]')?.replaceChildren(document.createTextNode(ui[getTheme()]));if(announce)window.dispatchEvent(new CustomEvent('apu:languagechange',{detail:{language:lang}}));}
  function mount(){
    if(!document.body?.classList.contains('apu-legal')||document.querySelector('[data-apu-legal-i18n-ui]'))return;
    const box=document.createElement('div');box.dataset.apuLegalI18nUi='true';box.dataset.apuNoTranslate='true';box.setAttribute('translate','no');box.className='apu-legal-i18n-v631';
    const theme=document.createElement('button');theme.type='button';theme.dataset.legalTheme='true';theme.innerHTML='◐ <span data-legal-theme-label></span>';theme.onclick=()=>applyTheme(getTheme()==='dark'?'light':'dark');
    const select=document.createElement('select');select.dataset.legalLanguage='true';Object.entries(langs).forEach(([code,name])=>{const option=document.createElement('option');option.value=code;option.textContent=name;select.appendChild(option)});select.onchange=()=>applyLanguage(select.value,true);
    box.append(theme,select);document.body.appendChild(box);applyTheme(getTheme());applyLanguage(getLang(),true);
  }
  if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',mount,{once:true});else mount();
})();
