/* Use system font stack with Inter if available, else fallback */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

/* ============================================================
   LeadAI Design System — Dark-first, premium AI SaaS
   ============================================================ */

:root {
  /* Base surface hierarchy (luminance-based elevation in dark mode) */
  --bg: #0a0c10;
  --bg-elevated: #0e1117;
  --bg-card: #13161d;
  --bg-muted: #1a1e26;
  --bg-hover: #1f242e;

  /* Borders */
  --border: #232936;
  --border-soft: #1a1f28;
  --border-strong: #2c3343;

  /* Text */
  --text-primary: #f2f4f8;
  --text-secondary: #b4bdcc;
  --text-muted: #7c8594;
  --text-faint: #565e6b;

  /* Accent */
  --accent: #4f7cff;
  --accent-soft: rgba(79, 124, 255, 0.12);
  --accent-border: rgba(79, 124, 255, 0.35);
  --accent-strong: #3d6af5;

  /* Semantic */
  --success: #34d399;
  --success-soft: rgba(52, 211, 153, 0.12);
  --warning: #fbbf24;
  --warning-soft: rgba(251, 191, 36, 0.12);
  --danger: #f87171;
  --danger-soft: rgba(248, 113, 113, 0.12);

  /* Radii */
  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 14px;
  --r-xl: 18px;

  /* Spacing scale */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;

  /* Layout */
  --sidebar-w: 280px;
  --header-h: 60px;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;

  /* Motion */
  --ease: cubic-bezier(0.4, 0, 0.2, 1);
  --dur-fast: 120ms;
  --dur: 200ms;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

input,
textarea,
select {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 10px 14px;
  outline: none;
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

input::placeholder,
textarea::placeholder {
  color: var(--text-faint);
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

a {
  color: var(--accent);
  text-decoration: none;
}

/* ============ Layout ============ */
.app {
  display: flex;
  height: 100vh;
  height: 100dvh;
  position: relative;
}

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

/* ============ Scrollbar ============ */
::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 8px;
  border: 2px solid var(--bg);
}
::-webkit-scrollbar-thumb:hover {
  background: var(--text-faint);
}
::-webkit-scrollbar-track {
  background: transparent;
}

/* ============ Buttons ============ */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 9px 16px;
  border-radius: var(--r-md);
  font-size: 14px;
  font-weight: 500;
  background: var(--bg-muted);
  color: var(--text-primary);
  border: 1px solid var(--border);
  transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease),
    transform var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
  white-space: nowrap;
}

.btn:hover {
  background: var(--bg-hover);
  border-color: var(--border-strong);
}

.btn:active {
  transform: translateY(1px);
}

.btn-primary {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-strong);
  border-color: transparent;
}

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-secondary);
}

.btn-ghost:hover {
  background: var(--bg-muted);
  color: var(--text-primary);
}

.btn-danger {
  background: var(--danger-soft);
  border-color: transparent;
  color: var(--danger);
}

.btn-danger:hover {
  background: rgba(248, 113, 113, 0.22);
}

.btn-icon {
  padding: 8px;
  border-radius: var(--r-md);
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn-sm {
  padding: 5px 10px;
  font-size: 13px;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-lg {
  padding: 12px 20px;
  font-size: 15px;
}

/* ============ Sidebar ============ */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-elevated);
  border-right: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: transform var(--dur) var(--ease), width var(--dur) var(--ease);
  position: relative;
  z-index: 30;
}

.sidebar.collapsed {
  width: 0;
  border-right: none;
}

.sidebar-header {
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid var(--border-soft);
}

.logo {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--accent), #7b5cff);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  flex-shrink: 0;
}

