/* ───────────────────────────── fonts ───────────────────────────── */
@font-face { font-family: "IBM Plex Mono"; font-weight: 400; font-style: normal; font-display: swap; src: url("assets/fonts/ibm-plex-mono-latin-400-normal.woff2") format("woff2"); }
@font-face { font-family: "IBM Plex Mono"; font-weight: 400; font-style: italic; font-display: swap; src: url("assets/fonts/ibm-plex-mono-latin-400-italic.woff2") format("woff2"); }
@font-face { font-family: "IBM Plex Mono"; font-weight: 500; font-style: normal; font-display: swap; src: url("assets/fonts/ibm-plex-mono-latin-500-normal.woff2") format("woff2"); }
@font-face { font-family: "IBM Plex Mono"; font-weight: 600; font-style: normal; font-display: swap; src: url("assets/fonts/ibm-plex-mono-latin-600-normal.woff2") format("woff2"); }
@font-face { font-family: "Newsreader"; font-weight: 200 800; font-style: normal; font-display: swap; src: url("assets/fonts/newsreader-latin-variable.woff2") format("woff2"); }

/* ───────────────────────────── tokens ──────────────────────────── */
:root {
  /* deep teal night, lit from above */
  --ink:    #0b161c;
  --ink-2:  #0f1f27;
  --ink-3:  #142a34;
  --line:   #1b323d;
  --line-2: #27495a;

  --text:   #d8e2e3;
  --cream:  #ebe3d1;
  --muted:  #8aa3aa;
  --faint:  #526b75;

  --gold:     #e6b250;
  --gold-dim: #a9802c;

  --kw:  #e6b250;
  --str: #a9dcbd;
  --num: #f0b0a6;
  --tbl: #9cc9e0;
  --cmt: #6f8f9b;
  --err: #f09a9a;
  --link: #a3d9b8;

  --mono:  "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --serif: "Newsreader", Georgia, "Times New Roman", serif;

  --bar-h: 46px;
  --side-w: 232px;
  --pad: 28px;
  --rail: 56px;           /* room for the journey line + markers */
  --content: 860px;
}

