:root {
  --field: #d8cfc0;
  --paper: #f7f3ea;
  --ink: #2a241c;
  --muted: #6e6658;
  --faint: #8a8172;
  --line: #c9beab;
  --wash: #ebe3d2;
  --accent: #3d5a4c;
  --accent-soft: #d5e0d8;
  --measure: 58rem;
  --toc: 14rem;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--paper);
  color: var(--ink);
  font: 18.5px/1.7 "Source Serif 4", "Iowan Old Style", Palatino, Georgia, serif;
}

html[lang="zh-Hans"] body {
  font-family: "Noto Serif SC", "Source Han Serif SC", Songti SC, serif;
  font-size: 17.5px;
  line-height: 1.85;
}

a { color: var(--accent); text-underline-offset: 0.15em; }
a:hover { color: var(--ink); }

.shell {
  display: grid;
  grid-template-columns: var(--toc) minmax(0, var(--measure));
  gap: 3rem;
  justify-content: center;
  width: min(calc(var(--toc) + 3rem + var(--measure)), calc(100% - 4rem));
  margin: 2.4rem auto 5rem;
  align-items: start;
}

.toc {
  position: sticky;
  top: 2rem;
  padding-top: 0.35rem;
}
.toc-k {
  margin: 0 0 0.65rem;
  font: 600 11px/1.3 "Source Sans 3", system-ui, sans-serif;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--faint);
}
.toc ol {
  list-style: none;
  margin: 0;
  padding: 0;
  border-left: 1px solid var(--line);
}
.toc li { margin: 0; }
.toc a {
  display: block;
  padding: 0.28rem 0 0.28rem 0.85rem;
  color: var(--muted);
  text-decoration: none;
  font: 13.5px/1.4 "Source Sans 3", system-ui, sans-serif;
  border-left: 2px solid transparent;
  margin-left: -1px;
}
.toc a:hover { color: var(--ink); }
.toc a.on {
  color: var(--accent);
  border-left-color: var(--accent);
  font-weight: 600;
}

.page {
  min-width: 0;
  max-width: 100%;
  padding: 0;
}

.mast {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin: 0 0 1.45rem;
}

.date {
  margin: 0;
  color: var(--muted);
  font: 14px/1.4 "Source Sans 3", system-ui, sans-serif;
}

.lang {
  display: inline-flex;
  padding: 3px;
  background: var(--wash);
  border: 1px solid var(--line);
  border-radius: 999px;
}
.lang a {
  min-width: 4.4rem;
  padding: 0.28rem 0.85rem;
  border-radius: 999px;
  text-align: center;
  text-decoration: none;
  color: var(--muted);
  font: 500 13px/1.3 "Source Sans 3", system-ui, sans-serif;
  transition: background 0.15s ease, color 0.15s ease;
}
.lang a:hover { color: var(--ink); }
.lang a.on {
  background: var(--accent);
  color: var(--paper);
}

h1 {
  font-size: 1.9rem;
  line-height: 1.25;
  font-weight: 600;
  margin: 0 0 0.35rem;
  overflow-wrap: anywhere;
}

.byline {
  margin: 0 0 1.5rem;
  color: var(--muted);
  font: 15px/1.4 "Source Sans 3", system-ui, sans-serif;
}

article { min-width: 0; }

article p {
  text-align: justify;
  text-align-last: start;
}

html[lang="en"] article p {
  text-justify: inter-word;
  hyphens: auto;
  -webkit-hyphens: auto;
  hyphenate-limit-chars: 6 3 2;
  overflow-wrap: break-word;
}

html[lang="zh-Hans"] article p {
  text-justify: inter-character;
  line-break: loose;
  word-break: break-word;
}

article .byline,
article .see-note {
  text-align: start;
  text-justify: auto;
  hyphens: none;
  -webkit-hyphens: none;
  line-break: auto;
  word-break: normal;
}

h2 {
  font-size: 1.3rem;
  line-height: 1.3;
  font-weight: 600;
  margin: 2.5rem 0 0.8rem;
  scroll-margin-top: 1.4rem;
  overflow-wrap: anywhere;
}

p { margin: 0 0 1.05rem; }

.math-block {
  margin: 1.2rem 0 1.3rem;
  max-width: 100%;
  overflow-x: auto;
  text-align: center;
}
.katex-display {
  margin: 0.2rem 0 !important;
  overflow-x: auto;
  overflow-y: hidden;
}

.cite {
  font: 500 0.72em/0 "Source Sans 3", system-ui, sans-serif;
  vertical-align: super;
  text-decoration: none;
  color: var(--accent);
}
.cite:hover { color: var(--ink); }

.cite-tip {
  position: fixed;
  z-index: 20;
  max-width: 22rem;
  padding: 0.5rem 0.65rem;
  background: var(--ink);
  color: var(--paper);
  border-radius: 6px;
  font: 13px/1.45 "Source Sans 3", system-ui, sans-serif;
  pointer-events: none;
  opacity: 0;
}
.cite-tip.on { opacity: 1; }

.refs { font-size: 15px; line-height: 1.5; }
.refs ol { padding-left: 1.25rem; }
.refs li { margin: 0.4rem 0; scroll-margin-top: 1.2rem; }
.refs li.flash { background: var(--accent-soft); outline: 6px solid var(--accent-soft); }
.refs a { overflow-wrap: anywhere; }

.credit {
  margin: 2.4rem 0 0;
  padding-top: 0.9rem;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font: 13.5px/1.5 "Source Sans 3", system-ui, sans-serif;
}
.credit a { color: var(--muted); }

