/* =========================================================================
   cbct.css — styling for the CBCT (3D) matching spike.
   Standalone; mirrors the green Image Match RT theme but doesn't depend on
   the 2D app's styles.css.
   ========================================================================= */

:root {
  --bg: #0e141c;
  --surface: #161f2b;
  --surface-2: #1e2937;
  --border: #263241;
  --border-strong: #3a4759;
  --text: #e8edf2;
  --text-muted: #9aa7b5;
  --text-faint: #6b7888;
  --accent: #0d9488;         /* teal — matches the website */
  --accent-hover: #14b8a6;
  --accent-dim: #0891b2;     /* cyan — accent→dim gives the site gradient */
  --accent-rgb: 13,148,136;
  --grad: linear-gradient(135deg, #0d9488, #0891b2);
  --danger: #dc2626;
  --radius: 12px;
  --radius-sm: 8px;
  --header-h: 52px;
  --panel-w: 380px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text",
          "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
}

/* Light mode — the chrome goes light like the website; the image viewer and the
   monospace readout panels stay dark (clinically correct for CT/CBCT greyscale). */
html[data-theme="light"] {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --surface-2: #eceef1;
  --border: #e3e6ea;
  --border-strong: #cfd4dc;
  --text: #1d1d1f;
  --text-muted: #55606e;
  --text-faint: #8a929e;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  letter-spacing: -0.011em;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.cbct-topbar, .datasource-bar, .status-banner { flex: 0 0 auto; }

/* ---- Top bar ---- */
.cbct-topbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px 16px;
  min-height: var(--header-h);
  padding: 8px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.cbct-topbar .brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 15px;
}
.brand-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 10px rgba(var(--accent-rgb),0.6);
}
.brand-rt {
  display: inline-block;
  margin-left: 2px;
  padding: 1px 6px;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 1px;
  color: #fff;
  background: var(--grad);
  border-radius: 5px;
}
.brand-mode {
  margin-left: 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.5px;
  color: var(--text-faint);
  text-transform: uppercase;
}
.spacer { flex: 1; }

.topbar-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px 18px;
}
.topbar-controls label {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  color: var(--text-muted);
}
.topbar-controls select,
.topbar-controls input[type="range"] {
  accent-color: var(--accent);
}
.topbar-controls select {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 5px 8px;
  font-size: 12px;
}
.opacity-control input[type="range"] { width: 90px; }
.readout {
  font-variant-numeric: tabular-nums;
  color: var(--text);
  min-width: 34px;
}

/* View-switch button group (4-up / maximise) */
.view-switch {
  display: inline-flex;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.view-btn {
  padding: 5px 10px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--surface-2);
  border: none;
  border-right: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.view-btn:last-child { border-right: none; }
.view-btn:hover { color: var(--text); background: var(--border); }
.view-btn.active {
  background: var(--accent);
  color: #fff;
}

/* ---- Buttons ---- */
.btn {
  padding: 7px 14px;
  font-size: 12px;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-dim));
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: filter 0.12s, transform 0.12s;
}
.btn:hover { filter: brightness(1.08); transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn.active {
  background: var(--accent);
  color: #fff;
  box-shadow: inset 0 0 0 2px rgba(255,255,255,0.25);
}
.zoom-group { display: inline-flex; gap: 4px; align-items: center; }
.zoom-group .btn { min-width: 30px; font-size: 16px; font-weight: 700; padding: 5px 10px; }
.zoom-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  font-weight: 600;
  margin-right: 2px;
}
.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border-strong);
}
.btn-secondary:hover { background: var(--border); }

