@media screen {
  :root {
    --theme-topbar-bg: rgb(246 245 239 / 97%);
    --theme-selection-bg: #dae6cc;
    --theme-selection-ink: #0f352b;
    --theme-lead: #506359;
    --theme-border-hover: #b9c9b9;
    --theme-blue: #1f4f7a;
    --theme-blue-soft: #e6eef6;
    --theme-badge-border: #c7d6c1;
    --theme-score-bg: #eef4e7;
    --theme-warning-ink: #60491f;
    --theme-warning-border: #e6d3aa;
    --theme-warning-edge: #aa7529;
    --theme-control-border: #bdc8bb;
    --theme-placeholder: #778279;
    --theme-empty-border: #b5c4b0;
    --theme-button-ink: #fffefa;
    --theme-button-hover-ink: #fff;
    --theme-skip-ink: #fff;
    --theme-table-hover: #f7f9f2;
    --theme-deep-bg: #dfeedd;
    --theme-source-underline: #afbcad;
    --theme-info-border: #8fa99a;
    --theme-popover-shadow: 0 12px 36px rgb(22 49 37 / 17%);
    --theme-dialog-shadow: 0 18px 60px rgb(12 29 22 / 24%);
    --theme-dialog-backdrop: rgb(16 38 29 / 62%);
    --theme-card-hover-shadow: 0 8px 24px rgb(31 50 37 / 8%);
    --theme-image-mat: transparent;
    --theme-concept: #285f92;
    --theme-method: #76549a;
    --theme-number: #267452;
  }

  :root[data-theme="dark"] {
    color-scheme: dark;
    --paper: #141c19;
    --surface: #1c2621;
    --surface-soft: #253129;
    --ink: #e5e9e2;
    --muted: #abb8ad;
    --green: #a2d5b8;
    --green-dark: #c4e3ce;
    --green-soft: #2a4234;
    --accent: #e5ad86;
    --accent-soft: #3b2c23;
    --line: #445047;
    --warning: #e8c47f;
    --warning-soft: #3d3320;
    --shadow: 0 5px 22px rgb(0 0 0 / 18%);
    --theme-topbar-bg: rgb(20 28 25 / 97%);
    --theme-selection-bg: #3b5d44;
    --theme-selection-ink: #e5e9e2;
    --theme-lead: #bdcabe;
    --theme-border-hover: #738b78;
    --theme-blue: #a9ceef;
    --theme-blue-soft: #273a4d;
    --theme-badge-border: #617661;
    --theme-score-bg: #2b3d2c;
    --theme-warning-ink: #e2c99c;
    --theme-warning-border: #796741;
    --theme-warning-edge: #b79862;
    --theme-control-border: #627567;
    --theme-placeholder: #99ab9d;
    --theme-empty-border: #657361;
    --theme-button-ink: #102619;
    --theme-button-hover-ink: #102619;
    --theme-skip-ink: #102619;
    --theme-table-hover: #293a2f;
    --theme-deep-bg: #2b4334;
    --theme-source-underline: #677f6c;
    --theme-info-border: #91ae9a;
    --theme-popover-shadow: 0 12px 36px rgb(0 0 0 / 35%);
    --theme-dialog-shadow: 0 18px 60px rgb(0 0 0 / 45%);
    --theme-dialog-backdrop: rgb(4 10 7 / 76%);
    --theme-card-hover-shadow: 0 8px 24px rgb(0 0 0 / 24%);
    --theme-image-mat: #fff;
    --theme-concept: #9dcaf1;
    --theme-method: #ccb3e9;
    --theme-number: #9bd5b4;
  }

  ::selection { color: var(--theme-selection-ink); background: var(--theme-selection-bg); }
  .topbar { background: var(--theme-topbar-bg); }
  .lead { color: var(--theme-lead); }
  .card:hover, .reading-site .adjacent-note:hover { border-color: var(--theme-border-hover); }
  .badge.track, .decision.d-index { color: var(--theme-blue); background: var(--theme-blue-soft); }
  .sources.chips a { border-color: var(--theme-badge-border); }
  .score { border-color: var(--theme-badge-border); background: var(--theme-score-bg); }
  .warning { color: var(--theme-warning-ink); border-color: var(--theme-warning-border); border-left-color: var(--theme-warning-edge); }
  .toolbar input, .toolbar select { border-color: var(--theme-control-border); }
  .toolbar input::placeholder { color: var(--theme-placeholder); }
  .empty-state { border-color: var(--theme-empty-border); }
  .button { color: var(--theme-button-ink); }
  .button:hover { color: var(--theme-button-hover-ink); }
  .button.secondary, .button.secondary:hover { color: var(--green); }
  .skip-link, .skip-link:hover { color: var(--theme-skip-ink); }
  .data-table tbody tr:hover td { background: var(--theme-table-hover); }
  .decision.d-deep { background: var(--theme-deep-bg); }
  .sources a { text-decoration-color: var(--theme-source-underline); }
  .reading-site {
    --concept: var(--theme-concept);
    --method: var(--theme-method);
    --number: var(--theme-number);
  }
  .reading-site .info-trigger { border-color: var(--theme-info-border); }
  .reading-site .term-card { box-shadow: var(--theme-popover-shadow); }
  .reading-site .figure-dialog { box-shadow: var(--theme-dialog-shadow); }
  .reading-site .figure-dialog::backdrop { background: var(--theme-dialog-backdrop); }
  .reading-site .figure-view img, .reading-site .zoom-scroll img { background: var(--theme-image-mat); }
  .reading-site .mark-read[aria-pressed="true"],
  .reading-site .mark-read[aria-pressed="true"]:hover,
  .reading-site .toolbar [aria-pressed="true"],
  .reading-site .toolbar [aria-pressed="true"]:hover { color: var(--theme-button-ink); }

  .topbar-tools { display: flex; align-items: center; flex-wrap: wrap; justify-content: flex-end; gap: .25rem .65rem; flex: 0 0 auto; max-width: 300px; }
  .topbar-tools .edition { color: var(--muted); font-size: .71rem; white-space: nowrap; }
  .theme-control { position: relative; flex: 0 0 auto; }
  .theme-control[hidden], .theme-menu[hidden] { display: none; }
  .theme-toggle { display: inline-flex; align-items: center; justify-content: center; gap: .4rem; min-width: 40px; height: 40px; padding: 0 .65rem; border: 1px solid transparent; border-radius: 20px; color: var(--muted); background: transparent; cursor: pointer; font: inherit; font-size: .78rem; white-space: nowrap; }
  .wiki-site .theme-control .theme-toggle { width: auto; flex-basis: auto; min-width: 32px; height: 32px; }
  .theme-toggle:hover, .theme-toggle[aria-expanded="true"] { color: var(--green-dark); background: var(--green-soft); }
  .theme-toggle:focus-visible, .theme-menu button:focus-visible { outline: 2px solid var(--green); outline-offset: 2px; }
  .theme-icon { display: none; flex: 0 0 20px; width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
  :root[data-theme-mode="system"] .theme-icon[data-theme-icon="system"],
  :root[data-theme-mode="light"] .theme-icon[data-theme-icon="light"],
  :root[data-theme-mode="dark"] .theme-icon[data-theme-icon="dark"] { display: block; }
  .theme-menu { position: absolute; z-index: 100; top: calc(100% + .4rem); right: 0; width: 156px; max-width: calc(100vw - 24px); padding: .35rem; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); box-shadow: var(--theme-popover-shadow); }
  .theme-menu button { display: flex; align-items: center; justify-content: space-between; gap: .75rem; width: 100%; min-height: 40px; padding: .4rem .65rem; border: 0; border-radius: 7px; color: var(--ink); background: transparent; cursor: pointer; font: inherit; font-size: .85rem; white-space: nowrap; }
  .theme-menu button:hover, .theme-menu button:focus-visible { background: var(--green-soft); }
  .theme-check { visibility: hidden; color: var(--green); }
  .theme-menu [aria-checked="true"] .theme-check { visibility: visible; }
  .theme-status { flex: 0 0 100%; color: var(--warning); font-size: .68rem; line-height: 1.4; text-align: right; }
  .theme-status:empty { display: none; }
}