.logo-title {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.logo-sub {
  font-size: 11px;
  color: var(--text-muted);
  letter-spacing: 0.02em;
}

.new-chat-btn {
  margin: 14px 16px;
  width: calc(100% - 32px);
  justify-content: center;
  background: var(--accent);
  border: none;
  color: #fff;
  font-weight: 600;
}

.sidebar-nav {
  padding: 8px 12px;
  flex: 1;
  overflow-y: auto;
  max-height: 38vh;
}

.nav-section-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-faint);
  padding: 8px 12px 6px;
  font-weight: 600;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: var(--r-md);
  color: var(--text-secondary);
  font-size: 14px;
  width: 100%;
  text-align: left;
  margin-bottom: 1px;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}

.nav-item:hover {
  background: var(--bg-muted);
  color: var(--text-primary);
}

.nav-item.active {
  background: var(--accent-soft);
  color: var(--text-primary);
}

.nav-item svg {
  flex-shrink: 0;
}

.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--border-soft);
}

.user-area {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border-radius: var(--r-md);
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease);
}

.user-area:hover {
  background: var(--bg-muted);
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: linear-gradient(135deg, #7b5cff, var(--accent));
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  flex-shrink: 0;
}

.user-name {
  font-size: 14px;
  font-weight: 500;
}

.user-email {
  font-size: 12px;
  color: var(--text-muted);
}

/* Chat history list */
.chat-list {
  padding: 0 12px 8px;
}

.chat-list-group {
  margin-bottom: 8px;
}

.chat-list-empty {
  font-size: 13px;
  color: var(--text-faint);
  padding: 8px 12px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 8px;
  border-radius: var(--r-md);
  color: var(--text-secondary);
  font-size: 13.5px;
  width: 100%;
  text-align: left;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
  position: relative;
}

.history-item:hover {
  background: var(--bg-muted);
  color: var(--text-primary);
}

.history-item.active {
  background: var(--accent-soft);
  color: var(--text-primary);
}

.history-item-text {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-item-actions {
  display: none;
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  background: var(--bg-muted);
  border-radius: var(--r-sm);
  padding: 2px;
}

.history-item:hover .history-item-actions {
  display: flex;
}

.history-item-actions button {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--r-sm);
  color: var(--text-muted);
}

.history-item-actions button:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

/* ============ Header / Top bar ============ */
.topbar {
  height: var(--header-h);
  border-bottom: 1px solid var(--border-soft);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
  background: var(--bg);
  flex-shrink: 0;
  position: relative;
}

.topbar-title {
  flex: 1;
  font-size: 15px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mobile-menu-btn {
  display: none;
}

/* Header mini-menu (mobile overflow): kebab button + popover */
.header-menu-btn {
  display: none;
  padding: 8px;
  border-radius: var(--r-md);
  width: 42px;
  height: 42px;
  align-items: center;
  justify-content: center;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  color: var(--text-primary);
}

.header-menu-btn:hover {
  background: var(--bg-muted);
}

.header-menu-btn svg {
  width: 20px;
  height: 20px;
}

.header-menu {
  position: absolute;
  top: calc(var(--header-h) + env(safe-area-inset-top, 0px) + 8px);
  right: 12px;
  min-width: 190px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  z-index: 65;
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  animation: menuIn var(--dur-fast) var(--ease);
}

@keyframes menuIn {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: translateY(0); }
}

.header-menu[hidden] {
  display: none;
}

.header-menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: calc(var(--r-md) - 3px);
  background: transparent;
  border: none;
  color: var(--text-primary);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  text-align: left;
  width: 100%;
  min-height: 44px;
}

.header-menu-item svg {
  color: var(--text-secondary);
  flex-shrink: 0;
}

.header-menu-item:hover {
  background: var(--bg-muted);
  color: var(--text-primary);
}

.header-menu-item:hover svg {
  color: var(--accent);
}


.status-indicator {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-muted);
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--bg-card);
  border: 1px solid var(--border);
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--success);
}

.status-dot.offline {
  background: var(--text-faint);
}

/* ============ Main content area ============ */
.main-content {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  position: relative;
  display: flex;
  flex-direction: column;
}

