/* WeChat-CRM Bridge - UI v2
   Design tokens align with the Balsamiq wireframe set (mockup/).
*/

:root {
  --bg: #f6f8fb;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-muted: #475569;
  --text-faint: #94a3b8;

  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: #eef2ff;

  --success: #16a34a;
  --success-soft: #dcfce7;
  --warning: #d97706;
  --warning-soft: #fef3c7;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --info: #0284c7;
  --info-soft: #e0f2fe;

  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.08);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;

  --sidebar-w: 232px;
  --topbar-h: 60px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ===== Layout ===== */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
}

.sidebar {
  background: #0f172a;
  color: #e2e8f0;
  padding: 18px 12px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 8px 18px 8px;
  border-bottom: 1px solid #1e293b;
  margin-bottom: 12px;
}
.sidebar-brand .logo {
  width: 28px; height: 28px;
  border-radius: 8px;
  background: linear-gradient(135deg, #6366f1, #22d3ee);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: white;
}
.sidebar-brand .name { font-weight: 600; font-size: 14px; }
.sidebar-brand .sub  { font-size: 11px; color: #94a3b8; }

.nav-section { margin-top: 6px; }
.nav-section .title {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #64748b;
  padding: 10px 8px 4px;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  color: #cbd5e1;
  cursor: pointer;
  font-size: 13px;
  user-select: none;
}
.nav-item:hover { background: #1e293b; color: #fff; }
.nav-item.active { background: var(--primary); color: #fff; }
.nav-item .dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: #475569;
}
.nav-item.active .dot { background: #fff; }

.main { display: flex; flex-direction: column; min-width: 0; }

.topbar {
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  position: sticky; top: 0; z-index: 10;
}
.topbar .search {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 7px 12px;
  width: 320px;
  max-width: 40vw;
}
.topbar .search input {
  border: 0;
  background: transparent;
  outline: none;
  width: 100%;
  font-size: 13px;
  color: var(--text);
}
.topbar .actions { display: flex; align-items: center; gap: 10px; }
.user-chip {
  display: flex; align-items: center; gap: 8px;
  padding: 4px 8px 4px 4px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  font-size: 12px;
  color: var(--text-muted);
}
.user-chip .avatar {
  width: 26px; height: 26px; border-radius: 50%;
  background: linear-gradient(135deg, #6366f1, #22d3ee);
  color: white; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px;
}

.content {
  padding: 22px 24px 64px;
  max-width: 1320px;
  width: 100%;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
  gap: 16px;
  flex-wrap: wrap;
}
.page-title h1 {
  margin: 0 0 4px 0;
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.page-title p {
  margin: 0;
  color: var(--text-muted);
  font-size: 13px;
}

/* ===== Cards & sections ===== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: 18px;
}
.card + .card { margin-top: 16px; }
.card h3 { margin: 0 0 12px 0; font-size: 14px; font-weight: 600; }
.card .subtitle {
  font-size: 12px;
  color: var(--text-muted);
  margin: -6px 0 12px 0;
}

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
@media (max-width: 1100px) { .grid-4 { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 760px)  { .grid-2, .grid-4 { grid-template-columns: 1fr; } }

/* ===== KPI cards ===== */
.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px;
  box-shadow: var(--shadow-sm);
}
.kpi .label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}
.kpi .value {
  font-size: 24px;
  font-weight: 700;
  margin: 6px 0 4px;
  letter-spacing: -0.01em;
}
.kpi .delta {
  font-size: 11px;
  color: var(--success);
}
.kpi .delta.neg { color: var(--danger); }
.kpi .delta.neutral { color: var(--text-muted); }

/* ===== Forms ===== */
.form-row { margin-bottom: 10px; }
label.field-label {
  display: block;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 4px;
}
input, textarea, select {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 13px;
  color: var(--text);
  font-family: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
}
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.15);
}

button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  background: var(--primary);
  color: white;
  transition: background-color 0.15s, transform 0.05s;
  font-family: inherit;
}
button:hover, .btn:hover { background: var(--primary-hover); }
button:active, .btn:active { transform: translateY(1px); }
button[disabled] { opacity: 0.5; cursor: not-allowed; }

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border-strong);
}
.btn-secondary:hover { background: var(--surface-2); }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border-color: transparent;
}
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }

.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-row { display: flex; gap: 6px; flex-wrap: wrap; }

/* ===== Tables ===== */
.table-wrap { overflow-x: auto; }
table.data {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 13px;
}
table.data th, table.data td {
  padding: 10px 12px;
  text-align: left;
  vertical-align: top;
  border-bottom: 1px solid var(--border);
}
table.data thead th {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
}
table.data tbody tr:hover { background: var(--surface-2); }
table.data td.muted { color: var(--text-muted); }

/* ===== Badges ===== */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 500;
  background: var(--surface-2);
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.badge .dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: currentColor;
  opacity: 0.7;
}
.badge.ok      { background: var(--success-soft); color: #166534; border-color: #bbf7d0; }
.badge.warn    { background: var(--warning-soft); color: #92400e; border-color: #fde68a; }
.badge.err     { background: var(--danger-soft);  color: #991b1b; border-color: #fecaca; }
.badge.info    { background: var(--info-soft);    color: #075985; border-color: #bae6fd; }
.badge.primary { background: var(--primary-soft); color: #3730a3; border-color: #c7d2fe; }

/* ===== Filter bar ===== */
.filter-bar {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.filter-bar input,
.filter-bar select {
  width: auto;
  min-width: 140px;
}

/* ===== Empty state ===== */
.empty {
  padding: 32px 16px;
  text-align: center;
  color: var(--text-muted);
}
.empty h4 { margin: 8px 0 4px; color: var(--text); font-weight: 600; }
.empty p  { margin: 0; font-size: 13px; }
.empty .emoji { font-size: 28px; }

/* ===== Toasts ===== */
.toast-host {
  position: fixed;
  right: 22px;
  bottom: 22px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 1000;
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  min-width: 260px;
  max-width: 380px;
  padding: 11px 14px;
  border-radius: var(--radius-md);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-md);
  font-size: 13px;
  color: var(--text);
  display: flex;
  align-items: flex-start;
  gap: 10px;
  animation: toast-in 0.18s ease-out;
}
.toast .icon { font-size: 16px; line-height: 1.1; }
.toast.success { border-left: 3px solid var(--success); }
.toast.error   { border-left: 3px solid var(--danger); }
.toast.info    { border-left: 3px solid var(--info); }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ===== Small utilities ===== */
.small { font-size: 12px; color: var(--text-muted); }
.mono  { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.flex  { display: flex; }
.gap-8 { gap: 8px; }
.gap-12 { gap: 12px; }
.between { justify-content: space-between; }
.align-center { align-items: center; }
.hidden { display: none !important; }
.page { display: none; }
.page.active { display: block; }

/* ===== Sidebar on small screens ===== */
@media (max-width: 880px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    left: 0; top: 0;
    width: 260px;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    z-index: 100;
  }
  .sidebar.open { transform: translateX(0); }
  .topbar .menu-btn { display: inline-flex !important; }
}
.topbar .menu-btn { display: none; }
