:root {
  --bg: #f5f0e6;
  --surface: #fffcf5;
  --surface-2: #f9f4ea;
  --ink: #24201a;
  --muted: #776f61;
  --faint: #a69d8c;
  --line: #e4dccb;
  --accent: #b4532a;
  --accent-ink: #fffaf0;
  --accent-soft: #f4e3d6;
  --danger: #b3261e;
  --ok: #3f7a3a;

  --syn-comment: #9b927f;
  --syn-var: #2a6a8c;
  --syn-str: #4f7a35;
  --syn-num: #8b4ca3;
  --syn-op: #b4532a;
  --syn-head: #24201a;
  --syn-quoted: #7a5a1e;

  --mono: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --serif: "Newsreader", "Iowan Old Style", Georgia, serif;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --code-size: 13.5px;
  --code-line: 1.6;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #151410;
    --surface: #1c1a16;
    --surface-2: #211f1a;
    --ink: #ece6d8;
    --muted: #a39b8b;
    --faint: #6f685c;
    --line: #2f2b24;
    --accent: #e08a5b;
    --accent-ink: #1a120c;
    --accent-soft: #3a261b;
    --danger: #f2867c;
    --ok: #9cc47f;
    --syn-comment: #7f786b;
    --syn-var: #82bcd9;
    --syn-str: #a5cb85;
    --syn-num: #c9a2e6;
    --syn-op: #e08a5b;
    --syn-head: #f6f0e2;
    --syn-quoted: #dfbf7f;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #151410;
  --surface: #1c1a16;
  --surface-2: #211f1a;
  --ink: #ece6d8;
  --muted: #a39b8b;
  --faint: #6f685c;
  --line: #2f2b24;
  --accent: #e08a5b;
  --accent-ink: #1a120c;
  --accent-soft: #3a261b;
  --danger: #f2867c;
  --ok: #9cc47f;
  --syn-comment: #7f786b;
  --syn-var: #82bcd9;
  --syn-str: #a5cb85;
  --syn-num: #c9a2e6;
  --syn-op: #e08a5b;
  --syn-head: #f6f0e2;
  --syn-quoted: #dfbf7f;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- header ---------- */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 20px;
}
.brand { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.mark {
  display: inline-grid;
  place-items: center;
  width: 30px; height: 30px;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-ink);
  font-family: var(--mono);
  font-weight: 600;
  font-size: 18px;
  align-self: center;
}
.name { font-family: var(--serif); font-size: 28px; font-weight: 500; letter-spacing: -0.01em; }
.tag { font-family: var(--serif); font-style: italic; color: var(--muted); font-size: 17px; white-space: nowrap; }
.actions { display: flex; gap: 8px; align-items: center; }

button, select {
  font: inherit;
  font-size: 14px;
  color: var(--ink);
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--surface);
  padding: 6px 12px;
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
}
button:hover, select:hover { border-color: var(--faint); }
button:focus-visible, select:focus-visible, input:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button.ghost { background: transparent; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.primary:hover { filter: brightness(1.06); }
button.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
button.small { padding: 3px 9px; font-size: 13px; }
kbd {
  font-family: var(--mono);
  font-size: 11px;
  padding: 1px 5px;
  border-radius: 4px;
  border: 1px solid currentColor;
  opacity: .75;
  margin-left: 4px;
}
button.primary kbd { border-color: color-mix(in srgb, var(--accent-ink) 60%, transparent); }
select { padding-right: 28px; appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 15px) 52%, calc(100% - 10px) 52%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}

/* ---------- layout ---------- */
.split {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1.05fr) minmax(0, 1fr);
  gap: 14px;
  padding: 0 20px 20px;
}
.pane {
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: 0 1px 0 rgba(0,0,0,.02), 0 8px 24px -16px rgba(60,40,10,.25);
}
.pane-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px 8px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-2);
  min-height: 46px;
}
.label { font-family: var(--serif); font-style: italic; font-size: 16px; color: var(--muted); }
.dirty { color: var(--accent); margin-left: 6px; font-style: normal; font-size: 11px; vertical-align: 2px; }
.head-tools { display: flex; align-items: center; gap: 10px; }
.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); cursor: pointer; user-select: none; }
.toggle input { accent-color: var(--accent); margin: 0; }

/* ---------- editor ---------- */
.editor {
  flex: 1;
  min-height: 0;
  overflow: auto;
  display: flex;
  align-items: flex-start;
  font-family: var(--mono);
  font-size: var(--code-size);
  line-height: var(--code-line);
}
.gutter {
  position: sticky;
  left: 0;
  z-index: 2;
  flex: none;
  min-height: 100%;
  padding: 14px 10px 40px 12px;
  text-align: right;
  color: var(--faint);
  background: var(--surface);
  user-select: none;
  white-space: pre;
  border-right: 1px solid var(--line);
}
.gutter span { display: block; padding: 0 2px; border-radius: 3px; }
.gutter span.err { background: var(--danger); color: #fff; cursor: pointer; }
.gutter span.warn { color: var(--accent); font-weight: 600; }
.code {
  position: relative;
  flex: 1 0 auto;
  min-width: 0;
  min-height: 100%;
}
.hl, textarea#source {
  margin: 0;
  padding: 14px 16px 40px;
  font: inherit;
  line-height: inherit;
  white-space: pre;
  tab-size: 4;
  letter-spacing: normal;
  word-spacing: normal;
}
.hl { pointer-events: none; color: var(--ink); min-height: 100%; }
textarea#source {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  resize: none;
  border: 0;
  outline: none;
  overflow: hidden;
  background: transparent;
  color: transparent;
  caret-color: var(--accent);
}
textarea#source::selection { background: color-mix(in srgb, var(--accent) 28%, transparent); color: transparent; }
textarea#source:focus-visible { outline: none; }

