:root {
  --bg: #f6f5f2;
  --panel: #ffffff;
  --panel-2: #fbfaf8;
  --ink: #1d1d1f;
  --muted: #6b6b70;
  --faint: #9a9aa0;
  --line: #e4e2dd;
  --accent: #2f5bd3;
  --accent-ink: #ffffff;
  --accent-soft: #e8eefc;
  --ok: #1f8a4c;
  --ok-soft: #e3f3e9;
  --warn: #b26a00;
  --warn-soft: #fbf0dc;
  --bad: #c43d3d;
  --bad-soft: #fbe7e7;
  --code: #f0eee9;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141416; --panel: #1c1c1f; --panel-2: #19191c; --ink: #ececee; --muted: #a0a0a8; --faint: #73737b;
    --line: #2e2e33; --accent: #7d9bff; --accent-ink: #0d1530; --accent-soft: #222a42; --ok: #5cc98a; --ok-soft: #1a2e22;
    --warn: #e0a54a; --warn-soft: #33281a; --bad: #ef7b7b; --bad-soft: #3a1f1f; --code: #26262a;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141416; --panel: #1c1c1f; --panel-2: #19191c; --ink: #ececee; --muted: #a0a0a8; --faint: #73737b;
  --line: #2e2e33; --accent: #7d9bff; --accent-ink: #0d1530; --accent-soft: #222a42; --ok: #5cc98a; --ok-soft: #1a2e22;
  --warn: #e0a54a; --warn-soft: #33281a; --bad: #ef7b7b; --bad-soft: #3a1f1f; --code: #26262a;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.5); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.6 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.88em; }
.boot { padding: 40px; color: var(--muted); }
.hidden { display: none !important; }

/* buttons & inputs */
.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--panel);
  border-radius: 8px; padding: 6px 12px; cursor: pointer; white-space: nowrap; line-height: 1.4;
}
.btn:hover { border-color: var(--faint); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--bad); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { padding: 3px 9px; font-size: 13px; }
.btn:disabled { opacity: 0.5; cursor: default; }
.icon-btn { border: none; background: none; padding: 6px; border-radius: 8px; cursor: pointer; color: var(--muted); display: inline-flex; position: relative; }
.icon-btn:hover { background: var(--code); color: var(--ink); }
.icon-btn svg, .btn svg { width: 18px; height: 18px; }
input[type="text"], input[type="password"], input[type="search"], select, textarea {
  width: 100%; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 8px 10px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
.field { display: block; margin: 12px 0; }
.flabel { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
label.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.hint { font-size: 13px; color: var(--muted); }
.badge {
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--bad); color: #fff;
  font-size: 11px; line-height: 18px; text-align: center; font-weight: 600;
}
.icon-btn .badge { position: absolute; top: 0; right: -2px; }

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login form { width: 100%; max-width: 340px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px 24px; }
.login h1 { margin: 0 0 4px; font-size: 22px; }
.login .err { color: var(--bad); font-size: 14px; min-height: 20px; margin-top: 8px; }

/* layout */
.layout { display: grid; grid-template-columns: 290px 1fr; height: 100vh; height: 100dvh; }
.side { border-right: 1px solid var(--line); background: var(--panel-2); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; align-items: center; gap: 6px; padding: 14px 12px 10px 16px; }
.brand { font-weight: 700; font-size: 17px; flex: 1; letter-spacing: 0.01em; }
.conn { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); margin-right: 4px; }
.conn.on { background: var(--ok); }
.side-nav { padding: 0 8px; }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: 8px; cursor: pointer; color: var(--ink);
  text-decoration: none; border: none; background: none; width: 100%; text-align: left;
}
.nav-item:hover { background: var(--code); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); }
.nav-item svg { width: 17px; height: 17px; flex: none; }
.nav-item .grow { flex: 1; }
.side-section { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 4px; font-size: 12px; color: var(--muted); letter-spacing: 0.04em; }
.bot-list { flex: 1; overflow-y: auto; padding: 0 8px 16px; }
.dev-group { margin-top: 6px; }
.dev-label { font-size: 12px; color: var(--faint); padding: 6px 10px 2px; display: flex; align-items: center; gap: 6px; }
.bot-item { display: grid; grid-template-columns: 10px 1fr auto; gap: 2px 10px; align-items: center; padding: 7px 10px; border-radius: 8px; cursor: pointer; }
.bot-item:hover { background: var(--code); }
.bot-item.active { background: var(--accent-soft); }
.bot-item .name { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bot-item .snip { grid-column: 2 / 4; font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--faint); }
.dot.idle { background: var(--ok); }
.dot.busy { background: var(--accent); animation: pulse 1.4s ease-in-out infinite; }
.dot.waiting { background: var(--warn); }
.dot.starting { background: var(--accent); opacity: 0.5; }
.dot.offline { background: var(--faint); }
.dot.paused { background: var(--muted); }
@keyframes pulse { 50% { opacity: 0.35; } }
.main { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.topbar { display: none; align-items: center; gap: 6px; padding: 8px 10px; border-bottom: 1px solid var(--line); background: var(--panel-2); }
.topbar .title { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.scrim { display: none; }

/* page */
.page { flex: 1; overflow-y: auto; padding: 22px 28px 40px; }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head h2 { margin: 0; font-size: 20px; flex: 1; }
.empty { color: var(--muted); padding: 40px 0; text-align: center; }

/* inbox */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 10px; }
.inbox-item { cursor: pointer; }
.inbox-item.unread { border-left: 3px solid var(--accent); }
.inbox-item .row { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; flex-wrap: wrap; }
.inbox-item .who { font-weight: 600; }
.inbox-item .when { color: var(--faint); font-size: 12.5px; margin-left: auto; }
.inbox-item .body { max-height: 7.5em; overflow: hidden; position: relative; }
.inbox-item .body.fade::after { content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 2em; background: linear-gradient(transparent, var(--panel)); }
.tag { font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--code); color: var(--muted); white-space: nowrap; }
.tag.done { background: var(--ok-soft); color: var(--ok); }
.tag.blocked { background: var(--bad-soft); color: var(--bad); }
.tag.needs_decision { background: var(--warn-soft); color: var(--warn); }
.tag.progress { background: var(--accent-soft); color: var(--accent); }

