:root {
  --bg: #0d1117;
  --card: #161b22;
  --card2: #1c2129;
  --border: #21262d;
  --text: #e6edf3;
  --muted: #9aa4b2;
  --accent: #22c55e;
  --blue: #58a6ff;
  --amber: #d29922;
  --red: #f85149;
  --purple: #bc8cff;
  --radius: 16px;
  --topbar-h: 50px;
  --tabbar-h: 58px;
  --safe-top: env(safe-area-inset-top);
  --safe-bottom: env(safe-area-inset-bottom);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { touch-action: manipulation; -webkit-text-size-adjust: 100%; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  line-height: 1.55;
  overscroll-behavior-y: none;
  -webkit-font-smoothing: antialiased;
}

#app { max-width: 640px; margin: 0 auto; min-height: 100%; position: relative; }

/* 顶栏：与内容分层，安全区适配 */
#topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 20;
  height: calc(var(--topbar-h) + var(--safe-top));
  padding: var(--safe-top) 14px 0;
  display: flex; align-items: center; justify-content: space-between;
  background: rgba(13, 17, 23, 0.9); backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 8px rgba(0, 0, 0, .35);
  max-width: 640px; margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; font-size: 16.5px; letter-spacing: .01em; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: #6e7681; display: inline-block; transition: background .3s, box-shadow .3s; }
.dot.on { background: var(--accent); box-shadow: 0 0 10px var(--accent); }
.dot.err { background: var(--red); box-shadow: 0 0 10px var(--red); }
.dot.wait { background: var(--amber); box-shadow: 0 0 10px var(--amber); }
.icon-btn {
  background: var(--card2); color: var(--text); border: 1px solid var(--border);
  border-radius: 10px; width: 40px; height: 40px; font-size: 18px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}

/* 断开横幅：顶栏下方提示连接状态 */
.conn-banner {
  position: fixed; top: calc(var(--topbar-h) + var(--safe-top)); left: 0; right: 0; z-index: 19;
  max-width: 640px; margin: 0 auto;
  background: rgba(210, 153, 34, .14); color: var(--amber);
  border-bottom: 1px solid rgba(210, 153, 34, .35);
  font-size: 13px; text-align: center; padding: 7px 12px;
}
.conn-banner.hidden { display: none; }
.icon-btn:active { transform: scale(.94); background: #232a33; }

/* 主视图 */
#view { padding: calc(var(--topbar-h) + var(--safe-top) + 14px) 12px calc(var(--tabbar-h) + var(--safe-bottom) + 45px); }

/* 底部导航：图标 + 文字，触控热区加大 */
#tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  display: flex; max-width: 640px; margin: 0 auto;
  background: rgba(13, 17, 23, 0.95); backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
}
.tab-btn {
  position: relative; flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; min-height: var(--tabbar-h);
  background: none; border: none; color: var(--muted); font-size: 11.5px; cursor: pointer;
}
.tab-btn .ic { font-size: 19px; line-height: 1; }
.tab-btn.active { color: var(--accent); font-weight: 600; }
.tab-btn.active::before {
  content: ""; position: absolute; top: 0; left: 50%; transform: translateX(-50%);
  width: 34px; height: 3px; border-radius: 0 0 3px 3px; background: var(--accent);
}
.tab-btn:active { background: rgba(255, 255, 255, .05); }

/* 卡片 */
.card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; margin-bottom: 12px;
}
.card h3 { margin: 0 0 12px; font-size: 13.5px; color: var(--muted); font-weight: 700; letter-spacing: .02em; }
.section-title { font-size: 13px; color: var(--muted); margin: 18px 4px 10px; font-weight: 700; }

/* 状态页：标签-数值-进度条 紧凑关联 */
.stat-row { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 6px 0; font-size: 14px; }
.stat-row .k { color: var(--muted); flex-shrink: 0; }
.stat-row .v { text-align: right; word-break: break-all; }
.stat-block { padding: 6px 0; }
.bar { height: 7px; background: var(--card2); border-radius: 4px; overflow: hidden; margin: 3px 0 4px; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 4px; transition: width .4s; }
.bar.warn > i { background: var(--amber); }
.bar.danger > i { background: var(--red); }
.chip {
  display: inline-block; font-size: 12.5px; padding: 3px 10px; border-radius: 20px;
  background: var(--card2); color: var(--muted); border: 1px solid var(--border); margin: 2px 6px 2px 0;
}
.chip.run { color: var(--accent); border-color: rgba(34, 197, 94, .4); background: rgba(34, 197, 94, .1); }
.chip.done { color: var(--blue); }
.chip.err { color: var(--red); border-color: rgba(248, 81, 73, .4); background: rgba(248, 81, 73, .1); }
.mono { font-family: ui-monospace, SFMono-Regular, Consolas, "Courier New", monospace; font-size: 13px; }

