/* Code browser (#code-root). Colours come only from the shared tokens in assets/style.css.
   The viewer is a white "printout" sheet on the paper page; the file list sits beside it. */

#code-root {
  --code-sticky-top: 16px;
  --code-mono-size: 0.8125rem;
  font-family: var(--font-body);
  color: var(--ink);
  min-width: 0;
}
#code-root *,
#code-root *::before,
#code-root *::after { box-sizing: border-box; }
#code-root [hidden] { display: none !important; }

#code-root :focus { outline: none; }
#code-root :focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

#code-root .code-vh {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden;
  clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0;
}
#code-root .code-icon { display: block; flex: none; }
#code-root .code-empty { color: var(--ink-2); }

/* ---------- two-pane layout ---------- */

#code-root .code-browser {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 20px;
  align-items: start;
}
@media (min-width: 56.25em) {
  #code-root .code-browser {
    grid-template-columns: minmax(248px, 300px) minmax(0, 1fr);
    gap: 28px;
  }
  #code-root .code-side {
    position: sticky;
    top: var(--code-sticky-top);
    max-height: calc(100vh - var(--code-sticky-top) - 16px);
    max-height: calc(100dvh - var(--code-sticky-top) - 16px);
    display: flex;
    flex-direction: column;
  }
  /* The <details> element itself scrolls: its content slot does not take part in flex sizing. */
  #code-root .code-files {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-right: 8px;
    margin-right: -8px;
    scrollbar-width: thin;
    scrollbar-color: var(--rule) transparent;
  }
  #code-root .code-files > .code-files-summary { display: none; }
}

#code-root .code-side { min-width: 0; }
#code-root .code-view { min-width: 0; }

/* ---------- search ---------- */

#code-root .code-search { margin-bottom: 14px; }
#code-root .code-label {
  display: block;
  font-size: var(--step--1);
  font-weight: 600;
  color: var(--ink-2);
  margin-bottom: 6px;
}
#code-root .code-search-field { position: relative; }
#code-root .code-search-icon {
  position: absolute; left: 11px; top: 50%; transform: translateY(-50%);
  color: var(--ink-3); pointer-events: none;
}
#code-root .code-search-input {
  width: 100%;
  min-height: 42px;
  padding: 8px 12px 8px 36px;
  font: inherit;
  font-size: var(--step-0);
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  -webkit-appearance: none;
  appearance: none;
}
#code-root .code-search-input::placeholder { color: var(--ink-3); opacity: 1; }
#code-root .code-search-input:hover { border-color: var(--ink-3); }
#code-root .code-search-input:focus-visible { outline-offset: 0; border-color: var(--focus); }
#code-root .code-search-input::-webkit-search-cancel-button { cursor: pointer; }
#code-root .code-search-status {
  margin: 6px 0 0;
  font-size: var(--step--1);
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
  min-height: 1.4em;
}

/* ---------- pipeline strip ---------- */

/* ---------- file list ---------- */

#code-root .code-files { margin: 0; }
#code-root .code-files-summary {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 8px 12px;
  list-style: none;
  cursor: pointer;
  font-weight: 600;
  font-size: var(--step--1);
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
}
#code-root .code-files-summary::-webkit-details-marker { display: none; }
#code-root .code-files-chevron { color: var(--ink-3); transition: transform .15s ease; }
#code-root details[open] > summary .code-files-chevron { transform: rotate(90deg); }
#code-root .code-files-current {
  margin-left: auto;
  font-family: var(--font-mono);
  font-weight: 500;
  font-size: 0.75rem;
  color: var(--ink-2);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#code-root .code-files[open] > .code-files-summary { margin-bottom: 10px; }

#code-root .code-group { margin: 0 0 18px; }
#code-root .code-group-title {
  margin: 0 0 3px;
  font-family: var(--font-body);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--ink-2);
}
#code-root .code-list { list-style: none; margin: 0; padding: 0; }
#code-root .code-list li { margin: 0; }

#code-root .code-file {
  display: block;
  width: 100%;
  margin: 0 0 2px;
  padding: 7px 10px 8px 11px;
  text-align: left;
  font: inherit;
  color: inherit;
  background: transparent;
  border: 0;
  border-left: 3px solid transparent;
  border-radius: 0 var(--radius) var(--radius) 0;
  cursor: pointer;
}
#code-root .code-file:hover { background: var(--surface-2); }
#code-root .code-file:focus-visible { outline-offset: -2px; }
#code-root .code-file.is-current {
  background: var(--surface-2);
  border-left-color: var(--accent);
}
#code-root .code-file-top {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
}
#code-root .code-file-name {
  font-family: var(--font-mono);
  font-size: var(--code-mono-size);
  font-weight: 500;
  color: var(--ink);
  min-width: 0;
  overflow-wrap: anywhere;
}
#code-root .code-file-count {
  margin-left: auto;
  flex: none;
  padding: 0 7px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  line-height: 1.6;
  font-variant-numeric: tabular-nums;
  color: var(--accent-ink);
  background: var(--accent);
}
#code-root .code-file-count.is-name {
  color: var(--ink-2);
  background: transparent;
  border: 1px solid var(--rule);
  font-weight: 500;
}

#code-root .code-nomatch {
  margin: 4px 0 16px;
  font-size: var(--step--1);
  color: var(--ink-2);
}

/* kind badge, shared by list and viewer */
#code-root .code-kind {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  flex: none;
  padding: 0 6px;
  font-family: var(--font-body);
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
  line-height: 1.7;
  color: var(--ink-2);
  border: 1px solid var(--rule);
  border-radius: 4px;
}
#code-root .code-kind::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--ink-3); }
#code-root .code-kind--function::before { background: var(--fda); }
#code-root .code-kind--script::before { background: var(--accent); }

