/* MENU ONLY FINAL FIX - isolated hamburger open/close */
(function(){
  function ready(fn){
    if(document.readyState === 'loading') document.addEventListener('DOMContentLoaded', fn);
    else fn();
  }
  ready(function(){
    var btn = document.getElementById('menuToggle');
    var menu = document.getElementById('mobileMenu') || document.querySelector('.mobile-menu');
    if(!btn || !menu) return;

    // Remove old duplicated listeners without changing markup/style elsewhere.
    var fresh = btn.cloneNode(true);
    btn.parentNode.replaceChild(fresh, btn);
    btn = fresh;

    btn.setAttribute('aria-controls','mobileMenu');
    btn.setAttribute('aria-expanded','false');
    btn.setAttribute('type','button');
    if(!btn.getAttribute('aria-label')) btn.setAttribute('aria-label','Menu');

    function isOpen(){
      return menu.classList.contains('show') || menu.classList.contains('open') || menu.classList.contains('is-open');
    }
    function openMenu(){
      menu.classList.add('show','open','is-open');
      document.body.classList.add('menu-open');
      btn.textContent = '×';
      btn.setAttribute('aria-expanded','true');
      btn.setAttribute('aria-label','Κλείσιμο μενού');
    }
    function closeMenu(){
      menu.classList.remove('show','open','is-open');
      document.body.classList.remove('menu-open');
      btn.textContent = '☰';
      btn.setAttribute('aria-expanded','false');
      btn.setAttribute('aria-label','Ανοιγμα μενού');
    }
    function toggleMenu(e){
      if(e){ e.preventDefault(); e.stopPropagation(); }
      isOpen() ? closeMenu() : openMenu();
    }

    btn.addEventListener('click', toggleMenu, true);
    btn.addEventListener('touchend', toggleMenu, {capture:true, passive:false});

    var close = document.getElementById('closeMenu') || menu.querySelector('.close-menu');
    if(close){
      close.addEventListener('click', function(e){ e.preventDefault(); e.stopPropagation(); closeMenu(); }, true);
    }
    menu.querySelectorAll('a').forEach(function(a){
      a.addEventListener('click', closeMenu, false);
    });
    menu.addEventListener('click', function(e){
      if(e.target === menu) closeMenu();
    }, false);
    document.addEventListener('keydown', function(e){
      if(e.key === 'Escape') closeMenu();
    }, false);
  });
})();
