:root {
  --bg: #f5f7f8;
  --panel: #ffffff;
  --line: #e3e8ea;
  --text: #16211f;
  --muted: #6b7a76;
  --green: #0f8a6a;
  --green-soft: #e6f5ef;
  --amber: #b7791f;
  --amber-soft: #fdf3e2;
  --red: #c0392b;
  --red-soft: #fdecea;
  --blue: #2b6cb0;
  --blue-soft: #e8f1fb;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 32, 28, 0.06), 0 8px 24px rgba(16, 32, 28, 0.06);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 14px;
  line-height: 1.55;
}

button, input, select, textarea { font: inherit; color: inherit; }

.hidden { display: none !important; }

.app { display: flex; min-height: 100vh; }

/* ---------------- sidebar ---------------- */
.sidebar {
  width: 216px;
  flex: 0 0 216px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding: 18px 12px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand { display: flex; gap: 10px; align-items: center; padding: 4px 8px 18px; }
.brand .logo {
  width: 36px; height: 36px; border-radius: 10px;
  background: linear-gradient(135deg, #128c7e, #25d366);
  color: #fff; font-weight: 700; font-size: 13px;
  display: grid; place-items: center; letter-spacing: 0.5px;
}
.brand strong { display: block; font-size: 15px; }
.brand small { color: var(--muted); font-size: 11px; }

nav { display: flex; flex-direction: column; gap: 4px; }
.nav {
  display: flex; align-items: center; justify-content: space-between;
  background: transparent; border: 0; text-align: left;
  padding: 9px 12px; border-radius: 9px; cursor: pointer; color: var(--muted);
}
.nav:hover { background: #f0f4f3; color: var(--text); }
.nav.active { background: var(--green-soft); color: var(--green); font-weight: 600; }
.badge {
  background: var(--red); color: #fff; border-radius: 999px;
  font-size: 11px; padding: 0 6px; min-width: 18px; text-align: center;
}

.sidebar-foot {
  margin-top: auto; display: flex; align-items: center; gap: 8px;
  font-size: 12px; color: var(--muted); padding: 10px 10px 0;
  border-top: 1px solid var(--line);
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #cbd5d2; }
.status-dot.ok { background: #22a06b; }
.status-dot.warn { background: #e0a53c; }
.status-dot.err { background: #d25247; }

/* ---------------- main ---------------- */
main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 26px; border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.86); backdrop-filter: blur(6px);
  position: sticky; top: 0; z-index: 5;
}
.topbar h1 { font-size: 18px; margin: 0; font-weight: 650; }
.topbar-actions { display: flex; gap: 8px; align-items: center; }

.tab { padding: 22px 26px 40px; flex: 1; }

button.primary, button.ghost, button.danger, button.subtle {
  border-radius: 8px; border: 1px solid transparent; padding: 7px 13px; cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
button.primary { background: var(--green); color: #fff; }
button.primary:hover { background: #0c7760; }
button.ghost { background: #fff; border-color: var(--line); color: var(--text); }
button.ghost:hover { background: #f4f7f6; }
button.danger { background: #fff; border-color: #f0c9c3; color: var(--red); }
button.danger:hover { background: var(--red-soft); }
button.subtle { background: var(--green-soft); color: var(--green); }
button:disabled { opacity: 0.55; cursor: not-allowed; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(148px, 1fr)); gap: 12px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow);
}
.card .label { color: var(--muted); font-size: 12px; }
.card .value { font-size: 24px; font-weight: 680; margin-top: 4px; letter-spacing: -0.5px; }
.card .hint { font-size: 11px; color: var(--muted); margin-top: 2px; }
.card.accent { border-color: #bfe6d8; background: linear-gradient(180deg, #f2fbf7, #fff); }

.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid var(--line);
}
.panel-head h2 { margin: 0; font-size: 14px; font-weight: 620; }
.panel-body { padding: 14px 16px; }

.section { margin-top: 18px; }
.grid-2 { display: grid; grid-template-columns: 1.15fr 0.85fr; gap: 16px; }
@media (max-width: 1080px) { .grid-2 { grid-template-columns: 1fr; } }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  background: #f1f5f4; color: #40534f; border-radius: 999px;
  padding: 3px 9px; font-size: 12px;
}
.chip.green { background: var(--green-soft); color: var(--green); }
.chip.amber { background: var(--amber-soft); color: var(--amber); }
.chip.blue { background: var(--blue-soft); color: var(--blue); }
.chip.red { background: var(--red-soft); color: var(--red); }

.tag {
  display: inline-block; background: #f1f5f4; color: #4a5d58;
  border-radius: 6px; padding: 1px 7px; font-size: 11px; margin: 2px 4px 2px 0;
}

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid #edf1f0; font-size: 13px; }
th { color: var(--muted); font-weight: 600; font-size: 12px; }
tr:last-child td { border-bottom: 0; }

.empty { color: var(--muted); text-align: center; padding: 42px 12px; font-size: 13px; }

/* ---------------- inbox ---------------- */
.inbox { display: grid; grid-template-columns: 320px 1fr; gap: 16px; height: calc(100vh - 130px); }
@media (max-width: 900px) { .inbox { grid-template-columns: 1fr; height: auto; } }

.list-pane, .chat-pane {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); display: flex; flex-direction: column; min-height: 0; overflow: hidden;
}
.filters { display: flex; gap: 8px; padding: 12px; border-bottom: 1px solid var(--line); }
.filters input, .filters select { flex: 1; min-width: 0; border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; background: #fbfcfc; }

.contact-list { overflow-y: auto; flex: 1; }
.contact-item {
  padding: 11px 14px; border-bottom: 1px solid #f0f4f3; cursor: pointer;
}
.contact-item:hover { background: #f8fbfa; }
.contact-item.active { background: var(--green-soft); }
.contact-item .row1 { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.contact-item .name { font-weight: 600; font-size: 13px; }
.contact-item .meta { color: var(--muted); font-size: 11px; white-space: nowrap; }
.contact-item .preview {
  color: var(--muted); font-size: 12px; margin-top: 2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.chat-head { padding: 12px 16px; border-bottom: 1px solid var(--line); }
.chat-head .title { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.chat-head .title strong { font-size: 15px; }
.chat-head .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.chat-head .controls { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; align-items: center; }
.chat-head select { border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; background: #fff; }

.messages { flex: 1; overflow-y: auto; padding: 16px; background: #f7faf9; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 76%; padding: 8px 11px; border-radius: 12px; position: relative; white-space: pre-wrap; word-break: break-word; font-size: 13px; }
.msg.in { align-self: flex-start; background: #fff; border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg.out { align-self: flex-end; background: #dcf8ec; border-bottom-right-radius: 4px; }
.msg.out.operator { background: #dbe9fb; }
.msg .meta { display: block; font-size: 10px; color: #7b8c88; margin-top: 4px; }
.msg.blocked { opacity: 0.7; border: 1px dashed #d9a49c; }

.composer { border-top: 1px solid var(--line); padding: 12px; display: flex; gap: 8px; align-items: flex-end; background: #fff; }
.composer textarea {
  flex: 1; min-height: 44px; max-height: 140px; resize: vertical;
  border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px; background: #fbfcfc;
}
.composer .note { font-size: 11px; color: var(--amber); width: 100%; }

/* ---------------- misc ---------------- */
.kv { display: grid; grid-template-columns: 150px 1fr; gap: 8px 12px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-all; }

.result-block { background: #fbfcfc; border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin-top: 10px; }
.result-line { display: flex; justify-content: space-between; padding: 4px 0; font-size: 13px; border-bottom: 1px dashed #eef2f1; }
.result-line:last-child { border-bottom: 0; }
.score { font-variant-numeric: tabular-nums; color: var(--green); font-weight: 600; }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: #1c2b27; color: #fff; padding: 9px 16px; border-radius: 999px;
  font-size: 13px; box-shadow: var(--shadow); opacity: 0; pointer-events: none; transition: opacity 0.2s;
  z-index: 40;
}
.toast.show { opacity: 1; }

.login {
  position: fixed; inset: 0; background: rgba(18, 32, 28, 0.55);
  display: grid; place-items: center; z-index: 50;
}
.login-card {
  background: #fff; border-radius: 14px; padding: 24px; width: 340px; box-shadow: var(--shadow);
}
.login-card h2 { margin: 0 0 6px; font-size: 17px; }
.login-card p { margin: 0 0 14px; color: var(--muted); font-size: 12.5px; }
.login-card input { width: 100%; border: 1px solid var(--line); border-radius: 9px; padding: 9px 11px; margin-bottom: 12px; }
.login-card button { width: 100%; }

.alert { border-radius: 9px; padding: 9px 12px; font-size: 12.5px; margin-bottom: 12px; }
.alert.amber { background: var(--amber-soft); color: #8a5a10; }
.alert.green { background: var(--green-soft); color: #0b6b51; }
.alert.red { background: var(--red-soft); color: #8f2c22; }

.inline-form { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.inline-form input { border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; background: #fbfcfc; min-width: 90px; }
#tab-settings .inline-form input[type="number"] { width: 62px; min-width: 0; text-align: center; }
#tab-settings .inline-form { align-items: center; }
#tab-settings .panel-body { padding-bottom: 16px; }

/* ---------------------------- 接入向导 ---------------------------- */
.setup-field { margin-bottom: 13px; }
.setup-label { display: flex; align-items: center; gap: 8px; margin-bottom: 5px; flex-wrap: wrap; }
.setup-label label { font-size: 12.5px; font-weight: 600; }
.setup-field input {
  width: 100%; border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; background: #fbfcfc; font-size: 13px;
}
.setup-field input:focus { outline: none; border-color: var(--green); background: #fff; }
.setup-field small { display: block; color: var(--muted); font-size: 11.5px; margin-top: 4px; line-height: 1.45; }
.setup-actions { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.setup-mode { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.setup-mode button.active { background: var(--green-soft); border-color: #bfe6d8; color: var(--green); font-weight: 600; }
.setup-note { color: var(--muted); font-size: 11.5px; margin: 10px 0 0; line-height: 1.5; }
.copy-block { margin-bottom: 12px; }
.copy-label { font-size: 12px; color: var(--muted); margin-bottom: 5px; }
.copy-line { display: flex; align-items: center; gap: 8px; }
.copy-line code {
  flex: 1; min-width: 0; background: #f4f7f6; border: 1px solid var(--line);
  border-radius: 7px; padding: 7px 9px; font-size: 12px; word-break: break-all;
}
.copy-line button { flex: none; }
.setup-note-block {
  margin-top: 16px; padding: 11px 13px; background: #f7faf9;
  border: 1px solid var(--line); border-radius: 9px; font-size: 12px; color: #45534f;
}
.setup-note-block strong { display: block; margin-bottom: 6px; font-size: 12.5px; color: var(--text); }
.setup-note-block span { display: block; margin-top: 5px; padding-left: 10px; position: relative; line-height: 1.5; }
.setup-note-block span::before { content: '·'; position: absolute; left: 0; color: var(--green); font-weight: 700; }
.steps { margin: 14px 0 0; padding-left: 18px; font-size: 12.5px; color: #45534f; }
.steps li { margin-bottom: 6px; }
.test-item { display: flex; gap: 10px; padding: 9px 0; border-bottom: 1px dashed #eef2f1; }
.test-item:last-child { border-bottom: 0; }
.test-item .mark {
  width: 18px; height: 18px; border-radius: 50%; display: grid; place-items: center;
  font-size: 11px; font-weight: 700; flex: none; margin-top: 1px;
}
.test-item.ok .mark { background: var(--green-soft); color: var(--green); }
.test-item.warn .mark { background: var(--amber-soft); color: var(--amber); }
.test-item.fail .mark { background: var(--red-soft); color: var(--red); }
.test-item strong { font-size: 13px; font-weight: 600; }
.test-item p { margin: 2px 0 0; font-size: 12px; color: var(--muted); word-break: break-all; }
.test-item p.hint { color: var(--amber); font-size: 11.5px; margin-top: 3px; }
#tab-setup .grid-2 { align-items: start; }
#tab-setup .panel-body .setup-actions { margin-top: 14px; }

/* ---------------------------- 知识库管理 ---------------------------- */
.kb-wrap { display: grid; grid-template-columns: 308px minmax(0, 1fr); gap: 16px; align-items: start; }
.kb-list-panel { display: flex; flex-direction: column; overflow: hidden; }
.kb-tools { display: flex; gap: 6px; padding: 10px 12px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.kb-tools input {
  flex: 1; min-width: 110px; border: 1px solid var(--line); border-radius: 8px;
  padding: 6px 9px; background: #fbfcfc; font-size: 12.5px;
}
.kb-tools button { padding: 5px 9px; font-size: 12px; }
.kb-list-panel { position: sticky; top: 16px; max-height: calc(100vh - 40px); }
.kb-list { overflow-y: auto; flex: 1; min-height: 120px; padding-bottom: 6px; }
.kb-group {
  padding: 10px 13px 5px; font-size: 11px; letter-spacing: 0.03em;
  color: var(--muted); font-weight: 600;
}
.kb-item { padding: 8px 13px; cursor: pointer; border-left: 2px solid transparent; }
.kb-item:hover { background: #f8fbfa; }
.kb-item.active { background: var(--green-soft); border-left-color: var(--green); }
.kb-item .t { font-size: 13px; font-weight: 600; line-height: 1.35; }
.kb-item .m { font-size: 11px; color: var(--muted); margin-top: 2px; }
.kb-empty { padding: 8px 13px; font-size: 12px; color: var(--muted); }
.kb-foot {
  padding: 8px 13px; border-top: 1px solid var(--line);
  font-size: 10.5px; color: var(--muted); word-break: break-all;
}
.panel-form details { border-top: 1px solid var(--line); padding: 9px 0 3px; }
.panel-form details:first-of-type { border-top: 0; padding-top: 0; }
.panel-form summary {
  cursor: pointer; font-size: 13px; font-weight: 620; padding: 4px 0 6px;
  list-style: none; user-select: none;
}
.panel-form summary::-webkit-details-marker { display: none; }
.panel-form summary::before {
  content: '▸'; display: inline-block; margin-right: 7px; color: var(--muted);
  transition: transform 0.12s ease;
}
.panel-form details[open] > summary::before { transform: rotate(90deg); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 11px 14px; }
.form-grid .full { grid-column: 1 / -1; }
.fld label { display: block; font-size: 12px; font-weight: 600; color: #45534f; margin-bottom: 4px; }
.fld input, .fld textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 8px;
  padding: 7px 9px; background: #fbfcfc; font-size: 13px; font-family: inherit;
}
.fld input:focus, .fld textarea:focus { outline: none; border-color: var(--green); background: #fff; }
.fld input[readonly] { background: #f1f4f3; color: var(--muted); cursor: not-allowed; }
.fld textarea { resize: vertical; line-height: 1.5; min-height: 54px; }
.fld .tip { font-size: 11px; color: var(--muted); margin-top: 3px; line-height: 1.4; }
.faq-item { border: 1px solid var(--line); border-radius: 9px; padding: 10px 12px; margin-bottom: 9px; background: #fbfcfc; }
.faq-item .fld label { font-weight: 500; font-size: 11.5px; color: var(--muted); }
.faq-item .mini { margin-top: 4px; }
.form-actions { display: flex; align-items: center; gap: 8px; margin-top: 18px; flex-wrap: wrap; }
.form-hint { font-size: 11px; color: var(--muted); }
@media (max-width: 1080px) {
  .kb-wrap { grid-template-columns: 1fr; }
  .kb-list { max-height: 260px; }
  .form-grid { grid-template-columns: 1fr; }
}
button.mini { padding: 3px 9px; font-size: 11.5px; border-radius: 7px; }
