document.addEventListener('DOMContentLoaded', function () {
    const searchToggle = document.getElementById('topnavSearchToggle');
    const searchModal = document.getElementById('searchOverlayModal');
    const closeSearchBtn = document.getElementById('closeSearchOverlay');
    const searchBackdrop = document.getElementById('searchOverlayBackdrop');

    if (searchToggle && searchModal) {
        searchToggle.addEventListener('click', function (e) {
            e.preventDefault();
            searchModal.classList.add('active');
        });
    }
    if (closeSearchBtn && searchModal) {
        closeSearchBtn.addEventListener('click', function () {
            searchModal.classList.remove('active');
        });
    }
    if (searchBackdrop && searchModal) {
        searchBackdrop.addEventListener('click', function () {
            searchModal.classList.remove('active');
        });
    }

    const mobileNav = document.getElementById('headerMobileNav');
    if (mobileNav && window.bootstrap && window.bootstrap.Collapse) {
        mobileNav.querySelectorAll('a.dropdown-item').forEach(function (a) {
            a.addEventListener('click', function () {
                const instance =
                    window.bootstrap.Collapse.getInstance(mobileNav) ||
                    new window.bootstrap.Collapse(mobileNav, { toggle: false });
                instance.hide();
            });
        });
    }

    setTimeout(function () {
        const loader = document.getElementById('loader');
        if (loader) loader.style.display = 'none';
    }, 400);

    document.querySelectorAll('.dynamic-year').forEach(function (el) {
        el.textContent = new Date().getFullYear();
    });
});
