---
// ThemeToggler.astro
// A component to toggle between light and dark themes
---

<button id="theme-toggle" aria-label="Toggle dark mode" class="theme-toggle">
  <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="sun-icon">
    <circle cx="12" cy="12" r="5"></circle>
    <line x1="12" y1="1" x2="12" y2="3"></line>
    <line x1="12" y1="21" x2="12" y2="23"></line>
    <line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
    <line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
    <line x1="1" y1="12" x2="3" y2="12"></line>
    <line x1="21" y1="12" x2="23" y2="12"></line>
    <line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
    <line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
  </svg>
  <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="moon-icon">
    <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path>
  </svg>
</button>

<style>
  .theme-toggle {
    background: none;
    border: none;
    padding: 0.25rem;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    cursor: pointer;
    transition: color 0.3s ease, background-color 0.3s ease;
    position: relative;
    width: 34px;
    height: 34px;
  }
  
  .theme-toggle:hover {
    color: var(--text-primary);
    background: rgba(255, 255, 255, 0.1);
  }
  
  .sun-icon, .moon-icon {
    position: absolute;
    transition: transform 0.5s ease, opacity 0.5s ease;
  }
  
  :root:not(.light-mode) .sun-icon {
    opacity: 0;
    transform: rotate(-90deg);
  }
  
  :root:not(.light-mode) .moon-icon {
    opacity: 1;
    transform: rotate(0);
  }
  
  :root.light-mode .sun-icon {
    opacity: 1;
    transform: rotate(0);
  }
  
  :root.light-mode .moon-icon {
    opacity: 0;
    transform: rotate(90deg);
  }
</style>

<script>
  // Theme toggling logic
  document.addEventListener('DOMContentLoaded', () => {
    const themeToggle = document.getElementById('theme-toggle');
    
    // Check for saved theme preference or prefer-color-scheme
    const getInitialTheme = () => {
      const savedTheme = localStorage.getItem('theme');
      if (savedTheme) {
        return savedTheme === 'light';
      }
      // If no saved preference, check system preference
      return window.matchMedia('(prefers-color-scheme: light)').matches;
    };
    
    // Function to set theme
    const setTheme = (isLight) => {
      if (isLight) {
        document.documentElement.classList.add('light-mode');
        localStorage.setItem('theme', 'light');
      } else {
        document.documentElement.classList.remove('light-mode');
        localStorage.setItem('theme', 'dark');
      }
    };
    
    // Apply initial theme
    setTheme(getInitialTheme());
    
    // Theme toggle click handler
    themeToggle?.addEventListener('click', () => {
      const isCurrentlyLight = document.documentElement.classList.contains('light-mode');
      setTheme(!isCurrentlyLight);
    });
  });
</script>