@import url('./base.css');

/* =============================================================================
   Neurodesk imaging workspace — the one stylesheet an app needs.

   This file is the design language for every app mounted with
   `mountImagingWorkspace`. It is organised as a small vocabulary of classes
   (metrics in docs/architecture/design-system.md):

     Shell      .nd-imaging-workspace / -app-header / -controls / -viewer / -status
     Sections   details.nd-sidebar-section > summary.nd-section-title + .nd-section-content
     Fields     .nd-field, .nd-hint, .nd-check, .nd-row, .nd-file, .nd-file-button, .nd-file-info
     Images     nd-image-pair > .nd-image-slot (-header / -title / -clear / -actions)
     Buttons    .nd-btn, .nd-btn-primary, .nd-btn-secondary, .nd-btn-danger,
                .nd-btn-sm, .nd-btn-icon
     Messages   .nd-message.(error|warning|success|info), .nd-info-icon
     Viewer     .nd-viewer-toolbar, .nd-view-tabs/.nd-view-tab, .nd-viewer-actions,
                .nd-viewer-canvas-wrapper, .nd-viewer-empty, .nd-viewer-info
     Editing    .nd-mask-editor > .nd-tool-group/.nd-tool-btn, .nd-brush-control;
                .nd-edit-btn in result rows
     Console    .nd-console-container / -header / -title / -actions / -clear / -output,
                .nd-console-resizer, .nd-console-tabs / -tab, .nd-console-panels
     Status     .nd-imaging-status > .nd-status-label / .nd-status-text / progress
     Dialogs    dialog.nd-dialog > .nd-dialog-header + .nd-dialog-body, .nd-command

   Rules for app stylesheets (enforced by test/design-system.test.mjs):
     - colour only through --nd-color-* tokens; never hex/rgb literals
     - never restyle the vocabulary above; add a class to this file instead
     - app CSS is for scientific widgets that genuinely have no shared owner
   ============================================================================= */

/* --------------------------------------------------------------------------
   1. Shell grid
   -------------------------------------------------------------------------- */
nd-imaging-workspace,
nd-imaging-app-header,
nd-imaging-brand {
  box-sizing: border-box;
}

.nd-imaging-workspace {
  display: grid;
  grid-template-columns: minmax(260px, 320px) minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-height: 100vh;
  height: 100vh;
  overflow: hidden;
  color: var(--nd-color-text);
  background: var(--nd-color-bg);
  font-family: var(--nd-font-ui, Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif);
}

.nd-imaging-app-header {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--nd-space-md);
  padding: var(--nd-space-sm) var(--nd-space-lg);
  background: var(--nd-color-surface);
  border-bottom: 1px solid var(--nd-color-border);
}

.nd-imaging-brand {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: var(--nd-space-sm);
}

.nd-imaging-mark {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  flex: 0 0 auto;
  border-radius: var(--nd-radius-md);
  color: white;
  background: linear-gradient(135deg, var(--nd-color-primary), var(--nd-color-accent));
  font-weight: 700;
}

.nd-imaging-brand-copy {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.nd-imaging-brand-copy strong {
  font-size: 0.95rem;
  line-height: 1.2;
}

.nd-imaging-brand-copy small {
  color: var(--nd-color-text-muted);
  font-size: 0.72rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nd-imaging-navigation {
  display: flex;
  align-items: center;
  gap: var(--nd-space-sm);
}

.nd-imaging-navigation .nd-header-link {
  color: var(--nd-color-text);
  text-decoration: none;
}

/* Sidebar. The flat form (labels and controls as direct children) keeps the
   original flex layout; a sidebar built from sections becomes a plain column
   so section borders run edge to edge exactly as in QSMbly. */
.nd-imaging-controls {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: var(--nd-space-xs) var(--nd-space-sm);
  grid-column: 1;
  grid-row: 2;
  min-width: 0;
  min-height: 0;
  overflow-y: auto;
  padding: var(--nd-space-md);
  background: var(--nd-color-surface);
  border-right: 1px solid var(--nd-color-border);
  margin: 0 !important;
  border-top: 0;
  border-bottom: 0;
}

.nd-imaging-controls:has(> .nd-sidebar-section) {
  display: block;
  padding: 0;
}
.nd-imaging-controls:has(> .nd-sidebar-section) > :not(.nd-sidebar-section):not([hidden]) { padding: var(--nd-space-sm) var(--nd-space-md); }
.nd-imaging-controls:has(> .nd-sidebar-section) > :not(.nd-sidebar-section) > button { width: 100%; }

.nd-imaging-controls > label {
  display: block;
  width: 100%;
  margin-top: var(--nd-space-sm);
  color: var(--nd-color-text-muted) !important;
  font-size: 0.75rem;
  font-weight: 600;
}

.nd-imaging-controls > .row {
  flex: 0 0 auto;
  width: 100%;
  min-width: 0;
  align-content: flex-start;
}

.nd-imaging-controls > label:first-child { margin-top: 0; }

.nd-imaging-controls button,
.nd-imaging-controls select,
.nd-imaging-controls input:not([type="checkbox"]):not([type="range"]) {
  min-height: 34px;
  margin: var(--nd-space-xs) 0;
  padding: var(--nd-space-sm);
  color: var(--nd-color-text);
  background: var(--nd-color-surface-elevated);
  border: 1px solid var(--nd-color-border-light);
  border-radius: var(--nd-radius-sm);
  font: inherit;
}

.nd-imaging-controls > select,
.nd-imaging-controls > input:not([type="checkbox"]):not([type="range"]) {
  width: 100%;
}

.nd-imaging-controls button {
  cursor: pointer;
  transition: background var(--nd-transition-fast), border-color var(--nd-transition-fast);
}

.nd-imaging-controls button:hover:not(:disabled) {
  background: var(--nd-color-border);
  border-color: var(--nd-color-primary);
}

.nd-imaging-controls button:disabled { opacity: 0.45; cursor: not-allowed; }
.nd-imaging-controls #applyBtn,
.nd-imaging-controls #processButton {
  color: var(--nd-color-on-primary);
  background: var(--nd-color-primary);
  border-color: var(--nd-color-primary);
  font-weight: 600;
}

.nd-imaging-controls > label[for="overlayCheck"],
.nd-imaging-controls > label[for="overlayOpacity"],
.nd-imaging-controls > label[for="overlayColor"] {
  width: auto;
  margin-right: var(--nd-space-xs);
}

.nd-imaging-controls > input[type="range"] {
  width: min(100%, 150px);
  accent-color: var(--nd-color-primary);
}

.nd-imaging-viewer {
  grid-column: 2;
  grid-row: 2;
  min-width: 0;
  min-height: 0;
  position: relative;
  overflow: hidden;
  background: #000;
}

/* A viewer composed from toolbar + canvas + info + console stacks vertically. */
.nd-imaging-viewer:has(> .nd-viewer-canvas-wrapper) {
  display: flex;
  flex-direction: column;
  background: var(--nd-color-bg);
}

.nd-imaging-status {
  grid-column: 1 / -1;
  grid-row: 3;
  min-width: 0;
  padding: var(--nd-space-xs) var(--nd-space-lg);
  background: var(--nd-color-surface);
  border-top: 1px solid var(--nd-color-border);
  color: var(--nd-color-text-muted);
  margin: 0 !important;
}

.nd-imaging-status #images {
  display: flex;
  gap: var(--nd-space-xs);
  overflow-x: auto;
}

.nd-imaging-status button {
  flex: 0 0 auto;
  padding: var(--nd-space-xs) var(--nd-space-sm);
  color: var(--nd-color-text);
  background: var(--nd-color-surface-elevated);
  border: 1px solid var(--nd-color-border);
  border-radius: var(--nd-radius-sm);
  cursor: pointer;
}

/* --------------------------------------------------------------------------
   2. Sidebar sections (0.8rem sentence-case title, 12px/16px padding)
   -------------------------------------------------------------------------- */
