/* ==========================================================================
   OgbonLab, Code Playground v2 styles
   --------------------------------------------------------------------------
   Phase 2 of docs/code-playground-v2-engineering-plan.md (§3).
   Scoped under `.cpv2-shell` so v2-local tokens don't pollute OgbonLab
   globals. Anywhere outside the v2 surface, this file is dormant CSS.

   When Code Playground v2 is the default and v1 is deleted (post-Day-14
   cutover, mirroring AI Tutor v2), this file can be promoted to load
   unconditionally without changes, the .cpv2-shell scope is the
   intentional containment boundary.
   ========================================================================== */

.cpv2-shell {
  /* Mockup-local CodeMirror palette. Two values for the same token differ
     between light + dark so the editor background and the selection
     highlight stay readable in both themes. Inherits OgbonLab's global
     --bg/--ac/--bdr/etc from the parent context. */
  --bg-code: #FAF7F1;                       /* editor surface (light) */
  --code-ac-soft: rgba(46, 125, 107, 0.08); /* selection highlight (light) */
  --cpv2-bdr-soft: rgba(31, 32, 36, 0.06);
  --cpv2-bg3: #EEE9DF;                      /* active line / gutter highlight */
  --cpv2-font-mono: 'JetBrains Mono', 'SF Mono', Menlo, Consolas, ui-monospace, monospace;
}

[data-theme="dark"] .cpv2-shell {
  --bg-code: #181A1F;
  --code-ac-soft: rgba(79, 167, 145, 0.10);
  --cpv2-bdr-soft: rgba(232, 233, 236, 0.07);
  --cpv2-bg3: #24252B;
}

/* ─── Editor mount container ───────────────────────────────────────────── */
.cpv2-shell .cpv2-editor-mount {
  /* CodeMirror replaces the textarea. Fixed initial height for Phase 2;
     Phase 4 (settings modal) lets users tune it. Resize: vertical lets
     users drag it taller if they want. */
  height: 320px;
  min-height: 180px;
  resize: vertical;
  overflow: hidden;
  border: 1px solid var(--bdr);
  border-radius: var(--rad);
  background: var(--bg-code);
  margin-bottom: 10px;
}

/* Placeholder shown while the CodeMirror modules lazy-load. Replaced
   when EditorView mounts. */
.cpv2-shell .cpv2-editor-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-family: var(--cpv2-font-mono);
  font-size: 12px;
  color: var(--tx3);
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

/* ─── CodeMirror overrides (mockup §322-352, scoped under .cpv2-shell) ── */
/* font-size intentionally NOT set here, CodeMirror's EditorView.theme()
   manages it via the fontSizeCompartment (driven by user settings). A
   static font-size at .cm-editor specificity would beat the dynamic
   theme class and silently revert user adjustments. */
.cpv2-shell .cm-editor {
  height: 100%;
  background: var(--bg-code) !important;
  font-family: var(--cpv2-font-mono) !important;
}
.cpv2-shell .cm-editor.cm-focused {
  outline: none !important;
}
.cpv2-shell .cm-scroller {
  font-family: var(--cpv2-font-mono) !important;
  line-height: 1.55 !important;
}
.cpv2-shell .cm-gutters {
  background: transparent !important;
  border-right: 0.5px solid var(--cpv2-bdr-soft) !important;
  color: var(--tx3) !important;
}
.cpv2-shell .cm-activeLineGutter,
.cpv2-shell .cm-activeLine {
  background: var(--cpv2-bg3) !important;
}
.cpv2-shell .cm-content {
  caret-color: var(--ac) !important;
}
.cpv2-shell .cm-cursor {
  border-left-color: var(--ac) !important;
  border-left-width: 2px !important;
}
.cpv2-shell .cm-selectionBackground,
.cpv2-shell .cm-content ::selection {
  background: var(--code-ac-soft) !important;
}

/* ─── Mobile (<900px): tighter padding, smaller font ──────────────────── */
@media (max-width: 900px) {
  .cpv2-shell .cpv2-editor-mount {
    height: 240px;
  }
  .cpv2-shell .cm-editor {
    font-size: 12.5px;
  }
}

/* ==========================================================================
   Phase 3: Output rendering (§6)
   Run blocks, stdout, traceback, plot, plot actions, status pills.
   Mockup §397-495 ported verbatim, scoped under .cpv2-shell.
   ========================================================================== */

.cpv2-shell .cpv2-output-content {
  background: var(--bg2);
  border: 1px solid var(--bdr);
  border-radius: var(--rad);
  padding: 14px 16px;
  min-height: 120px;
  max-height: 480px;
  overflow-y: auto;
  font-family: var(--cpv2-font-mono);
  font-size: 12.5px;
  line-height: 1.5;
}
.cpv2-shell .cpv2-output-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 90px;
  color: var(--tx3);
  font-style: italic;
  font-family: var(--font-serif, Charter, Georgia, serif);
  font-size: 13px;
  text-align: center;
  padding: 12px;
}

