/* reVision engine stylesheet. Tokens first, then layout, then components. */
:root {
  --rv-bg: #ffffff;
  --rv-bg-2: #f4f5f7;
  --rv-fg: #1d2129;
  --rv-muted: #5f6672;
  --rv-line: #e1e4e8;
  --rv-accent: #1f6feb;
  --rv-box: rgba(31, 111, 235, 0.14);
  --rv-box-edge: #1f6feb;
  --rv-pin: #d29922;
  --rv-rail-w: 48px;
  --rv-panel-w: 0px;
  --rv-code-w: 1fr;
  --rv-media-h: 200px;
  --rv-font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --rv-mono: ui-monospace, "Cascadia Code", Consolas, "Liberation Mono", monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --rv-bg: #0f1218; --rv-bg-2: #171b23; --rv-fg: #e6e8ec; --rv-muted: #9aa3b2;
    --rv-line: #2a3140; --rv-accent: #6ea8ff; --rv-box: rgba(110, 168, 255, 0.18); --rv-box-edge: #6ea8ff; --rv-pin: #e3b341;
  }
}
:root[data-theme="dark"] {
  --rv-bg: #0f1218; --rv-bg-2: #171b23; --rv-fg: #e6e8ec; --rv-muted: #9aa3b2;
  --rv-line: #2a3140; --rv-accent: #6ea8ff; --rv-box: rgba(110, 168, 255, 0.18); --rv-box-edge: #6ea8ff; --rv-pin: #e3b341;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body.rv {
  display: grid;
  grid-template-columns: var(--rv-rail-w) var(--rv-panel-w) minmax(0, 1fr);
  background: var(--rv-bg); color: var(--rv-fg); font: 15px/1.55 var(--rv-font);
  overflow: hidden;
}

/* Explicit grid placement: the panel is display:none when closed, so nothing may slide into its column. */
.rv-rail { grid-column: 1; }
.rv-panel { grid-column: 2; }
.rv-main { grid-column: 3; }

/* Rail */
.rv-rail { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 6px 0; background: var(--rv-bg-2); border-right: 1px solid var(--rv-line); }
.rv-rail-btn { width: 36px; height: 36px; border: 0; border-radius: 6px; background: transparent; color: var(--rv-muted); font-size: 18px; cursor: pointer; }
.rv-rail-btn:hover, .rv-rail-btn[aria-pressed="true"] { color: var(--rv-fg); background: var(--rv-bg); }
.rv-rail-spacer { flex: 1; }

/* Side panel */
.rv-panel { overflow: auto; padding: 12px 14px; background: var(--rv-bg-2); border-right: 1px solid var(--rv-line); }
.rv-panel[hidden] { display: none; }
.rv-panel h2 { font-size: 14px; margin: 4px 0 10px; text-transform: uppercase; letter-spacing: .04em; color: var(--rv-muted); }
.rv-panel h3 { font-size: 14px; margin: 12px 0 4px; }
.rv-panel ol, .rv-panel ul { margin: 0; padding-left: 18px; }
.rv-panel li { margin: 2px 0; }
.rv-panel a { color: var(--rv-fg); text-decoration: none; }
.rv-panel li.rv-current > a { color: var(--rv-accent); font-weight: 600; }
.rv-variant, .rv-theme { border: 1px solid var(--rv-line); background: var(--rv-bg); color: var(--rv-fg); border-radius: 6px; padding: 4px 8px; cursor: pointer; font: inherit; }
.rv-variant small { color: var(--rv-muted); }
.rv-theme[aria-pressed="true"] { border-color: var(--rv-accent); color: var(--rv-accent); }
.rv-theme-row { display: flex; gap: 6px; }

