:root {
  --paper: #f4efe3;
  --paper-deep: #e8e0cc;
  --ink: #14110e;
  --ink-soft: #2a241c;
  --muted: #534c42;
  --rule: #7a241c;
  --rule-soft: #a34438;
  --line: #bfb49a;
  --figure: #ebe3d0;
  --teacher: #8f2d22;
  --implementer: #2c4a5e;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
  --serif: "Newsreader", "Iowan Old Style", "Palatino Linotype", Palatino, serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .view,
  .radar-poly,
  .mast-nav a::after {
    transition: none !important;
    animation: none !important;
  }
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: var(--paper);
  font-family: var(--serif);
  font-size: 1.125rem;
  font-optical-sizing: auto;
  line-height: 1.55;
  font-feature-settings: "lnum", "kern";
}

.grain {
  pointer-events: none;
  position: fixed;
  inset: 0;
  z-index: 8;
  opacity: 0.07;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.55'/%3E%3C/svg%3E");
}

.skip {
  position: absolute;
  left: 1rem;
  top: -4rem;
  z-index: 12;
  padding: 0.4rem 0.7rem;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--mono);
  font-size: 0.75rem;
}

.skip:focus {
  top: 1rem;
}

a {
  color: var(--rule);
  text-decoration-thickness: 1px;
  text-underline-offset: 0.18em;
}

a:hover {
  color: var(--ink);
}

:focus-visible {
  outline: 2px solid var(--rule);
  outline-offset: 3px;
}

.mast {
  position: sticky;
  top: 0;
  z-index: 6;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0.75rem 1.5rem;
  padding: 0.85rem 1.25rem;
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(8px);
}

.mast-mark {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  color: inherit;
  text-decoration: none;
}

.mast-name {
  font-weight: 600;
  letter-spacing: -0.02em;
}

.mast-status {
  font-family: var(--mono);
  font-size: 0.68rem;
  color: var(--muted);
  letter-spacing: 0.02em;
}

.mast-nav {
  display: flex;
  align-items: center;
  gap: 1.1rem;
  font-family: var(--mono);
  font-size: 0.78rem;
}

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

.mast-nav a:hover,
.mast-nav a[aria-current="page"] {
  color: var(--ink);
}

.mast-nav a[aria-current="page"]::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: -0.35rem;
  height: 1px;
  background: var(--rule);
}

main {
  width: min(42rem, calc(100% - 2.5rem));
  margin: 0 auto;
  padding: 2.75rem 0 5rem;
}

.view[hidden] {
  display: none;
}

.view:not([hidden]) {
  animation: enter 420ms ease both;
}

