:root {
  --bg: #f6f6f4;
  --panel: #ffffff;
  --side: #efefec;
  --line: #e2e2dd;
  --text: #1c1c1a;
  --muted: #5f5f5a;
  --accent: #c96442;
  --accent-ink: #ffffff;
  --user: #ece9e2;
  --code: #f1f0ec;
  --term-bg: #15161a;
  --term-fg: #d8d9de;
  --ok: #1f7a4d;
  --warn: #a35c00;
  --warn-bg: #fff4e0;
  --err: #b42318;
  --run: #2563eb;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Noto Sans Mono CJK SC", monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #18181b;
    --panel: #1f1f23;
    --side: #141417;
    --line: #2e2e33;
    --text: #ececec;
    --muted: #a1a1a8;
    --accent: #d97757;
    --user: #2a2a30;
    --code: #26262b;
    --term-bg: #0e0f12;
    --ok: #4ade80;
    --warn: #fbbf24;
    --warn-bg: #3a2c10;
    --err: #f87171;
    --run: #60a5fa;
  }
}
:root[data-theme="dark"] {
    --bg: #18181b;
    --panel: #1f1f23;
    --side: #141417;
    --line: #2e2e33;
    --text: #ececec;
    --muted: #a1a1a8;
    --accent: #d97757;
    --user: #2a2a30;
    --code: #26262b;
    --term-bg: #0e0f12;
    --ok: #4ade80;
    --warn: #fbbf24;
    --warn-bg: #3a2c10;
    --err: #f87171;
    --run: #60a5fa;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: var(--fs, 15px)/1.65 -apple-system, "PingFang SC", "Noto Sans SC", "Microsoft YaHei", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.small { font-size: 12.5px; }
.muted { color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.row { display: flex; gap: 8px; align-items: center; }

.app { display: flex; height: 100dvh; overflow: hidden; }

/* 左栏 */
.side {
  width: 300px; flex: 0 0 300px; background: var(--side); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px 8px; }
.brand { display: flex; gap: 10px; align-items: center; line-height: 1.3; }
.brand div { display: flex; flex-direction: column; }
.quota { padding: 4px 14px 8px; display: flex; flex-direction: column; gap: 6px; }
.q-row { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.q-row div:first-child { display: flex; justify-content: space-between; }
.q-bar { height: 5px; border-radius: 3px; background: var(--line); overflow: hidden; }
.q-bar i { display: block; height: 100%; border-radius: 3px; }
.new { padding: 6px 14px 10px; display: flex; flex-direction: column; gap: 4px; }
.new select#effort { flex: 0 0 auto; }
.new select { flex: 1; min-width: 0; min-height: 40px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); padding: 0 8px; }
.filter { padding: 0 14px 8px; }
.filter input { width: 100%; min-height: 38px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); padding: 0 10px; }
.list { flex: 1; overflow-y: auto; padding: 0 8px 12px; }
.group { font-size: 12px; color: var(--muted); padding: 10px 8px 4px; }
.item {
  display: flex; gap: 10px; align-items: flex-start; width: 100%; text-align: left; border: 0; background: transparent;
  padding: 9px 8px; border-radius: 8px; cursor: pointer; min-height: 44px;
}
.item:hover { background: color-mix(in srgb, var(--line) 60%, transparent); }
.item.on { background: var(--panel); box-shadow: 0 0 0 1px var(--line); }
.item .t { display: block; font-size: 14px; line-height: 1.35; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.item .m { display: block; font-size: 12px; color: var(--muted); margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { flex: 0 0 9px; width: 9px; height: 9px; border-radius: 50%; margin-top: 6px; background: transparent; border: 1.5px solid var(--line); }
.dot.idle { background: var(--muted); border-color: var(--muted); opacity: .55; }
.dot.running { background: var(--run); border-color: var(--run); animation: pulse 1.4s infinite; }
.dot.waiting { background: var(--warn); border-color: var(--warn); }
@keyframes pulse { 50% { opacity: .35; } }
.side-foot { padding: 10px 14px; border-top: 1px solid var(--line); }

/* 主区 */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.top { display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel); flex-wrap: wrap; }
.title { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.3; }
.title strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { font-size: 12px; padding: 2px 10px; border-radius: 999px; border: 1px solid var(--line); white-space: nowrap; }
.badge.running { color: var(--run); border-color: currentColor; }
.badge.waiting { color: var(--warn); border-color: currentColor; background: var(--warn-bg); }
.badge.idle { color: var(--muted); }
.badge.stopped { color: var(--muted); border-style: dashed; }
.btn { min-height: 38px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); cursor: pointer; white-space: nowrap; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--err); }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { width: 40px; height: 40px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); cursor: pointer; font-size: 18px; }
.only-mobile { display: none; }

