/* ==========================================================================
   RaceProof Dashboards — Master Stylesheet
   Shared across index.html, user_behavior.html, event_intelligence.html
   Editorial / Tufte-inspired. Source Serif 4 + IBM Plex Sans + IBM Plex Mono.
   Light default, dark mode via prefers-color-scheme.
   ========================================================================== */

/* ---------- Tokens ---------- */
:root {
  --serif: 'Source Serif 4', Georgia, serif;
  --sans: 'IBM Plex Sans', -apple-system, sans-serif;
  --mono: 'IBM Plex Mono', monospace;

  --bg: #faf9f7;
  --bg-card: #f2f0ec;
  --bg-annotation: #fdfbf5;

  --text: #1a1a18;
  --text-secondary: #6b6a65;
  --text-tertiary: #9c9b96;
  --border: #d8d6d0;

  --blue: #2b6cb0;
  --blue-light: rgba(43,108,176,0.12);
  --coral: #c05621;
  --coral-light: rgba(192,86,33,0.12);
  --teal: #1a7a5c;
  --teal-light: rgba(26,122,92,0.12);
  --amber: #b7791f;
  --pink: #97266d;

  --positive: #2b8a3e;
  --negative: #c92a2a;

  --finding: #2b6cb0;
  --question: #b7791f;
  --caveat: #c05621;

  --grid: rgba(0,0,0,0.06);
  --tick: rgba(0,0,0,0.45);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a18;
    --bg-card: #26261f;
    --bg-annotation: #2a2820;

    --text: #e8e6e1;
    --text-secondary: #9c9b96;
    --text-tertiary: #6b6a65;
    --border: #3d3d35;

    --blue: #63b3ed;
    --blue-light: rgba(99,179,237,0.15);
    --coral: #ed8936;
    --coral-light: rgba(237,137,54,0.15);
    --teal: #4fd1c5;
    --teal-light: rgba(79,209,197,0.15);
    --amber: #ecc94b;
    --pink: #ed64a6;

    --positive: #6cd97e;
    --negative: #e57373;

    --finding: #63b3ed;
    --question: #ecc94b;
    --caveat: #ed8936;

    --grid: rgba(255,255,255,0.07);
    --tick: rgba(255,255,255,0.5);
  }
}

/* ---------- Reset & base ---------- */
* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  max-width: 1200px;
  margin: 0 auto;
  padding: calc(56px + 3rem) 2rem 4rem;
  -webkit-font-smoothing: antialiased;
}

/* Prose stays at a comfortable reading width even though the canvas is wide.
   Data viz (charts, heatmaps, lists, KPI row) fills the full body width. */

/* ---------- Headline / lede ---------- */
h1 {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 2rem;
  line-height: 1.2;
  margin-bottom: 0.25rem;
}

.dateline {
  font-size: 0.8rem;
  color: var(--text-tertiary);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  margin-bottom: 1.5rem;
}

.lede {
  font-family: var(--serif);
  font-size: 1.15rem;
  line-height: 1.6;
  color: var(--text-secondary);
  margin-bottom: 2.5rem;
  max-width: 640px;
}

/* ---------- Sections ---------- */
.section { margin-bottom: 3.5rem; }

.section-kicker {
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-tertiary);
  margin-bottom: 0.25rem;
}

.section-title {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 1.35rem;
  margin-bottom: 0.35rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.section-desc {
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin-bottom: 1.25rem;
  max-width: 640px;
  line-height: 1.55;
}

.divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 3rem 0;
}

/* Inline info tooltip next to a section title */
.info-tip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1px solid var(--text-tertiary);
  color: var(--text-tertiary);
  font-family: var(--sans);
  font-size: 0.65rem;
  font-weight: 600;
  font-style: italic;
  cursor: help;
  user-select: none;
}

/* ---------- Stat boxes (KPI-style small) ---------- */
.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
  margin-bottom: 1.5rem;
}

.stat-box {
  background: var(--bg-card);
  border-radius: 6px;
  padding: 14px 16px;
}

.stat-label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-tertiary);
}

.stat-value {
  font-size: 1.5rem;
  font-weight: 600;
  margin-top: 2px;
}

.stat-note {
  font-size: 0.72rem;
  color: var(--text-secondary);
  margin-top: 1px;
}

