:root {
  --bg: #f6f7f9; --panel: #ffffff; --text: #1d2330; --muted: #6b7385; --line: #e3e6ec;
  --accent: #2f6fde; --accent-text: #ffffff; --danger: #c62f3b; --ok: #1f8a4c; --warn: #b7791f;
  --code-bg: #f0f2f5; --user-bubble: #e8f0fe; --term-bg: #0f1218;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b; --panel: #1a1e26; --text: #e6e9ef; --muted: #8d95a7; --line: #2a303b;
    --accent: #5b8ef0; --code-bg: #232833; --user-bubble: #22304a;
  }
}
* { 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; }
body.center { display: grid; place-items: center; padding: 16px; }
h1 { font-size: 1.4rem; margin: 0 0 1rem; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 24px; }
.login { width: 100%; max-width: 380px; }
label { display: block; margin-bottom: .8rem; font-size: .9rem; color: var(--muted); }
input, textarea, select {
  display: block; width: 100%; margin-top: .3rem; padding: .55rem .7rem; font: inherit; color: var(--text);
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
button {
  font: inherit; padding: .5rem 1rem; border-radius: 8px; border: 1px solid transparent; cursor: pointer;
  background: var(--accent); color: var(--accent-text);
}
button.ghost { background: transparent; color: var(--text); border-color: var(--line); }
button.danger { background: var(--danger); }
button:disabled { opacity: .5; cursor: default; }
.login button { width: 100%; margin-top: .4rem; }
.error { color: var(--danger); min-height: 1.2em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
code { background: var(--code-bg); padding: .1rem .35rem; border-radius: 4px; word-break: break-all; }
#qr svg { width: 200px; height: 200px; background: #fff; padding: 8px; border-radius: 8px; display: block; margin: .5rem auto; }

/* 主畫面 */
.layout { display: grid; grid-template-rows: auto 1fr; height: 100vh; height: 100dvh; }
header.top {
  display: flex; align-items: center; gap: .75rem; padding: .6rem 1rem;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
header.top .brand { font-weight: 600; margin-right: auto; }
nav.tabs { display: flex; gap: .25rem; }
nav.tabs button { background: transparent; color: var(--muted); border: none; padding: .4rem .7rem; }
nav.tabs button.active { color: var(--accent); background: var(--code-bg); }
.view { min-height: 0; display: none; }
.view.active { display: grid; }

#v-chat { grid-template-columns: 240px 1fr; }
aside.sessions { border-right: 1px solid var(--line); overflow: auto; padding: .75rem; background: var(--panel); }
aside.sessions button.new { width: 100%; margin-bottom: .75rem; }
aside.sessions .item {
  display: block; width: 100%; text-align: left; background: transparent; color: var(--text);
  border: none; padding: .45rem .6rem; border-radius: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
aside.sessions .item-row { display: flex; align-items: center; border-radius: 6px; }
aside.sessions .item-row.active { background: var(--code-bg); }
aside.sessions .item-row .item { flex: 1; min-width: 0; }
aside.sessions .rename, aside.sessions .delete { background: transparent; color: var(--muted); border: none; padding: .2rem .35rem; opacity: 0; }
aside.sessions .delete:hover { color: var(--danger); }
aside.sessions .item-row:hover :is(.rename, .delete), aside.sessions .item-row.active :is(.rename, .delete) { opacity: 1; }
@media (hover: none) { aside.sessions :is(.rename, .delete) { opacity: 1; } }
.chat { display: grid; grid-template-rows: 1fr auto auto; min-height: 0; }
.messages { overflow: auto; padding: 1rem 1.25rem; }
.msg { max-width: 860px; margin: 0 auto 1rem; }
.msg.user .bubble { background: var(--user-bubble); border-radius: 10px; padding: .6rem .9rem; margin-left: auto; width: fit-content; max-width: 85%; white-space: pre-wrap; }
.msg .text { overflow-wrap: anywhere; }
.msg .text pre { background: var(--code-bg); padding: .7rem; border-radius: 8px; overflow: auto; }
.msg .text p { margin: .4rem 0; }
.msg .err { color: var(--danger); }
details.tool { border: 1px solid var(--line); border-radius: 8px; margin: .4rem 0; background: var(--panel); }
details.tool summary { padding: .35rem .7rem; cursor: pointer; font-size: .88rem; color: var(--muted); }
details.tool pre { margin: 0; padding: .6rem .7rem; font-size: .8rem; max-height: 320px; overflow: auto; border-top: 1px solid var(--line); white-space: pre-wrap; }
details.reasoning summary { font-size: .85rem; color: var(--muted); cursor: pointer; }
details.reasoning div { font-size: .85rem; color: var(--muted); white-space: pre-wrap; }
.status-dot { display: inline-block; width: .55rem; height: .55rem; border-radius: 50%; margin-right: .35rem; background: var(--muted); }
.status-dot.completed { background: var(--ok); } .status-dot.error { background: var(--danger); } .status-dot.running, .status-dot.pending { background: var(--warn); }
.perm { max-width: 860px; margin: 0 auto 1rem; border: 1px solid var(--warn); border-radius: 10px; padding: .75rem 1rem; }
.perm .actions { display: flex; gap: .5rem; margin-top: .5rem; flex-wrap: wrap; }
.thinking { max-width: 860px; margin: 0 auto 1rem; color: var(--muted); }
form.composer { display: flex; gap: .5rem; padding: .75rem 1rem; border-top: 1px solid var(--line); background: var(--panel); }
form.composer textarea { margin: 0; resize: none; min-height: 2.8rem; max-height: 12rem; }
.empty { color: var(--muted); text-align: center; margin-top: 20vh; padding: 0 1rem; }

#v-terminal { padding: .5rem; background: var(--term-bg); }
#term { height: 100%; }

#v-admin { overflow: auto; padding: 1rem; align-content: start; }
#v-admin .card { max-width: 980px; width: 100%; margin: 0 auto 1rem; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { text-align: left; padding: .45rem .5rem; border-bottom: 1px solid var(--line); }
td .row-actions { display: flex; gap: .3rem; flex-wrap: wrap; }
td .row-actions button { padding: .2rem .55rem; font-size: .8rem; }
.inline-form { display: flex; gap: .5rem; flex-wrap: wrap; align-items: end; }
.inline-form label { margin: 0; flex: 1 1 160px; }
.badge { font-size: .75rem; padding: .1rem .45rem; border-radius: 999px; background: var(--code-bg); }
.badge.ready { color: var(--ok); } .badge.error { color: var(--danger); } .badge.provisioning { color: var(--warn); }
.notice { background: var(--code-bg); border-radius: 8px; padding: .75rem 1rem; margin-top: .75rem; }

@media (max-width: 720px) {
  #v-chat { grid-template-columns: 1fr; }
  aside.sessions { display: none; }
  aside.sessions.open { display: block; position: fixed; inset: 3.2rem 0 auto 0; max-height: 60vh; z-index: 5; border-bottom: 1px solid var(--line); }
  .mobile-only { display: inline-block !important; }
  header.top { padding: .5rem; gap: .4rem; }
  .messages { padding: .75rem; }
  table { font-size: .8rem; }
}
.mobile-only { display: none !important; }

/* 工作狀態列 */
.statusbar { padding: .45rem 1rem; border-top: 1px solid var(--line); background: var(--panel); font-size: .9rem; min-width: 0; }
.statusbar .st-row { display: flex; align-items: center; gap: .6rem; min-width: 0; }
.statusbar.clickable { cursor: pointer; }
.statusbar.clickable:hover { background: var(--code-bg); }
.st-tail { font: .8rem/1.4 ui-monospace, Menlo, Consolas, monospace; margin: .2rem 0 0 1.6rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.statusbar[hidden] { display: none; }
.statusbar #st-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 0 1 auto; }
.statusbar #st-time { white-space: nowrap; margin-left: auto; font-variant-numeric: tabular-nums; }
.statusbar.waiting { color: var(--warn); }
.spinner { flex: none; width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
.statusbar.waiting .spinner { border-top-color: var(--warn); }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }
.turn-meta { font-size: .8rem; color: var(--muted); margin-top: .4rem; }
details.tool summary { display: flex; align-items: baseline; gap: .4rem; }
details.tool summary::-webkit-details-marker { display: none; }
details.tool summary::before { content: '▸'; color: var(--muted); flex: none; }
details.tool[open] summary::before { content: '▾'; }
details.tool summary .tool-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
details.tool summary .tool-detail { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .82rem; }
details.tool summary .took { flex: none; font-size: .78rem; background: var(--code-bg); border-radius: 999px; padding: 0 .5rem; font-variant-numeric: tabular-nums; }
.status-dot.running, .status-dot.pending { animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .3; } }
.busy-dot { display: inline-block; width: .5rem; height: .5rem; border-radius: 50%; background: var(--warn); margin-right: .4rem; animation: pulse 1s ease-in-out infinite; }
.msg.user .queued { font-size: .8rem; color: var(--warn); text-align: right; margin-top: .25rem; }
details.tool.running { border-color: var(--warn); }