.nd-sidebar-section { width: 100%; min-width: 0; border-bottom: 1px solid var(--nd-color-border); }
.nd-section-title {
  display: flex;
  align-items: center;
  gap: var(--nd-space-sm);
  margin: 0;
  padding: 12px var(--nd-space-md);
  color: var(--nd-color-text);
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: -0.005em;
  cursor: pointer;
  user-select: none;
  list-style: none;
  transition: background var(--nd-transition-fast), color var(--nd-transition-fast);
}
.nd-section-title:hover { background: var(--nd-color-surface-elevated); }
.nd-section-title svg { width: 14px; height: 14px; flex-shrink: 0; color: var(--nd-color-text-muted); }
.nd-section-title::after {
  content: "";
  margin-left: auto;
  width: 6px;
  height: 6px;
  border-right: 1.5px solid var(--nd-color-text-dim);
  border-bottom: 1.5px solid var(--nd-color-text-dim);
  transform: translateY(-2px) rotate(45deg);
  transition: transform var(--nd-transition-fast);
}
.nd-section-title::-webkit-details-marker { display: none; }
.nd-section-title:focus-visible { outline: 2px solid var(--nd-color-primary); outline-offset: -2px; }
.nd-sidebar-section:not([open]) > .nd-section-title::after { transform: rotate(-45deg); }
.nd-workflow-disclosure > summary.nd-section-title::after,
.nd-workflow-disclosure:not([open]) > summary.nd-section-title::after { content: ""; }
.nd-workflow-disclosure:not([open]) > summary.nd-section-title::after { transform: rotate(-45deg); }
.nd-section-content { display: block; padding: 0 var(--nd-space-md) var(--nd-space-md); }
@media (pointer: coarse) {
  .nd-section-title { min-height: 44px; box-sizing: border-box; }
}
.nd-step-disabled .nd-section-content { pointer-events: none; opacity: 0.45; }
.nd-step-badge { margin-left: auto; font-size: 0.6rem; border-radius: 8px; padding: 1px 6px; background: var(--nd-color-border); color: var(--nd-color-text-dim); }

/* Section contents stack with one shared rhythm; children need no margins.
   Grid ignores display:none children, so hidden controls leave no gaps. */
.nd-imaging-controls .nd-section-content {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--nd-space-sm);
  align-content: start;
}
.nd-imaging-controls .nd-section-content > p { margin: 0; }
.nd-section-content > .nd-sidebar-section { border: 0; }
.nd-section-content > .nd-sidebar-section > .nd-section-title { padding: var(--nd-space-xs) 0; font-weight: 500; text-transform: none; letter-spacing: 0; font-size: 0.75rem; }
.nd-section-content > .nd-sidebar-section > .nd-section-title:hover { background: transparent; }
.nd-section-content > .nd-sidebar-section > .nd-section-content { padding: 0; }

/* --------------------------------------------------------------------------
   3. Fields (QSMbly: 0.8rem labels, 0.8rem controls, 0.7rem hints)
   -------------------------------------------------------------------------- */
:is(.nd-imaging-controls .nd-section-content, .nd-dialog-body, .nd-report-controls) :is(select, textarea, input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"])) {
  width: 100%;
  min-height: 30px;
  margin: 0;
  padding: var(--nd-space-xs) var(--nd-space-sm);
  color: var(--nd-color-text);
  background-color: var(--nd-color-bg);
  border: 1px solid var(--nd-color-border-light);
  border-radius: var(--nd-radius-sm);
  font: inherit;
  font-size: 0.8rem;
  transition: border-color var(--nd-transition-fast), box-shadow var(--nd-transition-fast);
}
:is(.nd-imaging-controls .nd-section-content, .nd-dialog-body, .nd-report-controls) :is(select, textarea, input):focus {
  outline: none;
  border-color: var(--nd-color-primary);
  box-shadow: 0 0 0 3px var(--nd-color-primary-glow);
}
:is(.nd-imaging-controls .nd-section-content, .nd-dialog-body, .nd-report-controls) select {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23858885' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
}
:is(.nd-imaging-controls .nd-section-content, .nd-dialog-body, .nd-report-controls) :is(select, input, button):disabled { opacity: 0.5; cursor: not-allowed; }
:is(.nd-imaging-controls .nd-section-content, .nd-report-controls) input[type="range"] { width: 100%; margin: 0; accent-color: var(--nd-color-primary); }

.nd-field { display: grid; gap: 2px; min-width: 0; }
.nd-field > :is(select, input) {
  min-width: 0;
  max-width: 100%;
  width: 100%;
  text-overflow: ellipsis;
}
.nd-field > label,
.nd-label {
  display: flex;
  align-items: center;
  gap: 4px;
  margin: 0;
  color: var(--nd-color-text);
  font-size: 0.8rem;
  font-weight: 500;
}
.nd-field > label small,
.nd-label small { color: var(--nd-color-text-muted); font-size: 0.7rem; font-weight: 400; }
.nd-hint { margin: 0; color: var(--nd-color-text-muted); font-size: 0.7rem; line-height: 1.4; }
.nd-hint strong { color: var(--nd-color-text); font-weight: 500; }
.nd-check { display: flex; align-items: center; gap: var(--nd-space-sm); color: var(--nd-color-text); font-size: 0.8rem; cursor: pointer; }
.nd-check input[type="checkbox"], .nd-check input[type="radio"] { width: 14px; height: 14px; margin: 0; cursor: pointer; accent-color: var(--nd-color-primary); }
.nd-row { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: var(--nd-space-xs); min-width: 0; }
.nd-row > * { min-width: 0; }

/* File field: one compact dashed drop zone around the native input. */
.nd-file {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--nd-space-sm);
  box-sizing: border-box;
  min-height: 40px;
  margin: 0;
  padding: var(--nd-space-sm) var(--nd-space-md);
  color: var(--nd-color-text-muted);
  background: transparent;
  border: 1px dashed var(--nd-color-border-light);
  border-radius: var(--nd-radius-sm);
  font-size: 0.75rem;
  font-weight: 400;
  line-height: 1.3;
  cursor: pointer;
  transition: border-color var(--nd-transition-fast), background var(--nd-transition-fast);
}
.nd-file:hover, .nd-file.dragover, .nd-file:focus-within { border-color: var(--nd-color-primary); background: var(--nd-color-primary-glow); }
.nd-file.dragover { border-style: solid; }
.nd-file.has-files { border-style: solid; border-color: var(--nd-color-success); background: var(--nd-color-success-bg); }
.nd-file:focus-within { outline: 2px solid var(--nd-color-primary); outline-offset: 2px; }
.nd-file > input[type="file"],
.nd-imaging-controls .nd-file > input[type="file"] { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; padding: 0; opacity: 0; cursor: pointer; }
.nd-file > svg { width: 18px; height: 18px; flex-shrink: 0; }
.nd-file:hover > svg { color: var(--nd-color-primary); }
.nd-file strong { color: var(--nd-color-primary); font-weight: 500; }
.nd-file:has(> input:disabled) { opacity: 0.5; cursor: not-allowed; }
.nd-file-info { margin: 0; color: var(--nd-color-text-muted); font-size: 0.75rem; overflow-wrap: anywhere; }
.nd-file-info strong { color: var(--nd-color-text); font-weight: 500; }

/* File picker as a button: the same label.nd-file around the native input,
   drawn as a secondary button, for slots that are drop zones themselves. */
.nd-file.nd-file-button {
  justify-content: center;
  min-height: 34px;
  color: var(--nd-color-text);
  background: var(--nd-color-surface-elevated);
  border-style: solid;
  border-radius: var(--nd-radius-md);
  font-size: 0.8rem;
  font-weight: 500;
  text-align: center;
}
.nd-file.nd-file-button:hover { border-color: var(--nd-color-border-light); background: var(--nd-color-border); }

/* Registration inputs (nd-image-pair): a fixed and a moving card, each a drop
   zone with its own load buttons, summary and clear action. */
.nd-image-pair { display: grid; gap: var(--nd-space-sm); min-width: 0; }
.nd-image-slot {
  display: grid;
  gap: var(--nd-space-xs);
  min-width: 0;
  padding: var(--nd-space-sm);
  border: 1px dashed var(--nd-color-border);
  border-radius: var(--nd-radius-md);
  transition: border-color var(--nd-transition-fast), background var(--nd-transition-fast);
}
.nd-image-slot.dragover { border-style: solid; border-color: var(--nd-color-primary); background: var(--nd-color-primary-glow); }
.nd-image-slot.has-files { border-style: solid; border-color: var(--nd-color-success); }
.nd-image-slot.locked { border-style: solid; }
.nd-image-slot-header { display: flex; align-items: center; gap: var(--nd-space-xs); min-height: 28px; }
.nd-image-slot-title { color: var(--nd-color-text); font-size: 0.8rem; font-weight: 600; }
.nd-image-slot-header .nd-image-slot-clear { min-height: 28px; margin: 0 0 0 auto; padding: 0; border: 0; background: transparent; font-size: 1rem; }
.nd-image-slot-header .nd-image-slot-clear[hidden] { display: none; }
.nd-image-slot-actions { grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); grid-auto-flow: row; }