/* 任务行 */
.task-item { padding: 10px 0; border-bottom: 1px solid var(--border); }
.task-item:last-child { border-bottom: none; }
.task-item .cmd { font-family: ui-monospace, Consolas, monospace; font-size: 13px; word-break: break-all; }

/* 会话列表 */
.thread-item { cursor: pointer; display: flex; align-items: center; gap: 10px; }
.thread-item:active { background: var(--card2); }
.thread-item .t-main { flex: 1; min-width: 0; }
.t-title { font-size: 15px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; line-height: 1.45; }
.t-meta { font-size: 12.5px; color: var(--muted); margin-top: 5px; line-height: 1.5; }
.chev { color: var(--muted); font-size: 22px; flex-shrink: 0; opacity: .7; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 20px; margin-left: 7px; vertical-align: 2px; }
.badge.run { color: var(--accent); background: rgba(34, 197, 94, .12); border: 1px solid rgba(34, 197, 94, .35); }
.badge.warn { color: var(--amber); background: rgba(210, 153, 34, .12); border: 1px solid rgba(210, 153, 34, .35); }

/* 会话详情：聊天对话（参考 DeepSeek/豆包） */
.back-btn { margin-bottom: 12px; }
.detail-head { font-size: 16.5px; font-weight: 700; margin-bottom: 4px; word-break: break-word; line-height: 1.4; }
.detail-meta { font-size: 12px; color: var(--muted); word-break: break-all; margin-bottom: 14px; line-height: 1.5; font-family: ui-monospace, Consolas, monospace; opacity: .85; }
.chat { display: flex; flex-direction: column; padding: 2px 0; }
.chat-msg { display: flex; margin-bottom: 12px; }
.chat-msg.same { margin-bottom: 5px; }
.chat-msg.user { justify-content: flex-end; }
.chat-msg.assistant { justify-content: flex-start; }
.ava {
  width: 26px; height: 26px; border-radius: 50%; flex-shrink: 0; margin: 4px 8px 0 0;
  background: #1d232b; border: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center; font-size: 13px;
}
.bubble { max-width: 84%; padding: 9px 13px; border-radius: 14px; font-size: 15px; line-height: 1.6; }
.chat-msg.user .bubble {
  background: #2e63e0; color: #fff;
  border-bottom-right-radius: 5px;
}
.chat-msg.assistant .bubble { background: #1d232b; border-bottom-left-radius: 5px; }
.chat-msg .msg-body { white-space: pre-wrap; word-break: break-word; }
.chat-msg .ts { font-size: 10.5px; opacity: .62; margin-top: 5px; text-align: right; }
.chat-msg.assistant .ts { text-align: left; }
.tool-chip {
  display: flex; align-items: center; justify-content: space-between; gap: 8px; align-self: stretch;
  background: rgba(148, 163, 184, .05); border: 1px solid rgba(148, 163, 184, .12); border-radius: 8px;
  padding: 5px 10px; font-size: 11.5px;
  color: var(--muted); margin: 0 0 4px; cursor: pointer; opacity: .85;
}
.tool-chip.same { margin-bottom: 3px; }
.tool-chip .tch-head { color: var(--muted); font-size: 11px; flex-shrink: 0; }
.tool-chip .tch-arrow { flex-shrink: 0; transition: transform .2s; opacity: .55; font-size: 10px; }
.tool-chip .tch-arrow { flex-shrink: 0; transition: transform .2s; opacity: .8; }
.tool-chip:active { background: #141a22; }
.tool-chip.open { border-color: rgba(188, 140, 255, .4); }
.tool-chip.open .tch-arrow { transform: rotate(180deg); }
.tool-detail {
  display: none; margin: 0 0 12px; align-self: stretch;
  background: rgba(13, 17, 23, .65); border: 1px solid var(--border); border-radius: 10px;
  padding: 9px 11px; font-family: ui-monospace, Consolas, monospace; font-size: 12px;
}
.tool-chip.open + .tool-detail { display: block; }
.tool-detail .tool-head { color: var(--purple); font-size: 11.5px; margin-bottom: 5px; }
.tg-list { display: flex; flex-direction: column; gap: 9px; }
.tg-item { border-top: 1px dashed rgba(148, 163, 184, .25); padding-top: 9px; }
.tg-item:first-child { border-top: none; padding-top: 0; }
.tg-head { color: var(--purple); font-size: 11.5px; margin-bottom: 4px; }
.tg-cmd { font-size: 12px; color: var(--text); margin-bottom: 4px; white-space: pre-wrap; word-break: break-all; }
.tg-out { font-size: 11.5px; color: var(--muted); white-space: pre-wrap; word-break: break-all; max-height: 150px; overflow: auto; }
.tool-detail .tool-head .ts { margin: 0; opacity: .6; }
.tool-detail .msg-body { font-size: 12.5px; line-height: 1.55; }
.event-chips { margin-bottom: 14px; }

/* 项目页 */
.proj-name { font-size: 15.5px; font-weight: 700; display: flex; align-items: center; gap: 8px; }
.proj-path { font-size: 12.5px; color: var(--muted); word-break: break-all; margin: 4px 0 10px; font-family: ui-monospace, Consolas, monospace; line-height: 1.5; }
.proj-err { color: var(--red); font-size: 13px; word-break: break-all; }
.cmd-btns { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.cmd-btn {
  background: var(--card2); color: var(--text); border: 1px solid var(--border);
  border-radius: 10px; padding: 9px 14px; font-size: 13.5px; cursor: pointer; min-height: 40px;
}
.cmd-btn:active { transform: scale(.96); background: #232a33; }
.cmd-btn.primary { background: rgba(34, 197, 94, .12); border-color: rgba(34, 197, 94, .4); color: var(--accent); }

/* 运行面板 */
.run-panel {
  position: fixed; left: 0; right: 0; bottom: calc(var(--tabbar-h) + var(--safe-bottom));
  z-index: 30; max-width: 640px; margin: 0 auto;
  background: #0a0d12; border-top: 1px solid var(--border); border-radius: 14px 14px 0 0;
  display: flex; flex-direction: column; max-height: 52vh;
}
.run-panel.hidden { display: none; }
.rp-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--border); font-size: 13px; }
.rp-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rp-status { font-size: 12px; color: var(--amber); }
.rp-status.done { color: var(--accent); }
.rp-status.err { color: var(--red); }

/* 一键检查（P0-3） */
.check-title { font-size: 12.5px; color: var(--muted); margin-top: 14px; font-weight: 600; }
.check-list { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.check-row { display: flex; align-items: center; gap: 10px; }
.check-row .cmd-btn { flex: 1; text-align: left; }
.cmd-btn.check { border-color: rgba(88, 166, 255, .35); color: var(--blue); background: rgba(88, 166, 255, .08); }
.check-state { flex-shrink: 0; font-size: 12.5px; color: var(--muted); min-width: 66px; text-align: right; white-space: nowrap; }
.check-state.pass { color: var(--accent); }
.check-state.fail { color: var(--red); font-weight: 600; }
.check-state.run { color: var(--amber); }
.check-state.warn { color: var(--amber); }
.rp-banner {
  margin: 10px 12px 0; padding: 10px 14px; border-radius: 12px;
  font-size: 15px; font-weight: 700; display: flex; align-items: center; gap: 10px;
}
.rp-banner.pass { background: rgba(34, 197, 94, .12); color: var(--accent); border: 1px solid rgba(34, 197, 94, .35); }
.rp-banner.fail { background: rgba(248, 81, 73, .12); color: var(--red); border: 1px solid rgba(248, 81, 73, .35); }
.rp-banner.warn { background: rgba(210, 153, 34, .12); color: var(--amber); border: 1px solid rgba(210, 153, 34, .35); }
.rp-banner-sub { font-size: 12px; font-weight: 500; opacity: .8; }
.rp-output {
  margin: 0; padding: 10px 12px; overflow-y: auto; flex: 1; -webkit-overflow-scrolling: touch;
  font-family: ui-monospace, Consolas, monospace; font-size: 13px; line-height: 1.5;
  white-space: pre-wrap; word-break: break-all; color: #c9d1d9;
}

/* 表单 */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; font-weight: 600; }
.field input, .field select, .field textarea {
  width: 100%; background: var(--card2); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px; padding: 11px 13px;
  font-size: 16px; /* ≥16px 防止 iOS 聚焦自动缩放 */
}
.field textarea { min-height: 96px; resize: vertical; }
.btn {
  background: var(--accent); color: #04140a; border: none; border-radius: 12px;
  padding: 13px 16px; font-size: 15px; font-weight: 700; cursor: pointer; width: 100%;
}
.btn:active { transform: scale(.98); }
.btn.ghost { background: var(--card2); color: var(--text); border: 1px solid var(--border); font-weight: 600; }
.btn-row { display: flex; gap: 8px; }
.btn-row .btn { flex: 1; }

/* Toast */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 76px);
  background: #2d333b; color: var(--text); padding: 10px 18px; border-radius: 22px;
  font-size: 13.5px; z-index: 50; box-shadow: 0 4px 18px rgba(0,0,0,.5);
  max-width: 86vw; text-align: center;
  animation: toast-in .22s ease-out;
}
.toast.hidden { display: none; }
.toast.err { background: rgba(248, 81, 73, .92); }
@keyframes toast-in { from { opacity: 0; transform: translateX(-50%) translateY(8px); } to { opacity: 1; transform: translateX(-50%) translateY(0); } }

