/* ═══════════════════════════════════════════
   HQ Layout Styles
   App shell, top bar, tab nav, view container,
   and responsive breakpoints.
   Depends on tokens.css.
   ═══════════════════════════════════════════ */

/* ── App shell ─────────────────────────────── */
.app-shell {
  background: var(--color-bg);
  color: var(--color-text);
  /* Use 100dvh so the bottom edge tracks iOS Safari's dynamic toolbar.
     With plain 100vh the app-shell renders at the LARGE viewport height
     (URL bar collapsed); when the URL bar is visible, the bottom of
     the shell — including the chat-input-area at the bottom of
     .chat-conversation's flex column — falls behind the URL bar and the
     mic + send buttons become unreachable in portrait. 100vh stays as
     a fallback for browsers without dvh (older Safari). Caught
     2026-05-04 from Brian's iPhone portrait report. */
  height: 100vh;
  height: 100dvh;
  font-family: var(--font-body);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Top bar ───────────────────────────────── */
.top-bar {
  padding: 12px var(--space-6);
  /* Respect iOS notch / status bar / Dynamic Island.
     `env()` falls back to 0 on browsers without safe-area support, so
     non-iOS is unaffected. `padding-top` reserves the status-bar inset
     so the logo + settings/logout buttons aren't hidden under the
     status bar in iOS Safari portrait. Caught 2026-05-04 (hq-v50)
     after hq-v49's switch to 100dvh on .app-shell exposed that the
     top-bar was anchoring under the notch. */
  padding-top: max(12px, env(safe-area-inset-top));
  padding-left: max(var(--space-6), env(safe-area-inset-left));
  padding-right: max(var(--space-6), env(safe-area-inset-right));
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  gap: var(--space-4);
  background: var(--color-panel);
  flex-shrink: 0;
  /* Fixed height so tab nav and view always anchor correctly */
  min-height: 52px;
}

.top-bar__logo {
  font-family: var(--font-display);
  font-size: var(--text-2xl);
  font-weight: 900;
  letter-spacing: -0.5px;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--color-text);
  white-space: nowrap;
  flex-shrink: 0;
}

.top-bar__logo-diamond {
  color: var(--color-amber);
}

.top-bar__status {
  font-size: 11px;
  color: var(--color-dim);
  font-family: var(--font-mono);
  white-space: nowrap;
}

.top-bar__spacer {
  flex: 1;
}

.top-bar__right {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-shrink: 0;
}

/* ── Usage pill (both windows, tap → Budgets & Usage) ─────────── */
.usage-pill {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  padding: 4px 10px;
  border: 1px solid var(--color-border);
  border-radius: 16px;
  background: transparent;
  color: var(--color-dim);
  font-family: var(--font-mono);
  font-size: 11px;
  white-space: nowrap;
  cursor: pointer;
  flex-shrink: 0;
}

.usage-pill--approach {
  color: var(--color-amber);
  border-color: color-mix(in srgb, var(--color-amber) 45%, transparent);
}