.bib { position: relative; min-width: 0; }
.bib pre {
  margin: 0;
  padding: 0.9rem 4.4rem 0.9rem 1rem;
  background: var(--wash);
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow-x: auto;
  font: 12.5px/1.55 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  white-space: pre;
}
.bib-copy {
  position: absolute;
  top: 0.5rem;
  right: 0.55rem;
  appearance: none;
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--muted);
  border-radius: 999px;
  padding: 0.2rem 0.6rem;
  font: 500 12px/1 "Source Sans 3", system-ui, sans-serif;
  cursor: pointer;
}
.bib-copy:hover { color: var(--accent); border-color: var(--accent); }

.fig {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  margin: 1.55rem 0 1.65rem;
}

.see-toggle {
  display: inline-flex;
  padding: 3px;
  margin: 0 0 0.75rem;
  background: var(--wash);
  border: 1px solid var(--line);
  border-radius: 999px;
}
.see-toggle button {
  appearance: none;
  border: 0;
  background: none;
  min-width: 3.6rem;
  padding: 0.26rem 0.8rem;
  border-radius: 999px;
  color: var(--muted);
  cursor: pointer;
  font: 500 13px/1.3 "Source Sans 3", system-ui, sans-serif;
}
.see-toggle button:hover { color: var(--ink); }
.see-toggle button.on {
  background: var(--accent);
  color: var(--paper);
}

.see-axis {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}
.see-axis .lab {
  width: 0.9rem;
  flex: 0 0 auto;
  font: 500 12px/1 "Source Sans 3", system-ui, sans-serif;
  color: var(--faint);
}
.frames {
  display: flex;
  gap: 0.28rem;
  flex: 1;
  min-width: 0;
}
.frame {
  flex: 1 1 0;
  min-width: 0;
  aspect-ratio: 1;
  border-radius: 4px;
  border: 1px solid var(--line);
  background: var(--paper);
  position: relative;
}
.frame span {
  position: absolute;
  inset: auto 0 2px;
  text-align: center;
  font: 500 10px/1 "Source Sans 3", system-ui, sans-serif;
  color: var(--faint);
}
.frame.past { background: var(--accent-soft); border-color: #b7c4bb; }
.frame.now { background: var(--accent); border-color: var(--accent); }
.frame.now span { color: var(--paper); }
.frame.future { opacity: 0.45; }
.fig-see[data-mode="bi"] .frame.future {
  background: var(--accent-soft);
  border-color: #b7c4bb;
  opacity: 1;
}

.see-note {
  margin: 0.55rem 0 0;
  font: 14px/1.5 "Source Sans 3", system-ui, sans-serif;
  color: var(--muted);
}

.code-cols {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
  min-width: 0;
}
.code-col { min-width: 0; }
.code-col header {
  font: 600 12px/1.2 "Source Sans 3", system-ui, sans-serif;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 0.35rem;
}
.code-col.native header { color: var(--accent); }
.code-col pre {
  margin: 0;
  padding: 0.9rem 1.05rem;
  background: var(--wash);
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow-x: auto;
  font: 13.5px/1.6 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  white-space: pre;
}
.code-col.native pre {
  background: var(--accent-soft);
  border-color: #b7c4bb;
}

.stack {
  list-style: none;
  margin: 0;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: var(--paper);
}
.stack li {
  display: grid;
  grid-template-columns: 5rem minmax(0, 1fr);
  gap: 0.35rem 0.85rem;
  padding: 0.62rem 0.85rem;
  cursor: pointer;
  border-bottom: 1px solid var(--line);
}
.stack li:last-child { border-bottom: 0; }
.stack li:hover { background: var(--wash); }
.stack li.on { background: var(--accent-soft); }
.stack .k {
  font: 600 12px/1.45 "Source Sans 3", system-ui, sans-serif;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--accent);
}
.stack .v {
  font: 15px/1.45 "Source Sans 3", system-ui, sans-serif;
  overflow-wrap: anywhere;
}
.stack .why {
  grid-column: 2;
  margin: 0;
  font: 13.5px/1.45 "Source Sans 3", system-ui, sans-serif;
  color: var(--muted);
  display: none;
}
.stack li.on .why { display: block; }

.matrix-wrap {
  max-width: 100%;
  overflow-x: auto;
}
.matrix {
  width: 100%;
  min-width: 28rem;
  border-collapse: collapse;
  font: 15px/1.45 "Source Sans 3", system-ui, sans-serif;
}
.matrix th,
.matrix td {
  padding: 0.55rem 0.7rem 0.55rem 0;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}
.matrix th {
  font-weight: 600;
  color: var(--muted);
  font-size: 12px;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}
.matrix td.up { color: var(--accent); font-weight: 600; }
.matrix td.flat { color: var(--muted); }

@media (max-width: 1100px) {
  .shell {
    grid-template-columns: minmax(0, var(--measure));
    width: min(var(--measure), calc(100% - 3rem));
  }
  .toc { display: none; }
}

@media (max-width: 640px) {
  h1 { font-size: 1.55rem; }
  .shell { width: calc(100% - 2rem); margin: 1.2rem auto 3rem; }
  .stack li { grid-template-columns: 1fr; }
  .stack .why { grid-column: 1; }
  .matrix { min-width: 22rem; }
}

@media (max-width: 520px) {
  body { font-size: 16.5px; }
  .shell { width: calc(100% - 1.5rem); }
  .frame span { display: none; }
}
