:root {
  --bg: #f4f6fa;
  --panel: #ffffff;
  --panel-soft: #f9fafc;
  --border: #e3e8ef;
  --text: #1f2937;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --green: #059669;
  --green-soft: #ecfdf5;
  --red: #dc2626;
  --red-soft: #fef2f2;
  --amber: #d97706;
  --amber-soft: #fffbeb;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 8px 24px rgba(16, 24, 40, .06);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}
.hidden { display: none !important; }
code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12px; }

.boot { padding: 48px; text-align: center; color: var(--muted); }

/* ---------- 通用控件 ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13px;
  cursor: pointer;
  transition: background .15s, border-color .15s, opacity .15s;
}
.btn:hover:not(:disabled) { background: var(--panel-soft); border-color: #cbd5e1; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn-ghost { background: transparent; }
.btn-block { width: 100%; margin-top: 8px; }
.icon-btn {
  border: 0; background: transparent; font-size: 24px; line-height: 1;
  color: var(--muted); cursor: pointer; padding: 0 4px;
}
.badge {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; padding: 4px 10px; border-radius: 999px; white-space: nowrap;
  border: 1px solid transparent;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge-muted { background: #f1f5f9; color: #64748b; border-color: #e2e8f0; }
.badge-ok { background: var(--green-soft); color: var(--green); border-color: #a7f3d0; }
.badge-warn { background: var(--amber-soft); color: var(--amber); border-color: #fde68a; }
.badge-err { background: var(--red-soft); color: var(--red); border-color: #fecaca; }

/* ---------- 登录 ---------- */
.auth-view { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; }
.auth-card {
  width: 100%; max-width: 380px; background: var(--panel);
  border: 1px solid var(--border); border-radius: 14px; padding: 32px;
  box-shadow: var(--shadow);
}
.auth-card h1 { margin: 0 0 6px; font-size: 20px; }
.auth-sub { margin: 0 0 24px; color: var(--muted); font-size: 13px; }
.auth-card label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.auth-card input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 14px; outline: none; margin-bottom: 14px;
}
.auth-card input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37, 99, 235, .12); }
.auth-card .btn { width: 100%; padding: 10px; }
.auth-error {
  background: var(--red-soft); color: var(--red); border: 1px solid #fecaca;
  border-radius: 8px; padding: 8px 10px; font-size: 12px; margin-bottom: 12px;
}
.auth-foot { margin: 18px 0 0; font-size: 11px; color: var(--muted); text-align: center; }

