:root {
  --bg: #f6f5f1;
  --panel: #ffffff;
  --text: #1f1e1b;
  --muted: #6b6a64;
  --line: #e2e0d8;
  --accent: #c2603d;
  --accent-ink: #ffffff;
  --user: #ece9df;
  --code-bg: #1f1e1b;
  --code-ink: #f1efe8;
  --ok: #3f7d4e;
  --warn: #b7791f;
  --err: #b4412f;
  --add-bg: #e6f4ea;
  --add-ink: #1e5631;
  --del-bg: #fbe9e7;
  --del-ink: #8c2f1f;
  --changed: #fdf3dc;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1a18;
    --panel: #24231f;
    --text: #ecebe6;
    --muted: #a3a199;
    --line: #3a3934;
    --user: #33312c;
    --code-bg: #121210;
    --add-bg: #1d3324;
    --add-ink: #a8dbb4;
    --del-bg: #3d221d;
    --del-ink: #f0b3a6;
    --changed: #3a3220;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 system-ui, "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
}
button, input, textarea { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.small { padding: 2px 8px; font-size: 13px; }
input, textarea {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  padding: 8px 10px;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
a { color: var(--accent); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.err { color: var(--err); }

/* ---- 登入 ---- */
.login {
  max-width: 360px;
  margin: 12vh auto 0;
  padding: 28px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.login h1 { margin: 0 0 4px; font-size: 20px; }

/* ---- 聊天 ---- */
.app { display: flex; flex-direction: column; height: 100%; }
header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  flex-wrap: wrap;
}
header h1 { font-size: 16px; margin: 0; margin-right: auto; }
.quota { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.bar { width: 120px; height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
.bar > div { height: 100%; background: var(--ok); width: 100%; transition: width .3s; }
.bar.low > div { background: var(--warn); }
.bar.empty > div { background: var(--err); }

.banner {
  background: var(--warn);
  color: #fff;
  padding: 8px 16px;
  text-align: center;
  font-size: 14px;
}
.admin .banner { border-radius: 10px; }
.banner button { color: var(--text); }

.body { flex: 1; display: flex; min-height: 0; }
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
}

/* ---- 檔案側欄 ---- */
.files {
  width: 340px;
  flex-shrink: 0;
  border-left: 1px solid var(--line);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.files-head { display: flex; align-items: center; gap: 6px; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.files-head strong { margin-right: auto; font-size: 14px; }
.files-zip {
  margin: 10px 12px 4px;
  padding: 7px 12px;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-ink);
  text-align: center;
  text-decoration: none;
  font-size: 14px;
}
#files-note { padding: 0 12px; font-size: 12px; }
#files-list { list-style: none; margin: 4px 0 0; padding: 0 0 12px; overflow-y: auto; flex: 1; }
#files-list li { padding: 5px 12px; border-bottom: 1px solid var(--line); font-size: 13px; }
#files-list a { color: var(--text); text-decoration: none; overflow-wrap: anywhere; font-family: ui-monospace, "SFMono-Regular", monospace; }
#files-list a:hover { color: var(--accent); text-decoration: underline; }
#files-list .meta { color: var(--muted); font-size: 11.5px; }
#files-list .dir { color: var(--muted); }
#files-list a.preview { color: var(--accent); font-family: inherit; }
#files-list a.thumb { display: block; margin-top: 4px; }
#files-list a.thumb img { display: block; max-width: 100%; max-height: 160px; border: 1px solid var(--line); border-radius: 4px; background: #fff; }
.files.dragover { outline: 2px dashed var(--accent); outline-offset: -6px; }
#files-list li.changed { background: var(--changed); }
#files-list .badge {
  float: right; margin-left: 6px; padding: 0 6px; border-radius: 4px;
  background: var(--accent); color: var(--accent-ink); font-size: 11px;
}
#files-list button.linklike {
  border: none; background: none; padding: 0; font: inherit; color: var(--accent); cursor: pointer;
}
#files-list button.linklike:hover { text-decoration: underline; }
#files-list .files-empty { font-size: 12px; border-bottom: none; }
#files-list li.files-code-wrap { padding: 0; border-bottom: none; }
.files-code > summary { padding: 8px 12px; font-size: 13px; color: var(--muted); cursor: pointer; }
.files-code > ul { list-style: none; margin: 0; padding: 0; }
.files-code > ul li { padding-left: 24px; }
@media (min-width: 1400px) { .files { width: 400px; } }
@media (max-width: 760px) {
  .files { position: fixed; inset: 0; width: auto; z-index: 10; border-left: none; }
}
/* ---- 對話紀錄（左側）---- */
.convs {
  width: 260px;
  flex-shrink: 0;
  border-right: 1px solid var(--line);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
#convs-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; }
#convs-list li { margin: 0; display: flex; align-items: center; gap: 2px; }
#convs-list li .conv { flex: 1; min-width: 0; }
button.conv-del {
  flex-shrink: 0; padding: 4px 6px; font-size: 13px; border-color: transparent; background: none;
  opacity: 0; transition: opacity .15s;
}
#convs-list li:hover button.conv-del, button.conv-del:focus-visible { opacity: .7; }
button.conv-del:hover:not(:disabled) { opacity: 1; border-color: var(--line); }
@media (hover: none) { button.conv-del { opacity: .6; } }
.conv {
  display: block; width: 100%; text-align: left; padding: 8px 10px; margin-bottom: 2px;
  border: 1px solid transparent; border-radius: 8px; background: none; cursor: pointer;
}
.conv:hover:not(:disabled) { background: var(--bg); border-color: var(--line); }
.conv.current { background: var(--user); border-color: var(--line); }
.conv:disabled { cursor: default; opacity: .6; }
.conv.current:disabled { opacity: 1; }
.conv-title { display: block; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-meta { display: block; font-size: 12px; color: var(--muted); margin-top: 2px; }
.convs-empty { padding: 12px; font-size: 13px; }
@media (max-width: 760px) {
  .convs { position: fixed; inset: 0; width: auto; z-index: 10; border-right: none; }
}

/* 回覆裡的 Markdown 表格：太寬就橫向捲動，不撐破對話框 */
.md-table { overflow-x: auto; margin: 8px 0; }
.md-table table { border-collapse: collapse; font-size: 14px; }
.md-table th, .md-table td { border: 1px solid var(--line); padding: 5px 10px; vertical-align: top; }
.md-table th { background: var(--user); font-weight: 600; white-space: nowrap; }
.md-table tbody tr:nth-child(even) td { background: var(--bg); }
.md-table .al-right { text-align: right; font-variant-numeric: tabular-nums; }
.md-table .al-center { text-align: center; }

.thread { max-width: 820px; margin: 0 auto; display: flex; flex-direction: column; gap: 14px; }
.msg { padding: 10px 14px; border-radius: 12px; overflow-wrap: anywhere; }
.msg.user { background: var(--user); align-self: flex-end; max-width: 85%; white-space: pre-wrap; }
.msg.assistant { background: var(--panel); border: 1px solid var(--line); }
.msg.system { color: var(--muted); font-size: 13px; text-align: center; }
.msg.error { color: var(--err); font-size: 14px; border: 1px solid var(--err); background: transparent; }
.steps { font-size: 13px; color: var(--muted); }
.steps > summary { cursor: pointer; }
.step-list { display: flex; flex-direction: column; gap: 6px; margin-top: 6px; }
.step-text { padding: 0 4px; overflow-wrap: anywhere; }

/* ---- 工具卡片 ---- */
.tool { border: 1px solid var(--line); border-radius: 8px; background: var(--panel); color: var(--text); }
.tool > summary {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 10px; cursor: pointer; list-style: none; min-width: 0;
}
.tool > summary::-webkit-details-marker { display: none; }
.tool > summary::before { content: "▸"; color: var(--muted); font-size: 11px; width: 10px; transition: transform .15s; }
.tool[open] > summary::before { transform: rotate(90deg); }
.tool.flat > summary { cursor: default; }
.tool.flat > summary::before { visibility: hidden; }
.tool-icon { width: 1.3em; text-align: center; flex-shrink: 0; }
.tool-verb { color: var(--muted); flex-shrink: 0; }
.tool-target {
  font-family: ui-monospace, "SFMono-Regular", monospace; font-size: 12.5px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1;
}
.tool-extra { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; flex-shrink: 0; }
.tool-state { font-size: 12px; flex-shrink: 0; }
.tool-state.ok { color: var(--ok); }
.tool-state.error { color: var(--err); }
.tool-state.denied { color: var(--muted); }
.tool-state.stopped { color: var(--warn); }
.tool-state.running::after {
  content: ""; display: inline-block; width: 10px; height: 10px; vertical-align: -1px;
  border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.tool.denied { border-style: dashed; }
.tool-body { padding: 0 10px 8px; display: flex; flex-direction: column; gap: 6px; }
.tool-body pre {
  margin: 0; padding: 8px 10px; border-radius: 6px; overflow-x: auto; max-height: 360px; overflow-y: auto;
  font: 12.5px/1.5 ui-monospace, "SFMono-Regular", monospace; white-space: pre;
}
.tool-body pre.cmd, .tool-body pre.out { background: var(--code-bg); color: var(--code-ink); }
.tool-body pre.out.err { color: #f0b3a6; }
.tool-body pre.diff { background: var(--bg); border: 1px solid var(--line); padding: 4px 0; }
.diff span { display: block; padding: 0 10px; }
.diff .add { background: var(--add-bg); color: var(--add-ink); }
.diff .del { background: var(--del-bg); color: var(--del-ink); }
.diff .ctx { color: var(--muted); }
.tool-note { font-size: 12.5px; color: var(--muted); }
.tool-note.err { color: var(--err); }
.status { font-size: 13px; color: var(--muted); padding: 2px 4px; }
.empty-hint { text-align: center; color: var(--muted); margin-top: 18vh; }

.md p { margin: 0 0 .6em; }
.md p:last-child { margin-bottom: 0; }
.md h3, .md h4 { margin: .8em 0 .3em; font-size: 1em; }
.md ul, .md ol { margin: 0 0 .6em; padding-left: 1.4em; }
.md code { background: var(--user); padding: 1px 5px; border-radius: 4px; font-size: .92em; font-family: ui-monospace, "SFMono-Regular", monospace; }
.md pre { background: var(--code-bg); color: var(--code-ink); padding: 12px; border-radius: 8px; overflow-x: auto; margin: 0 0 .6em; }
.md pre code { background: none; padding: 0; color: inherit; font-size: 13px; }

footer {
  border-top: 1px solid var(--line);
  background: var(--panel);
  padding: 12px 16px;
}
.composer { max-width: 820px; margin: 0 auto; display: flex; gap: 8px; align-items: flex-end; position: relative; }

/* ---- 技能選單（打 /）---- */
.skills-btn { flex-shrink: 0; min-height: 44px; white-space: nowrap; }
.skill-menu {
  position: absolute; left: 0; right: 0; bottom: calc(100% + 6px); z-index: 20;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .14); padding: 4px; max-height: 50vh; overflow-y: auto;
}
.skill-menu-head { font-size: 12px; color: var(--muted); padding: 6px 10px 4px; }
.skill-item {
  display: grid; grid-template-columns: auto auto auto 1fr; align-items: baseline; gap: 8px;
  padding: 8px 10px; border-radius: 7px; cursor: pointer;
}
.skill-item.active { background: var(--user); }
.skill-icon { font-size: 16px; }
.skill-label { font-weight: 600; }
.skill-name { font-size: 12px; color: var(--muted); }
.skill-desc { font-size: 13px; color: var(--muted); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.skill-empty { padding: 8px 10px; font-size: 13px; color: var(--muted); }
.skill-chip {
  max-width: 820px; margin: 0 auto 8px; display: flex; align-items: center; gap: 8px;
  font-size: 13px; color: var(--muted);
}
.skill-chip-text { flex: 1; min-width: 0; }
button.skill-chip-x { padding: 0 6px; font-size: 12px; line-height: 20px; border-color: transparent; background: none; color: var(--muted); }
.skill-tag {
  display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12.5px; font-weight: 600;
  background: var(--accent); color: var(--accent-ink); white-space: nowrap;
}
.empty-skills { margin-top: 8px; }
@media (max-width: 560px) {
  .skill-item { grid-template-columns: auto 1fr auto; }
  .skill-desc { grid-column: 1 / -1; white-space: normal; }
}
.composer textarea { flex: 1; resize: none; min-height: 44px; max-height: 40vh; }
button.stop { border-color: var(--err); color: var(--err); font-weight: 600; }
button.stop::before { content: "■ "; font-size: .8em; }
.hint { max-width: 820px; margin: 4px auto 0; font-size: 12px; color: var(--muted); }

/* ---- 管理 ---- */
.admin main { max-width: 1280px; margin: 0 auto; padding: 16px; display: flex; flex-direction: column; gap: 16px; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; }
.card .label { font-size: 12px; color: var(--muted); }
.card .value { font-size: 22px; font-variant-numeric: tabular-nums; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px; overflow-x: auto; }
.panel h2 { font-size: 15px; margin: 0 0 10px; }
table { border-collapse: collapse; width: 100%; font-size: 13.5px; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-weight: 500; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
td input.quota-input { width: 72px; padding: 2px 6px; }
.actions { display: flex; gap: 4px; flex-wrap: wrap; min-width: 420px; }
.tag { font-size: 12px; padding: 1px 6px; border-radius: 4px; background: var(--user); }
.tag.busy { background: var(--accent); color: var(--accent-ink); }
.tag.off { background: var(--err); color: #fff; }
.form-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.form-row input { width: 160px; }
.form-row input[type="checkbox"] { width: auto; }
.panel code { background: var(--user); padding: 1px 5px; border-radius: 4px; font-size: .92em; }
.form-row input.short { width: 90px; }
.log { max-height: 420px; overflow-y: auto; font-size: 13px; }
.log .role { color: var(--muted); font-size: 12px; }
.log pre { white-space: pre-wrap; margin: 2px 0 10px; font-family: inherit; }

/* 在另一台電腦（同組共用帳號）送出的訊息 */
.msg-origin { display: block; margin-top: 4px; font-size: 11.5px; color: var(--muted); }

/* ---- 文件預覽（Office／PDF 每頁一張圖）---- */
.viewer {
  position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .55);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.viewer-box {
  width: min(1000px, 100%); height: 100%; background: var(--panel); border-radius: 10px;
  display: flex; flex-direction: column; overflow: hidden;
}
.viewer-head {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 10px 14px; border-bottom: 1px solid var(--line);
}
.viewer-head strong { margin-right: auto; font-size: 15px; overflow-wrap: anywhere; }
.viewer-dl { color: var(--accent); font-size: 14px; }
.viewer-info { flex-basis: 100%; font-size: 12px; }
.viewer-body { flex: 1; overflow-y: auto; padding: 16px; background: var(--bg); }
.viewer-page { margin: 0 auto 18px; max-width: 900px; }
.viewer-page img { display: block; width: 100%; background: #fff; border: 1px solid var(--line); box-shadow: 0 1px 4px rgba(0, 0, 0, .12); }
.viewer-page figcaption { text-align: center; font-size: 12px; color: var(--muted); margin-top: 4px; }
.viewer-wait { padding: 40px 12px; text-align: center; color: var(--muted); }
.viewer-wait.err { color: var(--err); }
@media (max-width: 760px) { .viewer { padding: 0; } .viewer-box { border-radius: 0; } .viewer-body { padding: 8px; } }
