:root {
  color-scheme: light dark;
  --bg: #f6f6f4;
  --surface: #ffffff;
  --border: #dcdcd6;
  --text: #1c1c1a;
  --muted: #6b6b66;
  --accent: #2f5fd0;
  --accent-text: #ffffff;
  --user-bg: #e4ecfb;
  --code-bg: #f0f0ec;
  --ok: #2c8a4b;
  --warn: #b7791f;
  --error: #c0392b;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161615;
    --surface: #1f1f1d;
    --border: #35352f;
    --text: #ecece8;
    --muted: #9a9a92;
    --accent: #7ea2f5;
    --accent-text: #10131a;
    --user-bg: #263352;
    --code-bg: #2a2a27;
    --ok: #5cc27e;
    --warn: #e0a84a;
    --error: #ef7b6e;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}

header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

header h1 { margin: 0; font-size: 16px; font-weight: 600; }

.spacer { flex: 1; }

.status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

.status::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
}

.status[data-state="ready"]::before { background: var(--ok); }
.status[data-state="working"]::before { background: var(--accent); animation: pulse 1s infinite; }
.status[data-state="reconnecting"]::before,
.status[data-state="connecting"]::before { background: var(--warn); }
.status[data-state="down"]::before { background: var(--error); }

@keyframes pulse { 50% { opacity: 0.3; } }

.cost {
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  cursor: default;
}

.cost[hidden] { display: none; }

button, select, textarea {
  font: inherit;
  color: inherit;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
}

.user {
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  color: var(--muted);
}

/* The sign-in page, shown instead of the chat when a login is required. */
body.login { align-items: center; justify-content: center; padding: 16px; }

.login-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  width: 100%;
  max-width: 360px;
  padding: 32px 24px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  text-align: center;
}

.login-card h1 { margin: 0; font-size: 18px; font-weight: 600; }
.login-card p { margin: 0; overflow-wrap: anywhere; }

a.button {
  padding: 8px 16px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent);
  color: var(--accent-text);
  text-decoration: none;
}

a.button:hover { filter: brightness(1.08); }

button { padding: 6px 12px; cursor: pointer; }
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.5; cursor: default; }
select { padding: 6px 8px; max-width: 260px; }

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.empty { margin: auto; color: var(--muted); text-align: center; }

.msg { max-width: 860px; width: 100%; margin: 0 auto; display: flex; flex-direction: column; gap: 8px; }

.msg.user .bubble {
  align-self: flex-end;
  max-width: 80%;
  padding: 8px 12px;
  border-radius: 12px;
  background: var(--user-bg);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg.user.pending .bubble { opacity: 0.6; }

.text { white-space: pre-wrap; overflow-wrap: anywhere; }

.text code {
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--code-bg);
  font: 13px/1.45 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

pre {
  margin: 0;
  padding: 10px 12px;
  border-radius: 6px;
  background: var(--code-bg);
  font: 13px/1.45 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 420px;
  overflow: auto;
}

details {
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
}

details > summary {
  padding: 6px 10px;
  cursor: pointer;
  font-size: 13px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

details > .body { padding: 0 10px 10px; display: flex; flex-direction: column; gap: 8px; }

details.tool > summary .name { color: var(--text); font-weight: 600; }
details.tool[data-status="completed"] > summary::after { content: "  ✓"; color: var(--ok); }
details.tool[data-status="error"] > summary::after { content: "  ✗"; color: var(--error); }
details.tool[data-status="running"] > summary::after,
details.tool[data-status="streaming"] > summary::after { content: "  …"; color: var(--accent); }

.label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }

.notice { font-size: 13px; color: var(--muted); }
.notice.turn { font-size: 12px; font-variant-numeric: tabular-nums; }
.notice.error { color: var(--error); }

#permissions {
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 10px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#permissions[hidden] { display: none; }

.permission { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.permission .what { flex: 1; min-width: 200px; overflow-wrap: anywhere; }

#composer {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

#composer textarea { flex: 1; resize: vertical; padding: 8px 10px; min-height: 44px; }

#send { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
#stop { border-color: var(--error); color: var(--error); }
#stop[hidden] { display: none; }
