/* ==========================================================================
   Carnage Analytics — design system
   Direction : télémétrie d'after-party. Identité portée par la typo
   (Bricolage Grotesque en display) et la ligne « pulse », pas par de nouvelles
   couleurs : les couleurs de DONNÉES restent la palette dataviz validée
   (CVD/contraste — skill dataviz, palette.md). Les surfaces de cartes sont
   EXACTEMENT celles contre lesquelles la palette a été validée ; seul le plan
   de page est réchauffé.
   ========================================================================== */

:root {
  /* Plans & encre */
  --page: #f4f3ee;
  --surface-1: #fcfcfb;          /* surface de chart validée — ne pas dériver */
  --text-primary: #131311;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);

  /* Catégoriel (slots utilisés uniquement) */
  --series-1: #2a78d6;           /* accent / barres nominales */
  --series-1-soft: #cde2fb;      /* piste des meters (même rampe, pas de gris) */
  --deemphasis: #c3c2b7;         /* forme « emphasis » : le contexte en gris */

  /* Statut (réservé aux issues de partie — jamais réutilisé en série) */
  --status-good: #0ca30c;
  --status-warning: #fab219;
  --status-serious: #ec835a;
  --status-critical: #d03b3b;

  --focus-ring: #2a78d6;
  --shadow: 0 1px 2px rgba(19, 19, 17, 0.04), 0 4px 14px rgba(19, 19, 17, 0.04);

  --font-display: "Bricolage Grotesque", system-ui, -apple-system, sans-serif;
  --font-body: system-ui, -apple-system, "Segoe UI", sans-serif;
}

[data-theme="dark"] {
  --page: #0c0c0b;
  --surface-1: #1a1a19;
  --text-primary: #f5f4f0;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --series-1: #3987e5;
  --series-1-soft: #184f95;
  --deemphasis: #52514e;
  --shadow: none;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --page: #0c0c0b;
    --surface-1: #1a1a19;
    --text-primary: #f5f4f0;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --series-1: #3987e5;
    --series-1-soft: #184f95;
    --deemphasis: #52514e;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
}

html, body { margin: 0; padding: 0; }

/* Les ancres de section s'arrêtent sous la topbar sticky. */
main section[id], main .group-head { scroll-margin-top: 76px; }

body {
  background: var(--page);
  color: var(--text-primary);
  font: 14px/1.45 var(--font-body);
  -webkit-font-smoothing: antialiased;
}

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.86em;
  color: var(--text-secondary);
}

/* --------------------------------------------------------------- topbar */
/* Sticky : reste disponible en scrollant ; se condense (pulse repliée) et
   gagne un voile + hairline une fois décollée du haut de page. */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--page) 82%, transparent);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid transparent;
  transition: border-color 0.2s ease;
}

.topbar.is-stuck { border-bottom-color: var(--border); }

.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px 16px;
  max-width: 1160px;
  margin: 0 auto;
  padding: 16px 24px 12px;
}

.brand { min-width: 0; }

.wordmark-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.logo-h1 { margin: 0; line-height: 0; }

.logo {
  height: 44px;
  width: auto;
  display: block;
  transition: height 0.2s ease;
}

.topbar.is-stuck .logo { height: 32px; }

.wordmark-sub {
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 600;
  color: var(--series-1);
  letter-spacing: 0.02em;
}

/* Ligne « pulse » : la lecture du jour en une phrase — l'élément signature.
   Repliée quand la barre est collée, pour condenser le header au scroll. */
.pulse {
  margin: 8px 0 0;
  font-size: 14px;
  color: var(--text-secondary);
  max-width: 68ch;
  overflow: hidden;
  max-height: 3em;
  opacity: 1;
  transition: max-height 0.2s ease, opacity 0.2s ease, margin 0.2s ease;
}
.pulse strong { color: var(--text-primary); font-weight: 650; }

.topbar.is-stuck .pulse { max-height: 0; opacity: 0; margin-top: 0; }

/* ---------------------------------------------------- rail de navigation */
/* Sommaire A–H dans la gouttière gauche (desktop large) : scroll-spy +
   saut direct de section. Masqué quand la gouttière n'existe pas. */
