/* --- 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: all 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 bar (subtle with background) --- */
.webr-runall-bar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
  padding: 7px 14px;
  margin: 0 0 18px 0;
  background: #f8fafb;
  border: 1px solid #edf0f3;
  border-radius: 8px;
  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-label {
  font-size: 11px;
  color: #9ca3af;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  display: flex;
  align-items: center;
  gap: 5px;
}
.webr-runall-label::before {
  content: '';
  width: 6px; height: 6px;
  border-radius: 50%;
  background: #2da44e;
  flex-shrink: 0;
}
.webr-runall-btn {
  background: none;
  color: #3f73d8;
  border: 1px solid #d0d7de;
  padding: 4px 12px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 400;
  cursor: pointer;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  transition: all 0.15s ease;
}
.webr-runall-btn:hover { background: #f6f8fa; border-color: #3f73d8; }
.webr-runall-btn:disabled { color: #a5b4d4; cursor: wait; }
.webr-runall-btn.running { color: #d29922; border-color: #d29922; 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: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59,130,246,0.25), 0 2px 8px rgba(0,0,0,0.3);
}
@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: rgba(30, 41, 59, 0.8);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  border-bottom: 0.8px solid rgba(51, 65, 85, 0.5);
  font-family: 'IBM Plex Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  position: sticky;
  top: 0;
  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: #94a3b8; letter-spacing: 0.02em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.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; }

/* --- Pre-CodeMirror: reserve space to prevent CLS (dark theme) --- */
.webr-editor {
  position: relative;
  min-height: 80px;
  font-family: 'JetBrains Mono', '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 8px 52px;
  background: #020617;
  white-space: pre;
  overflow: hidden;
  color: #e3e8ef;
}
/* 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 CodeMirror initializes, it replaces the content — reset padding */
.webr-editor .CodeMirror {
  height: auto;
  min-height: 48px;
  font-size: 14px;
  font-family: 'JetBrains Mono', 'Fira Code', 'SF Mono', 'Consolas', ui-monospace, monospace;
  font-variant-ligatures: contextual;
  font-feature-settings: 'calt' 1;
  line-height: 1.4;
  background: #020617;
  color: #e3e8ef;
  padding: 4px 0;
}
.webr-editor.cm-initialized { padding: 0; min-height: auto; }
/* Prevent CodeMirror's internal scroll container from capturing wheel events —
   editors are fully expanded (viewportMargin: Infinity) so never need their own scrollbar */
.webr-editor .CodeMirror-scroll { overflow: hidden !important; }
.webr-editor .CodeMirror-gutters {
  background: #020617;
  border-right: none;
  padding-right: 8px;
}
.webr-editor .CodeMirror-linenumber {
  color: #6e7681;
  font-size: 13px;
  padding: 0 8px 0 12px;
  min-width: 20px;
}
.webr-editor .CodeMirror-cursor { border-left-color: #60a5fa; border-left-width: 2px; }
.webr-editor .CodeMirror-selected { background: #334155 !important; }
.webr-editor .CodeMirror-activeline-background { background: linear-gradient(to right, transparent 0 18px, #1e3a5f 18px); }
.webr-editor .CodeMirror-matchingbracket { color: #60a5fa !important; font-weight: 700; background: #1e3a5f; border-radius: 2px; }

/* --- R syntax colors (dark theme) --- */
.webr-editor .cm-keyword { color: #f87171; font-weight: 500; }
.webr-editor .cm-atom { color: #c084fc; }
.webr-editor .cm-number { color: #60a5fa; }
.webr-editor .cm-def { color: #c084fc; }
.webr-editor .cm-variable { color: #e2e8f0; }
.webr-editor .cm-variable-2 { color: #fb923c; }
.webr-editor .cm-variable-3 { color: #60a5fa; }
.webr-editor .cm-property { color: #60a5fa; }
.webr-editor .cm-operator { color: #f87171; }
.webr-editor .cm-comment { color: #64748b; font-style: italic; }
.webr-editor .cm-string { color: #86efac; }
.webr-editor .cm-string-2 { color: #86efac; }
.webr-editor .cm-builtin { color: #c084fc; font-weight: 500; }
.webr-editor .cm-function-call { color: #c4b5fd; }

/* --- 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) infinite;
}
@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: 'JetBrains Mono', '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-top: none;
  transition: min-height 180ms ease-out, padding 180ms ease-out,
              border-top-color 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: 0.8px solid 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 on mobile --- */
@media (max-width: 640px) {
  .webr-header { padding: 7px 10px; min-height: 40px; gap: 8px; }
  .webr-header-label { display: none; }
  .webr-run-btn { min-width: 80px; padding: 5px 10px; }
  .webr-run-shortcut { display: none; }
}

/* --- 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; -webkit-backdrop-filter: none; backdrop-filter: none; }
}

/* --- Plot output --- */
.webr-plot-output { text-align: center; padding: 16px; background: #0d1117; 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 { background: #1e293b; border-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 { background: #334155; color: #e2e8f0; border-color: #3b82f6; }

/* --- First-run hint message --- */
.webr-first-run-hint {
  padding: 6px 12px;
  font-size: 12px;
  color: #94a3b8;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: rgba(30, 41, 59, 0.6);
  border-top: 0.8px solid rgba(51, 65, 85, 0.3);
  text-align: center;
  animation: webrHintFadeIn 300ms ease-out;
}
@keyframes webrHintFadeIn {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}
