:root {
  color-scheme: light;
  --theme-surface: #f4f7fb;
  --theme-panel: #ffffff;
  --theme-panel-2: #edf3fa;
  --theme-text: #0a1020;
  --theme-muted: #526078;
  --theme-line: #d8e0ec;
  --theme-shadow: rgba(28, 44, 75, 0.1);
}

html[data-theme="dark"] {
  color-scheme: dark;
  --theme-surface: #07101d;
  --theme-panel: #0d192a;
  --theme-panel-2: #111f32;
  --theme-text: #edf6ff;
  --theme-muted: #a8b7ca;
  --theme-line: #22324a;
  --theme-shadow: rgba(0, 0, 0, 0.42);
}

.theme-control-cluster {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.65rem;
  margin-left: auto;
}

.theme-toggle {
  position: relative;
  z-index: 1;
  display: inline-grid;
  width: 52px;
  min-width: 52px;
  height: 44px;
  min-height: 44px;
  padding: 0;
  place-items: center;
  overflow: visible;
  color: #fff;
  background: rgba(255, 255, 255, 0.055);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 999px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06);
  cursor: pointer;
  isolation: isolate;
  touch-action: manipulation;
  transition: border-color 180ms ease, background 180ms ease, box-shadow 180ms ease, transform 180ms ease;
}

.theme-toggle::before {
  position: absolute;
  z-index: -1;
  inset: -9px;
  background: radial-gradient(circle, rgba(76, 194, 255, 0.22), rgba(107, 93, 211, 0.08) 46%, transparent 72%);
  border-radius: inherit;
  content: "";
  opacity: 0;
  filter: blur(7px);
  transition: opacity 220ms ease;
}

.theme-toggle:hover,
.theme-toggle:focus-visible {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(105, 200, 255, 0.55);
  box-shadow: 0 8px 24px rgba(0, 120, 212, 0.22), inset 0 1px 0 rgba(255, 255, 255, 0.12);
  transform: translateY(-1px);
}

.theme-toggle:hover::before,
.theme-toggle:focus-visible::before,
html[data-theme="dark"] .theme-toggle::before {
  opacity: 1;
}

.theme-toggle:focus-visible {
  outline: 3px solid #69c8ff;
  outline-offset: 3px;
}

.theme-toggle-track {
  position: relative;
  display: block;
  width: 38px;
  height: 22px;
  padding: 2px;
  background: linear-gradient(105deg, #ffca62, #69c8ff 48%, #8f7aef);
  border-radius: 999px;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.22);
}

.theme-toggle-orb {
  position: relative;
  display: grid;
  width: 18px;
  height: 18px;
  place-items: center;
  color: #815b00;
  background: #fff4c7;
  border-radius: 50%;
  box-shadow: 0 2px 7px rgba(1, 8, 20, 0.42);
  transform: translateX(0);
  transition: color 240ms ease, background 240ms ease, transform 240ms cubic-bezier(0.2, 0.75, 0.22, 1);
}

.theme-toggle-orb::after {
  content: "☀";
  font: 700 11px/1 Arial, sans-serif;
}

html[data-theme="dark"] .theme-toggle-orb {
  color: #d8f5ff;
  background: #0d213b;
  transform: translateX(16px);
}

html[data-theme="dark"] .theme-toggle-orb::after {
  content: "◐";
}