/* ───────────────────────────── base ────────────────────────────── */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
body {
  margin: 0;
  background: var(--ink);
  background-image: radial-gradient(900px 520px at 50% -120px, rgba(230, 178, 80, .10), transparent 70%);
  background-repeat: no-repeat;
  color: var(--text);
  font-family: var(--mono);
  font-size: 14px;
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
a { color: inherit; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
::selection { background: rgba(230, 178, 80, .35); }

/* film grain, fixed, very quiet */
.grain {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  opacity: .055;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}
.bar, .app, .foot { position: relative; z-index: 1; }

/* ───────────────────────────── top bar ─────────────────────────── */
.bar {
  height: var(--bar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--pad);
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}
.bar-left { display: flex; align-items: center; gap: 10px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--gold); box-shadow: 0 0 0 3px rgba(230, 178, 80, .15); }
.db-name { font-weight: 500; }
.bar-note { color: var(--faint); }
.bar-right { display: flex; gap: 20px; }
.bar-right a { color: var(--muted); text-decoration: none; }
.bar-right a:hover { color: var(--text); text-decoration: underline; text-underline-offset: 3px; }

/* ───────────────────────────── layout ──────────────────────────── */
.app {
  flex: 1;
  display: grid;
  grid-template-columns: var(--side-w) minmax(0, 1fr);
}

/* ───────────────────────────── sidebar ─────────────────────────── */
.side {
  border-right: 1px solid var(--line);
  padding: 18px 0 24px;
  position: sticky;
  top: 0;
  align-self: start;
  max-height: 100vh;
  overflow-y: auto;
  scrollbar-width: thin;
  display: flex;
  flex-direction: column;
  gap: 22px;
}
.side-title { font-size: 12px; color: var(--muted); padding: 0 20px 6px; }
.side-list { list-style: none; margin: 0; padding: 0; }
.side-list button {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  background: none;
  border: 0;
  border-left: 2px solid transparent;
  padding: 5px 18px;
  text-align: left;
  font-size: 13px;
  cursor: pointer;
  color: var(--text);
}
.side-list button:hover { background: var(--ink-2); }
.side-list button.active { border-left-color: var(--gold); background: var(--ink-2); }
#chapter-list button { justify-content: flex-start; gap: 12px; }
#chapter-list .n { color: var(--faint); font-size: 11.5px; min-width: 2ch; }
#chapter-list button.active .n { color: var(--gold); }
.side-list .count { color: var(--faint); font-size: 12px; }
.history button { display: block; color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.side-empty { color: var(--faint); font-size: 12.5px; padding: 5px 20px; }
.side-foot { margin-top: auto; padding: 0 20px; color: var(--faint); font-size: 12px; line-height: 1.55; }
.side-foot p { margin: 0 0 8px; }
.side-foot code { color: var(--muted); }

/* ───────────────────────────── the story ───────────────────────── */
.story {
  width: 100%;
  max-width: calc(var(--content) + var(--rail) + var(--pad) * 2);
  margin: 0 auto;
  padding: 40px var(--pad) 80px calc(var(--pad) + var(--rail));
  position: relative;
}
/* the journey line */
.story::before {
  content: "";
  position: absolute;
  left: calc(var(--pad) + 10px);
  top: 48px;
  bottom: 40px;
  width: 1px;
  background: linear-gradient(var(--line-2), var(--line) 70%, transparent);
}
.chapter { position: relative; margin: 0 0 96px; }
.chapter:last-child { margin-bottom: 0; }
/* marker on the line */
.chapter::before {
  content: "";
  position: absolute;
  left: calc(var(--rail) * -1 + 6px);
  top: 8px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--gold);
  box-shadow: 0 0 0 5px var(--ink), 0 0 18px rgba(230, 178, 80, .35);
}
.chapter.hero::before { top: 18px; }

.chapter-head { margin-bottom: 22px; }
.chapter-n { display: block; color: var(--gold); font-size: 13px; letter-spacing: .04em; margin-bottom: 8px; }
.chapter-title {
  margin: 0 0 14px;
  font-family: var(--serif);
  font-weight: 500;
  font-size: 46px;
  line-height: 1.05;
  letter-spacing: -.01em;
  color: var(--cream);
  font-optical-sizing: auto;
}
.lede {
  margin: 0;
  font-family: var(--serif);
  font-size: 19.5px;
  line-height: 1.55;
  color: #b7c5c8;
  max-width: 62ch;
}
.lede a { color: var(--link); text-decoration: underline; text-underline-offset: 3px; }
.scroll-hint { margin: 26px 0 0; font-size: 12.5px; color: var(--faint); }

/* ───────────────────────────── editor ──────────────────────────── */
.editor {
  background: var(--ink-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  transition: border-color .15s ease, box-shadow .15s ease;
  --gutter: 52px;
  font-size: 15px;
  line-height: 1.65;
}
.editor:focus-within { border-color: var(--line-2); }
.editor.running { border-color: var(--gold); box-shadow: 0 0 0 1px rgba(230, 178, 80, .25), 0 0 32px -8px rgba(230, 178, 80, .35); }

.code { position: relative; }
/* The textarea is invisible and sits on top of the highlighted copy.
   Both get identical font, padding and wrapping, so they line up. */
.highlight, .input {
  box-sizing: border-box;
  width: 100%;
  margin: 0;
  padding: 16px 18px 16px var(--gutter);
  font: inherit;
  line-height: inherit;
  letter-spacing: 0;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: break-word;
  tab-size: 2;
  overflow: hidden;
}
.highlight { color: var(--text); min-height: calc(1.65em * 3 + 32px); }
.highlight code { font: inherit; display: block; }
.highlight .line { display: block; position: relative; }
.highlight .line::before {
  content: attr(data-n);
  position: absolute;
  left: calc(var(--gutter) * -1);
  width: calc(var(--gutter) - 14px);
  text-align: right;
  color: var(--faint);
}
.input {
  position: absolute;
  inset: 0;
  height: 100%;
  background: transparent;
  color: transparent;
  caret-color: var(--gold);
  border: 0;
  outline: none;
  resize: none;
  display: block;
}
.input::placeholder { color: var(--faint); }
.input::selection { background: rgba(230, 178, 80, .3); color: transparent; }

.tok-kw  { color: var(--kw); font-weight: 500; }
.tok-str { color: var(--str); }
.tok-num { color: var(--num); }
.tok-tbl { color: var(--tbl); }
.tok-cmt { color: var(--cmt); font-style: italic; }
.tok-op  { color: var(--muted); }

.editor-foot {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 10px 8px 14px;
  border-top: 1px solid var(--line);
}
.editor-foot.no-chips { justify-content: flex-end; padding: 5px 8px; }
.no-chips .run { padding: 3px 8px 3px 10px; font-size: 12px; border-color: transparent; }
.no-chips .run:hover { border-color: var(--gold); }
.chips {
  flex: 1;
  min-width: 0;
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-mask-image: linear-gradient(90deg, #000 calc(100% - 36px), transparent);
          mask-image: linear-gradient(90deg, #000 calc(100% - 36px), transparent);
  padding-right: 36px;
}
.chips::-webkit-scrollbar { display: none; }
.no-chips .chips { display: none; }
.chip {
  flex: none;
  background: transparent;
  border: 1px solid var(--line-2);
  border-radius: 4px;
  padding: 4px 10px;
  font-size: 12.5px;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
}
.chip:hover { color: var(--text); border-color: var(--gold-dim); }
.run {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: transparent;
  color: var(--gold);
  border: 1px solid var(--gold-dim);
  border-radius: 4px;
  padding: 5px 9px 5px 12px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
}
.run:hover { background: var(--gold); color: var(--ink); border-color: var(--gold); }
.run:active { transform: translateY(1px); }
.run kbd { font: inherit; font-weight: 400; font-size: 11px; opacity: .75; }
.console .run { background: var(--gold); color: var(--ink); border-color: var(--gold); padding: 7px 10px 7px 14px; font-size: 13px; }
.console .run:hover { background: #f0c268; }

/* ───────────────────────────── results ─────────────────────────── */
.status { min-height: 26px; font-size: 12.5px; color: var(--muted); padding: 8px 2px 6px; }
.status .ok { color: var(--gold); }
.status .bad { color: var(--err); }
.status .time { color: var(--faint); }

.interp { font-size: 12.5px; color: var(--muted); margin: 0 0 10px; }
.interp code { color: var(--text); }

.output > * { animation: rise .32s ease-out both; }
@keyframes rise {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: none; }
}

.result {
  border: 1px solid var(--line);
  border-top: 2px solid var(--gold);
  border-radius: 6px;
  padding: 22px 26px 20px;
  font-family: var(--serif);
  color: var(--cream);
  background: linear-gradient(180deg, rgba(230, 178, 80, .035), transparent 140px);
}
.result a { color: var(--link); text-decoration: underline; text-underline-offset: 3px; text-decoration-thickness: 1px; }
.result a:hover { text-decoration-thickness: 2px; }
.result a.file { font-family: var(--mono); font-size: 13px; color: var(--gold); text-decoration: none; border: 1px solid var(--gold-dim); border-radius: 4px; padding: 4px 10px; white-space: nowrap; }
.result a.file:hover { background: var(--gold); color: var(--ink); border-color: var(--gold); }
.result-note { margin: 14px 0 0; font-family: var(--mono); font-size: 12.5px; color: var(--muted); }

/* the record: one row, one person */
.result.record { display: flex; gap: 36px; align-items: flex-start; padding: 28px 30px 26px; }
.photo-frame { flex: none; position: relative; width: 240px; height: 240px; }
.photo-frame::after {
  /* a hairline frame, slipped off the corner */
  content: "";
  position: absolute;
  inset: 0;
  transform: translate(10px, 10px);
  border: 1px solid var(--gold-dim);
  border-radius: 4px;
  pointer-events: none;
}
.photo, .monogram {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 4px;
  object-fit: cover;
  background: var(--ink-3);
  filter: saturate(.9) contrast(1.04);
  position: relative;
  z-index: 1;
}
.monogram { display: grid; place-items: center; color: var(--gold); font-family: var(--mono); font-size: 72px; font-weight: 500; }
.record-body { flex: 1; min-width: 0; }
.record-name {
  margin: 0 0 8px;
  font-size: 52px;
  line-height: 1.02;
  font-weight: 500;
  letter-spacing: -.012em;
  color: var(--cream);
}
.record-sub { margin: 0; font-size: 19.5px; color: var(--muted); }
.fields { margin: 22px 0 0; border-top: 1px solid var(--line-2); }
.field {
  display: grid;
  grid-template-columns: 118px minmax(0, 1fr);
  gap: 18px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}
.field:last-child { border-bottom: 0; }
.field-k { font-family: var(--mono); font-size: 12.5px; color: var(--muted); padding-top: 4px; }
.field-v { margin: 0; font-size: 17.5px; line-height: 1.5; }
.field-v.null { font-family: var(--mono); color: var(--faint); font-size: 13px; }
.field-v p { margin: 0 0 .9em; max-width: 58ch; }
.field-v p:last-child { margin-bottom: 0; }

/* the grid: everything else */
.grid-wrap { overflow-x: auto; }
table.grid { width: 100%; border-collapse: collapse; font-size: 16.5px; line-height: 1.45; }
.grid th {
  text-align: left;
  font-family: var(--mono);
  font-weight: 500;
  font-size: 12.5px;
  color: var(--muted);
  padding: 2px 18px 9px 0;
  border-bottom: 1px solid var(--line-2);
  white-space: nowrap;
}
.grid td { padding: 11px 18px 11px 0; border-bottom: 1px solid var(--line); vertical-align: top; }
.grid tr:last-child td { border-bottom: 0; }
.grid th.n, .grid td.n { width: 2.4em; padding-right: 14px; text-align: right; font-family: var(--mono); font-size: 12.5px; color: var(--faint); }
.grid td.n { padding-top: 13px; }
.grid td.null { font-family: var(--mono); font-size: 12.5px; color: var(--faint); }
.grid td.num { font-variant-numeric: tabular-nums; }
.grid td.wide { min-width: 22ch; max-width: 48ch; }
.grid td.key { font-weight: 600; white-space: nowrap; }

/* errors and messages stay in the console voice */
.error {
  border: 1px solid rgba(240, 154, 154, .3);
  background: rgba(240, 154, 154, .06);
  border-radius: 6px;
  padding: 14px 16px;
  color: var(--err);
  font-size: 14px;
}
.error .hint { color: var(--muted); margin-top: 6px; }
.message { border: 1px solid var(--line); border-radius: 6px; padding: 14px 16px; color: var(--muted); font-size: 13.5px; }
.message p { margin: 0 0 8px; }
.message p:last-child { margin: 0; }
.message code { color: var(--text); }

/* ───────────────────────────── footer ──────────────────────────── */
.foot {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 14px var(--pad);
  border-top: 1px solid var(--line);
  color: var(--faint);
  font-size: 12px;
}

/* ───────────────────────────── responsive ──────────────────────── */
@media (min-width: 901px) {
  .chips { flex-wrap: wrap; -webkit-mask-image: none; mask-image: none; padding-right: 0; }
}

@media (max-width: 900px) {
  :root { --pad: 16px; --rail: 0px; }
  .app { grid-template-columns: minmax(0, 1fr); }
  .side { display: none; }
  .story { padding-top: 24px; }
  .story::before, .chapter::before { display: none; }
  .chapter { margin-bottom: 72px; }
  .chapter-title { font-size: 36px; }
  .lede { font-size: 18px; }
}

@media (max-width: 720px) {
  /* 16px keeps iOS from zooming the page when the editor gets focus */
  .editor { font-size: 16px; line-height: 1.55; --gutter: 40px; }
  .highlight, .input { padding: 12px 12px 12px var(--gutter); }
  .highlight { min-height: calc(1.55em * 3 + 24px); }
  .highlight .line::before { width: calc(var(--gutter) - 10px); }
  .editor-foot { padding: 8px 10px 8px 12px; gap: 10px; }

  .result { padding: 18px 16px 16px; }
  .result.record { flex-direction: column; gap: 24px; padding: 20px 18px 18px; }
  .photo-frame { width: 168px; height: 168px; }
  .photo-frame::after { transform: translate(8px, 8px); }
  .monogram { font-size: 56px; }
  .record-name { font-size: 38px; }
  .record-sub { font-size: 17px; }
  .field { grid-template-columns: minmax(0, 1fr); gap: 2px; padding: 10px 0; }
  .field-k { padding-top: 0; font-size: 12px; }
  .field-v { font-size: 17px; }

  /* short tables stay tables, just tighter */
  table.grid.compact { font-size: 16px; }
  .grid.compact th { padding-right: 12px; }
  .grid.compact td { padding: 9px 12px 9px 0; }
  .grid.compact th.n, .grid.compact td.n { width: 1.8em; padding-right: 10px; }

  /* wide tables become a stack of records */
  table.grid:not(.compact), .grid:not(.compact) thead, .grid:not(.compact) tbody, .grid:not(.compact) tr, .grid:not(.compact) td { display: block; }
  .grid:not(.compact) thead { display: none; }
  .grid:not(.compact) tr { position: relative; padding: 12px 0; border-bottom: 1px solid var(--line); }
  .grid:not(.compact) tr:last-child { border-bottom: 0; }
  .grid:not(.compact) td { padding: 3px 44px 3px 0; border-bottom: 0; }
  .grid:not(.compact) td.n { position: absolute; right: 0; top: 12px; width: auto; padding: 0; }
  .grid:not(.compact) td.wide { max-width: none; min-width: 0; }
  .grid:not(.compact) td.key { white-space: normal; }
  .grid:not(.compact) td[data-col]::before { content: attr(data-col); display: block; font-family: var(--mono); font-size: 11.5px; color: var(--muted); }
  .grid:not(.compact) td.n::before { content: none; }

  .foot { flex-direction: column; gap: 4px; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .output > * { animation: none; }
  .editor, .run { transition: none; }
}