.empty { margin: auto; max-width: 520px; padding: 24px; text-align: center; }
.empty h2 { font-size: 20px; margin: 0 0 8px; }

.chat { flex: 1; overflow-y: auto; padding: 20px 16px 12px; min-height: 80px; }
.chat > * { max-width: 860px; margin-left: auto; margin-right: auto; }
.more { display: block; margin: 0 auto 16px; }
.msg { margin-bottom: 14px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble { background: var(--user); padding: 9px 14px; border-radius: 14px 14px 4px 14px; max-width: 85%; white-space: pre-wrap; word-break: break-word; }
.msg.assistant { word-break: break-word; }
.msg.assistant :first-child { margin-top: 0; }
.msg.assistant :last-child { margin-bottom: 0; }
.msg.assistant pre { background: var(--code); padding: 10px 12px; border-radius: 8px; overflow-x: auto; font: 13px/1.5 var(--mono); }
.msg.assistant code { font-family: var(--mono); font-size: .9em; background: var(--code); padding: 1px 4px; border-radius: 4px; }
.msg.assistant pre code { background: none; padding: 0; }
.msg.assistant table { border-collapse: collapse; display: block; overflow-x: auto; margin: 8px 0; }
.msg.assistant th, .msg.assistant td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; vertical-align: top; }
.msg.assistant th { background: var(--code); }
.note { text-align: center; font-size: 12px; color: var(--muted); margin: 12px auto; }
.time { font-size: 11.5px; color: var(--muted); margin: 18px auto 6px; text-align: center; }

