/*
 * terminal.css — Track M · Impact + Coalitions Terminal (V3)
 *
 * Page-specific layout on top of Track A's tokens (styles.css).
 * Implements the Bloomberg-style terminal: dark KPI bar, 1.4fr Impact
 * Engine | 1fr Coordinate body split.
 *
 * Design ground rules (PRD §6 + track-m.md §10):
 *   - All colours via var(--token). Zero hardcoded hex/rgb values.
 *   - Dark KPI bar uses var(--ink) background.
 *   - Three-family type stack (--serif / --sans / --mono).
 *   - No emoji, no gradients, no rounded-left-border accent strips.
 *
 * Token reference (subset used here):
 *   --paper, --paper-2, --paper-3
 *   --ink, --ink-2, --ink-3, --ink-4
 *   --civic, --civic-soft
 *   --risk, --risk-soft
 *   --support, --support-soft
 *   --rule, --rule-soft
 *   --serif, --sans, --mono
 */

/* ─── Terminal shell ─────────────────────────────────────────────────────── */

.terminal-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

/* ─── Top bar — dark KPI bar ─────────────────────────────────────────────── */
/* PRD §10: dark KPI bar uses var(--ink) background.                          */

.terminal-topbar {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--rule);
  background: var(--ink);
  color: var(--paper);
  flex-shrink: 0;
  min-height: 0;
}

.terminal-topbar__left {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-shrink: 0;
}

.terminal-topbar__wordmark {
  font-size: 20px;
  letter-spacing: -0.02em;
  color: var(--paper);
}

.terminal-topbar__divider {
  width: 1px;
  height: 22px;
  background: rgba(255, 255, 255, 0.2);
}

.terminal-topbar__context {
  font-size: 11px;
  letter-spacing: 0.08em;
  opacity: 0.7;
  color: var(--paper);
}

.terminal-topbar__right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
  margin-left: auto;
}

.terminal-topbar__live {
  font-size: 11px;
  letter-spacing: 0.1em;
  opacity: 0.8;
  color: var(--paper);
}

.terminal-topbar__avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--paper-3);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink);
  font-size: 11px;
  font-weight: 600;
}

/* ─── KPI strip (5 cells inside the dark top bar) ───────────────────────── */

.terminal-kpi-strip {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  flex: 1;
  min-width: 0;
}

.terminal-kpi-cell {
  padding: 6px 18px;
  border-right: 1px solid rgba(255, 255, 255, 0.12);
}

.terminal-kpi-cell--last {
  border-right: none;
}

.terminal-kpi-cell__label {
  font-size: 9px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--paper);
  opacity: 0.55;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.terminal-kpi-cell__value {
  font-size: 26px;
  line-height: 1.05;
  margin-top: 2px;
  color: var(--paper);
}

/* Tone modifiers for KPI values */
.terminal-kpi-cell__value.risk    { color: var(--risk);    }
.terminal-kpi-cell__value.civic   { color: var(--civic);   }
.terminal-kpi-cell__value.support { color: var(--support); }

