/*
 * style.css — site chrome for the p5.tree API reference.
 * Light/dark follows prefers-color-scheme. The sketch stage is a fixed
 * dark box: the canvas never inherits the theme.
 */

:root {
  color-scheme: light dark;
  --bg:       #ffffff;
  --bg-2:     #f5f6f8;   /* sidebar, code, editor */
  --fg:       #1c1e22;
  --muted:    #646870;
  --border:   #e1e4e9;
  --accent:   #138d75;
  --accent-2: #a2338a;   /* types */
  --stage:    #0a0a0e;   /* fixed — not themed */
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --sidebar: 15rem;
  --stage-w: 400px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:       #131417;
    --bg-2:     #1b1d22;
    --fg:       #e4e6ea;
    --muted:    #9a9ea7;
    --border:   #2b2e35;
    --accent:   #3ec9ab;
    --accent-2: #e08fd0;
  }
}

/* ── Base ───────────────────────────────────────────────────────────────── */

* { box-sizing: border-box; }

body {
  margin: 0;
  display: grid;
  grid-template-columns: var(--sidebar) minmax(0, 1fr);
  min-height: 100vh;
  font: 16px/1.55 var(--sans);
  color: var(--fg);
  background: var(--bg);
}

a { color: var(--accent); }
a:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

img { max-width: 100%; height: auto; }

code, pre, textarea { font-family: var(--mono); }
code { font-size: .92em; padding: .1em .35em; border-radius: 4px; background: var(--bg-2); }
pre {
  padding: .9rem 1rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-2);
  line-height: 1.45;
  overflow-x: auto;
}
pre code { padding: 0; background: none; font-size: .85rem; }

blockquote { margin: 0; padding-left: 1rem; border-left: 3px solid var(--border); color: var(--muted); }
hr { border: 0; border-top: 1px solid var(--border); margin: 2rem 0; }