.tool { border: 1px solid var(--line); border-radius: 10px; background: var(--panel); margin-bottom: 10px; overflow: hidden; }
.tool summary { list-style: none; display: flex; gap: 8px; align-items: baseline; padding: 7px 12px; cursor: pointer; min-height: 36px; }
.tool summary::-webkit-details-marker { display: none; }
.tool .tag { flex: 0 0 auto; font-size: 11.5px; font-weight: 600; color: var(--accent-ink); background: #3f3f46; padding: 1px 7px; border-radius: 5px; }
.tool .sum { flex: 1; min-width: 0; font: 12.5px/1.5 var(--mono); color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool[open] .sum { white-space: pre-wrap; word-break: break-all; }
.tool .st { flex: 0 0 auto; font-size: 11.5px; }
.tool .st.ok { color: var(--ok); }
.tool .st.err { color: var(--err); }
.tool .st.wait { color: var(--muted); }
.tool .out { margin: 0; padding: 8px 12px; border-top: 1px solid var(--line); background: var(--code); font: 12px/1.5 var(--mono); white-space: pre-wrap; word-break: break-all; max-height: 360px; overflow: auto; }
.diff { margin: 0; font: 12px/1.5 var(--mono); white-space: pre-wrap; word-break: break-all; border-top: 1px solid var(--line); }
.diff .del { background: color-mix(in srgb, var(--err) 14%, transparent); padding: 6px 12px; }
.diff .add { background: color-mix(in srgb, var(--ok) 14%, transparent); padding: 6px 12px; }

.splitter { height: 8px; cursor: row-resize; background: var(--line); flex: 0 0 8px; position: relative; touch-action: none; }
.splitter::after { content: ""; position: absolute; left: 50%; top: 2px; width: 40px; height: 4px; margin-left: -20px; border-radius: 2px; background: var(--muted); opacity: .5; }
.term-wrap { height: 34vh; flex: 0 0 auto; background: var(--term-bg); overflow: auto; }
.term { margin: 0; padding: 10px 12px; color: var(--term-fg); font: 12.5px/1.35 var(--mono); white-space: pre; min-width: max-content; }

.keys { display: flex; gap: 6px; align-items: center; padding: 8px 16px 0; overflow-x: auto; flex-wrap: nowrap; }
.keys > span { flex: 0 0 auto; white-space: nowrap; }
.key { min-height: 36px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); cursor: pointer; white-space: nowrap; }
.keys.waiting .key[data-key="1"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.keys.waiting { background: var(--warn-bg); padding-bottom: 8px; }

.composer { display: flex; gap: 8px; align-items: flex-end; padding: 10px 16px 14px; padding-bottom: max(14px, env(safe-area-inset-bottom)); }
.composer textarea { flex: 1; min-height: 44px; max-height: 40vh; resize: none; border: 1px solid var(--line); border-radius: 12px; background: var(--panel); padding: 10px 12px; line-height: 1.5; }
.composer textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 50%, transparent); }
.send { min-height: 44px; }

.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: #27272a; color: #fff; padding: 8px 16px; border-radius: 10px; font-size: 14px; z-index: 50; max-width: 90vw; }

@media (max-width: 760px) {
  .only-mobile { display: inline-flex; align-items: center; justify-content: center; }
  .side { position: fixed; inset: 0 auto 0 0; width: 86vw; max-width: 340px; z-index: 40; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 40px rgba(0,0,0,.25); }
  .app.side-open .side { transform: none; }
  .top { padding: 8px 10px; }
  .chat { padding: 14px 12px 8px; }
  .term-wrap { height: 40vh; }
  .composer { padding: 8px 10px 10px; }
  .keys { padding: 6px 10px 0; }
  .keys > span { display: none; }
  #toggleTerm { order: 5; }
}

/* 设置面板 */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 60; display: flex; align-items: center; justify-content: center; padding: 16px; }
.dialog { background: var(--panel); color: var(--text); border-radius: 14px; width: 100%; max-width: 420px; max-height: 90dvh; overflow: auto; padding: 18px 18px 14px; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.dialog h3 { margin: 0 0 12px; font-size: 18px; }
.setting { display: flex; flex-direction: column; gap: 6px; padding: 10px 0; border-top: 1px solid var(--line); }
.setting > span { font-weight: 600; font-size: 14px; }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button { min-height: 36px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); cursor: pointer; }
.seg button.on { border-color: var(--accent); color: var(--accent); font-weight: 600; box-shadow: inset 0 0 0 1px var(--accent); }
.swatch { width: 34px; height: 34px; border-radius: 50%; border: 2px solid transparent; cursor: pointer; padding: 0; }
.swatch.on { border-color: var(--text); }
.dialog .foot { display: flex; justify-content: flex-end; padding-top: 12px; }

/* 聊天区小部件 */
.msg { position: relative; }
.copy { position: absolute; top: -6px; right: 0; font-size: 12px; padding: 2px 8px; border-radius: 6px; border: 1px solid var(--line); background: var(--panel); color: var(--muted); cursor: pointer; opacity: 0; transition: opacity .15s; }
.msg:hover .copy, .msg:focus-within .copy { opacity: 1; }
.msg.user .copy { right: auto; left: 0; }
@media (hover: none) { .copy { opacity: .7; position: static; display: inline-block; margin-top: 4px; } .msg.user { flex-direction: column; align-items: flex-end; } }
.turn-foot { text-align: center; font-size: 12px; color: var(--muted); margin: 4px auto 16px; }
.err { text-align: center; margin: 24px auto; color: var(--err); }
.err button { margin-top: 10px; }