/* ─── Run block ────────────────────────────────────────────────────────── */
.cpv2-shell .cpv2-run-block {
  margin-bottom: 16px;
}
.cpv2-shell .cpv2-run-block:last-child { margin-bottom: 0; }

.cpv2-shell .cpv2-run-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 0.5px dashed var(--cpv2-bdr-soft);
}
.cpv2-shell .cpv2-run-meta {
  font-family: var(--cpv2-font-mono);
  font-size: 10.5px;
  color: var(--tx3);
  letter-spacing: 0.3px;
}
.cpv2-shell .cpv2-run-status {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-family: var(--cpv2-font-mono);
  font-size: 10.5px;
  color: var(--tx2);
}
.cpv2-shell .cpv2-run-status.cpv2-ok      { color: var(--gn, #2e7d6b); }
.cpv2-shell .cpv2-run-status.cpv2-err     { color: var(--rd, #c83d3d); }
.cpv2-shell .cpv2-run-status.cpv2-running { color: var(--ac); }

.cpv2-shell .cpv2-run-status-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: currentColor;
}
.cpv2-shell .cpv2-run-status.cpv2-running .cpv2-run-status-dot {
  animation: cpv2-pulse 1.4s ease-in-out infinite;
}

/* ─── Stdout block ─────────────────────────────────────────────────────── */
.cpv2-shell .cpv2-stdout {
  background: var(--bg-code);
  border: 0.5px solid var(--cpv2-bdr-soft);
  border-radius: 7px;
  padding: 10px 12px;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--tx);
  font-family: var(--cpv2-font-mono);
  font-size: 12.5px;
  margin-bottom: 8px;
}
.cpv2-shell .cpv2-stdout:last-child { margin-bottom: 0; }

/* ─── Traceback block (red-tinted; also used for Cancelled/Timeout) ──── */
.cpv2-shell .cpv2-traceback {
  background: rgba(192, 57, 43, 0.06);
  border: 0.5px solid rgba(192, 57, 43, 0.25);
  border-radius: 7px;
  padding: 10px 12px;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--rd, #c83d3d);
  font-family: var(--cpv2-font-mono);
  font-size: 12px;
  margin-bottom: 8px;
}
.cpv2-shell .cpv2-traceback:last-child { margin-bottom: 0; }
[data-theme="dark"] .cpv2-shell .cpv2-traceback {
  background: rgba(226, 112, 101, 0.08);
  border-color: rgba(226, 112, 101, 0.25);
  color: #e27065;
}

/* ─── Plot block + action row ──────────────────────────────────────────── */
.cpv2-shell .cpv2-plot {
  margin-bottom: 12px;
  border: 0.5px solid var(--bdr);
  border-radius: 9px;
  overflow: hidden;
  background: white;
}
.cpv2-shell .cpv2-plot:last-child { margin-bottom: 0; }
.cpv2-shell .cpv2-plot img {
  display: block;
  max-width: 100%;
  height: auto;
}
.cpv2-shell .cpv2-plot-actions {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  padding: 6px 10px;
  background: var(--bg2);
  border-top: 0.5px solid var(--cpv2-bdr-soft);
}
.cpv2-shell .cpv2-plot-action {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 9px;
  background: transparent;
  color: var(--tx2);
  border: none;
  border-radius: 5px;
  font-family: var(--font-sans, -apple-system, sans-serif);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  transition: background 150ms, color 150ms;
}
.cpv2-shell .cpv2-plot-action:hover {
  background: var(--bg3, var(--cpv2-bg3));
  color: var(--tx);
}
.cpv2-shell .cpv2-plot-action svg {
  width: 11px;
  height: 11px;
}

/* Send-to-AI-Tutor button, hidden by default; revealed when the
   cross-tool handoff feature flag is on (boot script in index.html
   sets <html data-ctxh="on"> when OGBON_FLAGS.crossToolHandoff is
   true). Plan §6.5 + cross-tool handoff plan §4.1. */
.cpv2-shell .cpv2-plot-action-tutor {
  display: none;
}
html[data-ctxh="on"] .cpv2-shell .cpv2-plot-action-tutor {
  display: inline-flex;
  color: var(--ac);
}
html[data-ctxh="on"] .cpv2-shell .cpv2-plot-action-tutor:hover {
  background: rgba(184, 82, 47, 0.08);
  color: var(--ac);
}

/* ─── CTH Phase 2: "Ask Ogbon about this code" header button ─────────── */
/* Hidden by default; revealed when crossToolHandoff is on. Sits in the
   action row between Presets and Settings. The icon is a chat bubble. */
.cpv2-shell .cpv2-ask-ogbon-btn {
  display: none;
  align-items: center;
}
html[data-ctxh="on"] .cpv2-shell .cpv2-ask-ogbon-btn {
  display: inline-flex;
}
.cpv2-shell .cpv2-ask-ogbon-btn svg {
  width: 13px;
  height: 13px;
  vertical-align: middle;
  margin-right: 4px;
}

/* ==========================================================================
   Phase 4: Prefill banner + Settings modal (§5, §7)
   ========================================================================== */

/* ─── Prefill banner ──────────────────────────────────────────────────── */
.cpv2-shell .cpv2-prefill-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 14px;
  margin-bottom: 10px;
  background: rgba(184, 82, 47, 0.06); /* warm terracotta tint */
  border: 0.5px solid rgba(184, 82, 47, 0.25);
  border-radius: var(--rad);
  font-size: 12.5px;
  color: var(--tx1);
}
[data-theme="dark"] .cpv2-shell .cpv2-prefill-banner {
  background: rgba(232, 123, 78, 0.08);
  border-color: rgba(232, 123, 78, 0.28);
}
.cpv2-shell .cpv2-prefill-banner-text {
  font-style: italic;
  font-family: var(--font-serif, Charter, Georgia, serif);
}
.cpv2-shell .cpv2-prefill-banner-action {
  background: transparent;
  border: none;
  color: var(--ac);
  font-family: var(--font-sans, -apple-system, sans-serif);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: underline;
  text-decoration-thickness: 0.5px;
  text-underline-offset: 2px;
  padding: 2px 6px;
  border-radius: 4px;
}
.cpv2-shell .cpv2-prefill-banner-action:hover {
  background: rgba(184, 82, 47, 0.08);
}

/* ─── Settings modal ──────────────────────────────────────────────────── */
.cpv2-shell .cpv2-settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  padding: 24px;
}
.cpv2-shell .cpv2-settings-modal {
  background: var(--bg);
  border: 1px solid var(--bdr);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.25);
  padding: 22px 24px;
  max-width: 420px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
}
.cpv2-shell .cpv2-settings-title {
  margin: 0 0 4px;
  font-family: var(--font-serif, Charter, Georgia, serif);
  font-size: 18px;
  font-weight: 600;
  color: var(--tx);
}
.cpv2-shell .cpv2-settings-tagline {
  margin: 0 0 18px;
  font-size: 12.5px;
  color: var(--tx2);
  font-style: italic;
}
.cpv2-shell .cpv2-settings-row {
  margin-bottom: 14px;
}
.cpv2-shell .cpv2-settings-label {
  display: block;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--tx2);
  margin-bottom: 5px;
}
.cpv2-shell .cpv2-settings-input {
  width: 100%;
  padding: 6px 10px;
  background: var(--bg2);
  border: 1px solid var(--bdr);
  border-radius: 6px;
  color: var(--tx);
  font-family: var(--cpv2-font-mono);
  font-size: 13px;
  box-sizing: border-box;
}
.cpv2-shell .cpv2-settings-input:focus {
  outline: none;
  border-color: var(--ac);
  box-shadow: 0 0 0 2px rgba(184, 82, 47, 0.12);
}
.cpv2-shell .cpv2-settings-help {
  margin-top: 5px;
  font-size: 11px;
  color: var(--tx3);
  font-style: italic;
}
.cpv2-shell .cpv2-settings-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
  padding-top: 14px;
  border-top: 0.5px solid var(--cpv2-bdr-soft);
}

