:root {
  --bg: #0f1115;
  --bg-soft: #161a21;
  --bg-card: #1b2028;
  --line: #262c36;
  --text: #e8ebf0;
  --text-dim: #98a2b3;
  --text-faint: #6b7480;
  --user: #2b5cff;
  --user-soft: rgba(43, 92, 255, .14);
  --ok: #38d39f;
  --warn: #f0b429;
  --err: #ff6b6b;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --radius: 14px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6fa;
    --bg-soft: #ffffff;
    --bg-card: #ffffff;
    --line: #e3e8ef;
    --text: #131720;
    --text-dim: #5a6473;
    --text-faint: #8b95a5;
    --user-soft: rgba(43, 92, 255, .09);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
        "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  overscroll-behavior-y: none;
}

#app { height: 100%; position: relative; }

.view {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
}
.view.hidden { display: none; }

/* ---------- 顶栏 ---------- */

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 14px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
  z-index: 5;
}

.topbar h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: .01em;
}

.icon-btn {
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg-soft);
  color: var(--text);
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
}
.icon-btn:active { transform: scale(.95); }

.chat-topbar { padding-right: 12px; }

.chat-title-wrap { flex: 1 1 auto; min-width: 0; }

.chat-title {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-sub {
  font-size: 11.5px;
  color: var(--text-faint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}

.live-dot {
  width: 9px;
  height: 9px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: var(--text-faint);
  transition: background .3s;
}
.live-dot.on { background: var(--ok); box-shadow: 0 0 0 3px rgba(56, 211, 159, .18); }
.live-dot.busy { background: var(--warn); animation: pulse 1.1s ease-in-out infinite; }
.live-dot.off { background: var(--err); }

@keyframes pulse { 50% { opacity: .35; } }

/* ---------- 会话列表 ---------- */

.list-body {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 10px 12px calc(env(safe-area-inset-bottom, 0px) + 24px);
}

.session-item {
  display: block;
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: inherit;
  padding: 12px 13px;
  margin-bottom: 9px;
  cursor: pointer;
  font: inherit;
}
.session-item:active { background: var(--bg-soft); }

.session-item .t {
  font-size: 14.5px;
  font-weight: 560;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.session-item .m {
  margin-top: 6px;
  font-size: 11.5px;
  color: var(--text-faint);
  display: flex;
  gap: 8px;
  align-items: center;
}

.badge {
  font-size: 10.5px;
  padding: 1px 6px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--text-dim);
}

.badge.live { color: var(--ok); border-color: rgba(56, 211, 159, .4); }

.empty {
  text-align: center;
  color: var(--text-faint);
  font-size: 13px;
  padding: 48px 16px;
}

/* ---------- 对话滚动区 ---------- */

.scroll {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 8px 12px 16px;
  scroll-behavior: auto;   /* 追加历史时靠手动定位，不要平滑滚动 */
}

.load-earlier {
  display: flex;
  justify-content: center;
  padding: 6px 0 12px;
}

.ghost-btn {
  border: 1px solid var(--line);
  background: var(--bg-soft);
  color: var(--text-dim);
  border-radius: 999px;
  padding: 6px 15px;
  font-size: 12.5px;
  cursor: pointer;
}
.ghost-btn:disabled { opacity: .55; }

.messages { display: flex; flex-direction: column; gap: 10px; }

/* ---------- 单条消息 ---------- */

.msg { display: flex; flex-direction: column; max-width: 100%; }

.msg.user { align-items: flex-end; }
.msg.assistant { align-items: flex-start; }

.msg.user .bubble {
  background: var(--user-soft);
  border: 1px solid color-mix(in srgb, var(--user) 34%, transparent);
  border-radius: 14px 14px 4px 14px;
  padding: 9px 12px;
  max-width: 88%;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg.assistant .body {
  width: 100%;
  padding: 2px 2px 0;
}

.msg-meta {
  font-size: 10.5px;
  color: var(--text-faint);
  margin: 3px 2px 0;
}

/* 正文里的富文本 */
.md p { margin: 0 0 .62em; }
.md p:last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3, .md h4 {
  margin: .9em 0 .4em;
  line-height: 1.32;
  font-weight: 640;
}
.md h1 { font-size: 17.5px; }
.md h2 { font-size: 16.5px; }
.md h3 { font-size: 15.5px; }
.md h4 { font-size: 15px; }
.md ul, .md ol { margin: .3em 0 .7em; padding-left: 1.35em; }
.md li { margin: .18em 0; }
.md code {
  font-family: var(--mono);
  font-size: .88em;
  background: color-mix(in srgb, var(--line) 55%, transparent);
  border-radius: 5px;
  padding: .1em .38em;
  word-break: break-word;
}
.md pre {
  margin: .55em 0;
  padding: 10px 11px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.md pre code {
  background: none;
  padding: 0;
  font-size: 12.5px;
  line-height: 1.5;
  white-space: pre;
}
.md blockquote {
  margin: .5em 0;
  padding: .1em 0 .1em .8em;
  border-left: 2px solid var(--line);
  color: var(--text-dim);
}
.md a { color: var(--user); text-decoration: none; word-break: break-all; }
.md strong { font-weight: 640; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1em 0; }
.md table {
  border-collapse: collapse;
  font-size: 12.5px;
  display: block;
  overflow-x: auto;
  margin: .5em 0;
}
.md th, .md td { border: 1px solid var(--line); padding: 4px 8px; text-align: left; }
.md img { max-width: 100%; height: auto; border-radius: 8px; }

/* 推理 */
.reasoning {
  margin: 4px 0 8px;
  border-left: 2px solid var(--line);
  padding-left: 9px;
}
.reasoning > summary {
  cursor: pointer;
  font-size: 11.5px;
  color: var(--text-faint);
  list-style: none;
  user-select: none;
}
.reasoning > summary::-webkit-details-marker { display: none; }
.reasoning > summary::before { content: "▸ "; }
.reasoning[open] > summary::before { content: "▾ "; }
.reasoning .rtext {
  margin-top: 5px;
  font-size: 12.5px;
  color: var(--text-dim);
  white-space: pre-wrap;
  word-break: break-word;
}

/* 工具调用 */
.tool {
  margin: 5px 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg-soft);
  overflow: hidden;
}
.tool-head {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 7px 10px;
  font-size: 12.5px;
  cursor: pointer;
}
.tool-icon {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-faint);
  flex: 0 0 auto;
  width: 13px;
  text-align: center;
}
.tool-name { font-weight: 600; flex: 0 0 auto; }
.tool-brief {
  color: var(--text-dim);
  font-family: var(--mono);
  font-size: 11.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1 1 auto;
  min-width: 0;
}
.tool-status {
  flex: 0 0 auto;
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--text-faint);
}
.tool-status.completed { color: var(--ok); border-color: rgba(56, 211, 159, .38); }
.tool-status.error, .tool-status.failed { color: var(--err); border-color: rgba(255, 107, 107, .38); }
.tool-status.running { color: var(--warn); border-color: rgba(240, 180, 41, .38); }

.tool-output {
  margin: 0;
  padding: 0 10px 9px;
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.45;
  color: var(--text-dim);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 200px;
  overflow-y: auto;
  border-top: 1px solid var(--line);
  padding-top: 8px;
}
.tool.collapsed .tool-output { display: none; }

/* 图片 */
.img-wrap { margin: 7px 0; }
.img-wrap img {
  display: block;
  max-width: 100%;
  max-height: 60vh;
  width: auto;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg-soft);
}
.img-cap {
  font-size: 10.5px;
  color: var(--text-faint);
  margin-top: 4px;
}
.img-missing {
  font-size: 12px;
  color: var(--text-faint);
  border: 1px dashed var(--line);
  border-radius: 9px;
  padding: 9px 11px;
}

/* 尾部提示 */
.tail-note {
  text-align: center;
  font-size: 11.5px;
  color: var(--text-faint);
  padding: 10px 0 2px;
  min-height: 4px;
}

.hint {
  flex: 0 0 auto;
  text-align: center;
  font-size: 11px;
  color: var(--text-faint);
  padding: 6px 10px calc(env(safe-area-inset-bottom, 0px) + 7px);
  border-top: 1px solid var(--line);
  background: var(--bg);
}

.spinner {
  display: inline-block;
  width: 11px;
  height: 11px;
  border: 1.6px solid var(--line);
  border-top-color: var(--text-dim);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  vertical-align: -1px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.error-box {
  margin: 10px 0;
  padding: 10px 12px;
  border: 1px solid rgba(255, 107, 107, .4);
  border-radius: 10px;
  background: rgba(255, 107, 107, .08);
  color: var(--err);
  font-size: 13px;
}
