:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #efede8;
  --text: #1c1c1e;
  --muted: #6b6a66;
  --border: #dedbd3;
  --accent: #6d4fd1;
  --accent-text: #ffffff;
  --user-bubble: #6d4fd1;
  --user-text: #ffffff;
  --danger: #c0392b;
  --ok: #2e8b57;
  --warn: #b7791f;
  --code-bg: #f0eee9;
  --diff-add: rgba(46, 139, 87, 0.15);
  --diff-del: rgba(192, 57, 43, 0.14);
  --radius: 16px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171a;
    --surface: #1f2024;
    --surface-2: #2a2b30;
    --text: #ecebea;
    --muted: #9b9aa0;
    --border: #34353b;
    --accent: #9b82f5;
    --accent-text: #120d24;
    --user-bubble: #4b3a91;
    --user-text: #f3f0ff;
    --danger: #ef6b5b;
    --ok: #5cc98a;
    --warn: #e0b04f;
    --code-bg: #26272c;
    --diff-add: rgba(92, 201, 138, 0.16);
    --diff-del: rgba(239, 107, 91, 0.16);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; cursor: pointer; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- login ---------- */
.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.login-card {
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.login-logo { margin: 0 auto 4px; border-radius: 16px; }
.login-card h1 { font-size: 1.4rem; margin: 0 0 8px; }
.login-card input {
  font: inherit;
  font-size: 17px;
  padding: 14px 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
.login-card input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.btn-primary {
  padding: 14px 16px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  font-size: 17px;
}
.btn-primary:disabled { opacity: 0.6; }
.login-error { color: var(--danger); min-height: 1.5em; margin: 0; font-size: 0.95rem; }

/* ---------- app layout ---------- */
.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.title { display: flex; align-items: center; gap: 8px; margin-right: auto; }
.brand { font-weight: 700; font-size: 1.1rem; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.dot.ok { background: var(--ok); }
.dot.bad { background: var(--danger); }

.chip {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 0.8rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.icon-btn {
  width: 40px; height: 40px;
  display: grid; place-items: center;
  border: 0; background: transparent; border-radius: 10px;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg, .send svg {
  width: 22px; height: 22px;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

.messages {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.empty { margin: auto 0; text-align: center; color: var(--muted); }
.empty-title { font-size: 1.05rem; margin-bottom: 16px; }
.suggestions { display: flex; flex-direction: column; gap: 8px; align-items: center; }
.suggestion {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 10px 16px;
  font-size: 0.95rem;
}

/* ---------- messages ---------- */
.msg { max-width: 100%; overflow-wrap: anywhere; }
.msg.user {
  align-self: flex-end;
  max-width: 85%;
  background: var(--user-bubble);
  color: var(--user-text);
  padding: 10px 14px;
  border-radius: var(--radius) var(--radius) 4px var(--radius);
  white-space: pre-wrap;
}
.msg.assistant { align-self: stretch; }
.msg.note {
  align-self: stretch;
  font-size: 0.9rem;
  color: var(--muted);
  border-left: 3px solid var(--border);
  padding: 2px 0 2px 10px;
  white-space: pre-wrap;
}
.msg.note.ok { border-left-color: var(--ok); }
.msg.note.bad { border-left-color: var(--danger); }

.status {
  font-size: 0.82rem;
  color: var(--muted);
  font-family: var(--mono);
  margin: 2px 0;
  display: flex;
  gap: 6px;
  align-items: baseline;
}
.status::before { content: "›"; }
.status.live::after {
  content: "";
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent);
  animation: pulse 1s infinite ease-in-out;
  align-self: center;
}
@keyframes pulse { 50% { opacity: 0.2; } }

.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol { margin: 0.5em 0; }
.md ul, .md ol { padding-left: 1.3em; }
.md h1, .md h2, .md h3 { font-size: 1.05rem; margin: 0.9em 0 0.4em; }
.md a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
.md code {
  font-family: var(--mono);
  font-size: 0.88em;
  background: var(--code-bg);
  padding: 1px 5px;
  border-radius: 5px;
}
.md a code { color: var(--accent); }
.md pre {
  background: var(--code-bg);
  padding: 10px 12px;
  border-radius: 10px;
  overflow-x: auto;
  font-size: 0.85rem;
}
.md pre code { background: none; padding: 0; }
.md blockquote { margin: 0.5em 0; padding-left: 10px; border-left: 3px solid var(--border); color: var(--muted); }
.md table { border-collapse: collapse; display: block; overflow-x: auto; font-size: 0.9rem; }
.md th, .md td { border: 1px solid var(--border); padding: 4px 8px; }
.cursor::after { content: "▍"; color: var(--accent); animation: pulse 1s infinite; }

.error { color: var(--danger); font-size: 0.92rem; }

/* ---------- confirm card ---------- */
.confirm {
  border: 1px solid var(--warn);
  background: var(--surface);
  border-radius: 14px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.confirm-title { font-weight: 700; display: flex; gap: 8px; align-items: center; }
.confirm-title::before { content: "⚠"; color: var(--warn); }
.confirm-detail { font-family: var(--mono); font-size: 0.85rem; white-space: pre-wrap; overflow-wrap: anywhere; }
.confirm pre {
  margin: 0;
  max-height: 40vh;
  overflow: auto;
  background: var(--code-bg);
  border-radius: 8px;
  padding: 8px 10px;
  font-family: var(--mono);
  font-size: 0.78rem;
  line-height: 1.4;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.confirm pre .add { background: var(--diff-add); display: block; }
.confirm pre .del { background: var(--diff-del); display: block; }
.confirm pre .hunk { color: var(--muted); display: block; }
.confirm-actions { display: flex; gap: 10px; }
.confirm-actions button {
  flex: 1;
  padding: 12px;
  border-radius: 10px;
  font-weight: 600;
  border: 1px solid var(--border);
  background: var(--surface-2);
}
.confirm-actions .yes { background: var(--danger); border-color: var(--danger); color: #fff; }
.confirm-actions button:disabled { opacity: 0.5; }
.confirm-state { font-size: 0.9rem; color: var(--muted); }

/* ---------- composer ---------- */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px calc(env(safe-area-inset-bottom) + 10px);
  border-top: 1px solid var(--border);
  background: var(--bg);
}
.composer textarea {
  flex: 1;
  resize: none;
  font: inherit;
  font-size: 17px; /* >=16px stops iOS zooming on focus */
  line-height: 1.4;
  min-height: 48px;
  max-height: 40vh;
  padding: 12px 14px;
  border-radius: 22px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.send {
  width: 48px; height: 48px;
  flex: none;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  display: grid; place-items: center;
}
.send:disabled { opacity: 0.45; }
.send.stop svg { stroke-width: 0; fill: currentColor; }

@media (min-width: 720px) {
  .messages, .composer { padding-left: max(16px, calc((100% - 720px) / 2)); padding-right: max(16px, calc((100% - 720px) / 2)); }
}
