/* ============================================================
   THEME TOKENS
   Dark: Premium Dark + Blue Gradient, Glassmorphism
   Light: equivalent clean professional counterpart
============================================================ */
:root[data-theme="dark"] {
  --bg: #0f172a;
  --bg-gradient: radial-gradient(1200px 600px at 10% -10%, #1e3a8a33, transparent),
                 radial-gradient(1000px 500px at 90% 10%, #2563eb22, transparent),
                 #0f172a;
  --card-bg: rgba(255, 255, 255, 0.08);
  --card-border: rgba(255, 255, 255, 0.12);
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --primary: #2563eb;
  --primary-hover: #3b82f6;
  --success: #22c55e;
  --danger: #ef4444;
  --warning: #f59e0b;
  --input-bg: rgba(255, 255, 255, 0.06);
  --input-border: rgba(255, 255, 255, 0.14);
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
  --dropdown-bg: rgba(23, 33, 58, 0.97);
}

:root[data-theme="light"] {
  --bg: #eef2f9;
  --bg-gradient: radial-gradient(1200px 600px at 10% -10%, #2563eb14, transparent),
                 radial-gradient(1000px 500px at 90% 10%, #2563eb0f, transparent),
                 #eef2f9;
  --card-bg: rgba(255, 255, 255, 0.65);
  --card-border: rgba(15, 23, 42, 0.08);
  --text-primary: #0f172a;
  --text-secondary: #55627a;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #d97706;
  --input-bg: rgba(15, 23, 42, 0.035);
  --input-border: rgba(15, 23, 42, 0.12);
  --shadow: 0 8px 32px rgba(15, 23, 42, 0.10);
  --dropdown-bg: rgba(255, 255, 255, 0.97);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: 'Poppins', sans-serif;
  background: var(--bg-gradient);
  color: var(--text-primary);
  min-height: 100vh;
  transition: background 0.3s ease, color 0.3s ease;
}

.hidden { display: none !important; }

/* ---------- Glassmorphism ---------- */
.glass {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

/* ---------- Animations ---------- */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: translateY(0); } }
@keyframes shimmer { 0% { background-position: -400px 0; } 100% { background-position: 400px 0; } }

.fade-in { animation: fadeIn 0.5s ease both; }
.slide-up { animation: slideUp 0.5s ease both; }

/* ============================================================
   LOGIN SCREEN
============================================================ */
.screen {
  min-height: 100vh;
  width: 100%;
}

#loginScreen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  padding: 40px 32px;
  text-align: center;
}

.login-logo {
  color: var(--primary);
  margin-bottom: 8px;
}

.login-card h1 {
  margin: 8px 0 4px;
  font-weight: 700;
  font-size: 24px;
}

.subtitle {
  color: var(--text-secondary);
  margin: 0 0 28px;
  font-size: 14px;
}

.error-text {
  color: var(--danger);
  font-size: 13px;
  margin: 4px 0 12px;
  text-align: left;
}

/* ============================================================
   FORM ELEMENTS
============================================================ */
.field { margin-bottom: 16px; text-align: left; }
.field label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

input[type="text"], input[type="password"], input[type="date"], input[type="datetime-local"],
textarea, select {
  width: 100%;
  padding: 11px 14px;
  border-radius: 10px;
  border: 1px solid var(--input-border);
  background: var(--input-bg);
  color: var(--text-primary);
  font-family: 'Poppins', sans-serif;
  font-size: 14px;
  outline: none;
  transition: box-shadow 0.2s ease, border-color 0.2s ease;
}
textarea { resize: vertical; font-family: 'Poppins', sans-serif; }

input:focus, textarea:focus, select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.25);
}

.hint { font-size: 12px; color: var(--text-secondary); margin: 4px 0 0; }

.checkbox-row {
  display: flex; align-items: flex-start; gap: 8px;
  margin-top: 14px; font-size: 12.5px; color: var(--text-secondary);
  cursor: pointer;
}
.checkbox-row input[type="checkbox"] { margin-top: 2px; accent-color: var(--primary); width: 15px; height: 15px; flex-shrink: 0; }
.checkbox-row.inline { margin-top: 0; align-items: center; }

.selection-bar {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 10px; padding: 8px 12px;
  background: var(--input-bg); border-radius: 8px;
}

.history-item-checkbox { margin-right: 4px; accent-color: var(--primary); }

.warmup-badge {
  font-size: 10.5px; padding: 1px 7px; border-radius: 20px;
  background: rgba(37, 99, 235, 0.15); color: var(--primary); font-weight: 500;
}
.count-badge {
  background: rgba(37, 99, 235, 0.15);
  color: var(--primary);
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 11px;
}

