/* --- Loading banner: compact, collapses when ready --- */
.webr-loading-banner {
  padding: 8px 16px;
  background: #f0f4ff;
  color: #3b5998;
  text-align: center;
  margin: 0 0 8px 0;
  border-radius: 6px;
  font-size: 13px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  border: 1px solid #c7d2fe;
  transition: max-height 0.4s ease, padding 0.4s ease, margin 0.4s ease,
              opacity 0.4s ease, border-color 0.4s ease, background 0.4s ease, color 0.4s ease;
  overflow: hidden;
  max-height: 50px;
}
.webr-loading-banner.ready {
  background: #ecfdf5;
  color: #166534;
  border-color: #86efac;
}
.webr-loading-banner.hidden {
  max-height: 0;
  padding: 0 16px;
  margin: 0;
  border-color: transparent;
  opacity: 0;
}

/* --- Run All: inline inside engagement-header strip --- */
.webr-runall-inline {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
  opacity: 0;
  transition: opacity 0.3s ease;
}
.webr-runall-inline.visible { opacity: 1; }
.webr-runall-inline .webr-runall-label {
  font-size: 12px;
  color: #475569;
  font-weight: 400;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.webr-runall-inline .webr-runall-label::before {
  content: '';
  width: 6px; height: 6px;
  border-radius: 50%;
  background: #22c55e;
  flex-shrink: 0;
}
/* --- Run All: standalone fallback bar --- */
.webr-runall-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 6px 0;
  margin: 0 0 16px 0;
  border-bottom: 1px solid #e5e7eb;
  opacity: 0;
  transform: translateY(-4px);
  transition: opacity 0.3s ease, transform 0.3s ease;
}
.webr-runall-bar.visible { opacity: 1; transform: translateY(0); }
.webr-runall-bar .webr-runall-label {
  font-size: 12px;
  color: #475569;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-weight: 400;
  display: flex;
  align-items: center;
  gap: 6px;
}
.webr-runall-bar .webr-runall-label::before {
  content: '';
  width: 6px; height: 6px;
  border-radius: 50%;
  background: #22c55e;
  flex-shrink: 0;
}
/* --- Run All button (shared) --- */
.webr-runall-btn {
  background: none;
  color: #475569;
  border: 1px solid #d1d5db;
  padding: 3px 10px;
  border-radius: 5px;
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}
.webr-runall-btn:hover { color: #1e293b; border-color: #94a3b8; background: #f8fafc; }
.webr-runall-btn:disabled { color: #94a3b8; cursor: wait; }
.webr-runall-btn.running { color: #d97706; border-color: #d97706; animation: pulse-run 1.5s ease-in-out infinite; }

/* --- Code block container (dark theme, pythoncompiler.io style) --- */
.webr-container {
  position: relative;
  margin: 22px 0;
  border: 0.8px solid rgba(51, 65, 85, 0.5);
  border-radius: 8px;
  overflow: hidden;
  background: #0d1117;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: box-shadow 150ms ease-out, border-color 150ms ease-out;
}
.webr-container:hover {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
  border-color: rgba(51, 65, 85, 0.8);
}
.webr-container:focus-within {
  border-color: rgba(51, 65, 85, 0.8);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}
@keyframes webrIntroPulse {
  0%, 100% { box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3); }
  50%      { box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3), 0 0 0 4px rgba(5, 150, 105, 0.25); }
}
.webr-container:first-of-type { animation: webrIntroPulse 2s ease-out 1; }

/* --- Code block header bar (sticky, dark theme) --- */
.webr-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 12px;
  min-height: 36px;
  background: #1e293b;
  border-bottom: 0.8px solid rgba(51, 65, 85, 0.5);
  font-family: 'IBM Plex Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  /* Static by default — engagement progress bar owns the sticky slot.
     Avoids CLS when engagement.css loads and overrides sticky→static. */
  position: static;
  z-index: 2;
}
.webr-header-left  { display: flex; align-items: center; gap: 8px; min-width: 0; }
.webr-header-right { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.webr-header-label {
  font-size: 12px; font-weight: 500; color: #e2e8f0; letter-spacing: 0.02em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  min-width: 0; max-width: 100%;
}
.webr-header-badge {
  background: #1e3a8a; color: #93c5fd; padding: 2px 7px;
  border-radius: 4px; font-size: 11px; font-weight: 700; letter-spacing: 0.03em;
  flex-shrink: 0;
}

/* --- Run button shortcut hint --- */
.webr-run-shortcut {
  font-size: 10px; opacity: 0.6; margin-left: 3px; font-weight: 400;
  font-variant-numeric: tabular-nums;
}

/* --- Bottom button bar: KILLED. DOM nodes stay in place (md2html contract),
       CSS hides them. Copy + Run live in the header now. --- */
.webr-buttons { display: none !important; }
.webr-buttons-left, .webr-buttons-right { display: none !important; }
.webr-reset-btn { display: none !important; }

/* --- Keyboard hint --- */
.webr-kbd-hint {
  font-size: 10px;
  color: #64748b;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}
.webr-kbd-hint kbd {
  background: #334155;
  border: 1px solid #475569;
  border-radius: 3px;
  padding: 1px 4px;
  font-size: 10px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  box-shadow: 0 1px 0 rgba(0,0,0,0.15);
  color: #94a3b8;
}
/* --- Copy button (promoted to header, icon-only 28x28 ghost, no border) --- */
.webr-copy-btn {
  width: 28px;
  height: 28px;
  padding: 0;
  background: transparent;
  border: none;
  color: #94a3b8;
  border-radius: 5px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 120ms ease, color 120ms ease;
}
.webr-copy-btn:hover  { background: #334155; color: #e2e8f0; }
.webr-copy-btn:focus-visible { outline: 2px solid #3b82f6; outline-offset: 2px; }
.webr-copy-btn.copied { color: #4ade80; }

/* --- Code editor: dark theme, reserves space to prevent CLS --- */
.webr-editor {
  position: relative;
  min-height: 80px;
  font-family: 'Fira Code', 'SF Mono', 'Consolas', ui-monospace, monospace;
  font-variant-ligatures: contextual;
  font-feature-settings: 'calt' 1;
  font-size: 14px;
  line-height: 1.4;
  padding: 8px 12px;
  background: #020617;
  white-space: pre;
  overflow: hidden;
  color: #e3e8ef;
  cursor: text;
  outline: none;
  counter-reset: ln;
}
/* Pre-hydration keyboard focus — subtle so it doesn't compete with the code.
   Only fires on tab-to focus (not click); post-hydration the caret itself
   is the focus indicator. */
.webr-editor:focus-visible { outline: 1px dotted #475569; outline-offset: -4px; }
/* Right-edge scroll affordance — toggled by JS when editor overflows */
.webr-editor::after {
  content: '';
  position: absolute;
  right: 0; top: 0; bottom: 0;
  width: 24px;
  background: linear-gradient(to right, rgba(2,6,23,0), rgba(2,6,23,1));
  pointer-events: none;
  opacity: 0;
  transition: opacity 150ms ease;
}
.webr-editor.has-overflow::after { opacity: 1; }

/* After the CodeJar swap the outer .webr-editor becomes a thin wrapper; the
   inner contenteditable div is the visible code area. Padding moves inward so
   the blue focus ring (on .webr-container) still hugs the container edge. */
.webr-editor.webr-editor-hydrated { padding: 0; overflow: visible; }

/* Editable code surface. Uses contenteditable + CodeJar — not a textarea —
   so live Prism syntax highlighting can paint token spans inside it. Layout
   matches the static Pygments render exactly so the click-to-edit swap is
   visually continuous. */
.webr-editor-input {
  display: block;
  width: 100%;
  min-height: 80px;
  max-height: 520px;
  box-sizing: border-box;
  margin: 0;
  padding: 8px 12px;
  font-family: inherit;
  font-size: 14px;
  line-height: 1.4;
  color: #e3e8ef;
  background: #020617;
  border: 0;
  outline: none;
  white-space: pre;
  overflow: auto;
  tab-size: 2;
  -moz-tab-size: 2;
  caret-color: #60a5fa;
}
.webr-editor-input::selection { background: #334155; }
.webr-editor-input:focus { outline: none; }
.webr-editor-input:focus-visible { outline: none; }

/* --- Prism token colors mapped to the Pygments palette above --- */
/* Base text color stays inherited from .webr-editor (#e3e8ef). */
.webr-editor .token.comment,
.webr-editor .token.prolog,
.webr-editor .token.doctype,
.webr-editor .token.cdata         { color: #64748b; font-style: italic; }
.webr-editor .token.string,
.webr-editor .token.regex,
.webr-editor .token.char           { color: #86efac; }
.webr-editor .token.keyword        { color: #f87171; font-weight: 500; }
.webr-editor .token.boolean,
.webr-editor .token.constant,
.webr-editor .token.null           { color: #c084fc; font-weight: 500; }
.webr-editor .token.number,
.webr-editor .token.ellipsis       { color: #60a5fa; }
.webr-editor .token.operator,
.webr-editor .token.percent-operator { color: #f87171; }
.webr-editor .token.function       { color: #e2e8f0; }
.webr-editor .token.variable       { color: #e3e8ef; }
.webr-editor .token.punctuation    { color: #cbd5e1; }
.webr-editor .token.important      { color: #fb923c; font-weight: 500; }

/* --- Build-time syntax highlighting (Pygments) ---
   Static editors carry <span class="cl"> line wrappers plus Pygments class
   spans. Line numbers rendered via CSS generated content (::before) so they
   don't appear in textContent or user copy-paste. */
.webr-editor .cl { counter-increment: ln; display: inline; }
.webr-editor .cl::before {
  content: counter(ln);
  display: inline-block;
  /* content-box + nowrap so two/three-digit line numbers (10+) never wrap to a
     second row inside the gutter. The global *{box-sizing:border-box} otherwise
     shrinks the 28px box to a 16px content area, wrapping "10" to "1"/"0" and
     disturbing the code below (esp. after CodeJar hydration on long blocks). */
  box-sizing: content-box;
  width: 2.4ch;
  padding-right: 12px;
  text-align: right;
  white-space: nowrap;
  color: #6e7681;
  user-select: none;
  -webkit-user-select: none;
}
/* Pygments token classes → dark theme palette */
.webr-editor .c,
.webr-editor .c1,
.webr-editor .cm,
.webr-editor .cp,
.webr-editor .cs  { color: #64748b; font-style: italic; }
.webr-editor .k,
.webr-editor .kc,
.webr-editor .kd,
.webr-editor .kn,
.webr-editor .kp,
.webr-editor .kr  { color: #f87171; font-weight: 500; }
.webr-editor .kt,
.webr-editor .nb,
.webr-editor .bp  { color: #c084fc; font-weight: 500; }
.webr-editor .o,
.webr-editor .ow  { color: #f87171; }
.webr-editor .nf,
.webr-editor .fm  { color: #e2e8f0; }
.webr-editor .s,
.webr-editor .s1,
.webr-editor .s2,
.webr-editor .sa,
.webr-editor .sb,
.webr-editor .sc,
.webr-editor .sd,
.webr-editor .se,
.webr-editor .sh,
.webr-editor .si,
.webr-editor .sr,
.webr-editor .ss  { color: #86efac; }
.webr-editor .m,
.webr-editor .mi,
.webr-editor .mf,
.webr-editor .mh,
.webr-editor .mo,
.webr-editor .il  { color: #60a5fa; }
.webr-editor .na,
.webr-editor .nt  { color: #fb923c; }
.webr-editor .nv,
.webr-editor .vc,
.webr-editor .vg,
.webr-editor .vi  { color: #fb923c; }
.webr-editor .nd  { color: #c4b5fd; font-style: italic; }
.webr-editor .ne  { color: #f87171; font-weight: 500; }
.webr-editor .err { color: #fca5a5; }

/* --- Run button (green, pythoncompiler.io style) --- */
.webr-run-btn {
  min-height: 26px;
  padding: 4px 10px;
  background: #059669;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.2);
  transition: transform 120ms cubic-bezier(.4,0,.2,1),
              background 120ms ease,
              box-shadow 120ms ease;
}
.webr-run-btn:hover  { background: #047857; transform: translateY(-1px); box-shadow: 0 2px 6px rgba(0,0,0,0.3); }
.webr-run-btn:active { background: #065f46; transform: translateY(0); box-shadow: none; }
.webr-run-btn:disabled { background: #065f46; color: rgba(255,255,255,0.5); cursor: wait; transform: none; box-shadow: none; }
.webr-run-btn:focus-visible { outline: 2px solid #34d399; outline-offset: 2px; }
.webr-run-btn.running { background: #d29922; animation: pulse-run 1.5s ease-in-out infinite; }
@keyframes pulse-run { 0%, 100% { opacity: 1; } 50% { opacity: 0.8; } }

/* First unrun code block subtly pulses to pull the first click. */
.webr-container.engagement-unrun-first .webr-run-btn {
  animation: webrRunInvite 2.4s cubic-bezier(0.4, 0, 0.2, 1) 3;
}
@keyframes webrRunInvite {
  0%, 100% { box-shadow: 0 1px 2px rgba(0,0,0,0.08), 0 0 0 0 rgba(5, 150, 105, 0.35); }
  50%      { box-shadow: 0 1px 2px rgba(0,0,0,0.08), 0 0 0 6px rgba(5, 150, 105, 0); }
}
@media (prefers-reduced-motion: reduce) {
  .webr-container.engagement-unrun-first .webr-run-btn { animation: none; }
}

/* --- Output panel (dark theme, pythoncompiler.io style) --- */
.webr-output {
  display: block;
  min-height: 0;
  margin: 0;
  padding: 0 16px 0 16px;
  background: #020617;
  color: #edeef3;
  font-family: 'Fira Code', 'SF Mono', 'Consolas', ui-monospace, monospace;
  font-size: 13px;
  line-height: 1.55;
  max-height: 400px;
  overflow-y: auto;
  white-space: pre-wrap;
  border: 0 none;
  border-top: 0.8px solid transparent;
  border-radius: 0;
  transition: min-height 180ms ease-out, padding 180ms ease-out,
              opacity 150ms ease-out;
}
.webr-output::-webkit-scrollbar { width: 8px; }
.webr-output::-webkit-scrollbar-track { background: #020617; }
.webr-output::-webkit-scrollbar-thumb { background: #475569; border-radius: 4px; }
.webr-output.has-content,
.webr-output.has-message,
.webr-output.has-error {
  min-height: 44px;
  padding: 28px 16px 12px 16px;
  border-top-color: rgba(51, 65, 85, 0.5);
}
/* OUTPUT label */
.webr-output.has-content::before,
.webr-output.has-message::before,
.webr-output.has-error::before {
  content: 'OUTPUT';
  display: block;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: #64748b;
  margin-bottom: 8px;
  margin-top: -16px;
}
.webr-output.has-error   { color: #f87171; }
.webr-output.is-loading  { opacity: 0.65; }

/* --- Responsive: compact header + horizontal scroll on mobile --- */
@media (max-width: 640px) {
  .webr-header { padding: 7px 10px; min-height: 40px; gap: 8px; }
  .webr-header-label { font-size: 11px; }
  .webr-run-btn { min-width: 80px; padding: 5px 10px; }
  .webr-run-shortcut { display: none; }
  /* Allow horizontal scroll so long R lines stay reachable on mobile.
     Desktop suppresses scroll to avoid wheel-event capture; touch scrolling
     doesn't conflict with page scroll. These rules must live after the
     desktop `overflow: hidden` on .webr-editor so they win. */
  .webr-editor {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }
  .webr-editor-input {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }
}

/* --- A11y: honor reduced motion --- */
@media (prefers-reduced-motion: reduce) {
  .webr-container,
  .webr-container:first-of-type,
  .webr-run-btn,
  .webr-copy-btn,
  .webr-output,
  .webr-editor::after {
    animation: none !important;
    transition: none !important;
  }
  .webr-run-btn:hover { transform: none; }
}

/* --- Print: disable sticky header + animations --- */
@media print {
  .webr-container:first-of-type { animation: none; }
  .webr-header { position: static; }
}

/* --- Plot output --- */
.webr-plot-output { text-align: center; padding: 16px; background: #ffffff; display: none; border-top: 0.8px solid rgba(51, 65, 85, 0.5); }
.webr-plot-output.has-content { display: block; }
.webr-plot-output img, .webr-plot-output canvas { max-width: 100%; height: auto; border-radius: 6px; box-shadow: 0 1px 4px rgba(0,0,0,0.2); }

.mermaid { margin: 15px 0; text-align: center; }

/* ===== DARK MODE — Non-code-box WebR components only ===== */
/* Code boxes are already dark-themed in base styles.
   Only loading banner and Run All bar need dark mode overrides. */

/* Loading banner */
html.dark .webr-loading-banner { background: #1e293b; color: #94a3b8; border-color: #334155; }
html.dark .webr-loading-banner.ready { background: #1e293b; color: #4ade80; border-color: #334155; }

/* Run All bar */
html.dark .webr-runall-bar { border-bottom-color: #334155; }
html.dark .webr-runall-label { color: #94a3b8; }
html.dark .webr-runall-label::before { background: #4ade80; }
html.dark .webr-runall-btn { color: #94a3b8; border-color: #475569; }
html.dark .webr-runall-btn:hover { color: #e2e8f0; border-color: #64748b; background: #1e293b; }
html.dark .webr-runall-inline .webr-runall-label { color: #94a3b8; }
html.dark .webr-runall-inline .webr-runall-label::before { background: #4ade80; }

/* --- First-run hint message --- */
.webr-first-run-hint {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 16px;
  font-size: 12.5px;
  color: #cbd5e1;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: linear-gradient(135deg, rgba(30, 41, 59, 0.8), rgba(15, 23, 42, 0.9));
  border-top: 1px solid rgba(56, 189, 248, 0.15);
  text-align: center;
  letter-spacing: 0.01em;
  animation: webrHintFadeIn 400ms ease-out;
}
.webr-hint-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid rgba(56, 189, 248, 0.25);
  border-top-color: #38bdf8;
  border-radius: 50%;
  animation: webrHintSpin 0.8s linear infinite;
  flex-shrink: 0;
}
@keyframes webrHintSpin {
  to { transform: rotate(360deg); }
}
@keyframes webrHintFadeIn {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}
.webr-first-run-hint.is-fading {
  opacity: 0;
  transform: translateY(-4px);
  transition: opacity 400ms ease-out, transform 400ms ease-out;
}
