:root {
  --ember: #c2410c;
  --ember-2: #e2622e;
  --font-heading: "Bricolage Grotesque", "Space Grotesk", system-ui, sans-serif;
  --font-body: "Hanken Grotesk", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, Menlo, monospace;
  --radius: 10px;
  --topbar-h: 60px;
  --tap: 44px;
}

:root, [data-theme="dark"] {
  --bg: #0e0e11;
  --surface: #141418;
  --surface-2: #1b1b20;
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.16);
  --text: #c9c9c4;
  --text-strong: #e8e8e4;
  --muted: #8a8a85;
  --accent-text: #e2622e;
  --ok: #8fceab;
  --danger: #e08a7a;
  --add-bg: rgba(90, 168, 119, 0.14);
  --del-bg: rgba(194, 65, 12, 0.16);
}

[data-theme="light"] {
  --bg: #faf9f6;
  --surface: #ffffff;
  --surface-2: #f3f1eb;
  --border: rgba(23, 23, 26, 0.1);
  --border-strong: rgba(23, 23, 26, 0.18);
  --text: #3d3a34;
  --text-strong: #17171a;
  --muted: #6b675e;
  --accent-text: #b23a0a;
  --ok: #1f7a45;
  --danger: #a33b28;
  --add-bg: rgba(47, 125, 79, 0.12);
  --del-bg: rgba(178, 58, 10, 0.1);
}

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; max-width: 100%; overflow-x: hidden; }
body {
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
}
h1, h2, h3 { font-family: var(--font-heading); color: var(--text-strong); letter-spacing: -0.03em; margin: 0; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.15rem; }
a { color: var(--text-strong); }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
img, svg, pre, code { max-width: 100%; }

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

.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  min-height: var(--topbar-h);
  padding: 0.7rem 1.1rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.brand { display: flex; align-items: center; gap: 0.5rem; color: var(--text-strong); text-decoration: none; font-weight: 650; min-height: var(--tap); }
.brand__mark { color: var(--ember); flex: none; }
.topbar__title { flex: 1; min-width: 0; }
.topbar__title p, .muted, .hint { color: var(--muted); margin: 0.15rem 0 0; }
.topbar__title p { overflow-wrap: anywhere; }
.topbar__actions { display: flex; gap: 0.45rem; flex: none; }

.btn {
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text-strong);
  border-radius: 8px;
  min-height: var(--tap);
  padding: 0.45rem 0.85rem;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.btn:disabled { opacity: 0.55; cursor: wait; }
.btn-primary { background: var(--ember); border-color: var(--ember); color: #fffaf6; }
.btn-ghost { background: transparent; }
.btn-icon { width: var(--tap); height: var(--tap); padding: 0; }
.icon { width: 1.1rem; height: 1.1rem; }

.shows { margin: 0; font-size: 0.92rem; line-height: 1.45; overflow-wrap: anywhere; }
.shows--bar {
  padding: 0.7rem 1rem;
  background: rgba(194, 65, 12, 0.08);
  border-bottom: 1px solid rgba(194, 65, 12, 0.28);
}
.shows__k {
  margin-right: 0.45rem;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ember);
}

.strip {
  display: flex;
  gap: 0.55rem;
  align-items: flex-start;
  padding: 0.55rem 1.1rem;
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.92rem;
}
.strip__dot { width: 0.5rem; height: 0.5rem; margin-top: 0.45rem; border-radius: 99px; background: var(--ember); flex: none; }

.intro { padding: 0.9rem 1.1rem 0.2rem; }
.intro ul { margin: 0.45rem 0 0.2rem; padding-left: 1.1rem; }
.intro li { margin: 0.2rem 0; }
.intro code { font-family: var(--font-mono); font-size: 0.86em; color: var(--accent-text); }

.gate {
  margin: 0.8rem 1.1rem 1.2rem;
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  max-width: 36rem;
}
.gate p { margin: 0.4rem 0 0.8rem; }
.turnstile { width: 100%; max-width: 100%; min-height: 65px; overflow: hidden; }
.turnstile iframe { max-width: 100%; }

.tabs {
  display: none;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.35rem;
  padding: 0.45rem 0.7rem;
  position: sticky;
  top: 0;
  z-index: 4;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.tab {
  min-height: var(--tap);
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
}
.tab.is-active { background: var(--ember); border-color: var(--ember); color: #fffaf6; }

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(0, 0.95fr) minmax(0, 1fr);
  align-items: stretch;
  min-height: calc(100dvh - 250px);
}
.pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 0.9rem 1rem 1rem;
  border-left: 1px solid var(--border);
  background: var(--surface);
}
.pane:first-child { border-left: 0; background: transparent; }
.pane__head { display: flex; align-items: center; justify-content: space-between; gap: 0.6rem; margin-bottom: 0.7rem; }
.count {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--muted);
  overflow-wrap: anywhere;
  text-align: right;
}

.messages {
  flex: 1;
  min-height: 10rem;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}
