:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-soft: #eff6ff;
  --brand: #1e3a8a;
  --accent: #0891b2;
  --bg: #f5f7fa;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --sunken: #eef2f7;
  --text: #1a2433;
  --muted: #475569;
  --dim: #94a3b8;
  --line: #e2e8f0;
  --line-strong: #cbd5e1;
  --ok: #15803d;
  --ok-bg: #dcfce7;
  --warn: #92400e;
  --warn-bg: #fef3c7;
  --danger: #b91c1c;
  --danger-bg: #fee2e2;
  --shadow: 0 8px 24px rgba(15, 23, 42, .08);
  font-family: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.6 var(--font, inherit); }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
a { color: inherit; }
a.btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 244px 1fr; }
.sidebar { background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.brand { height: 64px; padding: 12px 18px; display: flex; align-items: center; gap: 12px; background: var(--brand); color: white; }
.brand-mark { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; background: rgba(255,255,255,.12); font-weight: 700; }
.brand-title { font-weight: 700; line-height: 1.25; }
.brand-sub { display: block; font-size: 11px; color: rgba(255,255,255,.65); letter-spacing: .06em; }
.nav { padding: 16px 10px; display: grid; gap: 8px; }
.nav button { height: 38px; border: 0; border-radius: 8px; background: transparent; color: var(--muted); text-align: left; padding: 0 12px; font-weight: 600; }
.nav button.active, .nav button:hover { background: var(--primary-soft); color: var(--primary); }
.sidebar-foot { margin-top: auto; padding: 14px 18px; border-top: 1px solid var(--line); color: var(--muted); }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar { height: 64px; background: var(--surface); border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 12px; padding: 0 22px; }
.topbar h1 { margin: 0; font-size: 20px; }
.topbar .hint { color: var(--muted); font-size: 12px; }
.spacer { flex: 1; }
.content { min-height: 0; flex: 1; padding: 18px; }

.sales-grid { height: calc(100vh - 100px); display: grid; grid-template-columns: 300px minmax(420px, 1fr) 360px; gap: 14px; }
.panel { min-width: 0; min-height: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 1px 2px rgba(15,23,42,.04); display: flex; flex-direction: column; overflow: hidden; }
.panel-head { padding: 14px 16px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 10px; }
.panel-head h2 { margin: 0; font-size: 16px; }
.panel-body { padding: 14px 16px; overflow: auto; }
.tight { padding: 10px 12px; }

.search-row { padding: 12px; border-bottom: 1px solid var(--line); display: grid; gap: 10px; }
.input, .select, .textarea { width: 100%; border: 1px solid var(--line-strong); border-radius: 9px; background: white; padding: 8px 10px; color: var(--text); outline: none; }
.textarea { min-height: 72px; resize: vertical; }
.input:focus, .select:focus, .textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.14); }
.filter-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--line); background: var(--surface-alt); color: var(--muted); border-radius: 999px; padding: 4px 8px; font-size: 11px; }
.chip.active { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); font-weight: 700; }

