// Theme Toggle (function() { const toggle = document.getElementById('theme-toggle'); const iconLight = document.getElementById('theme-icon-light'); const iconDark = document.getElementById('theme-icon-dark'); const html = document.documentElement; // Get initial theme function getTheme() { const saved = localStorage.getItem('theme'); if (saved) return saved; if (window.matchMedia('(prefers-color-scheme: dark)').matches) return 'dark'; return 'light'; } // Apply theme function applyTheme(theme) { html.setAttribute('data-theme', theme); localStorage.setItem('theme', theme); updateIcons(theme); } // Update icons function updateIcons(theme) { if (theme === 'dark') { iconLight?.classList.remove('hidden'); iconDark?.classList.add('hidden'); } else { iconLight?.classList.add('hidden'); iconDark?.classList.remove('hidden'); } } // Initialize applyTheme(getTheme()); // Toggle handler toggle?.addEventListener('click', () => { const current = html.getAttribute('data-theme'); applyTheme(current === 'dark' ? 'light' : 'dark'); }); })(); // Mobile Menu (function() { const menuToggle = document.getElementById('mobile-menu-toggle'); const mobileMenu = document.getElementById('mobile-menu'); menuToggle?.addEventListener('click', () => { mobileMenu?.classList.toggle('hidden'); }); })(); // Search (function() { const searchToggle = document.getElementById('search-toggle'); const searchModal = document.getElementById('search-modal'); const searchBackdrop = document.getElementById('search-backdrop'); const searchInput = document.getElementById('search-input'); const searchResults = document.getElementById('search-results'); const searchCount = document.getElementById('search-count'); let fuse = null; let searchIndex = null; // Load search index async function loadSearchIndex() { if (searchIndex) return searchIndex; try { const response = await fetch('/index.json'); searchIndex = await response.json(); fuse = new Fuse(searchIndex, { keys: ['title', 'content', 'tags'], threshold: 0.3, includeMatches: true, }); return searchIndex; } catch (err) { console.error('Failed to load search index:', err); return []; } } // Open search function openSearch() { searchModal?.classList.remove('hidden'); document.body.style.overflow = 'hidden'; searchInput?.focus(); loadSearchIndex(); } // Close search function closeSearch() { searchModal?.classList.add('hidden'); document.body.style.overflow = ''; if (searchInput) searchInput.value = ''; searchResults!.innerHTML = '