:root {
  color-scheme: light;
  --bg: #f8f6f0;
  --surface: #ffffff;
  --surface-2: #f1efe8;
  --text: #22201c;
  --muted: #656057;
  --line: #d9d3c8;
  --accent: #2f7d68;
  --accent-2: #b36b28;
  --accent-soft: #e4f1ec;
  --mark: #fff1a8;
  --code-bg: #20231f;
  --code-text: #f2f0e7;
  --shadow: 0 14px 34px rgba(39, 35, 27, 0.1);
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #171713;
  --surface: #22221d;
  --surface-2: #2b2b24;
  --text: #f1eedf;
  --muted: #b9b3a4;
  --line: #444236;
  --accent: #7cc8ad;
  --accent-2: #e2a35f;
  --accent-soft: #23372f;
  --mark: #6a5418;
  --code-bg: #11130f;
  --code-text: #f8f4df;
  --shadow: 0 18px 40px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.72;
}

a {
  color: var(--accent);
  text-decoration: none;
}

a:hover,
a:focus-visible {
  text-decoration: underline;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.reading-progress {
  position: fixed;
  inset: 0 0 auto 0;
  z-index: 50;
  height: 4px;
  background: transparent;
}

.reading-progress span {
  display: block;
  width: 0;
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width 120ms ease-out;
}

.site-header {
  position: sticky;
  top: 0;
  z-index: 40;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(16px);
}

.header-inner {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) minmax(280px, 620px);
  gap: 22px;
  align-items: center;
  max-width: 1480px;
  margin: 0 auto;
  padding: 16px 24px;
}

.brand-title {
  margin: 0;
  font-size: clamp(1.2rem, 2vw, 1.9rem);
  line-height: 1.2;
  letter-spacing: 0;
}

.brand-subtitle {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.toolbar {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 10px;
  align-items: center;
}

.search-wrap {
  position: relative;
}

.search-wrap input {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 42px 0 14px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.search-wrap input:focus-visible,
button:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 34%, transparent);
  outline-offset: 2px;
}

.clear-search {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  width: 30px;
  height: 30px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

button {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.toolbar button {
  min-height: 42px;
  padding: 0 14px;
  white-space: nowrap;
}

.layout {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  gap: 24px;
  max-width: 1480px;
  margin: 0 auto;
  padding: 24px;
}

.sidebar {
  position: sticky;
  top: 92px;
  align-self: start;
  max-height: calc(100vh - 112px);
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.sidebar-header {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.sidebar-header h2 {
  margin: 0;
  font-size: 1rem;
}

.search-meta {
  margin-top: 6px;
  color: var(--muted);
  font-size: 0.86rem;
}

.toc {
  padding: 10px 10px 18px;
}

.toc ol {
  list-style: none;
  margin: 0;
  padding: 0;
}

.toc li {
  margin: 2px 0;
}

.toc a {
  display: block;
  border-radius: 8px;
  padding: 7px 8px;
  color: var(--text);
  font-size: 0.92rem;
  line-height: 1.35;
}

.toc ol ol {
  margin: 3px 0 10px 12px;
  border-left: 1px solid var(--line);
  padding-left: 8px;
}

.toc ol ol a {
  color: var(--muted);
  font-size: 0.84rem;
}

.toc a.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 700;
  text-decoration: none;
}

.search-results {
  border-top: 1px solid var(--line);
  padding: 12px 16px 16px;
}

.search-results:empty {
  display: none;
}

.search-results a {
  display: block;
  margin: 0 0 8px;
  color: var(--text);
  font-size: 0.88rem;
}

.content {
  min-width: 0;
}

.book-intro,
.complexity-panel,
.chapter {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.book-intro {
  padding: clamp(24px, 4vw, 44px);
}

.book-intro h2 {
  margin: 0 0 12px;
  font-size: clamp(1.7rem, 3vw, 2.8rem);
  line-height: 1.15;
  letter-spacing: 0;
}

.book-intro p {
  max-width: 930px;
}

.meta-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin-top: 24px;
}

.meta-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: var(--surface-2);
}

.meta-card strong {
  display: block;
  margin-bottom: 4px;
}

.meta-card span {
  color: var(--muted);
  font-size: 0.92rem;
}

.complexity-panel {
  margin-top: 24px;
  padding: 22px;
}

.section-title-row,
.chapter-title-row {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
}

.complexity-panel h2,
.chapter h2 {
  margin: 0;
  line-height: 1.22;
  letter-spacing: 0;
}

.table-wrap {
  overflow-x: auto;
  margin-top: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 760px;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 10px 12px;
  text-align: left;
  vertical-align: top;
}

th {
  background: var(--surface-2);
  font-size: 0.88rem;
}

tr:last-child td {
  border-bottom: 0;
}

code,
kbd {
  border-radius: 6px;
  background: var(--surface-2);
  padding: 0.12rem 0.32rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 0.92em;
}

pre {
  overflow-x: auto;
  border-radius: 8px;
  margin: 16px 0;
  padding: 16px;
  background: var(--code-bg);
  color: var(--code-text);
  line-height: 1.55;
}

pre code {
  background: transparent;
  color: inherit;
  padding: 0;
}

.chapter {
  margin-top: 24px;
  overflow: hidden;
}

.chapter-title-row {
  padding: 20px 22px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-2);
}

.chapter-kicker {
  display: block;
  margin-bottom: 3px;
  color: var(--accent-2);
  font-size: 0.82rem;
  font-weight: 700;
}

.chapter-toggle,
.section-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  min-width: 42px;
  height: 38px;
  padding: 0 10px;
}

