// Mobile Menu Handler — wires the topbar burger to the slide-in drawer
// (#mobile-drawer). On pages that ship a page sidebar (#sidebar), the
// drawer borrows its contents while open so the user gets one unified
// nav surface on mobile.
function setupMobileMenu() {
  const toggle = document.getElementById('mobile-menu-toggle');
  const drawer = document.getElementById('mobile-drawer');
  const overlay = document.getElementById('mobile-overlay');

  if (!toggle || !drawer || !overlay) {
    setTimeout(setupMobileMenu, 100);
    return;
  }

  const pageSection = document.getElementById('mobile-drawer-page-section');
  let borrowedNodes = [];
  let sidebarOrigin = null;

  function borrowSidebar() {
    const sidebar = document.getElementById('sidebar');
    if (!sidebar || !pageSection) return;
    sidebarOrigin = sidebar;
    borrowedNodes = Array.from(sidebar.children);
    borrowedNodes.forEach((node) => pageSection.appendChild(node));
  }

  function returnSidebar() {
    if (!sidebarOrigin || borrowedNodes.length === 0) return;
    borrowedNodes.forEach((node) => sidebarOrigin.appendChild(node));
    borrowedNodes = [];
    sidebarOrigin = null;
  }

  function openMenu() {
    borrowSidebar();
    drawer.classList.add('mobile-open');
    overlay.classList.add('active');
    document.body.style.overflow = 'hidden';
  }

  function closeMenu() {
    drawer.classList.remove('mobile-open');
    overlay.classList.remove('active');
    document.body.style.overflow = '';
    returnSidebar();
  }

  function toggleMenu() {
    if (drawer.classList.contains('mobile-open')) closeMenu();
    else openMenu();
  }

  toggle.addEventListener('click', toggleMenu);
  overlay.addEventListener('click', closeMenu);

  // Close when any nav-item, sidebar-item, or category gets clicked
  // (they trigger navigation, so the drawer should dismiss itself).
  drawer.addEventListener('click', (e) => {
    const dismisser = e.target.closest('.nav-item, .sidebar-item, .category');
    if (dismisser) closeMenu();
  });

  window.addEventListener('resize', () => {
    if (window.innerWidth > 768 && drawer.classList.contains('mobile-open')) {
      closeMenu();
    }
  });

  window.closeMobileMenu = closeMenu;
}
