:root {
  --bg: #ffffff; --bg-side: #f6f8fa; --bg-soft: #f6f8fa; --bg-user: #ddf4ff; --border: #d0d7de; --border-soft: #e7ebef;
  --text: #1f2328; --muted: #656d76; --accent: #0969da; --accent-text: #ffffff; --danger: #cf222e; --ok: #1a7f37;
  --warn: #9a6700; --code-bg: #f1f3f5; --shadow: 0 8px 24px rgba(140, 149, 159, .2);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117; --bg-side: #010409; --bg-soft: #161b22; --bg-user: #1b2b45; --border: #30363d; --border-soft: #21262d;
    --text: #e6edf3; --muted: #8d96a0; --accent: #2f81f7; --accent-text: #ffffff; --danger: #f85149; --ok: #3fb950;
    --warn: #d29922; --code-bg: #1c2128; --shadow: 0 8px 24px rgba(1, 4, 9, .6);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  color: var(--text); background: var(--bg); -webkit-text-size-adjust: 100%; overflow: hidden;
}
button, select, textarea, input { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.spacer { flex: 1; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* 版面 */
#app { display: flex; height: 100dvh; }
#side {
  width: 272px; flex: none; display: flex; flex-direction: column; background: var(--bg-side);
  border-right: 1px solid var(--border); min-height: 0;
}
#main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#scrim { display: none; }

/* 側欄 */
.side-head { padding: 14px 12px 8px; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; margin: 2px 4px 12px; }
.logo {
  width: 22px; height: 22px; border-radius: 6px; flex: none; background: var(--accent)
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cpath d='M8 11.5a2 2 0 0 1 2-2h4l2 2h6a2 2 0 0 1 2 2v7a2 2 0 0 1-2 2H10a2 2 0 0 1-2-2z' fill='%23fff'/%3E%3C/svg%3E") center/100% no-repeat;
}
.logo.big { width: 44px; height: 44px; border-radius: 11px; display: inline-block; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 6px 14px; border-radius: 6px;
  border: 1px solid var(--border); background: var(--bg); font-weight: 500; text-decoration: none !important; color: var(--text);
}
.btn:hover { background: var(--bg-soft); }
.btn.block { width: 100%; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .6; cursor: default; }
#convs { flex: 1; overflow-y: auto; padding: 4px 8px 12px; min-height: 0; }
.group { font-size: 12px; color: var(--muted); padding: 12px 8px 4px; font-weight: 600; }
.conv { display: flex; align-items: center; border-radius: 6px; }
.conv:hover, .conv.active { background: var(--border-soft); }
.conv-title {
  flex: 1; min-width: 0; text-align: left; border: 0; background: none; padding: 7px 8px; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.conv-more { border: 0; background: none; padding: 4px 8px; color: var(--muted); border-radius: 6px; opacity: 0; }
.conv:hover .conv-more, .conv.active .conv-more { opacity: 1; }
.empty-side { color: var(--muted); padding: 16px 8px; font-size: 13px; }
.side-foot { border-top: 1px solid var(--border); padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); font-size: 13px; }
.row-btn {
  display: flex; align-items: center; gap: 8px; width: 100%; border: 0; background: none; padding: 6px 4px; border-radius: 6px;
  text-align: left;
}
.row-btn:hover { background: var(--border-soft); }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--muted); }
.dot.ok { background: var(--ok); }
.dot.bad { background: var(--danger); }
.dot.wait { background: var(--warn); animation: pulse 1.2s infinite; }
.usage { padding: 6px 4px; }
.usage-line { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; margin-bottom: 4px; }
.bar { height: 4px; border-radius: 2px; background: var(--border); overflow: hidden; }
.bar > div { height: 100%; width: 0; background: var(--ok); transition: width .3s; }
.bar > div.warm { background: var(--warn); }
.bar > div.hot { background: var(--danger); }
.foot-row { display: flex; align-items: center; gap: 12px; padding: 4px; }
.link { border: 0; background: none; color: var(--accent); padding: 0; font-size: 13px; }
.link:hover { text-decoration: underline; }

