(() => {
  'use strict';
  if (window.__APU_V624_LAYOUT__) return;
  window.__APU_V624_LAYOUT__ = true;
  const root = document.documentElement;
  const base = String(window.__APU_V624_BASE__ || '/').replace(/\/?$/, '/');
  const info = window.__APU_STANDALONE__ || {};
  const title = info.title || document.title || 'AI Project';

  function ensureBar(){
    let bar=document.getElementById('apu-v624-utilitybar');
    if(bar)return bar;
    bar=document.createElement('header');
    bar.id='apu-v624-utilitybar';
    bar.dataset.apuNoTranslate='true';
    bar.setAttribute('aria-label','Project display controls');
    const brand=document.createElement('a');
    brand.id='apu-v624-brand';brand.href=base;brand.setAttribute('aria-label',`${title} home`);
    const icon=document.createElement('img');icon.src=`${base}icons/icon-64.png?v=624`;icon.alt='';icon.width=42;icon.height=42;
    const copy=document.createElement('span');copy.id='apu-v624-brand-copy';
    const strong=document.createElement('strong');strong.textContent=title;
    const small=document.createElement('small');small.textContent='Standalone Cloudflare Project';
    copy.append(strong,small);brand.append(icon,copy);
    const slot=document.createElement('div');slot.id='apu-v624-controls-slot';slot.dataset.apuControlsSlot='true';
    bar.append(brand,slot);
    document.body.insertBefore(bar,document.body.firstChild);
    return bar;
  }
  function moveDock(){
    const slot=document.getElementById('apu-v624-controls-slot');
    const dock=document.getElementById('apu-ui-dock-v62');
    if(!slot||!dock)return false;
    dock.dataset.apuInline='true';dock.dataset.apuMenuMounted='true';
    if(dock.parentElement!==slot)slot.appendChild(dock);
    return true;
  }
  let cookieObserver=null,resizeObserver=null;
  function syncCookieSpace(){
    const panel=document.getElementById('apuCookiePanel');
    const open=!!panel&&!panel.hidden&&getComputedStyle(panel).display!=='none';
    if(open){
      const h=Math.ceil(panel.getBoundingClientRect().height+32+Math.max(0,parseFloat(getComputedStyle(panel).bottom)||0));
      root.dataset.apuCookieOpen='true';root.style.setProperty('--apu-v624-cookie-space',`${h}px`);
      if(!resizeObserver&&'ResizeObserver'in window){resizeObserver=new ResizeObserver(syncCookieSpace);resizeObserver.observe(panel);}
    }else{
      delete root.dataset.apuCookieOpen;root.style.setProperty('--apu-v624-cookie-space','0px');
      if(resizeObserver){resizeObserver.disconnect();resizeObserver=null;}
    }
  }
  function observe(){
    const observer=new MutationObserver(()=>{moveDock();syncCookieSpace();});
    observer.observe(document.body,{subtree:true,childList:true,attributes:true,attributeFilter:['hidden','style','class']});
    window.addEventListener('resize',syncCookieSpace,{passive:true});
    window.addEventListener('orientationchange',syncCookieSpace,{passive:true});
    [0,80,250,600,1200,2400].forEach(ms=>setTimeout(()=>{moveDock();syncCookieSpace();},ms));
  }
  function init(){ensureBar();moveDock();syncCookieSpace();observe();}
  if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',init,{once:true});else init();
})();
