/* app.css — styles for the model runner (app.html).
 * Loaded after tokens.css + base.css. WS7 Stage F extracted these verbatim from
 * app.html's inline <style>; the design system (tokens) is now shared, so styling
 * refinements here carry across every model the shell runs. */

/* ---- Header ----------------------------------------------------------- */
.app-header {
  display: flex; align-items: center; gap: var(--space-4); flex-wrap: wrap;
  padding: var(--space-3) var(--space-5);
  border-bottom: 1px solid var(--line); background: var(--surface);
}
.brand { display: flex; align-items: center; gap: var(--space-2); text-decoration: none; white-space: nowrap; }
.brand-name { font-size: 15px; font-weight: 700; }
.brand span { color: var(--muted); font-weight: 500; }
.header-controls { display: flex; align-items: center; gap: var(--space-3); margin-left: auto; }
.header-controls select {
  padding: 6px 9px; background: var(--bg);
  border: 1px solid var(--line); border-radius: var(--radius-sm); max-width: 340px;
}
#substrate-config { display: flex; align-items: center; gap: var(--space-2); }
.substrate-label { font-size: 12px; font-weight: 600; color: var(--muted); }
#server-config { display: flex; align-items: center; gap: var(--space-2); }
#server-config input {
  width: 190px; padding: 6px 9px; background: var(--bg);
  border: 1px solid var(--line); border-radius: var(--radius-sm);
}

/* ---- Toolbar ---------------------------------------------------------- */
.toolbar {
  display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap;
  padding: var(--space-2) var(--space-5);
  background: var(--toolbar-bg); border-bottom: 1px solid var(--line);
}
.toolbar-spacer { flex: 1; }
/* Command buttons default to the blue accent. Run is the exception (green, below);
   Cancel + Clear-data keep their semantic outline look. */
.toolbar .cmd {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 12px; border: 1px solid var(--accent); background: var(--accent);
  border-radius: var(--radius-sm); cursor: pointer; font-weight: 600; font-size: 13px;
  color: var(--accent-fg);
}
.toolbar .cmd:hover { filter: brightness(1.05); }
/* Run — the "go" action — is green to set it apart from the blue command buttons. */
.toolbar .cmd.primary { background: var(--accent-green); border-color: var(--accent-green); color: var(--accent-green-fg); }
.toolbar .cmd.primary:hover { filter: brightness(1.05); }
.toolbar .cmd:disabled { opacity: .5; cursor: default; filter: none; }
.toolbar .cmd .icon { display: block; }
.toolbar .cmd.cmd-cancel { background: var(--surface); border-color: var(--error); color: var(--error); }
.toolbar .cmd.cmd-cancel:hover { background: var(--error); color: var(--accent-fg); filter: none; }
.toolbar .cmd.cmd-clear-data { background: var(--surface); border-color: var(--muted); color: var(--muted); }
.toolbar .cmd.cmd-clear-data:hover { background: var(--hover); color: var(--fg); filter: none; }

/* Observed-data import dialog (WS7 Stage D). */
dialog.data-import { border: 1px solid var(--line); border-radius: 8px; padding: 0; background: var(--bg); color: var(--fg); width: min(440px, 92vw); box-shadow: 0 8px 32px rgba(0,0,0,.25); }
dialog.data-import::backdrop { background: rgba(0,0,0,.4); }
dialog.data-import form { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
dialog.data-import h2 { margin: 0; font-size: 16px; }
.di-file { margin: 0; color: var(--muted); font-size: 13px; }
.di-hint { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.4; }
.di-yrow-disabled { opacity: .55; }
.di-yrow-disabled .di-ycol { font-style: italic; }
.di-field { display: flex; flex-direction: column; gap: 4px; }
.di-label { font-size: 12px; font-weight: 600; color: var(--muted); }
/* Form controls must carry the theme's bg/fg — native defaults render a white
   field, so themed text (light in dark mode) is invisible on it. */
dialog.data-import select,
dialog.data-import input[type=text] {
  width: 100%; padding: 6px 9px; background: var(--bg); color: var(--fg);
  border: 1px solid var(--line); border-radius: var(--radius-sm);
}
.di-yseries { display: flex; flex-direction: column; gap: 6px; max-height: 180px; overflow-y: auto; }
.di-yrow { display: grid; grid-template-columns: auto 1fr 1.4fr; align-items: center; gap: 8px; }
.di-yrow .di-ycol { font-size: 13px; color: var(--fg); }
.di-yname { padding: 4px 6px; }
.di-msg { margin: 0; color: var(--error); font-size: 13px; min-height: 1em; }
.di-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.di-actions .cmd { padding: 6px 14px; border-radius: 6px; border: 1px solid var(--line); background: var(--bg); color: var(--fg); cursor: pointer; }
.di-actions .cmd.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.di-actions .cmd:disabled { opacity: .5; cursor: default; }
.toolbar-progress {
  font-size: 12px; font-weight: 600; color: var(--muted);
  font-variant-numeric: tabular-nums; white-space: nowrap;
}

/* ---- Status ----------------------------------------------------------- */
#status {
  padding: 6px var(--space-5); font-size: 13px; color: var(--muted);
  min-height: 30px; display: flex; align-items: center;
  border-bottom: 1px solid var(--line); background: var(--canvas);
}
#status.error { color: var(--error); }