.terminal-kpi-cell__sub {
  font-size: 10px;
  color: var(--paper);
  opacity: 0.5;
  margin-top: 1px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ─── Body — 2-column split ──────────────────────────────────────────────── */

.terminal-body {
  display: grid;
  grid-template-columns: 1.4fr 1fr;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

/* ─── Left: Impact Engine ────────────────────────────────────────────────── */

.terminal-impact {
  padding: 20px 24px;
  border-right: 1px solid var(--rule);
  overflow-y: auto;
}

.terminal-impact__header {
  display: flex;
  align-items: baseline;
  gap: 24px;
  margin-bottom: 4px;
}

.terminal-impact__header-left {
  display: flex;
  align-items: baseline;
  gap: 24px;
  flex: 1;
  min-width: 0;
}

.terminal-impact__headline {
  font-size: 26px;
  margin: 0;
  letter-spacing: -0.01em;
  color: var(--ink);
  white-space: nowrap;
}

.terminal-impact__tabs {
  display: flex;
  gap: 14px;
}

.terminal-tab {
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  background: none;
  border: none;
  border-bottom: 1.5px solid transparent;
  cursor: pointer;
  padding: 4px 0;
  color: var(--ink-3);
  transition: color 0.1s ease, border-color 0.1s ease;
}

.terminal-tab:hover {
  color: var(--ink);
}

.terminal-tab--active {
  border-bottom-color: var(--ink);
  color: var(--ink);
}

.terminal-impact__bill-label {
  font-size: 12px;
  color: var(--ink-3);
  margin-bottom: 16px;
}

/* ─── Tab panels ─────────────────────────────────────────────────────────── */

.terminal-tab-panel { display: block; }
.terminal-tab-panel[hidden] { display: none; }

.terminal-tab-panel__empty {
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ─── Waterfall chart ────────────────────────────────────────────────────── */

.terminal-waterfall {
  border: 1px solid var(--rule);
  background: var(--paper);
  padding: 18px;
  margin-bottom: 18px;
}

.terminal-waterfall__header {
  font-size: 9px;
  color: var(--ink-3);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  margin-bottom: 12px;
}

.terminal-waterfall__chart {
  height: 160px;
  margin-bottom: 14px;
  position: relative;
}

.terminal-waterfall__loading {
  height: 100%;
}

/* SVG waterfall rendered by terminal.js fills this container */
.terminal-waterfall__chart svg {
  width: 100%;
  height: 100%;
  display: block;
}

.terminal-waterfall__footer {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--ink-3);
  padding-top: 12px;
  border-top: 1px solid var(--rule-soft);
}

/* ─── Who-benefits / who-pays grid ──────────────────────────────────────── */

.terminal-who-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin-bottom: 18px;
}

.terminal-who-col {
  border: 1px solid var(--rule);
  padding: 14px;
}

.terminal-who-col__header {
  font-size: 9px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  margin-bottom: 10px;
}

.terminal-who-col__header.support { color: var(--support); }
.terminal-who-col__header.risk    { color: var(--risk);    }

.terminal-who-row {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  padding: 6px 0;
  border-top: 1px dashed var(--rule-soft);
}

.terminal-who-row:first-child { border-top: none; }

.terminal-who-row__label { color: var(--ink-2); }
.terminal-who-row__value { color: var(--ink); }

.terminal-who-row--empty {
  font-size: 12px;
  color: var(--ink-3);
}

/* ─── Argument provenance panel ──────────────────────────────────────────── */

.terminal-provenance {
  border: 1px solid var(--rule);
  padding: 14px;
  margin-bottom: 4px;
}

.terminal-provenance__header {
  font-size: 9px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-3);
  margin-bottom: 10px;
}

.terminal-provenance__loading {
  font-size: 12px;
  color: var(--ink-3);
  padding: 6px 0;
}

.terminal-provenance__empty {
  font-size: 12px;
  color: var(--ink-3);
  padding: 8px 0;
}

.terminal-provenance-row {
  padding: 10px 0;
  border-top: 1px solid var(--rule-soft);
}

.terminal-provenance-row__claim {
  font-size: 12.5px;
  color: var(--ink);
  margin-bottom: 4px;
}

.terminal-provenance-row__meta {
  display: flex;
  gap: 10px;
  font-size: 10.5px;
  color: var(--ink-3);
  align-items: center;
  flex-wrap: wrap;
}

.terminal-provenance-row__source { }
.terminal-provenance-row__funder { }

.terminal-provenance-row__chip {
  margin-left: auto;
}

/* ─── Sources tab panel ──────────────────────────────────────────────────── */

.terminal-source-row {
  padding: 12px 0;
  border-top: 1px solid var(--rule-soft);
}

.terminal-source-row:first-child { border-top: none; }

.terminal-source-row__name {
  font-size: 13px;
  color: var(--ink);
  font-weight: 500;
  margin-bottom: 4px;
}

.terminal-source-row__kind {
  font-size: 10px;
  color: var(--ink-3);
  font-family: var(--mono);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  margin-bottom: 6px;
}

.terminal-source-row__funder {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  color: var(--ink-2);
  padding: 3px 0;
}

