:root {
  --bg: #0f1419;
  --bg-2: #161c24;
  --panel: #1b232e;
  --panel-2: #212b38;
  --border: #2b3745;
  --text: #e6edf3;
  --muted: #8b98a5;
  --accent: #39d353;
  --accent-2: #2f81f7;
  --danger: #f85149;
  --warn: #e3b341;
  --radius: 10px;
  font-synthesis: none;
  /* Scales all rem-based sizing up from the 16px default (matches the live portal). */
  font-size: 19px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  font-size: 17px;
}

h1, h2, h3 { margin: 0; font-weight: 600; }
.muted { color: var(--muted); font-size: 14px; }
.hidden { display: none !important; }

/* -------------------------------------------------------- top bar */
.topbar {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px;
  padding: 10px 18px; background: var(--bg-2); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { font-size: 22px; letter-spacing: 0.5px; }
.brand p { margin: 0; font-size: 13px; color: var(--muted); }
.logo { font-size: 26px; color: var(--accent); }
.status-pill {
  padding: 5px 14px; border-radius: 999px; background: var(--panel-2);
  border: 1px solid var(--border); font-size: 14px; text-transform: capitalize;
}
.status-pill.running { color: var(--warn); border-color: var(--warn); }
.status-pill.done { color: var(--accent); border-color: var(--accent); }
.status-pill.error { color: var(--danger); border-color: var(--danger); }
.status-pill.awaiting_relabel { color: var(--accent-2); border-color: var(--accent-2); }

/* -------------------------------------------------------- layout */
.layout { display: grid; grid-template-columns: 330px 1fr; height: calc(100vh - 55px); }
.sidebar {
  background: var(--bg-2); border-right: 1px solid var(--border);
  padding: 14px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px;
}
.main { padding: 0; overflow: hidden; display: flex; flex-direction: column; }

.panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.panel h2 { font-size: 16px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--muted); margin-bottom: 10px; }
.panel.grow { flex: 1; display: flex; flex-direction: column; min-height: 0; }

/* -------------------------------------------------------- dropzone */
.dropzone {
  border: 2px dashed var(--border); border-radius: var(--radius);
  padding: 20px; text-align: center; transition: border-color .15s, background .15s; cursor: pointer;
}
.dropzone p { margin: 4px 0; }
.dropzone.drag { border-color: var(--accent); background: rgba(57,211,83,.08); }
.file-info { margin-top: 10px; font-size: 14px; color: var(--accent); word-break: break-all; }
.mask-row { margin-top: 10px; }
.btn.small { padding: 5px 10px; font-size: 14px; }
.opt.inline { display: inline-flex; padding: 0; }
.restore-row { display: flex; gap: 6px; margin-top: 6px; }
.restore-row select, .restore-row input[type="text"] { flex: 1; min-width: 0; background: var(--panel-2); color: var(--text); border: 1px solid var(--border); border-radius: 6px; padding: 5px; font-size: 14px; }
input[type="color"] { width: 34px; height: 26px; padding: 0; border: 1px solid var(--border); border-radius: 6px; background: var(--panel-2); cursor: pointer; }

/* -------------------------------------------------------- options */
.opt { display: flex; align-items: center; gap: 8px; padding: 4px 0; font-size: 16px; cursor: pointer; }
.advanced { margin: 8px 0; }
.advanced summary { cursor: pointer; color: var(--muted); font-size: 14px; }

/* -------------------------------------------------------- buttons */
.btn {
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; font-size: 16px; transition: .15s;
}
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: #06210e; border-color: var(--accent); font-weight: 600; width: 100%; margin-top: 10px; }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.toggle.active { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }

