<script lang="ts">
  import '../app.css';
  import SearchBar from '$lib/components/SearchBar.svelte';
  import { subscriptions } from '$lib/stores/subscriptions';
  import { playlists } from '$lib/stores/playlists';
  import { onMount } from 'svelte';
  import { browser } from '$app/environment';

  let { children } = $props();
  let theme = $state<'dark' | 'light'>('dark');

  onMount(() => {
    subscriptions.init();
    playlists.init();

    const stored = localStorage.getItem('actualyt-theme');
    if (stored === 'light' || stored === 'dark') {
      theme = stored;
    }
  });

  function toggleTheme() {
    theme = theme === 'dark' ? 'light' : 'dark';
    if (browser) {
      localStorage.setItem('actualyt-theme', theme);
    }
  }
</script>

<svelte:head>
  <title>ActualYT</title>
</svelte:head>

<div class="app" data-theme={theme}>
  <header class="header">
    <div class="header-content container">
      <a href="/" class="logo">ActualYT</a>
      <SearchBar />
      <nav class="nav">
        <a href="/subscriptions" class="nav-link">Subscriptions</a>
        <a href="/playlists" class="nav-link">Playlists</a>
        <button class="theme-toggle" onclick={toggleTheme} aria-label="Toggle theme">
          {#if theme === 'dark'}
            <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
              <path d="M12 7c-2.76 0-5 2.24-5 5s2.24 5 5 5 5-2.24 5-5-2.24-5-5-5zM2 13h2c.55 0 1-.45 1-1s-.45-1-1-1H2c-.55 0-1 .45-1 1s.45 1 1 1zm18 0h2c.55 0 1-.45 1-1s-.45-1-1-1h-2c-.55 0-1 .45-1 1s.45 1 1 1zM11 2v2c0 .55.45 1 1 1s1-.45 1-1V2c0-.55-.45-1-1-1s-1 .45-1 1zm0 18v2c0 .55.45 1 1 1s1-.45 1-1v-2c0-.55-.45-1-1-1s-1 .45-1 1zM5.99 4.58a.996.996 0 0 0-1.41 0 .996.996 0 0 0 0 1.41l1.06 1.06c.39.39 1.03.39 1.41 0s.39-1.03 0-1.41L5.99 4.58zm12.37 12.37a.996.996 0 0 0-1.41 0 .996.996 0 0 0 0 1.41l1.06 1.06c.39.39 1.03.39 1.41 0a.996.996 0 0 0 0-1.41l-1.06-1.06zm1.06-10.96a.996.996 0 0 0 0-1.41.996.996 0 0 0-1.41 0l-1.06 1.06c-.39.39-.39 1.03 0 1.41s1.03.39 1.41 0l1.06-1.06zM7.05 18.36a.996.996 0 0 0 0-1.41.996.996 0 0 0-1.41 0l-1.06 1.06c-.39.39-.39 1.03 0 1.41s1.03.39 1.41 0l1.06-1.06z"/>
            </svg>
          {:else}
            <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
              <path d="M12 3a9 9 0 1 0 9 9c0-.46-.04-.92-.1-1.36a5.389 5.389 0 0 1-4.4 2.26 5.403 5.403 0 0 1-3.14-9.8c-.44-.06-.9-.1-1.36-.1z"/>
            </svg>
          {/if}
        </button>
      </nav>
    </div>
  </header>

  <main class="main">
    {@render children()}
  </main>
</div>

<style>
  .app {
    min-height: 100vh;
    background: var(--bg-primary);
  }

  .header {
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border-color);
  }

  .header-content {
    display: flex;
    align-items: center;
    gap: 2rem;
    padding-top: 0.75rem;
    padding-bottom: 0.75rem;
  }

  .logo {
    font-size: 1.25rem;
    font-weight: 700;
    text-decoration: none;
    color: var(--accent-color);
    flex-shrink: 0;
  }

  .nav {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-left: auto;
  }

  .nav-link {
    text-decoration: none;
    color: var(--text-secondary);
    font-size: 0.9rem;
    padding: 0.5rem;
    border-radius: 4px;
    transition: color 0.15s ease;
  }

  .nav-link:hover {
    color: var(--text-primary);
  }

  .theme-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease;
  }

  .theme-toggle:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
  }

  .main {
    padding: 2rem 0;
  }

  @media (max-width: 768px) {
    .header-content {
      flex-wrap: wrap;
      gap: 1rem;
    }

    .nav {
      order: -1;
      width: 100%;
      justify-content: flex-end;
    }

    .logo {
      order: -2;
    }
  }
</style>