/* ============================================================
   BUTTONS
============================================================ */
.btn {
  font-family: 'Poppins', sans-serif;
  font-weight: 500;
  font-size: 14px;
  padding: 11px 20px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn-block { width: 100%; }
.btn-sm { padding: 7px 14px; font-size: 12.5px; }

.btn-primary {
  background: linear-gradient(135deg, var(--primary), #1d4ed8);
  color: white;
  box-shadow: 0 4px 14px rgba(37, 99, 235, 0.35);
}
.btn-primary:hover { background: linear-gradient(135deg, var(--primary-hover), var(--primary)); }

.btn-ghost {
  background: var(--input-bg);
  color: var(--text-primary);
  border: 1px solid var(--input-border);
}
.btn-ghost:hover { background: rgba(37, 99, 235, 0.12); border-color: var(--primary); }

.button-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 20px; }
.button-row .btn-primary { margin-left: auto; }

/* ============================================================
   ICON BUTTONS
============================================================ */
.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid var(--input-border);
  background: var(--input-bg);
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.15s ease;
}
.icon-btn:hover { background: rgba(37, 99, 235, 0.15); transform: translateY(-1px); }
.icon-btn.danger:hover { background: rgba(239, 68, 68, 0.15); color: var(--danger); }
.icon-btn.subtle { opacity: 0.55; }
.icon-btn.subtle:hover { opacity: 1; }

/* ============================================================
   HEADER / TOPBAR
============================================================ */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  margin: 16px;
  border-radius: 16px;
  position: sticky;
  top: 16px;
  z-index: 50;
}
.topbar-left { display: flex; align-items: center; gap: 10px; color: var(--primary); }
.brand { font-weight: 700; font-size: 18px; color: var(--text-primary); }
.topbar-right { display: flex; align-items: center; gap: 10px; }

/* ---------- Account switcher ---------- */
.account-switcher { position: relative; }
.account-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: 30px;
  padding: 6px 12px 6px 6px;
  cursor: pointer;
  color: var(--text-primary);
  font-family: 'Poppins', sans-serif;
}
.avatar-wrap { position: relative; display: inline-flex; }
.avatar {
  width: 28px; height: 28px; border-radius: 50%;
  object-fit: cover;
}
.avatar-fallback {
  display: flex; align-items: center; justify-content: center;
  background: var(--primary); color: white; font-size: 12px; font-weight: 600;
}
/* Photo sits on top of the letter fallback; if it 404s/CORS-fails the
   inline onerror removes the <img>, revealing the fallback underneath —
   so a broken photo never shows a broken-image glyph. */
.avatar-photo {
  position: absolute; top: 0; left: 0;
  background: var(--primary);
}
.online-dot {
  position: absolute; bottom: -1px; right: -1px;
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--success);
  border: 2px solid var(--bg);
}
.account-email { font-size: 13px; max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.account-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 300px;
  padding: 10px;
  z-index: 100;
  /* Solid-enough background so content scrolled underneath (e.g. the
     "Failed" stat card) can't bleed through the blur — the generic
     .glass opacity (0.08) is too low for a dropdown sitting over
     busy content. */
  background: var(--dropdown-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
}
.account-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 10px; cursor: pointer;
}
.account-item:hover { background: rgba(37, 99, 235, 0.12); }
.account-item.active { background: rgba(37, 99, 235, 0.18); }
.account-item-info { flex: 1; min-width: 0; }
.account-item-name { font-size: 13px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.account-item-email { font-size: 11.5px; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.account-item-remove { opacity: 0.5; padding: 4px; border-radius: 6px; }
.account-item-remove:hover { opacity: 1; color: var(--danger); background: rgba(239,68,68,0.12); }
.account-dropdown-divider { height: 1px; background: var(--input-border); margin: 8px 0; }
.add-account-btn {
  display: flex; align-items: center; gap: 8px; justify-content: center;
  padding: 9px; border-radius: 10px; cursor: pointer; font-size: 13px; color: var(--primary);
}
.add-account-btn:hover { background: rgba(37, 99, 235, 0.12); }

/* ============================================================
   MAIN CONTENT / LAYOUT
============================================================ */
.main-content { padding: 0 16px 40px; max-width: 1400px; margin: 0 auto; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 20px;
}
.stat-card { padding: 20px; text-align: center; }
.stat-card.clickable { cursor: pointer; }
.stat-label { font-size: 12.5px; color: var(--text-secondary); margin-bottom: 6px; }
.stat-value { font-size: 28px; font-weight: 700; }
.stat-success { color: var(--success); }
.stat-warning { color: var(--warning); }
.stat-danger { color: var(--danger); }

.layout-grid {
  display: grid;
  grid-template-columns: 1.1fr 1fr;
  gap: 20px;
  align-items: start;
}

.panel { padding: 24px; }
.panel-title { margin: 0 0 20px; font-size: 17px; font-weight: 600; }
.panel-subtitle { margin: 0 0 12px; font-size: 14px; font-weight: 600; color: var(--text-secondary); }
.panel-header-row { display: flex; align-items: center; justify-content: space-between; }
.header-actions { display: flex; gap: 8px; }

.file-label {
  display: inline-flex; align-items: center; gap: 8px;
  cursor: pointer; font-size: 13px; color: var(--primary);
}
.attachment-preview {
  margin-top: 8px; padding: 8px 12px; border-radius: 8px;
  background: var(--input-bg); font-size: 12.5px; display: flex;
  align-items: center; justify-content: space-between;
}

.divider-with-label {
  display: flex; align-items: center; gap: 10px;
  margin: 18px 0 14px;
  color: var(--text-secondary); font-size: 11.5px;
}
.divider-with-label::before, .divider-with-label::after {
  content: ''; flex: 1; height: 1px; background: var(--input-border);
}

/* ============================================================
   SENDING CONTROLS / PROGRESS
============================================================ */
.sending-controls { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--input-border); }
.progress-info { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 8px; }
.progress-bar-track {
  height: 10px; border-radius: 20px; background: var(--input-bg);
  overflow: hidden; position: relative;
}
.progress-bar-fill {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, var(--primary), #60a5fa);
  border-radius: 20px;
  position: relative;
  transition: width 0.3s ease;
  overflow: hidden;
}
.progress-bar-fill::after {
  content: '';
  position: absolute; inset: 0;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.4), transparent);
  background-size: 200px 100%;
  animation: shimmer 1.4s infinite linear;
}
.progress-meta { display: flex; justify-content: space-between; font-size: 12px; color: var(--text-secondary); margin-top: 8px; }
.control-icons { display: flex; gap: 8px; margin-top: 12px; justify-content: center; }