/* --------------------------------------------------------------------------
   4. Buttons (QSMbly .btn family)
   -------------------------------------------------------------------------- */
.nd-btn,
.nd-view-tab,
.nd-tool-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--nd-space-sm);
  border: 0;
  border-radius: var(--nd-radius-sm);
  cursor: pointer;
  transition: background var(--nd-transition-fast), color var(--nd-transition-fast), opacity var(--nd-transition-fast);
}

.nd-btn { margin: 0; padding: var(--nd-space-sm) var(--nd-space-lg); border-radius: var(--nd-radius-sm); color: var(--nd-color-text); background: var(--nd-color-surface-elevated); font: inherit; font-size: 0.875rem; font-weight: 500; line-height: 1.2; }
.nd-btn svg { width: 16px; height: 16px; flex-shrink: 0; }
.nd-btn-sm { padding: var(--nd-space-xs) var(--nd-space-sm); font-size: 0.75rem; }
.nd-btn-sm svg { width: 14px; height: 14px; }
.nd-btn-primary { background: var(--nd-color-primary); color: var(--nd-color-on-primary); padding: var(--nd-space-sm) var(--nd-space-md); }
.nd-btn-primary:hover:not(:disabled) { background: var(--nd-color-primary-hover); }
.nd-btn-secondary { background: var(--nd-color-surface); color: var(--nd-color-text); border: 1px solid var(--nd-color-border-light); }
.nd-btn-secondary:hover:not(:disabled) { background: var(--nd-color-surface-elevated); border-color: var(--nd-color-border-light); }
.nd-btn-danger { background: var(--nd-color-error); color: white; }
.nd-btn-danger:hover:not(:disabled) { filter: brightness(0.9); }
.nd-btn-icon { padding: 2px; background: transparent; color: var(--nd-color-text-muted); border-radius: var(--nd-radius-sm); }
.nd-btn-icon-sm {
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
  min-width: 28px;
  min-height: 28px;
}
#controls .nd-result-selected {
  color: var(--nd-color-text);
  background: var(--nd-color-surface-elevated);
  border-color: var(--nd-color-primary);
  box-shadow: 0 0 0 2px var(--nd-color-primary);
}
.nd-result-swatch {
  flex: 0 0 auto;
  width: 11px;
  height: 11px;
  border: 1px solid var(--nd-color-border);
  border-radius: 50%;
}
.nd-btn-icon:hover:not(:disabled) { color: var(--nd-color-primary); background: var(--nd-color-surface-elevated); }
.nd-btn-block { width: 100%; font-size: 0.75rem; gap: var(--nd-space-xs); }
.nd-btn:disabled { opacity: 0.45; cursor: not-allowed; box-shadow: none; }

/* Inside a section every button is a full-width block, as in QSMbly. */
.nd-imaging-controls .nd-section-content .nd-btn,
.nd-imaging-controls .nd-section-content button:not(.nd-btn):not(.nd-btn-icon):not(.nd-view-btn):not(.nd-download-btn):not(.nd-edit-btn) {
  width: 100%;
  min-height: 32px;
  margin: 0;
  font-size: 0.8rem;
}
.nd-imaging-controls .nd-section-content .nd-btn-sm { min-height: 28px; font-size: 0.75rem; }
.nd-imaging-controls .nd-section-content button.nd-btn:hover:not(:disabled) { border-color: transparent; }
.nd-imaging-controls .nd-section-content button.nd-btn-secondary:hover:not(:disabled) { border-color: var(--nd-color-border-light); }
.nd-imaging-controls .nd-section-content .nd-btn-secondary { border-color: var(--nd-color-border-light); }
.nd-imaging-controls .nd-section-content .nd-btn-primary { border: 1px solid var(--nd-color-primary); background: var(--nd-color-primary); color: var(--nd-color-on-primary); font-weight: 600; }
.nd-imaging-controls .nd-section-content .nd-btn-primary:hover:not(:disabled) { border-color: var(--nd-color-primary-hover); background: var(--nd-color-primary-hover); }

/* --------------------------------------------------------------------------
   5. Messages and inline help
   -------------------------------------------------------------------------- */
.nd-message { display: flex; align-items: flex-start; gap: var(--nd-space-sm); margin: 0; padding: var(--nd-space-sm) var(--nd-space-md); border-radius: var(--nd-radius-sm); font-size: 0.75rem; line-height: 1.4; color: var(--nd-color-text-muted); background: var(--nd-color-surface-elevated); }
.nd-message.error { color: var(--nd-color-error); background: var(--nd-color-error-bg); }
.nd-message.warning { color: var(--nd-color-warning); background: var(--nd-color-warning-bg); }
.nd-message.success { color: var(--nd-color-success); background: var(--nd-color-success-bg); }
.nd-message.info { color: var(--nd-color-primary); background: var(--nd-color-primary-glow); }
.nd-message svg { width: 14px; height: 14px; flex-shrink: 0; margin-top: 1px; }

.nd-info-icon {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  flex-shrink: 0;
  border: 1px solid var(--nd-color-text-dim);
  border-radius: 50%;
  color: var(--nd-color-text-dim);
  font-size: 9px;
  font-weight: 700;
  font-style: normal;
  line-height: 1;
  cursor: help;
}
.nd-info-icon:focus-visible { outline: 2px solid var(--nd-color-primary); outline-offset: 2px; }
.nd-info-tooltip {
  position: fixed;
  z-index: 10000;
  width: min(220px, calc(100vw - 16px));
  padding: 6px 10px;
  color: var(--nd-color-text);
  background: var(--nd-color-surface-elevated);
  border: 1px solid var(--nd-color-border-light);
  border-radius: var(--nd-radius-sm);
  box-shadow: var(--nd-shadow-md);
  font-size: 0.75rem;
  font-weight: 400;
  line-height: 1.4;
  text-align: left;
  text-transform: none;
  letter-spacing: 0;
  white-space: normal;
  pointer-events: none;
}
.nd-info-tooltip[hidden] { display: none; }

/* --------------------------------------------------------------------------
   6. Viewer chrome (QSMbly toolbar: 34px, 0.75rem tabs, mono info bar)
   -------------------------------------------------------------------------- */