/* -------------------------------------------------------- stepper + console */
.stepper { list-style: none; margin: 0 0 10px; padding: 0; font-size: 14px; }
.stepper li { display: flex; align-items: center; gap: 8px; padding: 3px 0; color: var(--muted); }
.stepper li .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--border); flex: none; }
.stepper li.active { color: var(--text); }
.stepper li.active .dot { background: var(--warn); box-shadow: 0 0 0 3px rgba(227,179,65,.2); }
.stepper li.done { color: var(--accent); }
.stepper li.done .dot { background: var(--accent); }
/* -------------------------------------------------------- progress bar */
.progress-wrap { margin: 2px 0 10px; }
.progress-label { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.progress-track { height: 8px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px; overflow: hidden; }
.progress-fill { height: 100%; width: 0%; background: var(--accent); border-radius: 999px; transition: width .15s ease; }
.progress-fill.indeterminate { width: 35% !important; transition: none; animation: prog-indet 1.1s ease-in-out infinite; }
@keyframes prog-indet { 0% { margin-left: -35%; } 100% { margin-left: 100%; } }

.console {
  flex: 1; min-height: 120px; overflow-y: auto; background: #0a0e13; border: 1px solid var(--border);
  border-radius: 8px; padding: 8px; font-family: "Cascadia Code", Consolas, monospace; font-size: 13px; line-height: 1.5;
}
.console .line { white-space: pre-wrap; word-break: break-word; }
.console .line.error { color: var(--danger); }
.console .line.phase { color: var(--accent-2); font-weight: 600; }

/* -------------------------------------------------------- tabs */
.tabs { display: flex; gap: 2px; background: var(--bg-2); border-bottom: 1px solid var(--border); padding: 0 10px; }
.tab {
  border: none; background: transparent; color: var(--muted); padding: 12px 16px; cursor: pointer;
  font-size: 16px; border-bottom: 2px solid transparent;
}
.tab:hover:not(:disabled) { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.tab:disabled { opacity: .4; cursor: not-allowed; }

.tabpane { display: none; flex: 1; min-height: 0; overflow: auto; padding: 16px; }
.tabpane.active { display: flex; flex-direction: column; }

.welcome { max-width: 620px; }
.welcome ol { line-height: 1.9; }

/* -------------------------------------------------------- toolbar */
.toolbar { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; margin-bottom: 12px; }
.toolbar label { display: flex; align-items: center; gap: 6px; font-size: 14px; color: var(--muted); }
.toolbar input[type="number"], .toolbar select {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 5px 8px; font-size: 14px; width: 90px;
}
.toolbar select { width: auto; }

/* -------------------------------------------------------- 2x2 view grid */
.view-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  grid-template-areas: "axial sagittal" "seg coronal";
  gap: 12px; flex: 1; min-height: 0;
}
#quad-axial { grid-area: axial; }
#quad-sagittal { grid-area: sagittal; }
#quad-seg { grid-area: seg; }
#quad-coronal { grid-area: coronal; }

.view-cell {
  position: relative; background: #000; border: 1px solid var(--border); border-radius: 8px;
  display: flex; flex-direction: column; overflow: hidden; min-height: 0; min-width: 0;
}
.view-cell h4 { margin: 0; padding: 8px; font-size: 14px; text-align: center; background: var(--panel); color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.view-cell .cell-body { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.view-cell .plot { flex: 1; min-height: 0; width: 100%; }
.view-cell .seg-foot { padding: 4px 8px 6px; display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; max-height: 34%; overflow: auto; }
.view-cell .seg-foot .legend { padding: 0; gap: 8px; }

/* 3D view header: title + inline mask/skeleton/background controls */
#quad-seg .seg-head { display: flex; align-items: center; flex-wrap: wrap; gap: 6px 10px; padding: 6px 36px 6px 8px; background: var(--panel); }
#quad-seg .seg-head h4 { padding: 0; margin: 0; background: transparent; text-align: left; }
#quad-seg .seg-controls { display: flex; align-items: center; flex-wrap: wrap; gap: 6px 10px; }
#quad-seg .seg-controls select { background: var(--panel-2); color: var(--text); border: 1px solid var(--border); border-radius: 6px; padding: 3px 6px; font-size: 13px; }
#quad-seg .seg-controls .opt { font-size: 13px; color: var(--muted); padding: 0; gap: 5px; }
#quad-seg .seg-controls .mini-color { display: flex; align-items: center; gap: 5px; font-size: 13px; color: var(--muted); }
#quad-seg .seg-controls input[type="color"] { width: 26px; height: 22px; }

/* maximize / restore a single quadrant */
.maximize-btn {
  position: absolute; right: 6px; top: 6px; z-index: 5;
  width: 26px; height: 26px; padding: 0; border-radius: 6px; cursor: pointer;
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text);
  font-size: 17px; line-height: 1; display: flex; align-items: center; justify-content: center;
}
.maximize-btn:hover { border-color: var(--muted); }
.maximize-btn.active { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }
.reset-view-btn {
  position: absolute; right: 36px; top: 6px; z-index: 5;
  width: 26px; height: 26px; padding: 0; border-radius: 6px; cursor: pointer;
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text);
  font-size: 18px; line-height: 1; display: flex; align-items: center; justify-content: center;
}
.reset-view-btn:hover { border-color: var(--muted); }
.view-grid.has-max .view-cell { display: none; }
/* !important overrides the per-cell `#quad-* { grid-area }` (ID specificity) so the
   maximized view actually spans the whole grid instead of staying in its quadrant. */
.view-grid.has-max .view-cell.maximized { display: flex; grid-area: 1 / 1 / -1 / -1 !important; }

.ct-cell .img-wrap { position: relative; flex: 1; min-height: 0; margin: 6px; cursor: crosshair; overflow: hidden; }
/* Explicitly positioned/sized by layoutCtImage() to the physical-aspect content rect. */
.ct-cell img { position: absolute; left: 0; top: 0; image-rendering: pixelated; }
.ct-cross { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.ct-cross line { stroke: #37e0e0; stroke-width: 1; opacity: 0.85; }
.ct-cell input[type="range"] { width: 100%; margin: 0; }
.ct-cell .slice-row { padding: 8px; display: flex; align-items: center; gap: 8px; }
.ct-cell .slice-row span { font-size: 13px; color: var(--muted); min-width: 56px; text-align: right; }

/* -------------------------------------------------------- plots */
.plot { flex: 1; min-height: 420px; width: 100%; }
.legend { display: flex; flex-wrap: wrap; gap: 10px; padding: 8px 0; }
.legend .item { display: flex; align-items: center; gap: 6px; font-size: 14px; }
.legend .swatch, .swatch { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.swatch.red { background: #ff4136; } .swatch.blue { background: #2f81f7; }

/* -------------------------------------------------------- relabel */
.relabel-split { display: grid; grid-template-columns: 1fr 240px; gap: 14px; flex: 1; min-height: 0; }
.relabel-controls { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 14px; overflow-y: auto; }
.relabel-controls h3 { font-size: 14px; text-transform: uppercase; color: var(--muted); margin: 12px 0 8px; }
.selected-info { font-size: 16px; padding: 8px; background: var(--panel-2); border-radius: 6px; }
.label-choices { display: flex; flex-direction: column; gap: 4px; }
.label-choices.locked { opacity: .45; pointer-events: none; }
.label-choices .choice { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 6px; cursor: pointer; border: 1px solid transparent; }
.label-choices .choice:hover { background: var(--panel-2); }
.label-choices .choice.sel { border-color: var(--accent-2); background: var(--panel-2); }
.label-choices .choice .swatch { width: 14px; height: 14px; }

/* -------------------------------------------------------- downloads */
.download-list { display: flex; flex-direction: column; gap: 8px; max-width: 520px; }
.download-row { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; }
.download-row a { color: var(--accent); text-decoration: none; }

/* -------------------------------------------------------- modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.65); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal-card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; width: min(720px, 92vw); max-height: 92vh; overflow: hidden; display: flex; flex-direction: column; }
.modal-card.cx-card { width: min(1120px, 95vw); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.cx-viewer { display: grid; grid-template-columns: 1fr 230px; gap: 14px; padding: 16px; overflow: auto; }
.cx-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.cx-cell { position: relative; display: flex; flex-direction: column; gap: 4px; }
.cx-cell h5 { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); text-align: center; }
.cx-wrap { position: relative; background: #000; border: 1px solid var(--border); border-radius: 8px; aspect-ratio: 1; overflow: hidden; cursor: crosshair; }
/* Explicitly positioned/sized by cxLayoutImage() so zoom/pan works and the SVG overlay aligns. */
.cx-wrap img { position: absolute; left: 0; top: 0; image-rendering: pixelated; }
.cx-reset-btn {
  position: absolute; right: 4px; top: 2px; z-index: 5;
  width: 22px; height: 22px; padding: 0; border-radius: 6px; cursor: pointer;
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text);
  font-size: 16px; line-height: 1; display: flex; align-items: center; justify-content: center;
}
.cx-reset-btn:hover { border-color: var(--muted); }
.cx-svg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.cx-svg .cx-cross { stroke: #37e0e0; stroke-width: 1; opacity: 0.8; }
/* 2x2 cross-section layout: axial | sagittal (top), tapering | coronal (bottom). */
.cx-grid.cx-2x2 { grid-template-columns: 1fr 1fr; }
.cx-2x2 .cx-wrap, .cx-2x2 .cx-taper { aspect-ratio: auto; height: clamp(200px, 32vh, 320px); }
.cx-taper { background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.cx-taper-empty { display: flex; align-items: center; justify-content: center; height: 100%; padding: 10px; color: var(--muted); font-size: 14px; text-align: center; }
.cx-controls { display: flex; flex-direction: column; gap: 12px; }
.cx-controls input[type="range"] { width: 100%; }
.cx-legend { display: flex; flex-direction: column; gap: 4px; font-size: 14px; }
.cx-legend span { display: flex; align-items: center; gap: 6px; }
.cx-readout { font-size: 16px; line-height: 1.7; padding: 8px; background: var(--panel-2); border-radius: 6px; }

/* -------------------------------------------------------- static demo tweaks */
.tips { margin: 0; padding-left: 16px; line-height: 1.7; font-size: 14px; color: var(--muted); }
.tips li { margin-bottom: 6px; }
.tips strong { color: var(--text); }
/* Greyed, non-actionable buttons in the demo show the forbidden cursor on hover. */
.btn:disabled, .btn[disabled] { cursor: not-allowed; opacity: .45; }
#gateCheck { cursor: pointer; }
/* The demo pre-fills the console with a completed run's log; give it room to read. */
.sidebar .console { min-height: 150px; max-height: 240px; }

/* -------------------------------------------------------- guided tour */
.tour { position: fixed; inset: 0; z-index: 200; }
.tour.hidden { display: none; }
/* Spotlight: a transparent hole with a huge box-shadow that dims everything else. */
.tour-spot {
  position: absolute; top: 0; left: 0; width: 0; height: 0;
  border-radius: 12px;
  box-shadow: 0 0 0 9999px rgba(4, 8, 14, .66);
  outline: 2px solid var(--accent); outline-offset: 3px;
  pointer-events: none;
  transition: top .28s ease, left .28s ease, width .28s ease, height .28s ease;
}
.tour-card {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: min(360px, 92vw);
  background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 14px;
  box-shadow: 0 18px 52px rgba(0, 0, 0, .55);
  padding: 16px 18px 15px; pointer-events: auto;
  transition: top .28s ease, left .28s ease;
}
.tour-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.tour-count { font-size: 11px; color: var(--muted); letter-spacing: .08em; text-transform: uppercase; }
.tour-x { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 12px; padding: 2px 4px; }
.tour-x:hover { color: var(--text); }
.tour-card h4 { margin: 0 0 7px; font-size: 17px; color: var(--text); }
.tour-card p { margin: 0 0 14px; font-size: 14px; line-height: 1.6; color: var(--text); }
.tour-card p .kbd { color: var(--accent); font-weight: 600; }
.tour-actions { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.tour-dots { display: flex; gap: 5px; flex-wrap: wrap; max-width: 130px; }
.tour-dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--border); display: inline-block; transition: background .2s; }
.tour-dots i.on { background: var(--accent); }
.tour-btns { display: flex; gap: 8px; }
.tour-btns .btn { margin-top: 0; }
.tour-btns .btn.primary { width: auto; }
.tour-btns .btn:disabled { opacity: .4; }

/* -------------------------------------------------------- feedback */
.topbar-right { display: flex; align-items: center; gap: 10px; justify-self: end; }
.topbar-contact { justify-self: center; font-size: 17px; font-weight: 500; color: var(--accent-2); text-decoration: none; white-space: nowrap; }
.topbar-contact:hover { text-decoration: underline; }

.fb-body { padding: 18px; display: flex; flex-direction: column; gap: 12px; overflow: auto; }
.fb-field { display: flex; flex-direction: column; gap: 5px; font-size: 14px; color: var(--muted); }
.fb-field input, .fb-field select, .fb-field textarea {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 8px 10px; font-size: 15px; font-family: inherit; width: 100%;
}
.fb-field textarea { resize: vertical; min-height: 92px; }
.fb-field input:focus, .fb-field select:focus, .fb-field textarea:focus {
  outline: none; border-color: var(--accent-2);
}
.fb-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 2px; }
.fb-status { margin: 0; font-size: 14px; min-height: 18px; }
.fb-status.ok { color: var(--accent); }
.fb-status.err { color: var(--danger); }
.fb-status.pending { color: var(--muted); }

.sidebar-contact { margin: auto 0 0; padding: 4px 2px; font-size: 13px; color: var(--muted); }
.sidebar-contact a, .modal a { color: var(--accent-2); text-decoration: none; }
.sidebar-contact a:hover, .modal a:hover { text-decoration: underline; }