/* ---------- 主布局 ---------- */
.app { min-height: 100vh; display: flex; flex-direction: column; }
.topbar {
  height: 60px; flex: 0 0 60px; background: var(--panel);
  border-bottom: 1px solid var(--border); display: flex;
  align-items: center; justify-content: space-between; padding: 0 20px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 8px; background: var(--primary);
  color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 12px; letter-spacing: .5px;
}
.brand strong { display: block; font-size: 14px; }
.brand small { display: block; font-size: 11px; color: var(--muted); }
.status-group { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.notice {
  margin: 10px 20px 0; padding: 10px 14px; border-radius: 8px; font-size: 12px;
  background: var(--amber-soft); border: 1px solid #fde68a; color: #92400e;
}
.notice.err { background: var(--red-soft); border-color: #fecaca; color: #991b1b; }

.layout { flex: 1; display: flex; min-height: 0; height: calc(100vh - 60px); }
.sidebar {
  width: 300px; flex: 0 0 300px; background: var(--panel);
  border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0;
}
.sidebar-head {
  height: 46px; flex: 0 0 46px; padding: 0 16px; display: flex;
  align-items: center; justify-content: space-between;
  font-size: 12px; font-weight: 600; color: var(--muted);
  border-bottom: 1px solid var(--border); text-transform: uppercase; letter-spacing: .5px;
}
.count { background: #eef2f7; border-radius: 999px; padding: 1px 8px; font-size: 11px; }
.conversation-list { flex: 1; overflow-y: auto; }
.conv-item {
  padding: 12px 16px; border-bottom: 1px solid #f1f4f8; cursor: pointer;
  display: flex; gap: 10px; align-items: flex-start;
}
.conv-item:hover { background: var(--panel-soft); }
.conv-item.active { background: #eff6ff; border-left: 3px solid var(--primary); padding-left: 13px; }
.conv-avatar {
  width: 34px; height: 34px; flex: 0 0 34px; border-radius: 50%;
  background: #e8edf5; color: #475569; display: grid; place-items: center; font-size: 12px; font-weight: 600;
}
.conv-body { min-width: 0; flex: 1; }
.conv-top { display: flex; justify-content: space-between; gap: 8px; }
.conv-name { font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { font-size: 11px; color: var(--muted); flex: 0 0 auto; }
.conv-preview { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 3px; }
.conv-meta { display: flex; align-items: center; gap: 6px; margin-top: 4px; }
.unread { background: var(--red); color: #fff; border-radius: 999px; font-size: 10px; padding: 1px 6px; }
.tag-no-reply { font-size: 10px; color: var(--amber); background: var(--amber-soft); border: 1px solid #fde68a; border-radius: 4px; padding: 0 5px; }
.empty { padding: 32px 16px; text-align: center; color: var(--muted); font-size: 12px; line-height: 1.8; }
.sidebar-foot { border-top: 1px solid var(--border); padding: 12px 14px; font-size: 11px; }
.kv-line { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 6px; color: var(--muted); }
.kv-line code { max-width: 170px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chat-head {
  height: 58px; flex: 0 0 58px; padding: 0 20px; display: flex;
  align-items: center; justify-content: space-between; gap: 12px;
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.chat-head strong { font-size: 14px; display: block; }
.chat-head small { font-size: 11px; color: var(--muted); display: block; margin-top: 2px; }
.message-list { flex: 1; overflow-y: auto; padding: 20px; background: var(--bg); }
.msg-day { text-align: center; font-size: 11px; color: var(--muted); margin: 8px 0 16px; }
.msg-row { display: flex; margin-bottom: 14px; }
.msg-row.out { justify-content: flex-end; }
.bubble {
  max-width: min(72%, 640px); padding: 9px 13px; border-radius: 12px;
  background: var(--panel); border: 1px solid var(--border);
  box-shadow: 0 1px 2px rgba(16, 24, 40, .04);
  white-space: pre-wrap; word-break: break-word; font-size: 13px;
}
.msg-row.out .bubble { background: var(--primary); border-color: var(--primary); color: #fff; }
.bubble.system { background: transparent; border: 1px dashed var(--border); color: var(--muted); font-size: 12px; }
.bubble.pending { opacity: .65; }
.bubble.failed { border-color: #fecaca; background: var(--red-soft); color: #991b1b; }
.msg-meta { font-size: 10px; color: var(--muted); margin-top: 5px; display: flex; gap: 8px; }
.msg-row.out .msg-meta { justify-content: flex-end; color: #64748b; }

.composer {
  display: flex; gap: 10px; padding: 12px 16px 6px;
  background: var(--panel); border-top: 1px solid var(--border); align-items: flex-end;
}
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; font: inherit; font-size: 13px; outline: none;
}
.composer textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37, 99, 235, .1); }
.composer .btn { height: 40px; min-width: 76px; }
.composer-hint { margin: 0; padding: 0 16px 10px; font-size: 11px; color: var(--muted); background: var(--panel); }

/* ---------- 弹窗 ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .45);
  display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 50;
}
.modal-card {
  width: 100%; max-width: 420px; background: var(--panel);
  border-radius: 14px; padding: 22px; box-shadow: 0 20px 50px rgba(15, 23, 42, .2);
  max-height: 90vh; overflow-y: auto;
}
.modal-wide { max-width: 720px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-head h2 { font-size: 16px; margin: 0; }
.modal-sub { font-size: 12px; color: var(--muted); margin: 10px 0 16px; line-height: 1.7; }
.qr-box {
  width: 240px; height: 240px; margin: 0 auto 14px; border: 1px solid var(--border);
  border-radius: 10px; background: #fff; display: grid; place-items: center;
  font-size: 12px; color: var(--muted); text-align: center; padding: 8px;
}
.qr-box img { width: 224px; height: 224px; display: block; }
.qr-state { text-align: center; font-size: 13px; font-weight: 600; margin-bottom: 14px; }
.qr-state.ok { color: var(--green); }
.qr-state.err { color: var(--red); }
.modal-actions { display: flex; gap: 8px; justify-content: center; }
.modal-foot { font-size: 11px; color: var(--muted); text-align: center; margin: 16px 0 0; line-height: 1.6; }
.log-list { font-family: ui-monospace, Consolas, monospace; font-size: 12px; max-height: 60vh; overflow-y: auto; }
.log-line { padding: 6px 8px; border-bottom: 1px solid #f1f4f8; display: flex; gap: 10px; }
.log-line .ts { color: var(--muted); flex: 0 0 132px; }
.log-line.error .lv { color: var(--red); }
.log-line.warn .lv { color: var(--amber); }
.log-line .lv { flex: 0 0 44px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #111827; color: #fff; padding: 10px 18px; border-radius: 8px;
  font-size: 13px; z-index: 100; box-shadow: var(--shadow); max-width: 80vw;
}
.toast.err { background: var(--red); }

@media (max-width: 820px) {
  .layout { flex-direction: column; height: auto; }
  .sidebar { width: 100%; flex: 0 0 auto; max-height: 210px; border-right: 0; border-bottom: 1px solid var(--border); }
  .chat { min-height: 60vh; }
  .topbar { height: auto; flex: 0 0 auto; padding: 10px 14px; gap: 10px; flex-wrap: wrap; }
  .status-group { justify-content: flex-start; }
}
