/* Navigation
 * Mobile-first: bottom nav (5 tabs) + top bar.
 * Desktop: sticky top nav with backdrop blur.
 * Crew switcher overlay as dialog.
 *
 * Mobile bottom nav uses :has() for active state highlighting.
 * Desktop top nav uses checkbox toggle for mobile sheet fallback.
 */

@layer components {

  /* ══════════════════════════════════════ */
  /* Top bar (mobile app header)            */
  /* ══════════════════════════════════════ */

  .top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 20px 4px;
    flex-shrink: 0;
  }

  .top-bar__left {
    flex: 1;
  }

  .top-bar__context {
    font-size: 0.6875rem;
    font-weight: 600;
    color: var(--color-ink-muted);
    letter-spacing: 0.3px;
  }

  .top-bar__title {
    font-family: "Outfit", system-ui, sans-serif;
    font-size: 1.375rem;
    font-weight: 800;
  }

  .top-bar__right {
    display: flex;
    gap: 12px;
    align-items: center;
    padding-top: 4px;
  }

  /* ══════════════════════════════════════ */
  /* Crew switcher chip                     */
  /* ══════════════════════════════════════ */

  .crew-switcher {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 14px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-pill);
    font-size: 0.8125rem;
    font-weight: 500;
    cursor: pointer;
    color: var(--color-ink);
    text-decoration: none;

    & svg {
      width: 12px;
      height: 12px;
      color: var(--color-ink-muted);
    }

    &:focus-visible {
      outline: 2px solid oklch(var(--lch-flare));
      outline-offset: 2px;
    }
  }

  /* ══════════════════════════════════════ */
  /* Bottom navigation (mobile, always on)  */
  /* ══════════════════════════════════════ */

  .bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: 8px 0 max(22px, env(safe-area-inset-bottom));
    background: var(--color-surface);
    border-block-start: 1px solid var(--color-border-interactive);
    z-index: 10;
  }

  .bottom-nav__link {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    color: var(--color-ink-muted);
    min-width: 56px;
    padding: 4px 12px;
    border-radius: 12px;
    text-decoration: none;
    font-size: 0.625rem;
    font-weight: 500;
    position: relative;
    transition: color 150ms ease, background 150ms ease;

    & svg {
      width: 22px;
      height: 22px;
    }

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

    &:focus-visible {
      outline: 2px solid oklch(var(--lch-flare));
      outline-offset: 2px;
    }

    &[aria-current="page"] {
      color: oklch(var(--lch-flare));
      background: oklch(var(--lch-flare) / 0.1);
    }
  }

  /* Badge on bottom nav link */
  .bottom-nav__badge {
    position: absolute;
    top: -2px;
    right: 4px;
    background: oklch(var(--lch-flare));
    color: oklch(17% 0.02 276);
    font-size: 0.5625rem;
    font-weight: 700;
    padding: 1px 5px;
    border-radius: var(--radius-pill);
    min-width: 16px;
    text-align: center;
    line-height: 1.4;
  }

  /* Reserve space for bottom nav.
     --bottom-nav-inset is exposed for floating UI (e.g. map buttons) that
     need to stay clear of the nav — used in calc(.. + var(--bottom-nav-inset)). */
  :root {
    --bottom-nav-inset: 0px;
    /* Height of the sticky web top nav. On native this is 0 because the
       Hotwire Native shell renders its own top bar — full-bleed pages
       (map, chat) and sticky page-level headers (lineup) consume this
       value instead of hardcoding 4rem. */
    --top-nav-inset: 4rem;
  }

  body[data-native-platform="ios"],
  body[data-native-platform="android"] {
    --top-nav-inset: 0px;
  }

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

  /* ══════════════════════════════════════ */
  /* Desktop top nav (sticky)               */
  /* ══════════════════════════════════════ */

  .nav__toggle { display: none; }

  .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);
  }

  /* PWA back button */
  .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; }
  }

  .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;
  }

  .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 oklch(var(--lch-flare));
      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; }

  .nav__end {
    display: flex;
    align-items: center;
    gap: var(--inline-space-s);
    margin-inline-start: auto;
  }

  .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; }
  }

  .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-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; }

  .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); }
  }

  .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 */
  .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; }

  .nav__backdrop { display: none; }
  .nav__sheet { display: none; }

  /* ══════════════════════════════════════ */
  /* Mobile: hamburger → bottom sheet       */
  /* ══════════════════════════════════════ */

  @media (max-width: 48rem) {
    .nav__links { display: none; }
    .nav__user-menu { display: none; }
    .nav__sign-in { display: none; }

    .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); }
    }

    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: fade-in 250ms ease both;
    }

    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: sheet-slide-up 350ms cubic-bezier(0.22, 1, 0.36, 1) both;
    }

    .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;
    }

    .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: card-fade-in 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); }

    .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); }
    }

    .nav__sheet-locale-select {
      appearance: none;
      background: none;
      border: none;
      color: inherit;
      font: inherit;
      cursor: pointer;
      flex: 1;
      padding: 0;
    }

    /* Stagger animation for sheet items */
    body:has(.nav__toggle:checked) .nav__sheet-links li {
      animation: card-fade-in 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; }

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

    .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); }
    }

    .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; } }
  }

  /* ── 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); }
  }

  /* ── Notification bell ── */

  .notification-bell {
    position: relative;
    width: 24px;
    height: 24px;
    color: var(--color-ink-muted);

    &::after {
      content: "";
      position: absolute;
      top: 0;
      right: 0;
      width: 8px;
      height: 8px;
      background: oklch(var(--lch-flare));
      border-radius: 50%;
      border: 2px solid var(--color-canvas);
      display: none;
    }

    &--unread::after {
      display: block;
    }
  }

  /* ── 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; }
  }
}