/* ---------- viewer sheet ---------- */

#code-root .code-view {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: clamp(18px, 2.8vw, 32px);
}
#code-root .code-title-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  margin-bottom: 10px;
}
#code-root .code-title {
  margin: 0;
  font-family: var(--font-mono);
  font-size: var(--step-2);
  font-weight: 500;
  line-height: 1.25;
  letter-spacing: -0.01em;
  color: var(--ink);
  overflow-wrap: anywhere;
  min-width: 0;
}
#code-root .code-title:focus-visible { outline-offset: 4px; border-radius: 2px; }
#code-root .code-meta {
  font-size: var(--step--1);
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
}
#code-root .code-summary {
  margin: 0 0 14px;
  font-family: var(--font-serif);
  font-size: 1.1875rem;
  line-height: 1.45;
  color: var(--ink);
  max-width: 68ch;
}

/* notes on out-of-date or reworded comments */

/* inputs / outputs */

/* relations */

/* figure */

/* ---------- source block ---------- */

#code-root .code-block {
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--code-bg);
  overflow: hidden;
  min-width: 0;
}
#code-root .code-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 12px;
  padding: 6px 8px 6px 12px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--rule);
}
#code-root .code-bar-name {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--ink-2);
  min-width: 0;
  overflow-wrap: anywhere;
}
#code-root .code-matchnav {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  font-size: 0.78rem;
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
}
#code-root .code-match-pos { margin-right: 4px; }
#code-root .code-bar-actions {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
#code-root .code-copy-status {
  font-size: 0.78rem;
  color: var(--ink-2);
  text-align: right;
}
#code-root .code-copy,
#code-root .code-iconbtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 32px;
  font: inherit;
  font-size: var(--step--1);
  font-weight: 500;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  cursor: pointer;
}
#code-root .code-copy { padding: 4px 12px; flex: none; }
#code-root .code-iconbtn { width: 32px; padding: 0; }
#code-root .code-copy:hover,
#code-root .code-iconbtn:hover { border-color: var(--ink-3); }
#code-root .code-iconbtn:disabled { color: var(--ink-3); cursor: default; border-color: var(--rule); }
#code-root .code-copy.is-done { border-color: var(--good); color: var(--good); }

#code-root .code-pre {
  position: relative;
  margin: 0;
  max-height: min(78vh, 62rem);
  overflow: auto;
  font-family: var(--font-mono);
  font-size: var(--code-mono-size);
  line-height: 1.6;
  color: var(--code-ink);
  background: var(--code-bg);
  tab-size: 4;
  -moz-tab-size: 4;
  white-space: pre;
  scrollbar-width: thin;
  scrollbar-color: var(--code-line) transparent;
}
#code-root .code-pre:focus-visible { outline-offset: -2px; }
#code-root .code-src {
  /* shrink-wraps the widest line so every line background spans the full scroll width */
  display: inline-block;
  min-width: 100%;
  padding: 8px 0 12px;
  font: inherit;
  color: inherit;
  background: none;
}
#code-root .code-line {
  display: inline-block;
  min-width: 100%;
  vertical-align: top;
  padding-right: 20px;
}
#code-root .code-line::before {
  content: attr(data-n);
  display: inline-block;
  position: sticky;
  left: 0;
  z-index: 1;
  width: calc(var(--code-gw, 3) * 1ch + 2.2ch);
  margin-right: 1.4ch;
  padding-right: 1ch;
  text-align: right;
  color: var(--code-line);
  background: var(--code-bg);
  border-right: 1px solid var(--rule);
  -webkit-user-select: none;
  user-select: none;
  font-variant-numeric: tabular-nums;
}
#code-root .code-line.is-hit,
#code-root .code-line.is-hit::before { background: var(--code-hl); }
#code-root .code-line.is-current::before { box-shadow: inset -3px 0 0 var(--accent); color: var(--ink); }
#code-root .code-line.is-edited::before { box-shadow: inset 3px 0 0 var(--warn); color: var(--ink-2); }

#code-root .code-src ::highlight(code-search) {
  background-color: var(--mark);
  color: var(--mark-ink);
}

/* highlight.js tokens (own rules, no external theme) */
#code-root .hljs-keyword { color: var(--code-kw); font-weight: 500; }
#code-root .hljs-built_in { color: var(--code-fn); }
#code-root .hljs-title,
#code-root .hljs-title.function_ { color: var(--code-fn); font-weight: 500; }
#code-root .hljs-string { color: var(--code-str); }
#code-root .hljs-number,
#code-root .hljs-literal { color: var(--code-num); }
#code-root .hljs-comment { color: var(--code-com); }
#code-root .hljs-doctag { color: var(--code-com); font-weight: 500; }
#code-root .hljs-params { color: var(--code-ink); }
#code-root .hljs-meta,
#code-root .hljs-section { color: var(--code-kw); }

/* ---------- wider screens ---------- */

/* ---------- phones ---------- */

@media (max-width: 56.249em) {
  #code-root .code-side { margin-bottom: 0; }
  #code-root .code-pre { max-height: 70vh; }
  #code-root .code-view { padding: 16px 14px 14px; }
}
@media (max-width: 30em) {
  #code-root .code-title { font-size: var(--step-1); }
  #code-root .code-summary { font-size: var(--step-0); }
  #code-root .code-bar-name { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  #code-root .code-files-chevron { transition: none; }
}
