:root {
  --bg: #f5f4f0;
  --panel: #ffffff;
  --text: #1f1e1b;
  --muted: #6e6c66;
  --border: #e4e2dc;
  --hover: #f0eee8;
  --active: #e9e6dd;
  --user-bg: #2b2a26;
  --user-text: #f7f6f2;
  --code-bg: #efede7;
  --link: #1f5fbf;
  --working: #2563eb;
  --attention: #c2410c;
  --idle: #15803d;
  --offline: #9a978f;
  --error: #b91c1c;
  --attention-bg: #fff4ec;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1917;
    --panel: #22211e;
    --text: #ecebe6;
    --muted: #a19e96;
    --border: #36342f;
    --hover: #2b2a26;
    --active: #33312c;
    --user-bg: #3a3833;
    --user-text: #f3f2ed;
    --code-bg: #2c2a26;
    --link: #7fb0ff;
    --working: #6b9bff;
    --attention: #fb923c;
    --idle: #4ade80;
    --offline: #7c7a73;
    --error: #f87171;
    --attention-bg: #3a2516;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.muted { color: var(--muted); }
.error { color: var(--error); }

.primary {
  background: var(--text);
  color: var(--bg);
  border: 0;
  border-radius: 8px;
  padding: 8px 16px;
  font-weight: 600;
}
.primary:disabled { opacity: .4; cursor: default; }
.ghost, .stop {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 12px;
}
.stop { border-color: var(--attention); color: var(--attention); font-weight: 600; }
.link { background: none; border: 0; color: var(--muted); padding: 0; }
.link:hover { color: var(--text); }

/* Sign-in */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px;
  display: grid;
  gap: 12px;
}
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; }
.login-card input {
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 8px;
  padding: 10px 12px;
}

/* Layout */
#app { display: grid; grid-template-columns: 340px minmax(0, 1fr); height: 100dvh; }
#list-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
}
#chat-pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
#chat { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  min-height: 56px;
}
.brand { font-size: 17px; margin: 0; flex: 1; }
.conn { width: 8px; height: 8px; border-radius: 50%; background: var(--offline); }
.conn.live { background: var(--idle); }
.empty { margin: auto; color: var(--muted); padding: 16px; text-align: center; }

/* Session list */
.session-list { flex: 1; overflow-y: auto; }
.group-head {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--panel);
  padding: 14px 16px 6px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .02em;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.group-head .plan { font-weight: 400; }