.thread { width: 100%; border: 0; border-bottom: 1px solid var(--line); background: white; text-align: left; padding: 12px 14px; }
.thread:hover { background: var(--surface-alt); }
.thread.active { background: var(--primary-soft); box-shadow: inset 3px 0 0 var(--primary); }
.thread-top { display: flex; gap: 8px; align-items: center; }
.thread-name { font-weight: 700; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-time { margin-left: auto; color: var(--dim); font-size: 11px; }
.thread-preview { margin-top: 5px; color: var(--muted); font-size: 12px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.thread-meta { margin-top: 8px; display: flex; flex-wrap: wrap; gap: 6px; }

.badge { display: inline-flex; align-items: center; gap: 5px; border-radius: 6px; padding: 3px 7px; font-size: 11px; font-weight: 700; white-space: nowrap; }
.badge:before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.ok { color: var(--ok); background: var(--ok-bg); }
.badge.warn { color: var(--warn); background: var(--warn-bg); }
.badge.danger { color: var(--danger); background: var(--danger-bg); }
.badge.info { color: var(--primary); background: var(--primary-soft); }
.badge.neutral { color: var(--muted); background: #f1f5f9; }

.conversation-head { padding: 14px 16px; background: white; border-bottom: 1px solid var(--line); display: flex; align-items: flex-start; gap: 12px; }
.avatar { width: 38px; height: 38px; border-radius: 999px; display: grid; place-items: center; background: var(--primary-soft); color: var(--primary); font-weight: 800; }
.conv-title h2 { margin: 0; font-size: 17px; }
.conv-title p { margin: 2px 0 0; color: var(--dim); font-size: 12px; }
.messages { flex: 1; min-height: 0; overflow: auto; padding: 16px 20px; background: var(--bg); }
.message { display: flex; gap: 9px; max-width: 88%; margin-bottom: 14px; }
.message.customer { margin-left: auto; flex-direction: row-reverse; }
.mini-avatar { width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; background: #e8eef6; color: var(--muted); font-size: 11px; font-weight: 800; flex: none; }
.message.customer .mini-avatar { background: var(--primary); color: white; }
.message-meta { display: flex; gap: 8px; color: var(--dim); font-size: 11px; margin-bottom: 5px; }
.message.customer .message-meta { justify-content: flex-end; }
.bubble { padding: 10px 13px; border-radius: 10px; border: 1px solid var(--line); background: white; box-shadow: 0 1px 2px rgba(15,23,42,.04); }
.message.customer .bubble { background: var(--primary); color: white; border-color: var(--primary); }
.message.system { max-width: 100%; margin-left: 0; }
.message.system .bubble { background: #fffbeb; border-color: #fde68a; color: var(--warn); }
.source-line { margin-top: 5px; color: var(--muted); font-size: 11px; display: flex; flex-wrap: wrap; gap: 5px; }
.source-line span { border: 1px solid var(--line); border-radius: 5px; padding: 1px 5px; background: white; }

.composer { border-top: 1px solid var(--line); background: white; padding: 12px 14px; display: grid; gap: 10px; }
.action-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.btn { border: 1px solid var(--line-strong); background: white; color: var(--muted); border-radius: 8px; height: 32px; padding: 0 11px; font-weight: 700; font-size: 12px; }
.btn:hover { border-color: var(--primary); color: var(--primary); }
.btn.primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn.primary:hover { background: var(--primary-dark); }
.btn.danger { color: var(--danger); border-color: #fca5a5; }
.btn.danger:hover { background: var(--danger-bg); }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.card { border: 1px solid var(--line); border-radius: 12px; background: white; padding: 12px; margin-bottom: 12px; }
.card h3 { margin: 0 0 9px; font-size: 13px; }
.kv { display: grid; grid-template-columns: 94px 1fr; gap: 7px; font-size: 12px; margin: 5px 0; }
.kv span:first-child { color: var(--dim); }
.tags { display: flex; flex-wrap: wrap; gap: 5px; }
.tag { border: 1px solid var(--line); border-radius: 6px; padding: 2px 6px; color: var(--muted); background: var(--surface-alt); font-size: 11px; }
.metric { margin: 8px 0; }
.metric-top { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); }
.bar { height: 8px; border-radius: 999px; background: var(--sunken); overflow: hidden; margin-top: 5px; }
.bar i { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), var(--primary)); }

.page-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.wide { grid-column: 1 / -1; }
.table { width: 100%; border-collapse: collapse; font-size: 12px; }
.table th, .table td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.table th { color: var(--muted); background: var(--surface-alt); font-weight: 800; }
.table small { display: block; color: var(--dim); line-height: 1.45; margin-top: 3px; }
.settings-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.switch-row { display: flex; gap: 10px; align-items: flex-start; justify-content: space-between; border-bottom: 1px solid var(--line); padding: 10px 0; }
.switch-row:last-child { border-bottom: 0; }
.switch-row b { display: block; }
.switch-row small { color: var(--muted); }
.number { width: 96px; }

.customer-info-page { display: grid; gap: 14px; }
.customer-kpis { display: grid; grid-template-columns: repeat(8, minmax(112px, 1fr)); gap: 10px; }
.kpi-mini { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; box-shadow: 0 1px 2px rgba(15,23,42,.04); }
.kpi-mini span { display: block; color: var(--muted); font-size: 12px; }
.kpi-mini b { display: block; margin-top: 3px; font-size: 22px; line-height: 1.2; }
.kpi-mini .badge { margin-top: 8px; width: 22px; justify-content: center; }
.customer-toolbar .panel-head { gap: 8px; }
.customer-filter-grid { display: grid; grid-template-columns: minmax(220px, 1.7fr) repeat(4, minmax(128px, 1fr)); gap: 10px; overflow: visible; }
.customer-filter-grid label { display: grid; gap: 5px; color: var(--muted); font-size: 12px; font-weight: 700; }
.customer-table-panel { min-height: 0; }
.table-wrap { overflow: auto; max-width: 100%; }
.customer-table { min-width: 1680px; }
.customer-table tbody tr { cursor: pointer; }
.customer-table tbody tr:hover { background: #f8fbff; }
.customer-table tbody tr.active { background: var(--primary-soft); box-shadow: inset 3px 0 0 var(--primary); }
.customer-table td:first-child b { display: block; font-size: 13px; }
.sort-btn { width: 100%; border: 0; background: transparent; color: inherit; font-size: inherit; font-weight: 800; padding: 0; text-align: left; }
.sort-btn:hover { color: var(--primary); }
.row-actions { display: flex; flex-wrap: wrap; gap: 6px; min-width: 218px; }
.empty-state { text-align: center; color: var(--muted); padding: 40px 18px; }
.empty-state h2, .empty-state h3 { color: var(--text); margin: 0 0 6px; }

.customer-drawer { position: fixed; inset: 0; z-index: 20; display: flex; justify-content: flex-end; }
.drawer-backdrop { position: absolute; inset: 0; background: rgba(15,23,42,.35); }
.drawer-panel { position: relative; width: min(720px, 96vw); height: 100%; background: var(--surface); box-shadow: -18px 0 42px rgba(15,23,42,.18); display: flex; flex-direction: column; }
.drawer-head { padding: 16px 18px; border-bottom: 1px solid var(--line); display: flex; align-items: flex-start; gap: 10px; }
.drawer-head h2 { margin: 0; font-size: 18px; }
.drawer-head p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.drawer-tabs { padding: 10px 12px; border-bottom: 1px solid var(--line); display: flex; gap: 6px; overflow-x: auto; }
.drawer-tabs button { flex: none; border: 1px solid var(--line); border-radius: 999px; background: var(--surface-alt); color: var(--muted); height: 28px; padding: 0 10px; font-size: 12px; font-weight: 700; }
.drawer-tabs button.active { border-color: var(--primary); background: var(--primary-soft); color: var(--primary); }
.drawer-body { flex: 1; overflow: auto; padding: 14px; background: var(--bg); }
.detail-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.detail-grid .wide { grid-column: 1 / -1; }

.simulator-shell { grid-template-columns: 320px 1fr; }
.simulator-sidebar { min-width: 0; }
.sim-sidebar-actions { padding: 12px; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; border-bottom: 1px solid var(--line); }
.sim-customer-list { overflow: auto; padding: 8px; display: grid; gap: 8px; }
.sim-customer { width: 100%; border: 1px solid var(--line); border-radius: 12px; background: white; padding: 10px; display: grid; grid-template-columns: 30px 1fr auto; gap: 9px; align-items: start; text-align: left; }
.sim-customer:hover { border-color: var(--primary); background: var(--surface-alt); }
.sim-customer.active { border-color: var(--primary); background: var(--primary-soft); box-shadow: inset 3px 0 0 var(--primary); }
.sim-customer b { display: block; line-height: 1.2; }
.sim-customer small { display: block; color: var(--dim); font-size: 11px; line-height: 1.35; margin-top: 3px; max-width: 150px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.avatar.sm { width: 30px; height: 30px; font-size: 12px; }
.simulator-content { min-height: calc(100vh - 64px); display: grid; grid-template-columns: minmax(420px, 1fr) 360px; gap: 14px; }
.sim-chat, .sim-status { height: calc(100vh - 100px); }
.sim-messages { min-height: 0; }
.sim-composer .hint { color: var(--muted); }
.typing-bubble { display: inline-flex; gap: 4px; align-items: center; min-width: 54px; }
.typing-bubble i { display: block; width: 7px; height: 7px; border-radius: 50%; background: var(--dim); animation: typingPulse 1s infinite ease-in-out; }
.typing-bubble i:nth-child(2) { animation-delay: .15s; }
.typing-bubble i:nth-child(3) { animation-delay: .3s; }
.audit-mini { border-bottom: 1px solid var(--line); padding: 7px 0; }
.audit-mini:last-child { border-bottom: 0; }
.audit-mini b, .audit-mini small { display: block; }
.audit-mini small { color: var(--muted); }

@keyframes typingPulse {
  0%, 80%, 100% { transform: translateY(0); opacity: .35; }
  40% { transform: translateY(-3px); opacity: 1; }
}

@media (max-width: 1180px) {
  .sales-grid { grid-template-columns: 280px minmax(380px,1fr); }
  .sales-grid > .details-panel { display: none; }
  .page-grid, .settings-grid, .simulator-content { grid-template-columns: 1fr; }
  .customer-kpis { grid-template-columns: repeat(4, minmax(112px, 1fr)); }
  .customer-filter-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .sim-chat, .sim-status { height: auto; min-height: 360px; }
}

@media (max-width: 760px) {
  .app-shell, .simulator-shell { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .content { padding: 10px; }
  .sales-grid { height: auto; grid-template-columns: 1fr; }
  .panel { min-height: 360px; }
  .customer-kpis, .customer-filter-grid, .detail-grid { grid-template-columns: 1fr; }
  .drawer-head { flex-wrap: wrap; }
}
