:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --text: #1a1f36;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --success: #16a34a;
  --danger: #dc2626;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 4px 12px rgba(16, 24, 40, 0.06);

  --sidebar-bg: #14151f;
  --sidebar-bg-alt: #1b1d2b;
  --sidebar-text: rgba(255, 255, 255, 0.62);
  --sidebar-text-strong: #ffffff;
  --sidebar-border: rgba(255, 255, 255, 0.08);
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(120, 120, 140, 0.35); border-radius: 999px; }
::-webkit-scrollbar-thumb:hover { background: rgba(120, 120, 140, 0.55); }

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

@keyframes pulseSkeleton {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}

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

/* ---- Layout shell ---- */
.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 246px;
  flex-shrink: 0;
  background: linear-gradient(180deg, var(--sidebar-bg), var(--sidebar-bg-alt));
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  padding: 22px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
  box-shadow: 4px 0 24px rgba(0, 0, 0, 0.18);
  z-index: 5;
}

.main-content {
  flex: 1;
  min-width: 0;
  padding: 24px 28px 60px;
}

.page { display: none; }
.page.active { display: block; animation: fadeInUp 0.28s ease; }

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.page-header h2 { margin: 0; font-size: 20px; }

/* ---- Brand / nav ---- */
.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 8px 20px;
}

.brand-mark {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: linear-gradient(135deg, #6366f1, #4338ca, #6366f1);
  background-size: 200% 200%;
  animation: gradientShift 6s ease infinite;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  letter-spacing: 0.5px;
  box-shadow: var(--shadow);
  flex-shrink: 0;
  transition: transform 0.2s ease;
}

.brand:hover .brand-mark { transform: scale(1.06) rotate(-2deg); }

@keyframes gradientShift {
  0%, 100% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
}

.brand h1 { margin: 0; font-size: 16px; font-weight: 700; color: var(--sidebar-text-strong); letter-spacing: -0.01em; }
.subtitle { margin: 2px 0 0; font-size: 12px; color: var(--text-muted); }
.brand .subtitle { font-size: 11.5px; color: var(--sidebar-text); }

.user-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  margin-bottom: 14px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--sidebar-border);
  transition: background 0.15s ease;
}

.user-card:hover { background: rgba(255, 255, 255, 0.09); }

.user-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: linear-gradient(135deg, #6366f1, #4338ca);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  flex-shrink: 0;
  overflow: hidden;
}

.user-avatar img { width: 100%; height: 100%; object-fit: cover; }

