:root {
  color-scheme: light;
  --bg: #f8fafc;
  --surface: #ffffff;
  --text: #142033;
  --muted: #526173;
  --border: #d7dee8;
  --accent: #0f9f6e;
  --link: #2563eb;
  --code-bg: #eef3f8;
  --code-surface: #f6f8fb;
  --code-text: #203040;
  --code-comment: #6b7b8c;
  --code-keyword: #8b3fd1;
  --code-string: #0f766e;
  --code-number: #b45309;
  --code-name: #1d4ed8;
}

* {
  box-sizing: border-box;
}

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

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem clamp(1rem, 4vw, 2.5rem);
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.92);
  position: sticky;
  top: 0;
  z-index: 10;
  backdrop-filter: blur(12px);
}

.site-title {
  color: var(--text);
  font-weight: 700;
  text-decoration: none;
}

.site-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 0.8rem;
  font-size: 0.95rem;
}

a {
  color: var(--link);
}

.site-nav a {
  color: var(--muted);
  text-decoration: none;
}

.book-shell {
  display: grid;
  grid-template-columns: 18rem minmax(0, 1fr);
  min-height: calc(100vh - 58px);
}

.toc-sidebar {
  border-right: 1px solid var(--border);
  background: #ffffff;
}

.toc-nav {
  position: sticky;
  top: 58px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.2rem;
  max-height: calc(100vh - 58px);
  padding: 1.25rem 1rem;
  overflow-y: auto;
}

.toc-root,
.toc-section,
.toc-subsection,
.toc-section summary,
.toc-subsection summary,
.toc-subnav,
.toc-subnav a {
  width: 100%;
  min-width: 0;
}

.toc-heading {
  margin: 0 0 0.45rem;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

.toc-root,
.toc-section summary,
.toc-subsection summary,
.toc-subnav a {
  display: block;
  padding: 0.4rem 0.55rem;
  border-radius: 6px;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.3;
  text-decoration: none;
}

.toc-section summary,
.toc-subsection summary {
  cursor: pointer;
  font-weight: 650;
}

.toc-section summary::marker,
.toc-subsection summary::marker {
  color: var(--muted);
}

.toc-section[open] summary,
.toc-section.active summary,
.toc-subsection[open] summary,
.toc-root:hover,
.toc-root.active,
.toc-subnav a:hover,
.toc-subnav a.active {
  background: #eef3f8;
  color: var(--text);
}

.toc-subnav {
  display: flex;
  flex-direction: column;
  gap: 0.08rem;
  margin: 0.25rem 0 0.45rem 0.6rem;
  padding-left: 0.55rem;
  border-left: 1px solid var(--border);
}

.toc-subnav a {
  padding: 0.28rem 0.45rem;
  font-size: 0.84rem;
  overflow-wrap: anywhere;
}

.toc-subsection summary {
  padding: 0.28rem 0.45rem;
  font-size: 0.84rem;
}

.toc-subnav-nested {
  margin-left: 0.45rem;
  padding-left: 0.5rem;
}

.toc-subnav-nested a {
  font-size: 0.8rem;
}

.mjx-container[jax="SVG"] {
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
}

.page-content {
  width: min(100%, 980px);
  margin: 0 auto;
  padding: 2rem clamp(1rem, 4vw, 2.5rem) 4rem;
  background: var(--surface);
}

h1,
h2,
h3 {
  line-height: 1.2;
}

h1 {
  margin-top: 0;
}

img[src*="assets/diagrams/"] {
  display: block;
  width: 100%;
  max-width: 100%;
  height: auto;
  margin: 1.5rem auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #f8fafc;
}

.experiment-nav {
  margin: 1rem 0 1.5rem;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #f8fafc;
  color: var(--muted);
  font-size: 0.95rem;
}

.experiment-nav a {
  font-weight: 600;
  text-decoration: none;
}

table {
  width: 100%;
  border-collapse: collapse;
  display: block;
  overflow-x: auto;
}

th,
td {
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

th {
  background: #f1f5f9;
}

pre,
.highlight {
  overflow-x: auto;
  border-radius: 8px;
  background: var(--code-surface);
}

pre {
  padding: 1rem;
  color: var(--code-text);
}

.highlight {
  margin: 1rem 0;
  border: 1px solid var(--border);
}

.highlight pre {
  margin: 0;
  background: transparent;
}

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.highlight code {
  background: transparent;
}

.highlight .c,
.highlight .cm,
.highlight .cp,
.highlight .c1,
.highlight .cs {
  color: var(--code-comment);
  font-style: italic;
}

.highlight .k,
.highlight .kc,
.highlight .kd,
.highlight .kn,
.highlight .kp,
.highlight .kr,
.highlight .kt,
.highlight .ow {
  color: var(--code-keyword);
  font-weight: 650;
}

.highlight .s,
.highlight .sa,
.highlight .sb,
.highlight .sc,
.highlight .dl,
.highlight .sd,
.highlight .s2,
.highlight .se,
.highlight .sh,
.highlight .si,
.highlight .sx,
.highlight .sr,
.highlight .s1,
.highlight .ss {
  color: var(--code-string);
}

.highlight .m,
.highlight .mb,
.highlight .mf,
.highlight .mh,
.highlight .mi,
.highlight .mo,
.highlight .il {
  color: var(--code-number);
}

.highlight .na,
.highlight .nb,
.highlight .nc,
.highlight .nd,
.highlight .ne,
.highlight .nf,
.highlight .nl,
.highlight .nn,
.highlight .nt,
.highlight .nv,
.highlight .vc,
.highlight .vg,
.highlight .vi,
.highlight .vm {
  color: var(--code-name);
}

.highlight .o,
.highlight .p {
  color: #334155;
}

.highlight .gd {
  color: #b91c1c;
  background: #fee2e2;
}

.highlight .gi {
  color: #166534;
  background: #dcfce7;
}

:not(pre) > code {
  padding: 0.12rem 0.28rem;
  border-radius: 4px;
  background: var(--code-bg);
}

@media (max-width: 640px) {
  .site-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .book-shell {
    display: block;
  }

  .toc-sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }

  .toc-nav {
    position: static;
    display: flex;
    flex-direction: column;
    max-height: none;
    padding: 0.9rem 1rem;
  }

  .page-content {
    padding-top: 1.3rem;
  }
}