/* Main two-pane area */
.rv-main { display: grid; grid-template-columns: var(--rv-code-w) 6px minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); min-width: 0; min-height: 0; height: 100vh; }
.rv-code-pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; border-right: 1px solid var(--rv-line); tab-size: 4; }
.rv-code-head { display: flex; justify-content: space-between; gap: 12px; padding: 8px 12px; border-bottom: 1px solid var(--rv-line); background: var(--rv-bg-2); font-size: 13px; }
.rv-file { font-family: var(--rv-mono); }
.rv-commit { color: var(--rv-muted); text-decoration: none; white-space: nowrap; }
.rv-code-body { flex: 1; overflow: auto; padding: 8px 0; font: 13px/1.5 var(--rv-mono); }
.rv-line { display: flex; white-space: pre; padding: 0 12px 0 0; border-left: 3px solid transparent; }
.rv-ln { width: 56px; flex: none; text-align: right; padding-right: 14px; color: var(--rv-muted); user-select: none; }
.rv-line.rv-boxed { background: var(--rv-box); border-left-color: var(--rv-box-edge); }
.rv-line.rv-boxed.rv-box-start { box-shadow: inset 0 1px 0 var(--rv-box-edge); }
.rv-line.rv-boxed.rv-box-end { box-shadow: inset 0 -1px 0 var(--rv-box-edge); }
.rv-line.rv-boxed.rv-box-start.rv-box-end { box-shadow: inset 0 1px 0 var(--rv-box-edge), inset 0 -1px 0 var(--rv-box-edge); }
body.rv-pinned .rv-line.rv-boxed { border-left-color: var(--rv-pin); }

.rv-splitter { cursor: col-resize; background: var(--rv-line); }
.rv-splitter:hover, .rv-splitter:focus { background: var(--rv-accent); outline: none; }

.rv-right { display: grid; grid-template-rows: minmax(0, 1fr) auto; min-width: 0; height: 100vh; }
.rv-lesson { overflow: auto; padding: 20px 28px 40px; max-width: 78ch; }
.rv-crumb { margin: 0 0 4px; font-size: 13px; color: var(--rv-muted); }
.rv-lesson h1 { font-size: 26px; margin: 0 0 12px; }
.rv-lesson pre { background: var(--rv-bg-2); padding: 10px 12px; border-radius: 6px; overflow: auto; font: 13px/1.5 var(--rv-mono); }
.rv-lesson code { font-family: var(--rv-mono); font-size: 0.92em; }
.rv-lesson table { border-collapse: collapse; }
.rv-lesson td, .rv-lesson th { border: 1px solid var(--rv-line); padding: 4px 8px; }
.rv-pager { display: flex; justify-content: space-between; margin-top: 32px; padding-top: 12px; border-top: 1px solid var(--rv-line); }
.rv-pager a { color: var(--rv-accent); text-decoration: none; }

/* Anchor phrases, intrinsics, diagrams */
.rv-anchor { border-bottom: 2px solid var(--rv-box-edge); background: var(--rv-box); border-radius: 3px 3px 0 0; padding: 0 2px; cursor: pointer; }
.rv-anchor:hover, .rv-anchor:focus { outline: 2px solid var(--rv-box-edge); outline-offset: 1px; }
.rv-anchor.rv-pinned { border-bottom-color: var(--rv-pin); background: transparent; box-shadow: inset 0 -2px 0 var(--rv-pin); }
.rv-intrinsic { color: var(--rv-accent); text-decoration: none; border-bottom: 1px dotted var(--rv-accent); }
.rv-diagram { margin: 16px 0; text-align: center; }
.rv-diagram svg { max-width: 100%; height: auto; }
/* Graphviz emits black strokes and fills; recolour from tokens so diagrams follow the theme. */
.rv-svg text { fill: var(--rv-fg); }
.rv-svg .node polygon, .rv-svg .node ellipse, .rv-svg .node path, .rv-svg .edge path { stroke: var(--rv-fg); }
.rv-svg .edge polygon { fill: var(--rv-fg); stroke: var(--rv-fg); }
.rv-svg .node.rv-svg-anchor { cursor: pointer; }
.rv-svg .node.rv-node-active polygon, .rv-svg .node.rv-node-active ellipse { stroke: var(--rv-box-edge); stroke-width: 2.5; fill: var(--rv-box); }
.rv-diagram figcaption { font-size: 13px; color: var(--rv-muted); }