.nd-viewer-toolbar, .nd-mask-editor { display: flex; align-items: center; gap: var(--nd-space-md); min-height: 34px; padding: var(--nd-space-xs) var(--nd-space-md); background: var(--nd-color-surface); border-bottom: 1px solid var(--nd-color-border); flex-wrap: wrap; flex-shrink: 0; }
.nd-view-tabs, .nd-viewer-actions { display: flex; align-items: center; gap: var(--nd-space-sm); flex-wrap: wrap; }
.nd-view-tabs { gap: 2px; }
.nd-tool-group { display: flex; align-items: center; gap: 2px; }
.nd-viewer-actions { margin-left: auto; }
.nd-view-tab, .nd-tool-btn { padding: var(--nd-space-xs) var(--nd-space-sm); color: var(--nd-color-text-muted); background: transparent; font: inherit; font-size: 0.75rem; font-weight: 500; }
.nd-view-tab:hover:not(:disabled), .nd-tool-btn:hover:not(:disabled) { color: var(--nd-color-text); background: var(--nd-color-surface-elevated); }
.nd-view-tab.active, .nd-view-tab[aria-selected="true"], .nd-tool-btn.active, .nd-tool-btn[aria-pressed="true"] { color: var(--nd-color-primary); background: var(--nd-color-primary-glow); }
.nd-view-tab:disabled, .nd-tool-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.nd-viewer-toolbar label, .nd-mask-editor label, .nd-viewer-label { color: var(--nd-color-text-muted); font-size: 0.7rem; }
.nd-viewer-toolbar select, .nd-mask-editor select, .nd-colormap-select { width: auto; min-height: 26px; margin: 0; padding: var(--nd-space-xs) var(--nd-space-sm); color: var(--nd-color-text); background: var(--nd-color-surface-elevated); border: 1px solid var(--nd-color-border); border-radius: var(--nd-radius-sm); font: inherit; font-size: 0.75rem; }
.nd-viewer-toolbar .nd-btn-sm, .nd-mask-editor .nd-btn-sm { min-height: 26px; }
.nd-window-controls { display: flex; align-items: center; gap: 6px; }
.nd-window-input { width: 80px; padding: 3px; color: var(--nd-color-text); background: var(--nd-color-surface); border: 1px solid var(--nd-color-border); border-radius: var(--nd-radius-sm); font-size: 0.7rem; text-align: center; }
.nd-range-slider-container { position: relative; width: 135px; height: 20px; }
.nd-range-track, .nd-range-selected { position: absolute; top: 8px; height: 4px; border-radius: 2px; pointer-events: none; }
.nd-range-track { width: 100%; background: var(--nd-color-border); }
.nd-range-selected { background: var(--nd-color-primary); }
.nd-range-slider { position: absolute; width: 100%; height: 20px; margin: 0; background: transparent; pointer-events: none; appearance: none; }
.nd-range-slider::-webkit-slider-thumb { pointer-events: auto; appearance: none; width: 14px; height: 14px; border-radius: 50%; background: var(--nd-color-primary); border: 2px solid white; }
.nd-opacity-control, .nd-brush-control { display: flex; align-items: center; gap: var(--nd-space-xs); margin: 0; color: var(--nd-color-text-muted); font-size: 0.75rem; font-weight: 400; }
.nd-opacity-control input[type="range"], .nd-brush-control input[type="range"] { width: 80px; margin: 0; accent-color: var(--nd-color-primary); }
.nd-opacity-control output, .nd-opacity-control span:last-child, .nd-brush-control span:last-child { min-width: 3ch; text-align: right; font-variant-numeric: tabular-nums; }
.nd-viewer-checkbox { display: flex; align-items: center; gap: 4px; color: var(--nd-color-text-muted); font-size: 0.7rem; cursor: pointer; white-space: nowrap; }
.nd-viewer-checkbox input[type="checkbox"] { width: 12px; height: 12px; margin: 0; cursor: pointer; accent-color: var(--nd-color-primary); }

