(function () {
  function initMobileNav() {
    var toggle = document.querySelector('.navbar .nav-toggle');
    var menu = document.querySelector('.navbar .nav-menu');
    if (!toggle || !menu) return;

    var setOpen = function (open) {
      menu.classList.toggle('active', open);
      menu.classList.toggle('show', open);
      toggle.classList.toggle('active', open);
      toggle.setAttribute('aria-expanded', String(open));
    };

    toggle.addEventListener('click', function (e) {
      e.preventDefault();
      e.stopPropagation();
      setOpen(!menu.classList.contains('active'));
    });

    toggle.addEventListener('keydown', function (e) {
      if (e.key === 'Enter' || e.key === ' ') {
        e.preventDefault();
        setOpen(!menu.classList.contains('active'));
      }
    });

    document.addEventListener('click', function (e) {
      if (!menu.classList.contains('active')) return;
      if (toggle.contains(e.target) || menu.contains(e.target)) return;
      setOpen(false);
    });

    menu.querySelectorAll('a').forEach(function (link) {
      link.addEventListener('click', function () {
        setOpen(false);
      });
    });
  }

  document.addEventListener('DOMContentLoaded', initMobileNav);
})();
