/*
 * Theming
 * -------
 * All colors are CSS custom properties so the three palettes — and the OS
 * default — share one set of semantic tokens. Palettes lean on a kitchen /
 * cookbook vocabulary:
 *
 *   Parchment  (light) — a cookbook page: cream paper, espresso ink, paprika.
 *   Cast Iron  (dark)  — a seasoned skillet: charcoal with an ember-copper glow.
 *   Espresso   (dark)  — a coffee bar: deep roast browns with caramel.
 *
 * Default (no data-theme attribute) follows the machine via
 * prefers-color-scheme; the header toggle sets data-theme to override.
 */

:root {
  --mono: ui-monospace, "SF Mono", "Cascadia Code", "Fira Code", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  /* Parchment (light) — also the default when the OS prefers light. */
  color-scheme: light;
  --bg: #efe6d2;
  --panel: #faf4e6;
  --panel-header: #ead9bd;
  --border: #d9c6a3;
  --text: #3a2c23;
  --muted: #8c745b;
  --accent: #c0402b;
  --accent-hover: #a5331f;
  --on-accent: #fff6ec;
  --error: #b22a1e;
  --ok: #5b7d3a;

  --cm-bg: var(--panel);
  --cm-text: var(--text);
  --cm-gutter-bg: #efe2c6;
  --cm-gutter-text: #a9906f;
  --cm-active-line: rgba(122, 90, 60, 0.08);
  --cm-active-gutter: rgba(122, 90, 60, 0.16);
  --cm-cursor: var(--accent);
  --cm-selection: rgba(192, 64, 43, 0.22);
}

/* Cast Iron palette tokens (reused by the OS-dark default and the explicit
   choice below). */

/* When the OS prefers dark and the user hasn't chosen, use Cast Iron. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    color-scheme: dark;
    --bg: #181715;
    --panel: #221f1c;
    --panel-header: #2a2723;
    --border: #393530;
    --text: #ece4d6;
    --muted: #9d9081;
    --accent: #e8743b;
    --accent-hover: #f58c54;
    --on-accent: #1b1815;
    --error: #ff6f5e;
    --ok: #93c069;

    --cm-bg: var(--panel);
    --cm-text: var(--text);
    --cm-gutter-bg: #1e1b18;
    --cm-gutter-text: #7c7264;
    --cm-active-line: rgba(255, 255, 255, 0.04);
    --cm-active-gutter: rgba(255, 255, 255, 0.07);
    --cm-cursor: var(--accent);
    --cm-selection: rgba(232, 116, 59, 0.26);
  }
}

/* Explicit Cast Iron selection. */
:root[data-theme="cast-iron"] {
  color-scheme: dark;
  --bg: #181715;
  --panel: #221f1c;
  --panel-header: #2a2723;
  --border: #393530;
  --text: #ece4d6;
  --muted: #9d9081;
  --accent: #e8743b;
  --accent-hover: #f58c54;
  --on-accent: #1b1815;
  --error: #ff6f5e;
  --ok: #93c069;

  --cm-bg: var(--panel);
  --cm-text: var(--text);
  --cm-gutter-bg: #1e1b18;
  --cm-gutter-text: #7c7264;
  --cm-active-line: rgba(255, 255, 255, 0.04);
  --cm-active-gutter: rgba(255, 255, 255, 0.07);
  --cm-cursor: var(--accent);
  --cm-selection: rgba(232, 116, 59, 0.26);
}

/* Espresso (dark) — deep coffee roast with caramel accent. */
:root[data-theme="espresso"] {
  color-scheme: dark;
  --bg: #1f1711;
  --panel: #281e17;
  --panel-header: #312419;
  --border: #443223;
  --text: #f1e6d4;
  --muted: #b09a80;
  --accent: #e0a458;
  --accent-hover: #f1b96b;
  --on-accent: #241a10;
  --error: #ff8068;
  --ok: #b0c06a;

  --cm-bg: var(--panel);
  --cm-text: var(--text);
  --cm-gutter-bg: #221a13;
  --cm-gutter-text: #8c7a62;
  --cm-active-line: rgba(224, 164, 88, 0.08);
  --cm-active-gutter: rgba(224, 164, 88, 0.14);
  --cm-cursor: var(--accent);
  --cm-selection: rgba(224, 164, 88, 0.24);
}

