File size: 1,842 Bytes
8a6f58d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
 * theme.js - Light/Dark Mode Logic
 * Handles theme switching, persistence, and UI synchronization.
 */

(function () {
    const THEME_KEY = 'ai_detector_theme';

    // 1. Immediate Theme Application (to <html> to prevent flash)
    const savedTheme = localStorage.getItem(THEME_KEY) || 'light';
    if (savedTheme === 'dark') {
        document.documentElement.classList.add('dark');
        // We'll add it to body as well once DOM is ready
    }

    function initThemeToggle() {
        const body = document.body;
        const html = document.documentElement;

        // Sync body class with html/saved state
        if (savedTheme === 'dark') {
            body.classList.add('dark');
        }

        const toggleBtn = document.getElementById('themeToggle');
        if (toggleBtn) {
            const icon = toggleBtn.querySelector('i');

            // Sync initial icon
            if (icon && body.classList.contains('dark')) {
                icon.classList.remove('fa-moon');
                icon.classList.add('fa-sun');
            }

            toggleBtn.addEventListener('click', () => {
                const isDark = body.classList.toggle('dark');
                html.classList.toggle('dark', isDark);
                localStorage.setItem(THEME_KEY, isDark ? 'dark' : 'light');

                // Toggle icon
                if (icon) {
                    if (isDark) {
                        icon.classList.replace('fa-moon', 'fa-sun');
                    } else {
                        icon.classList.replace('fa-sun', 'fa-moon');
                    }
                }
            });
        }
    }

    // Run when DOM is ready
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initThemeToggle);
    } else {
        initThemeToggle();
    }
})();