.row {
  display: flex;
  gap: 10px;
  padding: 10px 16px;
  color: inherit;
  text-decoration: none;
  border-left: 3px solid transparent;
}
.row:hover { background: var(--hover); }
.row.active { background: var(--active); border-left-color: var(--text); }
.row.dim { opacity: .55; }
.row-main { min-width: 0; flex: 1; }
.row-top { display: flex; align-items: baseline; gap: 8px; }
.row-title { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-time { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.row-sub, .row-preview {
  font-size: 13px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row-preview { color: var(--text); opacity: .8; }
.unread { font-weight: 700; color: var(--text); }
.list-empty { padding: 24px 16px; color: var(--muted); font-size: 14px; }
.list-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 16px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  font-size: 13px;
  color: var(--muted);
}

/* State dot and badge */
.dot { width: 9px; height: 9px; border-radius: 50%; margin-top: 6px; flex-shrink: 0; background: var(--offline); }
.dot.working { background: var(--working); animation: pulse 1.4s ease-in-out infinite; }
.dot.attention { background: var(--attention); }
.dot.idle { background: var(--idle); }
@keyframes pulse { 50% { opacity: .35; } }
@media (prefers-reduced-motion: reduce) { .dot.working { animation: none; } }
.badge {
  font-size: 12px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid currentColor;
  color: var(--offline);
  white-space: nowrap;
}
.badge.working { color: var(--working); }
.badge.attention { color: var(--attention); }
.badge.idle { color: var(--idle); }

/* Chat */
.back {
  display: none;
  font-size: 28px;
  line-height: 1;
  color: var(--text);
  text-decoration: none;
  padding: 0 8px 4px 0;
}
.chat-title { min-width: 0; flex: 1; }
.chat-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attention-banner {
  background: var(--attention-bg);
  color: var(--attention);
  border-bottom: 1px solid var(--border);
  padding: 8px 16px;
  font-size: 14px;
}
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overscroll-behavior: contain;
}
.msg { max-width: min(760px, 100%); overflow-wrap: anywhere; }
.msg.user {
  align-self: flex-end;
  max-width: min(620px, 88%);
  background: var(--user-bg);
  color: var(--user-text);
  border-radius: 14px 14px 4px 14px;
  padding: 8px 12px;
  white-space: pre-wrap;
}
.msg.user.pending { opacity: .55; }
.msg.user .from { display: block; font-size: 11px; opacity: .7; margin-top: 2px; text-align: right; }
.msg.assistant { align-self: flex-start; }
.msg.assistant p { margin: 0 0 8px; white-space: pre-wrap; }
.msg.assistant p:last-child { margin-bottom: 0; }
.msg.assistant pre {
  background: var(--code-bg);
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  margin: 0 0 8px;
  font-size: 13px;
}
.msg.assistant code { font: 13px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.msg.assistant :not(pre) > code { background: var(--code-bg); border-radius: 4px; padding: 1px 4px; }
.msg.assistant a { color: var(--link); }
.msg.tool {
  align-self: flex-start;
  font: 12.5px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
  margin: -4px 0;
}
.msg.tool b { color: var(--text); font-weight: 600; }
.msg.output {
  align-self: stretch;
  margin: 0;
  background: var(--code-bg);
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  font: 12.5px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--muted);
}
.msg.notice { align-self: center; font-size: 12px; color: var(--muted); white-space: pre-wrap; text-align: center; }
.composer {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  padding: 10px 16px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--panel);
}
.composer textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  padding: 8px 12px;
  max-height: 40vh;
  min-height: 40px;
  font-size: 16px; /* no zoom on iOS */
}
.composer-note { margin: 0; padding: 0 16px 10px; font-size: 13px; color: var(--muted); background: var(--panel); }

@media (max-width: 760px) {
  #app { grid-template-columns: minmax(0, 1fr); }
  body.chat-open #list-pane { display: none; }
  body:not(.chat-open) #chat-pane { display: none; }
  .back { display: inline-block; }
  .bar { padding-left: 12px; padding-right: 12px; }
}

/* Answer-mode switch in the chat header */
.mode {
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 8px;
  padding: 4px 6px;
  font-size: 12px;
  max-width: 11rem;
}

/* The reply while it streams */
.msg.assistant.streaming { opacity: .85; }
.msg.assistant.streaming::after { content: '▍'; animation: pulse 1s steps(2) infinite; }

/* Permission prompts and questions to answer here */
.requests {
  border-top: 1px solid var(--border);
  background: var(--attention-bg);
  padding: 10px 16px;
  display: grid;
  gap: 10px;
  max-height: 55vh;
  overflow-y: auto;
}
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  display: grid;
  gap: 8px;
}
.card-title { font-weight: 600; }
.card-sub { font-size: 13px; color: var(--muted); }
.card-detail {
  margin: 0;
  background: var(--code-bg);
  border-radius: 8px;
  padding: 8px 10px;
  font: 12.5px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  max-height: 30vh;
  overflow: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.card-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.question { display: grid; gap: 6px; }
.chip { font-size: 11px; font-weight: 600; border: 1px solid var(--border); border-radius: 999px; padding: 1px 8px; color: var(--muted); }
.options { display: flex; gap: 8px; flex-wrap: wrap; }
.option { border: 1px solid var(--border); background: var(--bg); border-radius: 8px; padding: 6px 12px; }
.option.picked { border-color: var(--text); background: var(--active); font-weight: 600; }
.other { border: 1px solid var(--border); background: var(--bg); border-radius: 8px; padding: 6px 10px; font-size: 16px; }
@media (max-width: 760px) {
  /* The answer switch gets a row of its own, so the title keeps its room */
  .chat-head { flex-wrap: wrap; row-gap: 6px; }
  .mode { order: 10; flex-basis: 100%; max-width: none; }
}
