:root {
  --primary: #16382e;
  --primary-dark: #0f2a22;
  --primary-soft: #e9ecdd;
  --accent: #c8953d;
  --accent-soft: #f0e4cd;
  --bg: #faf5e9;
  --card: #fffdf8;
  --border: #e7ddc6;
  --text: #1c2420;
  --text-muted: #8a8273;
  --danger: #b4473a;
  --success: #2f7d5d;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(28, 36, 32, 0.06), 0 8px 24px rgba(28, 36, 32, 0.08);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  --serif: "Songti SC", "SimSun", "STSong", Georgia, "Times New Roman", serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); text-decoration: none; }
button { font-family: var(--font); cursor: pointer; }
input, textarea, select { font-family: var(--font); font-size: 14px; }

/* ---------- 客服端 ---------- */
.chat-page {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  max-width: 760px;
  margin: 0 auto;
  background: var(--card);
  box-shadow: var(--shadow);
}
.chat-header {
  padding: 14px 18px;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
  position: relative;
}
.chat-header .avatar {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--primary);
  display: flex; align-items: center; justify-content: center;
  font-weight: 600;
  font-size: 16px;
  font-family: var(--serif);
  flex-shrink: 0;
}
.chat-header .title { font-size: 16px; font-weight: 600; font-family: var(--serif); letter-spacing: 1px; }
.chat-header .subtitle { font-size: 11px; opacity: 0.72; margin-top: 1px; font-family: var(--serif); letter-spacing: 0.4px; }
.chat-header .status { margin-left: auto; font-size: 12px; display: flex; align-items: center; gap: 6px; }
.chat-header .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.chat-header .dot.offline { background: #9aa39a; }
.chat-header .bookmark {
  position: absolute;
  left: 30px;
  bottom: -16px;
  width: 12px;
  height: 32px;
  background: var(--accent);
  clip-path: polygon(0 0, 100% 0, 100% 72%, 50% 100%, 0 72%);
}

.chat-body {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  background: var(--bg);
}
.msg { display: flex; gap: 10px; max-width: 86%; }
.msg .bubble {
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 14.5px;
  word-break: break-word;
  white-space: pre-wrap;
}
.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.msg.user .bubble { background: #efe8d4; color: var(--text); border-bottom-right-radius: 4px; }
.msg.bot { align-self: flex-start; }
.msg.bot .bubble { background: var(--primary); color: #fff; border-bottom-left-radius: 4px; }
.msg .bubble a { color: var(--accent); text-decoration: underline; word-break: break-all; }
.msg.bot .bubble a { color: var(--accent-soft); }
.msg .bot-avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--accent); color: var(--primary);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 600; flex-shrink: 0;
  font-family: var(--serif);
}
.msg .src-tags { margin-top: 6px; display: flex; flex-wrap: wrap; gap: 4px; }
.msg .src-tags span { font-size: 11px; color: var(--text-muted); background: var(--card); border: 0.5px solid var(--border); border-radius: 20px; padding: 1px 8px; }

.msg .attachments { margin-top: 8px; display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.msg .att-link {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--card); border: 0.5px solid var(--accent);
  color: var(--primary); padding: 8px 12px; border-radius: 8px;
  font-size: 13px; text-decoration: none; max-width: 280px;
}
.msg .att-link .att-arrow { color: var(--accent); }
.msg .att-img img { max-width: 180px; border-radius: 8px; border: 0.5px solid var(--border); display: block; }

.typing { display: flex; gap: 4px; padding: 4px 2px; }
.typing span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--accent);
  animation: blink 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.3; } 40% { opacity: 1; } }