/* ==========================================================================
   Phase 5: Preset starters dropdown (§4)
   Five geoscience-flavored Python starters in a dropdown menu anchored
   under the Presets button in the action row.
   ========================================================================== */

.cpv2-shell .cpv2-presets-wrap {
  position: relative;
  display: inline-block;
}

.cpv2-shell .cpv2-presets-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 100;
  min-width: 280px;
  background: var(--bg);
  border: 1px solid var(--bdr);
  border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.16);
  padding: 4px;
  /* Lift slightly when wider than the viewport, Phase 6 polish could
     anchor right when overflowing the action-row's right edge. */
  max-height: 60vh;
  overflow-y: auto;
}

[data-theme="dark"] .cpv2-shell .cpv2-presets-menu {
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
}

.cpv2-shell .cpv2-preset-item {
  display: flex;
  flex-direction: column;
  width: 100%;
  align-items: flex-start;
  gap: 2px;
  padding: 8px 12px;
  background: transparent;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  text-align: left;
  transition: background 120ms;
  font-family: var(--font-sans, -apple-system, sans-serif);
}
.cpv2-shell .cpv2-preset-item:hover {
  background: var(--bg2);
}
.cpv2-shell .cpv2-preset-item:focus-visible {
  outline: 2px solid var(--ac);
  outline-offset: -2px;
}
.cpv2-shell .cpv2-preset-label {
  font-size: 13.5px;
  font-weight: 500;
  color: var(--tx);
}
.cpv2-shell .cpv2-preset-meta {
  font-size: 10.5px;
  letter-spacing: 0.04em;
  color: var(--tx3);
  font-family: var(--cpv2-font-mono);
}
