:root {
  --bg: #f7f7f5;
  --panel: #ffffff;
  --text: #1d1d1b;
  --muted: #6b6b66;
  --border: #e2e1dc;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --user-bubble: #e8f0fe;
  --code-bg: #f0efeb;
  --warn: #9a6700;
  --error: #c62828;
  --cost: #b3261e;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161615;
    --panel: #1f1f1d;
    --text: #e8e6e1;
    --muted: #9b9a94;
    --border: #34332f;
    --accent: #4c8dff;
    --user-bubble: #23324d;
    --code-bg: #2a2926;
    --warn: #e0b341;
    --error: #ef7b7b;
    --cost: #ff8a80;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 system-ui, -apple-system, "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
}
a { color: var(--accent); }
button, input, textarea { font: inherit; color: inherit; }
button {
  background: var(--accent); color: var(--accent-text); border: 0; border-radius: 8px;
  padding: 8px 14px; cursor: pointer;
}
button:disabled { opacity: .5; cursor: default; }
.link-btn, .icon-btn { background: none; color: var(--accent); padding: 4px 6px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.warn { color: var(--warn); font-size: 14px; }
.error { color: var(--error); }
[hidden] { display: none !important; }

.center-view { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 24px; max-width: 420px; width: 100%; }
.card h1 { margin-top: 0; font-size: 22px; }
.card form { display: flex; flex-direction: column; gap: 8px; }
.card input { padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); }

#app-view { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 8px 16px;
  border-bottom: 1px solid var(--border); background: var(--panel); flex-wrap: wrap;
}
.brand { font-weight: 600; }
.costs { display: flex; gap: 14px; margin-left: auto; font-size: 14px; flex-wrap: wrap; }
.costs b { color: var(--cost); font-variant-numeric: tabular-nums; }
.user { display: flex; align-items: center; gap: 8px; font-size: 14px; }
#toggle-sidebar { display: none; }

.layout { flex: 1; display: flex; min-height: 0; }
aside#sidebar {
  width: 260px; border-right: 1px solid var(--border); background: var(--panel);
  padding: 12px; overflow-y: auto; flex-shrink: 0;
}
#new-conv { width: 100%; margin-bottom: 12px; }
#conv-list { list-style: none; margin: 0; padding: 0; }
#conv-list li {
  padding: 8px 10px; border-radius: 8px; cursor: pointer; font-size: 14px;
  display: flex; justify-content: space-between; gap: 8px;
}
#conv-list li:hover { background: var(--bg); }
#conv-list li.active { background: var(--user-bubble); }
#conv-list .t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#conv-list .c { color: var(--muted); font-size: 12px; white-space: nowrap; }

main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#chat { flex: 1; overflow-y: auto; padding: 16px; }
.turn { max-width: 860px; margin: 0 auto 28px; }
.question {
  background: var(--user-bubble); border-radius: 12px; padding: 10px 14px;
  margin-left: auto; width: fit-content; max-width: 85%; white-space: pre-wrap;
}
.process { margin: 10px 0; font-size: 13px; color: var(--muted); }
.process details { border-left: 2px solid var(--border); padding-left: 10px; }
.process summary { cursor: pointer; }
.thinking-text { white-space: pre-wrap; margin: 4px 0 8px; }
.tool { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; margin: 2px 0; word-break: break-all; }
.tool.err { color: var(--error); }
.status-line { font-size: 13px; color: var(--muted); margin: 6px 0; }
.answer { overflow-wrap: anywhere; }
.answer table { border-collapse: collapse; margin: 8px 0; display: block; overflow-x: auto; }
.answer th, .answer td { border: 1px solid var(--border); padding: 4px 8px; }
.answer pre { background: var(--code-bg); padding: 10px; border-radius: 8px; overflow-x: auto; }
.answer code { background: var(--code-bg); padding: 1px 4px; border-radius: 4px; }
.turn-cost { font-size: 13px; color: var(--muted); border-top: 1px dashed var(--border); padding-top: 6px; margin-top: 10px; }
.turn-cost b { color: var(--cost); }
.empty-hint { max-width: 640px; margin: 10vh auto 0; text-align: center; }

#ask-form { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); background: var(--panel); }
#question {
  flex: 1; resize: none; padding: 8px 10px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--bg);
}

@media (max-width: 760px) {
  #toggle-sidebar { display: inline-block; }
  aside#sidebar { position: fixed; top: 0; bottom: 0; left: 0; z-index: 10; transform: translateX(-100%); transition: transform .2s; }
  aside#sidebar.open { transform: none; }
  .costs { order: 5; width: 100%; margin-left: 0; gap: 10px; font-size: 13px; }
  #user-email { display: none; }
  #chat { padding: 12px 16px; }
}

/* ---- 後台 ---- */
body.admin { height: auto; }
.admin .topbar a.brand { text-decoration: none; }
.admin-body { max-width: 1200px; margin: 0 auto; padding: 16px; }
.admin-body h2 { margin: 24px 0 8px; font-size: 18px; }
.invite-form { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.invite-form input { padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel); }
.invite-form #inv-email { flex: 1 1 220px; }
.invite-form #inv-note { flex: 2 1 240px; }
.invite-form #inv-days { width: 70px; }
.table-wrap { overflow-x: auto; }
.admin table { border-collapse: collapse; width: 100%; font-size: 14px; background: var(--panel); }
.admin th, .admin td { border-bottom: 1px solid var(--border); padding: 6px 8px; text-align: left; white-space: nowrap; }
.admin th { color: var(--muted); font-weight: 500; }
.admin tr.dim td { color: var(--muted); }
.admin td .link-btn { padding: 0 4px; }
#user-convs-list { padding-left: 20px; font-size: 14px; }
.readonly-banner { max-width: 860px; margin: 0 auto 16px; padding: 8px 12px; border-radius: 8px; background: var(--code-bg); font-size: 14px; }

/* ---- 分享 ---- */
.share-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 8px 16px; border-bottom: 1px solid var(--border); background: var(--panel); font-size: 14px; }
.share-bar input { flex: 1 1 260px; min-width: 0; padding: 4px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); font-size: 13px; }
.share-bar .share-note { flex-basis: 100%; }
.link-btn.danger { color: var(--error); }
.share-header { max-width: 860px; margin: 0 auto 20px; }
.share-header h1 { font-size: 20px; margin: 0 0 4px; }
.share-footer { max-width: 860px; margin: 24px auto; font-size: 13px; }

/* ---- 模型選擇 ---- */
.model-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 6px 16px 0; font-size: 13px; background: var(--panel); border-top: 1px solid var(--border); }
.model-row select { padding: 3px 6px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); }
.model-row + #ask-form { border-top: 0; }