.terminal-source-row__funder-pct {
  font-family: var(--mono);
  color: var(--ink-3);
  font-size: 11px;
}

/* ─── Right: Coordinate / Coalitions ────────────────────────────────────── */

.terminal-coordinate {
  padding: 20px 24px;
  overflow-y: auto;
  background: var(--paper-2);
}

.terminal-coordinate__headline {
  font-size: 22px;
  margin: 0 0 4px;
  letter-spacing: -0.01em;
  color: var(--ink);
}

.terminal-coordinate__sub {
  font-size: 12px;
  color: var(--ink-3);
  margin-bottom: 16px;
}

/* ─── Coalition list ─────────────────────────────────────────────────────── */

.terminal-coalition-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 16px;
}

.terminal-coalitions-loading {
  font-size: 12px;
  color: var(--ink-3);
}

.terminal-coalition-card {
  background: var(--paper);
  border: 1px solid var(--rule);
  padding: 16px;
}

.terminal-coalition-card__top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 0;
}

.terminal-coalition-card__info {
  flex: 1;
  min-width: 0;
}

.terminal-coalition-card__badges {
  display: flex;
  gap: 6px;
  align-items: center;
  margin-bottom: 6px;
}

.terminal-coalition-card__match {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--ink-3);
}

.terminal-coalition-card__name {
  font-family: var(--serif);
  font-size: 16px;
  line-height: 1.3;
  color: var(--ink);
  overflow-wrap: break-word;
}

.terminal-coalition-card__join {
  font-family: inherit;
  font-size: 11px;
  padding: 6px 10px;
  cursor: pointer;
  border-radius: 2px;
  white-space: nowrap;
  flex-shrink: 0;
  transition: all 0.12s ease;
}

.terminal-coalition-card__join--out {
  border: 1px solid var(--ink);
  background: var(--ink);
  color: var(--paper);
}

.terminal-coalition-card__join--out:hover {
  background: var(--ink-2);
  border-color: var(--ink-2);
}

.terminal-coalition-card__join--in {
  border: 1px solid var(--support);
  background: var(--support-soft);
  color: oklch(32% 0.08 150);
}

.terminal-coalition-card__join--in:hover {
  background: var(--support-soft);
  opacity: 0.85;
}

.terminal-coalition-card__stats {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-top: 12px;
  margin-bottom: 6px;
}

.terminal-coalition-card__member-count {
  font-family: var(--serif);
  font-size: 26px;
  line-height: 1;
  color: var(--ink);
}

.terminal-coalition-card__growth {
  font-size: 11px;
  color: var(--ink-3);
}

.terminal-coalition-card__growth-count {
  color: var(--support);
}

.terminal-coalition-card__needs {
  font-size: 11px;
  color: var(--ink-2);
  margin-bottom: 6px;
}

.terminal-coalition-card__progress-track {
  height: 3px;
  background: var(--paper-3);
  margin-bottom: 4px;
}

.terminal-coalition-card__progress-fill {
  height: 100%;
  background: var(--civic);
}

.terminal-coalition-card__progress-labels {
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  color: var(--ink-3);
  font-family: var(--mono);
}

.terminal-coalition-card__actions {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px dashed var(--rule);
  display: flex;
  gap: 8px;
  font-size: 11px;
}

.terminal-coalition-card__actions .btn {
  flex: 1;
}

/* ─── Empty state for coalition list ────────────────────────────────────── */

.terminal-coalitions-empty {
  border: 1px solid var(--rule);
  padding: 24px;
  text-align: center;
}

.terminal-coalitions-empty__message {
  font-size: 12px;
  color: var(--ink-2);
  margin-bottom: 8px;
}

/* ─── Start coalition CTA ────────────────────────────────────────────────── */

.terminal-start-coalition {
  padding: 14px;
  border: 1px dashed var(--rule);
  text-align: center;
}

.terminal-start-coalition__prompt {
  font-size: 12px;
  color: var(--ink-2);
  margin-bottom: 6px;
}

/* ─── Responsive collapse ────────────────────────────────────────────────── */
/* Below 900px: coalitions panel stacks below impact engine.                  */