/* chat */
.chat { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-head { padding: 12px 20px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.chat-head .title { flex: 1; min-width: 160px; }
.chat-head h2 { margin: 0; font-size: 18px; display: flex; align-items: center; gap: 8px; }
.chat-head .sub { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head .actions { display: flex; gap: 6px; flex-wrap: wrap; }
.state-chip { font-size: 12px; font-weight: 500; padding: 1px 9px; border-radius: 999px; background: var(--code); color: var(--muted); }
.state-chip.idle { background: var(--ok-soft); color: var(--ok); }
.state-chip.busy, .state-chip.starting { background: var(--accent-soft); color: var(--accent); }
.state-chip.waiting { background: var(--warn-soft); color: var(--warn); }
.banner { margin: 10px 20px 0; padding: 10px 14px; border-radius: 8px; background: var(--warn-soft); color: var(--ink); font-size: 14px; }
.banner code { background: var(--panel); padding: 2px 6px; border-radius: 4px; word-break: break-all; }
.msgs { flex: 1; overflow-y: auto; padding: 16px 20px 8px; }
.more { text-align: center; margin-bottom: 10px; }
.msg { margin: 0 0 14px; display: flex; flex-direction: column; }
.msg .meta { font-size: 12px; color: var(--faint); margin: 0 4px 3px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.msg.mine { align-items: flex-end; }
.msg.mine .bubble { background: var(--accent); color: var(--accent-ink); border-radius: 14px 14px 4px 14px; padding: 8px 13px; max-width: min(640px, 88%); white-space: pre-wrap; word-break: break-word; }
.msg.mine .status { font-size: 12px; color: var(--faint); margin: 3px 4px 0; }
.msg.mine .status.failed { color: var(--bad); }
.msg.report .bubble { background: var(--panel); border: 1px solid var(--line); border-radius: 14px 14px 14px 4px; padding: 10px 15px; max-width: min(820px, 100%); overflow-wrap: anywhere; }
.activity { margin: 0 0 14px; border: 1px dashed var(--line); border-radius: 10px; padding: 8px 12px; font-size: 13px; color: var(--muted); }
.activity summary { cursor: pointer; user-select: none; }
.activity ol { list-style: none; margin: 6px 0 0; padding: 0; max-height: 240px; overflow-y: auto; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; }
.activity li { white-space: pre-wrap; word-break: break-all; padding: 1px 0; }
.composer { border-top: 1px solid var(--line); padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); display: flex; gap: 8px; align-items: flex-end; background: var(--panel-2); }
.composer textarea { resize: none; max-height: 40vh; min-height: 42px; line-height: 1.5; }
.composer .btn { height: 42px; }

/* markdown */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md pre, .md blockquote, .md .md-table { margin: 0 0 0.7em; }
.md ul, .md ol { padding-left: 1.4em; }
.md h3, .md h4, .md h5, .md h6 { margin: 0.9em 0 0.4em; font-size: 15.5px; }
.md pre { background: var(--code); padding: 10px 12px; border-radius: 8px; overflow-x: auto; font-size: 13px; line-height: 1.5; }
.md code { background: var(--code); padding: 1px 5px; border-radius: 4px; }
.md pre code { background: none; padding: 0; }
.md blockquote { border-left: 3px solid var(--line); margin-left: 0; padding-left: 12px; color: var(--muted); }
.md .md-table { overflow-x: auto; }
.md table { border-collapse: collapse; font-size: 14px; }
.md th, .md td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; vertical-align: top; }
.md th { background: var(--code); }
.md hr { border: none; border-top: 1px solid var(--line); }

/* devices */
.dev-card .row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.dev-card h3 { margin: 0; font-size: 16px; flex: 1; display: flex; align-items: center; gap: 8px; }
.found { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 8px; }
.found-item { display: flex; align-items: center; gap: 10px; padding: 6px 0; flex-wrap: wrap; }
.found-item .grow { flex: 1; min-width: 180px; }
.found-item .p { font-size: 12.5px; color: var(--muted); word-break: break-all; }
.token-box { background: var(--code); border-radius: 8px; padding: 10px 12px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; word-break: break-all; margin: 8px 0; }

/* modal & drawer */
.overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.35); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal { background: var(--panel); border-radius: 14px; width: 100%; max-width: 520px; max-height: 90vh; overflow-y: auto; padding: 20px 22px; box-shadow: var(--shadow); }
.modal h3 { margin: 0 0 6px; font-size: 18px; }
.modal .foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.radio-row { display: flex; gap: 8px; flex-wrap: wrap; }
.radio-row label { flex: 1; min-width: 180px; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; cursor: pointer; }
.radio-row label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.radio-row input { margin-right: 6px; }
.radio-row small { display: block; color: var(--muted); font-size: 12.5px; }
.notif-panel { position: fixed; top: 0; right: 0; bottom: 0; width: min(380px, 100%); background: var(--panel); border-left: 1px solid var(--line); box-shadow: var(--shadow); z-index: 40; display: flex; flex-direction: column; }
.notif-head { display: flex; align-items: center; gap: 8px; padding: 14px 14px 10px 18px; border-bottom: 1px solid var(--line); }
.notif-head h3 { margin: 0; flex: 1; font-size: 17px; }
.notif-tools { display: flex; gap: 6px; padding: 8px 14px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.notif-list { flex: 1; overflow-y: auto; }
.notif { padding: 10px 18px; border-bottom: 1px solid var(--line); cursor: pointer; }
.notif:hover { background: var(--panel-2); }
.notif.unread { background: var(--accent-soft); }
.notif .t { font-weight: 600; font-size: 14px; }
.notif .b { font-size: 13px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.notif .w { font-size: 12px; color: var(--faint); }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 8px 16px; border-radius: 8px; z-index: 60; font-size: 14px; max-width: calc(100% - 32px); }

@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  .side { position: fixed; top: 0; bottom: 0; left: 0; width: min(300px, 86vw); z-index: 30; transform: translateX(-102%); transition: transform 0.2s ease; box-shadow: var(--shadow); }
  .layout.drawer .side { transform: none; }
  .layout.drawer .scrim { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, 0.3); z-index: 20; }
  .topbar { display: flex; }
  .page { padding: 16px 16px 32px; }
  .chat-head { padding: 10px 16px; }
  .msgs { padding: 12px 16px 8px; }
  .banner { margin: 10px 16px 0; }
  .composer { padding-left: 12px; padding-right: 12px; }
  .page-head .search { flex-basis: 100%; max-width: none; order: 3; }
}

