:root {
  --bg: #faf9f5;
  --surface: #ffffff;
  --surface-2: #f0eee6;
  --border: #e3e0d6;
  --text: #1f1e1c;
  --muted: #6f6c64;
  --accent: #c96442;
  --accent-text: #ffffff;
  --user-bg: #f0eee6;
  --ok: #2f8a4e;
  --err: #c0392b;
  --warn: #b7791f;
  --code-bg: #f4f2ec;
  --radius: 12px;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1b19; --surface: #262522; --surface-2: #302f2b; --border: #3b3934;
    --text: #ecebe6; --muted: #9c998f; --accent: #d97757; --user-bg: #302f2b;
    --ok: #5cb87a; --err: #e5675a; --warn: #e0a84a; --code-bg: #201f1d;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1c1b19; --surface: #262522; --surface-2: #302f2b; --border: #3b3934;
  --text: #ecebe6; --muted: #9c998f; --accent: #d97757; --user-bg: #302f2b;
  --ok: #5cb87a; --err: #e5675a; --warn: #e0a84a; --code-bg: #201f1d;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, sans-serif;
  -webkit-text-size-adjust: 100%;
}
a { color: var(--accent); }
button, input, textarea, select { font: inherit; color: inherit; }
button {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px;
  padding: 7px 12px; cursor: pointer; white-space: nowrap;
}
button:hover { background: var(--surface-2); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.danger { color: var(--err); }
button:disabled { opacity: .5; cursor: default; }
input, textarea, select {
  width: 100%; border: 1px solid var(--border); background: var(--surface);
  border-radius: 8px; padding: 9px 11px;
}
textarea { resize: vertical; }
label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.err-text { color: var(--err); }

/* layout */
.page { max-width: 760px; margin: 0 auto; padding: 16px 16px 48px; }
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 8px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--bg); border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 17px; margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .back { border: none; background: none; font-size: 20px; padding: 4px 8px; }
/* Matching borderless square icon buttons in the top bar (Archive, ⋯). */
.topbar .icon-btn {
  display: inline-flex; align-items: center; justify-content: center; width: 34px; height: 34px;
  padding: 0; border: none; background: none; border-radius: 8px; color: var(--muted);
}
.topbar .icon-btn svg { display: block; }
.topbar .icon-btn:hover { background: var(--surface-2); color: var(--text); }
.topbar .auto-toggle { display: flex; align-items: center; gap: 8px; cursor: pointer; white-space: nowrap; color: var(--muted); }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; margin: 10px 0;
}
.list-item { display: block; text-decoration: none; color: inherit; }
.list-item:hover { border-color: var(--accent); }
.list-item h3 { margin: 0 0 2px; font-size: 16px; }
.badge {
  display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 99px;
  background: var(--surface-2); color: var(--muted);
}
.badge.live { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
h2 { font-size: 15px; margin: 24px 0 6px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }

/* login */
.login { max-width: 360px; margin: 12vh auto; padding: 16px; }
.login h1 { font-size: 22px; }

/* chat */
.chat { display: flex; flex-direction: column; height: 100dvh; }
.messages { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 12px 16px 24px; }
.messages > .inner { max-width: 820px; margin: 0 auto; }
.msg { margin: 10px 0; overflow-wrap: anywhere; }
.msg.user {
  margin-left: 15%; background: var(--user-bg); border-radius: var(--radius);
  padding: 9px 13px; white-space: pre-wrap;
}
.msg.user img { max-width: 100%; max-height: 240px; border-radius: 8px; display: block; margin-bottom: 6px; }
.msg.sub { margin-left: 14px; padding-left: 10px; border-left: 2px solid var(--border); }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md pre {
  background: var(--code-bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; overflow-x: auto; font-size: 13px;
}
.md code { font-family: var(--mono); font-size: 0.9em; }
.md :not(pre) > code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; }
.md pre code.hljs { background: transparent; padding: 0; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 4px 8px; }
.md blockquote { margin: 0; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }
.md img { max-width: 100%; }

details.tool, details.thinking {
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
  margin: 6px 0; font-size: 13.5px;
}
details > summary {
  list-style: none; cursor: pointer; padding: 7px 11px; display: flex; gap: 8px; align-items: baseline;
  min-width: 0;
}
details > summary::-webkit-details-marker { display: none; }
.tool .name { font-weight: 600; flex: none; }
.tool .arg {
  font-family: var(--mono); font-size: 12.5px; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0;
}
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; align-self: center; background: var(--warn); }
.dot.ok { background: var(--ok); }
.dot.err { background: var(--err); }
.dot.run { animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .3; } }
details .body { padding: 0 11px 10px; }
details .body pre {
  margin: 6px 0 0; padding: 8px 10px; background: var(--code-bg); border-radius: 6px;
  font-family: var(--mono); font-size: 12px; white-space: pre-wrap; overflow-wrap: anywhere;
  max-height: 420px; overflow-y: auto;
}
.diff-del { background: color-mix(in srgb, var(--err) 14%, transparent); }
.diff-add { background: color-mix(in srgb, var(--ok) 14%, transparent); }
.thinking summary { color: var(--muted); font-style: italic; }
.thinking .body { color: var(--muted); white-space: pre-wrap; }
.todos { list-style: none; padding: 4px 11px 10px; margin: 0; }
.todos li::before { content: "○ "; color: var(--muted); }
.todos li.in_progress::before { content: "◐ "; color: var(--warn); }
.todos li.completed { color: var(--muted); text-decoration: line-through; }
.todos li.completed::before { content: "● "; color: var(--ok); }
.result-line { font-size: 12px; color: var(--muted); margin: 4px 0 14px; }
.notice { font-size: 13px; color: var(--muted); text-align: center; margin: 10px 0; }
.notice.err { color: var(--err); }

