/* =============================================================
   CODE PLAYGROUND — standalone stylesheet
   Self-contained design tokens (scoped to .cpg) so this can be
   dropped into any page without clashing with host page styles.
   ============================================================= */

.cpg {
  --cpg-bg: #05070f;
  --cpg-panel: rgba(15, 23, 42, .78);
  --cpg-panel-alt: rgba(30, 41, 59, .55);
  --cpg-border: rgba(251, 191, 36, .16);
  --cpg-border-strong: rgba(251, 191, 36, .32);
  --cpg-text: #f1f5f9;
  --cpg-muted: #94a3b8;
  --cpg-gold: #fbbf24;
  --cpg-gold-glow: rgba(251, 191, 36, .18);
  --cpg-radius: .8rem;
  --cpg-html: #fb923c;
  --cpg-css: #38bdf8;
  --cpg-js: #facc15;
  --cpg-green: #34d399;
  --cpg-red: #f87171;
  --cpg-yellow: #fbbf24;

  all: initial;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  color: var(--cpg-text);
  background: var(--cpg-bg);
  background-image:
    radial-gradient(circle at 15% 0%, rgba(251, 191, 36, .07), transparent 32%),
    radial-gradient(circle at 85% 20%, rgba(56, 189, 248, .05), transparent 30%);
  width: 100%;
  padding: .8rem 1rem 1rem;
  /* Bounded to the viewport (minus the site nav) so nothing inside can
     ever stretch the page itself — every scrollable area gets its own
     internal scrollbar instead. */
  height: calc(100vh - var(--app-nav-h, 60px));
  margin-top: var(--app-nav-h, 60px);
  overflow: hidden;
  line-height: 1.5;
}

html.theme-light .cpg {
  --cpg-bg: #edf2f7;
  --cpg-panel: rgba(255, 255, 255, .92);
  --cpg-panel-alt: rgba(226, 232, 240, .82);
  --cpg-border: rgba(15, 23, 42, .14);
  --cpg-border-strong: rgba(161, 98, 7, .32);
  --cpg-text: #0f172a;
  --cpg-muted: #475569;
  --cpg-gold: #a16207;
  --cpg-gold-glow: rgba(202, 138, 4, .14);
  background-image:
    radial-gradient(circle at 15% 0%, rgba(202, 138, 4, .08), transparent 32%),
    radial-gradient(circle at 85% 20%, rgba(14, 116, 144, .06), transparent 30%)
}

html.theme-light .cpg :is(.cpg-editor-mount, .cpg-fallback-editor, .cpg-console) {
  background: rgba(248, 250, 252, .94);
  color: var(--cpg-text)
}

html.theme-light .cpg .cm-editor,
html.theme-light .cpg .cm-scroller,
html.theme-light .cpg .cm-gutters {
  color: #172033
}

/* Fullscreen mode for the app shell is kept as a harmless fallback for
   older browser state, but the toolbar button now targets the preview. */
.cpg:fullscreen,
.cpg.is-fullscreen {
  height: 100vh;
  margin-top: 0;
  padding: .8rem;
}

.cpg *, .cpg *::before, .cpg *::after {
  box-sizing: border-box;
  font-family: inherit;
}

.cpg button, .cpg select {
  font: inherit;
  color: inherit;
}

/* ── BACK BAR / BREADCRUMB ── */
.cpg-backbar {
  display: flex;
  align-items: center;
  gap: .8rem;
  flex-shrink: 0;
  margin-bottom: .6rem;
  font-size: .8rem;
}

.cpg-back-link {
  display: inline-flex;
  align-items: center;
  gap: .3rem;
  background: transparent;
  border: none;
  color: var(--cpg-muted);
  cursor: pointer;
  font-size: .82rem;
  font-weight: 600;
  padding: .2rem 0;
  transition: .18s ease;
}

.cpg-back-link:hover {
  color: var(--cpg-gold);
}

.cpg-crumb {
  display: flex;
  align-items: center;
  gap: .4rem;
  color: var(--cpg-muted);
}