.theme-toggle-label {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

html[data-theme-animating="true"] body,
html[data-theme-animating="true"] .knowledge,
html[data-theme-animating="true"] .knowledge-card,
html[data-theme-animating="true"] .library,
html[data-theme-animating="true"] .resource-card,
html[data-theme-animating="true"] .article-layout,
html[data-theme-animating="true"] .phase-card,
html[data-theme-animating="true"] .sop-phase,
html[data-theme-animating="true"] .task-card,
html[data-theme-animating="true"] .toc {
  transition: color 240ms ease, background-color 240ms ease, border-color 240ms ease, box-shadow 240ms ease;
}

/* Shared dark foundation across the knowledge home, libraries, and SOPs. */
html[data-theme="dark"] body {
  --ink: var(--theme-text);
  --ink-soft: var(--theme-muted);
  --surface: var(--theme-surface);
  --panel: var(--theme-panel);
  --line: var(--theme-line);
  --night: #030812;
  --midnight: #030812;
  --midnight-2: #071424;
  --panel-muted: var(--theme-panel-2);
  --success: #4ee0aa;
  --success-soft: #0d2c24;
  --warning: #f4b657;
  --warning-soft: #33240d;
  --danger: #ff766d;
  --danger-soft: #351817;
  background: var(--theme-surface);
  color: var(--theme-text);
}

/* Knowledge homepage */
html[data-theme="dark"] .knowledge {
  color: var(--theme-text);
  background:
    linear-gradient(rgba(105, 200, 255, 0.045) 1px, transparent 1px),
    linear-gradient(90deg, rgba(105, 200, 255, 0.045) 1px, transparent 1px),
    var(--theme-surface);
  background-size: 70px 70px;
}

html[data-theme="dark"] .knowledge-card {
  color: var(--theme-text);
  background:
    linear-gradient(rgba(105, 200, 255, 0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(105, 200, 255, 0.035) 1px, transparent 1px),
    radial-gradient(circle at 50% 42%, color-mix(in srgb, var(--card-accent) 14%, transparent), transparent 38%),
    linear-gradient(145deg, #101c2e, color-mix(in srgb, var(--card-accent) 7%, #081322));
  background-size: 32px 32px, 32px 32px, auto, auto;
  border-color: color-mix(in srgb, var(--card-accent) 30%, var(--theme-line));
  box-shadow: 0 24px 58px rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.06);
}

html[data-theme="dark"] .knowledge-card:hover,
html[data-theme="dark"] .knowledge-card:focus-visible {
  border-color: color-mix(in srgb, var(--card-accent) 62%, var(--theme-line));
  box-shadow: 0 30px 72px rgba(0, 0, 0, 0.42), 0 12px 32px color-mix(in srgb, var(--card-accent) 14%, transparent), inset 0 1px 0 rgba(255, 255, 255, 0.09);
}

html[data-theme="dark"] .knowledge-card h3::before {
  background:
    radial-gradient(circle at 38% 30%, rgba(255, 255, 255, 0.12), transparent 29%),
    radial-gradient(circle, color-mix(in srgb, var(--card-accent) 18%, #14243a), color-mix(in srgb, var(--card-accent) 5%, #0a1627) 63%, transparent 64%);
  border-color: color-mix(in srgb, var(--card-accent) 42%, #1a2b42);
  box-shadow: 0 0 0 14px color-mix(in srgb, var(--card-accent) 5%, transparent), 0 25px 58px rgba(0, 0, 0, 0.38), inset 0 0 42px rgba(105, 200, 255, 0.06);
}

html[data-theme="dark"] .knowledge-card:hover h3,
html[data-theme="dark"] .knowledge-card:focus-visible h3,
html[data-theme="dark"] .card-affordance {
  color: color-mix(in srgb, var(--card-accent) 70%, #ffffff);
}

html[data-theme="dark"] .contact-direct {
  color: #061121;
  background: linear-gradient(135deg, #c8ff67, #70e8f1 58%, #79a7ff);
  border-color: rgba(185, 255, 56, 0.72);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.38), 0 0 40px rgba(76, 194, 255, 0.12);
}

html[data-theme="dark"] .contact-direct:hover,
html[data-theme="dark"] .contact-direct:focus-visible {
  color: #fff;
  background: #0078d4;
}

/* Category libraries */
html[data-theme="dark"] .library {
  background:
    linear-gradient(rgba(105, 200, 255, 0.045) 1px, transparent 1px),
    linear-gradient(90deg, rgba(105, 200, 255, 0.045) 1px, transparent 1px),
    var(--theme-surface);
  background-size: 70px 70px;
}

html[data-theme="dark"] .resource-card {
  background: linear-gradient(145deg, rgba(17, 31, 50, 0.98), rgba(9, 20, 35, 0.98));
  border-color: color-mix(in srgb, var(--accent) 28%, var(--theme-line));
  box-shadow: 0 16px 38px rgba(0, 0, 0, 0.28);
}

html[data-theme="dark"] .resource-card:hover,
html[data-theme="dark"] .resource-card.is-expanded {
  box-shadow: 0 24px 56px rgba(0, 0, 0, 0.4), 0 10px 30px color-mix(in srgb, var(--accent) 10%, transparent);
}

html[data-theme="dark"] .tags span {
  color: #b7c7dc;
  background: #121f31;
  border-color: #2a3b54;
}

html[data-theme="dark"] .resource-links a {
  background: color-mix(in srgb, var(--accent) 12%, #0d192a);
  border-color: color-mix(in srgb, var(--accent) 38%, var(--theme-line));
}

html[data-theme="dark"] .resource-links a:hover {
  background: color-mix(in srgb, var(--accent) 19%, #0d192a);
}

html[data-theme="dark"] .team-callout a {
  color: #061121;
  background: linear-gradient(135deg, #c8ff67, #70e8f1);
}

/* SOP article system */
html[data-theme="dark"] .article-layout {
  background: var(--theme-surface);
}

html[data-theme="dark"] .sop-toolbar,
html[data-theme="dark"] .quick-response,
html[data-theme="dark"] .first-ten-minutes,
html[data-theme="dark"] .sop-priority,
html[data-theme="dark"] .phase-card,
html[data-theme="dark"] .sop-phase,
html[data-theme="dark"] .task-card,
html[data-theme="dark"] .sop-task,
html[data-theme="dark"] .phase-task,
html[data-theme="dark"] .task-checklist label,
html[data-theme="dark"] .closure-checklist label,
html[data-theme="dark"] .check-task,
html[data-theme="dark"] .action-list li,
html[data-theme="dark"] .detail-card,
html[data-theme="dark"] .article-source,
html[data-theme="dark"] .toc,
html[data-theme="dark"] .toc-toggle,
html[data-theme="dark"] [data-toc-toggle],
html[data-theme="dark"] .reference-list a,
html[data-theme="dark"] .sop-disclosure,
html[data-theme="dark"] .article-disclosure,
html[data-theme="dark"] details.supporting-detail,
html[data-theme="dark"] .sop-screenshot,
html[data-theme="dark"] .screenshot-figure,
html[data-theme="dark"] .evidence-figure {
  color: var(--theme-text);
  background: var(--theme-panel);
  border-color: var(--theme-line);
  box-shadow: 0 14px 38px var(--theme-shadow);
}

html[data-theme="dark"] .prerequisites-section {
  background:
    radial-gradient(circle at 100% 0, rgba(76, 194, 255, 0.12), transparent 34%),
    var(--theme-panel);
  border-color: #285075;
}

html[data-theme="dark"] #licensing-access { background: radial-gradient(circle at 100% 0, rgba(76, 194, 255, 0.12), transparent 34%), var(--theme-panel); border-color: #285075; }

html[data-theme="dark"] :is(.license-matrix, .sop-table, .access-matrix, .access-table),
html[data-theme="dark"] :is(.license-matrix, .sop-table, .access-matrix, .access-table) tbody th,
html[data-theme="dark"] :is(.license-matrix, .sop-table, .access-matrix, .access-table) td {
  color: var(--theme-text);
  background: var(--theme-panel);
  border-color: var(--theme-line);
}

html[data-theme="dark"] :is(.license-matrix, .sop-table, .access-matrix, .access-table) thead th {
  color: #bcd0e7;
  background: var(--theme-panel-2);
  border-color: var(--theme-line);
}

html[data-theme="dark"] :is(.license-matrix, .sop-table, .access-matrix, .access-table) tbody tr:hover > * { background: #12233a; }

html[data-theme="dark"] :is(.license-matrix-wrap, .sop-table-wrap, .table-scroll, .access-table-wrap) {
  background: var(--theme-panel);
  border-color: var(--theme-line);
}

html[data-theme="dark"] :is(.action-access, .action-access-callout) {
  color: #c9ddf1;
  background: #10243b;
  border-color: #2b4866;
  border-left-color: #4cc2ff;
}

html[data-theme="dark"] :is(.action-access, .action-access-callout) > strong,
html[data-theme="dark"] .action-access dt { color: var(--theme-text); }

html[data-theme="dark"] .license-matrix,
html[data-theme="dark"] .license-matrix tbody th,
html[data-theme="dark"] .license-matrix td {
  color: var(--theme-text);
  background: var(--theme-panel);
  border-color: var(--theme-line);
}

html[data-theme="dark"] .license-matrix thead th {
  color: #bcd0e7;
  background: var(--theme-panel-2);
  border-color: var(--theme-line);
}

html[data-theme="dark"] .license-matrix tbody tr:hover > * { background: #12233a; }

html[data-theme="dark"] .action-access > div {
  background: var(--theme-panel-2);
  border-color: var(--theme-line);
}

html[data-theme="dark"] .action-access dd { color: #c9ddf1; }
html[data-theme="dark"] .matrix-intro p { color: var(--theme-text-muted); }

html[data-theme="dark"] .sop-toolbar button,
html[data-theme="dark"] .sop-toolbar a,
html[data-theme="dark"] .sop-tool-button,
html[data-theme="dark"] [data-action="print"],
html[data-theme="dark"] [data-action="copy-url"],
html[data-theme="dark"] [data-action="copy-page-url"],
html[data-theme="dark"] [data-action="copy-checklist"],
html[data-theme="dark"] [data-action="reset-checklist"],
html[data-theme="dark"] [data-action="copy-evidence"],
html[data-theme="dark"] [data-sop-action] {
  color: var(--theme-text);
  background: var(--theme-panel-2);
  border-color: #30445f;
}

html[data-theme="dark"] .sop-toolbar button:hover,
html[data-theme="dark"] .sop-toolbar a:hover,
html[data-theme="dark"] .sop-tool-button:hover,
html[data-theme="dark"] [data-sop-action]:hover {
  color: #7fd4ff;
  background: #142640;
  border-color: #4cc2ff;
}

html[data-theme="dark"] .quick-response,
html[data-theme="dark"] .first-ten-minutes,
html[data-theme="dark"] .sop-priority {
  background: radial-gradient(circle at 100% 0, rgba(76, 194, 255, 0.13), transparent 35%), var(--theme-panel);
  border-color: #275071;
}

html[data-theme="dark"] .checklist-progress,
html[data-theme="dark"] [data-checklist-progress] {
  background: #10243b;
  border-color: #285075;
}

html[data-theme="dark"] .task-checklist label:hover,
html[data-theme="dark"] .closure-checklist label:hover,
html[data-theme="dark"] .check-task:hover {
  background: #12233a;
  border-color: #3c6d91;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.28);
}

html[data-theme="dark"] .task-checklist li.is-complete label,
html[data-theme="dark"] .closure-checklist li.is-complete label,
html[data-theme="dark"] .check-task.is-complete {
  background: var(--success-soft);
  border-color: #256d59;
}

html[data-theme="dark"] .task-checklist li.is-complete strong,
html[data-theme="dark"] .closure-checklist li.is-complete strong,
html[data-theme="dark"] .check-task.is-complete strong {
  color: #8de8c4;
}

html[data-theme="dark"] .path-card {
  background: linear-gradient(120deg, #10243b, #0d192a);
  border-color: #285075;
}

html[data-theme="dark"] .path-card p,
html[data-theme="dark"] .phase-goal,
html[data-theme="dark"] .sop-goal,
html[data-theme="dark"] .goal-line,
html[data-theme="dark"] .plain-note,
html[data-theme="dark"] .evidence-card,
html[data-theme="dark"] .owner-card {
  color: #c9ddf1;
  background: #10243b;
  border-color: #2b4866;
}

html[data-theme="dark"] .alert-title,
html[data-theme="dark"] .stop-rule h3,
html[data-theme="dark"] .decision-card h3,
html[data-theme="dark"] .completion-card h3,
html[data-theme="dark"] .complete-when h3,
html[data-theme="dark"] .evidence-card h3,
html[data-theme="dark"] .owner-card h3,
html[data-theme="dark"] .alert > strong,
html[data-theme="dark"] .sop-alert > strong,
html[data-theme="dark"] .stop-rule > strong,
html[data-theme="dark"] .decision-card > strong,
html[data-theme="dark"] .completion-card > strong,
html[data-theme="dark"] .complete-when > strong,
html[data-theme="dark"] .evidence-card > strong,
html[data-theme="dark"] .owner-card > strong {
  color: var(--theme-text);
}

html[data-theme="dark"] .alert-warning,
html[data-theme="dark"] .sop-alert.warning,
html[data-theme="dark"] .decision-card {
  color: #f1d39b;
  background: var(--warning-soft);
  border-color: #6c4d1c;
  border-left-color: var(--warning);
}

html[data-theme="dark"] .alert-danger,
html[data-theme="dark"] .sop-alert.danger,
html[data-theme="dark"] .stop-rule {
  color: #ffc1bc;
  background: var(--danger-soft);
  border-color: #71302d;
  border-left-color: var(--danger);
}

html[data-theme="dark"] .alert-success,
html[data-theme="dark"] .sop-alert.success,
html[data-theme="dark"] .completion-card,
html[data-theme="dark"] .complete-when {
  color: #a9e9d3;
  background: var(--success-soft);
  border-color: #256d59;
  border-left-color: var(--success);
}

html[data-theme="dark"] .sop-disclosure[open] summary,
html[data-theme="dark"] .article-disclosure[open] summary,
html[data-theme="dark"] details.supporting-detail[open] summary,
html[data-theme="dark"] .toc a:hover,
html[data-theme="dark"] .toc a[aria-current="location"] {
  background: #10243b;
}

@media (min-width: 1120px) and (hover: hover) and (pointer: fine) {
  html[data-theme="dark"] .toc {
    background: transparent;
    border-color: transparent;
    box-shadow: none;
  }

  html[data-theme="dark"] .toc a:hover,
  html[data-theme="dark"] .toc a[aria-current="location"] {
    background: transparent;
  }

  html[data-theme="dark"] .toc-steps::before {
    background: #2a405b;
  }

  html[data-theme="dark"] .toc-steps::after {
    background: linear-gradient(180deg, #70e8f1, #4cc2ff);
    box-shadow: 0 0 15px rgba(76, 194, 255, 0.38);
  }

  html[data-theme="dark"] .toc-dot {
    background: #60758f;
    border-color: #8195ad;
    box-shadow: 0 0 0 4px rgba(96, 117, 143, 0.1);
  }

  html[data-theme="dark"] .toc a[data-complete="true"] .toc-dot {
    background: #4cc2ff;
    border-color: #9be4ff;
    box-shadow: 0 0 13px rgba(76, 194, 255, 0.34);
  }

  html[data-theme="dark"] .toc a[aria-current="location"] .toc-dot {
    background: #70d4ff;
    border-color: #e4f7ff;
    box-shadow: 0 0 0 5px rgba(76, 194, 255, 0.19), 0 0 21px rgba(76, 194, 255, 0.48);
  }

  html[data-theme="dark"] .toc a:hover .toc-dot,
  html[data-theme="dark"] .toc a:focus-visible .toc-dot {
    background: #a8ff2f;
    border-color: #f2ffd9;
    box-shadow: 0 0 0 6px rgba(168, 255, 47, 0.18), 0 0 24px rgba(168, 255, 47, 0.52);
  }

  html[data-theme="dark"] .toc-link-label {
    color: var(--theme-text);
    background: rgba(11, 23, 40, 0.97);
    border-color: #2a405b;
    box-shadow: 0 18px 46px rgba(0, 0, 0, 0.34);
  }

  html[data-theme="dark"] .toc-link-label::after {
    border-color: #2a405b;
  }
}

html[data-theme="dark"] .sop-screenshot img,
html[data-theme="dark"] .screenshot-figure img,
html[data-theme="dark"] .evidence-figure img {
  background: #fff;
}

html[data-theme="dark"] .article-cta a {
  color: #061121;
}

@media (max-width: 680px) {
  body {
    min-width: 0 !important;
  }

  .theme-control-cluster {
    gap: 0.35rem;
  }

  .site-header,
  .resource-header,
  .article-header {
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }

  .resource-header .theme-toggle,
  .article-header .theme-toggle,
  .site-header .theme-toggle {
    position: fixed;
    right: max(1rem, env(safe-area-inset-right));
    bottom: max(1rem, env(safe-area-inset-bottom));
    z-index: 90;
    background: rgba(5, 9, 20, 0.9);
    box-shadow: 0 14px 36px rgba(0, 0, 0, 0.38), 0 0 26px rgba(76, 194, 255, 0.16);
    backdrop-filter: blur(14px);
  }
}

@media (prefers-reduced-motion: reduce) {
  .theme-toggle,
  .theme-toggle::before,
  .theme-toggle-orb,
  html[data-theme-animating="true"] body,
  html[data-theme-animating="true"] .knowledge,
  html[data-theme-animating="true"] .knowledge-card,
  html[data-theme-animating="true"] .library,
  html[data-theme-animating="true"] .resource-card,
  html[data-theme-animating="true"] .article-layout,
  html[data-theme-animating="true"] .phase-card,
  html[data-theme-animating="true"] .sop-phase,
  html[data-theme-animating="true"] .task-card,
  html[data-theme-animating="true"] .toc {
    transition: none !important;
  }
}

@media print {
  .theme-toggle {
    display: none !important;
  }
}
