:root {
  --bg: #fafaf9;
  --surface: #ffffff;
  --surface-2: #f7f6f4;
  --border: #e9e7e2;
  --border-strong: #d8d5cf;
  --text: #1c1b19;
  --muted: #78756f;
  --faint: #9c9992;
  --accent: #2f6f4f;
  --accent-soft: #eaf3ee;
  --accent-line: #c5ded0;
  --warn: #b4791f;
  --warn-soft: #fdf3e2;
  --danger: #b3453c;
  --danger-soft: #fbeceb;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(20, 18, 14, .04), 0 4px 16px rgba(20, 18, 14, .04);
}

* { box-sizing: border-box; }

/* `hidden` 屬性要贏過下面啲 display:flex 規則，否則一啲 form / row 收唔埋 */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang HK",
    "Microsoft JhengHei", "Noto Sans CJK TC", sans-serif;
  font-size: 14px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }

/* ---------------- layout ---------------- */

.app { display: flex; min-height: 100vh; }

.sidebar {
  width: 232px;
  flex: none;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px 14px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand { font-weight: 700; font-size: 16px; letter-spacing: -0.02em; padding: 0 8px; }
.brand span { color: var(--accent); }

.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  border: none;
  background: transparent;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--muted);
  font-size: 14px;
  font-family: inherit;
  text-align: left;
  transition: background .12s, color .12s;
}
.nav-item svg {
  width: 16px; height: 16px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 1.6;
  stroke-linecap: round; stroke-linejoin: round;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.side-foot { margin-top: auto; display: flex; flex-direction: column; gap: 6px; padding: 0 10px; }
.side-foot .kv { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); }
.side-foot .kv b { color: var(--text); font-weight: 550; }
.side-foot .kv b.warn { color: var(--warn); }

.main { flex: 1; min-width: 0; padding: 28px 32px 64px; }
.main > .tab { display: none; }
.main > .tab.active { display: block; animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(2px); } to { opacity: 1; } }

.page-head { margin-bottom: 20px; }
.page-head h1 { font-size: 20px; }
.page-head .sub { margin: 4px 0 0; color: var(--muted); font-size: 13px; }

.grid-2 { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 20px; align-items: start; }
.col { display: flex; flex-direction: column; gap: 16px; min-width: 0; }

/* ---------------- card ---------------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
}
.card + .card { margin-top: 0; }
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: 12px;
}
.card-head h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); }
.head-tools { display: flex; align-items: center; gap: 8px; }

.count-pill {
  font-size: 11px; padding: 2px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}

/* ---------------- controls ---------------- */

/* 所有「文字類」輸入框（text/number/email/password/search/url/tel/date… 同冇寫 type 嘅）
   都用同一套樣；只排除 checkbox/radio/button 呢類唔應該有框嘅 */
input:not([type="checkbox"], [type="radio"], [type="submit"], [type="button"], [type="reset"], [type="range"], [type="color"], [type="file"], [type="image"]),
textarea,
select {
  width: 100%;
  padding: 8px 11px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  transition: border-color .12s, box-shadow .12s;
}
textarea { resize: vertical; line-height: 1.65; }
select { cursor: pointer; }
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
input::placeholder, textarea::placeholder { color: var(--faint); }

.filter { max-width: 180px; padding: 5px 10px; font-size: 13px; }

.row { display: flex; gap: 10px; align-items: flex-end; }
.row > * { flex: 1; min-width: 0; }
.stack { display: flex; flex-direction: column; gap: 10px; }

.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field.sm { max-width: 120px; }
.label { font-size: 12px; color: var(--muted); }

