:root {
  --bg: #f5f7fa;
  --panel: #ffffff;
  --border: #e2e8f0;
  --muted: #64748b;
  --text: #0f172a;
  --accent: #1d4ed8;
  --accent-hover: #1e40af;
  --good: #15803d;
  --warn: #c2410c;
  --danger: #b91c1c;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  font-size: 14px;
  line-height: 1.4;
}

.topbar {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 14px 24px;
  display: flex; align-items: baseline; justify-content: space-between;
  position: sticky; top: 0; z-index: 10;
}

.disclaimer-banner {
  background: #fef3c7;          /* amber-100 */
  border-bottom: 2px solid #f59e0b;  /* amber-500 */
  color: #78350f;               /* amber-900 - high contrast on amber-100 */
  padding: 10px 24px;
  font-size: 12.5px;
  line-height: 1.45;
}
.disclaimer-banner strong {
  color: #7c2d12;               /* a touch warmer for the lead */
  font-weight: 700;
}
.disclaimer-banner em {
  font-style: italic;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand .logo {
  height: 32px;
  width: auto;
  display: block;
  object-fit: contain;
}
.brand h1 { margin: 0; font-size: 18px; font-weight: 600; }
.tagline { color: var(--muted); font-size: 12px; }

.layout {
  display: grid;
  grid-template-columns: minmax(420px, 1fr) minmax(360px, 1fr);
  gap: 16px;
  padding: 16px 24px;
}
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px 18px;
}
.panel h2 {
  margin: 0 0 10px;
  font-size: 14px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

fieldset {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  margin: 0 0 14px;
}
fieldset legend {
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  padding: 0 4px;
}

.grid.two {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 14px;
}
label {
  display: flex;
  flex-direction: column;
  font-size: 12px;
  color: var(--muted);
  gap: 3px;
}
label.check {
  flex-direction: row;
  align-items: center;
  gap: 6px;
  color: var(--text);
  font-size: 13px;
}
/* Single-line checkbox label: prevents the text + trailing ? button from
   wrapping to the next line. Used for the "4-rod construct" and "Cement at
   UIV" rows where the inline ? would otherwise be pushed onto a new line. */
label.check.nowrap {
  white-space: nowrap;
  flex-wrap: nowrap;
}
label.check.nowrap .info { margin-left: 4px; }
label .req { color: var(--danger); margin-left: 2px; }

/* Header row of a label: parameter name + * required mark + ? info button,
   all on a single horizontal line, sitting above the input field. */
label > .lhead {
  display: inline-flex;
  flex-direction: row;
  align-items: center;
  gap: 3px;
}

/* Spinopelvic parameter slider + numeric input pair (added 2026-06-11).
   Each parameter gets a range slider on the left for quick scrubbing and
   a narrow number box on the right for precise radiograph values. The
   two stay in sync via wireParamSliders() in app.js. */
.param-slider-row {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 6px;
  width: 100%;
}
.param-slider-row input[type="range"] {
  flex: 1 1 auto;
  min-width: 0;
  height: 18px;
  margin: 0;
  accent-color: var(--accent);
}
.param-slider-row input[type="number"] {
  flex: 0 0 56px;
  width: 56px;
  font-variant-numeric: tabular-nums;
}

/* Info-tooltip ? buttons next to each parameter label */
button.info {
  display: inline-flex; align-items: center; justify-content: center;
  width: 14px; height: 14px;
  margin-left: 4px; padding: 0;
  border-radius: 50%;
  background: transparent; color: var(--accent);
  border: 1px solid var(--accent);
  font-size: 9px; font-weight: 700; font-family: inherit;
  cursor: pointer; line-height: 1;
  vertical-align: middle;
}
button.info:hover {
  background: var(--accent); color: white;
}

/* Floating popover. Positioned by JS. */
.info-popover {
  position: absolute;
  z-index: 100;
  max-width: 320px;
  background: #fffbeb;          /* amber-50, distinct from input fields */
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 12px;
  line-height: 1.45;
  color: var(--text);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
}
.info-popover .popover-title {
  font-weight: 700; color: var(--accent);
  margin-bottom: 4px;
}
.info-popover .popover-norm {
  font-size: 11px; color: var(--muted);
  margin-top: 6px;
}
.info-popover .popover-ref {
  display: block; margin-top: 6px;
  font-size: 11px;
}
.info-popover .popover-ref a {
  color: var(--accent); text-decoration: underline;
}
.info-popover .popover-close {
  float: right; cursor: pointer; color: var(--muted);
  font-size: 14px; line-height: 1; margin-left: 6px;
  user-select: none;
}

input[type="number"], input[type="text"], select {
  font: inherit;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: white;
  color: var(--text);
  width: 100%;
}
input:focus, select:focus {
  outline: 2px solid #93c5fd;
  border-color: var(--accent);
}
.inline-num { width: 90px; display: inline-block; margin-left: 4px; }
/* Widen the max-interbody-techniques input + pelvic-screws count input so
   multi-digit values are not visually clipped on the screen. */
#rule_max_interbody_value, #rule_pelvic_fixation_min, #m_num_pelvic_screws {
  width: 90px !important;
}
/* ── Dual-handle range slider for surgeon-set hardware bounds (added 2026-06-02) ── */
.range-row {
  display: grid;
  grid-template-columns: 180px 56px minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  margin-bottom: 6px;
  max-width: 100%;
  box-sizing: border-box;
}
.range-row > * { min-width: 0; }
.range-row .check { margin: 0; }
.range-row .range-readout {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--accent, #1f2937);
  text-align: center;
}
.range-slider {
  position: relative;
  height: 28px;
  min-width: 0;
  /* Inset by half the thumb width on both sides so the thumb never
     overflows the grid cell / fieldset border. */
  padding: 0 12px;
  box-sizing: border-box;
}
.range-slider input[type="range"] {
  position: absolute;
  left: 12px;
  right: 12px;
  width: auto;
  height: 28px;
  background: transparent;
  pointer-events: none;
  -webkit-appearance: none;
  appearance: none;
  outline: none;
  margin: 0;
}
.range-slider input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  background: #d1d5db;
  border-radius: 2px;
}
.range-slider input[type="range"]::-moz-range-track {
  height: 4px;
  background: #d1d5db;
  border-radius: 2px;
}
.range-slider input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  pointer-events: auto;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #1f2937;
  border: 2px solid #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,.2);
  cursor: grab;
  margin-top: -7px;
}
.range-slider input[type="range"]::-moz-range-thumb {
  pointer-events: auto;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #1f2937;
  border: 2px solid #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,.2);
  cursor: grab;
}
.range-slider input[type="range"]:active::-webkit-slider-thumb { cursor: grabbing; }
@media (max-width: 720px) {
  .range-row { grid-template-columns: 1fr; gap: 4px; }
  .range-row .range-readout { text-align: left; }
}

