:root {
  --bg: #ffffff;
  --fg: #1a1d21;
  --muted: #5b6168;
  --line: #e3e6ea;
  --accent: #1f77b4;
  --warn-bg: #fff4e5;
  --warn-line: #ffb86b;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--fg);
  background: var(--bg);
  line-height: 1.45;
}

header {
  border-bottom: 1px solid var(--line);
  padding: 0.75rem 1.25rem;
}
.brand { font-weight: 700; font-size: 1.1rem; text-decoration: none; color: var(--fg); }

main { padding: 1.25rem 1.5rem; max-width: 1400px; margin: 0 auto; }

a { color: var(--accent); }
.caption, .note { color: var(--muted); }
.note { font-size: 0.85rem; margin: 0.35rem 0; }
.meta { color: var(--muted); }
.links { font-size: 0.9rem; }

code {
  background: #f3f4f6; padding: 0.1rem 0.35rem; border-radius: 4px;
  font-size: 0.85em;
}

.scope { margin: 0.75rem 0 0.25rem; font-size: 0.95rem; }

details.howto {
  border: 1px solid var(--line); border-radius: 8px;
  padding: 0.5rem 0.85rem; margin: 0.75rem 0;
}
details.howto summary { cursor: pointer; font-weight: 600; }

.warning {
  background: var(--warn-bg); border: 1px solid var(--warn-line);
  border-radius: 8px; padding: 0.75rem 1rem; margin: 0.85rem 0;
}
/* standalone error page (error.html): a narrow column under the site header */
.error-page { max-width: 40rem; margin: 2.5rem auto; }
/* retry control inside a swapped-in error box (_error_fragment.html) */
.load-error button {
  border: 1px solid var(--line); border-radius: 4px; padding: 0.2rem 0.6rem;
  background: var(--bg); cursor: pointer;
}

/* layout: sidebar filters + results */
.layout { display: grid; grid-template-columns: 250px 1fr; gap: 1.5rem; align-items: start; }
/* min-width:0 stops the wide ranked table from stretching the 1fr column past its track */
.results { min-width: 0; }
.filters { position: sticky; top: 1rem; }
/* Heading row: title left, "Reset" action right (the h2's old bottom margin lives here now). */
.filters-head { display: flex; align-items: center; justify-content: space-between; margin: 0 0 0.5rem; }
.filters h2 { font-size: 1rem; margin: 0; }
.filters form { display: flex; flex-direction: column; gap: 0.7rem; }
.filters label { display: flex; flex-direction: column; gap: 0.2rem; font-size: 0.85rem; font-weight: 600; }
.filters input[type="number"], .filters input[type="text"], .filters select {
  font: inherit; padding: 0.3rem 0.4rem; border: 1px solid var(--line); border-radius: 6px;
}
.filters .slider-row { display: flex; align-items: center; gap: 0.5rem; }
.filters input[type="range"] { flex: 1; accent-color: var(--accent); }
.filters .slider-row output { font-weight: 700; font-variant-numeric: tabular-nums; min-width: 2.5ch; text-align: right; }
.filters fieldset { border: 1px solid var(--line); border-radius: 6px; margin: 0; padding: 0.4rem 0.6rem; }
.filters legend { font-size: 0.8rem; font-weight: 600; color: var(--muted); }
.filters .chk { flex-direction: row; align-items: center; gap: 0.35rem; font-weight: 400; }
.filters .hint { font-weight: 400; font-size: 0.78rem; color: var(--muted); }
.filters .reset {
  font-size: 0.8rem; font-weight: 600; text-decoration: none;
  color: var(--muted); padding: 0.3rem 0.7rem; border: 1px solid var(--line); border-radius: 6px;
}
.filters .reset:hover { color: var(--accent); border-color: var(--accent); }
/* header actions (Show/hide toggle + Reset). The toggle is mobile-only — the
   >900px block reveals it; the form is always shown on desktop. */
.filters-head-actions { display: flex; align-items: center; gap: 0.4rem; }
/* .filters .filters-toggle (0,2,0) must out-specify ".filters label {display:flex}" (0,1,1),
   since the toggle is a <label>; otherwise it would show on desktop. */
.filters .filters-toggle { display: none; }
/* Medical-specialty checkboxes: a scrollable list so it stays compact like the
   old <select multiple> while giving finger-friendly tap targets. */
