/*
 * Mobile navbar drawer — peer-checked fallback + polish.
 *
 * The compiled Tailwind bundle (tailwind-main.css) currently ships without
 * peer-checked variants, so the markup class `peer-checked:flex` on
 * #mobile-nav-drawer has no effect and the hamburger toggle never opens
 * the drawer. This file implements the :checked sibling selector directly
 * so the pure-CSS hamburger works regardless of the Tailwind build state.
 *
 * SHELL: the drawer is a THREE-PART panel — a pinned header, a scrolling
 * body, and a pinned footer. It used to be one `overflow-y: auto` box, so
 * a catalog of 4 products + 14 subjects (measured 959px of content in an
 * 864px panel) scrolled the brand and the close control off the top. The
 * header/footer are `flex: 0 0 auto`; ONLY `.mobile-nav-drawer__body`
 * scrolls. Same bug class as the chat panel's unreachable header
 * (root CLAUDE.md, "A bottom-anchored chat panel's HEIGHT decides
 * whether its header is reachable").
 *
 * Selector contract (do not change these ids without updating base.html):
 *   - #mobile-nav-toggle     : the <input type="checkbox"> driver
 *   - #mobile-nav-toggle-btn : the <label> that flips the checkbox
 *   - #mobile-nav-drawer     : the drawer panel that should slide in
 *   - #mobile-nav-backdrop   : the dim/blur backdrop behind the drawer
 *
 * Every colour is a `--kais-drawer-*` token with a dark fallback; the
 * light palette is defined in theme.css. A literal here is invisible to
 * the light-theme token sweep and ships as unreadable ink.
 */

/* ── Open the drawer when the hamburger checkbox is checked ───────────── *
 * This rule owns the ENTIRE open state — `display` and `flex-direction`
 * both. `.mobile-nav-drawer` must declare no `display` of its own, because
 * its rule is specificity (0,1,0), byte-for-byte the same as Tailwind's
 * `.hidden { display: none }`, and base.html links tailwind-main.css at
 * line 36 while this sheet arrives at line 56. At equal specificity the
 * later sheet wins on source order, so a `display: flex` up there kills
 * the markup's `hidden` class and pins the drawer permanently OPEN —
 * MEASURED at 384x882 as `display: flex`, 320x866, on screen with the
 * checkbox UNCHECKED, and the close control inert (it flipped the
 * checkbox and dimmed the backdrop while the panel stayed put).
 *
 * `flex-direction: column` is declared HERE rather than left to the
 * markup's `flex-col`: tailwind-main.css is VENDORED and pre-built with
 * no pipeline, so relying on any utility in it is a standing hazard.
 * The three-part shell (__header / __body / __footer) needs the column. */
#mobile-nav-toggle:checked ~ #mobile-nav-drawer {
  display: flex !important;
  flex-direction: column;
  animation: mobileNavDrawerIn 220ms ease-out both;
}

#mobile-nav-toggle:checked ~ #mobile-nav-backdrop {
  display: block !important;
  animation: mobileNavBackdropIn 220ms ease-out both;
}

/* While the drawer is open, hide the navbar-level hamburger button
 * completely — the drawer provides its OWN in-panel close control
 * (.mobile-nav-drawer__close), and we no longer want a floating
 * ✕ glyph outside the panel (the previous design placed the close
 * on the navbar bar, visually disconnected from the drawer card). */
#mobile-nav-toggle:checked ~ #mobile-nav-toggle-btn {
  visibility: hidden;
  pointer-events: none;
}

