:root {
  --bg: #f5f6f8; --card: #fff; --fg: #1d2330; --muted: #6b7280; --line: #e3e6eb; --soft: #eef1f5;
  --accent: #1f6feb; --accent-soft: #e7f0ff; --ok: #1a7f37; --warn: #b26a00; --err: #c62828;
  --side: 260px;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #111418; --card: #1a1f26; --fg: #e6e8eb; --muted: #9aa3ad; --line: #2b323b; --soft: #222831;
    --accent: #4c8dff; --accent-soft: #1c2a42; }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--fg); font: 15px/1.55 system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  display: flex; flex-direction: column; overflow: hidden; }
h1 { font-size: 17px; margin: 0 auto 0 0; }
.logo { width: 26px; height: 26px; flex: none; }
h2 { font-size: 16px; margin: 0 0 10px; }
input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea { background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 7px 9px; min-width: 0; }
textarea { resize: none; }
button { background: var(--accent); color: #fff; border: 0; border-radius: 6px; padding: 7px 14px; cursor: pointer; }
button:disabled { opacity: .5; cursor: default; }
button.ghost { background: transparent; color: var(--fg); border: 1px solid var(--line); }
button.link { background: none; color: var(--accent); padding: 0 4px; font-size: 13px; }
button.icon { padding: 4px 9px; }
button.icon svg { display: block; }
#btnSettings { padding: 7px; }
button.stop { background: var(--err); }
button.wait, button.wait:disabled { background: var(--muted); opacity: 1; font-size: 13px; }
.muted { color: var(--muted); font-size: 13px; }
.warn { color: var(--warn); }
.err { color: var(--err); }
.spacer { flex: 1; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; margin-top: 14px; }
.pill { font-size: 12px; padding: 1px 8px; border-radius: 99px; border: 1px solid var(--line); white-space: nowrap; }
.pill.on, .pill.ok { color: var(--ok); border-color: var(--ok); }
.pill.off, .pill.failed { color: var(--err); border-color: var(--err); }
.pill.need_clarify, .pill.off_topic, .pill.running { color: var(--warn); border-color: var(--warn); }
.pill.stopped { color: var(--muted); }
/* 生成中的底色 = 上下文用量（--ctx 由 ai.js 设），超过 80% 整个变红 */
.pill.running { background: linear-gradient(90deg, color-mix(in srgb, var(--warn) 25%, transparent) var(--ctx, 0%), transparent var(--ctx, 0%)); }
.pill.running.hot { color: var(--err); border-color: var(--err);
  background: linear-gradient(90deg, color-mix(in srgb, var(--err) 25%, transparent) var(--ctx, 0%), transparent var(--ctx, 0%)); }

/* ---------- 顶栏 + 左右布局 ---------- */
.top { display: flex; gap: 10px; align-items: center; padding: 10px 16px; background: var(--card); border-bottom: 1px solid var(--line); flex: none; }
#btnSide { display: none; }
.layout { flex: 1; display: flex; min-height: 0; }
aside { width: var(--side); flex: none; background: var(--card); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.new { margin: 12px; }
.side-title { padding: 0 16px 6px; font-size: 12px; color: var(--muted); }
#sessions { list-style: none; margin: 0; padding: 0 8px 12px; overflow-y: auto; flex: 1; }
#sessions li { display: flex; align-items: center; gap: 4px; padding: 7px 8px; border-radius: 6px; cursor: pointer; }
#sessions li:hover { background: var(--soft); }
#sessions li.on { background: var(--accent-soft); }
#sessions .t { flex: 1; min-width: 0; }
#sessions .t div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
#sessions .t small { color: var(--muted); font-size: 12px; }
#sessions .del { visibility: hidden; background: none; color: var(--muted); padding: 2px 6px; }
#sessions li:hover .del, #sessions li.on .del { visibility: visible; }
#sessions .del:hover { color: var(--err); }
#sessions .none { color: var(--muted); font-size: 13px; cursor: default; }
#sessions .none:hover { background: none; }
main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }

/* ---------- 对话 ---------- */
#msgs { flex: 1; overflow-y: auto; padding: 20px 16px; }
#msgs > * { max-width: 900px; margin-left: auto; margin-right: auto; }
.empty { color: var(--muted); padding: 40px 0; }
.empty h2 { color: var(--fg); }
.msg { margin-bottom: 18px; display: flex; flex-direction: column; }
.msg.user { align-items: flex-end; }
.bubble { background: var(--accent); color: #fff; padding: 9px 13px; border-radius: 12px 12px 2px 12px; max-width: 80%; white-space: pre-wrap; word-break: break-word; }
.used { margin-top: 4px; display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; max-width: 80%; }
.used span { font-size: 12px; color: var(--muted); background: var(--soft); border-radius: 4px; padding: 0 6px; }
.answer { background: var(--card); border: 1px solid var(--line); border-radius: 12px 12px 12px 2px; padding: 12px 14px; width: 100%; }
.answer .head { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
.answer p { margin: 4px 0; white-space: pre-wrap; }
.progress { margin: 4px 0 0; padding-left: 20px; color: var(--muted); font-size: 13px; }
.progress li:last-child { color: var(--fg); }

/* 生成中：转圈 + 已用时间 + 当前步骤后面跳动的省略号，让用户知道还在干活 */
.spin { width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; flex: none; }
.elapsed { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.answer.running { position: relative; overflow: hidden; }
.answer.running::after { content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 2px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent); background-size: 40% 100%; background-repeat: no-repeat;
  animation: sweep 1.4s ease-in-out infinite; }
.answer.running .progress li:last-child::after { content: ""; display: inline-block; width: 1.2em; text-align: left; animation: dots 1.2s steps(4) infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes sweep { from { background-position: -40% 0; } to { background-position: 140% 0; } }
@keyframes dots { 0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; } }
@media (prefers-reduced-motion: reduce) {
  .spin, .answer.running::after, .answer.running .progress li:last-child::after { animation: none; }
}
.codebox { position: relative; margin-top: 8px; }
.codebox pre { margin: 0; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; max-height: 480px; overflow: auto;
  font: 13px/1.5 ui-monospace, Consolas, monospace; white-space: pre; }
.codebox button { position: absolute; top: 6px; right: 6px; padding: 2px 10px; font-size: 12px; }
details { margin-top: 8px; font-size: 13px; }
summary { cursor: pointer; color: var(--muted); }
table { width: 100%; border-collapse: collapse; margin-top: 6px; font-size: 13px; }
td, th { border-bottom: 1px solid var(--line); padding: 4px 6px; text-align: left; vertical-align: top; }
td code { word-break: break-all; }
.meta div { margin: 2px 0; }
.foot { display: flex; gap: 6px; margin-top: 8px; align-items: center; }
.foot button { padding: 2px 10px; font-size: 13px; }
.foot button.on { border-color: var(--accent); color: var(--accent); }

/* ---------- 输入区 + 附件栏 ---------- */
.composer { flex: none; border-top: 1px solid var(--line); background: var(--card); padding: 8px 16px 12px; }
.composer > * { max-width: 900px; margin-left: auto; margin-right: auto; }
.files-head { display: flex; gap: 8px; align-items: center; font-size: 13px; }
.files-bar { display: flex; gap: 6px; align-items: stretch; margin: 6px auto 8px; }
.files { display: flex; gap: 6px; overflow-x: auto; flex: 1; padding-bottom: 4px; scrollbar-width: thin; }
.files:empty::before { content: "还没有附件。上传设备说明书后，AI 才能按设备协议生成指令。"; color: var(--muted); font-size: 13px; align-self: center; }
.chip { flex: none; display: flex; align-items: center; gap: 6px; border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; background: var(--bg);
  font-size: 13px; max-width: 260px; cursor: pointer; user-select: none; }
.chip.on { border-color: var(--accent); background: var(--accent-soft); }
.chip.disabled { cursor: default; opacity: .75; }
.chip input[type=checkbox] { margin: 0; }
.chip .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.chip .sub { display: block; font-size: 11px; color: var(--muted); white-space: nowrap; }
.chip .sub a { color: inherit; text-decoration: underline dotted; cursor: pointer; }
.chip .x { background: none; color: var(--muted); padding: 0 4px; font-size: 15px; line-height: 1; }
.chip .x:hover { color: var(--err); }
.chip.add { color: var(--accent); border-style: dashed; justify-content: center; }
.input-row { display: flex; gap: 8px; align-items: stretch; }

/* 贴的图片（发送前） */
.pics { display: flex; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.pic { position: relative; width: 64px; height: 64px; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; background: var(--bg); }
.pic img { width: 100%; height: 100%; object-fit: cover; cursor: zoom-in; display: block; }
.pic .x { position: absolute; top: 2px; right: 2px; width: 18px; height: 18px; padding: 0; border-radius: 50%; background: rgba(0,0,0,.55); color: #fff; font-size: 12px; line-height: 18px; }
.pic.busy::after { content: ""; position: absolute; inset: 22px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
.bubble-pics { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; margin-bottom: 6px; max-width: 80%; }
.bubble-pics img { height: 96px; width: auto; max-width: 240px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); cursor: zoom-in; background: #fff; }

/* 拖文件进来时，底部输入区高亮 */
.composer { position: relative; }
.drop-hint { position: absolute; inset: 4px; z-index: 5; display: flex; align-items: center; justify-content: center; text-align: center; padding: 12px;
  border: 2px dashed var(--accent); border-radius: 10px; background: color-mix(in srgb, var(--accent-soft) 92%, transparent); color: var(--accent); font-weight: 600; pointer-events: none; }
.drop-hint[hidden] { display: none; }
.imgdlg { width: auto; max-width: calc(100vw - 32px); padding: 10px; }
.imgdlg form { display: flex; flex-direction: column; gap: 8px; align-items: flex-end; }
.imgdlg img { max-width: calc(100vw - 60px); max-height: calc(100vh - 120px); display: block; }
.input-row textarea { flex: 1; height: 128px; }
.input-side { display: flex; flex-direction: column; gap: 6px; }
.input-side button { flex: 1; min-width: 92px; }

dialog { border: 1px solid var(--line); border-radius: 10px; background: var(--card); color: var(--fg); width: min(440px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0, 0, 0, .35); }
dialog label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); margin-top: 10px; }

/* ---------- 手机：历史对话改成抽屉 ---------- */
@media (max-width: 760px) {
  #btnSide { display: inline-block; }
  aside { position: fixed; top: 0; bottom: 0; left: 0; z-index: 20; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 20px rgba(0,0,0,.2); }
  aside.open { transform: none; }
  #scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .3); z-index: 10; }
  .hide-sm { display: none; }
  .bubble, .used { max-width: 92%; }
  .input-side select { width: 92px; }
  #msgs, .composer { padding-left: 12px; padding-right: 12px; }
}
.imgtext { white-space: pre-wrap; font: 12px/1.5 ui-monospace, Consolas, monospace; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 8px; max-height: 320px; overflow: auto; margin: 6px 0 0; }

/* ---------- 写程序 / 问问题 ---------- */
.modes { display: flex; border: 1px solid var(--line); border-radius: 8px; padding: 2px; gap: 2px; background: var(--bg); }
.modes button { background: none; color: var(--muted); padding: 3px 12px; font-size: 14px; border-radius: 6px; }
.modes button.on { background: var(--card); color: var(--accent); box-shadow: 0 1px 2px rgb(0 0 0 / .12); }
body.mode-ask .only-code, body:not(.mode-ask) .only-ask { display: none; }

/* 问答的 Markdown 回答 */
.md { overflow-wrap: anywhere; }
.md p { margin: 6px 0; }
.md h3, .md h4, .md h5, .md h6 { margin: 12px 0 4px; font-size: 15px; font-weight: 600; }
.md h3 { font-size: 16px; }
.md ul, .md ol { margin: 6px 0; padding-left: 22px; }
.md li { margin: 2px 0; }
.md code { background: var(--soft); border-radius: 4px; padding: 1px 5px; font: 13px/1.5 ui-monospace, SFMono-Regular, Consolas, monospace; }
.md .codebox code, .md pre code { background: none; padding: 0; }
.md blockquote { margin: 6px 0; padding: 2px 10px; border-left: 3px solid var(--line); color: var(--muted); white-space: pre-wrap; }
.md .tbl { overflow-x: auto; margin: 6px 0; }
.md table { border-collapse: collapse; font-size: 14px; }
.md th, .md td { border: 1px solid var(--line); padding: 4px 8px; text-align: left; vertical-align: top; }
.md th { background: var(--soft); }
.md a, .refs a { color: var(--accent); }
.refs { margin-top: 10px; padding-top: 8px; border-top: 1px dashed var(--line); font-size: 14px; display: flex; flex-direction: column; gap: 3px; }
.refs-title { font-size: 12px; color: var(--muted); }
@media (max-width: 760px) {
  .modes button { padding: 3px 8px; }
  h1 { display: none; }
  .modes { margin-right: auto; }
}
