:root {
  --bg: #f8fafc;
  --panel: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --brand: #1e3a5f;
  --brand-ink: #ffffff;
  --accent: #2563eb;
  --danger: #b91c1c;
  --ok: #15803d;
  --user-bubble: #1e3a5f;
  --agent-bubble: #f1f5f9;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --panel: #111a2c;
    --ink: #e2e8f0;
    --muted: #94a3b8;
    --line: #1f2a40;
    --brand: #3b6ea8;
    --accent: #60a5fa;
    --danger: #f87171;
    --ok: #4ade80;
    --user-bubble: #2b4f7c;
    --agent-bubble: #18233a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.grow { flex: 1 1 auto; min-width: 0; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Buttons: one shared style, 40px+ tap targets. */
.btn {
  min-height: 44px;
  padding: 0 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-decoration: none;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn-danger { color: var(--danger); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-small { min-height: 40px; padding: 0 10px; font-size: 0.9rem; }
.btn-icon { width: 44px; padding: 0; font-size: 1.2rem; }
.btn-icon[aria-pressed="true"] { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-wide { width: 100%; }

/* Top bar and tabs */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--brand);
  color: var(--brand-ink);
}
.brand { font-weight: 700; flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .btn-ghost { color: var(--brand-ink); }
.pill {
  font-size: 0.8rem;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.15);
  max-width: 50vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pill.is-busy { background: #f59e0b; color: #111; }

.tabs { display: flex; border-bottom: 1px solid var(--line); background: var(--panel); }
.tab {
  flex: 1;
  min-height: 44px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 600;
  border-bottom: 3px solid transparent;
  cursor: pointer;
}
.tab.is-active { color: var(--ink); border-bottom-color: var(--brand); }

/* Panes: one at a time on phones, side by side on wide screens. */
.layout { flex: 1 1 auto; min-height: 0; display: flex; }
.pane { display: none; flex-direction: column; min-width: 0; min-height: 0; flex: 1 1 auto; }
.pane.is-shown { display: flex; }
@media (min-width: 900px) {
  .tabs { display: none; }
  .pane { display: flex; }
  .pane-chat { flex: 0 0 400px; border-right: 1px solid var(--line); }
}

/* Changes (collapsed by default) */
.changes { border-bottom: 1px solid var(--line); background: var(--panel); padding: 0 12px; }
.changes summary { min-height: 44px; display: flex; align-items: center; cursor: pointer; font-weight: 600; }
.changes-list { margin: 0 0 8px; padding-left: 20px; max-height: 30vh; overflow-y: auto; }
.changes-list li { margin: 4px 0; overflow-wrap: anywhere; }
.changes-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }

/* Conversation */
.log { list-style: none; margin: 0; padding: 12px; flex: 1 1 auto; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 92%; padding: 10px 12px; border-radius: 14px; overflow-wrap: anywhere; white-space: pre-wrap; }
.msg-user { align-self: flex-end; background: var(--user-bubble); color: #fff; border-bottom-right-radius: 4px; }
.msg-agent { align-self: flex-start; background: var(--agent-bubble); border-bottom-left-radius: 4px; }
.msg-system { align-self: center; text-align: center; color: var(--muted); font-size: 0.85rem; background: transparent; }
.msg-meta { font-size: 0.75rem; opacity: 0.75; margin-bottom: 2px; }
.msg-failed { border: 1px solid var(--danger); }
.steps { margin: 6px 0 0; padding: 0; list-style: none; font-size: 0.8rem; color: var(--muted); white-space: normal; }
.steps li::before { content: "• "; }
.steps li.blocked { color: var(--danger); }
.steps li.sub { padding-left: 12px; }
.msg-actions { margin-top: 8px; display: flex; gap: 8px; flex-wrap: wrap; }
.msg a { color: inherit; }

.composer { border-top: 1px solid var(--line); background: var(--panel); padding: 8px 12px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
.composer textarea {
  width: 100%;
  resize: vertical;
  min-height: 64px;
  max-height: 40vh;
  padding: 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  font: inherit;
}
.composer-row { display: flex; align-items: center; gap: 8px; margin-top: 8px; }

/* Preview */
.preview-bar { display: flex; gap: 6px; padding: 6px 8px; border-bottom: 1px solid var(--line); background: var(--panel); align-items: center; }
.preview-bar input { min-height: 40px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); font: inherit; }
#frame { flex: 1 1 auto; width: 100%; border: 0; background: #fff; }

/* Sign-in page */
.login-body { align-items: center; justify-content: center; padding: 16px; overflow: auto; }
.login-card { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 24px; }
.login-card h1 { margin: 0 0 8px; font-size: 1.4rem; }
.status { min-height: 1.5em; overflow-wrap: anywhere; }
