:root {
  --primary: #4b5563;
  --primary-hover: #374151;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --success: #22c55e;
  --bg: #f5f5f5;
  --card: #ffffff;
  --border: #e5e5e5;
  --text: #1a1a1a;
  --text-muted: #6b7280;
  --radius: 8px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  position: relative;
}
body::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='400'%3E%3Cg stroke='rgba(99,102,241,0.06)' stroke-width='0.8'%3E%3Cline x1='40' y1='60' x2='120' y2='30'/%3E%3Cline x1='120' y1='30' x2='200' y2='80'/%3E%3Cline x1='200' y1='80' x2='300' y2='40'/%3E%3Cline x1='300' y1='40' x2='360' y2='100'/%3E%3Cline x1='40' y1='60' x2='80' y2='150'/%3E%3Cline x1='120' y1='30' x2='160' y2='120'/%3E%3Cline x1='200' y1='80' x2='160' y2='120'/%3E%3Cline x1='80' y1='150' x2='160' y2='120'/%3E%3Cline x1='160' y1='120' x2='260' y2='160'/%3E%3Cline x1='300' y1='40' x2='260' y2='160'/%3E%3Cline x1='360' y1='100' x2='380' y2='200'/%3E%3Cline x1='260' y1='160' x2='380' y2='200'/%3E%3Cline x1='80' y1='150' x2='40' y2='240'/%3E%3Cline x1='160' y1='120' x2='120' y2='240'/%3E%3Cline x1='260' y1='160' x2='320' y2='260'/%3E%3Cline x1='380' y1='200' x2='320' y2='260'/%3E%3Cline x1='40' y1='240' x2='120' y2='240'/%3E%3Cline x1='120' y1='240' x2='200' y2='280'/%3E%3Cline x1='200' y1='280' x2='320' y2='260'/%3E%3Cline x1='40' y1='240' x2='100' y2='340'/%3E%3Cline x1='200' y1='280' x2='180' y2='360'/%3E%3Cline x1='320' y1='260' x2='360' y2='350'/%3E%3Cline x1='100' y1='340' x2='180' y2='360'/%3E%3Cline x1='180' y1='360' x2='280' y2='380'/%3E%3Cline x1='280' y1='380' x2='360' y2='350'/%3E%3Cline x1='360' y1='100' x2='400' y2='60'/%3E%3Cline x1='380' y1='200' x2='400' y2='240'/%3E%3Cline x1='360' y1='350' x2='400' y2='400'/%3E%3Cline x1='0' y1='60' x2='40' y2='60'/%3E%3Cline x1='0' y1='240' x2='40' y2='240'/%3E%3Cline x1='100' y1='340' x2='0' y2='400'/%3E%3C/g%3E%3Cg fill='rgba(99,102,241,0.08)'%3E%3Ccircle cx='40' cy='60' r='3'/%3E%3Ccircle cx='120' cy='30' r='4'/%3E%3Ccircle cx='200' cy='80' r='3'/%3E%3Ccircle cx='300' cy='40' r='3.5'/%3E%3Ccircle cx='360' cy='100' r='2.5'/%3E%3Ccircle cx='80' cy='150' r='3'/%3E%3Ccircle cx='160' cy='120' r='5'/%3E%3Ccircle cx='260' cy='160' r='3.5'/%3E%3Ccircle cx='380' cy='200' r='3'/%3E%3Ccircle cx='40' cy='240' r='3'/%3E%3Ccircle cx='120' cy='240' r='4'/%3E%3Ccircle cx='200' cy='280' r='3.5'/%3E%3Ccircle cx='320' cy='260' r='3'/%3E%3Ccircle cx='100' cy='340' r='3'/%3E%3Ccircle cx='180' cy='360' r='4'/%3E%3Ccircle cx='280' cy='380' r='2.5'/%3E%3Ccircle cx='360' cy='350' r='3'/%3E%3C/g%3E%3C/svg%3E");
  background-size: 400px 400px;
}
body > * {
  position: relative;
  z-index: 1;
}

/* ── Auth Gate ─────────────────────────────────────────────────────────── */
#auth-gate {
  min-height: 100vh;
  background: var(--bg);
}
#auth-gate .app-header {
  margin-bottom: 0;
}
.auth-gate-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: calc(100vh - 56px);
}
.auth-card {
  background: var(--card);
  border-radius: 12px;
  padding: 32px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 4px 24px rgba(0,0,0,.08);
}
.auth-card h2 {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 20px;
  text-align: center;
  color: var(--text);
}
.auth-card label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 4px;
}
.auth-card input[type="email"],
.auth-card input[type="password"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  margin-bottom: 14px;
  font-family: inherit;
}
.auth-card input:focus {
  outline: none;
  border-color: #6366f1;
  box-shadow: 0 0 0 3px rgba(99,102,241,.12);
}
.auth-card .btn {
  width: 100%;
  padding: 10px;
  font-size: 15px;
  margin-top: 4px;
}
.auth-error {
  background: #fee2e2;
  color: #991b1b;
  padding: 8px 12px;
  border-radius: var(--radius);
  font-size: 13px;
  margin-bottom: 14px;
}

/* ── Layout ────────────────────────────────────────────────────────────── */
.app-header {
  background: #312e81;
  color: #fff;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.header-logo {
  font-size: 20px;
  font-weight: 700;
  color: #fff;
  text-decoration: none;
}
.header-logo:hover { opacity: .9; }
.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}
.user-email {
  font-size: 13px;
  opacity: .85;
}

.container {
  max-width: 960px;
  margin: 0 auto;
  padding: 24px 16px;
}