.empty { color: var(--muted); text-align: center; padding: 32px 12px; font-size: 13.5px; }
.loading { color: var(--muted); text-align: center; padding: 24px 12px; font-size: 13.5px; }
.kv { display: flex; justify-content: space-between; gap: 10px; padding: 5px 0; font-size: 13.5px; }
.kv .k { color: var(--muted); flex-shrink: 0; }
.kv .v { text-align: right; word-break: break-all; }

/* 附件列表 */
.file-row { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.file-row .fn { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13.5px; }
.file-row .fs { color: var(--muted); font-size: 12.5px; flex-shrink: 0; }

/* 可访问性 */
:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }

/* 文件浏览器（项目内） */
.file-panel {
  position: fixed; inset: 0; z-index: 60; max-width: 640px; margin: 0 auto;
  background: var(--bg); display: flex; flex-direction: column;
}
.file-panel.hidden { display: none; }
.fb-head {
  display: flex; align-items: center; gap: 10px;
  padding: calc(var(--safe-top) + 8px) 12px 10px;
  background: rgba(13, 17, 23, .92); border-bottom: 1px solid var(--border);
}
.fb-title { flex: 1; font-weight: 700; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fb-body { flex: 1; overflow-y: auto; padding: 10px 14px calc(var(--safe-bottom) + 12px); display: flex; flex-direction: column; }
.fb-path { font-size: 12px; color: var(--muted); margin-bottom: 10px; word-break: break-all; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.fb-dirty { font-size: 11px; color: var(--amber); border: 1px solid rgba(210, 153, 34, .4); border-radius: 20px; padding: 1px 8px; flex-shrink: 0; }
.fb-dirty.hidden { display: none; }
.fb-row { display: flex; align-items: center; gap: 10px; padding: 13px 4px; border-bottom: 1px solid rgba(255,255,255,.06); cursor: pointer; }
.fb-row:active { background: rgba(255,255,255,.05); }
.fb-row .fb-ic { font-size: 16px; flex-shrink: 0; }
.fb-row .fb-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.fb-row .fb-size { font-size: 11.5px; color: var(--muted); flex-shrink: 0; }
.fb-row .fb-chev { color: var(--muted); font-size: 20px; opacity: .7; flex-shrink: 0; }
.fb-editor {
  flex: 1; min-height: 0; width: 100%; box-sizing: border-box;
  background: #0b0e13; color: #d7e0ea; border: 1px solid var(--border); border-radius: 10px;
  padding: 12px; font-family: ui-monospace, Consolas, monospace; font-size: 14px; line-height: 1.6;
  resize: none; outline: none;
}
.fb-editor:focus { border-color: rgba(188, 140, 255, .5); }
.fb-actions { padding: 10px 0 4px; }


/* ---------- 会话详情：继续对话输入区 ---------- */
.chat-input-wrap {
  position: sticky;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom));
  background: var(--bg);
  padding: 10px 0 10px;
  display: flex;
  gap: 8px;
  align-items: flex-end;
  z-index: 5;
  box-shadow: 0 -4px 12px rgba(0, 0, 0, .25);
}
.chat-input-wrap textarea {
  flex: 1;
  min-height: 44px;
  max-height: 130px;
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px 12px;
  font-size: 16px;
  line-height: 1.45;
  resize: none;
  font-family: inherit;
}
.chat-input-wrap textarea:focus { outline: none; border-color: rgba(34, 197, 94, .5); }
.chat-input-wrap textarea:disabled { opacity: .55; }
.chat-input-wrap .cmd-btn { padding: 11px 16px; font-size: 14px; flex-shrink: 0; }
.chat-input-wrap .cmd-btn:disabled { opacity: .55; }
.run-hint { font-size: 12px; color: #f0b429; margin: 4px 2px 0; }

/* 备案信息页脚 */
.site-footer {
  position: fixed;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom));
  left: 0; right: 0; z-index: 15;
  text-align: center;
  font-size: 11px;
  line-height: 1.4;
  padding: 2px 12px 8px;
  color: #57606a;
  pointer-events: none;
}
.site-footer a { color: #57606a; text-decoration: none; pointer-events: auto; }
