(function () {
    function applyFooterMenuColumns() {
        const perColRaw = (window.SilvanusFooter && window.SilvanusFooter.menuItemsPerColumn) || 5;
        const perCol = Math.max(1, parseInt(perColRaw, 10) || 5);

        // Bootstrap lg breakpoint ~= 992px
        const isDesktop = window.matchMedia("(min-width: 992px)").matches;

        // Target only footer menus. We keep markup unchanged (menu.twig output).
        const menus = document.querySelectorAll(".site-footer__col--menu ul.list-column");
        menus.forEach((ul) => {
            const items = ul.querySelectorAll(":scope > li").length;

            if (!isDesktop) {
                ul.style.columnCount = "1";
                return;
            }

            if (items <= perCol) {
                ul.style.columnCount = "1";
                return;
            }

            const cols = Math.ceil(items / perCol);
            ul.style.columnCount = String(cols);
        });
    }

    document.addEventListener("DOMContentLoaded", applyFooterMenuColumns);
    window.addEventListener("resize", applyFooterMenuColumns);
})();