.ask { border: 1px solid var(--accent); border-radius: var(--radius); padding: 12px; background: var(--surface); margin: 10px 0; }
.ask .q { font-weight: 600; margin: 4px 0 8px; }
.ask .opt {
  display: block; width: 100%; text-align: left; white-space: normal; margin: 6px 0; padding: 9px 12px;
}
.ask .opt.sel { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--surface)); }
.ask .opt small { display: block; color: var(--muted); }

.composer {
  border-top: 1px solid var(--border); background: var(--bg);
  padding: 8px 12px; padding-bottom: max(8px, env(safe-area-inset-bottom));
}
.composer .inner { max-width: 820px; margin: 0 auto; }
.composer textarea { min-height: 44px; max-height: 40vh; resize: none; font-size: 16px; }
.composer .actions { display: flex; gap: 6px; margin-top: 6px; align-items: center; flex-wrap: wrap; }
.thumbs { display: flex; gap: 6px; flex-wrap: wrap; }
.thumbs img { height: 48px; border-radius: 6px; border: 1px solid var(--border); }
.live-dots::after { content: "…"; animation: pulse 1s infinite; }
.menu { position: relative; display: flex; }
.menu-pop {
  position: absolute; right: 0; top: 110%; z-index: 10; background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 4px; min-width: 190px;
  box-shadow: 0 6px 24px rgba(0,0,0,.18);
}
.menu-pop button { display: block; width: 100%; text-align: left; border: none; }
dialog {
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text);
  width: min(560px, calc(100vw - 32px)); padding: 16px;
}
dialog::backdrop { background: rgba(0,0,0,.4); }
.repo-row { border: 1px dashed var(--border); border-radius: 10px; padding: 10px; margin: 8px 0; }
@media (max-width: 480px) { .composer .branch { display: none; } }