/* ============ Chat view ============ */
.chat-view {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.chat-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.chat-container {
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  padding: 24px 20px;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

/* Empty state */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 40px 20px;
}

.empty-logo {
  width: 56px;
  height: 56px;
  border-radius: 16px;
  background: linear-gradient(135deg, var(--accent), #7b5cff);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  margin-bottom: 20px;
  box-shadow: 0 8px 30px var(--accent-soft);
}

.empty-title {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.03em;
  margin: 0 0 10px;
}

.empty-subtitle {
  color: var(--text-secondary);
  font-size: 15.5px;
  max-width: 460px;
  margin: 0 0 32px;
  line-height: 1.6;
}

/* Chat messages */
.msg-group {
  margin-bottom: 28px;
  animation: msgIn var(--dur) var(--ease);
}

@keyframes msgIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg-user {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 4px;
}

.msg-user .msg-bubble {
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  border-radius: var(--r-lg);
  border-bottom-right-radius: 4px;
  padding: 12px 16px;
  max-width: 85%;
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg-ai {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.ai-avatar {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--accent), #7b5cff);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  flex-shrink: 0;
  margin-top: 2px;
}

.msg-ai .msg-content {
  flex: 1;
  min-width: 0;
}

.msg-ai .msg-body {
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.7;
  margin: 0;
}

.msg-body p {
  margin: 0 0 12px;
}

.msg-body p:last-child {
  margin-bottom: 0;
}

.msg-body h1, .msg-body h2, .msg-body h3 {
  margin: 16px 0 8px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.msg-body ul, .msg-body ol {
  padding-left: 22px;
  margin: 8px 0;
}

.msg-body li {
  margin-bottom: 4px;
}

/* Empty / no-response state (webhook returned nothing) */
.empty-response {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  padding: 16px 18px;
  border: 1px dashed var(--border);
  border-radius: var(--r-md);
  background: var(--bg-muted);
  color: var(--text-secondary);
}

.empty-response-icon {
  color: var(--accent);
  display: flex;
}

.empty-response-title {
  font-weight: 600;
  color: var(--text-primary);
  font-size: 15px;
}

.empty-response-sub {
  font-size: 13.5px;
  line-height: 1.5;
}

.empty-response .retry-btn {
  margin-top: 6px;
}

.msg-body code {
  background: var(--bg-muted);
  border: 1px solid var(--border);
  padding: 2px 6px;
  border-radius: 5px;
  font-size: 13.5px;
}

.msg-body a {
  text-decoration: underline;
  text-underline-offset: 3px;
}

/* ============ Keyword branch ============ */
.keyword-branch {
  margin-top: 14px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  overflow: hidden;
  animation: msgIn var(--dur) var(--ease);
}

.keyword-branch-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-soft);
  background: var(--bg-elevated);
}

.keyword-branch-title {
  flex: 1;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 8px;
}

.keyword-branch-title svg {
  color: var(--accent);
}

.keyword-branch-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.keyword-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 16px;
}

.kw-tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--bg-muted);
  border: 1px solid var(--border-strong);
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease),
    color var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}

.kw-tag:hover {
  border-color: var(--accent);
  color: var(--text-primary);
  background: var(--accent-soft);
}

.kw-tag:active {
  transform: scale(0.96);
}

.kw-tag.copied {
  border-color: var(--success);
  color: var(--success);
  background: var(--success-soft);
}

.kw-tag .kw-copy-icon {
  opacity: 0.6;
  display: none;
}

.kw-tag:hover .kw-copy-icon {
  display: inline-flex;
}

.kw-empty {
  padding: 16px;
  color: var(--text-muted);
  font-size: 13.5px;
}

/* ============ Loading / typing indicator ============ */
.loading-state {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 8px 0;
}

.loading-label {
  color: var(--text-muted);
  font-size: 14px;
}

.typing-dots {
  display: inline-flex;
  gap: 4px;
  align-items: center;
}

.typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.2s infinite ease-in-out;
}

.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* ============ Error state ============ */
.error-state {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  background: var(--danger-soft);
  border: 1px solid rgba(248, 113, 113, 0.25);
  border-radius: var(--r-lg);
  padding: 16px;
  animation: msgIn var(--dur) var(--ease);
}

.error-state .error-icon {
  color: var(--danger);
  flex-shrink: 0;
}

.error-state .error-title {
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.error-state .error-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.error-state .err-msg {
  color: var(--text-secondary);
  font-size: 14px;
}

/* ============ Chat input ============ */
.chat-input-wrap {
  padding: 8px 20px 20px;
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
}

/* Likely-words suggestions popover */
.suggest-bar {
  position: relative;
  display: flex;
  justify-content: flex-start;
  margin-bottom: 8px;
}

.suggest-toggle {
  width: auto;
  height: 32px;
  padding: 0 12px;
  gap: 6px;
  border: 1px solid var(--border-soft);
  border-radius: 999px;
  background: var(--bg-elevated);
  color: var(--text-secondary);
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  transition: border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease),
    background var(--dur-fast) var(--ease);
}

.suggest-toggle:hover {
  border-color: var(--accent-border);
  color: var(--text-primary);
  background: var(--bg-muted);
}

.suggest-toggle svg {
  color: var(--accent);
  width: 15px;
  height: 15px;
}

.suggest-pop {
  position: absolute;
  bottom: calc(100% + 10px);
  left: 0;
  z-index: 60;
  width: min(360px, calc(100vw - 24px));
  max-height: 60vh;
  overflow-y: auto;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  padding: 12px;
  animation: popIn 0.16s var(--ease);
}

.suggest-pop[hidden] {
  display: none;
}

@keyframes popIn {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.suggest-pop-head {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin: 0 0 10px;
}

.suggest-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.suggest-chip {
  border: 1px solid var(--border-soft);
  background: var(--bg-card);
  color: var(--text-secondary);
  border-radius: 999px;
  padding: 7px 12px;
  font-size: 13px;
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease),
    color var(--dur-fast) var(--ease);
}

.suggest-chip:hover {
  border-color: var(--accent-border);
  background: var(--accent-soft);
  color: var(--text-primary);
}

.chat-input-container {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  padding: 10px 12px;
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
}

.chat-input-container:focus-within {
  border-color: var(--accent);
  box-shadow: 0 4px 30px rgba(79, 124, 255, 0.12);
}

.chat-input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.chat-input-buttons {
  display: flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}

.chat-textarea {
  flex: 1;
  background: transparent;
  border: none;
  box-shadow: none !important;
  resize: none;
  padding: 6px 4px;
  max-height: 160px;
  min-height: 28px;
  font-size: 15px;
  line-height: 1.6;
  color: var(--text-primary);
}

.chat-textarea:focus {
  border: none;
  box-shadow: none;
}

.chat-hint {
  font-size: 11.5px;
  color: var(--text-faint);
  text-align: center;
  margin-top: 8px;
}

.send-btn {
  width: 38px;
  height: 38px;
  border-radius: var(--r-md);
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--dur-fast) var(--ease), opacity var(--dur-fast) var(--ease);
}

.send-btn:hover:not(:disabled) {
  background: var(--accent-strong);
}

.send-btn:disabled {
  background: var(--bg-muted);
  color: var(--text-muted);
  cursor: not-allowed;
}

.tool-btn {
  width: 34px;
  height: 34px;
  border-radius: var(--r-md);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}

.tool-btn:hover {
  background: var(--bg-muted);
  color: var(--text-primary);
}

/* ============ Drawer / Modal overlay ============ */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(3px);
  z-index: 90;
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--dur) var(--ease), visibility var(--dur) var(--ease);
}