.bubble {
  max-width: 100%;
  padding: 0.65rem 0.75rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  overflow-wrap: anywhere;
}
.bubble--user { align-self: flex-end; background: rgba(194, 65, 12, 0.12); border-color: rgba(194, 65, 12, 0.35); }
.bubble p { margin: 0; }
.meta { margin-top: 0.35rem; color: var(--muted); font-size: 0.82rem; }
.meta code { font-family: var(--font-mono); color: var(--accent-text); }

.chips { display: flex; flex-wrap: wrap; gap: 0.4rem; margin: 0.7rem 0; }
.chip {
  min-height: var(--tap);
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: transparent;
  color: var(--text-strong);
}

.composer {
  position: sticky;
  bottom: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.45rem;
  align-items: end;
  padding-top: 0.35rem;
  padding-bottom: calc(0.35rem + env(safe-area-inset-bottom));
  background: linear-gradient(to bottom, transparent, var(--bg) 8px);
}
textarea, input[type="text"] {
  width: 100%;
  min-height: var(--tap);
  font-size: 16px;
  padding: 0.55rem 0.7rem;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--bg);
  resize: vertical;
}
.hint:empty { display: none; }
.hint.is-bad { color: var(--danger); }

.tree { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-bottom: 0.7rem; }
.tree button {
  min-height: var(--tap);
  padding: 0.3rem 0.7rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  font-family: var(--font-mono);
  font-size: 0.82rem;
}
.tree button.is-active { border-color: var(--ember); color: var(--accent-text); }
.md { overflow-wrap: anywhere; }
.md h2, .md h3, .md h4 { margin: 0.8rem 0 0.35rem; }
.md p, .md ul { margin: 0.35rem 0; }
.md ul { padding-left: 1.1rem; }
.md code { font-family: var(--font-mono); font-size: 0.86em; }
.md pre, .source {
  margin: 0;
  padding: 0.75rem;
  overflow: auto;
  border-radius: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-family: var(--font-mono);
  font-size: 0.8rem;
  white-space: pre-wrap;
}

.steps, .commits { list-style: none; margin: 0 0 0.7rem; padding: 0; display: flex; flex-direction: column; gap: 0.35rem; }
.steps li, .commits button {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  padding: 0.45rem 0.65rem;
  text-align: left;
}
.commits button { width: 100%; min-height: var(--tap); color: inherit; }
.commits button.is-active { border-color: var(--ember); }
.commit__msg { display: block; color: var(--text-strong); }
.commit__meta { display: block; color: var(--muted); font-family: var(--font-mono); font-size: 0.75rem; }
.diff {
  margin: 0.4rem 0 0;
  padding: 0.7rem;
  overflow: auto;
  max-width: 100%;
  border-radius: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-family: var(--font-mono);
  font-size: 0.78rem;
  line-height: 1.45;
  white-space: pre;
}
.diff .add { color: var(--ok); background: var(--add-bg); }
.diff .del { color: var(--danger); background: var(--del-bg); }
.foot { padding: 0.4rem 1.1rem calc(0.6rem + env(safe-area-inset-bottom)); color: var(--muted); border-top: 1px solid var(--border); }
.foot p { margin: 0; overflow-wrap: anywhere; }
.foot a { display: inline-flex; align-items: center; min-height: var(--tap); }

input:not([type="checkbox"]):not([type="radio"]):not([type="file"]):not([type="hidden"]),
textarea, select { font-size: 16px; }

@media (max-width: 900px) {
  .tabs { display: grid; }
  .workspace { display: block; min-height: 0; }
  .pane { display: none; border-left: 0; padding-bottom: 0.6rem; }
  .pane.is-active {
    display: flex;
    height: calc(100dvh - 3.4rem);
    max-height: calc(100dvh - 3.4rem);
    min-height: 0;
    overflow: hidden;
  }
  #pane-chat .messages { flex: 1 1 auto; min-height: 0; overflow: auto; }
  #pane-chat .chips,
  #pane-chat .composer,
  #pane-chat .hint { flex: none; }
  #pane-memory.is-active,
  #pane-history.is-active { overflow: auto; }
  #pane-memory.is-active > *,
  #pane-history.is-active > * { flex-shrink: 0; }
  #pane-history .pane__head {
    position: sticky;
    top: -0.9rem;
    z-index: 1;
    margin: -0.9rem -1rem 0.7rem;
    padding: 0.9rem 1rem 0.55rem;
    background: var(--surface);
  }
  .topbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    padding-top: max(10px, env(safe-area-inset-top));
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }
  .topbar__title { grid-column: 1 / -1; grid-row: 2; }
  .topbar__actions { grid-column: 2; grid-row: 1; }
  .intro, .shows--bar, .strip, .gate { padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
  .gate { margin-left: max(12px, env(safe-area-inset-left)); margin-right: max(12px, env(safe-area-inset-right)); }
  .composer { padding-left: max(0px, env(safe-area-inset-left)); padding-right: max(0px, env(safe-area-inset-right)); }
}

@media (max-width: 900px) and (max-height: 500px) {
  .pane.is-active { min-height: 0; }
  .messages { min-height: 6rem; }
  .intro { padding-top: 0.45rem; padding-bottom: 0; }
  .intro ul { margin-top: 0.2rem; }
}