/* sidebar: fixed on wide screens, a drawer behind ☰ on narrow ones */
:root { --side-w: 240px; }
#sidebar {
  position: fixed; top: 0; bottom: 0; left: 0; z-index: 20; width: var(--side-w);
  overflow-y: auto; background: var(--surface); border-right: 1px solid var(--border);
  padding: 12px 8px; padding-top: max(12px, env(safe-area-inset-top));
}
#sidebar .brand { display: block; font-weight: 600; color: var(--text); text-decoration: none; padding: 4px 10px 10px; }
.side-head {
  display: flex; align-items: center; padding: 4px 10px; font-size: 12px; color: var(--muted);
  text-transform: uppercase; letter-spacing: .04em; font-weight: 600;
}
.side-head .add { border: none; background: none; padding: 0 6px; font-size: 18px; color: var(--muted); }
.side-item {
  display: flex; align-items: center; gap: 6px; padding: 7px 10px; border-radius: 8px;
  color: var(--text); text-decoration: none;
}
.side-head:not(:first-of-type) { margin-top: 14px; }
.side-item .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: inherit; text-decoration: none; }
.side-item .add { opacity: .6; padding: 0 6px; }
.side-item .add:hover { opacity: 1; }
.side-item:hover { background: var(--surface-2); }
/* Each chat is a small rounded card. */
.chat-item {
  flex-direction: column; align-items: stretch; gap: 0; padding: 7px 12px; margin: 4px 6px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
}
.chat-item:hover { background: var(--surface-2); }
.chat-item.active { border-color: var(--accent); background: var(--surface-2); }
/* Chat titles wrap onto more lines instead of being cut off. */
.chat-item .name { font-size: 14px; white-space: normal; overflow-wrap: anywhere; line-height: 1.3; }
.chat-item .proj { font-size: 12px; }
.side-empty { padding: 4px 10px; }
.side-item.active { background: var(--surface-2); font-weight: 600; }
#app { margin-left: var(--side-w); }
#scrim { display: none; }
.nav-toggle { display: none; }
body.no-nav #sidebar { display: none; }
body.no-nav #app { margin-left: 0; }
@media (max-width: 899px) {
  #app { margin-left: 0; }
  #sidebar { transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 24px rgba(0,0,0,.25); }
  body.nav-open #sidebar { transform: none; }
  body.nav-open #scrim { display: block; position: fixed; inset: 0; z-index: 15; background: rgba(0,0,0,.4); }
  .nav-toggle { display: inline-block; border: none; background: none; font-size: 18px; padding: 4px 8px; }
}
.chat.dropping .messages { outline: 2px dashed var(--accent); outline-offset: -8px; }
.thumbs:not(:empty) { margin-bottom: 6px; }
.thumb { position: relative; display: inline-block; }
.thumb .thumb-x {
  position: absolute; top: -6px; right: -6px; width: 22px; height: 22px; padding: 0;
  border-radius: 50%; font-size: 14px; line-height: 20px; text-align: center;
  background: var(--surface); box-shadow: 0 1px 4px rgba(0,0,0,.3);
}
details.steps { border: 1px solid var(--border); border-radius: 10px; margin: 6px 0; font-size: 13.5px; }
details.steps > summary { color: var(--muted); }
details.steps > summary::before { content: "▸"; flex: none; }
details.steps[open] > summary::before { content: "▾"; }
details.steps > .body { padding: 0 8px 4px; }
details.steps > .body > details { margin: 4px 0; }
.inline-action { margin: 4px 0; }
.chat-item.nested { margin-left: 18px; flex-direction: row; align-items: center; gap: 8px; }
.pill { flex: none; font-size: 10.5px; font-weight: 500; line-height: 1; padding: 3px 6px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); }
.pill.running { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }
.pill.waiting { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.chat-item.nested .name { font-size: 13.5px; color: var(--muted); }
.chat-item.nested.active .name { color: var(--text); }
#sidebar { display: flex; flex-direction: column; overflow: hidden; padding-bottom: 0; }
.side-top { flex: 2 1 0; overflow-y: auto; min-height: 0; }
.side-archive { flex: 1 1 0; overflow-y: auto; min-height: 0; border-top: 1px solid var(--border); padding: 8px 0; }
.side-archive .side-head { margin-top: 0; }
.composer .input-row { display: flex; gap: 6px; align-items: flex-end; }
.composer .input-row textarea { flex: 1; }
.composer .input-row button { min-height: 44px; }
@media (pointer: fine) { .touch-only { display: none !important; } }

/* iOS-style switch */
input.switch {
  appearance: none; -webkit-appearance: none; flex: none; position: relative; cursor: pointer;
  width: 40px; height: 24px; margin: 0; padding: 0; border: none; border-radius: 99px;
  background: var(--border); transition: background .2s;
}
input.switch::before {
  content: ""; position: absolute; top: 2px; left: 2px; width: 20px; height: 20px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .2s;
}
input.switch:checked { background: var(--ok); }
input.switch:checked::before { transform: translateX(16px); }
input.switch:disabled { opacity: .5; cursor: default; }
input.switch:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.chat-title { cursor: text; border-radius: 4px; padding: 0 2px; }
.chat-title:hover { background: var(--surface-2); }
.chat-title[contenteditable="true"] { outline: 2px solid var(--accent); background: var(--surface); }

/* "Claude is working" dots under the last message */
.loader { max-width: 820px; margin: 6px auto 0; display: flex; gap: 5px; padding: 6px 2px; }
.loader[hidden] { display: none; }
.loader span { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: bounce 1.2s infinite ease-in-out; }
.loader span:nth-child(2) { animation-delay: .15s; }
.loader span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: .4; } 30% { transform: translateY(-5px); opacity: 1; } }
