/**
 * Theme Toggler Logic
 * Handles dark/light mode switches, persistence, and application.
 */

// Function to apply theme
function applyTheme(themeName) {
    document.documentElement.setAttribute('data-theme', themeName);
    localStorage.setItem('theme', themeName);
    const toggleCheckbox = document.getElementById('theme-toggle-input');
    if (toggleCheckbox) {
        toggleCheckbox.checked = (themeName === 'dark');
    }
}

// Initial theme setup on page load
document.addEventListener('DOMContentLoaded', () => {
    const currentTheme = localStorage.getItem('theme') || 'light';
    applyTheme(currentTheme);

    const toggleCheckbox = document.getElementById('theme-toggle-input');
    if (toggleCheckbox) {
        toggleCheckbox.addEventListener('change', (e) => {
            const theme = e.target.checked ? 'dark' : 'light';
            applyTheme(theme);
        });
    }
});