.usage-pill--critical {
  color: var(--color-red, #e0604f);
  border-color: color-mix(in srgb, var(--color-red, #e0604f) 55%, transparent);
}

/* ── Tab nav ───────────────────────────────── */
.tab-nav {
  display: flex;
  padding: 0 var(--space-6);
  border-bottom: 1px solid var(--color-border);
  background: var(--color-panel);
  flex-shrink: 0;
  overflow-x: auto;
  overflow-y: hidden;
  /* Smooth momentum scrolling on iOS; otherwise the tab nav feels stiff
     compared to native horizontal scrollers. */
  -webkit-overflow-scrolling: touch;
  /* Hide scrollbar on the tab nav itself */
  scrollbar-width: none;
}

.tab-nav::-webkit-scrollbar {
  display: none;
}

.tab-nav__tab {
  /* Reset native <button> styling — the element is now <button> per the
     2026-04-30 a11y audit (Tier 2 #8) so it is keyboard-reachable, but
     it must visually remain the same flat/divider tab. */
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  font-family: inherit;
  appearance: none;
  -webkit-appearance: none;

  padding: 11px 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--color-dim);
  font-size: var(--text-md);
  font-weight: 600;
  margin-bottom: -1px;
  white-space: nowrap;
  flex-shrink: 0;
  transition: color var(--transition-fast), border-color var(--transition-fast);
  min-height: 44px;
  user-select: none;
}

.tab-nav__tab:hover {
  color: var(--color-text);
}

.tab-nav__tab.active {
  border-bottom-color: var(--color-amber);
  color: var(--color-amber);
}

.tab-nav__tab-icon {
  font-size: var(--text-md);
}

.tab-nav__tab-badge {
  font-size: 9px;
  padding: 1px 5px;
  border-radius: var(--radius-sm);
  background: #E8555533;
  color: var(--color-red);
  font-weight: 700;
  font-family: var(--font-mono);
}

/* ── View container ────────────────────────── */
.view-container {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  /* Each view manages its own internal scroll */
}

/* ── Chat drawer overlay (mobile) ──────────── */
.chat-drawer-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 100;
}

.chat-drawer-overlay.open {
  display: block;
}

/* ── Mobile hamburger ──────────────────────── */
.hamburger-btn {
  display: none;
  background: transparent;
  border: 1px solid var(--color-border);
  color: var(--color-dim);
  padding: 8px 10px;
  border-radius: var(--radius-md);
  cursor: pointer;
  font-size: var(--text-xl);
  min-height: 44px;
  min-width: 44px;
  align-items: center;
  justify-content: center;
}

/* ── Mobile bottom nav (≤768px) ────────────────
   Hidden on desktop; shown by the ≤768px media query below. It is a normal
   flex child of .app-shell (NOT position:fixed), so it anchors to the bottom
   of the column and .view-container flexes to fill the space above it —
   sidestepping the iOS fixed + dynamic-toolbar reachability problems. */
.bottom-nav {
  display: none;
  flex-shrink: 0;
  border-top: 1px solid var(--color-border);
  background: var(--color-panel);
  /* Reserve the iOS home-indicator inset so the last row of the tap targets
     isn't under the gesture bar. Falls back to 0 where unsupported. */
  padding-bottom: env(safe-area-inset-bottom);
}

.bottom-nav__item {
  flex: 1 1 0;
  min-width: 0;
  background: transparent;
  border: none;
  appearance: none;
  -webkit-appearance: none;
  font-family: inherit;
  cursor: pointer;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  /* Generous thumb target: 56px column incl. label; each column ≥ 75px wide
     at 375px (5 columns), comfortably above the 44px minimum. */
  min-height: 56px;
  padding: 6px 2px;
  color: var(--color-dim);
  user-select: none;
  transition: color var(--transition-fast);
}

.bottom-nav__item.active {
  color: var(--color-amber);
}

.bottom-nav__icon {
  font-size: 20px;
  line-height: 1;
}

.bottom-nav__label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0;
  white-space: nowrap;
}

.bottom-nav__badge {
  position: absolute;
  top: 4px;
  left: 50%;
  transform: translateX(4px);
  min-width: 16px;
  padding: 0 4px;
  border-radius: 8px;
  background: var(--color-red);
  color: #fff;
  font-size: 9px;
  font-weight: 700;
  font-family: var(--font-mono);
  line-height: 16px;
  text-align: center;
}

/* ── "More" bottom sheet ───────────────────────
   Only reachable via the bottom-nav More button (phones), but styled
   globally so the markup in the shell is inert until opened. */
.more-sheet-overlay {
  position: fixed;
  inset: 0;
  z-index: 300;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: flex-end;
}

.more-sheet-overlay[hidden] {
  display: none;
}

.more-sheet {
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--color-panel);
  border-top: 1px solid var(--color-border);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  padding: var(--space-3) var(--space-4);
  padding-bottom: max(var(--space-4), env(safe-area-inset-bottom));
  animation: more-sheet-in var(--transition-slow) ease;
}

@keyframes more-sheet-in {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

.more-sheet__handle {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: var(--color-border);
  margin: 2px auto var(--space-3);
}

.more-sheet__grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-2);
}

