(() => {
  'use strict';
  if (window.__APU_V626_MOBILE_DESIGN_PRESERVING_FIX__) return;
  window.__APU_V626_MOBILE_DESIGN_PRESERVING_FIX__ = true;

  const root = document.documentElement;
  root.dataset.apuV626MobileFixed = 'true';

  let observer;
  let resizeObserver;
  let raf = 0;

  function isMobile(){ return matchMedia('(max-width:760px)').matches; }

  function keepSingleBar(){
    const bars = [...document.querySelectorAll('#apu-v624-utilitybar')];
    if (bars.length > 1) bars.slice(1).forEach(node => node.remove());
    return bars[0] || null;
  }

  function keepDockInSlot(){
    const bar = keepSingleBar();
    const slot = document.getElementById('apu-v624-controls-slot');
    const docks = [...document.querySelectorAll('#apu-ui-dock-v62')];
    if (docks.length > 1) docks.slice(1).forEach(node => node.remove());
    const dock = docks[0];
    if (bar && slot && dock && dock.parentElement !== slot){
      dock.dataset.apuInline = 'true';
      dock.dataset.apuMenuMounted = 'true';
      slot.appendChild(dock);
    }
    return {bar,dock};
  }

  function measure(){
    cancelAnimationFrame(raf);
    raf = requestAnimationFrame(() => {
      const {bar} = keepDockInSlot();
      if (!bar) return;
      const height = Math.max(0, Math.ceil(bar.getBoundingClientRect().height));
      root.style.setProperty('--apu-v626mf-bar-actual', `${height}px`);
      root.dataset.apuV626MobileLayout = isMobile() ? 'compact-v626' : 'desktop-v626';
    });
  }

  function bind(){
    const {bar} = keepDockInSlot();
    if (bar && 'ResizeObserver' in window){
      if (resizeObserver) resizeObserver.disconnect();
      resizeObserver = new ResizeObserver(measure);
      resizeObserver.observe(bar);
    }
    measure();
  }

  function init(){
    bind();
    observer = new MutationObserver(() => bind());
    observer.observe(document.body, {subtree:true, childList:true, attributes:true, attributeFilter:['class','style','hidden']});
    window.addEventListener('resize', measure, {passive:true});
    window.addEventListener('orientationchange', measure, {passive:true});
    [0,80,220,500,1000,1800].forEach(ms => setTimeout(bind, ms));
  }

  if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init, {once:true});
  else init();
})();