/* ---- Status banner ---- */
.status-banner {
  padding: 8px 18px;
  font-size: 12px;
  color: var(--text-muted);
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}
.status-banner.error { color: #fca5a5; background: #2a1618; }

/* ---- Data source bar ---- */
.datasource-bar {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  padding: 10px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.ds-group {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.ds-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  font-weight: 700;
  color: var(--accent);
}
.ds-select {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 6px 9px;
  font-size: 12px;
  min-width: 200px;
}
.ds-divider {
  width: 1px;
  align-self: stretch;
  background: var(--border);
  margin: 0 4px;
}
.ds-upload {
  display: flex;
  align-items: center;
  gap: 10px;
}
.ds-upload-nifti { opacity: 0.95; }
.upload-or {
  font-size: 11px;
  color: var(--text-faint);
}
.upload-pick, .upload-pick-sm {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 10px;
  color: var(--text-muted);
}
.upload-pick > span:first-child,
.upload-pick-sm > span:first-child {
  text-transform: uppercase;
  letter-spacing: 0.4px;
  font-weight: 600;
}
.upload-pick input[type="file"],
.upload-pick-sm input[type="file"] {
  font-size: 11px;
  color: var(--text-muted);
  max-width: 170px;
}
.upload-status {
  font-size: 10px;
  color: var(--accent);
  font-weight: 600;
}
.upload-nifti-note {
  width: 100%;
  margin: 4px 0 0;
  font-size: 11px;
  color: var(--warning);
}
#loadUploadBtn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

@media (max-width: 900px) {
  .ds-divider { display: none; }
  .datasource-bar { gap: 10px; }
}

/* ---- Workspace ---- */
.cbct-workspace {
  display: grid;
  grid-template-columns: 1fr var(--panel-w);
  flex: 1 1 auto;
  min-height: 0;
}

/* Viewer + rotation rails. The viewer is ALWAYS dark; overlays inside it
   (hint, rotation rails, badges, pane controls) keep the dark palette in both
   themes by locally pinning the tokens they use. */
.viewer-wrap {
  position: relative;
  background: #000;
  overflow: hidden;
  --surface: #161f2b;
  --surface-2: #1e2937;
  --border: #263241;
  --border-strong: #3a4759;
  --text: #e8edf2;
  --text-muted: #9aa7b5;
  --text-faint: #6b7888;
}
#gl1 {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;   /* our pointer-drag owns touch; no page scroll/zoom hijack */
}
.viewer-wrap { touch-action: none; }

/* Mobile-only "hide/show controls" toggle at the top of the panel. */
.panel-toggle {
  display: none;
  position: sticky;
  top: 0;
  z-index: 4;
  width: 100%;
  padding: 11px;
  margin: 0 0 14px;
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  cursor: pointer;
}

/* Empty-state hint over the viewer (hidden once a dataset loads). */
.viewer-hint {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  pointer-events: none;   /* never blocks the canvas once it's interactive */
  background: radial-gradient(ellipse at center, rgba(var(--accent-rgb),0.06), transparent 60%);
}
.viewer-hint-card {
  max-width: 380px;
  padding: 26px 30px;
  background: rgba(15, 22, 32, 0.78);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}
.viewer-hint-icon {
  font-size: 40px;
  line-height: 1;
  color: var(--accent);
  margin-bottom: 12px;
}
.viewer-hint-card h3 {
  margin: 0 0 8px;
  font-size: 19px;
  font-weight: 700;
  color: var(--text);
}
.viewer-hint-card p {
  margin: 0;
  font-size: 13px;
  line-height: 1.55;
  color: var(--text-muted);
}
.viewer-hint-card b { color: var(--text); font-weight: 600; }

/* Rotation rails flanking the viewer */
.rot-rail {
  position: absolute;
  top: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 26px;
  padding: 16px 10px;
  background: rgba(15, 22, 32, 0.78);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
.rot-rail-left { left: 0; border-right: 1px solid var(--border); }

.rot-slider-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
.rot-slider-group label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  font-weight: 600;
}
/* Vertical rotation sliders, Elekta-style */
.rot-slider {
  writing-mode: vertical-lr;
  direction: rtl;
  width: 8px;
  height: 170px;
  accent-color: var(--accent);
  cursor: pointer;
}
.rot-readout {
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  min-width: 36px;
  text-align: center;
}
.rot-zero {
  margin-top: 4px;
  width: 22px;
  height: 18px;
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  background: var(--surface-2);
  color: var(--text-muted);
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  cursor: pointer;
}
.rot-zero:hover { background: var(--accent); border-color: var(--accent); color: #fff; }

/* Active-pane indicator badge */
.active-pane-badge {
  position: absolute;
  top: 10px;
  right: 12px;
  z-index: 5;
  padding: 4px 10px;
  font-size: 11px;
  color: var(--text-muted);
  background: rgba(15, 22, 32, 0.82);
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
.active-pane-badge b { color: var(--accent); }

/* Clip (comparison) canvas — layered exactly over the base canvas */
.clip-canvas {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;   /* the box handles interaction, not the canvas */
  z-index: 3;
}

/* Pane overlays layer — sits above canvases, holds maximise boxes/highlight */
.pane-overlays {
  position: absolute;
  inset: 0;
  z-index: 6;
  pointer-events: none;   /* individual tiles re-enable pointer events */
}
.pane-tile {
  position: absolute;
  pointer-events: none;   /* pass right-drag through to the canvas */
  box-sizing: border-box;
  border: 2px solid transparent;
  transition: border-color 0.12s;
}
.pane-tile.active {
  border-color: var(--accent);
  box-shadow: inset 0 0 12px rgba(var(--accent-rgb),0.25);
}
.pane-max-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 26px;
  height: 26px;
  font-size: 15px;
  line-height: 1;
  color: #fff;
  background: rgba(15, 22, 32, 0.78);
  border: 1px solid var(--border-strong);
  border-radius: 5px;
  cursor: pointer;
  pointer-events: auto;    /* the button is clickable even though tile isn't */
  opacity: 0.55;
  transition: opacity 0.12s, background 0.12s;
}
.pane-max-btn:hover { opacity: 1; background: var(--accent); border-color: var(--accent); }
.pane-active-chip {
  position: absolute;
  top: 6px;
  left: 6px;
  padding: 2px 7px;
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 0.6px;
  color: #fff;
  background: var(--accent);
  border-radius: 4px;
  pointer-events: none;
}

/* Comparison clip box */
.clip-box {
  position: absolute;
  z-index: 7;
  border: 2px solid #ffd166;
  box-shadow: 0 0 0 9999px rgba(0,0,0,0.001);  /* keep pointer target tidy */
  cursor: move;
  background: transparent;
}
.clip-box-tag {
  position: absolute;
  top: -20px;
  left: -2px;
  padding: 1px 7px;
  font-size: 10px;
  font-weight: 700;
  color: #1a1a1a;
  background: #ffd166;
  border-radius: 3px;
  white-space: nowrap;
}
.clip-box-resize {
  position: absolute;
  right: -7px;
  bottom: -7px;
  width: 14px;
  height: 14px;
  background: #ffd166;
  border: 2px solid #1a1a1a;
  border-radius: 3px;
  cursor: nwse-resize;
}

/* ---- Control panel ---- */
.cbct-panel {
  background: var(--surface);
  border-left: 1px solid var(--border);
  padding: 18px;
  overflow-y: auto;
}
.cbct-panel h3 {
  margin: 0 0 4px;
  font-size: 15px;
  font-weight: 700;
}
.panel-hint {
  margin: 0 0 16px;
  font-size: 11.5px;
  line-height: 1.5;
  color: var(--text-muted);
}

.control-block {
  margin-bottom: 18px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
}
.control-block h4 {
  margin: 0 0 10px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--accent);
  font-weight: 700;
}

.axis-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.axis-label {
  font-size: 13px;
  color: var(--text-muted);
  width: 64px;
  flex-shrink: 0;
  font-weight: 600;
}
.fine-controls {
  display: flex;
  align-items: center;
  gap: 4px;
}
.fine-btn {
  padding: 6px 9px;
  font-size: 12.5px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 5px;
  cursor: pointer;
  transition: background 0.1s;
  min-width: 34px;
}
.fine-btn:hover { background: var(--accent); border-color: var(--accent); color: #fff; }
.fine-btn:active { transform: translateY(1px); }
.axis-value {
  min-width: 48px;
  text-align: center;
  font-size: 15px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}
.control-block h4 { font-size: 12px; }

/* Live transform readout */
.readout-block pre {
  margin: 0;
  padding: 10px 12px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px;
  line-height: 1.6;
  color: #5eead4;
  background: #0c1119;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-variant-numeric: tabular-nums;
}

/* Large 6-DOF transform display */
.xform-big {
  background: #0c1119;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
}
.xform-line {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 4px 0;
}
.xform-line + .xform-line { border-top: 1px solid rgba(255,255,255,0.05); }
.xform-tag {
  font-size: 14px;
  font-weight: 800;
  color: var(--accent);
  width: 18px;
  flex-shrink: 0;
}
.xform-vals {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 22px;
  font-weight: 700;
  color: #5eead4;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.5px;
  flex: 1;
}
.xform-unit {
  font-size: 12px;
  color: var(--text-muted);
  flex-shrink: 0;
}

/* Imaging tools */
.tool-sublabel {
  display: block;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-faint);
  font-weight: 700;
  margin: 14px 0 6px;
}
.tool-sublabel:first-child { margin-top: 0; }
.window-presets {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 10px;
}
.window-btn {
  padding: 7px 8px;
  font-size: 12px;
  font-weight: 600;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.12s;
}
.window-btn:hover { background: var(--border); }
.window-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.threshold-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 7px;
}
.threshold-row label {
  font-size: 11px;
  color: var(--text-muted);
  width: 78px;
  flex-shrink: 0;
}
.threshold-row input[type="range"] { flex: 1; accent-color: var(--accent); }
.thr-readout {
  font-size: 12px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  width: 44px;
  text-align: right;
}
.apply-to-row {
  margin: 4px 0 2px;
}
.apply-to-row label {
  font-size: 11px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}
