<script>
  import { navigation } from '@state/navigation.svelte'
  import { locale } from '@state/locale.svelte'
  import { slides } from '@state/slides.svelte'
  import Menu from '@components/layout/Menu.svelte'

  const isMenuOpen = $derived(navigation.isMenuOpen)
  const currentLang = $derived(locale.current)
  const activeId = $derived(slides.active?.id ?? 'home')
  const menuItems = $derived(slides.all.filter(s => s.id !== 'home'))

  function getTitle(slide) {
    return slide.titles?.[currentLang] || slide.title || slide.id
  }

  function toggleMenu() {
    navigation.toggleMenu()
  }
</script>

<nav class="navbar" class:navbar--open={isMenuOpen}>
  <a href="/" class="navbar-logo">
    <img src="/assets/img/GIF_world_game_planete.gif" alt="World Game" class="wg-logo" />
  </a>

  <ul class="navbar-list">
    {#each menuItems as slide}
      <li>
        <a
          href={slide.path}
          class="navbar-item"
          class:active={activeId === slide.id}
        >
          {getTitle(slide)}
        </a>
      </li>
    {/each}
  </ul>

  <button
    class="navbar-toggle"
    onclick={toggleMenu}
    aria-label={isMenuOpen ? 'Fermer le menu' : 'Ouvrir le menu'}
    aria-expanded={isMenuOpen}
    aria-controls="main-menu"
  >
    <svg width="50" height="50" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
      <circle cx="7"  cy="7"  r="2" fill="white" class="dot dot-tl" />
      <circle cx="17" cy="7"  r="2" fill="white" class="dot dot-tr" />
      <circle cx="7"  cy="17" r="2" fill="white" class="dot dot-bl" />
      <circle cx="17" cy="17" r="2" fill="white" class="dot dot-br" />
      <line x1="9"  y1="9"  x2="15" y2="15" stroke="white" stroke-width="2.5" stroke-linecap="round" class="cross-line" />
      <line x1="15" y1="9"  x2="9"  y2="15" stroke="white" stroke-width="2.5" stroke-linecap="round" class="cross-line" />
    </svg>
  </button>
</nav>

<Menu />

<style>
  .navbar {
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: var(--z-header);
    font-family: "Danzza";
    font-size: var(--font-size-paragraph);
    font-weight: normal;
    color: white;
    text-align: center;
    text-transform: uppercase;
    padding: 2vh 0;
  }

  /* Logo */
  .navbar-logo {
    position: absolute;
    left: 5vh;
    top: 50%;
    transform: translateY(-50%);
    z-index: var(--z-header);
  }

  .wg-logo {
    height: 3rem;
    display: block;
    pointer-events: none;
  }

  /* Nav list */
  .navbar-list {
    list-style: none;
    margin: 0;
    padding: 0;
  }

  .navbar-list li {
    display: inline;
  }

  .navbar--open {
    background-color: transparent;
    backdrop-filter: none;
  }

  .navbar--open .navbar-list {
    visibility: hidden;
  }

  .navbar-item {
    display: inline-block;
    padding: 1vmax 2vmax;
    font-weight: bold;
    vertical-align: middle;
    position: relative;
    z-index: var(--z-content);
    color: white;
    transition: color 0.3s;
  }

  .navbar-item.active,
  .navbar-item:hover {
    color: #04fea0;
  }

  /* Hamburger button */
  .navbar-toggle {
    position: absolute;
    right: 5vh;
    top: 50%;
    transform: translateY(-50%);
    z-index: var(--z-header);
    background: none;
    border: none;
    padding: 0;
    cursor: pointer;
  }

  .navbar-toggle svg {
    height: 4.5vh;
    min-height: 36px;
    display: block;
  }

  /* Dot animation */
  .dot {
    transform-origin: center;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  }

  .navbar--open .dot-tl { transform: translate(5px,  5px)  scale(0.7); }
  .navbar--open .dot-tr { transform: translate(-5px, 5px)  scale(0.7); }
  .navbar--open .dot-bl { transform: translate(5px,  -5px) scale(0.7); }
  .navbar--open .dot-br { transform: translate(-5px, -5px) scale(0.7); }

  /* Cross lines */
  .cross-line {
    opacity: 0;
    transition: opacity 0.2s ease-in-out;
  }

  .navbar--open .cross-line {
    opacity: 1;
    transition-delay: 0.1s;
  }

  /* Mobile — var(--breakpoint-mobile) = 700px */
  @media (--mobile) {
    .navbar {
      min-height: 8vh;
      padding: 10vh 0 1vh;
      top: 0;
    }

    .navbar-item {
      display: none;
    }
  }

  /* Tablet — var(--breakpoint-tablet) = 912px */
  @media (--tablet-only) {
    .navbar-item {
      font-size: var(--font-size-paragraph-tablet);
      padding: 1vmax 1.8vmax;
    }
  }
</style>