/* ---------- KPI cards (larger, BI-style) ---------- */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
  margin-bottom: 2rem;
}

.kpi {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 16px 18px;
}

.kpi-label {
  font-size: 0.68rem;
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
  margin-bottom: 6px;
}

.kpi-value {
  font-size: 1.75rem;
  font-weight: 600;
  font-family: var(--mono);
  color: var(--text);
  line-height: 1.1;
  letter-spacing: -0.01em;
}

.kpi-delta {
  margin-top: 6px;
  font-size: 0.75rem;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.kpi-delta.positive { color: var(--positive); }
.kpi-delta.negative { color: var(--negative); }
.kpi-delta.neutral  { color: var(--text-tertiary); }
.kpi-delta .baseline { color: var(--text-tertiary); font-weight: 400; }

/* ---------- Charts ---------- */
.chart-container {
  position: relative;
  width: 100%;
}

/* ---------- Legend ---------- */
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-bottom: 10px;
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: help;
}

.legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 2px;
  flex-shrink: 0;
}

.legend-footnote {
  font-size: 0.72rem;
  color: var(--text-tertiary);
  margin-top: 6px;
  line-height: 1.5;
  max-width: 640px;
}

/* ---------- Annotation (inline italic) ---------- */
.annotation {
  font-size: 0.8rem;
  font-style: italic;
  color: var(--text-secondary);
  margin-top: 8px;
  line-height: 1.5;
}

/* ---------- Annotation panels (finding/question/caveat) ---------- */
.annotation-panel {
  background: var(--bg-annotation);
  border-left: 3px solid var(--finding);
  border-radius: 0 6px 6px 0;
  padding: 14px 18px;
  margin-top: 12px;
  font-size: 0.82rem;
  line-height: 1.55;
}

.annotation-panel.question { border-left-color: var(--question); }
.annotation-panel.caveat   { border-left-color: var(--caveat); }

.annotation-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.annotation-type {
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 2px 7px;
  border-radius: 3px;
  color: #fff;
}

.annotation-type.finding  { background: var(--finding); }
.annotation-type.question { background: var(--question); }
.annotation-type.caveat   { background: var(--caveat); }

.annotation-title { font-weight: 600; color: var(--text); }
.annotation-body  { color: var(--text-secondary); }

.annotation-research { margin-top: 8px; padding-left: 0; list-style: none; }
.annotation-research li {
  padding: 3px 0 3px 14px;
  position: relative;
  font-size: 0.78rem;
  color: var(--text-secondary);
}
.annotation-research li::before {
  content: "";
  position: absolute;
  left: 0;
  top: 10px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--border);
}

.annotation-assessment {
  margin-top: 8px;
  padding: 8px 12px;
  background: var(--bg-card);
  border-radius: 4px;
  font-size: 0.8rem;
  color: var(--text);
}

.annotation-status {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-tertiary);
  margin-top: 6px;
}

/* ---------- Quality banner ---------- */
.quality-banner {
  background: var(--bg-annotation);
  border: 1px solid var(--amber);
  border-radius: 6px;
  padding: 14px 18px;
  margin-bottom: 2.5rem;
  font-size: 0.85rem;
  line-height: 1.5;
}

.quality-banner-title {
  font-weight: 600;
  color: var(--amber);
  margin-bottom: 4px;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.quality-banner-text { color: var(--text-secondary); }

.quality-badge {
  display: inline-block;
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 2px 8px;
  border-radius: 3px;
  background: var(--amber);
  color: #fff;
  margin-left: 6px;
  vertical-align: middle;
}

/* ---------- Refresh banner ---------- */
.refresh-banner {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 16px;
  margin-bottom: 1rem;
  display: none;
  font-size: 0.82rem;
  line-height: 1.5;
  align-items: center;
  gap: 10px;
}

.refresh-banner.visible    { display: flex; }
.refresh-banner.refreshing { border-color: var(--blue); }

.refresh-text { flex: 1; color: var(--text-secondary); }

.refresh-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 5px 14px;
  font-size: 0.78rem;
  font-family: var(--sans);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}

.refresh-btn:hover {
  background: var(--bg-card);
  border-color: var(--text-tertiary);
}

.refresh-btn:disabled { opacity: 0.5; cursor: default; }