.t-comment { color: var(--syn-comment); font-style: italic; }
.t-query { color: var(--accent); font-style: italic; }
.t-var { color: var(--syn-var); }
.t-str { color: var(--syn-str); }
.t-quoted { color: var(--syn-quoted); }
.t-num { color: var(--syn-num); }
.t-op { color: var(--syn-op); font-weight: 600; }
.t-head { color: var(--syn-head); font-weight: 600; }

/* ---------- console ---------- */
.log {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 12px 16px 8px;
  font-family: var(--mono);
  font-size: var(--code-size);
  line-height: 1.55;
}
.entry { margin: 0 0 12px; }
.entry > * { white-space: pre-wrap; word-break: break-word; }
.e-query { color: var(--muted); }
.e-query b { color: var(--accent); font-weight: 600; margin-right: .6ch; }
.e-query code { color: var(--ink); font-family: inherit; }
.e-out { color: var(--ink); padding-left: 2ch; border-left: 2px solid var(--line); margin: 2px 0; }
.e-trace { color: var(--faint); font-size: 12px; }
.e-ans { color: var(--ink); }
.e-ans .val-true { color: var(--ok); font-weight: 600; }
.e-false { color: var(--muted); font-weight: 600; }
.e-err { color: var(--danger); }
.e-sys { color: var(--muted); font-style: italic; font-family: var(--serif); font-size: 15px; }
.e-sys a, .e-err a { color: inherit; text-decoration: underline; text-underline-offset: 2px; cursor: pointer; }
.more {
  display: inline-flex;
  gap: 6px;
  margin-left: 1ch;
  vertical-align: 1px;
}
.more button {
  font-family: var(--mono);
  font-size: 12px;
  padding: 1px 8px;
  border-radius: 6px;
}
.more button.next { border-color: var(--accent); color: var(--accent); }
.thinking { color: var(--muted); font-style: italic; }
.thinking::after { content: '…'; animation: blink 1.2s steps(4) infinite; display: inline-block; width: 1.2ch; overflow: hidden; vertical-align: bottom; }
@keyframes blink { from { width: 0; } to { width: 1.2ch; } }

.suggest {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 12px 0;
  border-top: 1px solid var(--line);
  max-height: 96px;
  overflow: auto;
}
.suggest:empty { display: none; }
.suggest button {
  font-family: var(--mono);
  font-size: 12px;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.suggest button:hover { color: var(--ink); border-color: var(--accent); }

.prompt {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px 12px 16px;
}
.ps { font-family: var(--mono); font-weight: 600; color: var(--accent); }
#query {
  flex: 1;
  min-width: 0;
  font-family: var(--mono);
  font-size: var(--code-size);
  color: var(--ink);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
}
#query::placeholder { color: var(--faint); }
#query:focus { outline: none; border-color: var(--accent); }

/* ---------- help dialog ---------- */
dialog {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  color: var(--ink);
  padding: 0;
  max-width: min(560px, calc(100vw - 32px));
  box-shadow: 0 30px 80px -20px rgba(0,0,0,.45);
}
dialog::backdrop { background: rgba(20, 15, 8, .45); }
.help { padding: 22px 26px 20px; line-height: 1.55; }
.help h2 { font-family: var(--serif); font-weight: 500; font-size: 26px; margin: 0 0 8px; }
.help h3 { font-family: var(--serif); font-style: italic; font-weight: 400; font-size: 18px; margin: 18px 0 4px; color: var(--muted); }
.help p { margin: 6px 0; }
.help code { font-family: var(--mono); font-size: 12.5px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; }
.help .keys { padding-left: 18px; margin: 6px 0; }
.help .keys li { margin: 3px 0; }
.help kbd { margin: 0 2px 0 0; }
.help .fine { color: var(--muted); font-size: 13px; }
.dlg-actions { display: flex; justify-content: flex-end; margin-top: 14px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--bg);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 14px;
  box-shadow: 0 10px 30px -10px rgba(0,0,0,.4);
  z-index: 10;
}

/* ---------- narrow screens ---------- */
@media (max-width: 860px) {
  body { height: auto; min-height: 100%; }
  .top { padding: 10px 16px; flex-wrap: wrap; }
  .tag { display: none; }
  .split { grid-template-columns: minmax(0, 1fr); padding: 0 16px 16px; }
  .program { height: 56vh; }
  .console { height: 72vh; }
}
@media (max-width: 420px) {
  .actions select { max-width: 150px; }
  .pane-head kbd { display: none; }
}
