document.addEventListener('DOMContentLoaded', function() {
    // Elementi
    const menuToggle = document.getElementById('mobile-menu-toggle');
    const menuDrawer = document.getElementById('mobile-drawer');
    const menuOverlay = document.getElementById('mobile-menu-overlay');
    
    // Otvaranje menija
    if (menuToggle) {
        menuToggle.addEventListener('click', function(e) {
            e.preventDefault();
            console.log("Menu button clicked");
            menuDrawer.classList.add('active');
            menuOverlay.classList.add('active');
            document.body.classList.add('menu-open');
        });
    } else {
        console.error("Menu toggle button not found! Check if ID 'mobile-menu-toggle' exists.");
    }
    
    // Zatvaranje menija klikom na overlay
    if (menuOverlay) {
        menuOverlay.addEventListener('click', function() {
            closeMenu();
        });
    } else {
        console.error("Menu overlay not found! Check if ID 'mobile-menu-overlay' exists.");
    }
    
    // Dodavanje X dugmeta za zatvaranje ako ne postoji
    if (menuDrawer) {
        // Provjera da li već postoji dugme za zatvaranje
        let closeButton = menuDrawer.querySelector('.menu-close');
        
        if (!closeButton) {
            closeButton = document.createElement('button');
            closeButton.className = 'menu-close';
            closeButton.innerHTML = '&times;';
            closeButton.style.cssText = `
                position: absolute;
                top: 10px;
                right: 10px;
                background: none;
                border: none;
                font-size: 24px;
                color: #333;
                cursor: pointer;
                z-index: 10;
            `;
            
            if (menuDrawer.firstChild) {
                menuDrawer.insertBefore(closeButton, menuDrawer.firstChild);
            } else {
                menuDrawer.appendChild(closeButton);
            }
            
            closeButton.addEventListener('click', closeMenu);
        }
    } else {
        console.error("Mobile drawer not found! Check if ID 'mobile-drawer' exists.");
    }
    
    // Zatvaranje menija na escape tipku
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape' && menuDrawer && menuDrawer.classList.contains('active')) {
            closeMenu();
        }
    });
    
    // Funkcija za zatvaranje menija
    function closeMenu() {
        if (menuDrawer) menuDrawer.classList.remove('active');
        if (menuOverlay) menuOverlay.classList.remove('active');
        document.body.classList.remove('menu-open');
    }
});