function handleHeaderScroll() {
    const header = document.querySelector(".desktop-header");

    if (window.scrollY > 30) {
        header.classList.add("scrolled");
    } else {
        header.classList.remove("scrolled");
    }
}

function closeAllMenus() {
    clearTimeout(closeTimer);
    $(".header-menus").removeClass("open");
}

$(document).ready(function () {

    $(".big-submenu-eachContent").hide();
    $(".big-submenu-ul-li").hide();

    $(".submenu-card").each(function () {
        const $heading = $(this).find(".submenu-heading").first();
        $heading.addClass('showheading');
        const $items = $heading.nextUntil(
            ".submenu-heading",
            ".big-submenu-ul-li"
        );
        if ($items.length === 0) return;
        $items.show();
        const $firstLi = $items.first();
        $firstLi.addClass("showactive");
        const target = $firstLi.data("target");
        $("#" + target).show();
    });

    let closeTimer = null;

    // OPEN on hover
    $(document).on("mouseenter", ".header-menus", function () {
        clearTimeout(closeTimer);
        $(".header-menus").removeClass("open");
        $(this).addClass("open");
    });

    $(document).on("mouseleave", ".header-menus", function () {
        const $menu = $(this);
        closeTimer = setTimeout(() => {
            $menu.removeClass("open");
        }, 80); 
    });

    // KEEP OPEN on click
    $(document).on("click", ".header-menus > span", function (e) {
        e.stopPropagation();
        const $menu = $(this).closest(".header-menus");
        $(".header-menus").not($menu).removeClass("open");
        $menu.addClass("open");
    });

    // Prevent submenu clicks from closing
    $(document).on("click", ".submenu-card", function (e) {
        e.stopPropagation();
    });

    // CLICK OUTSIDE → CLOSE EVERYTHING + RESET STATE
    $(document).on("click", function (e) {
        if ($(e.target).closest(".header-menus").length) return;
        closeAllMenus();
    });

    // When hovering a heading
    $(document).on("mouseenter", ".submenu-heading", function () {
        $('.submenu-heading').removeClass('showheading');
        $(this).addClass('showheading');
        $(".big-submenu-ul-li").hide().removeClass("showactive");
        $(".big-submenu-eachContent").hide();
        const $items = $(this).nextUntil(".submenu-heading", ".big-submenu-ul-li");
        $items.show();
        const defaultTarget = $(this).data("default");
        $("#" + defaultTarget).show();
        $items
            .filter(`[data-target="${defaultTarget}"]`)
            .addClass("showactive");
    });

    // When hovering a li
    $(document).on("mouseenter", ".big-submenu-ul-li", function () {
        const id = $(this).data("target");
        $(".big-submenu-ul-li").removeClass("showactive");
        $(this).addClass("showactive");
        $(".big-submenu-eachContent").hide();
        $("#" + id).show();
    });


    $(".mob-big-submenu-eachContent").hide();
    $(".no-leftmenu .mob-big-submenu-eachContent").show();
    $(".mob-big-submenu-ul-li").click(function () {
        var id = $(this).data("target"); 
        var $target = $("#" + id);
        if ($target.is(":visible")) {
            $target.slideUp();
            $(this).removeClass('showactive');
        } else {
            $(".mob-big-submenu-eachContent").slideUp();
            $(".mob-big-submenu-ul-li").removeClass('showactive');
            $(".no-leftmenu .mob-big-submenu-eachContent").show(); 
            $target.slideDown();
            $(this).addClass('showactive');
        }
    });


    $('.menu-icon').on('click', function () {
        $('.mobile-menu-holderDiv').addClass('active');
    });

    $('.close-menu-icon').on('click', function () {
        $('.mobile-menu-holderDiv').removeClass('active');
    });


    $('.mob-main-menu').on('click', function () {
        let targetId = $(this).data('target');
        $('.mob-submenu-holderDiv').addClass('active');
        $('.each-mob-submenu-holder').hide();
        $('#' + targetId).show(); 
    });

    $('.close-submenu-icon').on('click', function () {
        $('.mob-submenu-holderDiv').removeClass('active');
    });


    $(document).on('click', function () {
        $('.submenu-card').css({
            opacity: 0,
            top: '100%',
            visibility: 'hidden'
        });
    });

    // Run on initial page load
    window.addEventListener("load", handleHeaderScroll);

    // Run on scroll
    window.addEventListener("scroll", handleHeaderScroll);

});