@media (max-width: 900px) {
  .terminal-body {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto;
    overflow-y: auto;
  }

  .terminal-impact {
    border-right: none;
    border-bottom: 1px solid var(--rule);
    max-height: none;
    overflow-y: visible;
  }

  .terminal-coordinate {
    max-height: none;
    overflow-y: visible;
  }

  .terminal-kpi-strip {
    grid-template-columns: repeat(3, 1fr);
  }

  /* Hide last 2 KPI cells on narrow screens */
  .terminal-kpi-cell:nth-child(4),
  .terminal-kpi-cell:nth-child(5) {
    display: none;
  }
}

/* Tablet portrait (641–820px) — retain the 2-rail Impact + Coordinate
 * side-by-side layout instead of stacking. The KPI strip stays at 5
 * columns since terminal is a 2-rail page already; only the column ratios
 * tighten so the right rail keeps room for the coalition cards. */
@media (min-width: 641px) and (max-width: 820px) {
  .terminal-body {
    grid-template-columns: 1.2fr 1fr;
    grid-template-rows: none;
    overflow: hidden;
  }
  .terminal-impact {
    border-right: 1px solid var(--rule);
    border-bottom: none;
    padding: 16px 18px;
    overflow-y: auto;
  }
  .terminal-coordinate {
    padding: 16px 18px;
    overflow-y: auto;
  }
  .terminal-kpi-strip {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
  }
  .terminal-kpi-cell:nth-child(4),
  .terminal-kpi-cell:nth-child(5) {
    display: block;
  }
}

@media (max-width: 640px) {
  .terminal-topbar {
    flex-wrap: wrap;
    gap: 8px;
    padding: 10px 16px;
  }

  /* HP65 §13: previously the KPI strip dropped to 2 cells AND hid the
   * other 3 — losing user-relevant context (Coalitions joined / Rep
   * alignment / Deadlines). Switch to a horizontal-scroll strip that
   * keeps ALL 5 cells in view, swipeable. Each cell gets a min-width
   * so labels stay legible.                                            */
  .terminal-kpi-strip {
    display: flex;
    grid-template-columns: none;
    width: 100%;
    order: 3;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-snap-type: x proximity;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .terminal-kpi-strip::-webkit-scrollbar { display: none; }

  .terminal-kpi-cell,
  .terminal-kpi-cell:nth-child(3),
  .terminal-kpi-cell:nth-child(4),
  .terminal-kpi-cell:nth-child(5) {
    display: flex;
    flex-direction: column;
    flex: 0 0 auto;
    min-width: 140px;
    scroll-snap-align: start;
    border-right: 1px solid var(--rule-soft);
  }
  .terminal-kpi-cell:last-child {
    border-right: none;
  }

  .terminal-topbar__right {
    margin-left: 0;
  }

  .terminal-impact,
  .terminal-coordinate {
    padding: 16px;
  }
}

/* ── 375px-class refinements (≤480px) ────────────────────────────────────── */
/* Mobile responsiveness pass (HP52 §8.B.2). The Impact-Engine header was
 * a single flex row with `white-space: nowrap` on the headline at 26px —
 * at 375px viewport that overflowed and crowded the tabs. Stack the
 * headline above the tabs and let it wrap. The who-benefits/who-pays
 * grid also needs to collapse: at 375px each 1fr cell is too narrow to
 * show "Renters · district 9" + value side-by-side. */
@media (max-width: 480px) {
  .terminal-impact__header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  .terminal-impact__header-left {
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
    width: 100%;
  }
  .terminal-impact__headline {
    font-size: 22px;
    white-space: normal;
    line-height: 1.15;
  }
  .terminal-impact__tabs {
    gap: 10px;
    flex-wrap: wrap;
  }
  .terminal-who-grid {
    grid-template-columns: 1fr;
  }
  .terminal-coordinate__headline { font-size: 19px; }
  .terminal-topbar__wordmark { font-size: 17px; }
  .terminal-kpi-cell { padding: 6px 12px; }
  .terminal-kpi-cell__value { font-size: 22px; }
}
