/* live-cells.css — styling for the button-free live-code layer.
 *
 * Goal: the page looks identical whether or not the editors have mounted
 * (see live-cells.js). The only visible additions are the per-cell Run chip
 * and, once a student runs something, the floating status badge. The editor
 * copies the static <pre>'s metrics exactly; live output areas are relocated
 * into real .cell_output containers so custom.css styles live results like
 * baked ones. */

/* --- fully hidden inputs (`# hide` in an interactive notebook) ---------
 * Tagged `icm-hide-input` instead of myst-nb's `remove-input` because the
 * source must stay in the DOM (invisible) for the live run chain — a hidden
 * setup cell still has to execute before the cells below it. */
div.cell.tag_icm-hide-input .cell_input {
  display: none;
}

/* With the code hidden, nothing should announce that a cell produced this —
 * drop the OUTPUT tag so the figure or player stands on its own. */
div.cell.tag_icm-hide-input .cell_output::before {
  content: none;
}

/* --- per-cell Run chip --- */

.cell .cell_input {
  position: relative;
}

.live-run-chip {
  position: absolute;
  top: 0.35em;
  right: 0.35em;
  z-index: 2;
  padding: 0.1em 0.6em;
  font-size: 0.72rem;
  font-family: var(--pst-font-family-base, sans-serif);
  color: var(--live-chip-fg, #5a5a5a);
  background: var(--live-chip-bg, #ffffff);
  border: 1px solid var(--live-chip-border, #d0d0d0);
  border-radius: 999px;
  cursor: pointer;
  opacity: 0.65;
  transition: opacity 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.live-run-chip:hover {
  opacity: 1;
  color: var(--icm-accent, #c41230);
  border-color: var(--icm-accent, #c41230);
}

.live-run-chip.live-busy {
  opacity: 1;
  color: var(--icm-accent, #c41230);
  border-color: var(--icm-accent, #c41230);
  animation: live-pulse 1.1s ease-in-out infinite;
  pointer-events: none;
}

@keyframes live-pulse {
  50% { opacity: 0.45; }
}

/* The copy button overlaps our chip's corner; keep it clear of the chip. */
.cell .cell_input .copybtn {
  right: 4.5em;
}

/* --- per-cell run state + execution time (VSCode-notebook style) -------
 * live-cells.js tags executed cells .live-running / .live-ran / .live-failed
 * and appends a .live-exec-time badge; the cell's left bar takes the state
 * color. */
html[data-theme="light"] {
  --live-ok:  #2e8b57;   /* success green   */
  --live-err: #c41230;   /* error red       */
  --live-run: #c8a200;   /* running amber   */
  /* Editor palette: token colors mirror the theme's light pygments sheet;
   * the panel bg follows custom.css --live-code-bg (site-warm), which also
   * recolors the static <pre> so the two stay pixel-identical. */
  --cm-bg: #f2ebe1;
  --cm-fg: #292929;
  --cm-keyword: #6730c5;
  --cm-builtin: #7f4707;
  --cm-number: #7f4707;
  --cm-string: #00622f;
  --cm-operator: #00622f;
  --cm-comment: #515151;
  --cm-def: #005b82;
  --cm-name: #080808;
  /* Run chip / status pill chrome. */
  --live-chip-bg: #ffffff;
  --live-chip-border: #d0d0d0;
  --live-chip-fg: #5a5a5a;
}
html[data-theme="dark"] {
  --live-ok:  #4cc38a;
  --live-err: #ef6b78;
  --live-run: #e0c04a;
  /* Token colors mirror pygments' dark palette; the panel bg follows
   * custom.css --live-code-bg (site slate) on both static and mounted. */
  --cm-bg: #1c242a;
  --cm-fg: #f8f8f2;
  --cm-keyword: #dcc6e0;
  --cm-builtin: #ffd900;
  --cm-number: #ffd900;
  --cm-string: #abe338;
  --cm-operator: #abe338;
  --cm-comment: #ffd900;
  --cm-def: #00e0e0;
  --cm-name: #f8f8f2;
  --live-chip-bg: var(--icm-box-bg, #292521);
  --live-chip-border: var(--icm-box-border, #453f39);
  --live-chip-fg: #d5d2cf;
}

/* Recolor the left bar by state — the cell input and the collapsed "Show
 * setup" bar. The extra specificity + !important beat custom.css's iron
 * spine. */
body.live-active div.cell.live-running .cell_input,
body.live-active div.cell.live-running details.hide:not([open]) > summary {
  border-left-color: var(--live-run) !important;
}
body.live-active div.cell.live-ran .cell_input,
body.live-active div.cell.live-ran details.hide:not([open]) > summary {
  border-left-color: var(--live-ok) !important;
}
body.live-active div.cell.live-failed .cell_input,
body.live-active div.cell.live-failed details.hide:not([open]) > summary {
  border-left-color: var(--live-err) !important;
}

/* Execution-time badge, pinned to the input's bottom-right. Ticks while
 * running, freezes at the final duration, takes the state color. */
.cell .cell_input .live-exec-time {
  position: absolute;
  bottom: 0.3em;
  right: 0.55em;
  z-index: 2;
  padding: 0 0.35em;
  border-radius: 3px;
  background: color-mix(in srgb, var(--pst-color-on-background) 72%, transparent);
  font-family: var(--pst-font-family-base, sans-serif);
  font-size: 0.7rem;
  font-variant-numeric: tabular-nums;
  line-height: 1.5;
  color: var(--icm-accent-3);
  pointer-events: none;
}
div.cell.live-running .cell_input .live-exec-time { color: var(--live-run); }
div.cell.live-ran     .cell_input .live-exec-time { color: var(--live-ok); }
div.cell.live-failed  .cell_input .live-exec-time { color: var(--live-err); }

/* --- floating status pill / connected badge --- */

.live-status {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  z-index: 1000;
  max-width: 24rem;
  padding: 0.5em 0.9em;
  font-size: 0.8rem;
  font-family: var(--pst-font-family-base, sans-serif);
  font-variant-numeric: tabular-nums;
  background: var(--live-chip-bg, #ffffff);
  border: 1px solid var(--live-chip-border, #d0d0d0);
  border-left: 3px solid #9a9a9a;
  border-radius: 6px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
}

.live-status-busy { border-left-color: var(--live-run, #c8a200); }
.live-status-error { border-left-color: var(--live-err, #c41230); }

/* Persistent low-key badge once the kernel is up. */
.live-status-connected {
  border-left-color: var(--live-ok, #2e8b57);
  color: #2e6b47;
  font-size: 0.74rem;
  padding: 0.35em 0.7em;
  opacity: 0.92;
}
html[data-theme="dark"] .live-status-connected {
  color: var(--live-ok);
}

/* Live plotly widgets: the modebar collides with the page's svg/button CSS
 * and the widgets' axes are fixedrange — sliders are the interface, so drop
 * the toolbar rather than restyle it. */
.cell_output.live-output .js-plotly-plot .modebar-container {
  display: none;
}

/* Baked plotly figures render into their .icm-plotly-fig container. */
.icm-plotly-fig {
  margin: 0.4rem 0;
}

/* --- autorun placeholder ---
 * Holds the spot for a widget that runs on page load (`# autorun` cells),
 * so the page doesn't jump when it arrives. Removed once the run settles. */
.live-autorun-wait {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 18rem;
  padding: 1em;
  text-align: center;
  font-size: 0.85rem;
  font-family: var(--pst-font-family-base, sans-serif);
  color: var(--pst-color-text-muted, #6d6e71);
  border: 1px dashed var(--pst-color-border, #d0d0d0);
  border-radius: 6px;
  animation: live-autorun-pulse 2s ease-in-out infinite;
}

@keyframes live-autorun-pulse {
  0%, 100% { opacity: 0.85; }
  50% { opacity: 0.45; }
}

/* --- editors: match the static look exactly ---------------------------
 * Geometry measured from the built page: .cell_input pre → radius 4px,
 * padding 16px, menlo/consolas 13px, line-height 16.25px. Colors come from
 * the --cm-* vars above (one pair per theme, mirroring pygments.css).
 * !important on bg/fg: the TeachBooks code.css pins the editor to theme
 * surface colors with !important, which drift off pygments in dark mode. */

/* Thebe's own button bars are unused — the chips are the run path and page
 * reload is the reset. */
body.live-active .thebe-controls {
  display: none !important;
}

/* thebe 0.9.3 mounts CodeMirror 5 inside .cell_input. Heights auto: thebe's
 * stylesheet fixes a height. */
body.live-active .thebe-cell,
body.live-active .thebe-cell .CodeMirror {
  height: auto !important;
  min-height: 0 !important;
}

body.live-active .thebe-cell {
  margin: 0;
}

/* thebe wraps the editor and (former) output area with 8px margins — +16px
 * per cell versus the static page. The live output area is relocated out,
 * leaving .thebe-output as an empty husk. */
body.live-active .thebe-cell .thebe-input,
body.live-active .thebe-cell .thebe-output {
  margin: 0;
}

body.live-active .thebe-cell .CodeMirror {
  background: var(--cm-bg, #f3f4f5) !important;
  border: 0;
  border-radius: 4px;
  font-family: menlo, consolas, "DejaVu Sans Mono", monospace;
  font-size: 13px;
  line-height: 16.25px;
  color: var(--cm-fg, #222832) !important;
}

body.live-active .thebe-cell .CodeMirror-lines {
  padding: 16px;
}

/* CM5 hides native scrollbars with a -50px margin / +50px padding pair on
 * .CodeMirror-scroll; inside the theme's hide-input <details> that breaks
 * and adds 50px of phantom height. Editors here auto-size, so drop the
 * trick: grow with content, scroll horizontally only on overflow.
 * !important required — CM's stock sheet sets `overflow: scroll !important`.
 * CM's own scrollbar widgets are then redundant. */
body.live-active .thebe-cell .CodeMirror-scroll {
  margin-bottom: 0;
  margin-right: 0;
  padding-bottom: 0;
  overflow-x: auto !important;
  overflow-y: hidden !important;
}

body.live-active .thebe-cell .CodeMirror-vscrollbar,
body.live-active .thebe-cell .CodeMirror-hscrollbar,
body.live-active .thebe-cell .CodeMirror-scrollbar-filler {
  display: none !important;
}

/* CM's "heightForcer": a classless absolute div, hardcoded 50px, placed past
 * the document. With the margin trick gone it's 50px of phantom scroll under
 * every cell. It is the only classless direct child of the scroll element. */
body.live-active .thebe-cell .CodeMirror-scroll > div:not([class]) {
  display: none !important;
}

body.live-active .thebe-cell .CodeMirror pre.CodeMirror-line,
body.live-active .thebe-cell .CodeMirror pre.CodeMirror-line-like {
  padding: 0;
}

/* Syntax colors: CodeMirror's python tokens mapped to the active theme's
 * pygments palette via the --cm-* vars (one rule set, two var blocks).
 * !important required to beat code.css's light !important palette — without
 * it the look would split across themes. */
body.live-active .CodeMirror .cm-keyword { color: var(--cm-keyword, #6730c5) !important; }
body.live-active .CodeMirror .cm-atom { color: var(--cm-keyword, #6730c5) !important; }
body.live-active .CodeMirror .cm-builtin { color: var(--cm-builtin, #7f4707) !important; }
body.live-active .CodeMirror .cm-number { color: var(--cm-number, #7f4707) !important; }
body.live-active .CodeMirror .cm-string { color: var(--cm-string, #00622f) !important; }
body.live-active .CodeMirror .cm-string-2 { color: var(--cm-string, #00622f) !important; }
body.live-active .CodeMirror .cm-operator { color: var(--cm-operator, #00622f) !important; }
body.live-active .CodeMirror .cm-comment { color: var(--cm-comment, #515151) !important; }
body.live-active .CodeMirror .cm-def { color: var(--cm-def, #005b82) !important; }
body.live-active .CodeMirror .cm-variable,
body.live-active .CodeMirror .cm-property { color: var(--cm-name, #080808) !important; }

/* The book's pygments theme is uniformly regular weight; CodeMirror bolds
 * keywords by default. */
body.live-active .CodeMirror span[class*="cm-"] {
  font-weight: 400 !important;
  font-style: normal !important;
}

/* --- live outputs -------------------------------------------------------
 * The relocated container carries .cell_output, so custom.css applies
 * as-is; below only neutralizes JupyterLab's own chrome and hides the
 * container until the cell actually produces something. */

body.live-active .cell_output.live-output:has(.jp-OutputArea:empty) {
  display: none;
}

body.live-active .live-output .jp-OutputArea,
body.live-active .live-output .jp-OutputArea-output {
  background: transparent;
}

body.live-active .live-output .jp-OutputPrompt {
  display: none;
}

body.live-active .live-output .jp-OutputArea-output {
  padding: 0;
}

body.live-active .live-output .jp-RenderedHTMLCommon {
  padding: 0;
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

/* Python errors: a red-spined card (the rail's OUTPUT tag flips to a red
 * ERROR). thebe's rendermime emits tracebacks under the stderr mime type,
 * so live-cells.js tags them .live-error to tell them apart from ordinary
 * stderr. */
body.live-active .live-output [data-mime-type="application/vnd.jupyter.error"],
body.live-active .live-output .live-error {
  display: block;
  padding: 0.5rem 0.75rem;
  background: rgba(196, 18, 48, 0.06);
  border-left: 3px solid var(--cmu-red, #c41230);
  border-radius: 4px;
}

/* Ordinary stderr (tqdm progress, warnings) is usually noise — muted text
 * on a faint tint with a thin gold accent. */
body.live-active .live-output [data-mime-type="application/vnd.jupyter.stderr"]:not(.live-error) {
  display: block;
  padding: 0.4rem 0.65rem;
  background: color-mix(in srgb, var(--icm-accent-2, #b07e00) 7%, transparent);
  border-left: 2px solid color-mix(in srgb, var(--icm-accent-2, #b07e00) 45%, transparent);
  border-radius: 4px;
  color: var(--pst-color-text-muted);
}

div.cell div.cell_output:has([data-mime-type="application/vnd.jupyter.error"])::before,
div.cell div.cell_output:has(.live-error)::before {
  content: "error";
  color: var(--cmu-red, #c41230);
}

/* Traceback / ANSI colors, mapped to the book's math-highlight palette
 * (the --m-* variables already carry light- and dark-mode values). */
.live-output .ansi-red-fg, .live-output .ansi-red-intense-fg { color: var(--m-red, #dc2830); }
.live-output .ansi-green-fg, .live-output .ansi-green-intense-fg { color: var(--m-green, #007d4d); }
.live-output .ansi-yellow-fg, .live-output .ansi-yellow-intense-fg { color: var(--m-yellow, #b89b00); }
.live-output .ansi-blue-fg, .live-output .ansi-blue-intense-fg { color: var(--m-blue, #0271ae); }
.live-output .ansi-cyan-fg, .live-output .ansi-cyan-intense-fg { color: var(--m-cyan, #007d80); }
.live-output .ansi-magenta-fg, .live-output .ansi-magenta-intense-fg { color: var(--m-magenta, #c20caa); }
.live-output .ansi-bold { font-weight: 600; }

/* Fresh results announce themselves gently: new output blocks fade in and
 * the cell's rail pulses Carnegie red before settling back. */
.live-output .jp-OutputArea-child {
  animation: live-out-in 0.25s ease-out backwards;
}

@keyframes live-out-in {
  from { opacity: 0; transform: translateY(4px); }
}

.cell_output.live-fresh {
  animation: live-rail-pulse 1.4s ease-out;
}

@keyframes live-rail-pulse {
  0%, 55% { border-left-color: var(--cmu-red, #c41230); }
}

/* --- Record button -------------------------------------------------------
 * live-cells.js renders pq.record()'s control by reusing the book's audio
 * chip inside an `audio-output` card, so it matches a pq.play() output; the
 * only new bits are the record dot and the muted caption suffix. */
/* Shared min-width so the record card and the audio cards beside it line
 * up regardless of caption length. The descendant selector outspecifies the
 * base `.audio-block.audio-output` (min-width: 14rem). */
.cell_output .audio-block.audio-output,
.jp-OutputArea .audio-block.audio-output {
  min-width: 18rem;
}
.ba-record {
  -webkit-appearance: none;
  appearance: none;
}
.ba-record .ba-dot {
  position: relative; /* above the ring */
  width: 0.62em;
  height: 0.62em;
  border-radius: 50%;
  background: currentColor;
}
.audio-output-name .ba-sub { color: var(--pst-color-text-muted, #8a8a8a); font-weight: 500; }