.filters .spec-list { display: flex; flex-direction: column; gap: 0.15rem; max-height: 12rem; overflow-y: auto; }
/* Sidebar per-severity count histograms — a Zillow-style dual-thumb range slider over the bars. */
.filters .count-hists { display: flex; flex-direction: column; gap: 0.8rem; }
.count-hist-head { display: flex; align-items: center; gap: 0.4rem; font-size: 0.78rem; font-weight: 600; }
.count-hist-head .dot { width: 0.6rem; height: 0.6rem; border-radius: 50%; flex: none; }
.count-hist-head .brush-readout {
  margin-left: auto; font-weight: 700; color: var(--muted); font-variant-numeric: tabular-nums;
}
/* Inset by half the thumb width so the bars, ticks and thumb-centre travel share one x-coordinate. */
.rs-body { padding: 0 8px; }
.rs-hist { display: flex; align-items: flex-end; gap: 1px; height: 44px; }
.rs-bar { flex: 1 1 0; min-width: 0; border-radius: 1px 1px 0 0; opacity: 1; transition: opacity 0.08s; }
.rs-bar.dim { opacity: 0.2; }
.rs-track { position: relative; height: 18px; margin-top: 2px; }
.rs-rail { position: absolute; left: 0; right: 0; top: 7px; height: 4px; border-radius: 2px; background: var(--line); }
.rs-fill { position: absolute; top: 7px; height: 4px; border-radius: 2px; }
.rs-track input[type="range"] {
  position: absolute; left: 0; top: 0; width: 100%; height: 18px; margin: 0;
  -webkit-appearance: none; appearance: none; background: transparent; pointer-events: none;
}
/* Knob border = this histogram's bar color (--knob, set inline per .count-hist); accent fallback. */
.rs-track input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; pointer-events: auto; margin-top: 1px;
  width: 16px; height: 16px; border-radius: 50%; background: #fff; border: 2px solid var(--knob, var(--accent));
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.35); cursor: pointer;
}
.rs-track input[type="range"]::-moz-range-thumb {
  pointer-events: auto; width: 16px; height: 16px; border-radius: 50%; background: #fff;
  border: 2px solid var(--knob, var(--accent)); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.35); cursor: pointer;
}
.rs-track input[type="range"]::-webkit-slider-runnable-track { height: 18px; background: transparent; }
.rs-track input[type="range"]::-moz-range-track { height: 18px; background: transparent; }
.rs-min { z-index: 4; }
.rs-max { z-index: 5; }
.rs-ticks { position: relative; height: 0.9rem; margin-top: 1px; }
.rs-ticks span {
  position: absolute; transform: translateX(-50%); font-size: 0.62rem; color: var(--muted);
  font-variant-numeric: tabular-nums; white-space: nowrap;
}

