// SIMPLIFIED Mobile Menu - Guaranteed to Work
(function() {
  'use strict';

  console.log('🚀 Main.js loaded successfully');

  // Wait for DOM
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }

  function init() {
    console.log('✅ DOM loaded, initializing menu');
    
    // Set current year in footer
    const yearEl = document.querySelector("#year");
    if (yearEl) {
      yearEl.textContent = new Date().getFullYear();
    }

    // ===== MOBILE MENU - SIMPLIFIED VERSION =====
    const navToggle = document.querySelector(".nav-toggle");
    const navList = document.querySelector("#primary-menu");
    
    console.log('🔍 Toggle button:', navToggle);
    console.log('🔍 Menu list:', navList);
    
    if (!navToggle || !navList) {
      console.error('❌ ERROR: Menu elements not found!');
      console.error('Toggle:', navToggle, 'Menu:', navList);
      return;
    }

    console.log('✅ Menu elements found, adding click handler');

    // Simple toggle function
    function toggleMenu(e) {
      if (e) e.preventDefault();
      if (e) e.stopPropagation();
      
      const isOpen = navList.classList.contains("open");
      console.log('📱 Menu toggle clicked! Currently:', isOpen ? 'OPEN' : 'CLOSED');
      
      if (isOpen) {
        // Close menu
        navList.classList.remove("open");
        navToggle.classList.remove("active");
        navToggle.setAttribute("aria-expanded", "false");
        document.body.style.overflow = "";
        console.log('🔒 Menu CLOSED');
      } else {
        // Open menu
        navList.classList.add("open");
        navToggle.classList.add("active");
        navToggle.setAttribute("aria-expanded", "true");
        document.body.style.overflow = "hidden";
        console.log('📂 Menu OPENED');
      }
    }

    // Add click listener
    navToggle.addEventListener("click", toggleMenu, false);
    
    // Also respond to touch events on mobile
    navToggle.addEventListener("touchend", function(e) {
      e.preventDefault();
      toggleMenu(e);
    }, false);

    console.log('✅ Click handlers attached');

    // Close menu when clicking outside
    document.addEventListener("click", function(e) {
      if (!navList.classList.contains("open")) return;
      
      const clickedInside = navList.contains(e.target) || navToggle.contains(e.target);
      if (!clickedInside) {
        toggleMenu();
        console.log('🔒 Menu closed (clicked outside)');
      }
    });

    // Close menu when clicking a link
    const navLinks = navList.querySelectorAll("a");
    navLinks.forEach(function(link) {
      link.addEventListener("click", function(e) {
        // Only close if it's a real link (not # for dropdowns)
        const href = link.getAttribute("href");
        if (href && href !== "#" && href !== "") {
          toggleMenu();
          console.log('🔒 Menu closed (link clicked)');
        }
      });
    });

    // Handle submenu toggles on mobile
    const groupLinks = navList.querySelectorAll("li.group > a");
    groupLinks.forEach(function(link) {
      link.addEventListener("click", function(e) {
        // On mobile, toggle submenu
        if (window.innerWidth < 768) {
          e.preventDefault();
          const parent = link.parentElement;
          const wasOpen = parent.classList.contains("submenu-open");
          
          // Close all other submenus
          document.querySelectorAll(".submenu-open").forEach(function(item) {
            item.classList.remove("submenu-open");
          });
          
          // Toggle this submenu
          if (!wasOpen) {
            parent.classList.add("submenu-open");
          }
        }
      });
    });

    // Close menu on window resize to desktop
    window.addEventListener("resize", function() {
      if (window.innerWidth >= 768 && navList.classList.contains("open")) {
        toggleMenu();
        console.log('🔒 Menu closed (resized to desktop)');
      }
    });

    console.log('✅ Mobile menu initialized successfully');
  }

})();
