<script lang="ts">
  import { onMount } from 'svelte';
  import { page } from '$app/stores';
  import { goto } from '$app/navigation';
  import { profileStore } from '$lib/client/profile.svelte';
  import ProfileSwitcher from '$lib/components/ProfileSwitcher.svelte';
  import ConfirmDialog from '$lib/components/ConfirmDialog.svelte';

  let { children } = $props();

  let navQuery = $state('');

  $effect(() => {
    const path = $page.url.pathname;
    if (path === '/search' || path === '/search/web') {
      navQuery = ($page.url.searchParams.get('q') ?? '').trim();
    }
  });

  const showHeaderSearch = $derived($page.url.pathname !== '/');

  function submitNavSearch(e: SubmitEvent) {
    e.preventDefault();
    const q = navQuery.trim();
    if (!q) return;
    void goto(`/search?q=${encodeURIComponent(q)}`);
  }

  onMount(() => {
    profileStore.load();
  });
</script>

<ConfirmDialog />

<header class="bar">
  <a href="/" class="brand">Kochwas</a>
  {#if showHeaderSearch}
    <form class="nav-search" onsubmit={submitNavSearch} role="search">
      <input
        type="search"
        bind:value={navQuery}
        placeholder="Rezept suchen…"
        autocomplete="off"
        inputmode="search"
        aria-label="Suchbegriff"
      />
    </form>
  {/if}
  <div class="bar-right">
    <a href="/wishlist" class="nav-link" aria-label="Wunschliste">🍽️</a>
    <a href="/admin" class="nav-link" aria-label="Einstellungen">⚙️</a>
    <ProfileSwitcher />
  </div>
</header>

<main>
  {@render children()}
</main>

<style>
  :global(html, body) {
    margin: 0;
    padding: 0;
    background: #f8faf8;
    color: #1a1a1a;
    font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
    -webkit-font-smoothing: antialiased;
  }
  :global(a) {
    color: #2b6a3d;
  }
  :global(*) {
    box-sizing: border-box;
  }
  .bar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.6rem 1rem;
    background: white;
    border-bottom: 1px solid #e4eae7;
  }
  .brand {
    font-size: 1.15rem;
    font-weight: 700;
    text-decoration: none;
    color: #2b6a3d;
    flex-shrink: 0;
  }
  .nav-search {
    flex: 1;
    min-width: 0;
    display: flex;
  }
  .nav-search input {
    width: 100%;
    padding: 0.55rem 0.85rem;
    font-size: 0.95rem;
    border: 1px solid #cfd9d1;
    border-radius: 999px;
    background: #f4f8f5;
    min-height: 40px;
  }
  .nav-search input:focus {
    outline: 2px solid #2b6a3d;
    outline-offset: 1px;
    background: white;
  }
  .bar-right {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    flex-shrink: 0;
  }
  @media (max-width: 520px) {
    .brand {
      font-size: 0;
      width: 1.6rem;
      height: 1.6rem;
      background: #2b6a3d;
      border-radius: 8px;
      position: relative;
    }
    .brand::after {
      content: '🍳';
      font-size: 1rem;
      position: absolute;
      inset: 0;
      display: grid;
      place-items: center;
    }
    .nav-link {
      width: 36px;
      height: 36px;
      font-size: 1.05rem;
    }
  }
  .nav-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 999px;
    text-decoration: none;
    font-size: 1.15rem;
  }
  .nav-link:hover {
    background: #f4f8f5;
  }
  main {
    padding: 0 1rem 4rem;
    max-width: 760px;
    margin: 0 auto;
  }
</style>