/* ---- Workspace: sidebar + content ------------------------------------ */
.workspace { flex: 1; min-height: 0; display: grid; grid-template-columns: 340px 1fr; }
.sidebar {
  overflow-y: auto; padding: var(--space-4);
  border-right: 1px solid var(--line); background: var(--canvas);
}
.content { position: relative; display: flex; flex-direction: column; overflow: hidden; padding: var(--space-5); min-width: 0; }
@media (max-width: 820px) {
  .workspace { grid-template-columns: 1fr; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--line); }
}

/* ---- Run-in-progress overlay ----------------------------------------- */
/* A purely-visual scrim over the results area (pointer-events:none, so it never
   traps clicks — Cancel lives in the toolbar, outside this box).
   Visibility is toggled with display only (none <-> flex): it is fully shown or
   fully gone, never a partial state, so it can't be left as a faint stuck scrim.
   It appears immediately when a run starts (the app suppresses it for debounced
   live re-runs, so slider drags don't flash it). Shown via display:flex before a
   blocking wasm compute (after a paint yield), so it renders even while the main
   thread is busy; the spinner spins on the compositor. */
.busy-overlay {
  position: absolute; inset: 0; z-index: 20; pointer-events: none;
  display: none; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--canvas) 66%, transparent);
}
.busy-overlay.show { display: flex; }
.busy-card {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--surface); color: var(--fg);
  border: 1px solid var(--line); border-radius: 10px;
  box-shadow: var(--shadow-card); font-weight: 600;
}
.spinner {
  width: 20px; height: 20px; border-radius: 50%; flex: none;
  border: 3px solid color-mix(in srgb, var(--accent) 26%, transparent);
  border-top-color: var(--accent);
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Run button spinner: swap the play icon for a small spinner while running. */
.toolbar .cmd.is-running .icon { display: none; }
.toolbar .cmd.is-running::before {
  content: ""; width: 13px; height: 13px; border-radius: 50%; flex: none;
  border: 2px solid color-mix(in srgb, currentColor 35%, transparent);
  border-top-color: currentColor;
  animation: spin .7s linear infinite;
}

@media (prefers-reduced-motion: reduce) {
  /* Stop the rotational spin (motion); keep the overlay's opacity fade-in, which
     is not translational motion and still gates the no-flash reveal delay. */
  .spinner, .toolbar .cmd.is-running::before { animation: none; }
}

/* ---- Input panels (accordion) ---------------------------------------- */
/* Floating white cards on the slate canvas (Stage F "instrument/slate"). */
.input-panel {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); margin-bottom: var(--space-3); overflow: hidden;
  box-shadow: var(--shadow-card);
}
.input-panel > summary {
  cursor: pointer; list-style: none; user-select: none;
  display: flex; align-items: center; gap: var(--space-2);
  padding: 10px var(--space-4); font-size: 12px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .04em; color: var(--muted);
  background: var(--panel-header-bg);
}
/* An open panel separates its tinted header from the body with a hairline. */
.input-panel[open] > summary { border-bottom: 1px solid var(--line); }
.input-panel > summary::-webkit-details-marker { display: none; }
.input-panel > summary::before {
  content: "▸"; color: var(--muted); font-size: 17px; line-height: 1;
  width: 16px; text-align: center; flex: 0 0 auto;
  transition: transform .12s ease;
}
.input-panel[open] > summary::before { transform: rotate(90deg); }
.panel-title { flex: 1; }
.role-badge {
  font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em;
  padding: 2px 7px; border-radius: 999px;
}
.role-config { background: var(--badge-config-bg); color: var(--badge-config-fg); }
.role-presentation { background: var(--badge-view-bg); color: var(--badge-view-fg); }
.panel-body { padding: 0 var(--space-4) var(--space-4); }