.user-meta { min-width: 0; }
.user-name { font-size: 13px; font-weight: 600; color: var(--sidebar-text-strong); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-email { font-size: 11px; color: var(--sidebar-text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.hidden { display: none !important; }

/* ---- Language toggle ---- */
.lang-toggle {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: rgba(255, 255, 255, 0.06);
  border-radius: 8px;
  margin-bottom: 12px;
}

.lang-btn {
  flex: 1;
  border: none;
  background: transparent;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 700;
  color: var(--sidebar-text);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.lang-btn.active { background: rgba(255, 255, 255, 0.14); color: var(--sidebar-text-strong); }

/* Login page's toggle sits on a light card, not the dark sidebar */
.login-card .lang-toggle { background: #f2f3fa; }
.login-card .lang-btn { color: var(--text-muted); }
.login-card .lang-btn.active { background: var(--surface); color: var(--primary); box-shadow: var(--shadow); }

.side-nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
}

.nav-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
  border: none;
  background: transparent;
  padding: 9px 12px;
  border-radius: 8px;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--sidebar-text);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, transform 0.1s ease, padding-left 0.15s ease;
}

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

.nav-item:hover { background: rgba(255, 255, 255, 0.07); color: var(--sidebar-text-strong); padding-left: 16px; }
.nav-item:active { transform: scale(0.98); }

.nav-item.active {
  background: linear-gradient(135deg, #6366f1, #4338ca);
  color: white;
  box-shadow: 0 4px 14px rgba(79, 70, 229, 0.45);
}

.nav-item.active svg { opacity: 1; }
.nav-item.active:hover { padding-left: 12px; }

.logout-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--sidebar-text);
  border: 1px solid var(--sidebar-border);
}
.logout-btn:hover:not(:disabled) { background: rgba(255, 255, 255, 0.1); color: var(--sidebar-text-strong); border-color: rgba(255,255,255,0.16); }

/* ---- Cards / stats (shared) ---- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  animation: fadeInUp 0.35s ease backwards;
  transition: transform 0.18s ease, box-shadow 0.18s ease;
}

.stat-card:nth-child(1) { animation-delay: 0.02s; }
.stat-card:nth-child(2) { animation-delay: 0.06s; }
.stat-card:nth-child(3) { animation-delay: 0.10s; }
.stat-card:nth-child(4) { animation-delay: 0.14s; }
.stat-card:nth-child(5) { animation-delay: 0.18s; }
.stat-card:nth-child(6) { animation-delay: 0.22s; }

.stat-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 20px rgba(16, 24, 40, 0.1), 0 2px 6px rgba(16, 24, 40, 0.06);
}

.stat-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  font-weight: 600;
}

.stat-value { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }

.stat-value.loading {
  color: transparent;
  background: linear-gradient(90deg, var(--border) 25%, rgba(150,150,170,0.3) 50%, var(--border) 75%);
  background-size: 200% 100%;
  border-radius: 4px;
  animation: pulseSkeleton 1.3s ease infinite;
  display: inline-block;
  min-width: 30px;
}

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px;
  margin-bottom: 16px;
  transition: box-shadow 0.2s ease;
}

.card:hover { box-shadow: 0 4px 18px rgba(16, 24, 40, 0.08), 0 2px 6px rgba(16, 24, 40, 0.05); }

.card h2 { margin: 0 0 6px; font-size: 16px; font-weight: 700; }

.hint {
  margin: 0 0 14px;
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.5;
}

.hint code { background: #eef0f6; padding: 1px 6px; border-radius: 4px; font-size: 12px; }

.field { display: block; margin-bottom: 12px; }

.field span {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 6px;
}

input[type="text"],
input[type="file"],
input[type="password"],
select,
textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  background: #fbfbfd;
  color: var(--text);
}

textarea { resize: vertical; min-height: 90px; }

input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.15);
}

.btn {
  border: none;
  border-radius: 8px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.15s ease, box-shadow 0.15s ease;
  text-decoration: none;
  display: inline-block;
}

.btn:active:not(:disabled) { transform: scale(0.97); }

.btn-primary { background: linear-gradient(135deg, var(--primary), var(--primary-dark)); color: white; }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); transform: translateY(-1px); box-shadow: 0 6px 16px rgba(79, 70, 229, 0.32); }

.btn-secondary { background: #eef0f6; color: var(--text); }
.btn-secondary:hover:not(:disabled) { background: #e4e7f3; transform: translateY(-1px); }

.btn-ghost { background: transparent; color: var(--primary); border: 1px solid var(--border); }
.btn-ghost:hover:not(:disabled) { background: rgba(79, 70, 229, 0.08); border-color: var(--primary); }

.btn-danger { background: #fee2e2; color: #991b1b; }
.btn-danger:hover:not(:disabled) { background: #fecaca; }

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

.btn.is-loading { position: relative; color: transparent !important; pointer-events: none; }
.btn.is-loading::after {
  content: "";
  position: absolute;
  top: 50%; left: 50%;
  width: 15px; height: 15px;
  margin: -8px 0 0 -8px;
  border: 2px solid rgba(255,255,255,0.5);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}
.btn-secondary.is-loading::after, .btn-ghost.is-loading::after {
  border-color: rgba(79,70,229,0.3);
  border-top-color: var(--primary);
}

.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }

.result { margin-top: 12px; font-size: 13px; white-space: pre-wrap; }
.result.success { color: var(--success); }
.result.error { color: var(--danger); }

.email-preview {
  margin-top: 14px;
  padding: 16px;
  border: 1px dashed var(--border);
  border-radius: 10px;
  background: #fbfbfd;
  font-size: 13px;
  white-space: pre-wrap;
}

.email-preview .preview-subject { font-weight: 700; margin-bottom: 8px; }

.logs-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.logs-header select, .logs-header input { width: auto; min-width: 180px; }

.table-wrap { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }

th, td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

tbody tr { transition: background 0.12s ease; }
tbody tr:hover { background: rgba(79, 70, 229, 0.05); }

th {
  color: var(--text-muted);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

td.subject-cell, td.wrap-cell { white-space: normal; max-width: 320px; }

.badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}

.badge-sent { background: #dcfce7; color: #166534; }
.badge-failed { background: #fee2e2; color: #991b1b; }
.badge-yes { background: #e0e7ff; color: #3730a3; }
.badge-no { background: #f3f4f6; color: #6b7280; }

.ai-import-preview { margin-top: 14px; }
.ai-import-preview .table-wrap { margin-bottom: 12px; max-height: 320px; overflow-y: auto; }
#aiImportTable th:first-child, #aiImportTable td:first-child { width: 32px; }

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 36px 16px;
  color: var(--text-muted);
  text-align: center;
}

.empty-state svg { opacity: 0.35; }
.empty-state .empty-title { font-size: 13px; font-weight: 600; color: var(--text); }
.empty-state .empty-hint { font-size: 12px; }

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 14px;
  font-size: 13px;
  color: var(--text-muted);
}

/* ---- Chat assistant ---- */
.chat-card { display: flex; flex-direction: column; }

.chat-window {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 420px;
  min-height: 220px;
  overflow-y: auto;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fbfbfd;
  margin-bottom: 12px;
}

.chat-msg {
  max-width: 80%;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
  animation: fadeInUp 0.22s ease;
}

.chat-assistant {
  align-self: flex-start;
  background: #eef0f6;
  color: var(--text);
  border-bottom-left-radius: 2px;
}

.chat-user {
  align-self: flex-end;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
  border-bottom-right-radius: 2px;
}

.chat-msg.pending { opacity: 0.8; }

.typing-dots { display: inline-flex; gap: 4px; align-items: center; }
.typing-dots span {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.4;
  animation: typingBounce 1s ease infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }

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

.chat-input-row { display: flex; gap: 10px; }
.chat-input-row input { flex: 1; }

.voice-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 12px;
  padding: 8px 12px;
  background: #f6f7fb;
  border-radius: 8px;
}

