:root {
  color-scheme: light;
  --control: #fcfdf9;
  --control-hover: #e9f1e8;
  --control-selected: #e0f0d9;
  --control-border: #b9cbbf;
  --focus: #267e68;
  --success: #145c41;
  --danger: #8a3021;
  --terminal: #142f2b;
}

.header-tools {
  display: flex;
  align-items: center;
  gap: 8px;
}

.figure-card, .lesson-card { transition-property: transform, box-shadow; }

.theme-toggle {
  display: inline-grid;
  flex: 0 0 auto;
  place-items: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--control);
  color: var(--green);
}

.theme-toggle:hover {
  background: var(--control-hover);
  border-color: var(--control-border);
}

.theme-toggle::before {
  content: "";
  width: 18px;
  height: 18px;
  background: currentColor;
  mask: center / contain no-repeat url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M20.9 14.1A9 9 0 0 1 9.9 3.1 9 9 0 1 0 20.9 14.1Z' fill='none' stroke='black' stroke-width='1.8' stroke-linejoin='round'/%3E%3C/svg%3E");
}

.theme-toggle[aria-pressed="true"]::before {
  mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Ccircle cx='12' cy='12' r='4' fill='none' stroke='black' stroke-width='1.8'/%3E%3Cpath d='M12 2v2m0 16v2M2 12h2m16 0h2M4.9 4.9l1.4 1.4m11.4 11.4 1.4 1.4m0-14.2-1.4 1.4M6.3 17.7l-1.4 1.4' fill='none' stroke='black' stroke-width='1.8' stroke-linecap='round'/%3E%3C/svg%3E");
}

@media (min-width: 781px) and (max-width: 960px) {
  .header-inner {
    width: calc(100% - 44px);
    min-height: 0;
    flex-wrap: wrap;
    gap: 0 9px;
    padding-block: 7px 4px;
  }

  .site-nav { order: 3; width: 100%; overflow: auto; }
}

@media (max-width: 960px) {
  .header-tools { margin-left: auto; }
}

@media (max-width: 490px) {
  .header-inner .wordmark { font-size: 1.2rem; gap: 6px; }
  .header-inner .brand-icon { width: 27px; height: 27px; }
  .header-tools { gap: 4px; }
  .header-tools .header-progress { gap: 5px; padding-inline: 5px; font-size: .7rem; }
}

@media (forced-colors: active) {
  .theme-toggle::before { background: ButtonText; forced-color-adjust: none; }
}

