:root {
  color-scheme: light dark;
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #15191e;
  --muted: #586270;
  --border: #d9dee4;
  --accent: #1d5ca6;
  --accent-text: #ffffff;
  --term: #1d5ca6;
  --term-bg: rgba(29, 92, 166, 0.08);
  --rail: #b9c3cf;
  --error: #b42318;
  --shadow: 0 -8px 32px rgba(15, 20, 30, 0.18);
  --dg-metal: #c7ced6;
  --dg-steel: #75808d;
  --dg-steel-dark: #4b5560;
  --dg-line: #3a434e;
  --dg-hot: #e8743b;
  --dg-cool: #3f86d4;
  --dg-paint: #2f6f8f;
  --dg-anod: #9a7a52;
  --dg-wood: #b8875a;
  --dg-grain: #7c5531;
  --tabbar-h: 60px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101317;
    --surface: #1a1e24;
    --text: #e9ecf0;
    --muted: #9ca6b2;
    --border: #2c323b;
    --accent: #79aef5;
    --accent-text: #0c1a2c;
    --term: #8dbaf7;
    --term-bg: rgba(141, 186, 247, 0.12);
    --rail: #3a424d;
    --error: #ff8a80;
    --shadow: 0 -8px 32px rgba(0, 0, 0, 0.5);
    --dg-metal: #9aa4af;
    --dg-steel: #59636f;
    --dg-steel-dark: #3c444e;
    --dg-line: #c3cad2;
    --dg-hot: #f0884f;
    --dg-cool: #6aa8ec;
    --dg-paint: #4d93b5;
    --dg-anod: #b08f63;
    --dg-wood: #a87a4f;
    --dg-grain: #6a4728;
  }
}

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

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html:has(dialog[open]) {
  overflow: hidden;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 400 1.0625rem/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 24px);
  overflow-wrap: break-word;
}

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

h1,
h2 {
  line-height: 1.2;
  text-wrap: balance;
}

h1 {
  font-size: 1.75rem;
  margin: 8px 0 12px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* Top bar */

.topbar {
  padding: calc(env(safe-area-inset-top, 0px) + 12px) 16px 4px;
}

.brand {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
  letter-spacing: 0.01em;
}

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 16px;
}

.lede {
  font-size: 1.125rem;
  color: var(--text);
}

.hint,
.muted,
.eyebrow {
  color: var(--muted);
}

.eyebrow {
  margin: 8px 0 0;
  font-size: 0.9375rem;
  font-weight: 600;
}

/* Bottom tab bar, within thumb reach */

