(() => {
  'use strict';
  const theme = document.documentElement.dataset.scrollTheme || '';
  if (!['academic-evidence', 'student-performance'].includes(theme)) return;

  const isMobile = () => matchMedia('(max-width: 54rem)').matches;

  function clearAcademicLegacyNavigation() {
    if (theme !== 'academic-evidence' || !isMobile()) return;
    const root = document.getElementById('root');
    if (!root) return;

    root.querySelectorAll('button[aria-label="Open navigation"],button[aria-label="Close navigation"]').forEach(button => {
      let node = button.parentElement;
      while (node && node !== root && getComputedStyle(node).position !== 'fixed') node = node.parentElement;
      if (node && node !== root) node.setAttribute('data-apu-academic-legacy-nav', 'true');
    });

    root.querySelectorAll('[class*="md:hidden"][class*="fixed"]').forEach(node => {
      node.setAttribute('data-apu-academic-legacy-nav', 'true');
    });

    root.querySelectorAll('div,nav,header').forEach(node => {
      const style = getComputedStyle(node);
      const text = (node.textContent || '').replace(/\s+/g, ' ').trim();
      if (style.position === 'fixed' && /Overview.*Projects.*Curriculum Vitae/i.test(text)) {
        node.setAttribute('data-apu-academic-legacy-nav', 'true');
      }
    });

    [root, root.firstElementChild, root.querySelector('main')].filter(Boolean).forEach(node => {
      node.style.opacity = '1';
      node.style.filter = 'none';
      node.style.webkitFilter = 'none';
      node.style.backdropFilter = 'none';
      node.style.webkitBackdropFilter = 'none';
    });
  }

  function improveGeneratedDrawer() {
    if (!isMobile()) return;
    const bar = document.querySelector('.apu-v5-mobile-project-bar');
    const drawer = document.querySelector('.apu-v5-mobile-nav-drawer');
    const toggle = document.querySelector('.apu-v5-mobile-project-toggle');
    if (!bar || !drawer || !toggle || toggle.dataset.apuV51Ready) return;

    toggle.dataset.apuV51Ready = 'true';
    drawer.setAttribute('aria-hidden', drawer.classList.contains('is-open') ? 'false' : 'true');

    const observer = new MutationObserver(() => {
      const open = drawer.classList.contains('is-open');
      drawer.setAttribute('aria-hidden', open ? 'false' : 'true');
      toggle.setAttribute('aria-expanded', open ? 'true' : 'false');
    });
    observer.observe(drawer, {attributes: true, attributeFilter: ['class']});

    addEventListener('pageshow', () => {
      drawer.classList.remove('is-open');
      drawer.setAttribute('aria-hidden', 'true');
      toggle.setAttribute('aria-expanded', 'false');
    }, {once: true});
  }

  function guaranteeAcademIQScroll() {
    if (theme !== 'student-performance' || !isMobile()) return;
    document.documentElement.style.overflowY = 'auto';
    document.body.style.overflowY = 'auto';
    document.documentElement.style.height = 'auto';
    document.body.style.height = 'auto';
    const main = document.querySelector('.sp-main');
    if (main) {
      main.style.height = 'auto';
      main.style.maxHeight = 'none';
      main.style.overflow = 'visible';
    }
  }

  let raf = 0;
  const scan = () => {
    cancelAnimationFrame(raf);
    raf = requestAnimationFrame(() => {
      clearAcademicLegacyNavigation();
      improveGeneratedDrawer();
      guaranteeAcademIQScroll();
    });
  };

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

  const mutationObserver = new MutationObserver(scan);
  mutationObserver.observe(document.documentElement, {subtree: true, childList: true});
  setTimeout(() => mutationObserver.disconnect(), 15000);
  addEventListener('resize', scan, {passive: true});
  addEventListener('orientationchange', scan, {passive: true});
})();