/* ---- Fields (emitted by form.js) ------------------------------------- */
.field { margin-top: 14px; display: flex; flex-direction: column; gap: var(--space-1); }
.field label { font-size: 13px; font-weight: 600; }
.field label.required::after { content: " *"; color: var(--accent); }
.field small { color: var(--muted); font-size: 12px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 7px 9px; background: var(--bg);
  border: 1px solid var(--line); border-radius: var(--radius-sm);
}
.field input[type=checkbox] { width: auto; }
.field input[type=range] { padding: 0; }

/* Compound slider (widgets.js sliderControl): track + editable min/value/max. */
.slider-widget { display: flex; flex-direction: column; gap: 7px; }
.slider-widget .sw-range { width: 100%; padding: 0; }
.slider-widget .sw-bounds { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.slider-widget .sw-bounds input { padding: 3px 6px; font-size: 12px; text-align: center; }
.slider-widget .sw-min, .slider-widget .sw-max { width: 74px; color: var(--muted); }
.slider-widget .sw-value-wrap { display: flex; align-items: center; gap: 4px; }
.slider-widget .sw-value { width: 90px; font-weight: 600; }

/* ---- Saved runs panel (Stage E: run comparison) --------------------- */
.runs-count {
  margin-left: auto; font-size: 11px; background: var(--surface-2);
  padding: 1px 8px; border-radius: 999px; letter-spacing: 0; color: var(--muted);
}
.runs-body { padding-top: var(--space-3); }
.runs-save {
  width: 100%; display: inline-flex; align-items: center; justify-content: center;
  gap: var(--space-2); padding: 7px 12px; background: var(--surface-2); color: var(--fg);
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  font-weight: 600; font-size: 13px; cursor: pointer;
}
.runs-save:hover:not(:disabled) { border-color: var(--muted); }
.runs-save:disabled { opacity: .5; cursor: not-allowed; }
.runs-list { list-style: none; margin: var(--space-3) 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.run-row { display: flex; align-items: center; gap: 6px; }
.run-row .run-compare { flex: 0 0 auto; width: auto; }
.run-row .run-label {
  flex: 1 1 auto; min-width: 0; padding: 4px 7px; background: var(--bg);
  border: 1px solid var(--line); border-radius: var(--radius-sm); font-size: 13px; color: var(--fg);
}
.run-row .run-meta { flex: 0 0 auto; font-size: 11px; color: var(--muted); white-space: nowrap; }
.run-row .run-del { flex: 0 0 auto; padding: 4px; }
.runs-empty { margin: var(--space-3) 0 0; }

/* ---- Tabs (emitted by tabs.js) --------------------------------------- */
.tabs { display: flex; flex-direction: column; gap: var(--space-4); flex: 1; min-height: 0; }
/* Many tabs / a narrow viewport: the bar scrolls horizontally rather than wrapping,
   so the tablist stays a single row. */
.tab-bar { display: flex; gap: 2px; border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: thin; }
.tab {
  padding: 8px 14px; border: none; background: transparent; cursor: pointer;
  font-weight: 600; font-size: 13px; color: var(--muted);
  border-bottom: 2px solid transparent; margin-bottom: -1px;
  flex: 0 0 auto; white-space: nowrap;
}
.tab:hover { color: var(--fg); }
.tab[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }
.tab-panel { display: flex; flex-direction: column; gap: var(--space-4); flex: 1; min-height: 0; overflow-y: auto; }
.muted-note { color: var(--muted); font-size: 14px; }

/* ---- Output components (emitted by renderers.js) --------------------- */
.scalar { font-size: 16px; font-weight: 600; margin: 0; }
/* metrics-table (v1.7): a compact PK-metrics summary table, theme-aware. */
.metrics-box h3 { margin: 0 0 10px; font-size: 14px; }
.metrics-table { border-collapse: collapse; font-size: 13px; min-width: 320px; }
.metrics-table th, .metrics-table td { padding: 5px 14px 5px 0; text-align: left; border-bottom: 1px solid var(--line); }
.metrics-table thead th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.metrics-table td.num, .metrics-table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.metrics-table td.unit { color: var(--muted); }
.metrics-table tbody tr:last-child td { border-bottom: 0; }
/* Result views float as surface cards on the slate content canvas (Stage F).
   Scoped to .content so the offline style guide (.gallery) keeps its own framing. */
.content .chart-box,
.content .metrics-box,
.content .scalar {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow-card);
  padding: var(--space-4); overflow: hidden;
}
.content .scalar { padding: var(--space-3) var(--space-4); }

/* ---- Grouped plot legend (2-D: tissue × run comparison) -------------- */
/* A wrapper holds the chart (fills) beside a scrollable legend column. */
.chart-wrap { display: flex; flex: 1 1 auto; min-height: 0; gap: var(--space-4); }
.chart-wrap > .chart { flex: 1 1 auto; min-width: 0; }
.chart-legend {
  flex: 0 0 auto; width: 200px; overflow-y: auto; max-height: 100%;
  padding-left: 2px; scrollbar-width: thin;
}
/* Filter + bulk toggles for a large Series group (many pooled records). */
.legend-filter { display: flex; gap: 4px; margin-bottom: 6px; }
.legend-filter-input {
  flex: 1; min-width: 0; padding: 2px 6px; font-size: 11px;
  border: 1px solid var(--line); border-radius: 6px; background: var(--card); color: var(--fg);
}
.legend-bulk {
  flex: 0 0 auto; padding: 2px 7px; font-size: 11px; cursor: pointer;
  border: 1px solid var(--line); border-radius: 6px; background: var(--card); color: var(--muted);
}
.legend-bulk:hover { background: var(--hover); color: var(--fg); }
.legend-group { margin-bottom: var(--space-3); }
.legend-group + .legend-group { border-top: 1px solid var(--line); padding-top: var(--space-3); }
.legend-group-title {
  font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em;
  color: var(--muted); margin: 0 0 6px;
}
.legend-item {
  display: flex; align-items: flex-start; gap: var(--space-2); width: 100%;
  padding: 3px 6px; background: transparent; border: 0; border-radius: var(--radius-sm);
  cursor: pointer; color: var(--fg); font-size: 12px; text-align: left;
}
.legend-item:hover { background: var(--hover); }
.legend-item.off { opacity: .45; }
.legend-item.off .legend-label { text-decoration: line-through; }
/* Wrap long labels so the full name is always readable (a pooled series like
   "ID 1 · OCC 4 · Extravascular · 100 mg") — the filter above narrows the count. */
.legend-label { flex: 1; min-width: 0; white-space: normal; overflow-wrap: anywhere; line-height: 1.3; }
.legend-swatch { margin-top: 2px; }
.legend-swatch { flex: 0 0 auto; }
.legend-swatch-color { width: 12px; height: 12px; border-radius: 3px; }
.legend-swatch-dash { width: 24px; height: 8px; color: var(--fg); }
.legend-swatch-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
/* A lone chart grows to fill the results area (no gap below it); when a tab stacks
   several views each keeps its natural height (flex-shrink:0 — otherwise a tall facet
   is squeezed below its content and bleeds past its card) and the panel scrolls. */
.chart-box { display: flex; flex-direction: column; min-height: 340px; flex-shrink: 0; }
.tab-panel > .chart-box:only-child { flex: 1 1 auto; }
.chart-box h3 { margin: 0; font-size: 14px; }
/* Plot heading row: title on the left, the per-plot Y-axis Linear/Log toggle on the right. */
.chart-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 0 0 6px; flex: 0 0 auto; }
.scale-toggle { display: inline-flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }
.scale-toggle .scale-label { white-space: nowrap; }
/* Segmented Linear/Log toggle — theme-aware via tokens (works in light + dark). */
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: var(--radius-sm); overflow: hidden; }
.seg-btn { appearance: none; border: 0; background: var(--surface-2); color: var(--muted); font-size: 12px; line-height: 1; padding: 4px 12px; cursor: pointer; }
.seg-btn + .seg-btn { border-left: 1px solid var(--line); }
.seg-btn:hover:not(.active) { color: var(--fg); }
.seg-btn.active { background: var(--accent); color: var(--accent-fg); }
.error { color: var(--error); }
pre { white-space: pre-wrap; word-break: break-word; background: var(--surface-2); padding: 10px; border-radius: var(--radius-sm); margin: 0; }
details summary { cursor: pointer; }
svg.chart { background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius-sm); max-width: 100%; }
.chart .axis { stroke: var(--line); stroke-width: 1; }
.chart .series { stroke: var(--accent); stroke-width: 2; }
.chart .dot { fill: var(--accent); }
.chart .tick { fill: var(--muted); font-size: 10px; }