button.btn {
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
  font-weight: 550;
  transition: filter .12s, background .12s;
  white-space: nowrap;
}
button.btn:hover { filter: brightness(1.06); }
button.btn.ghost { background: var(--surface); color: var(--text); border-color: var(--border-strong); font-weight: 500; }
button.btn.ghost:hover { background: var(--surface-2); }
button.btn.danger { background: var(--surface); color: var(--danger); border-color: #e8cdca; font-weight: 500; }
button.btn.danger:hover { background: var(--danger-soft); }
button.btn.sm { padding: 5px 12px; font-size: 13px; }
button.btn.block { width: 100%; }
button.btn:disabled { opacity: .5; cursor: default; }

.icon-btn {
  border: none; background: transparent; cursor: pointer;
  color: var(--muted); font-size: 15px; line-height: 1; padding: 4px 6px; border-radius: 6px;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.checks { display: flex; flex-direction: column; gap: 7px; }
.checks.inline { flex-direction: row; flex-wrap: wrap; gap: 8px 18px; }
.checks label, .check-inline { display: flex; gap: 8px; align-items: center; font-size: 13px; cursor: pointer; }
.checks input, .check-inline input { accent-color: var(--accent); width: auto; }

/* ---------------- list ---------------- */

.list { display: flex; flex-direction: column; gap: 2px; }
.list-item {
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  border: 1px solid transparent;
}
.list-item:hover { background: var(--surface-2); }
.list-item.selected { background: var(--accent-soft); border-color: var(--accent-line); color: var(--accent); font-weight: 600; }
.list-item .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-item .count { font-size: 12px; color: var(--muted); flex: none; }
.list-item.selected .count { color: var(--accent); }

.empty { padding: 18px 4px; color: var(--faint); font-size: 13px; text-align: center; }

/* ---------------- table ---------------- */

table.docs { width: 100%; border-collapse: collapse; }
table.docs th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .05em;
  color: var(--muted); font-weight: 600; border-bottom: 1px solid var(--border); padding: 6px 8px;
}
table.docs td { padding: 10px 8px; border-bottom: 1px solid #f2f1ee; vertical-align: top; }
table.docs tr:last-child td { border-bottom: none; }
table.docs tr:hover td { background: #fbfaf8; }
.doc-title { font-weight: 550; word-break: break-word; }
.doc-sub { color: var(--faint); font-size: 12px; }
.tag {
  font-size: 11px; padding: 1px 7px; border-radius: 999px;
  border: 1px solid var(--border); color: var(--muted); background: var(--surface-2);
}
.tag.green { border-color: var(--accent-line); color: var(--accent); background: var(--accent-soft); }
.tag.warm { border-color: #ecd7ac; color: var(--warn); background: var(--warn-soft); }

/* ---------------- dropzone ---------------- */

.dropzone {
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 26px 18px;
  text-align: center;
  color: var(--muted);
  background: var(--surface-2);
  cursor: pointer;
  font-size: 13px;
  transition: border-color .12s, background .12s, color .12s;
}
.dropzone strong { color: var(--text); font-weight: 600; }
.dropzone:hover, .dropzone.over { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.dropzone.over strong { color: var(--accent); }

/* ---------------- chat ---------------- */

.chat-card { display: flex; flex-direction: column; gap: 0; padding: 0; overflow: hidden; }
.chat-bar {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
}
.chat-bar select { max-width: 320px; flex: none; }
.chat-bar .hint { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.chat-log {
  display: flex; flex-direction: column; gap: 14px;
  padding: 18px 16px; min-height: 260px; max-height: 60vh; overflow-y: auto;
  background: var(--surface);
}
.msg { max-width: 88%; padding: 11px 14px; border-radius: var(--radius); background: var(--surface-2); border: 1px solid var(--border); }
.msg.user { background: var(--accent-soft); border-color: var(--accent-line); align-self: flex-end; }
.msg.assistant { align-self: flex-start; background: var(--surface); }
.msg .who { font-size: 11px; color: var(--muted); margin-bottom: 5px; }
.msg .body { white-space: pre-wrap; word-break: break-word; font-size: 14px; }
.msg .body p { margin: 0 0 8px; }
.msg .body p:last-child { margin-bottom: 0; }
.msg .body ul, .msg .body ol { margin: 0 0 8px; padding-left: 20px; }
.msg .body code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 12.5px; }
.msg .body pre { background: var(--surface-2); padding: 10px; border-radius: var(--radius-sm); overflow-x: auto; }
.msg .body pre code { background: none; padding: 0; }
.msg .body strong { font-weight: 650; }

.composer { display: flex; gap: 10px; padding: 12px 16px; border-top: 1px solid var(--border); align-items: flex-end; }
.composer textarea { flex: 1; resize: none; max-height: 140px; }
.composer .btn { flex: none; }

.steps { margin-top: 10px; border-top: 1px dashed var(--border); padding-top: 8px; display: flex; flex-direction: column; gap: 4px; }
.step { font-size: 12px; color: var(--muted); }
.step summary { cursor: pointer; display: flex; gap: 8px; align-items: baseline; list-style: none; }
.step summary::-webkit-details-marker { display: none; }
.step summary::before { content: "▸"; color: var(--faint); font-size: 10px; }
.step[open] summary::before { content: "▾"; }
.step .tool { font-family: ui-monospace, Menlo, monospace; color: var(--accent); font-weight: 600; }
.step .args { color: var(--faint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.step pre {
  margin: 6px 0 0; padding: 9px 10px; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  max-height: 260px; overflow: auto; white-space: pre-wrap; word-break: break-word;
  font-size: 12px; color: var(--text);
}
.read-docs { margin-top: 8px; display: flex; flex-wrap: wrap; gap: 6px; }

.thinking { color: var(--muted); font-size: 13px; display: flex; align-items: center; gap: 8px; }
.dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: blink 1s infinite; }
@keyframes blink { 0%, 100% { opacity: .25 } 50% { opacity: 1 } }

/* ---------------- recall ---------------- */

.seg { display: inline-flex; gap: 2px; padding: 3px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; margin: 16px 0; }
.seg-item {
  border: none; background: transparent; padding: 6px 14px; border-radius: 8px;
  cursor: pointer; font-family: inherit; font-size: 13px; color: var(--muted);
}
.seg-item.active { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }

.recall-panel { display: none; }
.recall-panel.active { display: block; }
.recall-panel > .card { margin-bottom: 16px; }

.metrics { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.metric {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 8px 14px; min-width: 92px; box-shadow: var(--shadow);
}
.metric .k { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.metric .v { font-size: 19px; font-weight: 650; letter-spacing: -0.02em; }
.metric.good .v { color: var(--accent); }
.metric.bad .v { color: var(--danger); }
.metric.mid .v { color: var(--warn); }

.hit {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); padding: 12px 14px; margin-bottom: 10px; box-shadow: var(--shadow);
}
.hit.is-expected { border-color: var(--accent-line); background: linear-gradient(0deg, var(--accent-soft), var(--accent-soft)) no-repeat, var(--surface); }
.hit-top { display: flex; align-items: baseline; gap: 10px; }
.hit-rank {
  font-family: ui-monospace, Menlo, monospace; font-size: 12px; color: var(--muted);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px;
  padding: 1px 7px; flex: none;
}
.hit-title { font-weight: 600; word-break: break-word; flex: 1; }
.hit-score { font-size: 12px; color: var(--muted); flex: none; font-family: ui-monospace, Menlo, monospace; }
.bar { height: 3px; background: var(--surface-2); border-radius: 2px; margin: 8px 0; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 2px; }
.snippet { font-size: 13px; color: #4a4843; white-space: pre-wrap; word-break: break-word; }
.snippet mark { background: #fff2b8; color: inherit; padding: 0 1px; border-radius: 2px; }
.hit-foot { display: flex; gap: 8px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.hit pre.content {
  margin: 10px 0 0; padding: 10px 12px; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); max-height: 320px; overflow: auto;
  white-space: pre-wrap; word-break: break-word; font-size: 12.5px;
}

table.report { width: 100%; border-collapse: collapse; }
table.report th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .05em;
  color: var(--muted); border-bottom: 1px solid var(--border); padding: 7px 8px; white-space: nowrap;
}
table.report td { padding: 8px; border-bottom: 1px solid #f2f1ee; font-size: 13px; vertical-align: top; }
table.report tr.miss td { background: #fdf7f7; }
table.report .q { min-width: 200px; word-break: break-word; }
.mini { font-size: 12px; color: var(--muted); }

/* ---------------- misc ---------------- */

.hint { color: var(--muted); font-size: 12px; }
.mini { font-size: 12px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.flash {
  padding: 9px 13px; border-radius: var(--radius-sm); background: var(--warn-soft);
  color: var(--warn); font-size: 13px; margin-bottom: 16px; border: 1px solid #f0dfbe;
}

/* ---------------- auth gate ---------------- */

.auth-gate {
  position: fixed; inset: 0; z-index: 100;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.auth-gate.hidden { display: none; }
.auth-box {
  width: min(390px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 26px 26px 22px;
  box-shadow: 0 10px 40px rgba(20, 18, 14, .08);
}
.brand.big { font-size: 20px; text-align: center; }
.auth-sub { text-align: center; color: var(--muted); font-size: 13px; margin: 4px 0 16px; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-gate .seg { display: flex; width: 100%; margin: 0 0 16px; }
.auth-gate .seg-item { flex: 1; text-align: center; }
.auth-hint { margin-top: 12px; text-align: center; min-height: 18px; }

.toasts { position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 120; }
.toast {
  background: var(--text); color: #fff; padding: 9px 14px; border-radius: var(--radius-sm);
  font-size: 13px; box-shadow: 0 6px 20px rgba(0,0,0,.15); animation: slide .18s ease; max-width: 380px;
}
.toast.ok { background: var(--accent); }
.toast.err { background: var(--danger); }
@keyframes slide { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; } }

.modal { position: fixed; inset: 0; background: rgba(28,27,25,.35); display: flex; align-items: center; justify-content: center; z-index: 60; padding: 32px; }
.modal.hidden { display: none; }
.modal-box {
  background: var(--surface); border-radius: var(--radius); width: min(860px, 100%);
  max-height: 100%; display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.2);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-head h3 { font-size: 14px; word-break: break-word; }
.modal-body { padding: 16px 18px; overflow: auto; }
.modal-body pre.content {
  margin: 0; white-space: pre-wrap; word-break: break-word; font-size: 13px; line-height: 1.7;
  font-family: inherit;
}

/* ---------------- responsive ---------------- */

@media (max-width: 1000px) {
  .grid-2 { grid-template-columns: 1fr; }
}
@media (max-width: 780px) {
  .app { flex-direction: column; }
  .sidebar {
    width: 100%; height: auto; flex-direction: row; align-items: center;
    gap: 12px; border-right: none; border-bottom: 1px solid var(--border);
    padding: 10px 14px; position: sticky; top: 0; z-index: 20;
  }
  .nav { flex-direction: row; gap: 4px; overflow-x: auto; }
  .nav-item { white-space: nowrap; }
  .nav-item svg { display: none; }
  .side-foot { display: none; }
  .main { padding: 20px 16px 48px; }
  .msg { max-width: 96%; }
}