/* search */
.search { flex: 1; min-width: 180px; max-width: 360px; }
.search input { padding: 6px 10px; }
.hit { font-size: 14px; color: var(--muted); }
mark { background: var(--warn-soft); color: var(--ink); border-radius: 3px; padding: 0 2px; }

/* images */
.atts { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.atts a { display: block; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); background: var(--code); }
.atts img { display: block; width: 132px; height: 132px; object-fit: cover; }
.atts.small img { width: 72px; height: 72px; }
.msg.mine .atts { justify-content: flex-end; }
.pending { display: flex; gap: 8px; flex-wrap: wrap; padding: 0 16px; }
.pending:not(:empty) { padding-top: 10px; border-top: 1px solid var(--line); background: var(--panel-2); }
.thumb { position: relative; width: 64px; height: 64px; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb.busy img { opacity: 0.4; }
.thumb.busy::after { content: "上传中"; position: absolute; inset: 0; display: grid; place-items: center; font-size: 11px; color: var(--ink); }
.thumb .x { position: absolute; top: 2px; right: 2px; border: none; border-radius: 50%; width: 20px; height: 20px; padding: 2px; background: rgba(0, 0, 0, 0.55); color: #fff; cursor: pointer; display: grid; place-items: center; }
.thumb .x svg { width: 12px; height: 12px; }
.composer .icon-btn { height: 42px; }

/* per-turn process */
.linkish { border: none; background: none; color: var(--accent); cursor: pointer; padding: 0; font-size: 12px; }
.linkish:hover { text-decoration: underline; }
.activity.steps { margin: 0 0 6px; max-width: min(820px, 100%); }
.activity.steps ol { margin: 0; max-height: 320px; }

/* digest */
.tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; margin-bottom: 14px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.stat .n { font-size: 22px; font-weight: 650; font-variant-numeric: tabular-nums; }
.stat .l { font-size: 13px; color: var(--muted); }
.stat.warn .n { color: var(--warn); }
.stat.bad .n { color: var(--bad); }
.digest-row { cursor: pointer; }
.digest-row.dim { cursor: default; opacity: 0.7; }
.digest-row .row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 4px; }
.digest-row .who { font-weight: 600; }
.digest-row .when { margin-left: auto; color: var(--muted); font-size: 13px; }

/* settings */
.card h3 { margin: 0 0 6px; font-size: 16px; }
.row-btns { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0; }

/* templates */
.tpl-list { padding: 0 8px; max-height: 32vh; overflow-y: auto; flex-shrink: 0; }
.tpl-item { display: flex; align-items: center; gap: 6px; padding: 2px 2px 2px 10px; border-radius: 8px; cursor: pointer; }
.tpl-item:hover { background: var(--code); }
.tpl-item .name { flex: 1; min-width: 0; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tpl-item .icon-btn { padding: 5px; color: var(--accent); }
.tpl-item .icon-btn svg { width: 15px; height: 15px; }
.tpl-empty { border: 1px dashed var(--line); background: none; border-radius: 8px; padding: 8px 10px; margin: 2px 0; width: 100%; text-align: left; font: inherit; font-size: 12.5px; color: var(--muted); cursor: pointer; }
.tpl-empty:hover { border-color: var(--accent); color: var(--accent); }
.tpl-card .row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.tpl-card h3 { flex: 1; min-width: 140px; margin: 0; }
.tpl-card .meta { margin: 6px 0 8px; word-break: break-all; }
.tpl-text { font-size: 14px; white-space: pre-wrap; display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; }
.tpl-env { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 8px; }
.tpl-env code, .modal code { background: var(--code); padding: 1px 6px; border-radius: 4px; font-size: 12.5px; }
.modal .foot .grow { flex: 1; }
details.env summary { cursor: pointer; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }
.hint.bad { color: var(--bad); }

/* directory picker */
.picker { margin-top: 6px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); }
.picker.loading { opacity: 0.6; pointer-events: none; }
.pk-msg { padding: 8px 10px; }
.pk-recent { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; padding: 8px 10px 0; }
.chip { border: 1px solid var(--line); background: var(--panel); color: var(--ink); border-radius: 999px; padding: 2px 10px; font: inherit; font-size: 12.5px; cursor: pointer; max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip:hover { border-color: var(--accent); color: var(--accent); }
.pk-bar { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.pk-path { flex: 1; min-width: 0; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; color: var(--muted); word-break: break-all; }
.pk-list { max-height: 220px; overflow-y: auto; padding: 4px; }
.pk-dir { display: flex; align-items: center; gap: 8px; width: 100%; border: none; background: none; color: var(--ink); text-align: left; font: inherit; font-size: 14px; padding: 6px 8px; border-radius: 6px; cursor: pointer; }
.pk-dir:hover { background: var(--code); }
.pk-dir svg { width: 16px; height: 16px; color: var(--faint); flex-shrink: 0; }
.pk-dir .nm { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pk-new { display: flex; gap: 6px; padding: 8px 10px; border-top: 1px solid var(--line); }
.pk-new input { padding: 5px 8px; }
.pk-new .btn { white-space: nowrap; }
