document.addEventListener('DOMContentLoaded', (event) => {
    const sidebar = document.querySelector('.sidebar');
    const submenuLinks = document.querySelectorAll('.sidebar a');

    submenuLinks.forEach(link => {
        link.addEventListener('click', (event) => {
            if (link.nextElementSibling && link.nextElementSibling.classList.contains('submenu')) {
                event.preventDefault();
                toggleSubmenu(event, link.nextElementSibling);
                adjustSidebarHeight();
            }
        });
    });

    function toggleSubmenu(event, submenu) {
        event.preventDefault();
        const isVisible = submenu.style.display === 'flex';
        submenu.style.display = isVisible ? 'none' : 'flex';
        adjustSidebarHeight();
    }

    function adjustSidebarHeight() {
        const activeSubmenus = sidebar.querySelectorAll('.submenu[style="display: flex;"]');
        let totalHeight = 0;

        activeSubmenus.forEach(submenu => {
            totalHeight += submenu.scrollHeight;
        });

        sidebar.style.height = `calc(100vh + ${totalHeight}px)`;
    }
});
