Source: modules/theme.js

/**
 * Theme management module for dark/light mode.
 * @module modules/theme
 */

/**
 * Initializes the theme based on user preference or system setting.
 * Sets up the theme toggle button to switch between dark and light modes.
 * Stores the user's preference in localStorage.
 */
export function initTheme() {
  const isDarkMode =
    localStorage.theme === 'dark' ||
    (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches);

  // Set initial theme based on preference or system setting
  document.documentElement.classList.toggle('dark', isDarkMode);

  const themeToggle = document.getElementById('theme-toggle');
  if (themeToggle) {
    themeToggle.addEventListener('click', () => {
      document.documentElement.classList.toggle('dark');
      localStorage.theme = document.documentElement.classList.contains('dark') ? 'dark' : 'light';
    });
  }
}