.section-rail {
  position: fixed;
  z-index: 15;
  top: 50%;
  transform: translateY(-50%);
  left: calc((100vw - 1160px) / 2 - 58px);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

@media (max-width: 1290px) { .section-rail { display: none; } }

.rail-item {
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-secondary);
  font-family: var(--font-display);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  padding: 0;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.rail-item:hover { border-color: var(--axis); color: var(--text-primary); }

.rail-item.is-active {
  background: var(--series-1);
  border-color: var(--series-1);
  color: var(--surface-1);
}

.badge {
  font-size: 10.5px;
  font-weight: 650;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  align-self: center;
}

.badge-mock { color: var(--status-serious); border-color: currentColor; }

.topbar-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.sync-status { font-size: 12px; color: var(--text-muted); white-space: nowrap; }
.sync-status .sync-delta { color: var(--series-1); font-weight: 650; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font: inherit;
  font-size: 13px;
  font-weight: 550;
  color: var(--text-primary);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 7px 13px;
  cursor: pointer;
  box-shadow: var(--shadow);
}

.btn:hover { border-color: var(--axis); }
.btn:focus-visible, select:focus-visible, .link-btn:focus-visible, [tabindex="0"]:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 1px;
}
.btn-icon { padding: 7px 10px; }

.btn.is-busy { opacity: 0.7; pointer-events: none; }
.btn.is-busy .refresh-icon { animation: spin 0.9s linear infinite; }

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

/* --------------------------------------------------------------- status */
.status {
  max-width: 1160px;
  margin: 18px auto;
  padding: 0 24px;
  color: var(--text-secondary);
}

.status .status-inner {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 30px;
  text-align: center;
}

.status.is-error .status-inner { border-color: var(--status-critical); }
.status .status-title { font-weight: 600; color: var(--text-primary); margin: 0 0 4px; }
.status p { margin: 0 0 12px; }

/* ----------------------------------------------------------------- main */
main {
  max-width: 1160px;
  margin: 0 auto;
  padding: 10px 24px 44px;
  transition: opacity 0.15s ease;
}

main.is-refreshing { opacity: 0.55; pointer-events: none; }

/* Entrée orchestrée : cartes et bandeaux montent en fondu, décalés. */
main > * { animation: rise 0.4s cubic-bezier(0.22, 0.9, 0.3, 1) both; }
main > *:nth-child(1) { animation-delay: 0.02s; }
main > *:nth-child(2) { animation-delay: 0.06s; }
main > *:nth-child(3) { animation-delay: 0.10s; }
main > *:nth-child(4) { animation-delay: 0.14s; }
main > *:nth-child(5) { animation-delay: 0.18s; }
main > *:nth-child(6) { animation-delay: 0.22s; }
main > *:nth-child(7) { animation-delay: 0.26s; }
main > *:nth-child(8) { animation-delay: 0.30s; }

@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}

/* Les remplissages poussent depuis la gauche au premier rendu. */
.cellbar-fill, .hbar-fill, .stackbar .seg {
  transform-origin: left center;
  animation: grow 0.5s cubic-bezier(0.22, 0.9, 0.3, 1) both;
  animation-delay: 0.15s;
}

@keyframes grow {
  from { transform: scaleX(0); }
  to { transform: scaleX(1); }
}

@media (prefers-reduced-motion: reduce) {
  main > *, .cellbar-fill, .hbar-fill, .stackbar .seg { animation: none; }
  .btn.is-busy .refresh-icon { animation: none; }
  main { transition: none; }
}

/* ------------------------------------------------------------------ KPI */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin: 16px 0 4px;
}

.kpi {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 15px 17px 13px;
  box-shadow: var(--shadow);
}

.kpi-label { font-size: 12px; color: var(--text-secondary); margin: 0 0 5px; }

.kpi-value {
  font-family: var(--font-display);
  font-size: 33px;
  font-weight: 750;
  letter-spacing: -0.01em;
  line-height: 1;
}

.kpi-hint { font-size: 11.5px; color: var(--text-muted); margin-top: 6px; }

/* --------------------------------------------------------------- filtres */
.filter-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 14px;
  margin: 16px 0 6px;
}

.filter { display: inline-flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--text-secondary); }

.filter select {
  font: inherit;
  color: var(--text-primary);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 8px;
}

.filter-note { font-size: 11.5px; color: var(--text-muted); }

/* ---------------------------------------------------------------- cards */
.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 14px;
  margin-top: 16px;
  box-shadow: var(--shadow);
}

.card-head { padding: 17px 20px 0; }