/* ── Drawer surface: right-anchored sheet, 320dp wide ────────────────── *
 * Width follows the Material nav-drawer standard (320dp cap, never wider
 * than the viewport less a peek strip). The previous 280px cap left rows
 * cramped on a 384px phone and forced every product description into an
 * ellipsis.
 *
 * SIZING vs CAPPING: `dvh` tracks the visual viewport as browser chrome
 * moves; `svh` is the viewport at its SHORTEST, so the cap binds even
 * where an engine resolves `dvh` optimistically. They are not
 * interchangeable — the CAP is the guarantee. Bare `vh` (== `lvh`) must
 * never size or cap this panel, and there is deliberately no bare-`vh`
 * fallback: an engine without `svh` keeps uncapped behaviour rather than
 * a WRONG cap.
 *
 * Logical insets (`inset-block-start` / `inset-inline-end`) so the sheet
 * anchors to the hamburger's side in RTL as well as LTR; each offset is
 * `max()`-ed against the safe-area inset so a notch or a rounded corner
 * never clips the close control. Every env() carries a fallback — inside
 * calc()/max() an unknown keyword invalidates the WHOLE declaration. */
.mobile-nav-drawer {
  position: fixed;
  inset-block-start: max(8px, env(safe-area-inset-top, 0px));
  inset-inline-end: max(8px, env(safe-area-inset-right, 0px), env(safe-area-inset-left, 0px));
  inset-inline-start: auto;
  z-index: 10001;
  /* NO `display` here — see the :checked rule above. `gap` is inert until
   * that rule makes this a flex container, so it stays with the rest of
   * the surface geometry. */
  width: min(320px, calc(100vw - 32px));
  max-height: calc(100dvh - 16px);
  max-height: min(calc(100dvh - 16px), calc(100svh - 16px));
  overflow: hidden;
  background: linear-gradient(180deg,
              var(--kais-drawer-bg-1, #070c2e) 0%,
              var(--kais-drawer-bg-2, #080e33) 100%);
  border: 1px solid var(--kais-drawer-border, rgba(27, 38, 102, 0.7));
  border-radius: 14px;
  padding: 8px 8px 10px;
  margin-top: 0;
  box-shadow:
    0 20px 40px -10px var(--kais-drawer-shadow, rgba(0, 0, 0, 0.55)),
    0 0 0 1px var(--kais-drawer-inset, rgba(255, 255, 255, 0.03)) inset;
  gap: 6px;
}

/* Green→blue accent stripe across the top */
.mobile-nav-drawer::before {
  content: "";
  display: block;
  flex: 0 0 auto;
  height: 2px;
  border-radius: 2px;
  margin: 0 4px 6px;
  background: linear-gradient(90deg,
              rgba(34, 197, 94, 0.8) 0%,
              rgba(52, 211, 153, 0.7) 50%,
              rgba(59, 130, 246, 0.7) 100%);
}

/* ── In-panel header: logo + close button together ───────────────────── *
 * The previous layout left the KAIS logo on the navbar and the ✕ on
 * the navbar, with the drawer card floating below. We now surface the
 * logo and close inside the drawer so they visually belong to the
 * same panel as the nav items — and they are PINNED, so a long
 * catalog can never scroll the close control out of reach. */
.mobile-nav-drawer__header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 2px 6px;
  margin-bottom: 4px;
  border-bottom: 1px solid var(--kais-drawer-border-soft, rgba(27, 38, 102, 0.5));
}

/* ── Scrolling body ──────────────────────────────────────────────────── *
 * `min-height: 0` is load-bearing: a flex child's default `min-height:
 * auto` refuses to shrink below its content, so without it the body
 * grows past the panel and the drawer scrolls instead (taking the
 * pinned header with it). `overscroll-behavior: contain` stops a flick
 * past the last subject from scrolling the page underneath. */
.mobile-nav-drawer__body {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--kais-drawer-border-mid, rgba(27, 38, 102, 0.6)) transparent;
  /* Breathing room so a focus ring on an edge row is not clipped by the
   * scroll container (`overflow-y: auto` computes `overflow-x` to `auto`
   * too, so a ring drawn at outline-offset would be cut off). Published
   * as a custom property because the sticky section labels have to bleed
   * back across it — see .mobile-nav-section-label in
   * product-switcher.css. */
  --kais-drawer-body-gutter: 2px;
  padding-inline: var(--kais-drawer-body-gutter);
  margin-inline: calc(-1 * var(--kais-drawer-body-gutter));
}

.mobile-nav-drawer__body::-webkit-scrollbar {
  width: 6px;
}

.mobile-nav-drawer__body::-webkit-scrollbar-thumb {
  border-radius: 999px;
  background: var(--kais-drawer-border-mid, rgba(27, 38, 102, 0.6));
}

/* ── Pinned footer ───────────────────────────────────────────────────── *
 * Appearance is a preference, not navigation. At the top of the panel it
 * was the heaviest element on screen, outweighing every nav row; here it
 * is always reachable without competing for the reading order. */
.mobile-nav-drawer__footer {
  flex: 0 0 auto;
  margin-block-start: 6px;
  padding-block-start: 8px;
  padding-block-end: env(safe-area-inset-bottom, 0px);
  border-block-start: 1px solid var(--kais-drawer-border-soft, rgba(27, 38, 102, 0.5));
}

.mobile-nav-drawer__brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  color: var(--kais-drawer-text, #f3f4f6);
  text-decoration: none;
  border-radius: 6px;
  padding: 2px 4px;
  transition: background-color 150ms ease;
}

.mobile-nav-drawer__brand:hover,
.mobile-nav-drawer__brand:focus-visible {
  background: var(--kais-drawer-veil-2, rgba(255, 255, 255, 0.05));
}

.mobile-nav-drawer__logo {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  object-fit: contain;
  display: block;
}

.mobile-nav-drawer__brand-text {
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.02em;
  color: var(--kais-drawer-text-strong, #f9fafb);
}

.mobile-nav-drawer__close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 8px;
  color: var(--kais-drawer-icon, #e5e7eb);
  background: var(--kais-drawer-veil-1, rgba(255, 255, 255, 0.04));
  border: 1px solid var(--kais-drawer-border-mid, rgba(27, 38, 102, 0.6));
  cursor: pointer;
  transition: background-color 150ms ease,
              border-color 150ms ease,
              color 150ms ease;
}

.mobile-nav-drawer__close:hover,
.mobile-nav-drawer__close:focus-visible {
  background: rgba(34, 197, 94, 0.14);
  border-color: rgba(34, 197, 94, 0.45);
  color: var(--kais-drawer-accent, #4ade80);
}

.mobile-nav-drawer__close:active {
  transform: scale(0.96);
}

.mobile-nav-drawer__close svg {
  display: block;
}

/* Anchor / button rows inside the drawer.
 * `.mobile-nav-drawer > a` and `> form > button` are kept alongside the
 * body-scoped selectors: rows now live inside .mobile-nav-drawer__body,
 * but a direct child must keep the same treatment so a future row added
 * outside the scroll region can never render unstyled. */
.mobile-nav-drawer > a,
.mobile-nav-drawer__body > a,
.mobile-nav-drawer__body > form > button,
.mobile-nav-drawer > form > button {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  /* `min-height: 44px` is the tap-target FLOOR (WCAG 2.5.8), but stating it
   * EXPLICITLY replaces the flex item's automatic minimum size
   * (`min-height: auto`) — so inside the column-flex `__body` the default
   * `flex-shrink: 1` was free to squash every row to exactly 44px while
   * its content needed more. MEASURED at 384px: the three two-line
   * product rows each needed 62.3px in a 44.0px box, DoKAI 47.4px, and
   * "Introduction To Transformers With Pytorch" 47.2px. The overflow
   * spilled into the 4px gap and collided with the next row's name —
   * that is the drawer's visible "clutter". `flex-shrink: 0` turns the
   * 44px back into a floor instead of a cage; the body's
   * `overflow-y: auto` absorbs the extra height. */
  flex-shrink: 0;
  padding: 7px 10px;
  border-radius: 8px;
  color: var(--kais-drawer-text, #f3f4f6);
  font-weight: 600;
  font-size: 13px;
  line-height: 1.2;
  border: 1px solid transparent;
  transition: background-color 150ms ease,
              border-color 150ms ease,
              transform 120ms ease;
  text-decoration: none;
  width: 100%;
  text-align: left;
  background: transparent;
}

.mobile-nav-drawer > a:hover,
.mobile-nav-drawer > a:focus-visible,
.mobile-nav-drawer__body > a:hover,
.mobile-nav-drawer__body > a:focus-visible,
.mobile-nav-drawer__body > form > button:hover,
.mobile-nav-drawer__body > form > button:focus-visible,
.mobile-nav-drawer > form > button:hover,
.mobile-nav-drawer > form > button:focus-visible {
  background: var(--kais-drawer-veil-1, rgba(255, 255, 255, 0.04));
  border-color: rgba(34, 197, 94, 0.35);
  transform: translateX(2px);
}

.mobile-nav-drawer > a:active,
.mobile-nav-drawer__body > a:active,
.mobile-nav-drawer__body > form > button:active,
.mobile-nav-drawer > form > button:active {
  transform: translateX(0) scale(0.99);
}

/* ── Focus ring ──────────────────────────────────────────────────────── *
 * The row rules above tint a border on :focus-visible, which is a weak
 * indicator on a translucent surface and used to be paired with
 * `outline: none`. Every focusable thing in the drawer gets a real ring
 * instead. The offset is POSITIVE so the ring paints outside the row
 * rather than over its own content. */
.mobile-nav-drawer a:focus-visible,
.mobile-nav-drawer button:focus-visible,
.mobile-nav-drawer label:focus-visible,
.mobile-nav-drawer [tabindex]:focus-visible {
  outline: 2px solid var(--kais-drawer-accent, #4ade80);
  outline-offset: 2px;
}

/* Tighten SVG icons inside drawer rows */
.mobile-nav-drawer > a > svg,
.mobile-nav-drawer__body > a > svg,
.mobile-nav-drawer > form > button > svg,
.mobile-nav-drawer__body > form > button > svg,
.mobile-nav-profile > svg,
.mobile-nav-profile .nav-avatar,
.mobile-nav-profile .nav-avatar-initials {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

/* ── Account row: profile + balance on ONE line ──────────────────────── *
 * They were two stacked rows, and the coin pill alone on its own line
 * read as an orphaned chip rather than part of the account. */
.mobile-nav-account {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 44px;
  padding: 0 4px 0 0;
  margin-bottom: 2px;
  border-radius: 10px;
  background: var(--kais-drawer-veil-1, rgba(255, 255, 255, 0.04));
  border: 1px solid var(--kais-drawer-border-soft, rgba(27, 38, 102, 0.5));
}

.mobile-nav-profile {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1 1 auto;
  min-height: 44px;
  padding: 4px 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--kais-drawer-text, #f3f4f6);
  text-decoration: none;
  border-radius: 8px;
}

/* A long first name must ellipsis rather than push the balance off the
 * row (or wrap the pill onto a second line). */
.mobile-nav-profile__name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mobile-nav-profile .nav-avatar,
.mobile-nav-profile .nav-avatar-initials {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--kais-drawer-veil-3, rgba(255, 255, 255, 0.06));
  font-size: 11px;
  font-weight: 700;
  color: var(--kais-drawer-text, #f3f4f6);
}

/* Subtle separator before the logout form */
.mobile-nav-drawer > form,
.mobile-nav-drawer__body > form {
  margin-top: 4px;
  padding-top: 6px;
  border-top: 1px solid var(--kais-drawer-border-mid, rgba(27, 38, 102, 0.6));
}

/* Coin pill: compact, sits at the end of the account row */
.mobile-nav-drawer .coin-display {
  flex: 0 0 auto;
  margin: 0;
  padding: 3px 9px;
  font-size: 12px;
  border-radius: 999px;
  background: var(--kais-drawer-veil-3, rgba(255, 255, 255, 0.06));
  color: var(--kais-drawer-text, #f3f4f6);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.mobile-nav-drawer .coin-display .coin-symbol svg {
  width: 14px;
  height: 14px;
}

/* ── Navbar row on mobile ────────────────────────────────────────────── *
 * tailwind-main.css ships a ``.navbar { flex-direction: column; }`` rule
 * at ≤640px which stacks the logo and the hamburger trigger on separate
 * rows (the hamburger ends up beneath the logo). That predates the
 * peer-checked drawer, which expects the trigger to sit on the SAME
 * row as the logo, anchored to the right. Pin the row to horizontal
 * and right-align the hamburger; keep auth/nav siblings hidden on
 * mobile (they are ``hidden md:flex`` in markup but the stacked
 * navbar reserved vertical space for them regardless). */
@media (max-width: 640px) {
  .navbar {
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    align-items: center !important;
    justify-content: space-between !important;
  }
  .navbar .logo {
    margin-bottom: 0 !important;
  }
  .navbar .logo img {
    max-height: 40px;
  }
  #mobile-nav-toggle-btn {
    margin-left: auto;
  }
}

/* ── Escaping the navbar's stacking context ──────────────────────────── *
 * MEASURED, not assumed: with the drawer at z-index 10001 and the chat
 * FAB at 9999, the FAB still painted over the open panel. The drawer
 * lives inside `<nav class="navbar">`, which is `position: sticky` with
 * `z-index: 1000` AND a `backdrop-filter` — either one alone opens a
 * stacking context. Every z-index inside the drawer is therefore scoped
 * to the navbar's context and the whole subtree paints at the NAVBAR's
 * 1000, which loses to a body-level 9999 no matter how high the drawer
 * goes. Raising the drawer's own z-index cannot fix this; the CONTEXT
 * has to be lifted.
 *
 * Scoped below the md breakpoint so a checkbox left :checked by a
 * rotation from mobile to desktop width cannot leave the desktop navbar
 * parked above every other overlay (`display: none` does not stop
 * `:checked` from matching).
 *
 * `:has()` is the only selector that reaches an ANCESTOR of the
 * checkbox. Where it is unsupported the rule is dropped whole and the
 * drawer degrades to today's behaviour — the FAB overlapping a corner —
 * rather than to anything broken. */
@media (max-width: 767.98px) {
  .navbar:has(> #mobile-nav-toggle:checked) {
    z-index: 10001;
  }
}

/* ── Backdrop ────────────────────────────────────────────────────────── *
 * z-index sits ABOVE the floating chat FAB (`.kais-chatbot-fab`, 9999) so
 * the FAB and its persona avatar are dimmed with the rest of the page.
 * At the old z-index: 30 they painted straight over the open drawer and
 * swallowed taps meant for the rows beneath them. */
#mobile-nav-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--kais-backdrop, rgba(4, 7, 31, 0.55));
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  z-index: 10000;
}

/* ── Animations ──────────────────────────────────────────────────────── */
@keyframes mobileNavDrawerIn {
  from {
    opacity: 0;
    transform: translateY(-16px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes mobileNavBackdropIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* Respect user motion preferences */
@media (prefers-reduced-motion: reduce) {
  #mobile-nav-toggle:checked ~ #mobile-nav-drawer,
  #mobile-nav-toggle:checked ~ #mobile-nav-backdrop {
    animation: none;
  }
  .mobile-nav-drawer > a,
  .mobile-nav-drawer__body > a,
  .mobile-nav-drawer > form > button,
  .mobile-nav-drawer__body > form > button {
    transition: none;
  }
}

/* ── Desktop (md and up): force hamburger + drawer hidden ────────────── *
 * Tailwind's `.inline-flex` utility is emitted AFTER `.md\:hidden` in the
 * compiled bundle, so at ≥768px the later rule wins and the hamburger
 * stays visible on desktop. Force it hidden here. */
@media (min-width: 768px) {
  #mobile-nav-toggle-btn,
  #mobile-nav-drawer,
  #mobile-nav-backdrop {
    display: none !important;
  }
  /* Defensive: even if the checkbox is somehow :checked (e.g. device
   * rotation from mobile to desktop width) the drawer must not expand
   * the navbar row. */
  #mobile-nav-toggle:checked ~ #mobile-nav-drawer,
  #mobile-nav-toggle:checked ~ #mobile-nav-backdrop {
    display: none !important;
  }
  #mobile-nav-toggle {
    display: none !important;
  }
}
