:root {
  --bg: #f5f6fa;
  --panel: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #4f46e5;
  --accent-strong: #4338ca;
  --accent-soft: #eef2ff;
  --green: #059669;
  --amber: #b45309;
  --red: #dc2626;
  --sidebar: #0f172a;
  --sidebar-soft: #1e293b;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 8px 24px rgba(15, 23, 42, 0.05);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.92em; background: #f3f4f6; padding: 1px 5px; border-radius: 5px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------------------------------------------------------------- layout */
.app { display: flex; min-height: 100vh; }
.sidebar {
  position: sticky;
  top: 0;
  display: flex;
  flex-direction: column;
  width: 248px;
  height: 100vh;
  flex: 0 0 auto;
  padding: 22px 16px 16px;
  color: #e2e8f0;
  background: var(--sidebar);
}
.brand { display: flex; align-items: center; gap: 11px; padding: 0 6px 22px; }
.logo {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  border-radius: 12px;
  background: linear-gradient(135deg, #6366f1, #4338ca);
  color: #fff;
  font-size: 18px;
  font-weight: 700;
  box-shadow: 0 6px 16px rgba(79, 70, 229, 0.4);
}
.brand strong { display: block; color: #fff; font-size: 15px; letter-spacing: 0.3px; }
.brand small { display: block; margin-top: 1px; color: #94a3b8; font-size: 10.5px; letter-spacing: 1.2px; text-transform: uppercase; }
.site-picker { padding: 12px; border-radius: 12px; background: var(--sidebar-soft); margin-bottom: 18px; }
.site-picker label { display: block; margin-bottom: 6px; color: #94a3b8; font-size: 11px; }
.site-picker-row { display: flex; gap: 6px; }
.site-picker select {
  width: 100%;
  min-width: 0;
  padding: 8px 9px;
  border: 1px solid #334155;
  border-radius: 8px;
  background: #0f172a;
  color: #f1f5f9;
  outline: none;
}
.icon-btn {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  flex: 0 0 auto;
  border: 1px solid #334155;
  border-radius: 8px;
  background: #1e293b;
  color: #cbd5e1;
  font-size: 18px;
  cursor: pointer;
}
.icon-btn:hover { border-color: #6366f1; color: #fff; }
.nav { display: flex; flex-direction: column; gap: 3px; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: #cbd5e1;
  font-size: 13.5px;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}
.nav-item::before { content: ""; width: 5px; height: 5px; border-radius: 50%; background: #475569; }
.nav-item:hover { background: rgba(148, 163, 184, 0.1); color: #fff; }
.nav-item.active { background: rgba(99, 102, 241, 0.18); color: #fff; }
.nav-item.active::before { background: #818cf8; box-shadow: 0 0 0 3px rgba(129, 140, 248, 0.18); }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 8px; padding: 14px 6px 0; border-top: 1px solid #1e293b; }
.link-btn { padding: 0; border: 0; background: none; color: #94a3b8; text-align: left; font-size: 12px; cursor: pointer; }
.link-btn:hover { color: #fff; }
.main { min-width: 0; flex: 1; padding: 26px 30px 60px; }
.topbar { display: flex; align-items: flex-start; justify-content: space-between; gap: 18px; margin-bottom: 22px; }
.topbar h1 { margin: 0; font-size: 23px; letter-spacing: -0.3px; }
.topbar p { margin: 5px 0 0; font-size: 13px; }
.top-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.view { max-width: 1240px; }

/* ---------------------------------------------------------------- buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 7px 14px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fff;
  color: #374151;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}
.btn:hover { border-color: #cbd5e1; background: #f9fafb; }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn-primary { border-color: var(--accent); background: var(--accent); color: #fff; box-shadow: 0 4px 12px rgba(79, 70, 229, 0.18); }
.btn-primary:hover { border-color: var(--accent-strong); background: var(--accent-strong); }
.btn-danger { border-color: #fecaca; background: #fef2f2; color: var(--red); }
.btn-danger:hover { border-color: #fca5a5; background: #fee2e2; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn-ghost:hover { background: #f3f4f6; color: var(--text); }
.btn-sm { min-height: 30px; padding: 4px 10px; font-size: 12px; border-radius: 7px; }
.btn-block { width: 100%; }

/* ---------------------------------------------------------------- cards */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 18px; border-bottom: 1px solid var(--line); }
.card-head h2, .card-head h3 { margin: 0; font-size: 15px; }
.card-body { padding: 18px; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; align-items: center; gap: 10px; }
.row-wrap { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.spacer { flex: 1; }

/* ---------------------------------------------------------------- stats */
.stat { padding: 18px; }
.stat-label { color: var(--muted); font-size: 12px; }
.stat-value { margin-top: 6px; font-size: 28px; font-weight: 700; letter-spacing: -0.8px; }
.stat-foot { margin-top: 4px; color: var(--muted); font-size: 11.5px; }
.stat-accent { color: var(--accent); }
.chart { display: flex; align-items: flex-end; gap: 6px; height: 150px; padding-top: 10px; }
.chart-col { display: flex; flex: 1; flex-direction: column; align-items: center; justify-content: flex-end; gap: 6px; min-width: 0; height: 100%; }
.chart-bar { width: 100%; max-width: 26px; border-radius: 5px 5px 2px 2px; background: linear-gradient(180deg, #818cf8, #4f46e5); min-height: 2px; transition: height 0.3s ease; }
.chart-bar.muted { background: #e0e7ff; }
.chart-label { color: #9ca3af; font-size: 9.5px; white-space: nowrap; }
.chart-values { color: #6b7280; font-size: 10px; }

/* ---------------------------------------------------------------- tables */
.table-wrap { overflow-x: auto; }
table.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th {
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 11.5px;
  font-weight: 600;
  text-align: left;
  white-space: nowrap;
  background: #fafbfc;
}
.table td { padding: 11px 14px; border-bottom: 1px solid #f1f2f5; vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: #fafbff; }
.table tbody tr.clickable { cursor: pointer; }
.cell-title { font-weight: 600; color: #111827; }
.cell-sub { margin-top: 2px; color: var(--muted); font-size: 11.5px; max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { padding: 46px 20px; text-align: center; color: var(--muted); }
.empty strong { display: block; margin-bottom: 6px; color: #374151; font-size: 15px; }

/* ---------------------------------------------------------------- tags */
.tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid color-mix(in srgb, var(--tag-color, #6366f1) 35%, #fff);
  background: color-mix(in srgb, var(--tag-color, #6366f1) 10%, #fff);
  color: color-mix(in srgb, var(--tag-color, #6366f1) 75%, #111);
  font-size: 11px;
  font-weight: 500;
  line-height: 1.6;
  white-space: nowrap;
}
.tag-x { border: 0; background: transparent; color: inherit; padding: 0 0 0 2px; cursor: pointer; font-size: 13px; line-height: 1; opacity: 0.65; }
.tag-x:hover { opacity: 1; }
.tags { display: flex; flex-wrap: wrap; gap: 5px; }
.status {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 500;
  white-space: nowrap;
}
.status::before { content: ""; width: 5px; height: 5px; border-radius: 50%; background: currentColor; }
.status-open { background: #ecfdf5; color: #047857; }
.status-handoff { background: #fff7ed; color: #c2410c; }
.status-resolved { background: #f1f5f9; color: #475569; }
.badge { display: inline-flex; align-items: center; padding: 1px 7px; border-radius: 6px; background: #f3f4f6; color: #4b5563; font-size: 11px; }
.badge-warn { background: #fff7ed; color: #c2410c; }

/* ---------------------------------------------------------------- forms */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field > span, .field > label { color: #374151; font-size: 12.5px; font-weight: 500; }
.field .hint { color: var(--muted); font-size: 11.5px; font-weight: 400; line-height: 1.5; }
input[type="text"], input[type="password"], input[type="email"], input[type="number"], input[type="url"], select, textarea {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid #d8dbe2;
  border-radius: 9px;
  background: #fff;
  color: var(--text);
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1); }
textarea { resize: vertical; min-height: 82px; line-height: 1.6; }
textarea.code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; line-height: 1.6; min-height: 160px; }
input[type="color"] { width: 46px; height: 38px; padding: 3px; border: 1px solid #d8dbe2; border-radius: 8px; background: #fff; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; color: #374151; cursor: pointer; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 16px; }
.form-grid .span-2 { grid-column: span 2; }
.form-error { margin: 0 0 10px; padding: 8px 11px; border-radius: 8px; background: #fef2f2; border: 1px solid #fecaca; color: #b91c1c; font-size: 12.5px; }
.form-ok { padding: 8px 11px; border-radius: 8px; background: #ecfdf5; border: 1px solid #a7f3d0; color: #047857; font-size: 12.5px; }
.toolbar { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--line); background: #fcfcfd; }
.toolbar .field { margin: 0; min-width: 130px; }
.toolbar .field.grow { flex: 1; min-width: 180px; }
.toolbar .btn { min-height: 37px; }
.filters { display: grid; grid-template-columns: 150px 150px 1fr auto; gap: 10px; align-items: end; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.filters .field { margin: 0; }
.pagination { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); }
.help { color: var(--muted); font-size: 12px; line-height: 1.65; }
.danger-zone { border-color: #fecaca; }
.danger-zone .card-head h3 { color: #b91c1c; }
.kbd { padding: 1px 5px; border: 1px solid #d1d5db; border-bottom-width: 2px; border-radius: 5px; background: #f9fafb; font-size: 11px; }

/* ---------------------------------------------------------------- login */
.login-screen { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: radial-gradient(800px 400px at 50% -10%, #e0e7ff, transparent 70%), var(--bg); }
.login-card { width: 100%; max-width: 400px; padding: 30px; background: #fff; border: 1px solid var(--line); border-radius: 18px; box-shadow: 0 20px 60px rgba(15, 23, 42, 0.1); }
.login-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 24px; font-weight: 600; color: #374151; }
.login-card h1 { margin: 0 0 6px; font-size: 21px; letter-spacing: -0.3px; }
.login-card > p { margin: 0 0 20px; font-size: 13px; }
.login-foot { margin: 18px 0 0; padding-top: 14px; border-top: 1px solid var(--line); color: var(--muted); font-size: 11.5px; line-height: 1.6; }

/* ---------------------------------------------------------------- drawer */
.drawer-backdrop { position: fixed; inset: 0; z-index: 800; background: rgba(15, 23, 42, 0.4); backdrop-filter: blur(2px); animation: fade 0.18s ease; }
.drawer {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 810;
  display: flex;
  flex-direction: column;
  width: min(560px, 100vw);
  height: 100%;
  background: #fff;
  box-shadow: -20px 0 60px rgba(15, 23, 42, 0.18);
  animation: slide-in 0.22s ease;
}
.drawer-head { display: flex; align-items: flex-start; gap: 12px; padding: 18px 20px; border-bottom: 1px solid var(--line); }
.drawer-head h2 { margin: 0; font-size: 17px; }
.drawer-head .sub { margin-top: 3px; color: var(--muted); font-size: 12px; }
.drawer-body { flex: 1; overflow-y: auto; padding: 18px 20px 30px; }
.drawer-foot { padding: 12px 20px; border-top: 1px solid var(--line); background: #fafbfc; }
@keyframes slide-in { from { transform: translateX(24px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.section { margin-bottom: 22px; }
.section-title { margin: 0 0 10px; color: #374151; font-size: 12px; font-weight: 600; letter-spacing: 0.3px; text-transform: uppercase; }
.kv { display: grid; grid-template-columns: 92px 1fr; gap: 7px 10px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }
.note-box { padding: 10px 12px; border-radius: 9px; background: #fffbeb; border: 1px solid #fde68a; color: #92400e; font-size: 12.5px; line-height: 1.6; }

/* ---------------------------------------------------------------- chat log */
.chat-log { display: flex; flex-direction: column; gap: 11px; }
.chat-item { display: flex; flex-direction: column; gap: 4px; }
.chat-item.user { align-items: flex-end; }
.chat-item .who { color: var(--muted); font-size: 10.5px; }
.chat-item .bubble { max-width: 88%; padding: 9px 12px; border-radius: 12px; background: #f3f4f6; font-size: 13px; line-height: 1.65; white-space: pre-wrap; word-break: break-word; }
.chat-item.user .bubble { background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.chat-item.assistant .bubble { border-bottom-left-radius: 4px; }
.chat-item.tool .bubble { background: #f8fafc; border: 1px dashed #cbd5e1; color: #475569; font-size: 11.5px; border-radius: 8px; max-width: 100%; }
.chat-meta { color: #9ca3af; font-size: 10px; }
.chat-cites { margin-top: 6px; color: #6b7280; font-size: 11px; }
.chat-actions { margin-top: 6px; display: flex; flex-wrap: wrap; gap: 4px; }
.action-pill { padding: 1px 7px; border-radius: 5px; background: #eef2ff; color: #4338ca; font-size: 10.5px; }

/* ---------------------------------------------------------------- modal */
.modal-backdrop { position: fixed; inset: 0; z-index: 900; display: grid; place-items: center; padding: 20px; background: rgba(15, 23, 42, 0.45); backdrop-filter: blur(2px); animation: fade 0.15s ease; }
.modal { width: min(680px, 100%); max-height: calc(100vh - 40px); display: flex; flex-direction: column; background: #fff; border-radius: 16px; box-shadow: 0 30px 80px rgba(15, 23, 42, 0.28); animation: pop 0.18s ease; }
.modal.wide { width: min(860px, 100%); }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-body { flex: 1; overflow-y: auto; padding: 18px 20px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 13px 20px; border-top: 1px solid var(--line); background: #fafbfc; border-radius: 0 0 16px 16px; }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(0.985); } to { opacity: 1; transform: none; } }
.modal .close-x, .drawer .close-x { border: 0; background: none; color: var(--muted); font-size: 20px; line-height: 1; cursor: pointer; padding: 2px 6px; }

/* ---------------------------------------------------------------- toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  z-index: 1000;
  max-width: min(480px, calc(100vw - 40px));
  padding: 10px 16px;
  border-radius: 10px;
  background: #111827;
  color: #fff;
  font-size: 13px;
  box-shadow: 0 12px 30px rgba(15, 23, 42, 0.25);
  animation: pop 0.18s ease;
}
.toast.error { background: #b91c1c; }

/* ---------------------------------------------------------------- misc */
.code-block { position: relative; padding: 14px 16px; border-radius: 10px; background: #0f172a; color: #e2e8f0; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; line-height: 1.65; white-space: pre-wrap; word-break: break-all; }
.code-block .copy { position: absolute; top: 8px; right: 8px; }
.steps { margin: 0; padding-left: 20px; color: #4b5563; font-size: 13px; line-height: 1.9; }
.steps strong { color: #111827; }
.question-list { display: flex; flex-direction: column; gap: 9px; }
.question-item { display: flex; align-items: center; gap: 10px; font-size: 12.5px; }
.question-item .q { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.question-item .bar { width: 90px; height: 5px; border-radius: 999px; background: #eef2ff; overflow: hidden; }
.question-item .bar i { display: block; height: 100%; border-radius: 999px; background: var(--accent); }
.question-item .count { width: 30px; text-align: right; color: var(--muted); font-size: 11px; }
.kb-result { padding: 11px 12px; border: 1px solid var(--line); border-radius: 9px; margin-bottom: 8px; }
.kb-result h4 { margin: 0 0 4px; font-size: 12.5px; }
.kb-result p { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.6; }
.kb-result .score { float: right; color: var(--accent); font-size: 11px; font-weight: 600; }
.split { display: grid; grid-template-columns: 1.15fr 0.85fr; gap: 16px; }
.list-reset { margin: 0; padding: 0; list-style: none; }
.mini-list li { display: flex; justify-content: space-between; gap: 10px; padding: 7px 0; border-bottom: 1px dashed #eef0f3; font-size: 12.5px; }
.mini-list li:last-child { border-bottom: 0; }

@media (max-width: 1100px) {
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .split { grid-template-columns: 1fr; }
}
@media (max-width: 900px) {
  .app { flex-direction: column; }
  .sidebar { position: static; width: 100%; height: auto; padding: 14px; }
  .brand { padding-bottom: 12px; }
  .site-picker { margin-bottom: 10px; }
  .nav { flex-direction: row; overflow-x: auto; padding-bottom: 4px; }
  .nav-item { white-space: nowrap; }
  .nav-item::before { display: none; }
  .sidebar-foot { margin-top: 10px; flex-direction: row; align-items: center; justify-content: space-between; padding-top: 10px; }
  .main { padding: 18px 16px 50px; }
  .topbar { flex-direction: column; gap: 10px; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .filters { grid-template-columns: 1fr 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .span-2 { grid-column: span 1; }
}
@media (max-width: 560px) {
  .grid-4 { grid-template-columns: 1fr; }
  .filters { grid-template-columns: 1fr; }
  .drawer { width: 100%; }
  .modal { max-height: calc(100vh - 20px); }
}