.cpg-crumb a {
  color: var(--cpg-muted);
  text-decoration: none;
}

.cpg-crumb a:hover {
  color: var(--cpg-gold);
}

/* ── TOP TOOLBAR ── */
.cpg-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: .8rem;
  flex-shrink: 0;
  background: var(--cpg-panel);
  backdrop-filter: blur(10px);
  border: 1px solid var(--cpg-border);
  border-radius: var(--cpg-radius);
  padding: .8rem 1.1rem;
  margin-bottom: .8rem;
}

.cpg-toolbar-brand {
  display: flex;
  align-items: center;
  gap: .5rem;
}

.cpg-brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: .55rem;
  background: linear-gradient(135deg, var(--cpg-gold), #f59e0b);
  color: #111827;
  font-weight: 800;
  font-size: .78rem;
  letter-spacing: -.5px;
}

.cpg-brand-text {
  font-weight: 700;
  font-size: 1.02rem;
  letter-spacing: .2px;
}

.cpg-toolbar-controls {
  display: flex;
  align-items: center;
  gap: .7rem;
  flex-wrap: wrap;
}

.cpg-field {
  display: flex;
  align-items: center;
  gap: .5rem;
}

.cpg-field-label {
  font-size: .72rem;
  font-weight: 700;
  color: var(--cpg-muted);
  text-transform: uppercase;
  letter-spacing: .5px;
  white-space: nowrap;
}

.cpg-select {
  background: var(--cpg-panel-alt);
  border: 1px solid var(--cpg-border);
  color: var(--cpg-text);
  border-radius: .5rem;
  padding: .45rem 2rem .45rem .7rem;
  font-size: .82rem;
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23fbbf24' viewBox='0 0 16 16'%3e%3cpath d='M7.247 11.14L2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z'/%3e%3c/svg%3e");
  background-repeat: no-repeat;
  background-position: right .6rem center;
  background-size: 11px;
  outline: none;
}

.cpg-select:focus {
  border-color: var(--cpg-gold);
}

.cpg-status-badge {
  display: inline-flex;
  align-items: center;
  gap: .5rem;
  background: rgba(52, 211, 153, .1);
  border: 1px solid rgba(52, 211, 153, .3);
  color: #a7f3d0;
  padding: .48rem .8rem;
  border-radius: .55rem;
  font-size: .78rem;
  font-weight: 700;
  cursor: pointer;
  transition: .18s ease;
  white-space: nowrap;
}

.cpg-status-badge:hover {
  background: rgba(52, 211, 153, .18);
}

.cpg-status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--cpg-green);
  box-shadow: 0 0 6px rgba(52, 211, 153, .9);
}

.cpg-status-badge.is-paused {
  background: rgba(148, 163, 184, .1);
  border-color: rgba(148, 163, 184, .28);
  color: var(--cpg-muted);
}

.cpg-status-badge.is-paused .cpg-status-dot {
  background: var(--cpg-muted);
  box-shadow: none;
}

.cpg-toolbar-actions {
  display: flex;
  gap: .5rem;
}

/* ── BUTTONS ── */
.cpg-btn {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  border: 1px solid transparent;
  border-radius: .55rem;
  padding: .5rem .85rem;
  font-size: .8rem;
  font-weight: 600;
  cursor: pointer;
  transition: .18s ease;
  white-space: nowrap;
}