.voice-toggle { display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 600; cursor: pointer; }
.voice-toggle input { width: auto; accent-color: var(--primary); cursor: pointer; }
.voice-select { width: auto; max-width: 220px; padding: 6px 10px; font-size: 12px; }

.mic-btn { flex-shrink: 0; padding: 10px 12px; transition: background 0.15s ease, color 0.15s ease, box-shadow 0.15s ease; }
.mic-btn.mic-listening {
  background: var(--danger);
  color: white;
  border-color: var(--danger);
  animation: micPulse 1.2s ease infinite;
}

@keyframes micPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0.4); }
  50% { box-shadow: 0 0 0 8px rgba(220, 38, 38, 0); }
}

.voice-preview-btn, .voice-mode-btn { flex-shrink: 0; white-space: nowrap; }
.voice-mode-btn { display: inline-flex; align-items: center; gap: 6px; }

/* ---- Full-screen Voice Mode overlay ---- */
.voice-overlay {
  position: fixed;
  inset: 0;
  background: radial-gradient(circle at 50% 40%, #1b1d2b 0%, #0b0c12 75%);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  animation: fadeInUp 0.25s ease;
}

.voice-overlay-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 22px;
  padding: 24px;
  max-width: 90vw;
}

.voice-orb {
  width: 140px;
  height: 140px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  background: linear-gradient(135deg, #6366f1, #4338ca);
  box-shadow: 0 0 0 0 rgba(99, 102, 241, 0.5);
  animation: orbListen 1.8s ease infinite;
  transition: background 0.3s ease;
}

.voice-orb.state-thinking {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  animation: orbThink 1.1s ease infinite;
}

.voice-orb.state-speaking {
  background: linear-gradient(135deg, #16a34a, #15803d);
  animation: orbSpeak 0.7s ease infinite;
}

.voice-orb.state-muted {
  background: linear-gradient(135deg, #4b5563, #374151);
  animation: none;
  opacity: 0.7;
}

@keyframes orbListen {
  0%, 100% { box-shadow: 0 0 0 0 rgba(99, 102, 241, 0.45); transform: scale(1); }
  50% { box-shadow: 0 0 0 22px rgba(99, 102, 241, 0); transform: scale(1.03); }
}

@keyframes orbThink {
  0%, 100% { transform: scale(1) rotate(0deg); }
  50% { transform: scale(0.95) rotate(8deg); }
}

@keyframes orbSpeak {
  0%, 100% { box-shadow: 0 0 0 0 rgba(22, 163, 74, 0.5); transform: scale(1); }
  50% { box-shadow: 0 0 0 16px rgba(22, 163, 74, 0); transform: scale(1.06); }
}

.voice-status { color: white; font-size: 15px; font-weight: 600; }

.voice-transcript {
  color: rgba(255, 255, 255, 0.6);
  font-size: 13px;
  text-align: center;
  max-width: 480px;
  min-height: 18px;
}

.voice-overlay-controls { display: flex; gap: 20px; margin-top: 10px; }

.voice-control-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.1);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.12s ease;
}

.voice-control-btn:hover { background: rgba(255, 255, 255, 0.18); }
.voice-control-btn:active { transform: scale(0.94); }
.voice-control-btn.muted { background: var(--danger); }

.voice-end-btn { background: var(--danger); transform: rotate(135deg); }
.voice-end-btn:hover { background: #b91c1c; }

/* ---- Toasts ---- */
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 1000;
}

.toast {
  background: var(--text);
  color: white;
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 13px;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast.show { opacity: 1; transform: translateY(0); }
.toast.toast-error { background: var(--danger); }
.toast.toast-success { background: var(--success); }

@media (max-width: 800px) {
  .app-shell { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: relative; flex-direction: row; align-items: center; flex-wrap: wrap; }
  .side-nav { flex-direction: row; flex-wrap: wrap; }
  .logout-btn { width: auto; }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1117;
    --surface: #171a24;
    --text: #eef0f6;
    --text-muted: #9aa0ae;
    --border: #262b39;
  }
  input[type="text"], input[type="file"], input[type="password"], select, textarea {
    background: #12141c;
    color: var(--text);
  }
  .hint code { background: #1f2330; }
  .btn-secondary { background: #1f2330; color: var(--text); }
  .chat-window { background: #12141c; }
  .chat-assistant { background: #1f2330; }
  .email-preview { background: #12141c; }
  .voice-bar { background: #1f2330; }
}