/* ============================================================
   STATUS LOG
============================================================ */
.status-log-wrap { margin-top: 20px; }
.status-log-header { font-size: 12.5px; color: var(--text-secondary); margin-bottom: 6px; font-weight: 500; }
.status-log {
  height: 160px; overflow-y: auto;
  background: rgba(0,0,0,0.18);
  border-radius: 10px; padding: 12px;
  font-family: 'Courier New', monospace;
  font-size: 12px; line-height: 1.6;
  color: var(--text-secondary);
}
:root[data-theme="light"] .status-log { background: rgba(15,23,42,0.05); }
.status-log .log-success { color: var(--success); }
.status-log .log-error { color: var(--danger); }
.status-log .log-line { animation: fadeIn 0.3s ease; }

/* ============================================================
   HISTORY PANEL
============================================================ */
.filters-row { display: grid; grid-template-columns: 1fr 1fr 1fr 1.4fr; gap: 8px; margin-bottom: 16px; }
.history-list { max-height: 420px; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; }

.history-item {
  padding: 12px 14px; border-radius: 10px;
  background: var(--input-bg); border: 1px solid var(--input-border);
  font-size: 13px;
}
.history-item-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.history-recipient { font-weight: 500; }
.history-subject { color: var(--text-secondary); font-size: 12px; margin-top: 2px; }
.history-meta { display: flex; gap: 12px; font-size: 11.5px; color: var(--text-secondary); margin-top: 6px; }
.badge { padding: 2px 9px; border-radius: 20px; font-size: 11px; font-weight: 500; }
.badge-success { background: rgba(34,197,94,0.15); color: var(--success); }
.badge-failed { background: rgba(239,68,68,0.15); color: var(--danger); }
.badge-pending { background: rgba(245,158,11,0.15); color: var(--warning); }
.history-actions { display: flex; gap: 6px; }
.link-btn { background: none; border: none; color: var(--primary); font-size: 12px; cursor: pointer; font-family: 'Poppins', sans-serif; }
.link-btn:hover { text-decoration: underline; }

