/* topnav.css — shared 4-item topnav styling for non-home pages.
 * The home page (modules/textbooks/index.html) ships its own inline
 * topnav CSS with a richer right-cluster (lang/theme/account); here
 * we use a stripped-down topnav with brand + 4 menu items only,
 * because chapter / BQ / take pages already carry the .site-header
 * below this topnav, which owns lang/theme/account/hamburger.
 *
 * Selector matches the home topnav class (`nav.topnav`) so
 * topnav-current.js works unmodified across surfaces.
 */

nav.topnav {
  display: flex;
  align-items: center;
  gap: 32px;
  /* Padding / wordmark size / menu gap mirror home's hand-rolled
   * inline rules at site/index.html so the topnav renders identically
   * on every page (home / chapter / BQ / topic / atlas / explorer). */
  padding: 14px 32px;
  background: var(--color-page, #fcfbf8);
  border-bottom: 1px solid var(--color-rule, rgba(168, 40, 40, 0.22));
  font-family: 'Newsreader', serif;
  position: sticky;
  top: 0;
  z-index: 50;
  /* Break out of body's max-width (textbook.css gives body
   * `max-width: 820px` for non-chapter pages; chapter-shell extends to
   * 1280px). The 50%-50vw negative margin pulls the topnav back to the
   * viewport edge so chrome spans full width and the brand sits at
   * the same left position on every page — matching home's hand-rolled
   * topnav. Don't touch body's own width: chapter-shell.js's article
   * grid relies on body sizing being stable so Plotly doesn't reflow
   * mid-render. */
  width: 100vw;
  margin-left: calc(50% - 50vw);
  box-sizing: border-box;
}
nav.topnav .menu a { white-space: nowrap; }
/* Prevent horizontal scrollbar from the 100vw breakout (vw includes
 * scrollbar width in CSS; without this, the topnav extends ~17px past
 * the document scroll area). `clip` is preferred over `hidden` —
 * `hidden` creates a new scroll container which breaks `position: sticky`
 * on descendants (the topnav itself, the chapmast). */
html { overflow-x: clip; }
nav.topnav .brand {
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: var(--color-ink, #0a0a08);
  flex-shrink: 0;
}
nav.topnav .brand svg { display: block; }
nav.topnav .brand .wordmark {
  font-weight: 700;
  font-size: 19px;
  letter-spacing: -0.02em;
}
nav.topnav .brand .wordmark .hl {
  color: var(--color-accent, #a82828);
  font-style: italic;
  font-weight: 500;
}
nav.topnav .menu {
  display: flex;
  gap: 28px;
  font-weight: 600;
  text-transform: uppercase;
  font-size: 11px;
  letter-spacing: 0.18em;
  margin: 0 auto;
}
nav.topnav .right-cluster {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: 16px;
  flex-shrink: 0;
}
nav.topnav .right-cluster > div {
  display: flex;
  align-items: center;
}
nav.topnav .right-cluster > div:empty {
  display: none;
}

/* Right-cluster buttons mirror home's inline chrome. The slot scripts
 * (i18n.js, theme-toggle.js, intuition-mode.js) emit `.lang-btn` /
 * `.icon-btn` so chapter / BQ / atlas pages render the same compact
 * icon-button cluster as the home page.
 */
nav.topnav .right-cluster .lang-btn {
  font-family: 'Newsreader', serif;
  font-size: 12.5px;
  font-weight: 500;
  color: var(--color-ink-secondary, #2a2a26);
  cursor: pointer;
  padding: 6px 10px;
  border: none;
  background: transparent;
  border-radius: 16px;
  letter-spacing: 0.04em;
  transition: background 0.15s, color 0.15s;
}
nav.topnav .right-cluster .lang-btn:hover {
  background: rgba(0, 0, 0, 0.05);
  color: var(--color-ink, #0a0a08);
}
nav.topnav .right-cluster .icon-btn {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-ink-secondary, #2a2a26);
  cursor: pointer;
  padding: 0;
  transition: background 0.15s, color 0.15s;
}
nav.topnav .right-cluster .icon-btn:hover {
  background: rgba(0, 0, 0, 0.05);
  color: var(--color-ink, #0a0a08);
}
nav.topnav .right-cluster .icon-btn svg {
  width: 17px;
  height: 17px;
  stroke: currentColor;
  stroke-width: 1.6;
  fill: none;
}
nav.topnav .right-cluster .icon-btn.active {
  color: var(--color-accent, #a82828);
}
nav.topnav .right-cluster .lang-pop { position: relative; }
nav.topnav .right-cluster .lang-pop .lang-menu {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 6px;
  background: var(--color-page, #fcfbf8);
  border: 1px solid var(--color-rule, rgba(168, 40, 40, 0.22));
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  z-index: 100;
  min-width: 120px;
  overflow: hidden;
}
nav.topnav .right-cluster .lang-pop .lang-menu-item {
  display: block;
  padding: 8px 16px;
  font-family: 'Newsreader', serif;
  font-size: 13px;
  color: var(--color-ink, #0a0a08);
  text-decoration: none;
  cursor: pointer;
  transition: background 0.1s;
}
nav.topnav .right-cluster .lang-pop .lang-menu-item:hover { background: rgba(0, 0, 0, 0.04); }
nav.topnav .right-cluster .lang-pop .lang-menu-item.active {
  font-weight: 600;
  color: var(--color-accent, #a82828);
}

/* Hide the legacy .lang-selector-wrapper / .theme-toggle wrapper / labeled
 * .mode-toggle output if any older code path still emits them — the new
 * slot output is .lang-btn / .icon-btn directly. */
nav.topnav .right-cluster .lang-selector-wrapper,
nav.topnav .right-cluster .theme-toggle,
nav.topnav .right-cluster .lang-selector-container { display: none !important; }

/* Sign-in button (.uf-signin-btn from app-shell.js) sits as the last cluster
 * item — keep its existing pill styling but tighten the size to match. */
nav.topnav .right-cluster .uf-signin-btn,
nav.topnav .right-cluster .uf-account-btn {
  font-size: 12.5px;
  padding: 5px 12px;
}
nav.topnav .menu a {
  color: var(--color-ink, #0a0a08);
  text-decoration: none;
  border-bottom: 2px solid transparent;
  padding-bottom: 2px;
}
nav.topnav .menu a:hover  { border-bottom-color: var(--color-accent, #a82828); }
nav.topnav .menu a.current { border-bottom-color: var(--color-accent, #a82828); }

/* Mobile-toggle button: hidden on desktop, shown at <768px. Sits at the
 * far right of the topnav row. Opens a drawer containing the 4 menu items
 * and lang slot. Theme + account slots stay visible on the topnav row. */
nav.topnav .nav-mobile-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: none;
  background: transparent;
  color: var(--color-ink, #0a0a08);
  cursor: pointer;
  padding: 0;
  border-radius: 6px;
  flex-shrink: 0;
}
nav.topnav .nav-mobile-toggle:hover { background: rgba(0, 0, 0, 0.05); }

/* Mobile drawer (≤768px). The topnav row keeps brand + theme + account
 * + hamburger visible; the 4 menu items and lang slot drop into a
 * slide-down drawer that opens below the row when the hamburger is tapped.
 *
 * Single-icon hamburger (no morph to X) — drawer closes via tap-outside,
 * tap-link, Escape, or tap the hamburger again. */
@media (max-width: 768px) {
  nav.topnav {
    padding: 8px 16px;
    gap: 8px;
    flex-wrap: wrap;
    justify-content: flex-start;
  }
  nav.topnav .nav-mobile-toggle {
    display: flex;
    order: 5;
  }

  /* Brand stays leftmost. */
  nav.topnav .brand { order: 1; }

  /* The 4-item menu and lang slot collapse into a drawer; theme + account
   * stay visible on the row. */
  nav.topnav .menu {
    display: none;
    flex-basis: 100%;
    flex-direction: column;
    align-items: stretch;
    gap: 0;
    margin: 0;
    padding: 0;
    order: 99;
  }
  nav.topnav .right-cluster {
    /* Right-cluster stays visible but compresses; lang slot is hidden,
     * theme + account stay. Account slot bumps right to anchor against
     * the hamburger. */
    margin-left: auto;
    gap: 4px;
    order: 4;
  }
  nav.topnav .right-cluster .nav-lang-slot { display: none; }
  nav.topnav .right-cluster .nav-mode-slot { display: none; }

  /* When drawer is open, surface the menu + a lang row beneath the topnav row. */
  nav.topnav.is-open .menu {
    display: flex;
    border-top: 1px solid var(--color-rule, rgba(168, 40, 40, 0.22));
    margin-top: 8px;
    padding-top: 4px;
  }
  nav.topnav.is-open .menu a {
    display: block;
    padding: 14px 4px;
    font-size: 13px;
    border-bottom: 1px solid var(--color-rule, rgba(168, 40, 40, 0.22));
    border-left: 0;
  }
  nav.topnav.is-open .menu a:hover,
  nav.topnav.is-open .menu a.current {
    border-bottom-color: var(--color-accent, #a82828);
    background: rgba(168, 40, 40, 0.04);
  }
  /* Reveal the lang slot inside the drawer (full row beneath the menu). */
  nav.topnav.is-open .right-cluster .nav-lang-slot {
    display: flex;
    flex-basis: 100%;
    order: 100;
    padding: 12px 0 4px;
    border-top: 1px solid var(--color-rule, rgba(168, 40, 40, 0.22));
    margin-top: 4px;
  }
  nav.topnav.is-open .right-cluster .nav-lang-slot .lang-pop .lang-menu {
    right: auto;
    left: 0;
  }
  nav.topnav.is-open .right-cluster .nav-lang-slot .lang-btn {
    padding: 10px 14px;
    min-height: 44px;
  }

  /* T-6.6c — touch-target bump for language popup items. */
  nav.topnav .right-cluster .lang-pop .lang-menu-item {
    padding: 12px 18px;
    min-height: 44px;
    font-size: 14px;
    display: flex;
    align-items: center;
  }

  /* Touch-target bump for visible-row icon buttons (theme + signin + account). */
  nav.topnav .right-cluster .icon-btn { width: 40px; height: 40px; }
}

/* Compact tweak below 380px (very narrow) — tighten brand/cluster padding
 * so the row stays single-line with theme + account + hamburger all visible. */
@media (max-width: 379px) {
  nav.topnav { padding: 6px 10px; gap: 4px; }
  nav.topnav .brand .wordmark { font-size: 16px; }
}

/* T-7.2 — Capacitor app safe-area inset. When body.is-app is set (by
 * native-bridge.ts inside the Android app), the topnav grows its top
 * padding by the system status-bar height so the brand + hamburger
 * sit below the clock/battery cluster instead of being covered. On
 * the web the env() value is 0, so this rule is a no-op there. */
body.is-app nav.topnav {
  padding-top: calc(8px + env(safe-area-inset-top, 0px));
}

/* T-7.5 — Bottom-tab bar (Option A). Visible only in the Capacitor app
 * (body.is-app). 4 slots mirror the topnav: Topics / Big Questions /
 * Timeline / Takes. Active slot is set by mobile-nav.js based on the
 * current route. Hidden on the web. */
.app-tabbar { display: none; }
body.is-app .app-tabbar {
  display: flex;
  position: fixed;
  left: 0; right: 0;
  /* Anchor via top from dynamic viewport height — Capacitor WebView's
   * layout viewport can be taller than the visible region, causing
   * `bottom: 0` to drop the bar below the visible bottom. `100dvh`
   * resolves to the visible window height, so `top: 100dvh - h` pins
   * the bar to the visible bottom regardless of layout/visual mismatch. */
  top: calc(100dvh - 56px - env(safe-area-inset-bottom, 0px));
  z-index: 60;
  background: var(--color-page, #fcfbf8);
  border-top: 1px solid var(--color-rule, rgba(168, 40, 40, 0.22));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  /* tab content row is 56px; pb adds the system gesture-bar inset */
  height: calc(56px + env(safe-area-inset-bottom, 0px));
}
body.is-app .app-tabbar .app-tab {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 6px 4px;
  text-decoration: none;
  color: var(--color-ink-secondary, #2a2a26);
  font-family: var(--font-ui, 'Newsreader', serif);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-weight: 600;
  position: relative;
  -webkit-tap-highlight-color: transparent;
}
body.is-app .app-tabbar .app-tab svg {
  width: 22px;
  height: 22px;
  stroke: currentColor;
  stroke-width: 1.5;
  fill: none;
}
body.is-app .app-tabbar .app-tab.is-active {
  color: var(--color-accent, #a82828);
}
body.is-app .app-tabbar .app-tab.is-active::before {
  content: '';
  position: absolute;
  top: 0; left: 50%;
  width: 28px;
  height: 2px;
  background: var(--color-accent, #a82828);
  transform: translateX(-50%);
}
body.is-app .app-tabbar .app-tab:active { background: rgba(168, 40, 40, 0.05); }

/* Make space for the bottom-tab bar so fixed-position UI (sections-pill,
 * ask-bubble) doesn't sit underneath it. CSS variable lets the offsets
 * scale uniformly when the bar exists. */
body.is-app { --app-tabbar-h: 56px; }
body:not(.is-app) { --app-tabbar-h: 0px; }

/* Body padding-bottom so the last paragraph of any page isn't hidden
 * behind the tab bar after scrolling all the way down. */
body.is-app {
  padding-bottom: calc(var(--app-tabbar-h) + env(safe-area-inset-bottom, 0px) + 16px);
  /* Clip horizontal overflow inside the app shell. The home topic carousel
   * uses translateX(-180%) on side cards (intended visual stack, not a
   * scroller); without clipping, those cards extend the layout viewport
   * past the visual viewport. The bottom-tab bar is fixed left:0/right:0
   * which then anchors to the wider layout viewport, pushing TIMELINE/TAKES
   * off-screen and making the bar swipe-scrollable. Clipping here keeps the
   * tab bar pinned to the visual viewport on every route. */
  overflow-x: hidden;
}