.hu-readout {
  background: #0c1119;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
}
.hu-line {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 3px 0;
  font-size: 12px;
  color: var(--text-muted);
}
.hu-line b {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 15px;
  color: #5eead4;
  font-variant-numeric: tabular-nums;
}
.ruler-row { display: flex; }
.ruler-row .btn { flex: 1; }

/* RT Structures list */
.structures-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 8px;
}
.struct-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 6px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 12px;
  color: var(--text);
}
.struct-row:hover { background: var(--surface-2); }
.struct-swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  flex-shrink: 0;
  border: 1px solid rgba(255,255,255,0.2);
}
.struct-name { flex: 1; }

/* Window/level groups — visually separate reference vs CBCT */
.wl-group {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px;
  margin-bottom: 8px;
  background: rgba(255,255,255,0.02);
}
.wl-group-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}
.wl-group-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--accent);
}

/* Structures master toggle */
.struct-master-row {
  margin-bottom: 8px;
}
.struct-master-row .btn {
  width: 100%;
}
/* Swatch colors approximating the NiiVue colormaps used for structures */
.struct-red     { background: #e63946; }
.struct-green   { background: #2a9d8f; }
.struct-blue    { background: #4361ee; }
.struct-cyan    { background: #4cc9f0; }
.struct-magenta { background: #d62898; }
.struct-yellow  { background: #ffd166; }
.struct-warm    { background: linear-gradient(135deg, #ff9e00, #ff0054); }
.struct-cool    { background: linear-gradient(135deg, #4cc9f0, #4361ee); }
.struct-hot     { background: linear-gradient(135deg, #ffd166, #e63946); }
.struct-winter  { background: linear-gradient(135deg, #0077b6, #00f5d4); }


.button-row {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}
.button-row .btn { flex: 1; }

/* Submit + score */
.btn-submit {
  width: 100%;
  margin-bottom: 14px;
  padding: 11px;
  font-size: 14px;
  background: linear-gradient(135deg, var(--accent), var(--accent-dim));
}

/* Randomise — the student's "start the exercise" action. Distinct blue so it
   reads as a separate step from Submit. */
.btn-randomise {
  width: 100%;
  margin-bottom: 10px;
  padding: 11px;
  font-size: 14px;
  background: linear-gradient(135deg, #2563eb, #1d4ed8);
}

/* Expert / admin panel (only shown with ?admin=1). Amber framing marks it as a
   privileged control distinct from the student flow. */
.admin-panel {
  margin: 4px 0 16px;
  padding: 14px;
  background: rgba(180, 130, 20, 0.08);
  border: 1px solid #7a5a12;
  border-radius: var(--radius);
}
.admin-panel h4 {
  margin: 0 0 6px;
  font-size: 13px;
  color: #f0c25a;
  letter-spacing: 0.02em;
}
.admin-hint {
  margin: 0 0 10px;
  font-size: 12px;
  line-height: 1.45;
  color: var(--text-muted);
}
.admin-answer {
  margin: 0 0 12px;
  padding: 8px 10px;
  background: rgba(13, 148, 136, 0.10);
  border: 1px solid rgba(13, 148, 136, 0.45);
  border-radius: var(--radius-sm);
}
.admin-answer-label {
  display: block;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-faint);
  margin-bottom: 2px;
}
.admin-answer-val {
  font-size: 12px;
  font-weight: 600;
  color: #5eead4;
  font-variant-numeric: tabular-nums;
}
.admin-pw-label {
  display: block;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-faint);
  margin-bottom: 4px;
}
.admin-pw {
  width: 100%;
  box-sizing: border-box;
  margin-bottom: 10px;
  padding: 8px 10px;
  font-size: 13px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
}
.admin-pw:focus { outline: none; border-color: #f0c25a; }
.btn-save100 {
  width: 100%;
  padding: 10px;
  font-size: 14px;
  background: linear-gradient(135deg, #d97706, #b45309);
}

/* Save-dataset panel (admin mode + upload right). Blue framing = a save-to-server
   action, distinct from the amber expert panel. */
.dataset-panel {
  margin: 4px 0 16px;
  padding: 14px;
  background: rgba(37, 99, 235, 0.08);
  border: 1px solid #2d4a7a;
  border-radius: var(--radius);
}
.dataset-panel h4 {
  margin: 0 0 6px;
  font-size: 13px;
  color: #7fb0ff;
  letter-spacing: 0.02em;
}
.dataset-panel .structures-list { max-height: 168px; overflow-y: auto; margin: 4px 0 10px; }
.dataset-overwrite {
  display: flex; align-items: center; gap: 7px;
  font-size: 12px; color: var(--text-muted); margin: 6px 0 12px; cursor: pointer;
}
.btn-savedataset {
  width: 100%;
  padding: 11px;
  font-size: 14px;
  background: linear-gradient(135deg, #2563eb, #1d4ed8);
}
.btn-savedataset:disabled { opacity: 0.55; cursor: wait; }

/* Delete preset (admin) */
.btn-delete { background: linear-gradient(135deg, #dc2626, #991b1b); }

/* Upload progress inside the dataset panel */
.save-progress {
  display: flex; align-items: center; gap: 10px;
  margin: 2px 0 12px;
}
.save-progress-track {
  flex: 1; height: 8px; border-radius: 999px; overflow: hidden;
  background: var(--surface); border: 1px solid var(--border-strong);
}
.save-progress-bar {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, #2563eb, #38bdf8);
  transition: width 0.15s linear;
}
.save-progress-text {
  font-size: 12px; font-weight: 700; font-variant-numeric: tabular-nums;
  color: #7fb0ff; min-width: 40px; text-align: right;
}

/* Toast notification */
.toast {
  position: fixed;
  top: 14px; left: 50%; transform: translateX(-50%);
  z-index: 2000;
  max-width: min(92vw, 480px);
  padding: 12px 18px;
  font-size: 13.5px; font-weight: 600;
  color: #eafff2;
  background: rgba(15, 26, 20, 0.96);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  box-shadow: 0 8px 30px rgba(0,0,0,0.5);
  animation: toast-in 0.25s ease;
}
.toast.error { color: #ffe1e1; background: rgba(32, 14, 14, 0.96); border-color: var(--danger); }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, -8px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* Logged-in user chip in the top bar. */
.user-chip { font-size: 12px; color: var(--text-muted); white-space: nowrap; }

/* Login gate overlay (hard gate for the matcher). */
.login-gate {
  position: fixed; inset: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: center;
  background: rgba(8, 12, 18, 0.92); backdrop-filter: blur(3px);
}
.login-gate .login-card {
  width: 340px; max-width: 90vw; padding: 24px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.login-gate h2 { margin: 0 0 4px; font-size: 18px; }
.login-gate .gate-sub { margin: 0 0 14px; font-size: 12px; color: var(--text-muted); }
.login-gate .gate-field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.login-gate .gate-field label { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-faint); }
.login-gate .gate-field input, .login-gate .gate-field select {
  width: 100%; box-sizing: border-box; padding: 9px 10px; font-size: 13px; color: var(--text);
  background: var(--bg); border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
}
.login-gate .gate-err { color: #fca5a5; font-size: 12px; min-height: 16px; margin-bottom: 6px; }
.score-panel {
  margin-bottom: 16px;
  padding: 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  animation: score-in 0.3s ease;
}
@keyframes score-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.score-tier {
  text-align: center;
  font-size: 18px;
  font-weight: 900;
  letter-spacing: 1px;
  margin-bottom: 12px;
}
.score-tier.tier-perfect   { color: #ffd166; text-shadow: 0 0 16px rgba(255,209,102,0.5); }
.score-tier.tier-excellent { color: #06d6a0; }
.score-tier.tier-great     { color: #4cc9f0; }
.score-tier.tier-good      { color: #9aa7b5; }
.score-tier.tier-ok        { color: #6b7888; font-size: 15px; }
.score-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}
.score-cell {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  text-align: center;
}
.score-cell-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  font-weight: 700;
}
.score-cell-err {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
.score-cell-pts {
  font-size: 12px;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.score-cell-pts b { font-size: 14px; }
.score-combined-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  font-weight: 700;
}
.score-combined {
  font-size: 24px;
  font-weight: 900;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.panel-note {
  font-size: 10.5px;
  line-height: 1.55;
  color: var(--text-faint);
  padding: 10px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  border: 1px dashed var(--border-strong);
}

/* ===================== Tablet & mobile ===================== */
@media (max-width: 820px) {
  :root { --panel-w: 100%; }

  /* Top bar: compact, wraps */
  .cbct-topbar { height: auto; min-height: var(--header-h); flex-wrap: wrap; gap: 8px 10px; padding: 8px 12px; }
  .cbct-topbar .spacer { display: none; }
  .brand-mode { display: none; }
  .topbar-controls { flex-wrap: wrap; gap: 8px 10px; }
  .opacity-control input[type="range"] { width: 74px; }

  /* Workspace: viewer fills; the control panel is a scrollable dock beneath it */
  .cbct-workspace { display: flex; flex-direction: column; }
  .viewer-wrap { flex: 1 1 auto; min-height: 42vh; }
  .cbct-panel { flex: 0 0 auto; max-height: 46vh; border-left: none; border-top: 1px solid var(--border); }

  /* Collapse toggle appears on mobile; collapsing hands the space to the viewer */
  .panel-toggle { display: block; }
  .cbct-workspace.panel-collapsed .cbct-panel { max-height: 44px; overflow: hidden; }

  /* The vertical rotation rail overlaps a small viewer — hide it. Rotation is
     still fully available via the Rotation fine-buttons in the panel. */
  .rot-rail { display: none; }

  /* Larger touch targets — fine controls are the primary input on touch */
  .fine-btn { min-width: 46px; min-height: 44px; font-size: 15px; padding: 8px 10px; }
  .fine-controls { gap: 6px; }
  .axis-row { margin-bottom: 14px; }
  .axis-label { width: 52px; font-size: 12px; }
  .axis-value { font-size: 18px; min-width: 46px; }
  .rot-zero { width: 30px; height: 28px; font-size: 12px; }
  .window-btn { min-height: 44px; font-size: 13px; }
  .btn { padding: 9px 14px; font-size: 13px; }
  .view-btn { padding: 9px 13px; font-size: 13px; }
  .btn-submit, .btn-randomise, .btn-save100 { padding: 15px; font-size: 15px; }
  .datasource-bar { padding: 8px 12px; }
  .ds-select { min-width: 150px; }
}

/* ===================== Phone ===================== */
@media (max-width: 560px) {
  .cbct-topbar .brand { font-size: 13px; }
  /* Keep the many top-bar controls on one swipeable row to save vertical space */
  .topbar-controls { flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: 2px; }
  .topbar-controls > * { flex: 0 0 auto; }
  .datasource-bar { flex-wrap: nowrap; overflow-x: auto; }
  .datasource-bar > * { flex: 0 0 auto; }
  .viewer-wrap { min-height: 40vh; }
  .cbct-panel { max-height: 54vh; }
  .window-presets { grid-template-columns: repeat(4, 1fr); }
  .login-gate .login-card { width: 92vw; padding: 20px; }
}