.overlay.open {
  opacity: 1;
  visibility: visible;
}

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  height: 100%;
  width: 420px;
  max-width: 92vw;
  background: var(--bg-elevated);
  border-left: 1px solid var(--border);
  z-index: 100;
  transform: translateX(100%);
  transition: transform var(--dur) var(--ease);
  display: flex;
  flex-direction: column;
  box-shadow: -16px 0 40px rgba(0, 0, 0, 0.4);
}

.drawer.open {
  transform: translateX(0);
}

.drawer-header {
  padding: 20px;
  border-bottom: 1px solid var(--border-soft);
  display: flex;
  align-items: flex-start;
  gap: 12px;
  flex-shrink: 0;
}

.drawer-title {
  font-size: 17px;
  font-weight: 600;
}

.drawer-desc {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 4px;
  line-height: 1.5;
}

.drawer-body {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.drawer-footer {
  padding: 20px;
  border-top: 1px solid var(--border-soft);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  flex-shrink: 0;
  flex-wrap: wrap;
}

.close-btn {
  margin-left: auto;
}

/* Modal */
.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.96);
  width: 520px;
  max-width: 92vw;
  max-height: 88vh;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  z-index: 100;
  display: flex;
  flex-direction: column;
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--dur) var(--ease), visibility var(--dur) var(--ease),
    transform var(--dur) var(--ease);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
}

.modal.open {
  opacity: 1;
  visibility: visible;
  transform: translate(-50%, -50%) scale(1);
}

.modal-lg {
  width: 720px;
}

/* ============ Forms ============ */
.field {
  margin-bottom: 18px;
}

.field-label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.field textarea {
  width: 100%;
  min-height: 120px;
  resize: vertical;
  line-height: 1.6;
}

.field input,
.field select {
  width: 100%;
}

.field-span-2 {
  grid-column: 1 / -1;
}

.seniority-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.seniority-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 13px;
  border-radius: 999px;
  background: var(--bg-card, #1c1f26);
  border: 1px solid var(--border);
  cursor: pointer;
  font-size: 13px;
  text-transform: capitalize;
  color: var(--text-secondary);
  user-select: none;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.seniority-chip input {
  display: none;
}

.seniority-chip.checked {
  background: rgba(124, 92, 255, 0.14);
  border-color: var(--brand, #7c5cff);
  color: var(--text-primary);
}

.checkbox-row {
  display: flex;
  gap: 16px;
  align-items: center;
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 13.5px;
  color: var(--text-secondary);
  cursor: pointer;
  white-space: nowrap;
}

.checkbox-label input[type="checkbox"] {
  width: 15px;
  height: 15px;
  accent-color: var(--brand, #7c5cff);
  cursor: pointer;
}

.field-hint {
  font-size: 12px;
  color: var(--text-faint);
  margin-top: 6px;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  padding: 4px 12px;
  border-radius: 999px;
  margin-top: 8px;
}

.status-badge.manual {
  background: var(--bg-muted);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.status-badge.custom {
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid var(--accent-border);
}

.status-badge .sd-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
}

/* ============ Leads views ============ */
.page-view {
  display: none;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.page-view.active {
  display: flex;
}

.page-head {
  padding: 20px 24px;
  border-bottom: 1px solid var(--border-soft);
  display: flex;
  align-items: center;
  gap: 16px;
}

.page-title {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.page-subtitle {
  font-size: 13.5px;
  color: var(--text-muted);
  margin-top: 2px;
}

.page-body {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}

/* Lead cards */
.lead-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  overflow: hidden;
  transition: border-color var(--dur-fast) var(--ease);
}

.lead-card:hover {
  border-color: var(--border-strong);
}

.lead-card-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-soft);
}

.lead-card-name {
  flex: 1;
  font-weight: 600;
  font-size: 15px;
}

.lead-card-domain {
  font-size: 12.5px;
  color: var(--text-muted);
}

.lead-card-body {
  padding: 14px 16px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 10px;
}

.lead-field {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.lead-field-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-faint);
}

.lead-field-value {
  font-size: 13.5px;
  color: var(--text-secondary);
  word-break: break-word;
}

.lead-card-foot {
  padding: 10px 16px;
  border-top: 1px solid var(--border-soft);
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.checkbox {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  cursor: pointer;
  flex-shrink: 0;
}

.sent-badge {
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 999px;
  font-weight: 500;
  margin-left: auto;
}

.sent-badge.sent {
  background: var(--success-soft);
  color: var(--success);
}

.sent-badge.resend {
  background: var(--warning-soft);
  color: var(--warning);
}

.sent-badge.pending {
  background: var(--bg-muted);
  color: var(--text-muted);
}

.leads-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 16px;
}

.leads-toolbar {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.count-badge {
  font-size: 13px;
  color: var(--text-muted);
  padding: 6px 12px;
  background: var(--bg-card);
  border-radius: 999px;
  border: 1px solid var(--border);
}

/* Loading large states */
.waiting-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  text-align: center;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.elapsed {
  font-size: 15px;
  color: var(--text-primary);
  font-weight: 600;
  margin-bottom: 4px;
}

.waiting-sub {
  color: var(--text-muted);
  font-size: 13.5px;
}

.progress-bar {
  height: 5px;
  background: var(--bg-muted);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 16px;
  width: 260px;
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
  width: 0;
  transition: width 0.3s var(--ease);
}

/* ============ Analytics ============ */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}

.kpi-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 18px;
}