.chapter-body {
  padding: 0 22px 22px;
}

.chapter.collapsed .chapter-body {
  display: none;
}

.chapter.collapsed .chapter-title-row {
  border-bottom: 0;
}

.chapter-lead {
  margin: 20px 0;
  color: var(--muted);
  font-size: 1.02rem;
}

.lesson {
  scroll-margin-top: 112px;
  border-top: 1px solid var(--line);
  padding: 20px 0;
}

.lesson:first-of-type {
  border-top: 0;
}

.lesson h3 {
  margin: 0;
  font-size: clamp(1.1rem, 1.5vw, 1.45rem);
  line-height: 1.28;
  letter-spacing: 0;
}

.lesson.collapsed .lesson-body {
  display: none;
}

.lesson.is-current {
  margin-left: -12px;
  margin-right: -12px;
  padding-left: 12px;
  padding-right: 12px;
  background: color-mix(in srgb, var(--accent-soft) 55%, transparent);
}

.note,
.gpu-note,
.complexity-note,
.invariant {
  border-left: 4px solid var(--accent);
  border-radius: 8px;
  margin: 16px 0;
  padding: 12px 14px;
  background: var(--accent-soft);
}

.gpu-note {
  border-left-color: var(--accent-2);
}

.note strong,
.gpu-note strong,
.complexity-note strong,
.invariant strong {
  display: block;
  margin-bottom: 4px;
}

.formula {
  display: inline-block;
  border: 1px solid var(--line);
  border-radius: 8px;
  margin: 4px 0;
  padding: 2px 8px;
  background: var(--surface-2);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

.figure-text {
  border: 1px dashed var(--line);
  border-radius: 8px;
  margin: 16px 0;
  padding: 12px;
  background: var(--surface-2);
}

.chapter-nav {
  display: flex;
  gap: 10px;
  justify-content: space-between;
  margin: 24px 0 0;
}

.chapter-nav button {
  min-height: 42px;
  padding: 0 16px;
}

.footer {
  margin: 24px 0 0;
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.92rem;
}

mark {
  border-radius: 4px;
  background: var(--mark);
  color: inherit;
  padding: 0 2px;
}

.hidden-by-search {
  display: none;
}

@media (max-width: 980px) {
  .header-inner,
  .layout {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: relative;
    top: auto;
    max-height: 420px;
  }

  .toolbar {
    grid-template-columns: 1fr auto;
  }

  .toolbar .wide-action {
    grid-column: span 2;
  }

  .meta-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 620px) {
  .header-inner,
  .layout {
    padding-left: 14px;
    padding-right: 14px;
  }

  .toolbar {
    grid-template-columns: 1fr;
  }

  .toolbar .wide-action {
    grid-column: auto;
  }

  .chapter-title-row,
  .section-title-row {
    align-items: flex-start;
  }

  .chapter-nav {
    flex-direction: column;
  }

  .chapter-nav button {
    width: 100%;
  }
}