.more-sheet__item {
  position: relative;
  background: var(--color-panel2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  cursor: pointer;
  font-family: inherit;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 72px;
  padding: var(--space-3) var(--space-2);
  color: var(--color-text);
}

.more-sheet__item.active {
  border-color: var(--color-amber);
  color: var(--color-amber);
}

.more-sheet__icon {
  font-size: 22px;
  line-height: 1;
}

.more-sheet__label {
  font-size: 12px;
  font-weight: 600;
}

.more-sheet__badge {
  position: absolute;
  top: 6px;
  right: 6px;
  min-width: 16px;
  padding: 0 4px;
  border-radius: 8px;
  background: var(--color-red);
  color: #fff;
  font-size: 9px;
  font-weight: 700;
  font-family: var(--font-mono);
  line-height: 16px;
  text-align: center;
}

.more-sheet__actions {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  margin-top: var(--space-3);
}

.more-sheet__action {
  width: 100%;
  min-height: 48px;
  border-radius: var(--radius-lg);
  border: 1px solid var(--color-border);
  background: var(--color-panel2);
  color: var(--color-text);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: 0 var(--space-4);
}

.more-sheet__action--danger {
  color: var(--color-red);
  border-color: #E8555544;
}

.more-sheet__close {
  width: 100%;
  min-height: 44px;
  margin-top: var(--space-3);
  border-radius: var(--radius-lg);
  border: 1px solid var(--color-border);
  background: transparent;
  color: var(--color-dim);
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
}

/* ── Global project scope control (top bar) ─────────────────────
   "HQ is looking at project X." Sits LEFT of the mode pill inside
   .top-bar__right (mount order in app.js decides). Dot + name + caret;
   tap opens a listbox of All projects + every registered project. */
.global-scope {
  position: relative;
  display: inline-flex;
}

.global-scope__btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  padding: 6px 10px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: transparent;
  color: var(--color-text);
  font-family: inherit;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}

.global-scope__btn:hover {
  border-color: var(--color-amber);
}

.global-scope__dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.global-scope__name {
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.global-scope__caret {
  color: var(--color-dim);
  font-size: 10px;
}

.global-scope__dropdown {
  position: absolute;
  top: calc(100% + 6px);
  /* Right-anchored: the control lives on the right side of the top bar, so
     the panel must grow LEFT to stay inside the viewport on narrow windows. */
  right: 0;
  min-width: 200px;
  max-width: min(320px, 90vw);
  max-height: 60vh;
  overflow-y: auto;
  background: var(--color-panel);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
  padding: 4px;
  z-index: 1100;
}

.global-scope__item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  border: none;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--color-text);
  font-family: inherit;
  font-size: 13px;
  text-align: left;
  cursor: pointer;
}

.global-scope__item:hover {
  background: rgba(212, 165, 116, 0.10);
}

.global-scope__item.is-active {
  background: rgba(212, 165, 116, 0.16);
  color: var(--color-amber);
}

.global-scope__item-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ═══════════════════════════════════════════
   Responsive: ≤768px
   ═══════════════════════════════════════════ */
