:root {
  --paper: #EEF0F2;
  --surface: #FFFFFF;
  --ink: #1C2330;
  --muted: #5B6473;
  --rule: #CDD2D9;
  --logo: #3C3CC0;       /* the sketch's LOGO square */
  --logo-ink: #FFFFFF;
  --target: #B8102A;     /* the sketch's red destination outline */
  --good: #1D7A4C;
  --stage: #282828;      /* background(40) */
  --focus: #3C3CC0;
  --radius-s: 3px;
  --radius-m: 6px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #15181D;
    --surface: #1D2128;
    --ink: #E6E9EE;
    --muted: #9AA3B2;
    --rule: #2F353F;
    --logo: #8E8EFF;
    --logo-ink: #10132A;
    --target: #FF6B78;
    --good: #5FD39A;
    --focus: #8E8EFF;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--paper);
  color: var(--ink);
  font-family: "Atkinson Hyperlegible", system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  font-variant-numeric: tabular-nums;
}
a { color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.muted { color: var(--muted); }
.small { font-size: 14px; }

/* ---------- buttons ---------- */
.btn {
  font: inherit; font-size: 15px; font-weight: 700;
  border: 1px solid var(--rule); background: var(--surface); color: var(--ink);
  padding: 8px 14px; border-radius: var(--radius-s); cursor: pointer;
  text-decoration: none; display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--logo); border-color: var(--logo); color: var(--logo-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.quiet { background: transparent; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 24px; border-bottom: 1px solid var(--rule); background: var(--surface);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 12px; font-weight: 700; font-size: 18px; text-decoration: none; }
.brand-mark {
  width: 18px; height: 18px; background: var(--logo); transform: rotate(20deg);
  outline: 2px solid var(--target); outline-offset: 3px;
}
.topbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- layout ---------- */
.layout { display: grid; grid-template-columns: 290px minmax(0, 1fr); min-height: calc(100vh - 61px); }
.sidebar { border-right: 1px solid var(--rule); padding: 20px 16px 40px; }
#main { padding: 32px 40px 64px; max-width: 1080px; outline: none; }

.nav-compare {
  display: block; padding: 10px 12px; margin-bottom: 20px; font-weight: 700; text-decoration: none;
  border: 1px solid var(--rule); border-radius: var(--radius-s); background: var(--surface);
}
.nav-compare[aria-current="page"] { border-color: var(--logo); box-shadow: inset 3px 0 0 var(--logo); }
.sidebar-title { font-size: 14px; font-weight: 700; color: var(--muted); margin: 20px 0 6px; }

/* Version lineage: each design is a vertical line, each version a rotated square on it */
.family { margin: 0 0 14px; }
.family-name { font-weight: 700; font-size: 15px; margin: 0 0 2px 4px; overflow-wrap: anywhere; }
.lineage { list-style: none; margin: 0; padding: 0; position: relative; }
.family .lineage::before {
  content: ""; position: absolute; left: 15px; top: 10px; bottom: 10px; width: 1px; background: var(--rule);
}
.node {
  position: relative; display: flex; gap: 12px; align-items: flex-start;
  padding: 6px 8px; border-radius: var(--radius-s); text-decoration: none;
}
.node:hover { background: color-mix(in srgb, var(--logo) 8%, transparent); }
.node[aria-current="page"] { background: color-mix(in srgb, var(--logo) 14%, transparent); }
.node-mark {
  flex: none; width: 13px; height: 13px; margin-top: 5px; margin-left: 2px;
  border: 2px solid var(--muted); background: var(--paper); transform: rotate(var(--r, 20deg));
}
.node[aria-current="page"] .node-mark { border-color: var(--logo); background: var(--logo); }
.node.bench .node-mark { border-color: var(--muted); background: #C8C8C8; transform: none; }
.node-text { display: flex; flex-direction: column; min-width: 0; }
.node-name { font-weight: 700; font-size: 15px; }
.node-meta { font-size: 13px; color: var(--muted); }

/* ---------- detail ---------- */
.detail-head h1 { font-size: 34px; line-height: 1.15; margin: 0 0 6px; overflow-wrap: anywhere; }
.detail-head .byline { color: var(--muted); margin: 0 0 16px; }
.description {
  max-width: 68ch; white-space: pre-wrap; margin: 0 0 20px;
  padding-left: 14px; border-left: 3px solid var(--rule);
}
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 28px; }

.figures {
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 0;
  border-top: 1px solid var(--rule); border-bottom: 1px solid var(--rule); margin: 0 0 32px;
}
.figure { padding: 14px 16px 14px 0; }
.figure + .figure { padding-left: 16px; border-left: 1px solid var(--rule); }
.figure dt { font-size: 14px; color: var(--muted); }
.figure dd { margin: 2px 0 0; font-size: 26px; font-weight: 700; line-height: 1.2; }
.figure dd small { font-size: 14px; font-weight: 400; color: var(--muted); }
.faster { color: var(--good); }
.slower { color: var(--target); }

h2.section { font-size: 20px; margin: 0 0 10px; }

.table-wrap { overflow-x: auto; border: 1px solid var(--rule); border-radius: var(--radius-m); background: var(--surface); }
table { border-collapse: collapse; width: 100%; font-size: 15px; }
th, td { text-align: left; padding: 9px 14px; border-bottom: 1px solid var(--rule); white-space: nowrap; }
th { font-size: 14px; color: var(--muted); font-weight: 700; }
td.num, th.num { text-align: right; }
tbody tr:last-child td { border-bottom: none; }
tr.fresh td { animation: fresh 2.4s ease-out; }
@keyframes fresh { from { background: color-mix(in srgb, var(--logo) 22%, transparent); } to { background: transparent; } }
.err-cell { color: var(--target); font-weight: 700; }
.empty { padding: 28px; text-align: center; color: var(--muted); }

.code-block { margin-top: 28px; }
.code-block summary { cursor: pointer; font-weight: 700; }
.code-block pre {
  margin: 10px 0 0; max-height: 520px; overflow: auto; padding: 16px;
  background: var(--stage); color: #E8E8E8; border-radius: var(--radius-m);
  font: 13px/1.5 ui-monospace, "Cascadia Mono", Menlo, Consolas, monospace; tab-size: 2;
}

/* compare */
.bar-cell { width: 40%; min-width: 180px; }
.bar { position: relative; height: 12px; background: color-mix(in srgb, var(--rule) 60%, transparent); border-radius: 2px; }
.bar span { position: absolute; inset: 0 auto 0 0; background: var(--logo); border-radius: 2px; }
.bar.is-bench span { background: #8C8C8C; }
.bar i { position: absolute; top: -4px; bottom: -4px; width: 2px; background: var(--target); }
td a { font-weight: 700; }

/* ---------- dialogs ---------- */
dialog {
  border: 1px solid var(--rule); border-radius: var(--radius-m); padding: 0;
  background: var(--surface); color: var(--ink); width: min(560px, calc(100vw - 32px));
}
dialog::backdrop { background: rgb(20 22 28 / .55); }
dialog form { padding: 24px; display: flex; flex-direction: column; gap: 14px; }
dialog h2 { margin: 0; font-size: 22px; }
dialog p { margin: 0; }
label { display: flex; flex-direction: column; gap: 4px; font-weight: 700; font-size: 15px; }
input, textarea {
  font: inherit; font-weight: 400; color: var(--ink); background: var(--paper);
  border: 1px solid var(--rule); border-radius: var(--radius-s); padding: 8px 10px;
}
input[type="file"] { padding: 6px; }
.code-input { font: 13px/1.45 ui-monospace, Menlo, Consolas, monospace; width: 100%; margin-top: 8px; }
.paste summary { cursor: pointer; font-weight: 700; font-size: 15px; }
.hint { font-weight: 400; font-size: 14px; color: var(--muted); min-height: 1.2em; }
.form-error { color: var(--target); font-weight: 700; min-height: 1.2em; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ---------- stage ---------- */
.stage {
  position: fixed; inset: 0; z-index: 50; background: #1A1A1A; color: #E8E8E8;
  display: flex; flex-direction: column;
}
.stage[hidden] { display: none; }
.stage-bar {
  display: flex; align-items: center; gap: 16px; padding: 8px 16px; flex-wrap: wrap;
  border-bottom: 1px solid #333;
}
.stage-title { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.stage-muted { color: #A8A8A8; font-size: 14px; }
.stage-status { flex: 1; text-align: center; font-weight: 700; }
.stage-actions { display: flex; gap: 6px; }
.stage-btn { background: #2E2E2E; border-color: #444; color: #E8E8E8; padding: 6px 12px; font-size: 14px; }
.stage-btn:hover { border-color: #777; }
.stage-warn { background: #4A3A12; color: #FFE7A8; padding: 8px 16px; font-size: 14px; }
.stage-done {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap; justify-content: center;
  padding: 12px 16px; background: #23234A; border-bottom: 1px solid #3C3CC0;
}
.stage-done[hidden] { display: none; }
.stage-done .result-line { font-size: 17px; }
.stage-done .btn.primary { background: #5A5AE6; border-color: #5A5AE6; color: #fff; }
/* The canvas is never scaled: 1 sketch pixel = 1 CSS pixel, exactly like Processing */
.stage-scroll { flex: 1; overflow: auto; display: flex; }
.stage-canvas-wrap { margin: auto; padding: 12px; }
#runner { display: block; border: 0; width: 1200px; height: 800px; background: var(--stage); }
.stage-log {
  margin: 0; max-height: 28vh; overflow: auto; padding: 10px 16px; border-top: 1px solid #333;
  background: #111; color: #BDBDBD; font: 12px/1.45 ui-monospace, Menlo, Consolas, monospace;
}
.stage-log .log-err { color: #FF8A95; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0;
  background: var(--ink); color: var(--paper); padding: 10px 16px; border-radius: var(--radius-s);
  font-weight: 700; transition: opacity .2s, transform .2s; pointer-events: none; z-index: 60;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--rule); }
  #main { padding: 24px 16px 48px; }
  .figures { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .figure:nth-child(3) { border-left: 0; padding-left: 0; }
  .figure:nth-child(n+3) { border-top: 1px solid var(--rule); }
  .detail-head h1 { font-size: 28px; }
  .topbar { padding: 10px 16px; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
