:root {
  --bg: #0b0b0c;
  --surface: #161619;
  --surface-2: #1f1f24;
  --border: #2a2a31;
  --text: #ececed;
  --muted: #9a9aa2;
  --accent: #d97757;
  --accent-2: #e8886a;
  --danger: #e5484d;
  --ok: #46c46a;
  --radius: 16px;
  --topbar-h: 54px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- Pairing gate ---------- */
.gate {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center;
  background: radial-gradient(120% 120% at 50% 0%, #16130f 0%, var(--bg) 60%);
  padding: 24px;
}
.gate-card {
  width: 100%; max-width: 360px;
  display: flex; flex-direction: column; gap: 14px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 20px; padding: 28px 22px;
  text-align: center;
}
.gate-logo { font-size: 34px; color: var(--accent); }
.gate-card h1 { margin: 0; font-size: 22px; font-weight: 650; }
.gate-card p { margin: 0; }
.gate-card input {
  width: 100%; padding: 13px 14px; font-size: 16px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px; outline: none;
}
.gate-card input:focus { border-color: var(--accent); }
.err { color: var(--danger); font-size: 13px; min-height: 16px; }

button.primary {
  background: linear-gradient(180deg, var(--accent-2), var(--accent));
  color: #1a0f0a; border: none; border-radius: 12px;
  padding: 13px; font-size: 16px; font-weight: 650; cursor: pointer;
}
button.primary:active { transform: translateY(1px); }

/* ---------- Top bar ---------- */
.topbar {
  height: calc(var(--topbar-h) + var(--safe-top));
  padding-top: var(--safe-top);
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 8px;
  padding-left: 8px; padding-right: 8px;
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  position: relative; z-index: 20;
}
.topbar-center {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; justify-content: center;
}
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: 0 0 auto; transition: background .2s; }
.dot.on { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.dot.busy { background: var(--accent); box-shadow: 0 0 8px var(--accent); }

#account-select {
  max-width: 60vw;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 7px 10px; font-size: 14px; outline: none;
  text-overflow: ellipsis;
}
.icon-btn {
  flex: 0 0 auto; width: 40px; height: 40px;
  display: grid; place-items: center;
  background: transparent; color: var(--text);
  border: none; border-radius: 10px; font-size: 22px; cursor: pointer;
}
.icon-btn:active { background: var(--surface-2); }

/* ---------- Drawer ---------- */
.drawer {
  position: fixed; top: 0; left: 0; bottom: 0; z-index: 40;
  width: min(82vw, 340px);
  padding-top: calc(var(--safe-top) + 8px);
  background: var(--surface); border-right: 1px solid var(--border);
  transform: translateX(0); transition: transform .22s ease;
  display: flex; flex-direction: column;
}
.drawer.hidden { display: flex !important; transform: translateX(-102%); }
.drawer-head {
  flex: 0 0 auto;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; font-weight: 650; border-bottom: 1px solid var(--border);
}
.chip {
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 999px;
  padding: 6px 12px; font-size: 13px; cursor: pointer;
}
.session-list { overflow-y: auto; padding: 8px; display: flex; flex-direction: column; gap: 6px; }
/* Live list sizes to its content (capped); Resume list takes the rest + scrolls. */
#session-list { flex: 0 0 auto; max-height: 35vh; }
#disk-list { flex: 1 1 auto; min-height: 0; }
.session-item {
  padding: 11px 12px; border-radius: 12px; cursor: pointer;
  background: var(--surface-2); border: 1px solid transparent;
  display: flex; flex-direction: column; gap: 3px;
}
.session-item.active { border-color: var(--accent); }
.session-item .title { font-size: 14px; font-weight: 550; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-item .sub { font-size: 12px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.state-badge { font-size: 10px; padding: 1px 6px; border-radius: 999px; background: var(--border); color: var(--muted); }
.state-badge.busy { background: color-mix(in srgb, var(--accent) 30%, transparent); color: var(--accent-2); }
.state-badge.idle { background: color-mix(in srgb, var(--ok) 22%, transparent); color: var(--ok); }
.state-badge.exited { background: color-mix(in srgb, var(--danger) 22%, transparent); color: var(--danger); }

.scrim { position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.5); }

/* ---------- Messages ---------- */
.messages {
  flex: 1 1 auto; overflow-y: auto;
  padding: 16px 12px 20px;
  display: flex; flex-direction: column; gap: 12px;
  overscroll-behavior: contain;
}
.empty { margin: auto; text-align: center; color: var(--muted); display: flex; flex-direction: column; gap: 6px; }
.empty-glyph { font-size: 40px; color: var(--accent); opacity: .8; }

.msg { max-width: 88%; display: flex; flex-direction: column; gap: 4px; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; align-items: flex-start; }
.bubble {
  padding: 10px 14px; border-radius: var(--radius);
  white-space: pre-wrap; word-wrap: break-word; line-height: 1.5;
  font-size: 15px;
}
.msg.user .bubble {
  background: linear-gradient(180deg, var(--accent-2), var(--accent));
  color: #1a0f0a; border-bottom-right-radius: 6px;
}
.msg.assistant .bubble {
  background: var(--surface); border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}
.bubble.streaming::after {
  content: "▋"; color: var(--accent); animation: blink 1s steps(2) infinite; margin-left: 1px;
}
@keyframes blink { 50% { opacity: 0; } }

/* Tool call cards */
.tool {
  align-self: flex-start; max-width: 92%;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 12px; padding: 8px 12px; font-size: 13px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace; color: var(--muted);
  white-space: pre-wrap; word-break: break-word;
}
.tool .tool-name { color: var(--accent-2); font-weight: 600; }
.tool.result { color: var(--text); }

.turn-meta { font-size: 11px; color: var(--muted); align-self: flex-start; }

/* ---------- Status bar ---------- */
.status-bar {
  flex: 0 0 auto; padding: 4px 14px; font-size: 12px; color: var(--muted);
  border-top: 1px solid var(--border); background: var(--surface);
  display: flex; gap: 12px; align-items: center; justify-content: space-between;
}

/* ---------- Permission card ---------- */
.perm {
  align-self: center; max-width: 92%;
  background: var(--surface); border: 1px solid var(--accent);
  border-radius: 14px; padding: 12px 14px; font-size: 14px;
  display: flex; flex-direction: column; gap: 10px;
}
.perm .row { display: flex; gap: 8px; }
.perm button { flex: 1; padding: 9px; border-radius: 10px; border: 1px solid var(--border); font-size: 14px; cursor: pointer; }
.perm .allow { background: var(--ok); color: #05210d; border: none; }
.perm .deny { background: transparent; color: var(--text); }

/* ---------- Composer ---------- */
.composer {
  flex: 0 0 auto;
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px 10px calc(10px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(12px);
}
#input {
  flex: 1; resize: none; max-height: 40vh;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 20px;
  padding: 11px 14px; font-size: 16px; line-height: 1.4; outline: none;
  font-family: inherit;
}
#input:focus { border-color: color-mix(in srgb, var(--accent) 60%, var(--border)); }
.send-btn {
  flex: 0 0 auto; width: 44px; height: 44px; border-radius: 50%;
  background: linear-gradient(180deg, var(--accent-2), var(--accent));
  color: #1a0f0a; border: none; font-size: 18px; cursor: pointer;
  display: grid; place-items: center; transition: opacity .15s, transform .1s;
}
.send-btn:disabled { opacity: .4; }
.send-btn:active:not(:disabled) { transform: scale(.94); }

@media (min-width: 720px) {
  .messages, .composer { max-width: 820px; width: 100%; margin-left: auto; margin-right: auto; }
  .topbar { justify-content: center; }
}

/* ---------- Tool cards (claude.ai-style) ---------- */
.tool-card {
  align-self: flex-start; max-width: 94%;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; overflow: hidden; font-size: 13.5px;
}
.tool-card.sub { border-left: 2px solid var(--accent); margin-left: 12px; }
.tool-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; }
.tool-icon { flex: 0 0 auto; font-size: 14px; }
.tool-title {
  flex: 1; min-width: 0; font-weight: 550; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tool-status { flex: 0 0 auto; font-size: 12px; color: var(--muted); }
.tool-status.done { color: var(--ok); }
.tool-status.running { color: var(--accent); }
.badge {
  flex: 0 0 auto; font-size: 10px; padding: 2px 7px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.badge.bg { color: var(--accent-2); border-color: color-mix(in srgb, var(--accent) 40%, var(--border)); }
.tool-cmd, .tool-sub, .tool-out pre {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  white-space: pre-wrap; word-break: break-word; overflow-x: auto; margin: 0;
}
.tool-cmd {
  padding: 8px 12px; background: #0d0d0f; border-top: 1px solid var(--border);
  font-size: 12.5px; color: #d7d7db;
}
.tool-sub {
  padding: 6px 12px; border-top: 1px solid var(--border);
  font-size: 12px; color: var(--muted);
}
.tool-out { position: relative; border-top: 1px solid var(--border); background: #0d0d0f; }
.tool-out pre { padding: 8px 12px; font-size: 12px; color: #b9b9c0; }
.tool-out.clamped pre { max-height: 160px; overflow: hidden; }
.tool-out.clamped::after {
  content: ""; position: absolute; left: 0; right: 0; bottom: 26px; height: 40px;
  background: linear-gradient(transparent, #0d0d0f); pointer-events: none;
}
.more-btn {
  display: block; width: 100%; padding: 5px; background: var(--surface-2);
  color: var(--accent-2); border: none; border-top: 1px solid var(--border);
  font-size: 12px; cursor: pointer;
}
.thinking { align-self: flex-start; max-width: 94%; }
.thinking-head { font-size: 12.5px; color: var(--muted); font-style: italic; }
.thinking-body {
  margin: 4px 0 0; padding: 8px 12px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; font-size: 12.5px; color: var(--muted); white-space: pre-wrap;
}
.todo-list { padding: 6px 12px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 3px; }
.todo-row { display: flex; gap: 8px; align-items: flex-start; font-size: 13px; }
.todo-mark { flex: 0 0 auto; color: var(--muted); }
.todo-mark.completed { color: var(--ok); }
.todo-mark.in_progress { color: var(--accent); }
.turn-footer { align-self: center; font-size: 11px; color: var(--muted); opacity: .8; padding: 2px 0; }
.err-meta { color: var(--danger); }
.msg.assistant.sub { margin-left: 12px; border-left: 2px solid var(--accent); padding-left: 8px; }
.diff { border-top: 1px solid var(--border); background: #0d0d0f; overflow-x: auto; }
.diff-line {
  font-family: ui-monospace, Menlo, monospace; font-size: 12px; padding: 0 12px;
  white-space: pre-wrap; word-break: break-word;
}
.diff-line.del { color: #e5808a; background: rgba(229, 72, 77, 0.08); }
.diff-line.ins { color: #7bd88f; background: rgba(70, 196, 106, 0.08); }
.tool-card.error { border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); }
.tool-card.error .tool-out pre { color: #e5808a; }
.tool-status.err { color: var(--danger); }
.bubble .md-pre {
  background: #0d0d0f; border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; margin: 6px 0; overflow-x: auto;
  font-family: ui-monospace, Menlo, monospace; font-size: 12.5px;
}
.bubble .md-code {
  background: var(--surface-2); border-radius: 4px; padding: 1px 5px;
  font-family: ui-monospace, Menlo, monospace; font-size: 0.9em;
}
.bubble .md-h { display: inline-block; margin-top: 4px; }
.bubble a { color: var(--accent-2); }