/*
 * Layout
 * ------
 */

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
}

body {
  display: flex;
  flex-direction: column;
}

/* Smooth the palette switch (but not the editor, which CodeMirror repaints). */
body,
.toolbar,
.pane-header,
.footer,
.editor-host,
.output,
button.theme-toggle,
button#run,
select {
  transition: background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.6rem 1rem;
  background: var(--panel-header);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 0.5rem; font-weight: 600; }
.logo { font-size: 1.4rem; }
.title { letter-spacing: 0.01em; }

.controls { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.controls label { color: var(--muted); font-size: 0.9rem; display: flex; align-items: center; gap: 0.4rem; }

input[type="checkbox"] { accent-color: var(--accent); }

select {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.3rem 0.5rem;
  font: inherit;
}

button#run {
  background: var(--accent);
  color: var(--on-accent);
  border: none;
  border-radius: 6px;
  padding: 0.45rem 1rem;
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  transition: background 0.15s ease;
}
button#run:hover { background: var(--accent-hover); }
button#run:active { transform: translateY(1px); }

/* Link out to the cookbook — dressed like the theme toggle. */
a.toolbar-link {
  color: var(--text);
  text-decoration: none;
  font-size: 0.9rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.35rem 0.7rem;
  transition: background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease;
}
a.toolbar-link:hover { border-color: var(--accent); color: var(--accent); }

button.theme-toggle {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 1.05rem;
  line-height: 1;
  padding: 0.35rem 0.5rem;
  cursor: pointer;
}
button.theme-toggle:hover {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--text) 8%, transparent);
}
button.theme-toggle:active { transform: translateY(1px); }

.panes {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  min-height: 0;
}

.pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}
.editor-pane { border-right: 1px solid var(--border); }

.pane-header {
  padding: 0.4rem 0.8rem;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  background: var(--panel-header);
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.status { text-transform: none; letter-spacing: 0; font-size: 0.8rem; }
.status.ok { color: var(--ok); }
.status.err { color: var(--error); }

.editor-host {
  flex: 1;
  min-height: 0;
  overflow: auto;
  background: var(--cm-bg);
}

/* CodeMirror itself is themed from JS via EditorView.theme(), wired to the
   same --cm-* custom properties (see editor.js). A real CM theme is required
   to override its default light theme's high-specificity generated classes. */

.input-header { border-top: 1px solid var(--border); }
.input-header .hint {
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.75rem;
}

/* The playground's stand-in for stdin: whitespace-separated numbers consumed
   one per "Take ... from refrigerator" instruction. */
.stdin {
  flex: 0 0 auto;
  height: 3.2rem;
  resize: none;
  border: none;
  outline: none;
  padding: 0.5rem 0.8rem;
  font-family: var(--mono);
  font-size: 13px;
  background: var(--cm-bg);
  color: var(--cm-text);
  transition: background-color 0.2s ease, color 0.2s ease;
}

.output {
  flex: 1;
  margin: 0;
  padding: 0.8rem 1rem;
  overflow: auto;
  background: var(--panel);
  font-family: var(--mono);
  font-size: 14px;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.4;
}
.output.error { color: var(--error); }

.footer {
  padding: 0.5rem 1rem;
  font-size: 0.8rem;
  color: var(--muted);
  background: var(--panel-header);
  border-top: 1px solid var(--border);
}
.footer a { color: var(--accent); text-decoration: none; }
.footer a:hover { text-decoration: underline; }

@media (max-width: 720px) {
  .panes { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; }
  .editor-pane { border-right: none; border-bottom: 1px solid var(--border); }
}