/* ============================================================
   MODALS
============================================================ */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 200; padding: 20px;
}
.modal {
  width: 100%; max-width: 440px;
  padding: 28px;
}
.modal h3 { margin: 0 0 16px; font-size: 18px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

.confirm-summary { display: flex; flex-direction: column; gap: 10px; }
.confirm-row { display: flex; justify-content: space-between; font-size: 13.5px; }
.confirm-row span { color: var(--text-secondary); }

.warning-box {
  margin-top: 16px; padding: 12px 14px; border-radius: 10px;
  background: rgba(245, 158, 11, 0.12); border: 1px solid rgba(245,158,11,0.3);
  color: var(--warning); font-size: 12.5px;
}

.failed-list { max-height: 320px; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; }
.failed-item { padding: 10px 12px; border-radius: 8px; background: rgba(239,68,68,0.08); font-size: 13px; }
.failed-item .reason { color: var(--text-secondary); font-size: 11.5px; margin-top: 3px; }

/* ============================================================
   TOAST
============================================================ */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--card-bg); backdrop-filter: blur(16px);
  border: 1px solid var(--card-border);
  padding: 12px 22px; border-radius: 12px;
  font-size: 13.5px; z-index: 300;
  box-shadow: var(--shadow);
  animation: slideUp 0.3s ease;
}
.toast.toast-error { border-color: rgba(239,68,68,0.4); color: var(--danger); }
.toast.toast-success { border-color: rgba(34,197,94,0.4); color: var(--success); }

/* ============================================================
   RESPONSIVE
============================================================ */
@media (max-width: 1024px) {
  .layout-grid { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 640px) {
  .field-row { grid-template-columns: 1fr; }
  .filters-row { grid-template-columns: 1fr 1fr; }
  .topbar { padding: 10px 14px; margin: 10px; }
  .account-email { max-width: 90px; }
  .button-row { flex-direction: column; }
  .button-row .btn-primary { margin-left: 0; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .modal { padding: 20px; }
}

/* ============================================================
   INBOX (incoming mail for the active Gmail account)
   Reuses the same tokens as the rest of the dashboard so the
   inbox feels like part of the tool, not a bolt-on.
============================================================ */
.inbox-btn { position: relative; display: inline-flex; align-items: center; gap: 7px; }
.inbox-badge {
  position: absolute; top: -7px; right: -7px;
  min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 20px;
  background: var(--primary); color: #fff;
  border: 2px solid var(--bg);
  font-size: 11px; font-weight: 600; line-height: 14px;
  display: flex; align-items: center; justify-content: center;
}

.modal-wide { max-width: 680px; }

.inbox-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  margin: 14px 0 10px;
}
.inbox-list { max-height: 460px; }

.inbox-item { cursor: pointer; transition: background 0.15s ease, border-color 0.15s ease; }
.inbox-item:hover { background: var(--input-bg); }
.inbox-item.unread { border-left: 3px solid var(--primary); }
.inbox-sender { font-weight: 500; display: flex; align-items: center; gap: 7px; }
.inbox-item.unread .inbox-sender { font-weight: 700; }
.inbox-subject { color: var(--text-primary); margin-top: 2px; }
.inbox-item.unread .inbox-subject { font-weight: 600; }
.inbox-snippet { color: var(--text-secondary); font-size: 13px; margin-top: 2px; }
.inbox-meta { margin-top: 6px; font-size: 12px; color: var(--text-secondary); }
.inbox-unread-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--primary); flex-shrink: 0;
}
.inbox-replied-tag {
  font-size: 11px; font-weight: 600; color: var(--success);
  border: 1px solid rgba(34,197,94,0.4); border-radius: 10px;
  padding: 1px 7px; margin-left: 6px;
}

.inbox-detail { margin-top: 14px; }
.inbox-detail-subject { font-size: 17px; font-weight: 600; }
.inbox-detail-meta {
  display: flex; flex-direction: column; gap: 2px;
  margin: 8px 0 14px; font-size: 13px; color: var(--text-secondary);
}
.inbox-body-wrap {
  background: #fff; border-radius: 12px;
  padding: 14px 16px; max-height: 300px; overflow-y: auto;
}
.inbox-body-frame { width: 100%; height: 280px; border: 0; border-radius: 8px; background: #fff; }
.inbox-body-text {
  white-space: pre-wrap; word-break: break-word;
  color: #111; font-size: 14px; line-height: 1.6;
}

/* Reply box inside the reading view. */
.inbox-reply { margin-top: 16px; }
.inbox-reply label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 6px; }
.inbox-reply textarea {
  width: 100%; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--input-border); background: var(--input-bg);
  color: var(--text-primary); font-family: inherit; font-size: 14px;
  resize: vertical;
}
.inbox-reply textarea:focus { outline: none; border-color: var(--primary); }

/* Inbox v4: Delete button + per-account tag in the combined view */
.btn-danger-ghost {
  background: var(--input-bg);
  color: var(--danger);
  border: 1px solid var(--input-border);
}
.btn-danger-ghost:hover { background: rgba(239, 68, 68, 0.12); border-color: var(--danger); }
.btn-danger-ghost:disabled { opacity: 0.6; cursor: default; }
.inbox-account-tag {
  display: inline-block; margin-left: 8px; padding: 1px 8px;
  border-radius: 20px; background: rgba(127, 127, 127, 0.15);
  color: var(--text-secondary); font-size: 11px;
}