.profile-row {
  display: flex; flex-wrap: wrap; gap: 14px;
  margin-bottom: 6px;
}
.profile-row label {
  flex-direction: row; align-items: center; gap: 4px;
  color: var(--text); font-size: 13px;
}
.profile-desc {
  margin-top: 6px;
  padding: 8px 10px;
  background: #f1f5f9;
  border-left: 3px solid var(--accent);
  border-radius: 4px;
  font-size: 12px;
  line-height: 1.45;
  color: var(--text);
}
.profile-desc b { font-weight: 600; }
.profile-desc-item { margin-bottom: 6px; }
.profile-desc-item:last-child { margin-bottom: 0; }
.profile-desc .ref {
  display: block; margin-top: 4px;
  color: var(--muted); font-size: 11px;
}
.profile-desc .ref a {
  color: var(--accent);
  text-decoration: underline;
}
.profile-desc .ref a:hover {
  color: var(--accent-hover);
}

.constraints {
  width: 100%; border-collapse: collapse; font-size: 12px;
  margin: 6px 0;
}
.constraints th { color: var(--muted); font-weight: 500; text-align: left; padding: 4px; }
.constraints td { padding: 3px 4px; }
.constraints input, .constraints select {
  padding: 3px 5px; font-size: 12px;
}

/* Maneuvers table - same look as the custom-constraints table. */
.maneuvers {
  width: 100%; border-collapse: collapse; font-size: 12px;
  margin: 6px 0;
}
.maneuvers th { color: var(--muted); font-weight: 500; text-align: left; padding: 4px; }
.maneuvers td { padding: 3px 4px; vertical-align: middle; }
.maneuvers input, .maneuvers select {
  padding: 3px 5px; font-size: 12px;
}
.maneuvers .m-deg { width: 90px; }

