$(document).ready(function(){
    $("#menu-toggle").click(function(e) {
        e.preventDefault();
        $("#wrapper").toggleClass("toggled");
    });

    $(".overlay").click(function() {
        $("#wrapper").removeClass("toggled");
    });

    // Theme Toggle Functionality
    const themeToggle = $('#theme-toggle');
    const themeIcon = $('#theme-icon');
    const html = $('html');

    // Load saved theme or default to light
    const savedTheme = localStorage.getItem('theme') || 'light';
    setTheme(savedTheme);

    themeToggle.on('click', function() {
        const currentTheme = html.attr('data-bs-theme') || 'light';
        const newTheme = currentTheme === 'light' ? 'dark' : 'light';
        setTheme(newTheme);
        localStorage.setItem('theme', newTheme);
    });

    function setTheme(theme) {
        html.attr('data-bs-theme', theme);
        
        // Update icon
        if (theme === 'dark') {
            themeIcon.removeClass('bi-sun-fill').addClass('bi-moon-fill');
            themeToggle.attr('title', 'Cambiar a tema claro');
        } else {
            themeIcon.removeClass('bi-moon-fill').addClass('bi-sun-fill');
            themeToggle.attr('title', 'Cambiar a tema oscuro');
        }
    }

    // Check system preference on first load
    if (!localStorage.getItem('theme')) {
        const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
        setTheme(prefersDark ? 'dark' : 'light');
        localStorage.setItem('theme', prefersDark ? 'dark' : 'light');
    }
});