table { width: 100%; margin: .75rem 0 1.25rem; border-collapse: collapse; font-size: .9375rem; }
th, td { padding: .45rem .6rem; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
th { font-size: .8125rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
td > :first-child { margin-top: 0; }
td > :last-child  { margin-bottom: 0; }

/* ── Sidebar ────────────────────────────────────────────────────────────── */

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  padding: 1.25rem 1rem;
  background: var(--bg-2);
  border-right: 1px solid var(--border);
}
.brand {
  display: block;
  margin-bottom: 1rem;
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--fg);
  text-decoration: none;
}
.sidebar ul { margin: 0; padding: 0; list-style: none; }
.sidebar li a {
  display: block;
  padding: .3rem .5rem;
  border-radius: 4px;
  font: .875rem/1.5 var(--mono);
  color: var(--fg);
  text-decoration: none;
}
.sidebar li a:hover { background: var(--border); }
.sidebar li a[aria-current="page"] { background: var(--accent); color: #fff; }

/* Search — the field above the module list; results replace the list while typing. */
.sidebar .search {
  width: 100%;
  margin: 0 0 .75rem;
  padding: .35rem .5rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font: .875rem/1.5 var(--sans);
  color: var(--fg);
  background: var(--bg);
}
.sidebar .results li a { display: grid; grid-template-columns: 1fr auto; gap: 0 .5rem; }
.sidebar .results .owner { font-size: .75rem; color: var(--muted); }
.sidebar .results .hint {
  grid-column: 1 / -1;
  font: .75rem/1.4 var(--sans);
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sidebar .results .none { padding: .3rem .5rem; font: .875rem/1.5 var(--sans); color: var(--muted); }

/* ── Main ───────────────────────────────────────────────────────────────── */

main { min-width: 0; max-width: 76rem; padding: 2rem 2.5rem 3rem; }

h1, h2, h3 { line-height: 1.25; }
h1 { margin: 0 0 .5rem; font-size: 1.9rem; }
h2 { margin: 2.5rem 0 1rem; padding-bottom: .3rem; font-size: 1.4rem; border-bottom: 1px solid var(--border); }
h3 { margin: 2rem 0 .5rem; font-size: 1.05rem; }
h3 code { padding: 0; font-size: 1em; background: none; }

.lead { margin: 0 0 1rem; font-size: 1.15rem; color: var(--muted); }
.description > :first-child { margin-top: 0; }

.anchor { color: inherit; text-decoration: none; }
.anchor:hover { text-decoration: underline; text-decoration-color: var(--accent); }

.doclet, tr[id] { scroll-margin-top: 1rem; }
.doclet:target > h3 { margin-left: -.85rem; padding-left: .6rem; border-left: 3px solid var(--accent); }
tr:target td { background: color-mix(in srgb, var(--accent) 12%, transparent); }

.type { padding: 0; background: none; color: var(--accent-2); }
.opt { font-size: .75rem; color: var(--muted); }
tr.depth-1 td:first-child { padding-left: 1.6rem; }
tr.depth-2 td:first-child { padding-left: 2.6rem; }
.returns { margin: .5rem 0 1rem; }

footer {
  margin-top: 3rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  font-size: .8125rem;
  color: var(--muted);
}

/* ── Examples ───────────────────────────────────────────────────────────── */

.examples { margin-top: 1rem; }

.example {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: .75rem;
  margin: 1.25rem 0 2rem;
}
.example figcaption { font-size: .9375rem; color: var(--muted); }

.editor { min-width: 0; }
.editor textarea.source {            /* fallback when CodeMirror is absent */
  display: block;
  width: 100%;
  min-height: 14rem;
  padding: .6rem .8rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: .8125rem;
  line-height: 1.5;
  color: var(--fg);
  background: var(--bg-2);
  resize: vertical;
}

.stage {
  width: min(var(--stage-w), 100%);
  aspect-ratio: 4 / 3;
  border-radius: 6px;
  background: var(--stage);
  overflow: hidden;
}
.stage iframe { display: block; width: 100%; height: 100%; border: 0; }

.toolbar { display: flex; gap: .5rem; }
.toolbar button {
  padding: .3rem .9rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font: inherit;
  font-size: .875rem;
  color: var(--fg);
  background: var(--bg-2);
  cursor: pointer;
}
.toolbar button:hover { border-color: var(--muted); }
.toolbar button[data-run] { border-color: transparent; color: #fff; background: var(--accent); }
.toolbar button[data-run]:hover { filter: brightness(1.08); }

@media (min-width: 1200px) {
  .example { grid-template-columns: minmax(0, 1fr) var(--stage-w); }
  .example figcaption, .toolbar { grid-column: 1 / -1; }
  .stage { position: sticky; top: 1rem; align-self: start; }
}

/* ── CodeMirror ─────────────────────────────────────────────────────────── */

.editor .CodeMirror {
  height: auto;                      /* with viewportMargin: Infinity — grows to fit */
  border: 1px solid var(--border);
  border-radius: 6px;
  font: .8125rem/1.5 var(--mono);
  color: var(--fg);
  background: var(--bg-2);
}
.editor .CodeMirror-gutters { border-right: 1px solid var(--border); background: var(--bg-2); }
.editor .CodeMirror-linenumber { color: var(--muted); }
.editor .CodeMirror-cursor { border-left-color: var(--fg); }

@media (prefers-color-scheme: dark) {
  .editor .CodeMirror-selected,
  .editor .CodeMirror-focused .CodeMirror-selected { background: #2b3340; }
  .editor .cm-s-default .cm-keyword    { color: #c792ea; }
  .editor .cm-s-default .cm-atom,
  .editor .cm-s-default .cm-number     { color: #f78c6c; }
  .editor .cm-s-default .cm-def        { color: #82aaff; }
  .editor .cm-s-default .cm-variable   { color: #e4e6ea; }
  .editor .cm-s-default .cm-variable-2 { color: #addb67; }
  .editor .cm-s-default .cm-property   { color: #80cbc4; }
  .editor .cm-s-default .cm-operator   { color: #89ddff; }
  .editor .cm-s-default .cm-string,
  .editor .cm-s-default .cm-string-2   { color: #c3e88d; }
  .editor .cm-s-default .cm-comment    { color: #7f8c98; font-style: italic; }
  .editor .cm-s-default .cm-builtin    { color: #ffcb6b; }
  .editor .CodeMirror-matchingbracket  { color: #fff !important; text-decoration: none; }
}

/* ── Narrow ─────────────────────────────────────────────────────────────── */

@media (max-width: 760px) {
  body { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--border); }
  .sidebar ul { display: flex; flex-wrap: wrap; gap: .25rem; }
  main { padding: 1.25rem 1rem 2rem; }
}