.nd-viewer-canvas-wrapper { flex: 1; position: relative; min-height: 0; background: #000; }
.nd-viewer-canvas-wrapper canvas { position: absolute; inset: 0; width: 100% !important; height: 100% !important; }
.nd-viewer-panel-grid { position: absolute; inset: 0; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1px; overflow: hidden; background: var(--nd-color-border); }
.nd-viewer-panel { position: relative; min-width: 0; min-height: 0; overflow: hidden; background: #000; }
.nd-viewer-panel-title { position: absolute; top: var(--nd-space-sm); left: var(--nd-space-sm); z-index: 1; padding: 2px var(--nd-space-xs); color: var(--nd-color-text); background: var(--nd-color-surface); border: 1px solid var(--nd-color-border); border-radius: var(--nd-radius-sm); font-size: 0.7rem; font-weight: 600; pointer-events: none; }
/* Session comparison: one canvas per loaded image, side by side. The title is the
   button that makes its image the active one; aria-current marks that panel. */
.nd-compare-grid { position: absolute; inset: 0; z-index: 3; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); grid-auto-rows: minmax(0, 1fr); gap: 1px; overflow: hidden; background: var(--nd-color-border); }
.nd-compare-grid[hidden] { display: none; }
.nd-compare-grid[data-count="1"] { grid-template-columns: minmax(0, 1fr); }
.nd-compare-panel { position: relative; min-width: 0; min-height: 0; overflow: hidden; background: #000; }
.nd-compare-panel::after { content: ""; position: absolute; inset: 0; border: 2px solid transparent; pointer-events: none; }
.nd-compare-panel[aria-current="true"]::after { border-color: var(--nd-color-primary); }
.nd-compare-panel canvas { position: absolute; inset: 0; width: 100% !important; height: 100% !important; }
.nd-compare-title { position: absolute; top: var(--nd-space-sm); left: var(--nd-space-sm); z-index: 1; max-width: calc(100% - 2 * var(--nd-space-sm)); padding: 2px var(--nd-space-xs); overflow: hidden; color: var(--nd-color-text); background: var(--nd-color-surface); border: 1px solid var(--nd-color-border); border-radius: var(--nd-radius-sm); font-size: 0.7rem; font-weight: 600; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.nd-compare-title:hover { border-color: var(--nd-color-border-light); }
.nd-compare-title[aria-pressed="true"] { color: var(--nd-color-primary); border-color: var(--nd-color-primary); }
.nd-compare-title:focus-visible { outline: 2px solid var(--nd-color-primary); outline-offset: 1px; }
@media (pointer: coarse) { .nd-compare-title { min-height: 44px; } }
.nd-viewer-empty { position: absolute; inset: 0; display: grid; place-items: center; margin: 0; padding: var(--nd-space-lg); color: var(--nd-color-text-dim); font-size: 0.85rem; text-align: center; pointer-events: none; }
.nd-viewer-empty[hidden] { display: none; }
.nd-viewer-notice { position: absolute; left: var(--nd-space-md); right: var(--nd-space-md); bottom: var(--nd-space-md); margin: 0; padding: var(--nd-space-sm) var(--nd-space-md); color: var(--nd-color-text); background: var(--nd-color-surface-elevated); border: 1px solid var(--nd-color-border); border-radius: var(--nd-radius-sm); font-size: 0.75rem; }
.nd-viewer-notice[hidden] { display: none; }
.nd-viewer-info { display: flex; justify-content: space-between; gap: var(--nd-space-sm); min-height: 24px; padding: var(--nd-space-xs) var(--nd-space-md); background: var(--nd-color-surface); border-top: 1px solid var(--nd-color-border); color: var(--nd-color-text-muted); font: 0.7rem/1.5 "SF Mono", Consolas, monospace; flex-shrink: 0; }
.nd-viewer-info-primary { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nd-viewer-info-label { flex-shrink: 0; color: var(--nd-color-text-dim); }

/* --------------------------------------------------------------------------
   7. Technical log (QSMbly console: 120px, collapsed by default)
   -------------------------------------------------------------------------- */
.nd-console-container { height: 120px; flex-shrink: 0; display: flex; flex-direction: column; background: var(--nd-color-surface); border-top: 1px solid var(--nd-color-border); }
.nd-console-container.collapsed { height: auto; }
.nd-console-header { display: flex; align-items: center; justify-content: space-between; gap: var(--nd-space-sm); min-height: 28px; padding: var(--nd-space-xs) var(--nd-space-md); border-bottom: 1px solid var(--nd-color-border); flex-shrink: 0; }
.nd-console-container.collapsed > .nd-console-header { border-bottom: 0; }
.nd-console-title { margin: 0; padding: 0; color: var(--nd-color-text-muted); background: none; border: 0; font: inherit; font-size: 0.75rem; font-weight: 600; cursor: pointer; }
/* Outranks the generic [data-disclosure-toggle] reset, which base.css applies again when an app also loads inference-workspace.css. */
.nd-console-header > .nd-console-title { color: var(--nd-color-text-muted); font-size: 0.75rem; font-weight: 600; }
.nd-console-actions { display: flex; gap: var(--nd-space-sm); margin-left: auto; }
.nd-console-clear { padding: 0 var(--nd-space-xs); color: var(--nd-color-text-dim); background: none; border: 0; font: inherit; font-size: 0.7rem; cursor: pointer; transition: color var(--nd-transition-fast); }
.nd-console-clear:hover { color: var(--nd-color-text-muted); }
.nd-console-output { flex: 1; min-height: 0; margin: 0; overflow-y: auto; padding: var(--nd-space-xs) var(--nd-space-md); color: var(--nd-color-text-muted); font: 0.7rem/1.5 "SF Mono", Consolas, monospace; white-space: pre-wrap; overflow-wrap: anywhere; }
.nd-console-line { display: flex; gap: var(--nd-space-md); }
.nd-console-time { color: var(--nd-color-text-dim); flex-shrink: 0; }
.nd-console-message { color: var(--nd-color-text-muted); }
.nd-console-success .nd-console-message { color: var(--nd-color-success); }
.nd-console-error .nd-console-message { color: var(--nd-color-error); }
.nd-console-warning .nd-console-message { color: var(--nd-color-warning); }

/* Resizable log (bindConsoleResize / createConsole({ resizable })): the user's height is kept in
   --nd-console-height, so it survives collapse; the viewer above keeps the rest of the column. */
:is(.console-container, .nd-console-container)[data-console-resizable] { position: relative; height: var(--nd-console-height, 120px); min-height: 120px; max-height: max(120px, calc(100% - 200px)); }
:is(.console-container, .nd-console-container)[data-console-resizable].collapsed { height: auto; min-height: 0; }
.nd-console-resizer { position: absolute; top: -4px; left: 0; right: 0; z-index: 2; height: 8px; cursor: ns-resize; touch-action: none; }
.nd-console-resizer::after { content: ''; position: absolute; top: 3px; left: 50%; width: 32px; height: 2px; border-radius: 1px; background: var(--nd-color-border-light); transform: translateX(-50%); transition: background var(--nd-transition-fast); }
.nd-console-resizer:is(:hover, .dragging)::after { background: var(--nd-color-primary); }
.nd-console-resizer:focus-visible { outline: none; }
.nd-console-resizer:focus-visible::after { width: 64px; background: var(--nd-color-primary); }
.collapsed > .nd-console-resizer { display: none; }

/* Several logs in one console (createConsole({ channels })): tabs beside the title, one panel each. */
.nd-console-tabs { display: flex; align-items: center; gap: 2px; margin-right: auto; }
.nd-console-header:has(> .nd-console-tabs) > .nd-console-title { flex: 0 0 auto; }
.nd-console-tab { padding: 0 var(--nd-space-sm); color: var(--nd-color-text-dim); background: none; border: 0; border-radius: var(--nd-radius-sm); font: inherit; font-size: 0.7rem; cursor: pointer; transition: color var(--nd-transition-fast), background var(--nd-transition-fast); }
.nd-console-tab:hover { color: var(--nd-color-text-muted); }
.nd-console-tab[aria-selected="true"] { color: var(--nd-color-primary); background: var(--nd-color-primary-glow); }
.nd-console-tab:focus-visible { outline: 2px solid var(--nd-color-primary); outline-offset: 1px; }
.nd-console-panels { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* --------------------------------------------------------------------------
   8. Status bar (QSMbly sidebar status: label, value, cancel, 4px bar)
   -------------------------------------------------------------------------- */
.nd-imaging-status:has(> .nd-status-text, > .nd-status-label) { display: flex; align-items: center; gap: var(--nd-space-md); min-height: 28px; padding: 2px var(--nd-space-md); font-size: 0.75rem; }
.nd-status-label { color: var(--nd-color-text-dim); font-size: 0.7rem; font-weight: 500; }
.nd-status-text { flex: 1; min-width: 0; color: var(--nd-color-text-muted); font-size: 0.75rem; font-weight: 400; overflow-wrap: anywhere; }
.nd-status-text.error { color: var(--nd-color-error); }
.nd-status-elapsed { color: var(--nd-color-text-dim); font-size: 0.7rem; font-variant-numeric: tabular-nums; }
.nd-imaging-status progress { width: 160px; max-width: 30vw; height: 4px; margin: 0; border: 0; border-radius: 2px; overflow: hidden; background: var(--nd-color-border); appearance: none; -webkit-appearance: none; accent-color: var(--nd-color-primary); }
.nd-imaging-status progress::-webkit-progress-bar { background: var(--nd-color-border); border-radius: 2px; }
.nd-imaging-status progress::-webkit-progress-value { background: var(--nd-color-primary); border-radius: 2px; transition: width var(--nd-transition-normal); }
.nd-imaging-status progress::-moz-progress-bar { background: var(--nd-color-primary); border-radius: 2px; }
.nd-imaging-status .nd-btn-cancel { display: inline-flex; align-items: center; justify-content: center; width: 20px; height: 20px; margin: 0; padding: 0; color: var(--nd-color-error); background: transparent; border: 1px solid var(--nd-color-error); border-radius: var(--nd-radius-sm); font-size: 0.8rem; line-height: 1; cursor: pointer; }
.nd-imaging-status .nd-btn-cancel:hover { color: white; background: var(--nd-color-error); }
.nd-imaging-status .nd-btn-cancel[hidden] { display: none; }

/* --------------------------------------------------------------------------
   9. Dialogs (QSMbly modal geometry: centered, bounded, header + close)
   -------------------------------------------------------------------------- */
dialog.nd-dialog { box-sizing: border-box; width: min(560px, calc(100vw - 32px)); max-width: none; max-height: min(85vh, 85dvh); margin: auto; padding: 0; overflow: hidden; color: var(--nd-color-text); background: var(--nd-color-surface); border: 1px solid var(--nd-color-border); border-radius: var(--nd-radius-lg); box-shadow: var(--nd-shadow-lg); font-family: inherit; }
dialog.nd-dialog.nd-dialog-wide { width: min(720px, calc(100vw - 32px)); }
dialog.nd-dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
.nd-dialog-header { display: flex; align-items: center; justify-content: space-between; gap: var(--nd-space-md); padding: var(--nd-space-md) var(--nd-space-lg); border-bottom: 1px solid var(--nd-color-border); }
.nd-dialog-header h2 { margin: 0; font-size: 1rem; font-weight: 600; }
.nd-dialog-close { display: inline-flex; align-items: center; justify-content: center; min-width: 44px; min-height: 44px; margin: -12px -16px -12px 0; padding: 0; color: var(--nd-color-text-muted); background: none; border: 0; font-size: 1.5rem; line-height: 1; cursor: pointer; }
.nd-dialog-close:hover { color: var(--nd-color-text); }
.nd-dialog-body { max-height: calc(min(85vh, 85dvh) - 60px); overflow-y: auto; padding: var(--nd-space-lg); font-size: 0.875rem; line-height: 1.55; }
.nd-dialog-body > :first-child { margin-top: 0; }
.nd-dialog-body > :last-child { margin-bottom: 0; }
.nd-dialog-body > .nd-field { margin-bottom: var(--nd-space-sm); }
.nd-dialog-body p { margin: 0 0 var(--nd-space-sm); }
.nd-dialog-body h3 { margin: var(--nd-space-lg) 0 var(--nd-space-xs); padding-bottom: var(--nd-space-xs); border-bottom: 1px solid var(--nd-color-border); color: var(--nd-color-primary); font-size: 0.9rem; font-weight: 600; }
.nd-dialog-body h3:first-child { margin-top: 0; }
.nd-dialog-section {
  padding: var(--nd-space-lg);
  border: 1px solid var(--nd-color-border);
  border-radius: var(--nd-radius-sm);
  background: var(--nd-color-bg);
}
.nd-dialog-section + .nd-dialog-section {
  margin-top: var(--nd-space-lg);
}
.nd-dialog-section h4 {
  margin: var(--nd-space-sm) 0 var(--nd-space-xs);
  font-size: 0.875rem;
}
.nd-download-option + .nd-download-option {
  margin-top: var(--nd-space-md);
  padding-top: var(--nd-space-sm);
  border-top: 1px solid var(--nd-color-border);
}
.nd-download-option summary {
  cursor: pointer;
  padding: var(--nd-space-sm) 0;
  min-height: 44px;
}
.nd-dialog-body a { color: var(--nd-color-primary); overflow-wrap: anywhere; }
.nd-dialog-body pre { margin: 0 0 var(--nd-space-sm); padding: var(--nd-space-sm) var(--nd-space-md); overflow: auto; background: var(--nd-color-bg); border: 1px solid var(--nd-color-border); border-radius: var(--nd-radius-sm); font-size: 0.75rem; line-height: 1.6; }
.nd-dialog-body code { font-family: "SF Mono", Consolas, monospace; font-size: 0.8em; }
.nd-dialog-body ul, .nd-dialog-body ol { margin: 0 0 var(--nd-space-sm); padding-left: 1.2em; }
.nd-dialog-highlight { margin: 0 0 var(--nd-space-md); padding: var(--nd-space-md); background: var(--nd-color-surface-elevated); border: 1px solid var(--nd-color-primary); border-radius: var(--nd-radius-md); }
.nd-dialog-highlight strong { display: block; color: var(--nd-color-primary); }
.nd-dialog-highlight p { margin: 4px 0 0; color: var(--nd-color-text-muted); font-size: 0.8rem; }
.nd-citation { margin: 0 0 var(--nd-space-md); padding-left: var(--nd-space-sm); border-left: 2px solid var(--nd-color-border); }
.nd-citation strong { display: block; margin-bottom: 2px; font-size: 0.85rem; }
.nd-citation p { margin: 0 0 4px; color: var(--nd-color-text-muted); font-size: 0.8rem; }
.nd-citation small { display: block; margin-bottom: 4px; color: var(--nd-color-text-muted); font-size: 0.72rem; }
.nd-citation a { font-size: 0.78rem; text-decoration: none; }
.nd-citation a:hover { text-decoration: underline; }

/* Copyable terminal command. */
.nd-command { position: relative; display: block; margin: 0 0 var(--nd-space-sm); padding: var(--nd-space-sm) 64px var(--nd-space-sm) var(--nd-space-sm); background: var(--nd-color-bg); border: 1px solid var(--nd-color-border); border-radius: var(--nd-radius-sm); overflow-wrap: anywhere; }
.nd-command code { display: block; font-size: 0.75rem; line-height: 1.5; white-space: pre-wrap; }
.nd-command .nd-btn { position: absolute; top: 4px; right: 4px; min-width: 44px; min-height: 28px; padding: 2px var(--nd-space-sm); font-size: 0.7rem; }

/* --------------------------------------------------------------------------
   10. Utilities and touch sizing
   -------------------------------------------------------------------------- */
.nd-sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }
.nd-hidden, .hidden { display: none !important; }
[hidden] { display: none !important; }

/* Touch targets: coarse pointers and phone widths (narrow windows are touch far more often than not). */
@media (pointer: coarse), (max-width: 780px) {
  :is(.nd-imaging-controls .nd-section-content, .nd-dialog-body, .nd-report-controls) :is(button, select, .nd-btn, .nd-file, input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"])) { min-height: 44px; }
  :is(.nd-imaging-controls .nd-section-content, .nd-dialog-body, .nd-report-controls) :is(select, textarea, input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"])) { font-size: 16px; }
  :is(.nd-viewer-toolbar, .nd-mask-editor) :is(button, select, .nd-view-tab) { min-height: 44px; min-width: 44px; }
  :is(.nd-viewer-toolbar, .nd-mask-editor) select { font-size: 16px; }
  .nd-btn-icon-sm { min-width: 44px; min-height: 44px; }
  .nd-btn-sm,
  .nd-viewer-checkbox {
    min-width: 44px;
    min-height: 44px;
  }
  .nd-console-title, .nd-console-clear, .nd-console-tab { min-height: 44px; }
  .nd-console-header :is(.nd-console-title, .nd-console-clear, .nd-console-tab) { min-width: 44px; }
  /* The 44px grab area extends up over the info bar so it never covers the header buttons. */
  .nd-console-resizer { top: -36px; height: 44px; }
  .nd-console-resizer::after { top: 35px; }
  .nd-dialog-body .nd-btn, .nd-command .nd-btn { min-height: 44px; }
  .nd-imaging-status .nd-btn-cancel { width: 44px; height: 44px; }
}

@media (max-width: 780px) {
  .nd-imaging-workspace {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto minmax(0, 1fr) auto;
  }

  .nd-imaging-controls {
    grid-column: 1;
    grid-row: 2;
    max-height: 42vh;
    border-right: 0;
    border-bottom: 1px solid var(--nd-color-border);
  }

  .nd-imaging-viewer { grid-column: 1; grid-row: 3; }
  .nd-imaging-status { grid-column: 1; grid-row: 4; flex-wrap: wrap; }
  .nd-imaging-status progress { width: 100%; max-width: none; order: 10; }
  .nd-viewer-toolbar, .nd-mask-editor { gap: var(--nd-space-xs); padding: var(--nd-space-xs) var(--nd-space-sm); }
  /* Three NiiVue panels need more height than a phone's share of 100vh, and the canvases
     swallow touch gestures, so the page scrolls instead of the grid: square panels stacked
     in portrait, side by side in landscape; the status bar stays pinned. The :has() chains
     outrank site/app-theme.css's generic phone rows and viewer minimum heights. */
  .nd-imaging-workspace:has(.nd-viewer-canvas-wrapper > .nd-viewer-panel-grid) { grid-template-rows: auto auto auto auto; height: auto; overflow: visible; }
  .nd-imaging-workspace:has(.nd-viewer-canvas-wrapper > .nd-viewer-panel-grid) .nd-imaging-viewer { min-height: 0; }
  .nd-imaging-workspace:has(.nd-viewer-canvas-wrapper > .nd-viewer-panel-grid) .nd-imaging-status { position: sticky; bottom: 0; }
  .nd-imaging-viewer:has(> .nd-viewer-canvas-wrapper > .nd-viewer-panel-grid) .nd-viewer-canvas-wrapper { flex: none; min-height: 0; height: calc(3 * min(100vw, 360px)); }
  .nd-viewer-panel-grid { grid-template-columns: 1fr; grid-template-rows: repeat(3, minmax(0, 1fr)); }
}

@media (max-width: 780px) and (orientation: landscape) {
  .nd-imaging-viewer:has(> .nd-viewer-canvas-wrapper > .nd-viewer-panel-grid) .nd-viewer-canvas-wrapper { height: calc(100vw / 3); }
  .nd-viewer-panel-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); grid-template-rows: 1fr; }
}

/* --------------------------------------------------------------------------
   11. Legacy static layout (.nd-app-container family). Retained for apps that
   render the whole shell themselves; new apps use mountImagingWorkspace.
   -------------------------------------------------------------------------- */
.nd-app-container {
  display: grid;
  grid-template-columns: 320px 1fr;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
  max-height: 100vh;
  overflow: hidden;
  background: var(--nd-color-bg);
  color: var(--nd-color-text);
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.nd-app-header,
.nd-viewer-toolbar,
.nd-viewer-info,
.nd-console-container {
  background: var(--nd-color-surface);
  border-color: var(--nd-color-border);
}

.nd-app-header {
  grid-column: 1 / -1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--nd-space-sm) var(--nd-space-lg);
  border-bottom: 1px solid var(--nd-color-border);
}

.nd-logo { display: flex; align-items: center; gap: var(--nd-space-sm); }
.nd-logo-icon { width: 28px; height: 28px; object-fit: contain; }
.nd-logo h1 { margin: 0; font-size: 1rem; font-weight: 600; }
.nd-logo h1 span { margin-left: var(--nd-space-xs); color: var(--nd-color-text-muted); font-size: 0.85rem; font-weight: 400; }
.nd-logo .nd-version { font-size: 0.7rem; opacity: 0.7; }
.nd-header-links { display: flex; gap: var(--nd-space-sm); }
.nd-header-link { color: var(--nd-color-text); background: none; border: 0; cursor: pointer; opacity: 0.82; }
.nd-header-link:hover { opacity: 1; }

.nd-app-sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
  background: var(--nd-color-surface);
  border-right: 1px solid var(--nd-color-border);
}

.nd-sidebar-scroll { flex: 1; min-height: 0; overflow-y: auto; }
.nd-sidebar-status { flex-shrink: 0; padding: var(--nd-space-sm) var(--nd-space-md); background: var(--nd-color-surface-elevated); border-top: 1px solid var(--nd-color-border); }
.nd-status-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: var(--nd-space-xs); }
.nd-status-value { color: var(--nd-color-text); font-size: 0.75rem; }
.nd-progress-bar { height: 4px; background: var(--nd-color-border); border-radius: 2px; overflow: hidden; }
.nd-progress-fill { width: 0; height: 100%; background: linear-gradient(90deg, var(--nd-color-primary), var(--nd-color-accent)); transition: width 250ms ease; }

.nd-app-main { display: flex; flex-direction: column; min-width: 0; overflow: hidden; background: var(--nd-color-bg); }
.nd-modal-overlay { position: fixed; inset: 0; z-index: 1000; display: flex; align-items: center; justify-content: center; background: rgba(0, 0, 0, 0.6); opacity: 0; pointer-events: none; transition: opacity 200ms ease; }
.nd-modal-overlay.active { opacity: 1; pointer-events: auto; }
.nd-modal { max-width: 700px; width: min(90vw, 700px); max-height: 80vh; overflow-y: auto; background: var(--nd-color-surface); border: 1px solid var(--nd-color-border); border-radius: var(--nd-radius-lg); }
.nd-modal-header { display: flex; justify-content: space-between; align-items: center; padding: var(--nd-space-lg); border-bottom: 1px solid var(--nd-color-border); }
.nd-modal-header h3 { margin: 0; font-size: 1rem; }
.nd-modal-close { color: var(--nd-color-text-muted); background: none; border: 0; font-size: 1.5rem; cursor: pointer; }
.nd-modal-body { padding: var(--nd-space-lg); }

@media (max-width: 1024px) {
  .nd-app-container { grid-template-columns: 1fr; grid-template-rows: auto auto 1fr auto; }
  .nd-app-sidebar { border-right: 0; border-bottom: 1px solid var(--nd-color-border); max-height: 50vh; }
}

/* --------------------------------------------------------------------------
   12. Result rows (nd-result-list / QSMbly stage list): View/visibility · label · Edit · Download
   -------------------------------------------------------------------------- */
.nd-volume-toggle { display: flex; align-items: center; gap: 4px; min-width: 0; }
.nd-volume-toggle .nd-stage-label { flex: 1; min-width: 0; color: var(--nd-color-text); font-size: 0.8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nd-volume-toggle:has(.nd-result-visibility):has(.nd-view-btn) .nd-stage-label,
.nd-volume-toggle:has(.nd-edit-btn) .nd-stage-label {
  white-space: normal;
  overflow-wrap: anywhere;
}
.nd-result-visibility { display: inline-flex; align-items: center; justify-content: center; min-width: 52px; min-height: 28px; cursor: pointer; flex-shrink: 0; }
.nd-result-visibility input { width: 16px; height: 16px; margin: 0; cursor: pointer; accent-color: var(--nd-color-primary); }
.nd-imaging-controls .nd-section-content .nd-volume-toggle :is(.nd-view-btn, .nd-edit-btn, .nd-download-btn),
.nd-volume-toggle :is(.nd-view-btn, .nd-edit-btn, .nd-download-btn),
.nd-edit-btn { width: auto; min-height: 28px; margin: 0; padding: var(--nd-space-xs) var(--nd-space-sm); color: var(--nd-color-text); background: var(--nd-color-surface-elevated); border: 1px solid var(--nd-color-border); border-radius: var(--nd-radius-sm); font: inherit; font-size: 0.75rem; font-weight: 500; cursor: pointer; flex-shrink: 0; }
.nd-volume-toggle .nd-view-btn { min-width: 52px; }
/* Repeats the sidebar scope of the base row-button rule, which would otherwise outrank it. */
.nd-volume-toggle .nd-view-btn.active,
.nd-imaging-controls .nd-section-content .nd-volume-toggle .nd-view-btn.active { color: var(--nd-color-on-primary); background: var(--nd-color-primary); border-color: var(--nd-color-primary); }
.nd-volume-toggle :is(.nd-view-btn, .nd-edit-btn, .nd-download-btn):hover:not(:disabled),
.nd-edit-btn:hover:not(:disabled) { border-color: var(--nd-color-border-light); background: var(--nd-color-border); }
.nd-volume-toggle .nd-view-btn.active:hover:not(:disabled),
.nd-imaging-controls .nd-section-content .nd-volume-toggle .nd-view-btn.active:hover:not(:disabled) { background: var(--nd-color-primary-hover); border-color: var(--nd-color-primary-hover); }
.nd-volume-toggle :is(.nd-view-btn, .nd-edit-btn, .nd-download-btn):disabled,
.nd-edit-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.nd-empty-state { margin: 0; color: var(--nd-color-text-dim); font-size: 0.75rem; }
@media (pointer: coarse) { .nd-volume-toggle :is(.nd-view-btn, .nd-edit-btn, .nd-download-btn, .nd-result-visibility), .nd-edit-btn { min-height: 44px; } }

/* Light-DOM component hosts retain the block layout of their former containers. */
nd-console,
nd-viewer-toolbar,
nd-file-field,
nd-result-list {
  display: block;
}

/* Raster viewers preserve voxel-plane aspect ratio inside the shared panels. */
.nd-viewer-panel canvas.nd-raster-slice {
  object-fit: contain;
  image-rendering: pixelated;
}
.nd-slice-controls {
  display: flex;
  flex-wrap: wrap;
  gap: var(--nd-space-sm);
  padding: var(--nd-space-sm);
  background: var(--nd-color-surface);
}
.nd-slice-controls label {
  display: flex;
  flex: 1 1 180px;
  align-items: center;
  gap: var(--nd-space-xs);
  min-width: 0;
  color: var(--nd-color-text-muted);
  font-size: 0.75rem;
}
.nd-slice-controls input {
  flex: 1;
  min-width: 0;
  accent-color: var(--nd-color-primary);
}
.nd-table-scroll {
  min-width: 0;
  max-width: 100%;
  overflow-x: auto;
}
.nd-data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.75rem;
}
.nd-data-table :is(th, td) {
  padding: var(--nd-space-xs);
  text-align: left;
  border-bottom: 1px solid var(--nd-color-border);
  overflow-wrap: anywhere;
}
.nd-data-table [role="button"] {
  cursor: pointer;
  color: var(--nd-color-primary);
}
.nd-imaging-workspace [hidden] {
  display: none !important;
}
@media (pointer: coarse) {
  .nd-slice-controls input { min-height: 44px; }
}

.nd-btn-secondary[aria-pressed="true"] {
  border-color: var(--nd-color-primary);
  background: var(--nd-color-primary);
  color: var(--nd-color-bg);
}

@media (max-width: 1024px) {
  .nd-imaging-workspace:has(.nd-viewer-canvas-wrapper > .nd-viewer-panel-grid .nd-raster-slice) {
    padding-bottom: var(--nd-status-height, 4rem);
  }
  .nd-imaging-workspace:has(.nd-viewer-canvas-wrapper > .nd-viewer-panel-grid .nd-raster-slice) .nd-imaging-status {
    position: fixed;
    inset-inline: 0;
    bottom: 0;
    z-index: 10;
  }
}

.nd-data-table select { min-width: 96px; }
.nd-data-table th { white-space: nowrap; }

/* A report shown in place of the canvas, e.g. a volume table with charts. */
.nd-viewer-report {
  position: absolute;
  inset: 0;
  overflow: auto;
  padding: var(--nd-space-md);
  background: var(--nd-color-bg);
  color: var(--nd-color-text);
  font-size: 0.8rem;
}
.nd-viewer-report[hidden] { display: none; }
.nd-viewer-report p { margin: 0 0 var(--nd-space-xs); }
.nd-report-lead { font-weight: 600; }
.nd-report-notes { margin-top: var(--nd-space-md); color: var(--nd-color-text-muted); font-size: 0.7rem; }
/* A report's own controls (filter, instance, source): one wrapping row that stays above the
   scrolled table. Fields take the sidebar's styling through the shared field rules. */
.nd-report-controls { position: sticky; top: calc(-1 * var(--nd-space-md)); z-index: 1; display: flex; flex-wrap: wrap; align-items: flex-end; gap: var(--nd-space-sm) var(--nd-space-md); margin: calc(-1 * var(--nd-space-md)) calc(-1 * var(--nd-space-md)) 0; padding: var(--nd-space-sm) var(--nd-space-md); background: var(--nd-color-surface); border-bottom: 1px solid var(--nd-color-border); }
.nd-report-controls > .nd-field { flex: 1 1 180px; }
.nd-report-controls > :is(.nd-check, .nd-btn) { flex: 0 0 auto; min-height: 30px; }
.nd-viewer-report .nd-report-controls + .nd-report-notes { margin: var(--nd-space-sm) 0; }
.nd-viewer-report .nd-data-table td:first-child { font-family: "SF Mono", Consolas, monospace; white-space: nowrap; }
.nd-viewer-report .nd-data-table caption { padding: var(--nd-space-md) 0 var(--nd-space-xs); color: var(--nd-color-text); font-weight: 600; text-align: left; }

/* dicompare compliance reports (DicompareReportRenderer), in a report or a dialog. */
.dicompare-schema-header h4, .dicompare-acquisition h4 { margin: var(--nd-space-md) 0 var(--nd-space-xs); color: var(--nd-color-text); font-size: 0.85rem; }
.dicompare-schema-header p, .dicompare-empty, .dicompare-error { margin: 0 0 var(--nd-space-xs); color: var(--nd-color-text-muted); font-size: 0.75rem; }
.dicompare-error { color: var(--nd-color-error); }
.dicompare-schema-version, .dicompare-field-tag, .dicompare-status-message, .dicompare-rule-description { color: var(--nd-color-text-dim); font-size: 0.7rem; font-weight: 400; }
.dicompare-summary { display: flex; flex-wrap: wrap; gap: var(--nd-space-xs); margin: var(--nd-space-sm) 0; }
.dicompare-summary-badge, .dicompare-status { display: inline-block; padding: 1px var(--nd-space-sm); border: 1px solid var(--nd-color-border); border-radius: var(--nd-radius-sm); color: var(--nd-color-text-muted); font-size: 0.7rem; font-weight: 600; white-space: nowrap; }
:is(.dicompare-summary-badge.pass, .dicompare-status-pass) { color: var(--nd-color-success); border-color: currentColor; }
:is(.dicompare-summary-badge.fail, .dicompare-status-fail) { color: var(--nd-color-error); border-color: currentColor; }
:is(.dicompare-summary-badge.warning, .dicompare-status-warning) { color: var(--nd-color-warning); border-color: currentColor; }
.dicompare-section-label { margin: var(--nd-space-sm) 0 var(--nd-space-xs); color: var(--nd-color-text-muted); font-size: 0.7rem; font-weight: 600; }
.dicompare-table { width: 100%; border-collapse: collapse; font-size: 0.75rem; }
.dicompare-table :is(th, td) { padding: var(--nd-space-xs); border-bottom: 1px solid var(--nd-color-border); text-align: left; vertical-align: top; overflow-wrap: anywhere; }
.dicompare-table th { color: var(--nd-color-text-muted); font-weight: 600; }
.dicompare-unchecked-toggle { display: flex; justify-content: space-between; width: 100%; margin-top: var(--nd-space-sm); padding: var(--nd-space-xs) var(--nd-space-sm); color: var(--nd-color-text-muted); background: none; border: 1px solid var(--nd-color-border); border-radius: var(--nd-radius-sm); font: inherit; font-size: 0.75rem; cursor: pointer; }
.dicompare-unchecked-toggle:focus-visible { outline: 2px solid var(--nd-color-primary); outline-offset: 1px; }
.nd-data-table [data-flag] td { font-weight: 600; }
.nd-data-table .nd-numeric { text-align: right; font-variant-numeric: tabular-nums; }
/* A report made of stacked sections, each a heading and its table. */
.nd-report-stack { display: flex; flex-direction: column; gap: var(--nd-space-lg); max-width: 760px; }
.nd-report-stack section { display: flex; flex-direction: column; gap: var(--nd-space-sm); }
.nd-report-stack section section { margin-top: var(--nd-space-md); }
.nd-report-heading { display: flex; align-items: center; gap: var(--nd-space-xs); }
.nd-report-heading :is(h2, h3) { flex: 1; min-width: 0; margin: 0; color: var(--nd-color-text-muted); font-size: 0.75rem; font-weight: 600; letter-spacing: 0.05em; text-transform: uppercase; }
.nd-data-table caption { caption-side: top; padding-bottom: var(--nd-space-xs); color: var(--nd-color-text-dim); font-size: 0.7rem; text-align: left; }

/* Small-multiple SVG charts: recessive axes and grid, token colours only. */
.nd-chart-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: var(--nd-space-md);
  margin: var(--nd-space-md) 0;
}
.nd-chart-figure { margin: 0; min-width: 0; }
.nd-chart-figure figcaption { margin-bottom: var(--nd-space-xs); color: var(--nd-color-text); font-size: 0.75rem; }
.nd-chart-figure[data-flag] figcaption { font-weight: 600; }
.nd-chart { display: block; width: 100%; height: auto; overflow: visible; }
.nd-chart text { fill: var(--nd-color-text-muted); font-size: 9px; }
.nd-chart .nd-chart-axis { stroke: var(--nd-color-border); }
.nd-chart .nd-chart-gridline { stroke: var(--nd-color-border-light); stroke-dasharray: 2 3; }
.nd-chart .nd-chart-band-outer { fill: var(--nd-color-primary); fill-opacity: 0.14; }
.nd-chart .nd-chart-band-inner { fill: var(--nd-color-primary); fill-opacity: 0.3; }
.nd-chart .nd-chart-median { fill: none; stroke: var(--nd-color-primary); stroke-width: 1.5; }
.nd-chart .nd-chart-marker { fill: var(--nd-color-text); stroke: var(--nd-color-bg); stroke-width: 2; }
.nd-chart .nd-chart-level { stroke: var(--nd-color-text); stroke-width: 2; stroke-dasharray: 6 4; }
.nd-chart .nd-chart-range { fill: none; stroke: var(--nd-color-text-dim); stroke-dasharray: 1 3; }
.nd-chart .nd-chart-note { fill: var(--nd-color-text); font-weight: 600; }
.nd-chart-legend { display: flex; flex-wrap: wrap; gap: var(--nd-space-xs) var(--nd-space-md); margin: 0; padding: 0; list-style: none; color: var(--nd-color-text-muted); font-size: 0.7rem; }
.nd-chart-legend svg { width: 18px; height: 10px; margin-right: var(--nd-space-xs); vertical-align: middle; }

/* Embedded FreeBrowse 2.5: its own stylesheet stays inside an open shadow root. */
.nd-viewer-embed {
  position: absolute;
  inset: 0;
}
.nd-freebrowse {
  height: 100%;
  min-width: 0;
  container: nd-freebrowse / inline-size;
}
.nd-freebrowse .freebrowse-root,
.nd-freebrowse .freebrowse-root.dark {
  --background: var(--nd-color-bg);
  --foreground: var(--nd-color-text);
  --card: var(--nd-color-surface);
  --card-foreground: var(--nd-color-text);
  --popover: var(--nd-color-surface);
  --popover-foreground: var(--nd-color-text);
  --primary: var(--nd-color-primary);
  --primary-foreground: var(--nd-color-bg);
  --secondary: var(--nd-color-surface);
  --secondary-foreground: var(--nd-color-text);
  --muted: var(--nd-color-surface);
  --muted-foreground: var(--nd-color-text-muted);
  --accent: var(--nd-color-surface);
  --accent-foreground: var(--nd-color-text);
  --border: var(--nd-color-border);
  --input: var(--nd-color-border);
  --ring: var(--nd-color-primary);
  font-family: inherit;
}
.nd-freebrowse .freebrowse-root > div.flex {
  position: relative;
  min-height: 0;
}
.nd-freebrowse .freebrowse-root > header {
  padding: 4px 8px !important;
}
.nd-freebrowse .freebrowse-root > header h1,
.nd-freebrowse button[title="Switch to light mode"],
.nd-freebrowse button[title="Switch to dark mode"] {
  display: none !important;
}
.nd-freebrowse .freebrowse-root > header > div {
  flex-wrap: wrap !important;
  gap: 4px !important;
}
.nd-freebrowse .freebrowse-root > header > div > div {
  padding: 0 !important;
}
.nd-freebrowse .freebrowse-root > header button {
  min-width: 34px;
  min-height: 34px;
}
@container nd-freebrowse (max-width: 700px) {
  .nd-freebrowse .freebrowse-root > header > div > div {
    max-width: 100%;
  }
  .nd-freebrowse .freebrowse-root > header span:not(.sr-only) {
    font-size: 12px !important;
  }
  .nd-freebrowse .freebrowse-root > header > div > div > div {
    gap: 4px !important;
  }
  .nd-freebrowse .freebrowse-root > header [role="group"] span {
    display: none !important;
  }
  .nd-freebrowse .freebrowse-root > div > aside {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 10;
    max-width: 100%;
  }
  .nd-freebrowse .freebrowse-root > header button,
  .nd-freebrowse button {
    min-width: 44px !important;
    min-height: 44px !important;
  }
  .nd-freebrowse :is(input, select) {
    min-height: 44px !important;
    font-size: 16px !important;
  }
}