.kpi-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.kpi-value {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.funnel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 24px;
}

.funnel-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.funnel-label {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: var(--text-secondary);
}

.funnel-track {
  height: 26px;
  background: var(--bg-muted);
  border-radius: 6px;
  overflow: hidden;
}

.funnel-fill {
  height: 100%;
  border-radius: 6px;
  transition: width 0.5s var(--ease);
}

.funnel-fill.request { background: var(--accent); }
.funnel-fill.delivered { background: #7b5cff; }
.funnel-fill.open { background: #10b981; }
.funnel-fill.click { background: var(--warning); }

.raw-json {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 14px;
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 12px;
  color: var(--text-secondary);
  overflow-x: auto;
  max-height: 300px;
  white-space: pre-wrap;
  word-break: break-all;
}

/* ============ Bulk actions / stats bar ============ */
.batch-stats {
  display: flex;
  gap: 16px;
  align-items: center;
  font-size: 12.5px;
  color: var(--text-muted);
}

.batch-stats b {
  color: var(--text-primary);
  font-size: 14px;
}

/* ============ Toast ============ */
.toast-container {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
}

.toast {
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  padding: 12px 20px;
  font-size: 14px;
  color: var(--text-primary);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.4);
  animation: toastIn var(--dur) var(--ease);
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 90vw;
}

.toast.success { border-color: var(--success); }
.toast.error { border-color: var(--danger); }

@keyframes toastIn {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ============ Token pills / template preview ============ */
.token-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 10px 0;
}

.token-pill {
  font-size: 12px;
  padding: 4px 10px;
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  border-radius: 999px;
  color: var(--accent);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition: background var(--dur-fast) var(--ease);
}

.token-pill:hover {
  background: rgba(79, 124, 255, 0.22);
}

.preview-box {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 16px;
  margin-top: 10px;
  white-space: pre-wrap;
  line-height: 1.6;
  color: var(--text-secondary);
  font-size: 14px;
}

/* ============ Segmented / tabs ============ */
.seg {
  display: inline-flex;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 3px;
  gap: 2px;
}

.seg-btn {
  padding: 6px 14px;
  border-radius: var(--r-sm);
  font-size: 13px;
  color: var(--text-muted);
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}

.seg-btn.active {
  background: var(--bg-muted);
  color: var(--text-primary);
}

/* ============ Drag drop import ============ */
.dropzone {
  border: 2px dashed var(--border-strong);
  border-radius: var(--r-lg);
  padding: 48px 24px;
  text-align: center;
  color: var(--text-muted);
  transition: border-color var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease);
  cursor: pointer;
}

