    <script>
    // ===== THEME SWITCHER COM API =====
    (function() {
        const themeButtons = document.querySelectorAll('.theme-btn');
        const html = document.documentElement;

        // Função para aplicar tema visualmente
        function applyTheme(theme, updateButtons = true) {
            if (theme === 'auto') {
                const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
                html.setAttribute('data-theme', prefersDark ? 'dark' : 'light');
            } else {
                html.setAttribute('data-theme', theme);
            }

            // Atualizar botões ativos
            if (updateButtons) {
                themeButtons.forEach(btn => {
                    btn.classList.toggle('active', btn.dataset.theme === theme);
                });
            }
        }

        // Função para salvar tema via API
        async function saveThemeToAPI(theme) {
            try {
                const response = await fetch('/pages/superadmin/api/theme.php?action=set&theme=' + theme, {
                    method: 'POST',
                    credentials: 'same-origin'
                });
                const data = await response.json();
                if (!data.success) {
                    console.error('Erro ao salvar tema:', data.error);
                }
            } catch (error) {
                console.error('Erro ao salvar tema:', error);
            }
        }

        // Carregar tema do banco (já está no HTML via PHP)
        const dbTheme = window.superAdminThemeFromDB || 'light';
        applyTheme(dbTheme);

        // Event listeners para botões de tema
        themeButtons.forEach(btn => {
            btn.addEventListener('click', () => {
                const newTheme = btn.dataset.theme;
                applyTheme(newTheme);
                saveThemeToAPI(newTheme);
            });
        });

        // Listener para mudanças no sistema (quando modo auto está ativo)
        window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
            const currentTheme = window.superAdminThemeFromDB;
            if (currentTheme === 'auto') {
                html.setAttribute('data-theme', e.matches ? 'dark' : 'light');
            }
        });
    })();

    // Mobile Menu Toggle
    document.addEventListener('DOMContentLoaded', function() {
        const mobileMenuBtn = document.getElementById('mobileMenuBtn');
        const sidebar = document.querySelector('.sidebar');
        const sidebarOverlay = document.getElementById('sidebarOverlay');

        if (mobileMenuBtn && sidebar) {
            mobileMenuBtn.addEventListener('click', function() {
                sidebar.classList.toggle('open');
                if (sidebarOverlay) {
                    sidebarOverlay.classList.toggle('active');
                }
            });
        }

        if (sidebarOverlay) {
            sidebarOverlay.addEventListener('click', function() {
                sidebar.classList.remove('open');
                sidebarOverlay.classList.remove('active');
            });
        }

        // Close sidebar when clicking on a nav item (mobile)
        const navItems = document.querySelectorAll('.sidebar .nav-item');
        navItems.forEach(function(item) {
            item.addEventListener('click', function() {
                if (window.innerWidth <= 768) {
                    sidebar.classList.remove('open');
                    if (sidebarOverlay) {
                        sidebarOverlay.classList.remove('active');
                    }
                }
            });
        });

        // Handle resize
        window.addEventListener('resize', function() {
            if (window.innerWidth > 768) {
                sidebar.classList.remove('open');
                if (sidebarOverlay) {
                    sidebarOverlay.classList.remove('active');
                }
            }
        });
    });
    </script>
</body>
</html>
