<!-- ThemeToggle — Floating sun/moon toggle, bottom-right corner -->
<script>
  import { browser } from '$app/environment';

  let isDark = false;

  $: if (browser) {
    isDark = document.documentElement.getAttribute('data-theme') === 'dark';
  }

  function toggle() {
    isDark = !isDark;
    const t = isDark ? 'dark' : 'light';
    document.documentElement.setAttribute('data-theme', t);
    try { localStorage.setItem('mac_theme', t); } catch {}
  }
</script>

<button
  class="theme-toggle"
  on:click={toggle}
  title={isDark ? 'Switch to Light Mode' : 'Switch to Dark Mode'}
  aria-label="Toggle theme"
>
  <div class="icon-wrap" class:dark={isDark}>
    <!-- Sun -->
    <svg class="sun" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
      <circle cx="12" cy="12" r="5"/>
      <line x1="12" y1="1" x2="12" y2="3"/>
      <line x1="12" y1="21" x2="12" y2="23"/>
      <line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/>
      <line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/>
      <line x1="1" y1="12" x2="3" y2="12"/>
      <line x1="21" y1="12" x2="23" y2="12"/>
      <line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/>
      <line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/>
    </svg>
    <!-- Moon -->
    <svg class="moon" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
      <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>
    </svg>
  </div>
</button>

<style>
  .theme-toggle {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 100;
    width: 44px;
    height: 44px;
    border: 1px solid var(--border, rgba(0,0,0,0.12));
    border-radius: 50%;
    background: var(--surface, #fff);
    box-shadow: 0 2px 12px rgba(0,0,0,0.12);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s ease, box-shadow 0.2s ease, background-color 0.35s ease;
    outline: none;
    padding: 0;
  }
  .theme-toggle:hover {
    transform: scale(1.08);
    box-shadow: 0 4px 20px rgba(217,116,73,0.25);
  }
  .theme-toggle:active {
    transform: scale(0.95);
  }

  .icon-wrap {
    position: relative;
    width: 18px;
    height: 18px;
  }

  .sun, .moon {
    position: absolute;
    inset: 0;
    transition: opacity 0.35s ease, transform 0.35s ease;
  }

  /* Light mode: show sun, hide moon */
  .sun {
    opacity: 1;
    transform: rotate(0deg) scale(1);
    color: var(--accent, #D97449);
  }
  .moon {
    opacity: 0;
    transform: rotate(-90deg) scale(0.6);
    color: var(--accent, #D97449);
  }

  /* Dark mode: show moon, hide sun */
  .icon-wrap.dark .sun {
    opacity: 0;
    transform: rotate(90deg) scale(0.6);
  }
  .icon-wrap.dark .moon {
    opacity: 1;
    transform: rotate(0deg) scale(1);
  }
</style>
