:root {
  color-scheme: light;
  --background: #f4f6fa;
  --surface: #fff;
  --control: #f8faff;
  --table-heading: #f5f7fb;
  --hover: #f0f4fb;
  --text: #17243c;
  --secondary: #344868;
  --muted: #61708a;
  --line: #e5eaf2;
  --control-border: #d8deeb;
  --blue: #2858e2;
  --blue-hover: #2555d6;
  --accent: #244fc4;
  --accent-surface: #edf2ff;
  --accent-line: #aabff2;
  --focus: #4169e1;
  --warning: #77531e;
  --warning-surface: #fff3dc;
  --warning-note: #fcf9f1;
  --warning-line: #ecd7ab;
  --success: #326e58;
  --success-surface: #eaf7f1;
  --success-line: #c8e8da;
  --danger: #ad3647;
  --danger-surface: #fff5f6;
  --danger-hover: #ffe9ed;
  --danger-line: #e7b5bd;
  --backdrop: #111f3a80;
  --dialog-shadow: #17243c33;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --background: #0f1624;
  --surface: #182233;
  --control: #121c2c;
  --table-heading: #1f2c41;
  --hover: #233149;
  --text: #e7edf7;
  --secondary: #d0dcee;
  --muted: #a9b8ce;
  --line: #33435b;
  --control-border: #607391;
  --blue: #305cd6;
  --blue-hover: #3764df;
  --accent: #b2c8ff;
  --accent-surface: #263c62;
  --accent-line: #637fb5;
  --focus: #9bbbff;
  --warning: #f4d39b;
  --warning-surface: #3a2c1d;
  --warning-note: #30271d;
  --warning-line: #705633;
  --success: #a8e3c9;
  --success-surface: #1c392f;
  --success-line: #3b6757;
  --danger: #ffb5bf;
  --danger-surface: #3d242f;
  --danger-hover: #512c3a;
  --danger-line: #825063;
  --backdrop: #070c15bf;
  --dialog-shadow: #070c1599;
}

[data-theme-toggle] {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 1px solid var(--control-border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--secondary);
  font: 21px/1 "Segoe UI Symbol", system-ui, sans-serif;
  cursor: pointer;
}
[data-theme-toggle]::before { content: "☾" / ""; }
[data-theme-toggle][aria-pressed="true"]::before { content: "☀" / ""; }
[data-theme-toggle]:hover { background: var(--hover); }
[data-theme-toggle]:focus-visible { outline: 2px solid var(--focus); outline-offset: 3px; }
[data-theme-toggle][hidden] { display: none; }

.login-header, .catalog-actions { display: flex; align-items: center; gap: 12px; }
.login-header { justify-content: space-between; }
.login-header [data-theme-toggle],
.catalog-actions [data-theme-toggle] {
  --surface: #1e3050;
  --secondary: #e7edf7;
  --hover: #2a4168;
  --control-border: #627ca4;
  --focus: #9bbbff;
}

/* The study-type catalog ships its own light stylesheet. */
:root[data-theme="dark"] body[data-portal] { background: var(--background); color: var(--text); }
:root[data-theme="dark"] body[data-portal] header { background: var(--surface); border-bottom: 1px solid var(--line); }
:root[data-theme="dark"] body[data-portal] :is(section, article, .card) {
  background: var(--surface);
  border-color: var(--line);
}
:root[data-theme="dark"] body[data-portal] :is(input, select, textarea, #case-detail, pre, .metrics span) {
  background: var(--control);
  color: var(--text);
}
:root[data-theme="dark"] body[data-portal] :is(button, input, select, textarea) { border-color: var(--control-border); }
:root[data-theme="dark"] body[data-portal] :is(button, .badge) { background: var(--accent-surface); color: var(--accent); }
:root[data-theme="dark"] body[data-portal] button:hover { background: var(--hover); }
:root[data-theme="dark"] body[data-portal] :is(a, button, input, select, textarea):focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 3px;
}
:root[data-theme="dark"] body[data-portal] :is(th, td) { border-color: var(--line); }
:root[data-theme="dark"] body[data-portal] :is(label, th, .muted, footer) { color: var(--muted); }
:root[data-theme="dark"] body[data-portal] :is(input, textarea)::placeholder {
  color: var(--muted);
  opacity: 1;
}
:root[data-theme="dark"] body[data-portal] .warning {
  background: var(--warning-surface);
  color: var(--warning);
  border-color: var(--warning-line);
}
:root[data-theme="dark"] body[data-portal] .error { color: var(--danger); }
:root[data-theme="dark"] body[data-portal] .success { color: var(--success); }

@media (pointer: coarse) {
  [data-theme-toggle] { width: 44px; height: 44px; }
}
@media (max-width: 420px) {
  body[data-portal] header { flex-wrap: wrap; gap: 10px; }
}
