:root {
  --bg: #f7f7f5;
  --panel: #ffffff;
  --ink: #1c1c1e;
  --muted: #6b6b70;
  --line: #e6e6e2;
  --accent: #b4532a;      /* terracotta */
  --accent-ink: #ffffff;
  --user-bubble: #efe9e2;
  --assistant-bubble: #ffffff;
  --danger: #b00020;
  --radius: 14px;
  --maxw: 760px;
  font-synthesis: none;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17161a;
    --panel: #201f24;
    --ink: #f2f1ee;
    --muted: #a3a2a8;
    --line: #34333a;
    --accent: #d9764e;
    --user-bubble: #2c2a30;
    --assistant-bubble: #201f24;
    --danger: #ff6b6b;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.55;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

/* Top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px calc(12px + env(safe-area-inset-top, 0px));
  padding-top: max(12px, env(safe-area-inset-top, 0px));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 650;
  letter-spacing: -0.01em;
}
.dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}
.topbar-right { display: flex; align-items: center; gap: 10px; }
.whoami { color: var(--muted); font-size: 0.82rem; max-width: 42vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ghost-btn {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  padding: 7px 12px;
  border-radius: 999px;
  font-size: 0.85rem;
  cursor: pointer;
}
.ghost-btn:hover { border-color: var(--accent); }

/* Thread */
.thread {
  flex: 1;
  width: 100%;
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 20px 16px 8px;
  overflow-y: auto;
}

.empty {
  margin: 8vh auto 0;
  text-align: center;
  padding: 0 8px;
}
.empty h1 { font-size: 1.5rem; letter-spacing: -0.02em; margin: 0 0 8px; }
.empty p { color: var(--muted); margin: 0 auto 20px; max-width: 46ch; }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.chip {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  padding: 8px 13px;
  border-radius: 999px;
  font-size: 0.86rem;
  cursor: pointer;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }

/* Messages */
.msg { display: flex; margin: 14px 0; }
.msg.user { justify-content: flex-end; }
.bubble {
  max-width: 84%;
  padding: 11px 14px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg.user .bubble {
  background: var(--user-bubble);
  border-bottom-right-radius: 4px;
}
.msg.assistant .bubble {
  background: var(--assistant-bubble);
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
}
.bubble.error { border-color: var(--danger); color: var(--danger); }

/* Lightweight markdown-ish rendering */
.bubble code {
  background: color-mix(in srgb, var(--ink) 8%, transparent);
  padding: 1px 5px;
  border-radius: 5px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.9em;
}
.bubble pre {
  background: color-mix(in srgb, var(--ink) 8%, transparent);
  padding: 10px 12px;
  border-radius: 10px;
  overflow-x: auto;
}
.bubble pre code { background: none; padding: 0; }
.bubble strong { font-weight: 650; }

/* Tool activity pill */
.tool-note {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  color: var(--muted);
  margin: 2px 0 6px;
}
.tool-note .spark { color: var(--accent); }

.typing { display: inline-block; width: 1ch; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* Composer */
.composer {
  position: sticky;
  bottom: 0;
  width: 100%;
  max-width: var(--maxw);
  margin: 0 auto;
  display: flex;
  gap: 8px;
  align-items: flex-end;
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom, 0px));
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(8px);
  border-top: 1px solid var(--line);
}
.input {
  flex: 1;
  resize: none;
  max-height: 40vh;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--ink);
  font: inherit;
  line-height: 1.45;
}
.input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 55%, transparent); outline-offset: 0; }
.send-btn {
  flex: 0 0 auto;
  width: 44px; height: 44px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  display: grid; place-items: center;
  cursor: pointer;
}
.send-btn:disabled { opacity: 0.45; cursor: default; }

/* Scrollbar niceties */
.thread::-webkit-scrollbar { width: 9px; }
.thread::-webkit-scrollbar-thumb { background: var(--line); border-radius: 8px; }

/* ---- Passcode gate & auth visibility -----------------------------------
 * Before /api/me resolves the body has neither class, so nothing shows (no
 * flash). Then JS adds `authed` (show app) or `needs-login` (show gate). */
.gate { display: none; }
body.needs-login .gate { display: flex; }
body:not(.authed) .topbar,
body:not(.authed) .thread,
body:not(.authed) .composer { display: none; }

.gate {
  position: fixed;
  inset: 0;
  align-items: center;
  justify-content: center;
  padding: 24px calc(24px + env(safe-area-inset-right, 0px)) 24px calc(24px + env(safe-area-inset-left, 0px));
  background: var(--bg);
  z-index: 20;
}
.login-card {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 24px 22px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
}
.brand-lg { font-size: 0.95rem; margin-bottom: 4px; }
.login-card h1 { font-size: 1.35rem; letter-spacing: -0.02em; margin: 4px 0 0; }
.login-card .muted { color: var(--muted); margin: 0 0 4px; font-size: 0.9rem; }
.passcode-input { width: 100%; }
.primary-btn {
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  padding: 12px 16px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.primary-btn:disabled { opacity: 0.5; cursor: default; }
.login-error { color: var(--danger); font-size: 0.88rem; }
