:root {
  --bg: #0f1419;
  --bg-elevated: #1a2332;
  --card: #1e2a3a;
  --card-hover: #243044;
  --text: #e8edf4;
  --text-muted: #8b9cb3;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --accent: #10b981;
  --accent-dim: rgba(16, 185, 129, 0.15);
  --danger: #ef4444;
  --warn: #f59e0b;
  --border: #2d3a4f;
  --radius: 12px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.35);
  --nav-h: 56px;
}

[data-theme="light"] {
  --bg: #f0f4f8;
  --bg-elevated: #ffffff;
  --card: #ffffff;
  --card-hover: #f8fafc;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --shadow: 0 2px 12px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: "Segoe UI", "Noto Sans Arabic", Tahoma, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

body.rtl { direction: rtl; }

a { color: var(--primary); text-decoration: none; transition: color 0.15s; }
a:hover { color: var(--primary-hover); }

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

.topbar {
  position: sticky;
  top: 0;
  z-index: 100;
  height: var(--nav-h);
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 1.5rem;
  gap: 1.25rem;
}

.topbar .brand {
  font-weight: 800;
  font-size: 1.15rem;
  color: var(--text);
  margin-inline-end: auto;
  letter-spacing: -0.02em;
}

.topbar .brand span { color: var(--accent); }

.topbar nav { display: flex; gap: 0.25rem; align-items: center; flex-wrap: wrap; }

.topbar nav a {
  padding: 0.4rem 0.75rem;
  border-radius: 8px;
  font-size: 0.9rem;
  color: var(--text-muted);
}
.topbar nav a:hover, .topbar nav a.active {
  background: var(--card);
  color: var(--text);
  text-decoration: none;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-inline-start: auto;
}

.credits-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: var(--accent-dim);
  color: var(--accent);
  padding: 0.3rem 0.85rem;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 700;
  border: 1px solid rgba(16, 185, 129, 0.3);
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.75rem;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
}
.status-pill.ready { background: var(--accent-dim); color: var(--accent); }
.status-pill.not-ready { background: rgba(245, 158, 11, 0.15); color: var(--warn); }

.theme-toggle {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 8px;
  padding: 0.35rem 0.6rem;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
}

.container {
  max-width: 1040px;
  margin: 0 auto;
  padding: 1.75rem 1.5rem 3rem;
  flex: 1;
  width: 100%;
}

/* Typography */
h1 { font-size: 1.75rem; font-weight: 800; margin: 0 0 0.5rem; letter-spacing: -0.03em; }
h2 { font-size: 1.25rem; font-weight: 700; margin: 0 0 0.75rem; }
h3 { font-size: 1.05rem; font-weight: 700; margin: 0 0 0.5rem; }
.muted { color: var(--text-muted); font-size: 0.92rem; }

.page-header { margin-bottom: 1.75rem; }

/* Cards */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.35rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
.card:not(.card-action):hover {
  border-color: rgba(59, 130, 246, 0.35);
  box-shadow: 0 6px 28px rgba(0, 0, 0, 0.4), 0 0 0 1px rgba(59, 130, 246, 0.08);
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1rem;
  align-items: stretch;
}

.card-action {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  height: 100%;
  cursor: default;
  transform: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
.card-action:hover {
  border-color: rgba(59, 130, 246, 0.4);
  box-shadow: 0 6px 28px rgba(0, 0, 0, 0.4), 0 0 0 1px rgba(59, 130, 246, 0.1);
  transform: none;
}
.card-action .card-icon,
.card-action h3,
.card-action p,
.card-action .btn,
.card-action:hover .card-icon,
.card-action:hover h3,
.card-action:hover p,
.card-action:hover .btn {
  transform: none;
}
.card-action .card-action-footer {
  margin-top: auto;
  padding-top: 0.5rem;
}
.card-action .btn {
  color: #fff;
  background: var(--primary);
}
.card-action .btn:hover {
  background: var(--primary-hover);
  color: #fff;
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.25);
}
.card-action .btn-secondary {
  background: var(--bg-elevated);
  color: var(--text);
  border: 1px solid var(--border);
}
.card-action .btn-secondary:hover {
  background: var(--card-hover);
  color: var(--text);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}
