/* 16:9 stage.
 *
 * The stage is exactly as wide as min(100vw, 177.78vh) and as tall as
 * min(56.25vw, 100vh): on a 16:9 screen it fills the display edge to edge, on any
 * other screen it letterboxes rather than cropping or scrolling. Everything inside
 * is sized in `cqw`/`cqh` container units, so 1920 gives 16 px body text and 2560
 * grows proportionally instead of leaving a small dashboard on a big wall.
 *
 * No `max-width: 1440px` anywhere: that is what used to leave the bottom row of a
 * 1080p screen empty and force a scrollbar.
 */

:root {
  --ink: #1c2331;
  --muted: #6b7280;
  --line: #e3e6ec;
  --bg: #0e131b;
  --card: #ffffff;
  --accent: #3b6ea5;
  --warn: #b23c2e;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
}

body {
  color: var(--ink);
  font-family: "Segoe UI", Helvetica, Arial, sans-serif;
}

/* ------------------------------------------------------------------ stage */
/* The board is authored at exactly 1920 x 1080 and scaled to fit the window
   (`--stage-scale`, set by assets/scale.js).
 *
 * It cannot be laid out fluidly. Plotly draws its tick labels and titles in fixed
 * CSS pixels and re-lays them out only for the space it is given, so a fluid stage
 * at 1351 px wide halves every card while every label stays 13 px: eighteen ranking
 * rows land in a 90 px plot and pile up on each other. Scaling one authored size
 * keeps the composition identical at 1280, 1920 and 3840 - the type gets smaller,
 * the layout never breaks. */
.stage {
  container-type: size;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(var(--stage-scale, 1));
  transform-origin: center center;
  display: flex;
  flex-direction: column;
  gap: 0.45cqh;
  width: 1920px;
  height: 1080px;
  padding: 1.05cqw 1.15cqw 0.9cqw;
  background: var(--bg);
  color: var(--ink);
  overflow: hidden;
}

/* ----------------------------------------------------------------- topbar */
.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1.4cqw;
  padding-bottom: 0.5cqh;
  border-bottom: 1px solid #263041;
}

.topbar h1 {
  margin: 0 0 0.2cqh;
  font-size: clamp(15px, 1.42cqw, 34px);
  line-height: 1.1;
  letter-spacing: -0.02em;
  color: #f3f6fa;
}

.topbar p {
  margin: 0;
  font-size: clamp(11px, 0.72cqw, 17px);
  color: #93a0b4;
}

.chips { display: flex; gap: 0.6cqw; flex-wrap: nowrap; }

.chip {
  background: #161d29;
  border: 1px solid #263041;
  border-radius: 0.5cqw;
  padding: 0.45cqh 0.7cqw;
  min-width: 8.5cqw;
}

.chip-label {
  display: block;
  font-size: clamp(9.5px, 0.58cqw, 14px);
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: #8d9bb0;
}

.chip-value {
  display: block;
  font-size: clamp(12px, 1.02cqw, 24px);
  font-weight: 600;
  margin: 0.15cqh 0;
  color: #f3f6fa;
  font-variant-numeric: tabular-nums;
}