/* Media pane */
.rv-media { border-top: 1px solid var(--rv-line); background: var(--rv-bg-2); padding: 8px 12px; height: var(--rv-media-h); display: grid; grid-template-rows: auto minmax(0, 1fr); grid-template-columns: minmax(0, 1fr) auto; row-gap: 6px; column-gap: 0; }
.rv-media[hidden] { display: none; }
.rv-media audio, .rv-media video { width: 100%; max-height: 120px; grid-row: 1; grid-column: 1; }
/* Firefox sizes the player's grid row from a 150px default height, not its 40px controls (#45); 54px is Chromium's, so only Firefox changes. */
.rv-media audio { max-height: 54px; }
.rv-media-toggle { grid-row: 1; grid-column: 2; align-self: start; margin-left: 6px; border: 1px solid var(--rv-line); background: var(--rv-bg); color: var(--rv-fg); border-radius: 6px; padding: 4px 8px; cursor: pointer; font: 13px/1.4 var(--rv-font); }
.rv-media-toggle:focus-visible { outline: 2px solid var(--rv-accent); outline-offset: 1px; }
.rv-media-toggle[hidden] { display: none; }
.rv-media-toggle[aria-expanded="false"] .rv-media-chev { display: inline-block; transform: rotate(-90deg); }
.rv-transcript { margin: 0; padding: 0 0 0 20px; overflow: auto; font-size: 14px; grid-row: 2; grid-column: 1 / -1; }
.rv-seg { cursor: pointer; padding: 2px 4px; border-radius: 4px; color: var(--rv-muted); }
.rv-seg:hover, .rv-seg:focus { background: var(--rv-bg); outline: none; }
.rv-seg.rv-active { color: var(--rv-fg); background: var(--rv-box); }
.rv-media-error { color: var(--rv-muted); }
/* Media failed to load: message first, transcript scrolls below it, pane shrinks to content up to --rv-media-h. */
.rv-media.rv-media-failed { height: auto; max-height: var(--rv-media-h); }
.rv-media-failed .rv-media-error { grid-row: 1; grid-column: 1; margin: 0; }
/* Collapsed by the reader: only the toggle remains, as a full-width strip; the lesson takes the freed height. */
.rv-media.rv-media-collapsed { height: auto; max-height: none; padding: 0; border-top-width: 0; display: block; }
.rv-media-collapsed > :not(.rv-media-toggle) { display: none; }
.rv-media-collapsed .rv-media-toggle { display: block; width: 100%; margin: 0; border-radius: 0; border: 0; border-top: 1px solid var(--rv-line); text-align: left; }

/* Quiz */
.rv-quiz { margin-top: 28px; padding: 14px 16px; border: 1px solid var(--rv-line); border-radius: 8px; }
.rv-quiz h2 { margin: 0 0 8px; font-size: 18px; }
.rv-q { border: 0; padding: 0; margin: 0 0 12px; }
.rv-q legend { font-weight: 600; padding: 0; }
.rv-q label { display: block; margin: 2px 0; }
.rv-q-feedback { margin: 4px 0 0; font-size: 14px; }
.rv-q.rv-correct .rv-q-feedback { color: #1a7f37; }
.rv-q.rv-incorrect .rv-q-feedback { color: #cf222e; }
.rv-quiz button { border: 1px solid var(--rv-line); background: var(--rv-bg-2); color: var(--rv-fg); border-radius: 6px; padding: 6px 12px; cursor: pointer; font: inherit; }
.rv-quiz-score { font-weight: 600; }

/* Narrow layout: stack panes, rail becomes a top bar */
@media (max-width: 900px) {
  body.rv { grid-template-columns: 1fr; grid-template-rows: var(--rv-rail-w) auto minmax(0, 1fr); height: 100vh; }
  .rv-rail { grid-column: 1; grid-row: 1; flex-direction: row; border-right: 0; border-bottom: 1px solid var(--rv-line); padding: 0 6px; }
  .rv-panel { grid-column: 1; grid-row: 2; border-right: 0; border-bottom: 1px solid var(--rv-line); max-height: 40vh; }
  .rv-main { grid-column: 1; grid-row: 3; grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) 6px minmax(0, 1fr); height: auto; min-height: 0; }
  .rv-code-pane { border-right: 0; border-bottom: 1px solid var(--rv-line); }
  .rv-splitter { cursor: row-resize; }
  .rv-right { height: auto; }
  .rv-lesson { padding: 16px; }
  /* The media pane shares .rv-right with the lesson: cap it at half, so the lesson keeps at least as much (#13).
     Guarded because a var() inside min() defeats the cascade fallback: an engine without cq units would make the height auto and grow the pane to fit the transcript. */
  @supports (height: 1cqh) {
    .rv-right { container-type: size; }
    .rv-media { height: min(var(--rv-media-h), 50cqh); }
    .rv-media.rv-media-failed { max-height: min(var(--rv-media-h), 50cqh); }
  }
}
