:root {
  --bg: #faf9f5;
  --surface: #ffffff;
  --surface-2: #f0eee6;
  --border: #e3e0d6;
  --text: #1f1e1d;
  --muted: #6b6860;
  --accent: #c96442;
  --accent-contrast: #ffffff;
  --user-bubble: #efe9dd;
  --ok: #3d8a4f;
  --warn: #b7791f;
  --err: #c0392b;
  --info: #3b6ea8;
  --code-bg: #f3f1ea;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 6%);
  --radius: 14px;
  --mono: ui-monospace, 'Cascadia Code', 'SF Mono', Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1f1e1d;
    --surface: #2a2927;
    --surface-2: #33312e;
    --border: #3d3b37;
    --text: #ecebe6;
    --muted: #a3a09a;
    --accent: #d97757;
    --user-bubble: #3a3632;
    --ok: #6cc080;
    --warn: #e0b050;
    --err: #ef7a6a;
    --info: #7fb0ea;
    --code-bg: #242321;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 25%);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea { font-size: 16px; } /* avoid iOS zoom on focus */
a { color: var(--accent); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 0 14px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); cursor: pointer; white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); font-weight: 600; }
.btn.danger { color: var(--err); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.small { min-height: 32px; padding: 0 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn {
  width: 40px; height: 40px; display: inline-grid; place-items: center;
  border: 0; background: transparent; border-radius: 10px; cursor: pointer; flex: none;
}
.icon-btn:hover { background: var(--surface-2); }
a.icon-btn { color: inherit; }
.code-block { font-family: var(--mono); font-size: 12.5px; background: var(--code-bg); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; overflow-x: auto; white-space: pre; margin: 6px 0 10px; user-select: all; }
.steps { padding-left: 20px; margin: 0 0 8px; font-size: 14px; }
.steps li { margin-bottom: 6px; }
.steps code { font-family: var(--mono); font-size: 13px; background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
.icon-btn svg { width: 22px; height: 22px; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 13px; color: var(--muted); font-weight: 500; }
.input, select.input, textarea.input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); outline: none;
}
.input:focus { border-color: var(--accent); }

.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; background: var(--muted); }
.dot.running, .dot.busy { background: var(--accent); animation: pulse 1.2s ease-in-out infinite; }
.dot.idle { background: var(--ok); }
.dot.requires_action, .dot.waiting { background: var(--warn); animation: pulse .8s ease-in-out infinite; }
.dot.error { background: var(--err); }
@keyframes pulse { 50% { opacity: .35; } }

.badge {
  display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 500;
  padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.badge.accent { background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent); }
.badge.warn { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }

/* ---------- layout ---------- */
.page { max-width: 820px; margin: 0 auto; padding: 0 16px calc(24px + env(safe-area-inset-bottom)); }
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.topbar .title { flex: 1; min-width: 0; }
.topbar h1 { font-size: 17px; margin: 0; font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .sub { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; overflow: hidden; white-space: nowrap; }
.topbar .sub span { overflow: hidden; text-overflow: ellipsis; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { width: 28px; height: 28px; }

/* ---------- login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login form { width: min(360px, 100%); background: var(--surface); padding: 28px 24px; border-radius: 18px; box-shadow: var(--shadow); }
.login h1 { margin: 0 0 4px; font-size: 22px; display: flex; align-items: center; gap: 10px; }
.login p { color: var(--muted); margin: 0 0 20px; font-size: 14px; }
.login .error { color: var(--err); font-size: 14px; min-height: 20px; margin-bottom: 8px; }
.login .btn { width: 100%; }

/* ---------- dashboard ---------- */
.section-title { font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 22px 4px 8px; display: flex; align-items: center; gap: 8px; }
.section-title .spacer { flex: 1; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.list { display: flex; flex-direction: column; }
.list > * + * { border-top: 1px solid var(--border); }
.row {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px; cursor: pointer; min-height: 56px;
  text-decoration: none; color: inherit;
}
.row:hover { background: color-mix(in srgb, var(--surface-2) 50%, transparent); }
.row .main { flex: 1; min-width: 0; }
.row .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .meta { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: flex; gap: 6px; align-items: center; }
.row .chev { color: var(--muted); flex: none; transition: transform .15s; }
.project.open > .row .chev { transform: rotate(90deg); }
.project .sessions { background: color-mix(in srgb, var(--surface-2) 40%, transparent); border-top: 1px solid var(--border); }
.project .sessions .row { padding-left: 22px; min-height: 50px; }
.project .sessions .row .name { font-weight: 500; font-size: 14px; }
.project .actions { display: flex; gap: 8px; padding: 10px 14px 10px 22px; flex-wrap: wrap; }
.empty { padding: 18px; color: var(--muted); text-align: center; font-size: 14px; }
.search { display: flex; gap: 8px; margin-top: 14px; }
.search .input { flex: 1; }

/* ---------- sheet / modal ---------- */
.backdrop {
  position: fixed; inset: 0; z-index: 50; background: rgb(0 0 0 / 40%);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .15s ease-out;
}
@media (min-width: 700px) { .backdrop { align-items: center; } }
.sheet {
  width: min(640px, 100%); max-height: 88dvh; overflow: auto;
  background: var(--bg); border-radius: 18px 18px 0 0; padding: 18px 18px calc(18px + env(safe-area-inset-bottom));
  box-shadow: var(--shadow); animation: slideup .2s ease-out;
}
@media (min-width: 700px) { .sheet { border-radius: 18px; } }
.sheet h2 { margin: 0 0 14px; font-size: 18px; display: flex; align-items: center; gap: 8px; }
.sheet h2 .spacer { flex: 1; }
.sheet .btns { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 8px; }
@keyframes fade { from { opacity: 0; } }
@keyframes slideup { from { transform: translateY(24px); opacity: .6; } }

/* ---------- session view ---------- */
.session { display: flex; flex-direction: column; height: 100dvh; }
.transcript { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 12px 0 8px; }
.transcript-inner { max-width: 820px; margin: 0 auto; padding: 0 14px; display: flex; flex-direction: column; gap: 10px; }
.banner { max-width: 820px; margin: 8px auto 0; padding: 10px 14px; border-radius: 12px; font-size: 14px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.banner.info { background: color-mix(in srgb, var(--info) 14%, transparent); }
.banner.warn { background: color-mix(in srgb, var(--warn) 16%, transparent); }
.banner-wrap { padding: 0 14px; }

.msg-user { align-self: flex-end; max-width: 88%; background: var(--user-bubble); padding: 9px 13px; border-radius: 16px 16px 4px 16px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-user .imgs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 6px; }
.msg-user .imgs img { max-width: 140px; max-height: 140px; border-radius: 8px; object-fit: cover; }
.msg-text { overflow-wrap: anywhere; }
.msg-text > :first-child { margin-top: 0; }
.msg-text > :last-child { margin-bottom: 0; }
.msg-text pre { background: var(--code-bg); border: 1px solid var(--border); padding: 10px 12px; border-radius: 10px; overflow-x: auto; font-size: 13px; }
.msg-text code { font-family: var(--mono); font-size: .9em; }
.msg-text :not(pre) > code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
.msg-text table { border-collapse: collapse; display: block; overflow-x: auto; font-size: 14px; }
.msg-text th, .msg-text td { border: 1px solid var(--border); padding: 5px 9px; text-align: left; }
.msg-text blockquote { margin: 0; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }
.msg-text img { max-width: 100%; }
.draft { opacity: .9; }
.draft::after { content: '▍'; animation: pulse 1s infinite; color: var(--accent); }

.msg-system { align-self: center; font-size: 12.5px; color: var(--muted); text-align: center; max-width: 90%; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-system.error { color: var(--err); }
.msg-system.warning { color: var(--warn); }
.msg-result { align-self: flex-start; font-size: 12px; color: var(--muted); }

details.thinking { font-size: 13.5px; color: var(--muted); }
details.thinking summary { cursor: pointer; list-style: none; font-style: italic; }
details.thinking summary::-webkit-details-marker { display: none; }
details.thinking > div { white-space: pre-wrap; padding: 6px 0 0 12px; border-left: 2px solid var(--border); margin-top: 4px; }

.tool { border: 1px solid var(--border); background: var(--surface); border-radius: 10px; font-size: 13.5px; overflow: hidden; }
.tool > summary { display: flex; align-items: center; gap: 8px; padding: 7px 10px; cursor: pointer; list-style: none; min-height: 36px; }
.tool > summary::-webkit-details-marker { display: none; }
.tool .tname { font-weight: 600; flex: none; }
.tool .tsum { color: var(--muted); font-family: var(--mono); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; }
.tool .tstate { flex: none; font-size: 13px; width: 16px; text-align: center; }
.tool .tstate.ok { color: var(--ok); }
.tool .tstate.err { color: var(--err); }
.tool .tbody { border-top: 1px solid var(--border); padding: 8px 10px; display: flex; flex-direction: column; gap: 8px; }
.tool pre { margin: 0; font-family: var(--mono); font-size: 12px; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 360px; overflow: auto; background: var(--code-bg); padding: 8px; border-radius: 8px; }
.tool .label { font-size: 11px; font-weight: 600; text-transform: uppercase; color: var(--muted); letter-spacing: .04em; }
.tool.agent { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); }
.tool.agent .tname { color: var(--accent); }
.diff { font-family: var(--mono); font-size: 12px; background: var(--code-bg); border-radius: 8px; overflow: auto; max-height: 360px; }
.diff div { white-space: pre-wrap; overflow-wrap: anywhere; padding: 0 8px; }
.diff .del { background: color-mix(in srgb, var(--err) 16%, transparent); }
.diff .add { background: color-mix(in srgb, var(--ok) 16%, transparent); }
.todos { list-style: none; margin: 0; padding: 0; }
.todos li { display: flex; gap: 8px; padding: 2px 0; }
.todos li.completed { color: var(--muted); text-decoration: line-through; }
.todos li.in_progress { font-weight: 600; }
.spinner { width: 14px; height: 14px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* permission cards */
.pending { max-width: 820px; margin: 0 auto; width: 100%; padding: 0 12px; display: flex; flex-direction: column; gap: 8px; }
.perm { background: var(--surface); border: 1.5px solid var(--warn); border-radius: 14px; padding: 12px; box-shadow: var(--shadow); max-height: 55dvh; overflow: auto; }
.perm .ptitle { font-weight: 600; margin-bottom: 4px; }
.perm .pdesc { font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.perm pre { margin: 0 0 10px; font-family: var(--mono); font-size: 12px; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 200px; overflow: auto; background: var(--code-bg); padding: 8px; border-radius: 8px; }
.perm .btns { display: flex; gap: 8px; flex-wrap: wrap; }
.perm .btns .btn { flex: 1 1 auto; }
.perm .plan { max-height: 40dvh; overflow: auto; border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; margin-bottom: 10px; }
.question { margin-bottom: 12px; }
.question .q { font-weight: 600; margin-bottom: 6px; }
.question .opt { display: flex; gap: 10px; align-items: flex-start; padding: 8px 10px; border: 1px solid var(--border); border-radius: 10px; margin-bottom: 6px; cursor: pointer; }
.question .opt:has(input:checked) { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); }
.question .opt input { margin-top: 4px; accent-color: var(--accent); }
.question .opt small { display: block; color: var(--muted); }

/* composer */
.composer-wrap { border-top: 1px solid var(--border); background: var(--bg); padding: 8px 12px calc(8px + env(safe-area-inset-bottom)); }
.statusline { max-width: 820px; margin: 0 auto 6px; display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); min-height: 18px; }
.statusline .spacer { flex: 1; }
.statusline select { font-size: 12.5px; padding: 2px 4px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); }
.composer { max-width: 820px; margin: 0 auto; display: flex; align-items: flex-end; gap: 6px; background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 4px; }
.composer:focus-within { border-color: var(--accent); }
.composer textarea { flex: 1; border: 0; outline: 0; resize: none; background: transparent; padding: 8px 6px; max-height: 40dvh; min-height: 40px; line-height: 1.4; }
.composer .send { width: 40px; height: 40px; border-radius: 50%; border: 0; background: var(--accent); color: #fff; display: grid; place-items: center; cursor: pointer; flex: none; }
.composer .send.stop { background: var(--text); color: var(--bg); }
.composer .send:disabled { opacity: .4; }
.composer .send svg { width: 20px; height: 20px; }
.attachments { max-width: 820px; margin: 0 auto 6px; display: flex; gap: 6px; flex-wrap: wrap; }
.attachments .att { position: relative; }
.attachments img { width: 56px; height: 56px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }
.attachments button { position: absolute; top: -6px; right: -6px; width: 22px; height: 22px; border-radius: 50%; border: 0; background: var(--text); color: var(--bg); font-size: 13px; line-height: 1; cursor: pointer; }
.cmd-suggest { max-width: 820px; margin: 0 auto 6px; display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.cmd-suggest button { flex: none; border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 4px 10px; font-family: var(--mono); font-size: 13px; cursor: pointer; }

.jump { position: absolute; right: 18px; bottom: 12px; z-index: 5; box-shadow: var(--shadow); border-radius: 999px; }
.transcript-box { position: relative; flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* agents panel */
.agent-item { padding: 12px 4px; display: flex; gap: 10px; align-items: flex-start; cursor: pointer; }
.agent-item + .agent-item { border-top: 1px solid var(--border); }
.agent-item .main { flex: 1; min-width: 0; }
.agent-item .name { font-weight: 600; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.agent-item .meta { font-size: 12.5px; color: var(--muted); }
.agent-item .summary { font-size: 13px; margin-top: 2px; }
.agent-feed { display: flex; flex-direction: column; gap: 8px; }
.agent-feed .prompt { font-size: 13px; background: var(--surface-2); border-radius: 10px; padding: 8px 10px; white-space: pre-wrap; max-height: 160px; overflow: auto; }
.agents-btn { position: relative; }
.agents-btn .count { position: absolute; top: 2px; right: 0; background: var(--accent); color: #fff; font-size: 10px; font-weight: 700; min-width: 16px; height: 16px; border-radius: 8px; display: grid; place-items: center; padding: 0 4px; }

/* menu */
.menu { position: absolute; right: 8px; top: calc(52px + env(safe-area-inset-top)); z-index: 30; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); min-width: 220px; padding: 6px; }
.menu button { display: flex; width: 100%; text-align: left; border: 0; background: transparent; padding: 10px 12px; border-radius: 8px; cursor: pointer; gap: 8px; align-items: center; }
.menu button:hover { background: var(--surface-2); }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 4px 0; }

/* toasts */
#toasts { position: fixed; left: 50%; transform: translateX(-50%); top: calc(10px + env(safe-area-inset-top)); z-index: 100; display: flex; flex-direction: column; gap: 8px; width: min(420px, calc(100% - 24px)); pointer-events: none; }
.toast { pointer-events: auto; background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 12px; box-shadow: var(--shadow); font-size: 14px; cursor: pointer; animation: slideup .2s ease-out; }
.toast.error { background: var(--err); color: #fff; }