/* tables */
.opps-card { border: 1px solid var(--line); border-radius: 8px; padding: 0.5rem 0.85rem; margin: 0.5rem 0; }
/* ranked-card header: count on the left, mobile-only collapse chevron on the right */
.opps-head { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.opps-toggle { display: none; }  /* mobile-only; the ≤640px block shows it */
/* detail-page section compartment (same look as .opps-card) */
.panel { border: 1px solid var(--line); border-radius: 8px; padding: 0.5rem 0.85rem; margin: 0.85rem 0; }
.tablewrap { overflow-x: auto; }
/* bounded, internally-scrolling table so the sticky header pins to the card */
.scroll-table { max-height: 32rem; overflow: auto; }
table { border-collapse: collapse; width: 100%; font-size: 0.85rem; }
th, td { text-align: left; padding: 0.35rem 0.55rem; border-bottom: 1px solid var(--line); white-space: nowrap; vertical-align: top; }
th { position: sticky; top: 0; z-index: 1; background: var(--bg); box-shadow: inset 0 -1px 0 var(--line);
  cursor: help; color: var(--muted); font-weight: 600; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
/* device-name column: pin the width via one knob (--name-col); wrap long names (no clipping) */
.ranked .col-name {
  --name-col: 360px;  /* the one knob — raise/lower to widen/narrow this column */
  width: var(--name-col); min-width: var(--name-col); max-width: var(--name-col);
  white-space: normal; overflow-wrap: break-word;
}
table.ranked tbody tr:hover { background: #f7f9fb; }
.count { font-weight: 600; margin: 0.25rem 0 0.5rem; }

/* metrics */
.metrics { display: flex; flex-wrap: wrap; gap: 0.75rem; margin: 1rem 0; }
.metric {
  border: 1px solid var(--line); border-radius: 8px; padding: 0.5rem 0.85rem;
  min-width: 110px; display: flex; flex-direction: column; cursor: help;
}
.metric .label { font-size: 0.75rem; color: var(--muted); }
.metric .value { font-size: 1.3rem; font-weight: 700; font-variant-numeric: tabular-nums; }
/* separates the Unique-* tiles from the scoring tiles in the same .metrics row */
.metric-divider {
  align-self: stretch; display: flex; align-items: center;
  color: var(--line); font-size: 1.6rem; font-weight: 300; user-select: none;
}

/* event stat cards — one card per time window (YTD / last N years), four across, wrap if narrow */
.evstats { display: flex; flex-wrap: wrap; gap: 0.75rem; margin: 1rem 0; }
.evstat {
  border: 1px solid var(--line); border-radius: 8px; padding: 0.6rem 0.85rem;
  flex: 1 1 160px; min-width: 150px;
}
.evstat-head { font-size: 0.75rem; color: var(--muted); }
.evstat-total {
  font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.2;
}
.evstat-unit { font-size: 0.7rem; font-weight: 400; color: var(--muted); }
.evstat-rows { list-style: none; margin: 0.4rem 0 0; padding: 0; }
.evstat-rows li {
  display: flex; align-items: center; gap: 0.4rem; font-size: 0.8rem; padding: 0.05rem 0;
}
.evstat-rows .dot { width: 0.55rem; height: 0.55rem; border-radius: 50%; flex: none; }
.evstat-rows .n { margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 600; }

/* charts — the two big charts grow to fill the row; breakdown + legend stay compact, wrap if narrow */
.charts { display: flex; flex-wrap: wrap; gap: 1.5rem 1.5rem; align-items: flex-start; margin: 1rem 0; }
.chart { min-width: 0; }
.chart--fill { flex: 1 1 340px; }  /* Events + Top problems share the leftover width equally */
.chart h3 { margin: 0.75rem 0 0.35rem; font-size: 0.95rem; }
.chart .note { max-width: 460px; }  /* keep the long "incomplete period" caption from stretching */
.viz { width: 100%; min-height: 60px; }  /* width:100% so a width:"container" chart fills its cell */

/* shared severity legend — sits to the right of the three charts; labels the palette they share */
.chart-legend { min-width: 0; }
.legend { list-style: none; margin: 0.35rem 0 0; padding: 0; }
.legend li { display: flex; align-items: center; gap: 0.45rem; font-size: 0.85rem; padding: 0.12rem 0; }
.legend .dot { width: 0.7rem; height: 0.7rem; border-radius: 50%; flex: none; }

/* "Who's behind these reports" — manufacturer subtotal → top-5 brands × ordinal top-problem cells.
   Each cell: problem name + four event-type counts (colored) + total. Severity colors mirror
   presentation.SEVERITY_COLORS (Death/Injury/Malfunction/Other = red/orange/blue/gray) — keep in sync. */
.matrix .mx-name { white-space: normal; }
.matrix .mx-indent { padding-left: 1.6rem; }              /* brand rows indent under their maker */
table.matrix tr.mx-mfr > td { font-weight: 700; background: #f3f4f6; }  /* bold subtotal row (td, not th) */
.matrix .mx-count { color: var(--muted); font-weight: 400; font-size: 0.78rem; margin-left: 0.4rem; }
.matrix .mx-flag { color: var(--muted); font-weight: 400; font-style: italic; }
.matrix .mx-prob { font-weight: 600; white-space: normal; }
.matrix .mx-counts { display: flex; gap: 0.35rem; margin-top: 0.15rem; font-variant-numeric: tabular-nums; }
.matrix .mx-total { margin-left: 0.25rem; font-weight: 600; }
.matrix .mx-empty { color: var(--line); }
/* manufacturer header: name on line 1 (with the chevron), report count on its own line beneath */
.matrix .mx-mfr .mx-count { display: block; margin-left: 0; margin-top: 0.1rem; }
/* align each problem cell's value row to the bottom so all five line up across the row regardless of how
   many lines the problem title wraps (reliable for table cells; % height + flex is not — table-cell
   percentage heights don't resolve under an auto-height table). The .mx-name maker/brand cells keep the
   global vertical-align:top. */
table.matrix td:not(.mx-name) { vertical-align: bottom; }
.sev-death { color: #d62728; }
.sev-injury { color: #ff7f0e; }
.sev-malfunction { color: #1f77b4; }
.sev-other { color: #9aa0a6; }
/* device rows collapse under their maker; the maker-row chevron toggles its enclosing <tbody.mx-group>.
   Default = collapsed (only the .mx-mfr header shows); reuses .chev / .chev.open (rotate) below. */
.matrix tbody.mx-group .mx-brand { display: none; }
.matrix tbody.mx-group.open .mx-brand { display: table-row; }
.matrix .mx-chev { margin-right: 0.15rem; color: var(--muted); }
/* problem × device crosstab: a true grid. Uses the same AUTO table layout as the sibling .matrix
   tables so each column sizes to its content and the D/I/M/O/T counts stay on one line (a fixed
   layout forced equal narrow columns that couldn't fit the widely-varying numbers, wrapping or
   overflowing them). th,td white-space:normal lets the long device-name headers + problem names wrap
   (overriding the global th{white-space:nowrap}); it scrolls via .tablewrap when wider than the card.
   Each problem row is bold-labelled + gray-shaded to match the other two tables' primary rows. */
table.matrix.xtab th, table.matrix.xtab td { white-space: normal; }
table.matrix.xtab thead th:first-child, table.matrix.xtab td.mx-name { width: 15rem; }  /* narrow problem col */
table.matrix.xtab tbody td { background: #f3f4f6; }     /* shade every problem row (like mx-mfr) */
table.matrix.xtab td.mx-name { font-weight: 700; }      /* bold the problem labels (count stays muted) */
table.matrix.xtab .mx-counts span { font-weight: 600; } /* bold every cell value, not just the total */
table.matrix.xtab thead th .mx-count { display: block; margin-left: 0; margin-top: 0.1rem; }

/* bubble plot — its own card below the ranked list, same width as the table card */
.chart-card {
  border: 1px solid var(--line); border-radius: 8px; padding: 0.5rem 0.85rem; margin: 0.5rem 0;
}
.chart-card .viz { width: 100%; }
.chart-card figcaption { margin-bottom: 0.4rem; }
/* Mobile-only HTML legend for the bubble plot: on phones the chart variant drops its Vega legend
   (Vega can't stack two legends on their own lines), so label it here — Crowding (color ramp) and
   Events (sized dots) on two stacked rows. Hidden on desktop, where the Vega legend is shown. */
.bubble-legend { display: none; }
@media (max-width: 640px) {
  .bubble-legend { display: flex; flex-direction: column; gap: 0.4rem; margin-top: 0.5rem; }
}
.bl-row { display: flex; align-items: center; gap: 0.5rem; font-size: 0.8rem; }
.bl-title { font-weight: 600; color: var(--muted); min-width: 4.5rem; }  /* aligns both fill areas */
.bl-fill { flex: 1; min-width: 0; }  /* gradient + circles span the rest of the card width */
.bl-grad { width: 100%; height: 0.6rem; border-radius: 3px; background: linear-gradient(to right, #c6dbef, #08306b); }
.bl-gradticks {
  display: flex; justify-content: space-between; margin-top: 1px;
  font-size: 0.62rem; color: var(--muted); font-variant-numeric: tabular-nums;
}
.bl-sizes { display: flex; justify-content: space-between; align-items: center; }
.bl-size { display: inline-flex; align-items: center; gap: 0.3rem; font-variant-numeric: tabular-nums; }
.bl-dot { display: inline-block; border-radius: 50%; background: #6baed6; opacity: 0.7; flex: none; }

.narrative { white-space: pre-wrap; background: #f7f9fb; border: 1px solid var(--line);
  border-radius: 8px; padding: 0.75rem 1rem; }
.narrsel { display: inline-flex; flex-direction: column; gap: 0.2rem; margin-top: 0.75rem; font-size: 0.85rem; }

/* inline expandable report narrative (chevron toggles a full-width detail row beneath its row) */
.chev-cell { width: 1.6rem; text-align: center; }
.chev { background: none; border: none; cursor: pointer; color: var(--accent);
  font-size: 0.9rem; line-height: 1; padding: 0 0.25rem; transition: transform 0.12s; }
.chev.open { transform: rotate(90deg); }
.narr-row { display: none; }
.narr-row.open { display: table-row; }
.narr-cell { white-space: normal; background: #f7f9fb; }
.spinner { display: inline-block; width: 1.1rem; height: 1.1rem; margin: 0.15rem;
  border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%;
  animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation: none; } }

/* Skeleton placeholder while the lazy drilldown-detail section loads: card-shaped shimmer blocks that
   preview the .panel layout + reserve rough height (non-blocking; opts out of the global overlay via
   [data-quiet]). Pure client-side CSS. */
.dd-skeleton { margin: 0.85rem 0; }
.skeleton-panel {
  border: 1px solid var(--line); border-radius: 8px; padding: 0.85rem; margin: 0.85rem 0;
}
.sk-line {
  display: block; height: 0.8rem; margin: 0.55rem 0; border-radius: 4px;
  background: linear-gradient(90deg, #edf0f3 25%, #e0e4e8 37%, #edf0f3 63%);
  background-size: 400% 100%; animation: sk-shimmer 1.4s ease infinite;
}
.sk-title { height: 1rem; width: 40%; margin-bottom: 1rem; }
.sk-short { width: 60%; }
@keyframes sk-shimmer { 0% { background-position: 100% 0; } 100% { background-position: 0 0; } }
@media (prefers-reduced-motion: reduce) { .sk-line { animation: none; } }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

/* global loading overlay (slow-request feedback) - reuses .spinner above */
#loading-overlay {
  position: fixed; inset: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: center;
  background: rgba(26, 29, 33, 0.35);            /* dimmed backdrop (= --fg @ 35%) */
}
#loading-overlay[hidden] { display: none; }       /* toggle: beats display:flex */
.overlay-card {
  display: flex; flex-direction: column; align-items: center; gap: 0.75rem;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
  padding: 1.25rem 1.75rem; max-width: 22rem; text-align: center;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.15);
}
.overlay-card .spinner { width: 2rem; height: 2rem; border-width: 3px; }  /* bigger than inline */
.overlay-card p { margin: 0; color: var(--muted); font-size: 0.95rem; }

h1 { font-size: 1.5rem; margin: 0.25rem 0; }
hr { border: none; border-top: 1px solid var(--line); margin: 1.5rem 0; }

/* ==========================================================================
   Responsive / mobile. The >=901px desktop layout above is unchanged; the
   blocks below progressively adapt to narrow widths and touch pointers.
   ========================================================================== */

/* Tablet & narrower: single column; the sidebar un-sticks and its form
   collapses behind a toggle (CSS-only checkbox hack) so the ranked list isn't
   buried beneath two screens of filters. */
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .filters { position: static; }
  .filters .filters-toggle {
    display: inline-block; cursor: pointer; font-size: 0.8rem; font-weight: 600;
    color: var(--muted); padding: 0.3rem 0.7rem; border: 1px solid var(--line); border-radius: 6px;
  }
  .filters .filters-toggle:hover { color: var(--accent); border-color: var(--accent); }
  .filters form { display: none; }                      /* collapsed by default on mobile */
  .filters-toggle-cb:checked ~ form { display: flex; }  /* the toggle reveals it */
}

/* Phones: tighter chrome, a fluid device-name column, and single-axis table
   scroll - drop the nested vertical scrollbox so the page scrolls vertically
   while wide tables scroll only sideways (no two-axis scroll trap). */
@media (max-width: 640px) {
  main { padding: 1rem 0.9rem; }
  header { padding: 0.6rem 0.9rem; }
  .brand { font-size: 1rem; }
  h1 { font-size: 1.3rem; }
  .ranked .col-name { --name-col: min(360px, 56vw); }
  /* Bounded, internally-scrolling box (like desktop) so the sticky header stays
     pinned; 70vh keeps the box within the phone viewport regardless of height. */
  .scroll-table { max-height: 70vh; }
  .opps-card .scroll-table { max-height: 60vh; }  /* ranked list ~10 rows (drill-down tables stay 70vh) */
  th, td { padding: 0.3rem 0.45rem; }
  .charts { gap: 1rem; }
  .metrics, .evstats { gap: 0.5rem; }

  /* Collapse the ranked table (chevron in its header) so filter tweaks to the bubble chart below are
     visible. Checkbox lives outside #ranked (index.html) → state survives HTMX swaps. */
  .opps-toggle {
    display: inline-flex; align-items: center; justify-content: center; cursor: pointer;
    min-width: 2.2rem; min-height: 2.2rem; color: var(--muted); font-size: 1rem; line-height: 1;
    transition: transform 0.15s;
  }
  .opps-collapse-cb:checked ~ #ranked .scroll-table { display: none; }
  .opps-collapse-cb:checked ~ #ranked .opps-toggle { transform: rotate(180deg); }
}

/* Touch pointers (any width): enlarge the range-slider knobs and other hit
   targets to a finger-friendly size. */
@media (pointer: coarse) {
  .rs-body { padding: 0 11px; }                          /* = half the 22px thumb: keeps knob/bar alignment */
  .rs-track { height: 24px; }
  .rs-track input[type="range"] { height: 24px; }
  .rs-track input[type="range"]::-webkit-slider-thumb { width: 22px; height: 22px; }
  .rs-track input[type="range"]::-moz-range-thumb { width: 22px; height: 22px; }
  .rs-track input[type="range"]::-webkit-slider-runnable-track { height: 24px; }
  .rs-track input[type="range"]::-moz-range-track { height: 24px; }
  .rs-rail, .rs-fill { top: 10px; }
  .chev { padding: 0.35rem 0.5rem; font-size: 1rem; }
  .chev-cell { width: 2.2rem; }
  .filters .chk { padding: 0.25rem 0; }
}