@media screen {
  html[data-theme="dark"] {
    color-scheme: dark;
    --paper: #101a18;
    --white: #172522;
    --ink: #e9f4ed;
    --muted: #b3c6bb;
    --line: #334a42;
    --green: #9cdfbf;
    --green-dark: #0c1613;
    --blue: #1d3042;
    --violet: #302740;
    --mint: #1d352d;
    --shadow: 0 10px 36px #0005;
    --control: #1b2c27;
    --control-hover: #263d34;
    --control-selected: #2d4839;
    --control-border: #617f70;
    --focus: #a9e9c9;
    --success: #a6e7bc;
    --danger: #ffb6a3;
    --terminal: #0d1b17;
  }

  html[data-theme="dark"] :focus-visible,
  html[data-theme="dark"] .lesson-section:focus-visible {
    outline-color: var(--focus);
  }

  html[data-theme="dark"] .Transport { --tone: #a9d4ff; }
  html[data-theme="dark"] .Application { --tone: #dec7ff; }
  html[data-theme="dark"] .Network { --tone: #a3e6c3; }

  html[data-theme="dark"] :is(.skip, .site-header, .side, .panel, .lesson-card, .figure-card, dialog, .dialog-top) {
    background: var(--white);
  }

  html[data-theme="dark"] :is(.site-nav a, .header-progress, .unit-card p, .flow-node small) {
    color: var(--muted);
  }

  html[data-theme="dark"] :is(.site-nav, .side) a:is(:hover, [aria-current="page"]) {
    background: var(--control-hover);
    color: var(--green);
  }

  html[data-theme="dark"] .progress-dot {
    background: var(--green);
    box-shadow: 0 0 0 3px #9cdfbf26;
  }

  html[data-theme="dark"] .hero { box-shadow: 0 14px 38px #0007; }
  html[data-theme="dark"] .unit-card { border-color: var(--line); }
  html[data-theme="dark"] .unit-card.Transport { border-color: #3c5671; }
  html[data-theme="dark"] .unit-card.Application { border-color: #584568; }
  html[data-theme="dark"] .layer-icon { background: #0c16135c; }
  html[data-theme="dark"] .tag { background: var(--tint, var(--mint)); }
  html[data-theme="dark"] .panel.lesson-intro {
    background: linear-gradient(135deg, var(--tint, var(--mint)), var(--white) 80%);
  }

  html[data-theme="dark"] :is(.button, .chip, .toc button, .question-nav button, .header-field, input[type="search"], input[type="text"], input[type="number"], select) {
    background: var(--control);
    border-color: var(--control-border);
    color: var(--ink);
  }

  html[data-theme="dark"] input::placeholder { color: var(--muted); opacity: 1; }

  html[data-theme="dark"] :is(.button, .chip, .toc button, .question-nav button, .header-field):hover {
    background: var(--control-hover);
    border-color: var(--green);
  }

  html[data-theme="dark"] :is(.primary, .chip[aria-pressed="true"]) {
    background: var(--green);
    border-color: var(--green);
    color: #10271e;
  }

  html[data-theme="dark"] :is(.primary, .chip[aria-pressed="true"]):hover {
    background: #c6f1db;
    border-color: #c6f1db;
  }

  html[data-theme="dark"] .hero .primary {
    background: var(--lime);
    border-color: var(--lime);
    color: #162c24;
  }

  html[data-theme="dark"] .hero .primary:hover { background: #defcaa; }
  html[data-theme="dark"] .hero .secondary {
    background: #163b32;
    border-color: #779b88;
    color: #ebf5ec;
  }

  html[data-theme="dark"] .hero .secondary:hover { background: #265440; }

  html[data-theme="dark"] :is(.note, blockquote) {
    background: var(--mint);
    border-left-color: #82b36a;
  }

  html[data-theme="dark"] :is(code, th, .flow-node, .trace-row .line) { background: var(--control-hover); }
  html[data-theme="dark"] :is(code, th, summary, .score-big) { color: var(--green); }
  html[data-theme="dark"] :is(th, td, .flow-node) { border-color: var(--line); }
  html[data-theme="dark"] :is(pre, .terminal, video) { background: var(--terminal); }
  html[data-theme="dark"] pre code { background: none; color: inherit; }
  html[data-theme="dark"] :is(details, .lab-box, .option, .footer) { background: var(--control); }
  html[data-theme="dark"] .figure-thumb { background: var(--green-dark); }
  html[data-theme="dark"] dialog::backdrop { background: #080f0de0; }

  html[data-theme="dark"] :is(.video-chapters .active, .flow-node.on, .header-field[aria-pressed="true"], .option:has(input:checked), .question-nav .answered) {
    background: var(--control-selected);
    border-color: #84b49b;
  }

  html[data-theme="dark"] :is(.trace-row.last, .lab-status) {
    background: var(--control-selected);
    color: var(--success);
  }

  html[data-theme="dark"] .trace-row.last .line { background: var(--mint); }
  html[data-theme="dark"] .lab-status.error { background: #462b28; color: var(--danger); }
  html[data-theme="dark"] .bits span { background: var(--mint); }
  html[data-theme="dark"] .bits span.host-bit { background: var(--blue); }
  html[data-theme="dark"] .option:hover { background: var(--control-hover); }
  html[data-theme="dark"] .option:has(input:checked) { background: var(--control-selected); }
  html[data-theme="dark"] .option input { accent-color: var(--green); }
  html[data-theme="dark"] .question-nav [aria-current="step"] { outline-color: var(--focus); }
  html[data-theme="dark"] .question-nav .flagged { text-decoration-color: #ffb67d; }
  html[data-theme="dark"] .exam-meter { background: var(--line); }
  html[data-theme="dark"] .exam-meter i { background: var(--green); }
  html[data-theme="dark"] .correct { color: var(--success); }
  html[data-theme="dark"] .incorrect { color: var(--danger); }
}