.refresh-spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Loading / error / meta ---------- */
.loading {
  text-align: center;
  padding: 4rem 0;
  color: var(--text-tertiary);
  font-size: 0.9rem;
}

.error-msg {
  text-align: center;
  padding: 2rem;
  color: var(--coral);
  font-size: 0.9rem;
}

.meta {
  font-size: 0.72rem;
  color: var(--text-tertiary);
  text-align: right;
  margin-top: 3rem;
  font-family: var(--mono);
}

/* ---------- Toggle groups (button pills) ---------- */
.toggle-group {
  display: inline-flex;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px;
  margin: 0 8px 12px 0;
}

.toggle-group button {
  background: transparent;
  border: 0;
  color: var(--text-secondary);
  font-family: var(--sans);
  font-size: 0.72rem;
  font-weight: 500;
  padding: 5px 12px;
  cursor: pointer;
  border-radius: 3px;
  transition: background-color 100ms ease, color 100ms ease;
}

.toggle-group button:hover { color: var(--text); }

.toggle-group button.active {
  background: var(--blue);
  color: #fff;
}

/* ---------- List rows (top routes / countries) ---------- */
.list-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  gap: 12px;
  font-size: 0.82rem;
}

.list-row:last-child { border-bottom: none; }

.list-row .name {
  color: var(--text);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.list-row .bar-wrap {
  position: relative;
  height: 6px;
  background: var(--bg-card);
  border-radius: 3px;
  margin-top: 4px;
}

.list-row .bar {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  background: var(--blue);
  border-radius: 3px;
}

.list-row .value {
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--text-secondary);
  white-space: nowrap;
}

.list-row .sub {
  font-size: 0.7rem;
  color: var(--text-tertiary);
  margin-top: 2px;
}

/* ---------- Two-column grid ---------- */
.two-col {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(420px, 1fr));
  gap: 2.5rem;
  margin-bottom: 3.5rem;
}
/* Sections inside a two-col grid don't need their own bottom margin — the grid gap handles it. */
.two-col > .section { margin-bottom: 0; }

/* Scrollable list container (used by Top Routes / Top Countries). */
.list-scroll {
  max-height: 360px;
  overflow-y: auto;
  padding-right: 6px;
}
.list-scroll::-webkit-scrollbar { width: 8px; }
.list-scroll::-webkit-scrollbar-track { background: transparent; }
.list-scroll::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}
.list-scroll::-webkit-scrollbar-thumb:hover { background: var(--text-tertiary); }

/* ---------- Migration grid (user_behavior) ---------- */
.migration-grid {
  display: grid;
  grid-template-columns: auto repeat(4, 1fr);
  gap: 2px;
  font-size: 0.82rem;
  margin-top: 8px;
}

.migration-cell {
  padding: 8px 6px;
  text-align: center;
  border-radius: 3px;
}

.migration-header {
  font-weight: 600;
  color: var(--text-secondary);
  padding: 8px 6px;
  text-align: center;
}

.migration-label {
  font-weight: 500;
  color: var(--text-secondary);
  padding: 8px 6px;
  text-align: right;
}

/* ---------- Dashboard nav ---------- */
.dash-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  padding: 0 clamp(1.5rem, 5vw, 4rem);
  height: 56px;
  background: rgba(12, 12, 14, 0.85);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  overflow-x: auto;
}

.dash-nav-brand {
  color: #c4f04d;
  font-weight: 500;
  text-decoration: none;
  margin-right: 2rem;
  white-space: nowrap;
}

.dash-nav-brand:hover { color: #c4f04d; }

.dash-nav a.dash-nav-link {
  color: #9a9890;
  text-decoration: none;
  padding: 0;
  margin-right: 2rem;
  white-space: nowrap;
  transition: color 0.2s;
}

.dash-nav a.dash-nav-link:last-child { margin-right: 0; }
.dash-nav a.dash-nav-link:hover { color: #e8e6e1; }
.dash-nav a.dash-nav-link.active { color: #e8e6e1; }

/* ---------- Responsive ---------- */
@media (max-width: 640px) {
  body { padding: calc(56px + 1rem) 1rem 3rem; }
  h1 { font-size: 1.6rem; }
  .lede { font-size: 1.05rem; }
  .kpi-row { grid-template-columns: 1fr; }
  .kpi-value { font-size: 1.5rem; }
  .section { margin-bottom: 2.5rem; }
}
