(() => {
  'use strict';
  const MOBILE_QUERY = '(max-width: 54rem)';
  const rootSelector = '#root, #app, #__next, [data-reactroot]';
  let queued = false;

  function ensureViewport() {
    let meta = document.querySelector('meta[name="viewport"]');
    if (!meta) {
      meta = document.createElement('meta');
      meta.name = 'viewport';
      document.head.prepend(meta);
    }
    meta.content = 'width=device-width, initial-scale=1, viewport-fit=cover';
  }

  function wrapTables(scope) {
    scope.querySelectorAll('table').forEach((table) => {
      if (table.closest('.apu-mobile-table-shell,.table-wrap,.table-responsive,.apu-table-wrap,.sp-table-wrap')) return;
      const shell = document.createElement('div');
      shell.className = 'apu-mobile-table-shell';
      shell.setAttribute('role', 'region');
      shell.setAttribute('aria-label', 'Scrollable data table');
      table.parentNode.insertBefore(shell, table);
      shell.appendChild(table);
    });
  }

  function unlockDocumentFlow(scope) {
    const viewport = Math.max(window.innerHeight || 0, 1);
    scope.querySelectorAll('main,[role="main"],div,section').forEach((el) => {
      if (el.closest('.apu-cookie-panel,dialog,[role="dialog"],.modal,.table-wrap,.apu-table-wrap,.sp-table-wrap,pre')) return;
      const cs = getComputedStyle(el);
      const rect = el.getBoundingClientRect();
      const isPageScroller = rect.height >= viewport * 0.72 &&
        ['hidden','auto','scroll'].includes(cs.overflowY) &&
        (el.scrollHeight > el.clientHeight + 8 || cs.overflowY === 'hidden');
      if (isPageScroller) el.classList.add('apu-mobile-document-flow');
    });
  }

  function constrainWideElements(scope) {
    const width = document.documentElement.clientWidth || window.innerWidth;
    scope.querySelectorAll('main > *, [role="main"] > *, form, fieldset, article, section').forEach((el) => {
      if (el.closest('.apu-mobile-table-shell,.table-wrap,.table-responsive,.apu-table-wrap,.sp-table-wrap')) return;
      const rect = el.getBoundingClientRect();
      if (rect.width > width + 2 || rect.left < -2 || rect.right > width + 2) {
        el.setAttribute('data-apu-mobile-fit', 'true');
      }
    });
  }

  function apply() {
    queued = false;
    if (!matchMedia(MOBILE_QUERY).matches) return;
    document.documentElement.classList.add('apu-mobile-v4-8');
    const scope = document.querySelector(rootSelector) || document.body;
    wrapTables(scope);
    unlockDocumentFlow(scope);
    constrainWideElements(scope);
  }

  function schedule() {
    if (queued) return;
    queued = true;
    requestAnimationFrame(apply);
  }

  ensureViewport();
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', schedule, { once: true });
  } else {
    schedule();
  }
  window.addEventListener('load', schedule, { once: true });
  window.addEventListener('resize', schedule, { passive: true });

  const observer = new MutationObserver(schedule);
  observer.observe(document.documentElement, { childList: true, subtree: true });
  setTimeout(() => observer.disconnect(), 8000);
})();