button {
  font: inherit; font-size: 13px; cursor: pointer;
  padding: 7px 14px; border-radius: 5px; border: 1px solid var(--border);
  background: white; color: var(--text);
}
button.primary {
  background: var(--accent); color: white; border-color: var(--accent);
  font-weight: 600;
}
button.primary:hover { background: var(--accent-hover); }
button.ghost { background: transparent; }
button.danger {
  background: var(--danger); color: white; border-color: var(--danger);
  font-weight: 600;
}
button.danger:hover { background: #991b1b; border-color: #991b1b; }
button.success {
  background: var(--good); color: white; border-color: var(--good);
  font-weight: 600;
}
button.success:hover { background: #166534; border-color: #166534; }
button.warn {
  background: #d97706; color: white; border-color: #d97706;
  font-weight: 600;
}
button.warn:hover { background: #b45309; border-color: #b45309; }
button.small { padding: 3px 9px; font-size: 12px; }
button:disabled { opacity: 0.5; cursor: not-allowed; }

.actions {
  display: flex; align-items: center; gap: 10px; margin-top: 10px;
}
.status { color: var(--muted); font-size: 12px; }
.status.error { color: var(--danger); }
.status.ok { color: var(--good); }

.muted { color: var(--muted); }
.small { font-size: 12px; }

.spine-frame {
  background: #fafbfc;
  border: 1px dashed var(--border);
  border-radius: 6px;
  height: 720px;
  display: flex; align-items: stretch; justify-content: center;
  padding: 6px;
}
.spine-svg { width: 100%; height: 100%; display: block; }

/* Results panel */
.results { margin: 0 24px 24px; padding: 16px 18px; }
.results.hidden { display: none; }
/* Generic hidden utility: lets any element with class="hidden" disappear
   without needing a scoped selector. Used by the manual-plan toggle and
   the per-section "Your plan, immediate post-op (live, literature)"
   wrapper that should only appear after the surgeon ticks the box. */
.hidden { display: none !important; }

/* Banner that appears under the heading once the GA returns, summarising
   whether the best plan's predicted mech-fail meets the surgeon's threshold. */
.mech-banner {
  margin: 8px 0 12px;
  padding: 10px 12px;
  border-radius: 6px;
  font-size: 13px;
  line-height: 1.45;
  border-left: 4px solid transparent;
}
.mech-banner.hidden { display: none; }
.mech-banner.ok {
  background: #ecfdf5;          /* emerald-50 */
  border-left-color: var(--good);
  color: #064e3b;               /* emerald-900 */
}
.mech-banner.warn {
  background: #fef3c7;          /* amber-100 */
  border-left-color: var(--warn);
  color: #78350f;               /* amber-900 */
}
.mech-banner.danger {
  background: #fee2e2;          /* red-100 */
  border-left-color: var(--danger);
  color: #7f1d1d;               /* red-900 */
}
.mech-banner b { font-weight: 700; }
.results-grid {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 410px;
  gap: 14px;
  overflow-x: auto;
  padding-bottom: 8px;
  scroll-snap-type: x proximity;
}
.plan-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fbfcfe;
  padding: 12px;
  display: flex; flex-direction: column;
  scroll-snap-align: start;
}
.plan-card h3 {
  margin: 0 0 6px; font-size: 13px;
  display: flex; justify-content: space-between; align-items: baseline;
}
.plan-card .badge {
  font-size: 10px; padding: 2px 6px; border-radius: 10px;
  background: var(--accent); color: white;
}
.plan-card .badge.warn { background: var(--warn); }
.plan-card .badge.danger { background: var(--danger); }
.plan-card .badge.manual { background: #6b21a8; }

/* Surgeon-entered plan: dashed purple border + tint so it visually reads
   as the surgeon's own plan rather than as a GA recommendation. Same width
   and layout as a regular plan card so the side-by-side comparison is fair. */
.plan-card-manual {
  border: 2px dashed #6b21a8;
  background: #faf5ff;
}
.plan-card-manual h3 { color: #5b21b6; }

.plan-card .plan-text {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px; line-height: 1.5; color: var(--text);
  background: white; border: 1px solid var(--border);
  border-radius: 4px; padding: 6px 8px; margin: 4px 0;
  display: flex; flex-direction: column;
}
.plan-card .plan-row {
  display: flex; align-items: center; gap: 4px;
}
.plan-card .plan-key {
  display: inline-flex; align-items: center;
  width: 14ch;          /* matches the previous fixed-column alignment */
  flex-shrink: 0;
  font-weight: 600;
}
.plan-card .plan-key .info {
  margin-left: 4px;     /* small breathing room after the label */
}
.plan-card .plan-val { flex: 1; }
table.surgical-prefs {
  width: 100%; border-collapse: collapse; margin-top: 4px;
}
table.surgical-prefs th, table.surgical-prefs td {
  padding: 3px 6px; font-size: 0.92em;
}
table.surgical-prefs th { font-weight: 600; text-align: center; border-bottom: 1px solid var(--border, #ccc); }
table.surgical-prefs th:first-child { text-align: left; }
table.surgical-prefs td:nth-child(n+2) { text-align: center; width: 18%; }
.failure-rule {
  display: flex; align-items: center; gap: 8px;
  margin: 6px 0;
  flex-wrap: nowrap; white-space: nowrap;
}
.construct-hw {
  display: flex; flex-direction: column; gap: 6px;
  margin-top: 4px;
}
.construct-hw .check {
  display: inline-flex; align-items: center; gap: 6px;
  white-space: nowrap; flex-wrap: nowrap;
}
.construct-hw .inline-num {
  width: 6ch;
  flex-grow: 0; flex-shrink: 0;
}
.construct-hw select.inline-num {
  width: auto; min-width: 7ch; max-width: 18ch;
}
fieldset.collapsible > legend {
  cursor: pointer;
  user-select: none;
}
fieldset.collapsible > legend .legend-chevron {
  display: inline-block;
  margin-right: 8px;
  font-size: 1em;
  line-height: 1;
  width: 1em;
  text-align: center;
  transition: transform 0.15s ease;
  color: var(--muted, #555);
}
fieldset.collapsible.collapsed > legend .legend-chevron {
  transform: rotate(-90deg);
}
fieldset.collapsible.collapsed > *:not(legend) {
  display: none !important;
}
.prefs-rules-heading {
  margin-top: 10px;
  padding-top: 6px;
  border-top: 1px dashed var(--border, #ddd);
  color: var(--muted, #555);
}
.failure-rule .check {
  flex-shrink: 0;
}
.failure-rule .inline-num {
  width: 5ch; text-align: center;
  display: inline-block;
  margin: 0;
}
/* Selects need extra room for the dropdown arrow on top of their text;
   override the 5ch number-input width so "any" / "T12" / "Pelvis" labels
   don't truncate in the UIV / LIV range pickers. */
.failure-rule select.inline-num {
  width: auto;
  min-width: 75px;
  text-align: left;
  padding-right: 18px;
}
.failure-rule .info {
  flex-shrink: 0;
}
.level-region {
  display: flex; align-items: flex-start; gap: 8px; margin: 4px 0;
}
.level-region-label {
  flex: 0 0 7ch; font-size: 0.85em; font-weight: 600;
  color: var(--muted, #555); padding-top: 6px;
}
.level-chips { display: flex; flex-wrap: wrap; gap: 4px; }
.level-chip {
  display: inline-block; position: relative;
  padding: 4px 10px; min-width: 36px;
  border: 1px solid var(--border, #ccc); border-radius: 4px;
  background: white; cursor: pointer;
  font-weight: 600; font-size: 0.85em;
  text-align: center; user-select: none;
}
.level-chip input { display: none; }
.level-chip:hover { background: #f5f5f5; }
.level-chip:has(input:checked) {
  background: #fde8e8; border-color: #d44; color: #d44;
}
.level-chip:has(input:checked)::after {
  content: "X"; position: absolute;
  top: -7px; right: -7px;
  background: #d44; color: white;
  width: 16px; height: 16px;
  line-height: 14px; text-align: center;
  border-radius: 50%; font-size: 0.78em; font-weight: 700;
  border: 1px solid white;
}
.checkbox-line { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
table.surgical-prefs td.prefs-section {
  padding-top: 6px; color: var(--muted, #555);
  border-top: 1px dashed var(--border, #ddd);
}
.plan-card .tp-row {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
}
.plan-card .tp-row-3 {
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
}
.plan-card .tp-row-5 {
  grid-template-columns: repeat(5, minmax(140px, 1fr)) !important;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: thin;
}
.plan-card .tp-row-5 .params-mini {
  font-size: 8.5px;
}
.plan-card .tp-spines.tp-spines-5 {
  /* Use minmax(140px, 1fr) so each spine has a 140px floor (readable
     size for the cartoon) but can grow to fill available width when
     the card is wide. On narrow viewports where 5 x 140px exceeds the
     card width, overflow-x: auto exposes a horizontal scrollbar
     instead of clipping the 5th spine. */
  grid-template-columns: repeat(5, minmax(140px, 1fr)) !important;
  gap: 3px !important;
  overflow-x: auto;
  scrollbar-width: thin;
}
.plan-card .tp-spines.tp-spines-5 .tp-spine {
  min-width: 140px;
  overflow: visible;
}
.plan-card .tp-spines.tp-spines-5 .spine-mini {
  width: 100%;
  height: 240px;
}
.plan-card .tp-spines.tp-spines-5 .tp-spine-label {
  font-size: 8px;
}
.plan-card .tp-row-4 {
  grid-template-columns: 1fr 1fr 1fr 1fr;
  gap: 4px;
}
.plan-card .tp-row-4 .params-mini {
  font-size: 8.5px;
}
.plan-card .tp-spines.tp-spines-4 {
  /* Use minmax(140px, 1fr) so each spine has a 140px floor (readable
     size for the cartoon) but can grow to fill available width when
     the card is wide. On narrow viewports where 4 x 140px exceeds
     the card width, overflow-x: auto exposes a horizontal scrollbar
     instead of clipping the 4th spine. */
  grid-template-columns: repeat(4, minmax(140px, 1fr)) !important;
  gap: 3px !important;
  overflow-x: auto;
  scrollbar-width: thin;
}
.plan-card .tp-spines.tp-spines-4 .tp-spine {
  /* Drop the min-width: 0 override so the cells honor the 140px floor
     from the grid template. overflow: visible because the SVG sits
     happily inside the cell's natural width now. */
  min-width: 140px;
  overflow: visible;
}
.plan-card .tp-spines.tp-spines-4 .spine-mini {
  width: 100%;
  height: 240px;
}
.plan-card .tp-spines.tp-spines-4 .tp-spine-label {
  font-size: 8px;
}
/* Match the params row above so the 4 mini-params columns also have
   a sensible floor and scroll alignment with the spines row. */
.plan-card .tp-row.tp-row-4 {
  grid-template-columns: repeat(4, minmax(140px, 1fr)) !important;
  overflow-x: auto;
  scrollbar-width: thin;
}
.plan-card .tp { font-size: 11px; }
.plan-card .tp .label {
  font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--muted); font-size: 10px; margin-bottom: 2px;
}
.plan-card .tp-spines {
  display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 4px;
  margin-top: 6px;
}
.plan-card .tp-spine {
  display: flex; flex-direction: column; align-items: stretch;
}
.plan-card .tp-spine-label {
  font-size: 9px; text-align: center; color: var(--muted);
  font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em;
  margin-bottom: 2px;
}
.plan-card .spine-mini {
  height: 320px; background: #fafbfc; border: 1px dashed var(--border);
  border-radius: 4px; padding: 2px;
}
.plan-card .params-mini {
  font-family: ui-monospace, monospace; font-size: 9px;
  display: grid; grid-template-columns: auto 1fr; gap: 1px 4px;
  color: var(--text); line-height: 1.3;
}
.plan-card .params-mini .k { font-weight: 700; }

.footer {
  margin: 0 24px 16px; padding: 10px 0 8px;
  color: var(--muted); font-size: 11px;
  border-top: 1px solid var(--border);
}
.footer .credits {
  margin-bottom: 8px;
  color: var(--text);
  font-size: 12px;
  line-height: 1.55;
}
.footer .credits .credits-line { margin: 1px 0; }
.footer .credits-coauthors { color: var(--muted); font-size: 11px; }
.footer .credits strong { color: var(--text); font-weight: 600; }
.footer .footer-disclaimer {
  border-top: 1px dashed var(--border);
  padding-top: 6px;
  color: var(--muted);
  font-size: 11px;
}
.footer .footer-copyright {
  margin-top: 4px;
  color: var(--muted);
  font-size: 11px;
}
.footer .footer-copyright a {
  color: var(--accent);
  text-decoration: none;
}
.footer .footer-copyright a:hover { text-decoration: underline; }

/* Ground-truth mechanical-failure badges on the actual-plan card.
   Populated when a real case is loaded from real_cases.json. */
.badge.badge-mf-yes { background:#b91c1c; color:#fff; border-color:#7f1d1d; }
.badge.badge-mf-no  { background:#15803d; color:#fff; border-color:#14532d; }