.chip-note { font-size: clamp(9.5px, 0.55cqw, 13px); color: #8d9bb0; }

/* ----------------------------------------------------------------- banner */
.banner {
  flex: 0 0 auto;
  padding: 0.5cqh 0.8cqw;
  background: #3a1f1b;
  border-left: 3px solid #d9694f;
  border-radius: 0.35cqw;
  font-size: clamp(10px, 0.62cqw, 15px);
  color: #f0c9c0;
}

/* --------------------------------------------------------------- headline */
/* One sentence, generated from the same filters as the panels, so a reader who
   walks up to the screen knows what they are looking at before reading a chart. */
.headline {
  flex: 0 0 auto;
  padding: 0.55cqh 0.85cqw;
  background: #12202e;
  border-left: 3px solid #5b8fd6;
  border-radius: 0.35cqw;
  font-size: clamp(11px, 0.8cqw, 17px);
  line-height: 1.3;
  color: #dbe6f4;
  white-space: normal;
}

.headline b { color: #ffffff; font-weight: 600; }

/* --------------------------------------------------------------- controls */
.controls {
  flex: 0 0 auto;
  display: flex;
  gap: 0.85cqw;
  align-items: flex-end;
  flex-wrap: nowrap;
  padding: 0.2cqh 0;
}

.control {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.control.year { flex: 0 0 26%; min-width: 20cqw; }
.control.district { flex: 0 0 13%; }
.control.ruler { flex: 0 0 12%; }
.control.preset { flex: 0 0 17%; }
.control.text { flex: 0 0 auto; }
.control.tight { min-width: 12cqw; flex: 1 1 auto; }

/* The panel checklist is a disclosure so nine toggles never widen the bar. */
.drawer { position: relative; flex: 0 0 auto; }
.drawer > summary {
  list-style: none;
  display: inline-block;
  margin-bottom: 0.9cqh;
}
.drawer > summary::-webkit-details-marker { display: none; }
.drawer[open] > summary { background: #243044; }

.drawer-body {
  position: absolute;
  z-index: 40;
  top: 100%;
  right: 0;
  padding: 0.7cqh 0.8cqw;
  background: #161d29;
  border: 1px solid #2d3a4d;
  border-radius: 0.45cqw;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  white-space: nowrap;
}

.control > label,
.card-head-label {
  display: block;
  font-size: clamp(9.5px, 0.58cqw, 14px);
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: #8d9bb0;
  margin-bottom: 0.3cqh;
  white-space: nowrap;
}

.control .Select-control,
.control .Select-menu-outer,
.control .Select-value-label,
.control .Select-placeholder {
  font-size: clamp(11px, 0.68cqw, 16px) !important;
}

/* Dash sliders inherit their rail from a fixed theme; scale the type only. */
.control .rc-slider-mark-text {
  font-size: clamp(9.5px, 0.55cqw, 13px) !important;
  color: #8d9bb0 !important;
}

.panel-toggles {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.25cqh 0.8cqw;
}
.panel-toggles label {
  display: inline-flex;
  align-items: center;
  gap: 0.25cqw;
  font-size: clamp(10px, 0.62cqw, 15px);
  color: #c3ccd9;
  white-space: nowrap;
  cursor: pointer;
}
.panel-toggles input { margin: 0; }

.ghost-button {
  font: inherit;
  font-size: clamp(10px, 0.62cqw, 15px);
  color: #dbe3ee;
  background: #1b2331;
  border: 1px solid #2d3a4d;
  border-radius: 0.35cqw;
  padding: 0.55cqh 0.8cqw;
  cursor: pointer;
  white-space: nowrap;
}
.ghost-button:hover { background: #243044; }

/* -------------------------------------------------------- segmented radio */
.segmented {
  display: inline-flex;
  gap: 2px;
  padding: 3px;
  background: #eef1f5;
  border-radius: 0.4cqw;
  flex-wrap: wrap;
}

.segmented-item {
  position: relative;
  padding: 0.35cqh 0.6cqw;
  border-radius: 0.32cqw;
  font-size: clamp(10px, 0.62cqw, 15px);
  color: #46505f;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s ease, color 0.12s ease;
}

.segmented-item input {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
  margin: 0;
}

.segmented-item:hover { background: rgba(255, 255, 255, 0.65); }

.segmented-item:has(input:checked) {
  background: var(--card);
  color: var(--ink);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(20, 30, 50, 0.16);
}

.segmented-item:has(input:focus-visible) {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* -------------------------------------------------------------------- grid */
.grid {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  grid-template-rows: repeat(6, minmax(0, 1fr));
  gap: 0.55cqw;
  padding: 0.15cqh 0;
}

.card {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 0.45cqw;
  padding: 0.45cqh 0.5cqw;
}

/* Reading-order badge for the numbered Story preset. Sits in the empty top-right
   of the card, above the figure's own left-aligned title. */
.step {
  position: absolute;
  top: 0.35cqh;
  right: 0.55cqw;
  z-index: 5;
  padding: 0.12cqh 0.4cqw;
  border-radius: 0.3cqw;
  background: #eef1f5;
  color: #5b6472;
  font-size: clamp(9px, 0.5cqw, 12px);
  font-weight: 600;
  letter-spacing: 0.02em;
  white-space: nowrap;
  pointer-events: none;
}

.step:empty { display: none; }

/* The chart owns every pixel the card has left, whatever the preset gives it. */
.card .dash-graph {
  flex: 1 1 auto;
  min-height: 0;
  height: auto;
}

.card .js-plotly-plot,
.card .plot-container,
.card .svg-container { height: 100% !important; }

.card-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5cqw;
  flex-wrap: nowrap;
  padding: 0 0.15cqw 0.15cqh;
}
.card-head .card-head-label { margin-bottom: 0; color: #6b7280; }

/* `flex: 0 0 auto` matters more than the size: a note is the sentence that makes a
   chart readable, so the chart gives up the pixels, never the note. Shrinking it
   is what used to leave half a line dangling below the card's bottom edge. */
.note {
  flex: 0 0 auto;
  margin: 0.1cqh 0.15cqw 0;
  font-size: clamp(10px, 0.58cqw, 14px);
  line-height: 1.3;
  color: var(--muted);
}

/* The selected district's gap to the median, shown as a quiet status pill. */
.pill {
  padding: 0.2cqh 0.5cqw;
  border-radius: 999px;
  background: #fdf1e7;
  border: 1px solid #f3d8c3;
  color: #8a4a1c;
  font-size: clamp(9.5px, 0.58cqw, 14px);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.pill:empty { display: none; }

/* ------------------------------------------------------------------ footer */
.footer {
  flex: 0 0 auto;
  display: flex;
  justify-content: space-between;
  gap: 1cqw;
  flex-wrap: wrap;
  padding-top: 0.35cqh;
  border-top: 1px solid #263041;
  color: #7f8ca1;
  font-size: clamp(9.5px, 0.55cqw, 13px);
}

/* The stage is a 16:9 board built for a 1920 x 1080 wall. Between a laptop and a
   projector the type bottoms out on its clamp floor, so say so instead of letting
   the audience squint at 9 px. */
.footer-hint { display: none; }

@media (min-width: 1181px) and (max-width: 1599px) {
  .footer-hint { display: inline; }
}

/* -------------------------------------------------------------- densities */
.stage[data-density="compact"] { gap: 0.3cqh; }
.stage[data-density="compact"] .grid { gap: 0.4cqw; }
.stage[data-density="compact"] .card { padding: 0.3cqh 0.35cqw; border-radius: 0.3cqw; }
.stage[data-density="compact"] .note,
.stage[data-density="compact"] .footer { display: none; }
.stage[data-density="compact"] .topbar h1 { font-size: clamp(13px, 1.15cqw, 28px); }

/* ------------------------------------------------------------ kiosk mode */
/* A big screen runs the story, not the controls; the preset and the year are
   fixed on the command line. */
.stage.kiosk .controls,
.stage.kiosk .footer { display: none; }

/* ------------------------------------------------- laptop fallback (small or tall)
   Nothing is cropped: below a 16:9 viewport, or below 1180 px wide, the stage stops
   scaling and the page scrolls instead. A 16:9 board at 606 px wide would set
   12 px text in 240 px cards, which is not a small version of the design - it is a
   different, unreadable one. */
@media (max-aspect-ratio: 5/4), (max-width: 1180px) {
  html, body { overflow: auto; }
  /* Let the document itself scroll; a fixed-height body would scroll internally,
     which traps the wheel over a Plotly canvas. */
  body { height: auto; min-height: 100%; }
  .stage {
    /* `container-type: size` gives the stage size containment, so `height: auto`
       would collapse it to the 100vh floor and clip the stacked cards. */
    container-type: normal;
    position: static;
    transform: none;
    overflow: visible;
    width: 100vw;
    height: auto;
    min-height: 100vh;
    padding: 16px;
  }
  .grid {
    grid-template-columns: repeat(12, minmax(0, 1fr));
    /* The 6 x 1fr rows of the stage would fight the 300 px content rows. */
    grid-template-rows: none;
    grid-auto-rows: 300px;
  }
  .card { grid-column: span 12 !important; grid-row: auto !important; }
  .topbar h1 { font-size: 20px; }
  .topbar p, .control > label, .card-head-label, .note, .headline,
  .footer, .pill, .ghost-button, .segmented-item { font-size: 12px; }
  .chip-value { font-size: 16px; }
  .controls { flex-wrap: wrap; }
  .control.year { flex: 1 1 100%; }
}