.card-head h2 {
  font-family: var(--font-display);
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.005em;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.section-letter {
  flex: none;
  width: 24px;
  height: 24px;
  display: inline-grid;
  place-items: center;
  border-radius: 7px;
  font-family: var(--font-display);
  font-size: 12.5px;
  font-weight: 800;
  color: var(--surface-1);
  background: var(--series-1);
}

.card-sub { margin: 7px 0 0; font-size: 12.5px; color: var(--text-secondary); }

.card-body { padding: 12px 20px 18px; }

.group-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 16px;
  margin: 30px 2px 0;
}

.group-head h2 {
  font-family: var(--font-display);
  font-size: 16px;
  font-weight: 700;
  margin: 0;
}

.dimension-grid { display: grid; grid-template-columns: 1fr; }

/* Item de grid : sans min-width:0, le minimum automatique remonte le
   min-content de la table interne et la carte déborde du viewport au lieu
   de laisser son .table-scroll scroller. */
.dimension-grid > .card { min-width: 0; }

/* ------------------------------------------------------------ aide « ? » */
.kpi-head { justify-content: flex-start; gap: 8px; margin: 18px 2px 0; }
.kpi-head + .kpi-row { margin-top: 10px; }

.help-btn {
  flex: none;
  width: 17px;
  height: 17px;
  display: inline-grid;
  place-items: center;
  padding: 0;
  border-radius: 50%;
  border: 1px solid var(--axis);
  background: transparent;
  color: var(--text-muted);
  font: 600 11px/1 var(--font-body);
  cursor: help;
  transition: color 0.15s ease, border-color 0.15s ease;
}

.help-btn:hover,
.help-btn:focus-visible,
.help-btn[aria-expanded="true"] {
  color: var(--series-1);
  border-color: var(--series-1);
}

.help-popover {
  position: fixed;
  z-index: 40;
  max-width: 360px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.18);
  padding: 12px 14px;
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--text-secondary);
}

.help-popover p { margin: 0; }
.help-popover p + p { margin-top: 8px; }
.help-popover strong { color: var(--text-primary); font-weight: 650; }

/* --------------------------------------------------------------- légende */
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 12px; color: var(--text-secondary); }
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.legend-swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }

/* ---------------------------------------------------------------- tables */
.table-scroll { overflow-x: auto; }

table { border-collapse: collapse; width: 100%; }

th {
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  padding: 7px 10px;
  border-bottom: 1px solid var(--grid);
  white-space: nowrap;
}

td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--grid);
  vertical-align: middle;
  color: var(--text-primary);
}

tr:last-child td { border-bottom: none; }

tbody tr { transition: background 0.1s ease; }
tbody tr:hover { background: color-mix(in srgb, var(--series-1) 5%, transparent); }

th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }

