// QR Rapido - Performance Optimizations
// Moved from inline scripts to improve LCP and reduce render-blocking

// Fallback inline para garantir funcionamento do theme toggle
document.addEventListener('DOMContentLoaded', function() {
    // Theme toggle functionality
    const btn = document.getElementById('theme-toggle');
    if (btn) {
        btn.onclick = function() {
            const html = document.documentElement;
            const current = html.getAttribute('data-theme') || 'light';
            const newTheme = current === 'light' ? 'dark' : 'light';
            html.setAttribute('data-theme', newTheme);

            // Save preference to localStorage
            localStorage.setItem('theme-preference', newTheme);

            const icon = document.getElementById('theme-icon');
            const text = document.getElementById('theme-text');
            if (icon) {
                icon.className = newTheme === 'dark' ? 'fas fa-moon' : 'fas fa-sun';
            }
            if (text) {
                text.textContent = newTheme === 'dark' ? 'Escuro' : 'Claro';
            }
        };
    }

    // Load saved theme preference on page load
    const savedTheme = localStorage.getItem('theme-preference');
    if (savedTheme) {
        document.documentElement.setAttribute('data-theme', savedTheme);
        const icon = document.getElementById('theme-icon');
        const text = document.getElementById('theme-text');
        if (icon) {
            icon.className = savedTheme === 'dark' ? 'fas fa-moon' : 'fas fa-sun';
        }
        if (text) {
            text.textContent = savedTheme === 'dark' ? 'Escuro' : 'Claro';
        }
    }
});

// Optimize CSS loading fallback
document.addEventListener('DOMContentLoaded', function() {
    // Ensure non-critical CSS loads properly
    const printLinks = document.querySelectorAll('link[media="print"]');
    printLinks.forEach(link => {
        if (link.onload === null) {
            link.media = 'all';
        }
    });
});