/* AI Project Universe V6.34 — standalone project reliability and translation core */
(()=>{
  'use strict';
  if(window.__APU_STANDALONE_CORE_V634__)return;
  window.__APU_STANDALONE_CORE_V634__=true;
  const html=document.documentElement,cfg=window.__APU_STANDALONE__||{};
  const slug=String(html.dataset.apuStandaloneProject||cfg.slug||'').replace(/^\/+|\/+$/g,'');
  html.dataset.apuStandaloneReady='v6.34';
  const prefix=slug?`/projects/${slug}`:'';
  const localize=value=>{if(value==null)return value;try{const url=new URL(String(value),location.href);if(url.origin!==location.origin)return value;let path=url.pathname;if(prefix&&(path===prefix||path===prefix+'/'))path='/';else if(prefix&&path.startsWith(prefix+'/'))path=path.slice(prefix.length)||'/';return path+url.search+url.hash}catch(_){return value}};
  const push=history.pushState.bind(history),replace=history.replaceState.bind(history);
  history.pushState=(state,title,url)=>push(state,title,localize(url));history.replaceState=(state,title,url)=>replace(state,title,localize(url));
  try{const next=localize(location.href),current=location.pathname+location.search+location.hash;if(typeof next==='string'&&next!==current)replace(history.state,'',next)}catch(_){}
  const protectedSelector='select,option,[contenteditable="true"],[role="listbox"],[role="option"],[role="combobox"],[data-radix-select-content],[data-radix-select-item],[data-radix-select-viewport],[data-radix-popper-content-wrapper]';
  function protect(scope=document){if(scope instanceof Element&&scope.matches(protectedSelector)){scope.dataset.apuNoTranslate='true';scope.setAttribute('translate','no')}scope.querySelectorAll?.(protectedSelector).forEach(el=>{el.dataset.apuNoTranslate='true';el.setAttribute('translate','no')})}
  function ready(){protect(document);document.body?.setAttribute('data-apu-standalone-body','v6.34');const observer=new MutationObserver(list=>{for(const mutation of list)for(const node of mutation.addedNodes)if(node.nodeType===1)protect(node)});observer.observe(document.documentElement,{subtree:true,childList:true});document.dispatchEvent(new CustomEvent('apu:standalone-ready',{detail:{slug,version:'6.34'}}))}
  document.addEventListener('click',event=>{const link=event.target.closest?.('a[href]');if(!link||event.defaultPrevented||event.button>0||event.metaKey||event.ctrlKey||event.shiftKey||event.altKey||link.hasAttribute('download'))return;const raw=link.getAttribute('href');if(!raw||raw.startsWith('#')||raw.startsWith('mailto:')||raw.startsWith('tel:'))return;let url;try{url=new URL(raw,location.href)}catch(_){return}if(url.origin!==location.origin)return;const next=localize(url.href);if(typeof next==='string'&&next!==url.pathname+url.search+url.hash){event.preventDefault();history.pushState({},'',next);dispatchEvent(new PopStateEvent('popstate'))}},true);
  if('serviceWorker'in navigator)navigator.serviceWorker.getRegistrations?.().then(items=>items.forEach(item=>item.unregister())).catch(()=>{});
  if('caches'in window)caches.keys().then(keys=>Promise.all(keys.filter(k=>/apu|project|workbox|v63[0-3]/i.test(k)).map(k=>caches.delete(k)))).catch(()=>{});
  if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',ready,{once:true});else ready();
})();