td.cell-question { max-width: 380px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
td.cell-key { white-space: nowrap; }
td.cell-muted, .muted { color: var(--text-muted); }

.type-chip {
  display: inline-block;
  font-size: 11px;
  font-weight: 550;
  color: var(--text-secondary);
  border: 1px solid var(--grid);
  border-radius: 999px;
  padding: 1px 8px;
  white-space: nowrap;
}

.base-chip { font-size: 11px; font-weight: 600; white-space: nowrap; }
.base-chip.networked { color: var(--series-1); }
.base-chip.total { color: var(--text-secondary); }

/* Barre dans une cellule : piste = pas de l'accent plus clair (même rampe),
   remplissage fin, bout arrondi côté donnée, carré côté baseline. */
.cellbar { display: flex; align-items: center; gap: 8px; min-width: 130px; }
.cellbar-track {
  flex: 1;
  height: 8px;
  border-radius: 0 4px 4px 0;
  background: var(--series-1-soft);
  overflow: hidden;
}
.cellbar-fill {
  height: 100%;
  background: var(--series-1);
  border-radius: 0 4px 4px 0;
  min-width: 2px;
}
.cellbar-fill.is-zero { min-width: 0; }
.cellbar-value { font-variant-numeric: tabular-nums; font-size: 12.5px; min-width: 46px; text-align: right; }

/* Barres empilées outcomes : segments statut, gaps 2px = la surface qui sépare */
.stackbar {
  display: flex;
  gap: 2px;
  height: 12px;
  border-radius: 4px;
  overflow: hidden;
  min-width: 120px;
  background: transparent;
}
.stackbar .seg { height: 100%; }
.stackbar .seg:hover { filter: brightness(1.12); }
.seg-completed { background: var(--status-good); }
.seg-cancelled { background: var(--status-serious); }
.seg-error { background: var(--status-critical); }
.seg-abandoned { background: var(--status-warning); }
.seg-other { background: var(--deemphasis); }

/* ------------------------------------------------- barres horizontales (G) */
.hbar-list { display: flex; flex-direction: column; gap: 10px; margin-top: 8px; }
.hbar-row { display: grid; grid-template-columns: 230px 1fr 84px; align-items: center; gap: 10px; }
.hbar-label { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar-track { height: 14px; }
.hbar-fill {
  height: 100%;
  border-radius: 0 4px 4px 0;
  background: var(--series-1);
  min-width: 2px;
}
.hbar-fill.is-context { background: var(--deemphasis); }
.hbar-value { font-size: 12.5px; font-variant-numeric: tabular-nums; color: var(--text-secondary); white-space: nowrap; }

.callout {
  display: flex;
  align-items: baseline;
  gap: 14px;
  flex-wrap: wrap;
  padding: 15px 17px;
  border: 1px solid var(--grid);
  border-left: 3px solid var(--series-1);
  border-radius: 11px;
  margin: 4px 0 14px;
}

.callout-figure {
  font-family: var(--font-display);
  font-size: 38px;
  font-weight: 800;
  letter-spacing: -0.01em;
  line-height: 1;
  color: var(--series-1);
}

.callout-text { font-size: 13.5px; color: var(--text-secondary); max-width: 52ch; }

/* --------------------------------------------------------------- SVG (H) */
.chart-svg { width: 100%; min-width: 560px; height: auto; display: block; }
.chart-svg .gridline { stroke: var(--grid); stroke-width: 1; }
.chart-svg .baseline { stroke: var(--axis); stroke-width: 1; }
.chart-svg .tick-label, .chart-svg .axis-label { fill: var(--text-muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart-svg .bar { fill: var(--series-1); }
.chart-svg .bar-hit { fill: transparent; cursor: default; }
.chart-svg .direct-label { fill: var(--text-secondary); font-size: 11px; font-weight: 600; font-variant-numeric: tabular-nums; }
.chart-svg .median-marker { fill: var(--text-secondary); }
.chart-svg .median-label { fill: var(--text-secondary); font-size: 11px; font-weight: 600; }

/* ----------------------------------------------------------- états vides */
.empty-state {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 18px 14px;
  border: 1px dashed var(--axis);
  border-radius: 10px;
  color: var(--text-secondary);
  font-size: 13px;
  margin-top: 6px;
}
.empty-state::before { content: "⏳"; font-size: 16px; }
.empty-note { font-size: 12px; color: var(--text-muted); margin-top: 8px; }

.note {
  font-size: 12px;
  color: var(--text-secondary);
  border-top: 1px solid var(--grid);
  margin-top: 12px;
  padding-top: 10px;
}
.note strong { color: var(--text-primary); }

.link-btn {
  font: inherit;
  font-size: 12.5px;
  color: var(--series-1);
  background: none;
  border: none;
  padding: 6px 0 0;
  cursor: pointer;
  font-weight: 550;
}

/* ---------------------------------------------------------------- tooltip */
.tooltip {
  position: fixed;
  z-index: 30;
  pointer-events: none;
  background: var(--text-primary);
  color: var(--page);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12px;
  max-width: 320px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}
.tooltip .tt-title { font-weight: 600; margin-bottom: 3px; }
.tooltip .tt-row { display: flex; justify-content: space-between; gap: 14px; }
.tooltip .tt-row .tt-value { font-weight: 650; font-variant-numeric: tabular-nums; }
.tooltip .tt-key { display: inline-block; width: 10px; height: 3px; border-radius: 2px; margin-right: 5px; vertical-align: middle; }

/* ---------------------------------------------------------------- footer */
.page-foot { margin-top: 28px; font-size: 12px; color: var(--text-muted); }
.page-foot p { margin: 0 0 4px; }
.page-foot .link-btn { padding: 0; font-size: 12px; }

/* ------------------------------------------------------------- responsive */
@media (max-width: 640px) {
  .topbar-inner { padding: 12px 16px 10px; }
  main, .status { padding-left: 16px; padding-right: 16px; }
  .hbar-row { grid-template-columns: 1fr; gap: 4px; }
  .hbar-row .hbar-track { order: 2; }
  .hbar-row .hbar-value { order: 3; }
  .logo { height: 34px; }
  .kpi-value { font-size: 28px; }
  td.cell-question { max-width: 220px; }
}
