document.addEventListener('DOMContentLoaded', function() {
    // Sidebar Toggle Functionality
    const toggleBtn = document.getElementById('sidebar-toggle');
    const sidebar = document.getElementById('sidebar');
    const mainWrapper = document.getElementById('main-wrapper');

    if (toggleBtn) {
        toggleBtn.addEventListener('click', function() {
            if (window.innerWidth > 768) {
                // Desktop behavior
                sidebar.classList.toggle('collapsed');
                mainWrapper.classList.toggle('expanded');
            } else {
                // Mobile behavior
                sidebar.classList.toggle('mobile-open');
            }
        });
    }

    // Add active class to current nav item based on URL
    const currentLocation = window.location.href;
    const navLinks = document.querySelectorAll('.nav-link');
    
    navLinks.forEach(link => {
        if (link.href === currentLocation) {
            link.classList.add('active');
            // If it's inside a dropdown, open the parent
            const parentDropdown = link.closest('.nav-item.has-dropdown');
            if (parentDropdown) {
                parentDropdown.classList.add('open');
            }
        }
    });

    // Dropdown Toggle
    const dropdownToggles = document.querySelectorAll('.has-dropdown > .nav-link');
    dropdownToggles.forEach(toggle => {
        toggle.addEventListener('click', function(e) {
            e.preventDefault();
            const parentItem = this.parentElement;
            
            // Close other open dropdowns (accordion style)
            document.querySelectorAll('.nav-item.has-dropdown.open').forEach(item => {
                if (item !== parentItem) {
                    item.classList.remove('open');
                }
            });

            parentItem.classList.toggle('open');
        });
    });
});
