/* Navigation
 * Sticky top nav with backdrop blur and gradient logo.
 * Desktop: horizontal links + avatar dropdown.
 * Mobile: hamburger → bottom sheet.
 *
 * The sheet and backdrop live OUTSIDE the <nav> element because
 * backdrop-filter on .nav creates a containing block that breaks
 * position:fixed on children.
 *
 * Toggle uses a hidden checkbox (#nav-toggle) with :has() selectors
 * on body for zero-JS open/close.
 */

@layer components {

  /* ── Hidden checkbox toggle ── */
  .nav__toggle {
    display: none;
  }

  /* ── Top nav bar ── */
  .nav {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    gap: var(--inline-space);
    padding: 0 clamp(1rem, 3vw, 2rem);
    min-height: 4rem;
    background: color-mix(in oklch, var(--color-canvas) 85%, transparent);
    backdrop-filter: blur(12px) saturate(1.3);
    -webkit-backdrop-filter: blur(12px) saturate(1.3);
    border-block-end: 1px solid color-mix(in oklch, var(--color-border) 60%, transparent);
    box-shadow: 0 1px 3px oklch(0% 0 0 / 0.06);
  }

  /* ── Standalone PWA back button — hidden in browser, visible when installed ── */
  .nav__back {
    display: none;
    align-items: center;
    justify-content: center;
    min-width: var(--touch-min);
    min-height: var(--touch-min);
    padding: 0;
    margin-inline-start: -0.5rem;
    background: none;
    border: none;
    color: var(--color-ink-muted);
    cursor: pointer;
    transition: color 100ms ease;

    &:hover {
      color: var(--color-ink);
    }
  }

  @media (display-mode: standalone) {
    .nav__back {
      display: inline-flex;
    }
  }

  /* ── Brand logo — gradient text ── */
  .nav__brand {
    text-decoration: none;
    margin-inline-end: var(--inline-space-l);
    flex-shrink: 0;
  }

  .nav__logo {
    font-family: "Outfit", system-ui, sans-serif;
    font-size: 1.375rem;
    font-weight: 800;
    letter-spacing: -0.02em;
    background: linear-gradient(135deg, oklch(var(--lch-flare)), oklch(var(--lch-golden)));
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
  }

  /* ── Desktop nav links ── */
  .nav__links {
    display: flex;
    align-items: center;
    gap: 0.25ch;
    list-style: none;
    padding: 0;
    margin: 0;
  }

  .nav__link {
    display: inline-flex;
    align-items: center;
    gap: var(--inline-space-s);
    min-height: var(--touch-min);
    padding: 0.375em 0.875em;
    font-size: 0.9375rem;
    font-weight: 500;
    color: var(--color-ink-muted);
    text-decoration: none;
    border-radius: var(--radius-pill);
    transition: color 100ms ease, background 100ms ease;

    &:hover {
      color: var(--color-ink);
      background: color-mix(in oklch, var(--color-ink) 6%, transparent);
    }

    &:focus-visible {
      outline: 2px solid var(--color-accent);
      outline-offset: 2px;
    }

    &[aria-current="page"] {
      color: var(--color-accent);
      background: color-mix(in oklch, var(--color-accent) 10%, transparent);
    }
  }

  .nav__links form { display: contents; }

  /* ── Right-side cluster ── */
  .nav__end {
    display: flex;
    align-items: center;
    gap: var(--inline-space-s);
    margin-inline-start: auto;
  }

  /* ── Avatar dropdown trigger ── */
  .nav__avatar-trigger {
    display: inline-flex;
    align-items: center;
    padding: 3px;
    background: none;
    border: 2px solid transparent;
    border-radius: 50%;
    cursor: pointer;
    transition: border-color 100ms ease;

    &:hover { border-color: var(--color-accent); }
    &:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
  }

  /* ── User menu dropdown ── */
  .nav__user-menu .dropdown__menu {
    position-area: bottom span-left;
    min-width: 14rem;
    margin-block-start: 0.5rem;
  }

  .dropdown__header {
    display: flex;
    align-items: center;
    gap: var(--inline-space-s);
    padding: 0.625rem 0.875rem;
  }

  .dropdown__header-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.875rem;
    font-weight: 600;
    background: color-mix(in oklch, var(--color-accent) 20%, var(--color-surface));
    color: var(--color-accent);
    flex-shrink: 0;
  }

  .dropdown__header-info { overflow: hidden; }
  .dropdown__header-name { font-weight: 600; font-size: 0.875rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .dropdown__header-email { font-size: 0.75rem; color: var(--color-ink-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

  .dropdown__menu form { display: contents; }

  /* ── Sign-in CTA button ── */
  .nav__sign-in {
    display: inline-flex;
    align-items: center;
    min-height: 2.25rem;
    padding: 0.375em 1em;
    font-size: 0.875rem;
    font-weight: 600;
    color: oklch(100% 0 0);
    background: linear-gradient(135deg, oklch(var(--lch-flare)), oklch(var(--lch-golden)));
    border-radius: var(--radius-pill);
    text-decoration: none;
    transition: filter 100ms ease, box-shadow 200ms ease;

    &:hover {
      filter: brightness(1.1);
      box-shadow: 0 0 16px oklch(var(--lch-flare) / 0.3);
    }
  }

  /* ── Theme toggle — ghost button ── */
  .nav__theme-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.25rem;
    height: 2.25rem;
    padding: 0;
    background: none;
    border: none;
    border-radius: var(--radius-pill);
    color: var(--color-ink-muted);
    cursor: pointer;
    transition: color 100ms ease, background 100ms ease;

    &:hover { color: var(--color-ink); background: color-mix(in oklch, var(--color-ink) 6%, transparent); }
    &:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
  }

  /* ── Hamburger toggle label ── */
  .nav__toggle-label { display: none; }
  .nav__close { display: none; }

  body:has(.nav__toggle:checked) .nav__hamburger { display: none; }
  body:has(.nav__toggle:checked) .nav__close { display: block; }

  /* ── Sheet + backdrop — hidden on desktop ── */
  .nav__backdrop { display: none; }
  .nav__sheet { display: none; }

  /* ══════════════════════════════════════ */
  /* Mobile: bottom sheet                   */
  /* ══════════════════════════════════════ */

  @media (max-width: 48rem) {
    /* Hide desktop-only elements */
    .nav__links { display: none; }
    .nav__user-menu { display: none; }
    .nav__sign-in { display: none; }

    /* Show hamburger */
    .nav__toggle-label {
      display: flex;
      align-items: center;
      justify-content: center;
      min-height: var(--touch-min);
      min-width: var(--touch-min);
      color: var(--color-ink-muted);
      border-radius: var(--radius-pill);
      cursor: pointer;
      transition: color 100ms ease, background 100ms ease;

      &:hover { color: var(--color-ink); background: color-mix(in oklch, var(--color-ink) 6%, transparent); }
    }

    /* Backdrop — shown when toggle checked */
    body:has(.nav__toggle:checked) .nav__backdrop {
      display: block;
      position: fixed;
      inset: 0;
      background: oklch(0% 0 0 / 0.5);
      z-index: 25;
      cursor: pointer;
      animation: backdropIn 250ms ease both;
    }

    @keyframes backdropIn { from { opacity: 0; } }

    /* Bottom sheet — shown when toggle checked */
    body:has(.nav__toggle:checked) .nav__sheet {
      display: flex;
      flex-direction: column;
      position: fixed;
      bottom: 0;
      left: 0;
      right: 0;
      max-height: 80vh;
      padding: 0;
      background: color-mix(in oklch, var(--color-surface) 80%, transparent);
      backdrop-filter: blur(24px) saturate(1.4);
      -webkit-backdrop-filter: blur(24px) saturate(1.4);
      border-start-start-radius: 20px;
      border-start-end-radius: 20px;
      border-block-start: 1px solid color-mix(in oklch, var(--color-border) 40%, transparent);
      box-shadow: 0 -8px 40px oklch(0% 0 0 / 0.15);
      z-index: 30;
      overflow-y: auto;
      animation: sheetSlideUp 350ms cubic-bezier(0.22, 1, 0.36, 1) both;
    }

    @keyframes sheetSlideUp { from { translate: 0 100%; } }

    /* Handle bar */
    .nav__sheet-handle {
      display: flex;
      justify-content: center;
      padding: 0.75rem 0 0.375rem;
      flex-shrink: 0;
    }

    .nav__sheet-handle-bar {
      width: 2.5rem;
      height: 4px;
      border-radius: 2px;
      background: var(--color-ink-muted);
      opacity: 0.35;
    }

    /* User section at top of sheet */
    .nav__sheet-user {
      display: flex;
      align-items: center;
      gap: var(--inline-space);
      padding: var(--block-space) var(--block-space-l);
      border-block-end: 1px solid color-mix(in oklch, var(--color-border) 50%, transparent);
      animation: sheetItemIn 0.4s cubic-bezier(0.22, 1, 0.36, 1) 0.05s both;
    }

    .nav__sheet-user-name { font-weight: 600; font-size: 0.9375rem; }
    .nav__sheet-user-email { font-size: 0.75rem; color: var(--color-ink-muted); }

    /* Sheet links */
    .nav__sheet-links {
      display: flex;
      flex-direction: column;
      padding: var(--block-space-s) 0;
      list-style: none;
      margin: 0;
      position: relative;
      z-index: 1;
    }

    .nav__sheet-links form { display: contents; }

    .nav__sheet-link {
      display: flex;
      align-items: center;
      gap: var(--inline-space);
      min-height: var(--touch-min);
      padding: 0.625em var(--block-space-l);
      font: inherit;
      font-size: 1rem;
      font-weight: 500;
      color: var(--color-ink);
      text-decoration: none;
      border: none;
      background: none;
      width: 100%;
      text-align: start;
      cursor: pointer;
      transition: background 80ms ease;

      &:hover { background: color-mix(in oklch, var(--color-ink) 5%, transparent); }

      &[aria-current="page"] {
        color: var(--color-accent);
        font-weight: 600;
      }

      & svg { flex-shrink: 0; color: var(--color-ink-muted); }
      &[aria-current="page"] svg { color: var(--color-accent); }
    }

    .nav__sheet-link--danger {
      color: var(--color-negative);
      & svg { color: var(--color-negative); }
    }

    /* Stagger animation for sheet items */
    body:has(.nav__toggle:checked) .nav__sheet-links li {
      animation: sheetItemIn 0.4s cubic-bezier(0.22, 1, 0.36, 1) both;
    }

    body:has(.nav__toggle:checked) .nav__sheet-links li:nth-child(1) { animation-delay: 0.06s; }
    body:has(.nav__toggle:checked) .nav__sheet-links li:nth-child(2) { animation-delay: 0.10s; }
    body:has(.nav__toggle:checked) .nav__sheet-links li:nth-child(3) { animation-delay: 0.14s; }
    body:has(.nav__toggle:checked) .nav__sheet-links li:nth-child(4) { animation-delay: 0.18s; }
    body:has(.nav__toggle:checked) .nav__sheet-links li:nth-child(5) { animation-delay: 0.22s; }
    body:has(.nav__toggle:checked) .nav__sheet-links li:nth-child(6) { animation-delay: 0.26s; }
    body:has(.nav__toggle:checked) .nav__sheet-links li:nth-child(7) { animation-delay: 0.30s; }
    body:has(.nav__toggle:checked) .nav__sheet-links li:nth-child(8) { animation-delay: 0.34s; }
    body:has(.nav__toggle:checked) .nav__sheet-links li:nth-child(9) { animation-delay: 0.38s; }

    @keyframes sheetItemIn { from { opacity: 0; translate: 0 0.75rem; } }

    /* Separator */
    .nav__sheet-sep {
      border: none;
      border-block-start: 1px solid color-mix(in oklch, var(--color-border) 50%, transparent);
      margin-block: 0.25rem;
    }

    /* CTA button inside sheet */
    .nav__sheet-cta {
      display: flex;
      align-items: center;
      justify-content: center;
      min-height: var(--touch-min);
      margin: var(--block-space-s) var(--block-space);
      padding: 0.625em 1em;
      font-size: 1rem;
      font-weight: 600;
      color: oklch(100% 0 0);
      background: linear-gradient(135deg, oklch(var(--lch-flare)), oklch(var(--lch-golden)));
      border: none;
      border-radius: var(--radius-pill);
      text-decoration: none;
      cursor: pointer;
      transition: filter 100ms ease, box-shadow 200ms ease;

      &:hover { filter: brightness(1.1); box-shadow: 0 0 16px oklch(var(--lch-flare) / 0.3); }
    }

    /* Gradient blob */
    .nav__sheet-blob {
      position: absolute;
      width: 250px;
      height: 250px;
      border-radius: 50%;
      filter: blur(100px);
      opacity: 0.05;
      pointer-events: none;
      bottom: 10%;
      left: 50%;
      translate: -50% 0;
      background: conic-gradient(from 0deg, oklch(var(--lch-flare)), oklch(var(--lch-uv)), oklch(var(--lch-golden)), oklch(var(--lch-laser)), oklch(var(--lch-flare)));
      animation: sheetBlobSpin 14s linear infinite;
    }

    @keyframes sheetBlobSpin { to { rotate: 360deg; } }
  }

  /* ── Reduced motion ── */
  @media (prefers-reduced-motion: reduce) {
    body:has(.nav__toggle:checked) .nav__sheet,
    body:has(.nav__toggle:checked) .nav__backdrop,
    body:has(.nav__toggle:checked) .nav__sheet-links li,
    .nav__sheet-user {
      animation: none;
    }

    .nav__sheet-blob { animation: none; }
  }

  /* ══════════════════════════════════════ */
  /* Breadcrumbs                            */
  /* ══════════════════════════════════════ */

  .breadcrumbs {
    display: flex;
    align-items: center;
    gap: var(--inline-space-s);
    font-size: 0.875rem;
    color: var(--color-ink-muted);
    list-style: none;
    padding: 0;
    margin: 0;

    & li + li::before {
      content: "/";
      margin-inline-end: var(--inline-space-s);
      color: var(--color-border);
    }

    & a {
      color: var(--color-ink-muted);
      text-decoration: none;
      &:hover { color: var(--color-ink); text-decoration: underline; }
    }

    & [aria-current="page"] { color: var(--color-ink); }
  }

  /* ══════════════════════════════════════ */
  /* Bottom navigation                      */
  /* ══════════════════════════════════════ */

  .bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: var(--block-space-s) 0;
    padding-block-end: max(var(--block-space-s), env(safe-area-inset-bottom));
    background: color-mix(in oklch, var(--color-canvas) 85%, transparent);
    backdrop-filter: blur(12px) saturate(1.5);
    -webkit-backdrop-filter: blur(12px) saturate(1.5);
    border-block-start: 1px solid var(--color-border);
    z-index: 10;
  }

  .bottom-nav__link {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.25rem;
    min-height: var(--touch-min);
    font-size: 0.6875rem;
    color: var(--color-ink-muted);
    text-decoration: none;
    padding: 0.25rem 0.75rem;
    transition: color 100ms ease;

    & svg { width: 1.5rem; height: 1.5rem; }
    &:hover, &[aria-current="page"] { color: var(--color-link); }
  }

  .bottom-nav__link--now {
    color: var(--color-accent);
    font-weight: 600;
    &[aria-current="page"] { color: var(--color-accent); }
  }

  .bottom-nav__link--live { animation: now-pulse 2s ease-in-out infinite; }

  @keyframes now-pulse {
    0%, 100% { filter: drop-shadow(0 0 0 transparent); }
    50% { filter: drop-shadow(0 0 8px oklch(63% 0.23 304 / 0.6)); }
  }

  body:has(.bottom-nav) { padding-block-end: 4rem; }
}