@keyframes enter {
  from {
    opacity: 0;
    transform: translateY(0.45rem);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.kicker {
  margin: 0 0 0.6rem;
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--rule);
}

h1 {
  margin: 0 0 0.85rem;
  color: var(--ink);
  font-size: clamp(2.4rem, 7vw, 4.1rem);
  font-weight: 600;
  line-height: 0.95;
  letter-spacing: -0.035em;
}

.lede {
  margin: 0 0 2.4rem;
  max-width: 34rem;
  color: var(--ink-soft);
  font-size: 1.2rem;
  font-style: italic;
}

.abstract {
  padding: 1.35rem 0 0.4rem;
  border-top: 1px solid var(--ink);
}

.abstract h2 {
  margin: 0 0 0.85rem;
  font-family: var(--mono);
  font-size: 0.72rem;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.abstract p {
  margin: 0 0 0.9rem;
  color: var(--ink-soft);
}

.abstract p:last-child {
  margin-bottom: 0;
}

.contents {
  list-style: none;
  margin: 2.6rem 0 0;
  padding: 0;
  border-top: 1px solid var(--line);
}

.contents li {
  border-bottom: 1px solid var(--line);
}

.contents a {
  display: grid;
  grid-template-columns: 3rem 1fr;
  gap: 0.8rem;
  padding: 1.05rem 0;
  color: inherit;
  text-decoration: none;
}

.contents a:hover strong {
  color: var(--rule);
}

.contents-num {
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--rule);
  padding-top: 0.2rem;
}

.contents-body {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.contents-body strong {
  font-size: 1.25rem;
  font-weight: 550;
}

.contents-body span {
  color: var(--muted);
  font-size: 0.98rem;
}

.chapter-head {
  margin-bottom: 2rem;
  padding-bottom: 1.1rem;
  border-bottom: 1px solid var(--ink);
}

.chapter-num {
  margin: 0 0 0.35rem;
  font-family: var(--mono);
  font-size: 0.75rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--rule);
}

.paper h1 {
  font-size: clamp(2.1rem, 5vw, 3rem);
  margin-bottom: 0;
}

.paper h2 {
  margin: 2.1rem 0 0.7rem;
  font-size: 1.15rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.paper p,
.paper li,
.paper dd {
  color: var(--ink-soft);
}

.paper p {
  margin: 0 0 0.95rem;
}

.defs {
  display: grid;
  gap: 1rem;
  margin: 1.1rem 0 1.4rem;
}

.defs.compact {
  gap: 0.75rem;
}

.defs > div {
  display: grid;
  grid-template-columns: 8.2rem 1fr;
  gap: 0.9rem;
  padding: 0.85rem 0;
  border-top: 1px solid var(--line);
}

.defs.compact > div {
  grid-template-columns: 7.4rem 1fr;
  padding: 0.65rem 0;
}

.defs dt {
  margin: 0;
  font-family: var(--mono);
  font-size: 0.72rem;
  font-weight: 500;
  letter-spacing: 0.04em;
  color: var(--rule);
}

.defs dd {
  margin: 0;
}

.invariants,
.procedure,
.not-yet {
  margin: 0.6rem 0 1.3rem;
  padding: 0;
}

.invariants {
  list-style: none;
  counter-reset: inv;
}

.invariants li {
  position: relative;
  padding: 0.7rem 0 0.7rem 2.1rem;
  border-top: 1px solid var(--line);
  counter-increment: inv;
}

.invariants li::before {
  content: counter(inv);
  position: absolute;
  left: 0;
  top: 0.78rem;
  font-family: var(--mono);
  font-size: 0.7rem;
  color: var(--rule);
}

.procedure {
  list-style: none;
  counter-reset: step;
}

.procedure li {
  display: grid;
  gap: 0.25rem;
  padding: 0.95rem 0 0.95rem 2.2rem;
  border-top: 1px solid var(--line);
  counter-increment: step;
  position: relative;
}

.procedure li::before {
  content: counter(step);
  position: absolute;
  left: 0;
  top: 1.05rem;
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--rule);
}

.procedure strong {
  color: var(--ink);
}

.not-yet {
  padding-left: 1.1rem;
}

.not-yet li {
  margin: 0.4rem 0;
}

.loop {
  font-family: var(--mono);
  font-size: 0.95rem;
  color: var(--ink) !important;
  letter-spacing: 0.01em;
}

.figure {
  margin: 1.6rem 0 2rem;
  padding: 1rem 1rem 1.15rem;
  background: var(--figure);
  border: 1px solid var(--line);
}

.fig-label {
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--rule);
  margin-right: 0.35rem;
}

.figure figcaption {
  margin: 0 0 0.85rem;
  color: var(--ink-soft);
  font-size: 0.98rem;
}

.figure-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.7rem;
}

.figure-tools button {
  appearance: none;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  padding: 0.35rem 0.6rem;
  cursor: pointer;
}

.figure-tools button:hover,
.figure-tools button[aria-pressed="true"] {
  border-color: var(--ink);
  color: var(--ink);
}

.figure-tools button[aria-pressed="true"] {
  background: var(--paper);
}

.radar-wrap {
  background: var(--paper);
  border: 1px solid var(--line);
  padding: 0.4rem 0.4rem 0;
}

.radar {
  display: block;
  width: min(100%, 22rem);
  margin: 0 auto;
}

.radar-grid path {
  stroke: var(--line);
  stroke-width: 1;
}

.radar-axes line {
  stroke: var(--line);
  stroke-width: 1;
}

.radar-labels text {
  fill: var(--muted);
  font-family: var(--mono);
  font-size: 9px;
  letter-spacing: 0.04em;
}

.radar-poly {
  stroke-width: 1.6;
  transition: opacity 200ms ease;
}

.radar-poly.teacher {
  stroke: var(--teacher);
  fill: color-mix(in srgb, var(--teacher) 16%, transparent);
}

.radar-poly.implementer {
  stroke: var(--implementer);
  fill: color-mix(in srgb, var(--implementer) 16%, transparent);
}

.radar-poly.is-off {
  opacity: 0;
}

.radar-readout {
  margin: 0;
  padding: 0.45rem 0.65rem 0.55rem;
  font-family: var(--mono);
  font-size: 0.68rem;
  color: var(--muted);
  border-top: 1px solid var(--line);
}

.mono {
  color: var(--ink);
}

.colophon {
  width: min(42rem, calc(100% - 2.5rem));
  margin: 0 auto 2.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--line);
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0.4rem 1rem;
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--muted);
}

.colophon p {
  margin: 0;
}

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

.colophon a:hover {
  color: var(--rule);
}

@media (max-width: 640px) {
  body { font-size: 1.05rem; }
  .mast { padding: 0.75rem 1rem; }
  main { width: min(42rem, calc(100% - 2rem)); padding-top: 1.8rem; }
  .defs > div,
  .defs.compact > div {
    grid-template-columns: 1fr;
    gap: 0.25rem;
  }
  .contents a { grid-template-columns: 2.4rem 1fr; }
}