.hidden { display: none; }

/* ---- Diagnostics drawer (Stage C) ------------------------------------ */
.diagnostics {
  border-top: 1px solid var(--line); background: var(--surface);
}
.diag-summary {
  cursor: pointer; list-style: none; user-select: none;
  display: flex; align-items: center; gap: var(--space-2);
  padding: 8px var(--space-5); font-size: 12px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .04em; color: var(--muted);
}
.diag-summary::-webkit-details-marker { display: none; }
.diag-summary::before {
  content: "▸"; color: var(--muted); font-size: 11px; transition: transform .12s ease;
}
.diagnostics[open] .diag-summary::before { transform: rotate(90deg); }
.diag-count {
  margin-left: auto; font-size: 11px; background: var(--surface-2);
  padding: 1px 8px; border-radius: 999px; letter-spacing: 0;
}
.diag-progress { height: 4px; background: var(--surface-2); overflow: hidden; }
.diag-progress-fill {
  height: 100%; width: 0; background: var(--accent); transition: width .15s ease;
}
.diag-log {
  margin: 0; padding: var(--space-2) 0; list-style: none;
  max-height: 200px; overflow-y: auto;
  font: 12px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.diag-line { display: flex; gap: var(--space-3); padding: 1px var(--space-5); }
.diag-level {
  flex: 0 0 44px; font-weight: 700; color: var(--muted); text-align: right;
}
.diag-warn .diag-level { color: #b45309; }
.diag-error { color: var(--error); }
.diag-error .diag-level { color: var(--error); }
.diag-msg { color: var(--fg); white-space: pre-wrap; word-break: break-word; }
.diag-warn .diag-msg { color: var(--fg); }

/* ── Parameter-estimation panel (WS11 Phase D) ─────────────────────────── */
.estimation-panel .estimation-body { display: flex; flex-direction: column; gap: 8px; }
.est-row { display: grid; grid-template-columns: 20px 1fr 68px 68px; gap: 6px; align-items: center; }
.est-head { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.est-head .est-name, .est-head .est-bound { text-align: left; }
.est-name { font-family: var(--mono, monospace); font-size: 13px; }
.est-bound { width: 100%; min-width: 0; padding: 3px 5px; font-size: 12px;
  border: 1px solid var(--line); border-radius: 6px; background: var(--card); color: var(--fg); }
.est-weighting { display: grid; grid-template-columns: auto 1fr 68px 68px; gap: 6px; align-items: center; margin-top: 2px; }
.est-weighting label { font-size: 12px; color: var(--muted); }
.est-weighting select, .est-weighting input { padding: 3px 5px; font-size: 12px;
  border: 1px solid var(--line); border-radius: 6px; background: var(--card); color: var(--fg); }
/* Fit runs the estimator — same green + play icon as the toolbar Run button. */
.est-fit { align-self: flex-start; display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 12px; border-radius: 8px; background: var(--accent-green); border: 1px solid var(--accent-green);
  color: var(--accent-green-fg); cursor: pointer; }
.est-fit .icon { display: block; }
.est-fit:hover { filter: brightness(1.05); }
.est-fit:disabled { opacity: .5; cursor: default; filter: none; }
.est-summary { font-size: 12px; color: var(--muted); margin: 2px 0 0; }
.est-summary.error { color: var(--error); }
/* The result table can exceed the sidebar width (the 95% CI column is wide), so it
   sizes to its content and scrolls horizontally within the panel rather than clipping. */
.est-result { overflow-x: auto; }
.est-table { width: max-content; min-width: 100%; border-collapse: collapse; font-size: 12px; }
.est-table th { text-align: left; color: var(--muted); font-weight: 600; padding: 3px 6px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.est-table td { padding: 3px 6px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.est-table .est-ci { color: var(--muted); font-variant-numeric: tabular-nums; }