.dropzone:hover, .dropzone.dragover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.dropzone .dz-icon {
  color: var(--text-faint);
  margin-bottom: 12px;
}

.dropzone .dz-title {
  font-size: 15px;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.dropzone .dz-sub {
  font-size: 13px;
}

/* ============ Passcode modal styles ============ */
.passcode-dots {
  font-family: monospace;
  letter-spacing: 0.3em;
  font-size: 18px;
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 0;
  border-bottom: 1px solid var(--border-soft);
}

.toggle {
  position: relative;
  width: 44px;
  height: 24px;
  background: var(--bg-muted);
  border-radius: 999px;
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease);
  flex-shrink: 0;
}

.toggle.on {
  background: var(--accent);
}

.toggle::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  transition: transform var(--dur-fast) var(--ease);
}

.toggle.on::after {
  transform: translateX(20px);
}

/* Password lock input */
.lock-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  text-align: center;
  gap: 16px;
}

/* ============ Utilities ============ */
.flex { display: flex; }
.flex-col { display: flex; flex-direction: column; }
.items-center { align-items: center; }
.gap-2 { gap: 8px; }
.gap-3 { gap: 12px; }
.mt-0 { margin-top: 0; }
.mb-0 { margin-bottom: 0; }
.mb-1 { margin-bottom: 4px; }
.mb-2 { margin-bottom: 8px; }
.mb-3 { margin-bottom: 12px; }
.mb-4 { margin-bottom: 16px; }
.mt-4 { margin-top: 16px; }
.w-full { width: 100%; }
.flex-1 { flex: 1; }
.text-muted { color: var(--text-muted); }
.text-secondary { color: var(--text-secondary); }
.ml-auto { margin-left: auto; }

/* ============ Responsive ============ */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    transform: translateX(-100%);
    width: 280px;
    box-shadow: 16px 0 40px rgba(0, 0, 0, 0.5);
    z-index: 95;
    transition: transform var(--dur) var(--ease);
  }

  .sidebar.mobile-open {
    transform: translateX(0);
  }

  .sidebar.collapsed {
    width: 280px;
    transform: translateX(-100%);
  }

  .mobile-menu-btn {
    display: inline-flex;
    width: 42px;
    height: 42px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-strong);
    border-radius: var(--r-md);
    color: var(--text-primary);
    box-shadow: 0 1px 0 rgba(255, 255, 255, 0.04);
  }

  .mobile-menu-btn:hover {
    background: var(--bg-muted);
  }

  .mobile-menu-btn svg {
    width: 20px;
    height: 20px;
  }

  .header-menu-btn {
    display: inline-flex;
  }

  .topbar-title {
    font-size: 14px;
  }

  .page-head {
    padding: 16px;
  }

  .page-body {
    padding: 16px;
  }

  .chat-container {
    padding: 16px 12px;
  }

  .chat-input-wrap {
    padding: 8px 12px 16px;
  }

  .drawer {
    width: 100%;
    max-width: 100%;
  }

  .keyword-branch-actions .btn span.btn-label {
    display: none;
  }

  .form-grid {
    grid-template-columns: 1fr;
  }

  /* Safe-area insets for notched phones */
  .topbar {
    padding-top: env(safe-area-inset-top);
    height: calc(var(--header-h) + env(safe-area-inset-top));
  }

  .chat-input-wrap {
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
  }

  .header-actions > .btn {
    width: 40px;
    height: 40px;
  }

  .chat-input-wrap .chat-textarea {
    min-height: 46px;
  }

  .send-btn {
    width: 46px;
    height: 46px;
  }
}

@media (max-width: 480px) {
  .status-indicator span.status-text {
    display: none;
  }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