@media screen and (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    color-scheme: dark;
    --paper: #141c19;
    --surface: #1c2621;
    --surface-soft: #253129;
    --ink: #e5e9e2;
    --muted: #abb8ad;
    --green: #a2d5b8;
    --green-dark: #c4e3ce;
    --green-soft: #2a4234;
    --accent: #e5ad86;
    --accent-soft: #3b2c23;
    --line: #445047;
    --warning: #e8c47f;
    --warning-soft: #3d3320;
    --shadow: 0 5px 22px rgb(0 0 0 / 18%);
    --theme-topbar-bg: rgb(20 28 25 / 97%);
    --theme-selection-bg: #3b5d44;
    --theme-selection-ink: #e5e9e2;
    --theme-lead: #bdcabe;
    --theme-border-hover: #738b78;
    --theme-blue: #a9ceef;
    --theme-blue-soft: #273a4d;
    --theme-badge-border: #617661;
    --theme-score-bg: #2b3d2c;
    --theme-warning-ink: #e2c99c;
    --theme-warning-border: #796741;
    --theme-warning-edge: #b79862;
    --theme-control-border: #627567;
    --theme-placeholder: #99ab9d;
    --theme-empty-border: #657361;
    --theme-button-ink: #102619;
    --theme-button-hover-ink: #102619;
    --theme-skip-ink: #102619;
    --theme-table-hover: #293a2f;
    --theme-deep-bg: #2b4334;
    --theme-source-underline: #677f6c;
    --theme-info-border: #91ae9a;
    --theme-popover-shadow: 0 12px 36px rgb(0 0 0 / 35%);
    --theme-dialog-shadow: 0 18px 60px rgb(0 0 0 / 45%);
    --theme-dialog-backdrop: rgb(4 10 7 / 76%);
    --theme-card-hover-shadow: 0 8px 24px rgb(0 0 0 / 24%);
    --theme-image-mat: #fff;
    --theme-concept: #9dcaf1;
    --theme-method: #ccb3e9;
    --theme-number: #9bd5b4;
  }
}

@media screen and (max-width: 1280px) {
  .topbar-tools .edition { display: none; }
}

@media screen and (max-width: 800px) {
  .topbar > .nav { order: 3; flex: 1 0 100%; }
  .topbar-tools { order: 2; margin-left: auto; }
  .topbar > .sidebar-toggle { order: 2; }
}

@media screen and (max-width: 500px) {
  html { scroll-padding-top: 156px; }
}

@media screen and (hover: hover) and (pointer: fine) and (prefers-reduced-motion: no-preference) {
  .card:not([aria-disabled="true"]):hover { box-shadow: var(--theme-card-hover-shadow); }
}

@media screen and (max-width: 500px) {
  .theme-toggle [data-theme-label] { display: none; }
  .theme-toggle, .wiki-site .theme-control .theme-toggle { width: 40px; flex-basis: 40px; min-width: 40px; padding: 0; }
}

@media screen and (prefers-reduced-motion: no-preference) {
  .theme-toggle, .theme-menu button { transition: background-color .15s, color .15s; }
}