.tabbar {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 10;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.tabbar a {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.9375rem;
  text-decoration: none;
}

.tabbar a[aria-current="page"] {
  color: var(--accent);
  box-shadow: inset 0 3px 0 var(--accent);
}

/* Glossary terms */

.term {
  position: relative;
  display: inline;
  margin: 0;
  padding: 0 1px;
  border: 0;
  border-radius: 3px;
  background: var(--term-bg);
  color: var(--term);
  font: inherit;
  text-decoration: underline dotted 2px;
  text-underline-offset: 3px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

/* Expands the tap area to at least 44px tall without changing line layout. */
.term::after {
  content: "";
  position: absolute;
  inset: -11px -3px;
}

.term:focus-visible,
a:focus-visible,
button:focus-visible,
input:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.term-sample {
  color: var(--term);
  text-decoration: underline dotted 2px;
  text-underline-offset: 3px;
}

/* Process map: vertical, S1 at top */

.map {
  list-style: none;
  margin: 24px 0 0;
  padding: 0;
}

.stage {
  position: relative;
  display: grid;
  grid-template-columns: 44px 1fr;
  gap: 12px;
  padding-bottom: 16px;
}

.stage::before {
  content: "";
  position: absolute;
  top: 44px;
  bottom: 0;
  left: 21px;
  width: 2px;
  background: var(--rail);
}

.stage:last-child::before {
  display: none;
}

.stage-code {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 700;
  font-size: 0.875rem;
}

.stage-card {
  min-width: 0;
  padding: 12px 16px 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.stage-card h2 {
  margin: 0 0 4px;
  font-size: 1.1875rem;
}

.stage-card p {
  margin: 0 0 4px;
}

.stage-link,
.back,
.pager a {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  font-weight: 600;
}

/* Stage pages */

.placeholder {
  margin: 24px 0;
  padding: 16px;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  color: var(--muted);
}

.placeholder p {
  margin: 0;
}

.pager {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 8px 16px;
  border-top: 1px solid var(--border);
  padding-top: 8px;
}

.pager-next {
  margin-left: auto;
  text-align: right;
}

/* Deep dive pages */

main h2 {
  margin: 28px 0 8px;
  font-size: 1.25rem;
}

.draft-note {
  display: inline-block;
  margin: 0;
  padding: 2px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.875rem;
  font-weight: 600;
}

.points {
  margin: 0;
  padding-left: 1.25em;
}

.points li {
  margin: 4px 0;
}

.diagram {
  margin: 20px 0 8px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.diagram figcaption {
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.9375rem;
}

.dg {
  display: block;
  width: 100%;
  height: auto;
}

.dg text {
  fill: var(--text);
  font: 600 14px system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.dg .dg-sm {
  font-size: 12.5px;
}

.dg .dg-muted {
  fill: var(--muted);
  font-weight: 500;
}

.dg-metal { fill: var(--dg-metal); stroke: var(--dg-line); stroke-width: 1.5; }
.dg-steel { fill: var(--dg-steel); stroke: var(--dg-line); stroke-width: 1.5; }
.dg-steel-dark { fill: var(--dg-steel-dark); stroke: var(--dg-line); stroke-width: 1.5; }
.dg-hot { fill: var(--dg-hot); stroke: var(--dg-line); stroke-width: 1.5; }
.dg-hole { fill: var(--surface); stroke: var(--dg-line); stroke-width: 1.5; }
.dg-outline { fill: var(--bg); stroke: var(--dg-line); stroke-width: 2; }
.dg-outline-only { fill: none; stroke: var(--dg-line); stroke-width: 2; }
.dg-edge { stroke: var(--dg-line); stroke-width: 1.5; }
.dg-paint { fill: var(--dg-paint); }
.dg-anod { fill: var(--dg-anod); }
.dg-wood { fill: var(--dg-wood); }
.dg-scrap { fill: var(--dg-steel); }
.dg-tag { fill: var(--surface); stroke: var(--dg-line); stroke-width: 1.5; }
.dg-arrow { stroke: var(--accent); stroke-width: 3; fill: none; }
.dg-ah { fill: var(--accent); }
.dg-cut { stroke: var(--accent); stroke-width: 2; stroke-dasharray: 5 4; }
.dg-heat { stroke: var(--dg-hot); stroke-width: 3; fill: none; stroke-linecap: round; }
.dg-spray { stroke: var(--dg-cool); stroke-width: 2; fill: none; stroke-linecap: round; }
.dg-before { stroke: var(--muted); stroke-width: 2; stroke-dasharray: 6 5; fill: none; }
.dg-bracket { stroke: var(--muted); stroke-width: 1.5; fill: none; }
.dg-lines { stroke: var(--dg-line); stroke-width: 1; opacity: 0.4; fill: none; }
.dg-grain { stroke: var(--dg-grain); stroke-width: 2; fill: none; }
.dg-band { stroke: var(--accent); stroke-width: 4; }
.dg-hands { stroke: var(--dg-line); stroke-width: 3; stroke-linecap: round; fill: none; }
.dg-stop-hot { stop-color: var(--dg-hot); }
.dg-stop-metal { stop-color: var(--dg-metal); }

/* Glossary page */

.glossary-list h2 {
  margin: 24px 0 8px;
  font-size: 1rem;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  padding-bottom: 4px;
}

.glossary-list dl {
  margin: 0;
}

.glossary-entry {
  padding: 8px 0 12px;
  scroll-margin-top: 16px;
}

.glossary-entry dt {
  font-weight: 700;
  font-size: 1.125rem;
}

.glossary-entry dd {
  margin: 4px 0 0;
}

.glossary-entry:target dt {
  color: var(--accent);
}

.draft-tag {
  display: inline-block;
  margin-left: 8px;
  padding: 0 6px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
  vertical-align: middle;
}

/* Forms */

.login {
  display: grid;
  gap: 8px;
  margin-top: 16px;
}

.login label {
  font-weight: 600;
}

.login input {
  min-height: 48px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
}

.button {
  min-height: 48px;
  margin-top: 8px;
  padding: 10px 20px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.button-secondary {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--border);
}

.error {
  color: var(--error);
  font-weight: 600;
}

/* Bottom sheet */

.sheet {
  width: 100%;
  max-width: 640px;
  max-height: 80vh;
  margin: auto auto 0;
  padding: 0;
  border: 0;
  border-radius: 20px 20px 0 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.sheet::backdrop {
  background: rgba(0, 0, 0, 0.4);
}

.sheet[open] {
  display: flex;
  flex-direction: column;
  animation: sheet-in 220ms ease-out;
}

@keyframes sheet-in {
  from {
    transform: translateY(100%);
  }
}

.sheet-head {
  display: grid;
  grid-template-columns: 1fr 44px;
  align-items: center;
  padding: 8px 8px 0 20px;
  touch-action: none;
}

.sheet-grip {
  grid-column: 1 / -1;
  justify-self: center;
  width: 40px;
  height: 5px;
  margin-bottom: 4px;
  border-radius: 3px;
  background: var(--border);
}

.sheet-head h2 {
  margin: 0;
  font-size: 1.375rem;
}

.sheet-close {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font-size: 1.75rem;
  line-height: 1;
  cursor: pointer;
}

.sheet-body {
  padding: 4px 20px calc(20px + var(--safe-bottom));
  overflow-y: auto;
  overscroll-behavior: contain;
}

.sheet-body img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 8px 0;
  border-radius: 10px;
}

.sheet-def {
  margin: 8px 0;
  font-size: 1.0625rem;
}

.sheet-more {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  font-weight: 600;
}

@media (prefers-reduced-motion: reduce) {
  .sheet[open] {
    animation: none;
  }
}

@media (min-width: 700px) {
  h1 {
    font-size: 2rem;
  }

  .tabbar {
    grid-auto-columns: 160px;
    justify-content: center;
  }
}
