(function () {
    const mobileQuery = window.matchMedia('(max-width: 768px)');

    document.querySelectorAll('footer .footer-toggle-btn').forEach(function (button) {
        button.setAttribute('aria-expanded', button.classList.contains('expanded') ? 'true' : 'false');

        button.addEventListener('click', function (event) {
            if (!mobileQuery.matches) return;

            const content = button.nextElementSibling;
            if (!content || !content.classList.contains('footer-col-content')) return;

            event.preventDefault();
            event.stopImmediatePropagation();

            const expanded = !button.classList.contains('expanded');
            button.classList.toggle('expanded', expanded);
            button.setAttribute('aria-expanded', expanded ? 'true' : 'false');
            content.classList.toggle('expanded', expanded);
        }, true);
    });

    const resetOnDesktop = function () {
        if (mobileQuery.matches) return;

        document.querySelectorAll('footer .footer-toggle-btn').forEach(function (button) {
            button.classList.remove('expanded');
            button.setAttribute('aria-expanded', 'false');
            const content = button.nextElementSibling;
            if (content && content.classList.contains('footer-col-content')) {
                content.classList.remove('expanded');
            }
        });
    };

    if (mobileQuery.addEventListener) {
        mobileQuery.addEventListener('change', resetOnDesktop);
    } else {
        mobileQuery.addListener(resetOnDesktop);
    }
})();