/* ── Cards ─────────────────────────────────────────────────────────────── */
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
  margin-bottom: 16px;
}
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  user-select: none;
}
.card-header h2 {
  font-size: 16px;
  margin: 0;
  color: var(--text);
}
.chevron::after {
  content: "\25BE";
  display: inline-block;
  font-size: 14px;
  color: var(--text-muted);
  transition: transform .2s;
}
.card.collapsed .chevron::after {
  transform: rotate(-90deg);
}
.card-body {
  margin-top: 16px;
}
.card.collapsed .card-body {
  display: none;
}

/* ── Buttons ───────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s;
}
.btn-primary { background: #6366f1; color: #fff; }
.btn-primary:hover { background: #4f46e5; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-hover); }
.btn-sm { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }

/* ── KB Selector ───────────────────────────────────────────────────────── */
.kb-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 20px;
  padding: 12px 16px;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
}
.kb-selector {
  padding: 10px 14px;
  border: 2px solid #6366f1;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 700;
  background: var(--card);
  color: var(--text);
  flex: 1;
  min-width: 0;
}

/* ── Analytics Controls ────────────────────────────────────────────────── */
.analytics-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 20px;
  padding: 12px 16px;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
}
.leads-period-btn {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
  color: var(--text-muted);
}
.leads-period-btn:hover {
  border-color: #6366f1;
  color: #6366f1;
}
.leads-period-btn.active {
  background: #6366f1;
  border-color: #6366f1;
  color: #fff;
}

/* ── KPI Grid ──────────────────────────────────────────────────────────── */
.analytics-kpi-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  margin-bottom: 16px;
}
.analytics-kpi {
  text-align: center;
  padding: 24px 16px;
  border-radius: var(--radius);
  background: linear-gradient(135deg, #f9fafb 0%, #fff 100%);
  border: 1px solid var(--border);
}
.analytics-kpi-value {
  display: block;
  font-size: 32px;
  font-weight: 800;
  color: var(--text);
  line-height: 1.1;
}
.analytics-kpi-label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  margin-top: 6px;
  text-transform: uppercase;
  letter-spacing: .5px;
}

/* ── Conversation Rows ─────────────────────────────────────────────────── */
.convo-row {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 6px;
  cursor: pointer;
  transition: box-shadow .15s;
}
.convo-row:hover {
  box-shadow: 0 1px 4px rgba(0,0,0,.08);
}
.convo-summary {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
}
.convo-time {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
  min-width: 130px;
}
.convo-question {
  flex: 1;
  font-size: 13px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.convo-chevron::after {
  content: "\25B8";
  font-size: 12px;
  color: var(--text-muted);
  transition: transform .2s;
}
.convo-row.expanded .convo-chevron::after {
  transform: rotate(90deg);
}
.convo-detail {
  display: none;
  padding: 0 14px 12px;
  border-top: 1px solid var(--border);
}
.convo-row.expanded .convo-detail {
  display: block;
}
.convo-detail-row {
  font-size: 13px;
  line-height: 1.6;
  padding: 4px 0;
  word-break: break-word;
}
.convo-detail-row strong {
  color: var(--text-muted);
  font-weight: 600;
  margin-right: 4px;
}

/* ── Chat Messages ────────────────────────────────────────────────────── */
.chat-msg {
  padding: 8px 12px;
  border-radius: 8px;
  margin-bottom: 6px;
  font-size: 13px;
  line-height: 1.5;
  word-break: break-word;
}
.chat-msg-user {
  background: #eef2ff;
  border-left: 3px solid #6366f1;
}
.chat-msg-bot {
  background: #f3f4f6;
  border-left: 3px solid #9ca3af;
}
.chat-msg strong {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .3px;
  display: block;
  margin-bottom: 2px;
}
.chat-msg-user strong { color: #6366f1; }
.chat-msg-bot strong { color: #6b7280; }

/* ── Toast ─────────────────────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  padding: 12px 20px;
  border-radius: var(--radius);
  color: #fff;
  font-size: 14px;
  z-index: 10000;
  opacity: 0;
  transform: translateY(10px);
  transition: opacity .3s, transform .3s;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast-success { background: var(--success); }
.toast-error { background: var(--danger); }

/* ── Spinner ───────────────────────────────────────────────────────────── */
.spinner {
  display: inline-block;
  width: 16px; height: 16px;
  border: 2px solid rgba(0,0,0,.1);
  border-top-color: #6366f1;
  border-radius: 50%;
  animation: spin .6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty-state {
  text-align: center;
  padding: 40px;
  color: var(--text-muted);
}

/* ── Mobile ────────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  .app-header {
    padding: 12px 16px;
    flex-wrap: wrap;
    gap: 8px;
  }
  .app-header h1 { font-size: 16px; }
  .header-right { gap: 8px; }
  .user-email { font-size: 12px; }

  .container { padding: 16px 12px; }

  .card { padding: 14px; }

  .btn { padding: 6px 12px; font-size: 13px; }
  .btn-sm { padding: 4px 8px; font-size: 12px; }

  .auth-card { padding: 20px 16px; margin: 0 12px; }
  .auth-gate-body { min-height: calc(100vh - 50px); }

  .kb-bar { flex-wrap: wrap; }
  .kb-selector { width: 100%; flex: none; }

  .analytics-controls { flex-wrap: wrap; }
  .analytics-kpi-grid { grid-template-columns: 1fr; }
  .convo-time { min-width: 0; font-size: 11px; }
  .convo-summary { gap: 6px; padding: 8px 10px; }
}