.cpg-btn-icon {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

.cpg-btn-ghost {
  background: rgba(251, 191, 36, .08);
  border-color: var(--cpg-border);
  color: var(--cpg-text);
}

.cpg-btn-ghost:hover {
  background: rgba(251, 191, 36, .16);
  border-color: var(--cpg-border-strong);
  color: var(--cpg-gold);
}

.cpg-btn-solid {
  background: linear-gradient(135deg, var(--cpg-gold), #f59e0b);
  color: #111827;
}

.cpg-btn-solid:hover {
  box-shadow: 0 6px 18px var(--cpg-gold-glow);
  transform: translateY(-1px);
}

.cpg-btn-sm {
  padding: .35rem .65rem;
  font-size: .74rem;
}

.cpg-icon-btn {
  width: 30px;
  height: 30px;
  border-radius: .45rem;
  background: rgba(251, 191, 36, .1);
  border: 1px solid var(--cpg-border);
  cursor: pointer;
  font-size: .72rem;
  font-weight: 700;
  transition: .18s ease;
}

.cpg-icon-btn:hover {
  background: rgba(251, 191, 36, .2);
  color: var(--cpg-gold);
}

/* ── MOBILE CODE/PREVIEW SWITCH ── */
.cpg-mobile-switch {
  display: none;
  gap: .4rem;
  flex-shrink: 0;
  background: var(--cpg-panel);
  border: 1px solid var(--cpg-border);
  border-radius: .6rem;
  padding: .3rem;
  margin-bottom: .8rem;
}

.cpg-mobile-switch-btn {
  flex: 1;
  padding: .5rem;
  background: transparent;
  border: none;
  border-radius: .4rem;
  cursor: pointer;
  font-size: .82rem;
  font-weight: 700;
  color: var(--cpg-muted);
  transition: .18s ease;
}

.cpg-mobile-switch-btn.active {
  background: linear-gradient(135deg, var(--cpg-gold), #f59e0b);
  color: #111827;
}

/* ── WORKSPACE ──
   flex:1 + min-height:0 bounds this to whatever height is left inside
   the fixed-height .cpg shell, so it can never grow the page — long
   pasted code scrolls inside the editor panel instead of stretching
   everything below it. */
.cpg-workspace {
  display: grid;
  grid-template-columns: minmax(320px, .85fr) minmax(460px, 1.35fr);
  gap: .8rem;
  align-items: stretch;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.cpg-col {
  background: var(--cpg-panel);
  backdrop-filter: blur(10px);
  border: 1px solid var(--cpg-border);
  border-radius: var(--cpg-radius);
  padding: .8rem;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

/* ── LANGUAGE TABS ── */
.cpg-lang-tabs {
  display: flex;
  gap: .4rem;
  flex-shrink: 0;
  background: var(--cpg-panel-alt);
  border-radius: .5rem;
  padding: .3rem;
  margin-bottom: .6rem;
}

.cpg-lang-tab {
  flex: 1;
  background: transparent;
  border: none;
  border-radius: .4rem;
  padding: .4rem;
  cursor: pointer;
  transition: .18s ease;
}

.cpg-lang-tab.active {
  background: rgba(255, 255, 255, .06);
}

.cpg-tag {
  display: inline-block;
  width: 100%;
  text-align: center;
  font-size: .76rem;
  font-weight: 800;
  letter-spacing: .4px;
  padding: .12rem 0;
  border-radius: .3rem;
  color: var(--cpg-muted);
  transition: .18s ease;
}

.cpg-lang-tab.active .cpg-tag-html { color: var(--cpg-html); }
.cpg-lang-tab.active .cpg-tag-css { color: var(--cpg-css); }
.cpg-lang-tab.active .cpg-tag-js { color: var(--cpg-js); }

/* ── EDITOR ──
   No fixed pixel height any more — it fills whatever space is left in
   the bounded .cpg-col and clips at that boundary (min-height:0 is the
   part that actually makes flex respect the clip instead of growing to
   fit 1000 lines of pasted content). CodeMirror's own .cm-scroller, and
   the fallback textarea, each get their own internal scrollbar here. */
.cpg-editor-mount {
  flex: 1;
  min-height: 0;
  border: 1px solid var(--cpg-border);
  border-radius: .6rem;
  overflow: hidden;
  background: rgba(8, 11, 20, .7);
}

.cpg-editor-mount .cm-editor {
  height: 100%;
}

.cpg-editor-mount .cm-editor.cm-focused {
  outline: none;
}

.cpg-fallback-editor {
  width: 100%;
  height: 100%;
  border: none;
  outline: none;
  resize: none;
  overflow: auto;
  background: transparent;
  color: var(--cpg-text);
  font-family: 'SFMono-Regular', ui-monospace, Menlo, Consolas, monospace;
  font-size: 14px;
  line-height: 1.55;
  padding: .9rem;
  tab-size: 2;
}

.cpg-editor-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .6rem;
  flex-shrink: 0;
  margin-top: .6rem;
}

.cpg-font-controls {
  display: flex;
  gap: .4rem;
}

.cpg-editor-note {
  font-size: .72rem;
  color: var(--cpg-muted);
  text-align: center;
  flex: 1;
}

.cpg-line-count {
  font-size: .74rem;
  color: var(--cpg-muted);
  white-space: nowrap;
}

/* ── PREVIEW ── */
.cpg-preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .6rem;
  margin-bottom: .6rem;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.cpg-viewport-toggle {
  display: flex;
  gap: .3rem;
  background: var(--cpg-panel-alt);
  border-radius: .5rem;
  padding: .25rem;
  flex-wrap: wrap;
}

.cpg-viewport-btn {
  background: transparent;
  border: none;
  border-radius: .38rem;
  padding: .38rem .7rem;
  font-size: .76rem;
  font-weight: 600;
  color: var(--cpg-muted);
  cursor: pointer;
  transition: .18s ease;
  white-space: nowrap;
}

.cpg-viewport-btn.active {
  background: linear-gradient(135deg, var(--cpg-gold), #f59e0b);
  color: #111827;
}

/* Fills the remaining space in the preview column (bounded by the same
   flex/min-height:0 chain as the editor) instead of a fixed pixel
   height, so the preview panel no longer stretches the page. */
.cpg-iframe-wrap {
  --cpg-preview-width: 1366px;
  --cpg-preview-height: 768px;
  --cpg-preview-stage-width: 1366px;
  --cpg-preview-stage-height: 768px;
  --cpg-preview-scale: 1;
  flex: 1;
  flex-basis: auto;
  min-height: 0;
  overflow: hidden;
  position: relative;
  contain: layout paint;
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cpg-preview-stage {
  position: relative;
  flex: 0 0 auto;
  width: var(--cpg-preview-stage-width);
  height: var(--cpg-preview-stage-height);
  overflow: hidden;
  background: #fff;
  border: 1px solid var(--cpg-border-strong);
  border-radius: .55rem;
  box-shadow: 0 18px 45px rgba(15, 23, 42, .18);
}

.cpg-iframe {
  position: absolute;
  inset: 0 auto auto 0;
  width: var(--cpg-preview-width);
  height: var(--cpg-preview-height);
  display: block;
  border: none;
  background: #fff;
  transform: scale(var(--cpg-preview-scale));
  transform-origin: top left;
}

.cpg-iframe-wrap.is-mobile .cpg-preview-stage {
  border-radius: .8rem;
}

.cpg-iframe-wrap.is-tablet .cpg-preview-stage {
  border-radius: .7rem;
}

.cpg-iframe-wrap:fullscreen,
.cpg-iframe-wrap:-webkit-full-screen,
.cpg-iframe-wrap.is-preview-fullscreen {
  width: 100vw;
  height: 100vh;
  max-width: none;
  min-height: 100vh;
  padding: 0;
  border: none;
  border-radius: 0;
  background: #fff;
}

.cpg-iframe-wrap:fullscreen .cpg-preview-stage,
.cpg-iframe-wrap:-webkit-full-screen .cpg-preview-stage,
.cpg-iframe-wrap.is-preview-fullscreen .cpg-preview-stage,
.cpg-iframe-wrap:fullscreen .cpg-iframe,
.cpg-iframe-wrap:-webkit-full-screen .cpg-iframe,
.cpg-iframe-wrap.is-preview-fullscreen .cpg-iframe {
  width: 100%;
  height: 100%;
  max-width: none;
  border: none;
  border-radius: 0;
  box-shadow: none;
  transform: none;
}

.cpg-iframe-wrap.is-preview-fullscreen {
  position: fixed;
  inset: 0;
  z-index: 2147483647;
}

/* ── CONSOLE DRAWER ── */
.cpg-console {
  flex-shrink: 0;
  margin-top: .7rem;
  border: 1px solid var(--cpg-border);
  border-radius: .6rem;
  background: rgba(8, 11, 20, .6);
  display: flex;
  flex-direction: column;
  max-height: clamp(120px, 18vh, 180px);
  transition: max-height .18s ease, opacity .18s ease;
}

.cpg-console.is-collapsed {
  max-height: 48px;
}

.cpg-console-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .5rem .7rem;
  border-bottom: 1px solid var(--cpg-border);
  flex-shrink: 0;
}

.cpg-console-title {
  font-size: .74rem;
  font-weight: 800;
  color: var(--cpg-gold);
  text-transform: uppercase;
  letter-spacing: .6px;
}

.cpg-console-actions {
  display: flex;
  align-items: center;
  gap: .6rem;
}

.cpg-error-badge {
  font-size: .74rem;
  color: var(--cpg-muted);
  white-space: nowrap;
  padding: .15rem .5rem;
  border-radius: 1rem;
  background: var(--cpg-panel-alt);
}

.cpg-error-badge.has-errors {
  color: var(--cpg-red);
  background: rgba(248, 113, 113, .12);
}

.cpg-console-log {
  overflow-y: auto;
  padding: .4rem .7rem;
  font-family: 'SFMono-Regular', ui-monospace, Menlo, Consolas, monospace;
  font-size: .76rem;
  min-height: 0;
}

.cpg-console.is-collapsed .cpg-console-log {
  display: none;
}

.cpg-console-empty {
  color: var(--cpg-muted);
  padding: .4rem 0;
  font-family: inherit;
}

.cpg-log-row {
  display: flex;
  gap: .5rem;
  padding: .3rem 0;
  border-bottom: 1px solid rgba(148, 163, 184, .08);
  align-items: flex-start;
}

.cpg-log-row:last-child {
  border-bottom: none;
}

.cpg-log-dot {
  flex-shrink: 0;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  margin-top: .4em;
  background: var(--cpg-muted);
}

.cpg-log-error .cpg-log-dot { background: var(--cpg-red); }
.cpg-log-warn .cpg-log-dot { background: var(--cpg-yellow); }

.cpg-log-text {
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--cpg-text);
}

.cpg-log-error .cpg-log-text { color: #fca5a5; }
.cpg-log-warn .cpg-log-text { color: #fde68a; }

/* ── RESPONSIVE ── */
@media (max-width: 900px) {
  .cpg-workspace {
    grid-template-columns: 1fr;
  }

  .cpg-mobile-switch {
    display: flex;
  }

  .cpg-workspace #cpg-preview-col {
    display: none;
  }

  .cpg-workspace.show-preview #cpg-preview-col {
    display: flex;
  }

  .cpg-workspace.show-preview #cpg-editor-col {
    display: none;
  }
}

@media (max-height: 720px) {
  .cpg {
    padding-top: .6rem;
    padding-bottom: .6rem;
  }

  .cpg-backbar {
    margin-bottom: .45rem;
  }

  .cpg-toolbar {
    padding: .65rem .9rem;
    margin-bottom: .6rem;
  }

  .cpg-preview-header {
    margin-bottom: .45rem;
  }

  .cpg-console {
    margin-top: .55rem;
  }
}

@media (max-width: 560px) {
  .cpg {
    padding: .6rem;
  }

  .cpg-toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .cpg-toolbar-brand,
  .cpg-toolbar-controls,
  .cpg-toolbar-actions {
    width: 100%;
    min-width: 0;
  }

  .cpg-toolbar-controls {
    justify-content: space-between;
  }

  .cpg-field {
    flex: 1 1 150px;
    min-width: 0;
  }

  .cpg-select {
    width: 100%;
  }

  .cpg-status-badge {
    flex: 1 1 auto;
    min-width: 0;
    justify-content: center;
  }

  .cpg-toolbar-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .cpg-toolbar-actions .cpg-btn {
    min-width: 0;
    justify-content: center;
  }

  .cpg-field-label {
    display: none;
  }
}