.card-action .btn-accent {
  background: var(--accent);
  color: #fff;
  border: none;
}
.card-action .btn-accent:hover {
  background: #059669;
  color: #fff;
  box-shadow: 0 2px 8px rgba(16, 185, 129, 0.3);
}
.card-action .card-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: rgba(59, 130, 246, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
}
.card-action.done .card-icon { background: var(--accent-dim); }

/* Steps */
.steps {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-bottom: 1.5rem;
}
.step {
  padding: 0.4rem 0.9rem;
  border-radius: 999px;
  background: var(--card);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  color: var(--text-muted);
}
.step.done {
  background: var(--accent-dim);
  border-color: rgba(16, 185, 129, 0.35);
  color: var(--accent);
  font-weight: 600;
}

/* Forms */
label {
  display: block;
  margin: 0.85rem 0 0.35rem;
  font-weight: 600;
  font-size: 0.9rem;
}
label:first-child { margin-top: 0; }

input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
textarea,
select {
  width: 100%;
  padding: 0.7rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  font: inherit;
  background: var(--bg);
  color: var(--text);
  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(59, 130, 246, 0.2);
}
textarea { min-height: 130px; resize: vertical; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  border: none;
  border-radius: 10px;
  padding: 0.65rem 1.15rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  background: var(--primary);
  color: #fff;
  margin-top: 0.75rem;
  transition: background 0.15s, box-shadow 0.15s;
}
.btn:hover {
  background: var(--primary-hover);
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.25);
}
.btn:active { box-shadow: none; opacity: 0.92; }
.btn-secondary { background: var(--card); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--card-hover); box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15); }
.btn-accent { background: var(--accent); }
.btn-accent:hover { background: #059669; }
.btn-danger { background: var(--danger); }
.btn-sm { padding: 0.4rem 0.75rem; font-size: 0.85rem; margin-top: 0; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; box-shadow: none; }

/* Compose tabs */
.compose-tabs {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
.compose-tab {
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text-muted);
  border-radius: 10px;
  padding: 0.55rem 1rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s, color 0.15s;
}
.compose-tab:hover {
  border-color: rgba(59, 130, 246, 0.35);
  color: var(--text);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
}
.compose-tab.active {
  background: rgba(59, 130, 246, 0.12);
  border-color: rgba(59, 130, 246, 0.45);
  color: var(--text);
}
.compose-panel.hidden { display: none; }

.admin-actions {
  display: flex;
  gap: 0.35rem;
  flex-wrap: wrap;
  align-items: center;
}

/* Alerts */
.alert {
  padding: 0.85rem 1rem;
  border-radius: 10px;
  margin-bottom: 1rem;
  font-size: 0.92rem;
  border: 1px solid transparent;
}
.alert-error { background: rgba(239, 68, 68, 0.12); color: #fca5a5; border-color: rgba(239, 68, 68, 0.3); }
.alert-info { background: rgba(59, 130, 246, 0.12); color: #93c5fd; border-color: rgba(59, 130, 246, 0.3); }
.alert-success { background: var(--accent-dim); color: var(--accent); border-color: rgba(16, 185, 129, 0.3); }
.alert-warn { background: rgba(245, 158, 11, 0.12); color: #fcd34d; border-color: rgba(245, 158, 11, 0.3); }

.text-success { color: var(--accent); }
.text-warn { color: var(--warn); }

/* Tables */
.table-wrap { overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--border); }
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { padding: 0.7rem 1rem; text-align: start; border-bottom: 1px solid var(--border); }
th { background: var(--bg-elevated); font-weight: 700; color: var(--text-muted); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: rgba(59, 130, 246, 0.04); }

/* Auth pages */
.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  background: radial-gradient(ellipse at 50% 0%, rgba(59, 130, 246, 0.12) 0%, transparent 60%), var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 2rem;
  box-shadow: var(--shadow);
}
.auth-logo {
  text-align: center;
  font-size: 1.5rem;
  font-weight: 800;
  margin-bottom: 0.25rem;
}
.auth-logo span { color: var(--accent); }

/* Status output */
#status, #bulk-status, .mono-output {
  white-space: pre-wrap;
  font-family: Consolas, "Courier New", monospace;
  font-size: 0.82rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem;
  margin-top: 0.75rem;
  color: var(--text-muted);
}

/* Admin */
.admin-layout .topbar { background: #111827; border-color: #1f2937; }
.admin-badge {
  background: rgba(139, 92, 246, 0.2);
  color: #a78bfa;
  padding: 0.2rem 0.6rem;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 700;
}

.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.stat-box {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
}
.stat-box .val { font-size: 1.75rem; font-weight: 800; color: var(--accent); }
.stat-box .lbl { font-size: 0.8rem; color: var(--text-muted); }

/* Campaign progress */
.progress-wrap {
  position: relative;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  height: 1.25rem;
  min-width: 100px;
  overflow: hidden;
}
.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), #34d399);
  border-radius: 999px;
  transition: width 0.4s ease;
}
.progress-label {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text);
}
.status-badge {
  display: inline-block;
  padding: 0.2rem 0.55rem;
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 600;
}
.status-pending { background: rgba(239, 68, 68, 0.15); color: #fca5a5; }
.status-running { background: rgba(245, 158, 11, 0.15); color: #fcd34d; }
.status-paused { background: rgba(156, 163, 175, 0.15); color: #d1d5db; }
.status-completed { background: var(--accent-dim); color: var(--accent); }
.status-failed, .status-credits_exceeded { background: rgba(239, 68, 68, 0.15); color: #fca5a5; }
.status-cancelled { background: rgba(107, 114, 128, 0.15); color: #9ca3af; }
.row-highlight td { background: rgba(59, 130, 246, 0.08) !important; }

/* Campaign report */
.report-progress-card { margin-bottom: 1.25rem; padding: 1.25rem 1.5rem; }
.report-progress-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.65rem;
  font-weight: 700;
}
.report-progress-bar { height: 1.5rem; }
.report-live-hint { margin: 0.65rem 0 0; font-size: 0.82rem; }
.report-stat-row .stat-box .val { font-size: 1.5rem; }
.report-stat-sent .val { color: var(--accent); }
.report-stat-failed .val { color: var(--danger); }
.report-stat-pending .val { color: var(--warn); }
.report-stat-total .val { color: var(--primary); }
.report-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}
.report-tabs { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.report-tab {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 8px;
  padding: 0.35rem 0.85rem;
  font-size: 0.85rem;
  cursor: pointer;
  font-family: inherit;
}
.report-tab:hover { color: var(--text); border-color: var(--primary); }
.report-tab.active {
  background: rgba(59, 130, 246, 0.15);
  border-color: var(--primary);
  color: var(--primary);
  font-weight: 700;
}
.report-search input {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 0.45rem 0.85rem;
  min-width: 220px;
  font-family: inherit;
  font-size: 0.88rem;
}
.report-pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 1rem;
  border-top: 1px solid var(--border);
}
.log-status {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 600;
}
.log-status-sent { background: var(--accent-dim); color: var(--accent); }
.log-status-failed { background: rgba(239, 68, 68, 0.15); color: #fca5a5; }
.log-status-pending { background: rgba(245, 158, 11, 0.15); color: #fcd34d; }
.psid-code {
  font-size: 0.78rem;
  background: var(--bg);
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
  direction: ltr;
  display: inline-block;
}
.error-cell {
  font-size: 0.82rem;
  color: #fca5a5;
  max-width: 280px;
  word-break: break-word;
}
.row-actions {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-wrap: nowrap;
}
.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border-radius: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 1rem;
  line-height: 1;
  text-decoration: none;
  transition: background 0.15s, border-color 0.15s;
}
.btn-icon:hover {
  background: rgba(59, 130, 246, 0.12);
  border-color: var(--primary);
  text-decoration: none;
}
.alert-warn {
  background: rgba(245, 158, 11, 0.12);
  border: 1px solid rgba(245, 158, 11, 0.35);
  color: #fcd34d;
  padding: 0.85rem 1rem;
  border-radius: var(--radius);
  margin-bottom: 1rem;
}

@media (max-width: 768px) {
  .topbar { padding: 0 1rem; flex-wrap: wrap; height: auto; min-height: var(--nav-h); padding-block: 0.5rem; }
  .topbar nav { display: none; }
  .container { padding: 1.25rem 1rem 2rem; }
}