.chat-input {
  padding: 12px 14px;
  border-top: 0.5px solid var(--border);
  display: flex;
  gap: 10px;
  align-items: flex-end;
  flex-shrink: 0;
  background: var(--card);
}
.chat-input textarea {
  flex: 1;
  border: 0.5px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  resize: none;
  max-height: 120px;
  outline: none;
  font-size: 14.5px;
  line-height: 1.5;
  background: #fff;
}
.chat-input textarea:focus { border-color: var(--accent); }
.chat-input button {
  width: 44px; height: 44px;
  border: none; border-radius: 10px;
  background: var(--accent); color: var(--primary);
  font-size: 18px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.chat-input button:disabled { background: #e6d4ac; cursor: not-allowed; }
.chat-footer { text-align: center; font-size: 11px; color: var(--text-muted); padding: 8px; flex-shrink: 0; }

@media (max-width: 768px) {
  .chat-page { max-width: 100%; box-shadow: none; }
  .msg { max-width: 90%; }
}

/* ---------- 管理后台 ---------- */
.admin-page { display: flex; min-height: 100dvh; }
.sidebar {
  width: 220px;
  background: var(--primary);
  padding: 20px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex-shrink: 0;
}
.sidebar .logo {
  font-size: 17px; font-weight: 600; padding: 4px 12px 18px;
  color: #fff; font-family: var(--serif); letter-spacing: 1px;
}
.sidebar .logo .en { display: block; font-size: 11px; color: rgba(255,255,255,0.55); font-weight: 400; letter-spacing: 0.4px; margin-top: 2px; }
.sidebar .nav {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 12px; border-radius: 8px;
  color: rgba(255,255,255,0.62); font-size: 14px; cursor: pointer;
  border: none; background: none; text-align: left; width: 100%;
}
.sidebar .nav:hover { background: rgba(255,255,255,0.1); color: #fff; }
.sidebar .nav.active { background: rgba(255,255,255,0.12); color: #fff; font-weight: 600; box-shadow: inset 3px 0 0 var(--accent); }
.sidebar .nav .ic { width: 18px; text-align: center; }
.sidebar .foot { margin-top: auto; font-size: 12px; color: rgba(255,255,255,0.4); padding: 12px; }

.admin-main { flex: 1; padding: 24px; overflow: auto; background: var(--bg); }
.admin-main .topbar { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; }
.admin-main .topbar h1 { font-size: 20px; font-weight: 600; font-family: var(--serif); letter-spacing: 1px; }
.admin-main .topbar .logout { margin-left: auto; }

.tabs { display: none; }
.view { display: none; }
.view.active { display: block; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; margin-bottom: 24px; }
.stat-card { background: var(--card); border: 0.5px solid var(--border); border-radius: var(--radius); padding: 18px; }
.stat-card .num { font-size: 28px; font-weight: 600; color: var(--primary); font-family: var(--serif); }
.stat-card .label { font-size: 13px; color: var(--text-muted); margin-top: 2px; }

.panel { background: var(--card); border: 0.5px solid var(--border); border-radius: var(--radius); padding: 18px; }
.panel + .panel { margin-top: 16px; }
.panel .panel-head { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.panel .panel-head h2 { font-size: 16px; font-weight: 600; }
.panel .panel-head .spacer { flex: 1; }

.btn {
  border: none; border-radius: 8px; padding: 8px 14px;
  font-size: 14px; background: var(--primary); color: #fff;
}
.btn:hover { background: var(--primary-dark); }
.btn.ghost { background: var(--card); color: var(--text); border: 0.5px solid var(--border); }
.btn.ghost:hover { background: #f6f1e4; }
.btn.danger { background: #f3dfd9; color: var(--danger); }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btn.accent { background: var(--accent); color: var(--primary); }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; font-size: 14px; border-bottom: 0.5px solid var(--border); vertical-align: top; }
th { color: var(--text-muted); font-weight: 500; font-size: 13px; background: #f6f1e4; }
tr:hover td { background: #faf6ec; }
.tag { display: inline-block; padding: 1px 8px; border-radius: 20px; font-size: 12px; background: var(--primary-soft); color: var(--primary); }

.form-row { margin-bottom: 12px; }
.form-row label { display: block; font-size: 13px; color: var(--text-muted); margin-bottom: 5px; }
.form-row input, .form-row textarea, .form-row select {
  width: 100%; border: 0.5px solid var(--border); border-radius: 8px;
  padding: 9px 12px; outline: none; background: #fff;
}
.form-row input:focus, .form-row textarea:focus, .form-row select:focus { border-color: var(--accent); }
.form-row textarea { min-height: 90px; resize: vertical; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 8px; }

.modal-mask {
  position: fixed; inset: 0; background: rgba(15, 42, 34, 0.5);
  display: none; align-items: center; justify-content: center; z-index: 50; padding: 16px;
}
.modal-mask.show { display: flex; }
.modal {
  background: var(--card); border-radius: var(--radius); padding: 22px;
  width: 100%; max-width: 520px; max-height: 90dvh; overflow: auto;
  box-shadow: 0 20px 50px rgba(15, 42, 34, 0.28);
}
.modal h3 { font-size: 17px; font-weight: 600; margin-bottom: 14px; }

.empty { text-align: center; color: var(--text-muted); padding: 40px 0; font-size: 14px; }
.toast {
  position: fixed; top: 20px; left: 50%; transform: translateX(-50%);
  background: #16382e; color: #fff; padding: 10px 18px; border-radius: 8px;
  font-size: 14px; z-index: 100; display: none;
}
.toast.show { display: block; animation: fade 0.2s; }
@keyframes fade { from { opacity: 0; transform: translate(-50%, -6px); } to { opacity: 1; } }

/* 对话详情 */
.conv-msg { margin-bottom: 14px; }
.conv-msg .role { font-size: 12px; color: var(--text-muted); margin-bottom: 4px; }
.conv-msg .content { padding: 10px 14px; border-radius: 10px; background: #f6f1e4; }
.conv-msg.user .content { background: var(--primary-soft); }
.conv-msg .time { font-size: 11px; color: var(--text-muted); margin-top: 3px; }

@media (max-width: 768px) {
  .admin-page { flex-direction: column; }
  .sidebar { display: none; }
  .tabs {
    display: flex; gap: 4px; background: var(--primary); border-bottom: 0.5px solid rgba(255,255,255,0.1);
    padding: 8px 8px 0; overflow-x: auto;
  }
  .tabs button {
    border: none; background: none; padding: 10px 14px; font-size: 14px;
    color: rgba(255,255,255,0.62); border-bottom: 2px solid transparent; white-space: nowrap;
  }
  .tabs button.active { color: #fff; border-bottom-color: var(--accent); font-weight: 600; }
  .admin-main { padding: 16px; }
  .admin-main .topbar h1 { font-size: 18px; }
}

/* 知识库左右布局 */
.kb-layout { display: flex; gap: 16px; align-items: flex-start; }
.kb-sidebar { width: 220px; flex-shrink: 0; border: 0.5px solid var(--border); border-radius: 8px; padding: 6px; position: sticky; top: 0; max-height: calc(100vh - 140px); overflow-y: auto; }
.kb-cat { display: flex; justify-content: space-between; align-items: center; padding: 8px 10px; border-radius: 6px; cursor: pointer; font-size: 13px; }
.kb-cat:hover { background: #f5f1e6; }
.kb-cat.active { background: var(--accent-soft); font-weight: 600; }
.kb-cat .kb-n { font-size: 11px; color: var(--text-muted); }
.kb-main { flex: 1; min-width: 0; }
.kb-item { padding: 10px 14px; border: 0.5px solid var(--border); border-radius: 8px; margin-bottom: 8px; }
.kb-q { font-weight: 500; margin-bottom: 6px; }
.kb-a { font-size: 13px; color: var(--text-muted); margin-bottom: 8px; word-break: break-all; white-space: pre-wrap; }
.kb-actions { display: flex; gap: 6px; }

@media (max-width: 768px) {
  .kb-layout { flex-direction: column; }
  .kb-sidebar { width: 100%; display: flex; flex-wrap: wrap; gap: 4px; max-height: none; position: static; }
  .kb-cat { flex: 0 0 auto; }
}

/* 分页 */
.pagination { display: flex; align-items: center; gap: 10px; margin-top: 12px; }
.pagination .pg-info { font-size: 12px; color: var(--text-muted); }
.pagination .btn:disabled { opacity: 0.4; cursor: not-allowed; }