/* 頂列 */
#top {
  display: flex; align-items: center; gap: 8px; padding: 8px 14px; border-bottom: 1px solid var(--border-soft);
  min-height: 48px; padding-top: calc(8px + env(safe-area-inset-top));
}
#title { flex: 1; min-width: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon { border: 0; background: none; padding: 7px; border-radius: 6px; display: inline-flex; color: var(--muted); }
.icon:hover { background: var(--bg-soft); color: var(--text); }
#menuBtn { display: none; }
#model {
  border: 1px solid var(--border); border-radius: 6px; padding: 4px 8px; background: var(--bg); font-size: 13px; max-width: 150px;
}

/* 對話 */
#scroll { flex: 1; overflow-y: auto; min-height: 0; overscroll-behavior: contain; }
#thread { max-width: 780px; margin: 0 auto; padding: 20px 18px 28px; }
.msg { margin: 0 0 22px; }
.msg.user { display: flex; justify-content: flex-end; }
.bubble {
  background: var(--bg-user); border-radius: 14px 14px 4px 14px; padding: 9px 14px; max-width: 86%; white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 12px; }
.md h1, .md h2, .md h3, .md h4, .md h5, .md h6 { margin: 20px 0 8px; line-height: 1.35; font-weight: 600; }
.md h1 { font-size: 1.35em; } .md h2 { font-size: 1.2em; } .md h3 { font-size: 1.08em; } .md h4, .md h5, .md h6 { font-size: 1em; }
.md ul, .md ol { margin: 0 0 12px; padding-left: 1.5em; }
.md li { margin: 3px 0; }
.md li > ul, .md li > ol { margin: 3px 0 0; }
.md code { background: var(--code-bg); border-radius: 5px; padding: .12em .38em; font: 0.88em ui-monospace, SFMono-Regular, Consolas, monospace; }
.md pre { background: var(--code-bg); border-radius: 8px; padding: 12px 14px; overflow-x: auto; margin: 0 0 12px; }
.md pre code { background: none; padding: 0; font-size: 13px; line-height: 1.5; }
.md blockquote { margin: 0 0 12px; padding: 2px 14px; border-left: 3px solid var(--border); color: var(--muted); }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 18px 0; }
.table-wrap { overflow-x: auto; margin: 0 0 12px; }
.md table { border-collapse: collapse; font-size: 14px; min-width: 60%; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; vertical-align: top; }
.md th { background: var(--bg-soft); font-weight: 600; white-space: nowrap; }
.steps { margin: 0 0 10px; font-size: 13px; color: var(--muted); }
.steps summary { cursor: pointer; user-select: none; padding: 2px 0; }
.steps ul { list-style: none; margin: 6px 0 0; padding: 8px 12px; border: 1px solid var(--border-soft); border-radius: 8px; background: var(--bg-soft); }
.steps li { display: flex; gap: 8px; align-items: baseline; padding: 2px 0; }
.step-icon { width: 12px; flex: none; text-align: center; }
.steps li.done .step-icon::before { content: "✓"; color: var(--ok); }
.steps li.error .step-icon::before { content: "!"; color: var(--warn); font-weight: 700; }
.steps li.run .step-icon::before {
  content: ""; display: inline-block; width: 9px; height: 9px; border-radius: 50%; border: 2px solid var(--border);
  border-top-color: var(--accent); animation: spin .8s linear infinite;
}
.step-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.step-sum { flex: none; font-size: 12px; }
.changes { margin: 10px 0 0; border: 1px solid var(--border); border-radius: 8px; padding: 6px 12px; font-size: 13px; }
.change { display: flex; gap: 8px; padding: 3px 0; align-items: baseline; overflow-wrap: anywhere; }
.change-op { flex: none; color: var(--warn); font-weight: 600; }
.status { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; margin-top: 8px; }
.spinner {
  width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--border); border-top-color: var(--accent);
  animation: spin .8s linear infinite; flex: none;
}
.msg > .error { margin-top: 8px; padding: 8px 12px; border-radius: 8px; border: 1px solid var(--danger); color: var(--danger); font-size: 14px; }
.msg-foot { display: flex; gap: 12px; align-items: center; margin-top: 8px; font-size: 12px; }
.empty { text-align: center; padding: 12vh 8px 0; }
.empty h1 { font-size: 24px; margin: 14px 0 6px; font-weight: 600; }
.empty p { margin: 0 0 18px; }
.suggest { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; max-width: 560px; margin: 0 auto; }
.chip { border: 1px solid var(--border); background: var(--bg); border-radius: 10px; padding: 10px 12px; text-align: left; font-size: 14px; }
.chip:hover { background: var(--bg-soft); }

