/* pin docs: one stylesheet for every page, generated or hand-written.
   Read docs/STYLE.md for the rules. Order: tokens, base, layout, type,
   code, components, reference, tour. Colors come from light-dark(), so each
   token has one definition and follows the reader's color scheme. */

/* ---------- 1. Tokens ---------- */

:root {
  color-scheme: light dark;

  /* Color */
  --paper: light-dark(#fbf9f6, #171614);
  --surface: light-dark(#ffffff, #1e1c1a);
  --ink: light-dark(#1d1b19, #f1ece6);
  --muted: light-dark(#5a5550, #b3ada5);
  --rule: light-dark(#e2dcd4, #3a3632);
  --accent: light-dark(#a33c18, #f08a5d);
  --accent-ink: light-dark(#fbf9f6, #171614);
  --code: light-dark(#f4f0ea, #221f1c);
  --error: light-dark(#9b1c1c, #ff8a8a);
  --error-soft: light-dark(#fbeaea, #3a2220);
  --ok: light-dark(#2f6b3a, #9ddc8b);

  /* Syntax colors for pin and TypeScript */
  --kw: light-dark(#8a2a6e, #ff7ab8);
  --str: light-dark(#2f6b3a, #9ddc8b);
  --num: light-dark(#8a4d00, #ffb86b);
  --com: light-dark(#6b655e, #9a9289);
  --fn: light-dark(#1f5fa8, #7cb8ff);
  --type: light-dark(#0f6b6b, #6fd6d1);
  --interp: light-dark(#a33c18, #f08a5d);

  /* Type: a system UI stack and a matching monospace stack */
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --text-sm: .875rem;
  --text-base: 1.0625rem;
  --text-lg: 1.25rem;
  --text-h2: clamp(1.4rem, 3vw, 1.7rem);
  --text-h1: clamp(2.1rem, 4.6vw, 3rem);

  /* Space: one 4px-based scale. Use these, not ad hoc values. */
  --s1: .25rem;
  --s2: .5rem;
  --s3: .75rem;
  --s4: 1rem;
  --s6: 1.5rem;
  --s8: 2rem;
  --s12: 3rem;
  --s16: 4rem;

  /* Shape and width */
  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 14px;
  --wide: 72rem;
  --measure: 42rem;
}

/* ---------- 2. Base ---------- */

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-padding-top: var(--s6); scrollbar-gutter: stable; }

body {
  margin: 0;
  padding-inline: var(--s6);
  font: var(--text-base)/1.65 var(--sans);
  color: var(--ink);
  background: var(--paper);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: .2em; }
a:hover { text-decoration-thickness: 2px; }
::selection { background: color-mix(in srgb, var(--accent) 22%, transparent); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; border-radius: 3px; }

.skip-link {
  position: absolute; left: var(--s4); top: -4rem; z-index: 20;
  padding: var(--s2) var(--s4); border-radius: var(--radius-sm);
  background: var(--ink); color: var(--paper); text-decoration: none;
}
.skip-link:focus { top: var(--s4); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

/* ---------- 3. Header and footer (every page) ---------- */

.site-header {
  max-width: var(--wide); margin: 0 auto; padding: var(--s4) 0;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--s3) var(--s8);
  border-bottom: 1px solid var(--rule);
}
.brand {
  display: inline-flex; align-items: center; gap: var(--s2);
  font-size: 1.3rem; font-weight: 700; letter-spacing: -.02em; color: var(--ink); text-decoration: none;
}
.brand img { display: block; width: 28px; height: 28px; border-radius: 7px; }
.brand .dot { color: var(--accent); }

.site-nav ul { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: var(--s1) var(--s6); }
.site-nav a {
  display: inline-block; padding: var(--s1) 0; color: var(--ink); text-decoration: none; font-size: .95rem;
}
.site-nav a:hover { color: var(--accent); }
.site-nav a[aria-current="page"] { color: var(--accent); font-weight: 600; box-shadow: inset 0 -2px 0 var(--accent); }

.site-footer {
  max-width: var(--wide); margin: var(--s16) auto 0; padding: var(--s6) 0 var(--s12);
  border-top: 1px solid var(--rule);
  display: flex; flex-wrap: wrap; gap: var(--s2) var(--s6); justify-content: space-between;
  color: var(--muted); font-size: var(--text-sm);
}
.site-footer ul { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: var(--s1) var(--s4); }
.site-footer a { color: var(--muted); }
.site-footer a:hover { color: var(--accent); }
.site-footer p { margin: 0; max-width: none; }

/* ---------- 4. Layout ---------- */

/* A page is one column. Reference and tour pages add a section list beside the article. */
.page { max-width: var(--wide); margin: 0 auto; display: grid; grid-template-columns: minmax(0, 1fr); }
.page > main { min-width: 0; max-width: var(--measure); padding: var(--s12) 0 var(--s8); }
.page--ref { grid-template-columns: 13.5rem minmax(0, 1fr); gap: var(--s12); }
.page--ref > main { padding-top: var(--s12); }

.sidenav { position: sticky; top: var(--s6); align-self: start; padding-top: var(--s12); font-size: .92rem; }
.sidenav h2, .sidenav-label {
  margin: 0 0 var(--s3); font-size: .72rem; font-weight: 600; letter-spacing: .12em; text-transform: uppercase; color: var(--muted);
}
.sidenav ol, .sidenav ul { list-style: none; margin: 0; padding: 0; }
.sidenav li { margin: 0; }
.sidenav a { display: block; padding: var(--s1) var(--s2); border-radius: var(--radius-sm); color: var(--ink); text-decoration: none; line-height: 1.4; }
.sidenav a:hover { background: var(--code); }
.sidenav a[aria-current="page"] { background: var(--code); color: var(--accent); font-weight: 600; }

/* ---------- 5. Type ---------- */

.eyebrow {
  margin: 0 0 var(--s3); font-size: .75rem; font-weight: 600; letter-spacing: .12em; text-transform: uppercase; color: var(--accent);
}
h1 {
  margin: 0 0 var(--s4); font-size: var(--text-h1); line-height: 1.08; letter-spacing: -.03em; font-weight: 700; text-wrap: balance;
}
h2 {
  margin: var(--s12) 0 var(--s3); font-size: var(--text-h2); line-height: 1.2; letter-spacing: -.02em; font-weight: 650; text-wrap: balance; scroll-margin-top: var(--s6);
}
h3 { margin: var(--s8) 0 var(--s2); font-size: 1.1875rem; line-height: 1.3; font-weight: 650; scroll-margin-top: var(--s6); }
h1 + h2, .lead + h2 { margin-top: var(--s8); }

p, ul, ol { margin: 0 0 var(--s4); max-width: 68ch; }
ul, ol { padding-left: 1.25rem; }
li { margin-bottom: var(--s2); }
li > ul, li > ol { margin: var(--s2) 0 0; }
strong { font-weight: 650; }

.lead { margin: 0 0 var(--s8); font-size: var(--text-lg); line-height: 1.5; color: var(--muted); max-width: 40rem; }
.small { font-size: var(--text-sm); color: var(--muted); }
.muted { color: var(--muted); }
hr { margin: var(--s12) 0; border: 0; border-top: 1px solid var(--rule); }

/* ---------- 6. Code ---------- */

code, pre { font-family: var(--mono); }
:not(pre) > code {
  font-size: .9em; padding: .1em .35em; border-radius: 4px; background: var(--code); word-break: break-word;
}

pre {
  position: relative; margin: var(--s6) 0 var(--s8); padding: var(--s4) var(--s6);
  overflow-x: auto; font-size: .875rem; line-height: 1.65;
  background: var(--code); border: 1px solid var(--rule); border-radius: var(--radius);
}
pre[data-lang] { padding-top: var(--s8); }
pre[data-lang]::before {
  content: attr(data-lang); position: absolute; top: var(--s2); left: var(--s6);
  font: 600 .68rem/1 var(--sans); letter-spacing: .1em; text-transform: uppercase; color: var(--muted);
}
pre code { padding: 0; background: none; font-size: inherit; white-space: pre; word-break: normal; }

.tk-kw { color: var(--kw); font-weight: 600; }
.tk-str { color: var(--str); }
.tk-num { color: var(--num); }
.tk-com { color: var(--com); font-style: italic; }
.tk-fn { color: var(--fn); }
.tk-type { color: var(--type); }
.tk-deco { color: var(--accent); font-weight: 600; }
.tk-ph { color: var(--kw); }
.tk-pipe { color: var(--kw); font-weight: 600; }
.tk-op { color: var(--muted); }
.tk-interp { color: var(--interp); font-weight: 600; }

/* A compiler message is the output of the example above it, so the two read as one unit. */
pre:has(+ .compiler-message) { margin-bottom: 0; border-bottom-left-radius: 0; border-bottom-right-radius: 0; }
.compiler-message {
  margin: 0 0 var(--s8); padding: var(--s3) var(--s6);
  font-family: var(--mono); font-size: .875rem; line-height: 1.55; color: var(--ink);
  background: var(--error-soft); border: 1px solid var(--rule); border-left: 3px solid var(--error);
  border-top: 0; border-radius: 0 0 var(--radius) var(--radius);
}
.compiler-message .label {
  display: block; margin-bottom: var(--s1);
  font: 600 .68rem/1 var(--sans); letter-spacing: .1em; text-transform: uppercase; color: var(--error);
}
.compiler-message code { background: none; padding: 0; font-size: inherit; }

/* ---------- 7. Components ---------- */

.buttons { display: flex; flex-wrap: wrap; gap: var(--s3); margin: var(--s8) 0 0; }
.button {
  display: inline-block; padding: .65rem var(--s6); border: 1px solid var(--ink); border-radius: 8px;
  color: var(--ink); font-weight: 600; text-decoration: none; line-height: 1.2;
}
.button:hover { background: var(--code); }
.button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.button.primary:hover { background: var(--accent); filter: brightness(1.08); }

.cards { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--s4); margin: var(--s6) 0 var(--s4); }
.card { min-width: 0; padding: var(--s6); border: 1px solid var(--rule); border-radius: var(--radius-lg); background: var(--surface); }
.card h3 { margin: 0 0 var(--s2); font-size: 1.05rem; }
.card p { margin: 0; color: var(--muted); font-size: .95rem; }
@media (min-width: 640px) { .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

/* Callout: a note that the reader should not miss. */
.note {
  margin: var(--s6) 0; padding: var(--s4) var(--s6); border-left: 3px solid var(--accent);
  background: var(--code); border-radius: 0 var(--radius) var(--radius) 0;
}
.note p:last-child { margin-bottom: 0; }

/* Tables: one style everywhere. Wide tables scroll; the errors table stacks on phones. */
table {
  display: block; width: 100%; overflow-x: auto; margin: var(--s4) 0 var(--s8);
  border-collapse: collapse; font-size: .95rem;
}
th, td { padding: var(--s3); text-align: left; vertical-align: top; border-bottom: 1px solid var(--rule); }
th { font-size: .75rem; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); border-bottom-width: 2px; }
td code { word-break: break-word; }

/* Pager: previous and next reference section */
.pager {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--s4);
  margin: var(--s16) 0 0; padding: 0; max-width: none; list-style: none;
}
.pager a {
  display: block; height: 100%; padding: var(--s4) var(--s6); border: 1px solid var(--rule); border-radius: var(--radius);
  color: var(--ink); text-decoration: none;
}
.pager a:hover { border-color: var(--accent); }
.pager .next { grid-column: 2; text-align: right; }
.pager .next:only-child { grid-column: 2; }
.pager small { display: block; margin-bottom: var(--s1); font-size: .72rem; font-weight: 600; letter-spacing: .1em; text-transform: uppercase; color: var(--muted); }
.pager span { font-weight: 600; }
.pager li { margin: 0; }

/* Reference index: a numbered list of sections */
.ref-list { list-style: none; margin: var(--s8) 0 0; padding: 0; max-width: none; display: grid; gap: var(--s2); grid-template-columns: minmax(0, 1fr); }
.ref-list li { margin: 0; }
.ref-list a {
  display: block; padding: var(--s3) var(--s4); border: 1px solid var(--rule); border-radius: var(--radius-sm);
  color: var(--ink); text-decoration: none;
}
.ref-list a:hover { border-color: var(--accent); color: var(--accent); }
@media (min-width: 640px) { .ref-list { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

/* ---------- 8. Tour (playground) ---------- */

.tour-intro { max-width: var(--wide); margin: 0 auto; padding: var(--s12) 0 var(--s4); }
.tour-intro h1 { margin-bottom: var(--s2); font-size: clamp(2rem, 4vw, 2.6rem); }
.tour-intro p { margin: 0; color: var(--muted); font-size: var(--text-lg); max-width: 40rem; }

.page--tour { grid-template-columns: 13.5rem minmax(0, 1fr); gap: var(--s12); }
.page--tour > main { max-width: none; padding-top: var(--s8); }
.page--tour .sidenav { padding-top: 0; }
#lesson-list { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--s1); }
#lesson-list li { margin: 0; }
#lesson-list button {
  all: unset; box-sizing: border-box; display: flex; align-items: baseline; gap: var(--s2);
  width: 100%; padding: var(--s2) var(--s2); border-radius: var(--radius-sm); cursor: pointer;
  color: var(--ink); font: .92rem/1.4 var(--sans);
}
#lesson-list button:hover { background: var(--code); }
#lesson-list button[aria-current="true"] { background: var(--code); font-weight: 600; box-shadow: inset 3px 0 0 var(--accent); }
#lesson-list button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
#lesson-list .num { min-width: 1.2rem; color: var(--muted); font-variant-numeric: tabular-nums; }

.step { margin: 0 0 var(--s2); font-size: .75rem; font-weight: 600; letter-spacing: .12em; text-transform: uppercase; color: var(--accent); }
.tour-title { margin: 0 0 var(--s2); font-size: var(--text-h2); line-height: 1.2; letter-spacing: -.02em; min-height: 1.2em; }
.text { margin: 0 0 var(--s4); min-height: 4.8rem; font-size: 1.05rem; color: var(--muted); max-width: 42rem; }

/* The task is one clear instruction, set off with an accent rule */
.task {
  display: flex; flex-direction: column; justify-content: center; min-height: 3.6rem;
  margin: 0 0 var(--s8); padding: var(--s1) 0 var(--s1) var(--s4); max-width: 42rem;
  border-left: 3px solid var(--accent);
}
.task::before {
  content: "Try this"; margin-bottom: 2px; font-size: .72rem; font-weight: 600; letter-spacing: .1em; text-transform: uppercase; color: var(--accent);
}
.task span { font-weight: 500; }

.panes { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--s4); }
@media (max-width: 1000px) { .panes { grid-template-columns: minmax(0, 1fr); } }

.pane {
  display: flex; flex-direction: column; min-width: 0; height: 340px;
  overflow: hidden; border: 1px solid var(--rule); border-radius: var(--radius-lg); background: var(--code);
}
.pane-head {
  display: flex; justify-content: space-between; align-items: center; gap: var(--s2);
  min-height: 2.6rem; padding: var(--s1) var(--s2) var(--s1) var(--s4);
  border-bottom: 1px solid var(--rule); background: var(--paper);
  font-size: .72rem; font-weight: 600; letter-spacing: .1em; text-transform: uppercase; color: var(--muted);
}
.run-controls { display: flex; gap: var(--s2); }
#run-status { min-width: 9rem; min-height: 1.3em; text-align: right; text-transform: none; letter-spacing: 0; font-weight: 500; }

.tabs { display: flex; gap: var(--s1); }
.tabs button {
  padding: .15rem var(--s2); border: 1px solid var(--rule); border-radius: 6px; background: none;
  font: inherit; font-size: .76rem; font-weight: 500; letter-spacing: 0; text-transform: none; color: var(--muted); cursor: pointer;
}
.tabs button:hover:not(:disabled) { color: var(--ink); border-color: var(--muted); }
.tabs button[aria-selected="true"] { background: var(--ink); border-color: var(--ink); color: var(--paper); }
.tabs button:disabled { opacity: .4; cursor: default; }

.run, .stop {
  padding: .3rem var(--s3); border-radius: 8px; font: inherit; font-size: .8rem; font-weight: 600; letter-spacing: 0; text-transform: none; cursor: pointer;
}
.run { border: 0; background: var(--accent); color: var(--accent-ink); }
.run:disabled { opacity: .5; cursor: default; }
.stop { border: 1px solid var(--error); background: none; color: var(--error); }
.stop[hidden] { display: inline-block; visibility: hidden; } /* keeps the header from shifting while running */

/* Editor: colored code sits behind a transparent textarea, so typing and highlighting line up. */
.code-wrap { position: relative; flex: 1; min-height: 0; background: var(--code); }
.code-wrap textarea,
.code-layer {
  position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; padding: var(--s4) var(--s6);
  border: 0; border-radius: 0; font: .9rem/1.6 var(--mono); white-space: pre; overflow: auto; tab-size: 2;
}
.code-wrap textarea {
  z-index: 1; display: block; resize: none; background: transparent; color: transparent; caret-color: var(--ink);
}
.code-wrap textarea:focus { outline: 2px solid var(--accent); outline-offset: -2px; }
.code-wrap textarea::selection { background: color-mix(in srgb, var(--accent) 25%, transparent); }
.code-layer { z-index: 0; overflow: hidden; color: var(--ink); background: var(--code); pointer-events: none; }

/* Output panes: the TypeScript and the program output share the pane body style */
#output, #console {
  flex: 1; min-height: 0; margin: 0; padding: var(--s4) var(--s6); overflow: auto; overflow-anchor: none;
  border: 0; border-radius: 0;
  font: .9rem/1.6 var(--mono); white-space: pre; color: var(--ink); background: var(--code);
}
.pane.output { height: auto; }
.output #console { flex: none; height: 9rem; }
.output pre.error { color: var(--error); }

.status {
  display: flex; align-items: center; height: 4.6rem; overflow: auto; margin: var(--s4) 0 0; padding: var(--s3) var(--s4);
  border: 1px solid var(--rule); border-radius: var(--radius); background: var(--surface);
  font: .9rem/1.5 var(--mono); white-space: pre-wrap; color: var(--ok);
}
.status.error { color: var(--error); background: var(--error-soft); border-color: transparent; }

.pane.output { margin-top: var(--s4); }

.hint { margin: var(--s4) 0 0; font-size: var(--text-sm); color: var(--muted); }

.controls {
  display: flex; justify-content: space-between; align-items: center; gap: var(--s3); flex-wrap: wrap;
  margin-top: var(--s8); padding-top: var(--s6); border-top: 1px solid var(--rule);
}
.controls button {
  padding: .5rem var(--s4); border: 1px solid var(--rule); border-radius: 8px; background: none;
  font: inherit; font-size: .95rem; color: var(--ink); cursor: pointer;
}
.controls button:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.controls button:disabled { opacity: .35; cursor: default; }
.controls .reset { border-color: transparent; color: var(--muted); }
.controls .reset:hover { color: var(--accent); }

/* Shown when the tour opens from file:// instead of a server */
.notice {
  max-width: var(--wide); margin: var(--s6) auto 0; padding: var(--s4) var(--s6);
  border: 1px solid var(--error); border-radius: var(--radius); background: var(--error-soft); color: var(--ink);
}

/* ---------- 10. Small screens ---------- */

@media (max-width: 900px) {
  .page--ref, .page--tour { grid-template-columns: minmax(0, 1fr); gap: 0; }
  .sidenav { position: static; padding: var(--s6) 0 var(--s4); border-bottom: 1px solid var(--rule); }
  .sidenav ol, .sidenav ul, #lesson-list { display: flex; flex-wrap: wrap; gap: var(--s1); }
  .sidenav li, #lesson-list li { margin: 0; }
  .page > main, .page--ref > main { padding-top: var(--s8); }
  #lesson-list button { width: auto; }
}

@media (max-width: 720px) {
  body { padding-inline: var(--s4); font-size: 1rem; }
  .site-header { padding-block: var(--s3); }
  .site-nav ul { gap: var(--s1) var(--s4); }
  .site-nav a { font-size: .9rem; }
  .lead { font-size: 1.1rem; }
  pre { padding-inline: var(--s4); }
  .tour-intro { padding-top: var(--s8); }
  .pane { height: 300px; }
  .pager { grid-template-columns: minmax(0, 1fr); }
  .pager .next { grid-column: 1; text-align: left; }
}

/* Errors table: on phones each row becomes a labelled block */
@media (max-width: 640px) {
  .errors-table, .errors-table thead, .errors-table tbody, .errors-table tr, .errors-table td { display: block; width: 100%; }
  .errors-table thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .errors-table tr { padding: var(--s3) 0; border-bottom: 1px solid var(--rule); }
  .errors-table td { padding: var(--s1) 0; border: 0; }
  .errors-table td::before {
    content: attr(data-label); display: block; font-size: .72rem; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--muted);
  }
}
.errors-table code { white-space: normal; }
.errors-table td:first-child { min-width: 14rem; }