/* 输入框上方的胶囊 */
.chips { display: flex; gap: 6px; padding: 8px 16px 0; overflow-x: auto; flex-wrap: nowrap; }
.chip { flex: 0 0 auto; min-height: 30px; padding: 0 10px; border-radius: 999px; border: 1px solid var(--line); background: var(--panel); font-size: 12.5px; color: var(--muted); cursor: pointer; white-space: nowrap; }
.chip strong { color: var(--text); font-weight: 600; }
.chip:disabled { opacity: .5; cursor: default; }
.menu { position: fixed; z-index: 70; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 30px rgba(0,0,0,.18); padding: 6px; display: flex; flex-direction: column; min-width: 160px; }
.menu button { text-align: left; min-height: 38px; padding: 0 12px; border: 0; background: transparent; border-radius: 6px; cursor: pointer; }
.menu button:hover { background: var(--code); }
.menu .hint { font-size: 12px; color: var(--muted); padding: 4px 12px 6px; }
.send.stop { background: var(--err); border-color: var(--err); }
@media (max-width: 760px) { .chips { padding: 6px 10px 0; } }

/* 表格：列别挤成一个字一行；太宽就在框里横向滚动 */
.msg.assistant th, .msg.assistant td { min-width: 5.5em; word-break: normal; overflow-wrap: anywhere; }
.msg.assistant th:first-child, .msg.assistant td:first-child { min-width: 6.5em; }
.msg.assistant table { max-width: 100%; -webkit-overflow-scrolling: touch; }

/* 登录页 */
.login-wrap { min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: 100%; max-width: 400px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 22px 20px; display: flex; flex-direction: column; gap: 12px; }
.login-card form { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.field input { min-height: 46px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text); padding: 0 12px; font-size: 17px; }
.field input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 50%, transparent); }
.btn.wide { width: 100%; min-height: 46px; font-size: 16px; }
.center { text-align: center; }
.login-msg { text-align: center; color: var(--err); font-size: 14px; }
.login-msg.ok { color: var(--ok); }
.steps { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 14px; font-size: 14px; }
.steps .field { margin-top: 8px; }
.qr { width: 200px; height: 200px; margin: 10px auto 6px; background: #fff; padding: 6px; border-radius: 8px; }
.qr svg { width: 100%; height: 100%; display: block; }
.secret { display: block; margin-top: 6px; font: 14px/1.5 var(--mono); word-break: break-all; user-select: all; }
.devices { font-size: 13px; color: var(--muted); display: flex; flex-direction: column; gap: 4px; }

/* 分叉提醒 */
.fork-bar { background: color-mix(in srgb, var(--err) 14%, var(--panel)); color: var(--err); border-bottom: 1px solid color-mix(in srgb, var(--err) 40%, transparent); padding: 8px 16px; font-size: 14px; line-height: 1.5; }

/* iPhone/iPad：输入框字号小于 16px 时 Safari 一聚焦就放大页面，触屏设备上统一不小于 16px */
@supports (-webkit-touch-callout: none) {
  textarea, input, select { font-size: max(16px, var(--fs, 15px)) !important; }
}

/* 未读 */
.unread-badge { display: inline-block; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: #e5484d; color: #fff; font-size: 11px; font-weight: 600; line-height: 18px; text-align: center; vertical-align: 2px; }
.unread-badge[hidden] { display: none; }
.has-badge { position: relative; }
.has-badge .unread-badge { position: absolute; top: -4px; right: -6px; vertical-align: 0; }
.item.unread .t { font-weight: 600; }
.new-tag { color: #e5484d; font-weight: 600; }