@media (max-width: 768px) {
  .top-bar {
    padding: 10px var(--space-4);
    padding-top: max(10px, env(safe-area-inset-top));
    padding-left: max(var(--space-4), env(safe-area-inset-left));
    padding-right: max(var(--space-4), env(safe-area-inset-right));
    gap: var(--space-2);
  }

  .top-bar__status {
    display: none;
  }

  /* Phone navigation moves to the thumb-reachable bottom nav. Hide the
     horizontal top tab scroller (only ~4 of 10 tabs were visible at 375px)
     and the top-bar action buttons that pushed Settings off the right edge
     — Settings / Logout / mute now live in the More sheet. This is the fix
     for "had to zoom out to see the Settings button" (2026-07-11). */
  .tab-nav {
    display: none;
  }

  .top-bar__right #settings-btn,
  .top-bar__right #logout-btn,
  .top-bar__right #tts-mute-btn {
    display: none;
  }

  .bottom-nav {
    display: flex;
    align-items: stretch;
    justify-content: space-around;
  }

  /* Tighten the action buttons on phone — keep 44px tap size but
     remove excess padding. Settings/Logout get icon-only treatment
     via the label-hiding spans below. */
  .top-bar__right {
    gap: 6px;
  }

  .top-bar__right .btn-icon {
    padding: 6px 10px;
    min-height: 40px;
  }

  /* Global scope dropdown: below ~700px neither left- nor right-anchoring
     reliably fits (the control sits mid-bar with pills on both sides) —
     switch to a viewport-fixed sheet under the top bar, full width minus
     gutters. safe-area term tracks notch devices. */
  .global-scope__dropdown {
    position: fixed;
    left: 12px;
    right: 12px;
    top: calc(max(10px, env(safe-area-inset-top)) + 54px);
    min-width: 0;
    max-width: none;
  }

  .top-bar__logo {
    font-size: 18px;
  }

  .tab-nav {
    padding: 0 var(--space-3);
  }

  .tab-nav__tab {
    padding: 10px 12px;
    font-size: 12px;
  }

  .hamburger-btn {
    display: inline-flex;
  }

  /* Dashboard: single column — Chief chat moves below metrics */
  .dashboard-grid {
    grid-template-columns: 1fr !important;
  }

  .dashboard-chief {
    max-height: 420px !important;
  }

  /* Chat: directory becomes slide-out drawer */
  .chat-directory {
    position: fixed !important;
    top: 0;
    left: 0;
    /* 100dvh tracks iOS Safari's dynamic toolbar; 100vh stays as
       fallback for browsers without dvh. Matches chat.css drawer rule. */
    height: 100vh;
    height: 100dvh;
    z-index: 101;
    transform: translateX(-100%);
    transition: transform var(--transition-slow);
  }

  .chat-directory.open {
    transform: translateX(0);
  }

  /* Feed / Tasks: ensure header wraps cleanly */
  .view-header {
    flex-wrap: wrap;
  }

  .filter-group {
    flex-wrap: wrap;
  }
}

/* ═══════════════════════════════════════════
   Responsive: ≤480px
   ═══════════════════════════════════════════ */
@media (max-width: 480px) {
  .top-bar {
    padding: 8px var(--space-3);
    padding-top: max(8px, env(safe-area-inset-top));
    padding-left: max(var(--space-3), env(safe-area-inset-left));
    padding-right: max(var(--space-3), env(safe-area-inset-right));
  }

  .top-bar__logo {
    font-size: 16px;
  }

  /* Alert badge: keep visible but compact the text (counter still shown). */
  .alert-badge {
    padding: 5px 9px;
    font-size: 10px;
  }

  /* Global scope: stays visible on phone (the whole point is a one-glance
     "what is HQ looking at") but the name column tightens so the mode pill
     and dispatch pill still fit at 375px. */
  .global-scope__btn {
    padding: 6px 8px;
    font-size: 11px;
  }

  .global-scope__name {
    max-width: 84px;
  }


  /* ── Tab labels at ≤480px (audit Tier 1 #4) ─────────────────────
     Earlier this rule hid `.tab-nav__tab-label` and shrank tabs to
     icon-only. Decorative glyphs in isolation are illegible to users
     who don't already know what they mean (audit D1, HIGH). Restore
     the label and shrink it slightly; the tab nav already
     horizontal-scrolls (`.tab-nav { overflow-x: auto }`), so the
     extra width spills into scroll, not into a broken layout. */
  .tab-nav__tab {
    padding: 8px 10px;
    font-size: 10px;
    gap: 4px;
    min-width: 44px;
  }

  .tab-nav__tab-icon {
    font-size: 13px;
  }

  .tab-nav__tab-label {
    /* Compress label so all 9 tabs fit on a 375px viewport via
       horizontal scroll without each tab overshooting its row. */
    font-size: 10px;
    letter-spacing: 0;
  }

  .metric-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  .project-grid {
    grid-template-columns: 1fr !important;
  }

  .tasks-grid {
    grid-template-columns: 1fr !important;
  }

  .team-grid {
    grid-template-columns: 1fr !important;
  }
}