/* 輸入區 */
#composer { padding: 6px 18px calc(10px + env(safe-area-inset-bottom)); }
.box {
  max-width: 780px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px; border: 1px solid var(--border);
  border-radius: 14px; padding: 8px 8px 8px 14px; background: var(--bg); box-shadow: 0 1px 3px rgba(31, 35, 40, .06);
}
.box:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
#input { flex: 1; border: 0; outline: 0; resize: none; background: none; padding: 5px 0; max-height: 220px; line-height: 1.5; font-size: 16px; }
.send {
  width: 34px; height: 34px; border-radius: 9px; border: 0; background: var(--accent); color: var(--accent-text); flex: none;
  display: inline-flex; align-items: center; justify-content: center;
}
.send:disabled { background: var(--border); color: var(--muted); cursor: default; }
body.busy .send { background: var(--text); color: var(--bg); }
.hint { max-width: 780px; margin: 6px auto 0; font-size: 12px; color: var(--muted); text-align: center; }

/* 彈窗、提示 */
#modal { position: fixed; inset: 0; background: rgba(1, 4, 9, .5); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 30; }
.modal-card { background: var(--bg); border: 1px solid var(--border); border-radius: 12px; width: min(520px, 100%); max-height: 82dvh; display: flex; flex-direction: column; box-shadow: var(--shadow); }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 12px 12px 18px; border-bottom: 1px solid var(--border-soft); font-weight: 600; }
.modal-head > div { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#modalBody { padding: 16px 18px 18px; overflow-y: auto; }
.menu-list { display: flex; flex-direction: column; gap: 10px; }
.menu-list p { margin: 0 0 4px; }
.audit-row { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; padding: 8px 0; border-top: 1px solid var(--border-soft); font-size: 13px; }
.audit-op { color: var(--warn); font-weight: 600; }
.audit-path { overflow-wrap: anywhere; }
.audit-time { grid-column: 2; font-size: 12px; }
#toast {
  position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 8px 16px; border-radius: 8px; font-size: 14px; z-index: 40; max-width: 88vw; box-shadow: var(--shadow);
}

/* 登入頁 */
.login-body { background: var(--bg-side); overflow: auto; }
.login { max-width: 340px; margin: 0 auto; padding: 12vh 16px 40px; text-align: center; }
.login h1 { font-size: 22px; font-weight: 400; margin: 14px 0 18px; }
.login-card { text-align: left; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 18px; }
.login-card label { display: block; font-size: 14px; margin: 0 0 6px; }
.login-card input {
  width: 100%; padding: 7px 10px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); margin: 0 0 14px;
  font-size: 16px;
}
.login-card input:focus { outline: 0; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
.login-error { color: var(--danger); font-size: 14px; margin: -4px 0 12px; }
.login-note { color: var(--muted); font-size: 12px; margin-top: 16px; }

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 50% { opacity: .35; } }

/* 手機 */
@media (max-width: 820px) {
  #side {
    position: fixed; inset: 0 auto 0 0; width: min(300px, 84vw); z-index: 20; transform: translateX(-102%);
    transition: transform .2s ease; padding-top: env(safe-area-inset-top);
  }
  body.side-open #side { transform: none; box-shadow: var(--shadow); }
  body.side-open #scrim { display: block; position: fixed; inset: 0; background: rgba(1, 4, 9, .45); z-index: 15; }
  #menuBtn { display: inline-flex; }
  #thread { padding: 16px 14px 24px; }
  #composer { padding-left: 10px; padding-right: 10px; }
  .conv-more { opacity: 1; }
  .suggest { grid-template-columns: 1fr; }
  .bubble { max-width: 92%; }
  .hint { display: none; }
  .empty { padding-top: 8vh; }
}
