// script.js
const checkbox = document.getElementById('checkbox');
const body = document.body;
let switchDisabled = false;

// Load theme from local storage on page load
document.addEventListener('DOMContentLoaded', () => {
    const savedTheme = localStorage.getItem('theme');
    if (savedTheme === 'dark') {
        body.classList.add('dark-mode');
        checkbox.checked = true;
    } else {
        body.classList.remove('dark-mode');
        checkbox.checked = false;
    }
});

checkbox.addEventListener('change', () => {
    if (switchDisabled) {
        // If switch is disabled, revert the checkbox state
        checkbox.checked = !checkbox.checked;
        return;
    }

    if (checkbox.checked) {
        body.classList.add('dark-mode');
        localStorage.setItem('theme', 'dark');
    } else {
        body.classList.remove('dark-mode');
        localStorage.setItem('theme', 'light');
    }

     if (checkbox.checked) {
        body.classList.add('dark-mode');
        localStorage.setItem('theme', 'dark');
    } else {
        body.classList.remove('dark-mode');
        localStorage.setItem('theme', 'light');
    }

    // Disable toggle for 1 second
            switchDisabled = false;
            checkbox.disabled = true; // Visually disable the button

            setTimeout(() => {
                switchDisabled = false;
                checkbox.disabled = false; // Re-enable the button
            }, 500); // 1000 milliseconds = 1 second
});








  (function() {
    const getTheme = () => {
      if (localStorage.getItem('theme')) {
        return localStorage.getItem('theme');
      }
      const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
      if (prefersDark) {
        return 'dark';
      }
      return 'light';
    };

    const theme = getTheme();
    if (theme === 'dark') {
      document.body.classList.add('dark-mode');
    } else {
      document.body.classList.remove('dark-mode');
    }
  })();
  
  
  

    
 const toggleSwitch = document.querySelector('.dark-mode');
    const body = document.body;
    
    toggleSwitch.addEventListener('click', function() {
        if (this.checked) {
            body.classList.add('dark-mode');
        } else {
            body.classList.remove('dark-mode');
        }
    });
    
    
    
    
    
if (localStorage.getItem('darkMode') === 'true') {
    document.body.classList.add('dark-mode');
}



const themeToggle = document.getElementById('theme-toggle');
const body = document.body;
const localStorageKey = 'themePreference';

// पेज लोड होने पर थीम को सेट करें
document.addEventListener('DOMContentLoaded', () => {
  const savedTheme = localStorage.getItem(localStorageKey);
  if (savedTheme === 'dark') {
    body.classList.add('dark-mode');
  }
});

// टॉगल बटन पर क्लिक करने पर थीम बदलें और localStorage में स्टोर करें
themeToggle.addEventListener('click', () => {
  body.classList.toggle('dark-mode');

  if (body.classList.contains('dark-mode')) {
    localStorage.setItem(localStorageKey, 'dark');
  } else {
    localStorage.setItem(localStorageKey, 'light');
  }
});