/* ==========================================================================
   Meta Auto-DM — Premium SaaS Dashboard Theme (Dark & Light)
   ========================================================================== */

:root {
  --bg: #090d16;
  --bg-subtle: #0f1624;
  --surface: #131c2e;
  --surface-2: #1b2740;
  --surface-hover: #21304d;
  --border: rgba(148, 163, 184, 0.14);
  --border-strong: rgba(148, 163, 184, 0.25);
  --text: #f8fafc;
  --text-secondary: #cbd5e1;
  --muted: #94a3b8;
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --primary-soft: rgba(99, 102, 241, 0.15);
  --success: #10b981;
  --success-soft: rgba(16, 185, 129, 0.15);
  --warning: #f59e0b;
  --warning-soft: rgba(245, 158, 11, 0.15);
  --danger: #ef4444;
  --danger-soft: rgba(239, 68, 68, 0.15);
  --ig-grad: linear-gradient(135deg, #f58529 0%, #dd2a7b 50%, #8134af 100%);
  --fb-blue: #1877f2;
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
  --sidebar-w: 252px;
}

[data-theme="light"] {
  --bg: #f1f5f9;
  --bg-subtle: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --surface-hover: #e2e8f0;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-secondary: #334155;
  --muted: #64748b;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: rgba(79, 70, 229, 0.1);
  --shadow: 0 4px 20px rgba(15, 23, 42, 0.06);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

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

a:hover {
  text-decoration: underline;
}

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.84em;
  background: var(--surface-2);
  padding: 0.14rem 0.4rem;
  border-radius: 6px;
}

/* ==========================================================================
   App Shell (Sidebar + Main Workspace)
   ========================================================================== */

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

.sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 40;
  padding: 1.25rem 0.9rem;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.35rem 0.6rem 1.35rem;
  color: var(--text);
  font-weight: 800;
  font-size: 1.08rem;
  letter-spacing: -0.02em;
}

.sidebar-brand:hover {
  text-decoration: none;
}

.brand-icon {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: linear-gradient(135deg, #6366f1 0%, #ec4899 100%);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 1rem;
  box-shadow: 0 4px 14px rgba(99, 102, 241, 0.4);
}

.sidebar-section-label {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  padding: 0.5rem 0.75rem 0.35rem;
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  flex: 1;
}

.sidebar-link {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.65rem 0.8rem;
  border-radius: 10px;
  color: var(--text-secondary);
  font-size: 0.9rem;
  font-weight: 500;
  transition: background 0.15s, color 0.15s;
}

.sidebar-link svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  opacity: 0.85;
}

.sidebar-link:hover {
  background: var(--surface-2);
  color: var(--text);
  text-decoration: none;
}

.sidebar-link.active {
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 600;
}

.sidebar-link.active svg {
  opacity: 1;
}

.sidebar-footer {
  border-top: 1px solid var(--border);
  padding-top: 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.user-pill {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.45rem 0.6rem;
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 0.84rem;
}

.user-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, #3b82f6, #8b5cf6);
  color: #fff;
  font-weight: 700;
  font-size: 0.78rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.main-area {
  margin-left: var(--sidebar-w);
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.85rem 1.75rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.mobile-menu-btn {
  display: none;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 0.4rem 0.65rem;
  cursor: pointer;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.workspace {
  max-width: 1280px;
  width: 100%;
  margin: 0 auto;
  padding: 1.75rem 1.75rem 3.5rem;
}

/* ==========================================================================
   Page Header & Typography
   ========================================================================== */

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}

.page-header h1 {
  margin: 0;
  font-size: 1.65rem;
  font-weight: 800;
  letter-spacing: -0.025em;
}

.subtitle {
  color: var(--muted);
  margin: 0.3rem 0 0;
  font-size: 0.92rem;
}

/* ==========================================================================
   KPI Cards & Surfaces
   ========================================================================== */

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem 1.35rem;
  box-shadow: var(--shadow);
  position: relative;
  overflow: hidden;
  transition: transform 0.15s, border-color 0.15s;
}

.stat-card:hover {
  transform: translateY(-2px);
  border-color: var(--border-strong);
}

.stat-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.stat-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
}

.stat-icon.purple { background: var(--primary-soft); color: var(--primary); }
.stat-icon.green { background: var(--success-soft); color: var(--success); }
.stat-icon.pink { background: rgba(236, 72, 153, 0.15); color: #f472b6; }
.stat-icon.red { background: var(--danger-soft); color: var(--danger); }

.stat-label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stat-value {
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  margin-top: 0.45rem;
}

.stat-value.error-count {
  color: var(--danger);
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.35rem 1.45rem;
  box-shadow: var(--shadow);
  margin-bottom: 1.5rem;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.1rem;
  flex-wrap: wrap;
}

.card-header h2,
.card-header h3 {
  margin: 0;
  font-size: 1.08rem;
  font-weight: 700;
}

/* ==========================================================================
   Connected Accounts Grid
   ========================================================================== */

.accounts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(330px, 1fr));
  gap: 1rem;
}

.account-card {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  transition: border-color 0.15s;
}

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

.account-card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
}

.account-channels {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.channel-row {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.55rem 0.7rem;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.channel-dot {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 0.75rem;
  flex-shrink: 0;
}

.channel-dot.fb { background: var(--fb-blue); }
.channel-dot.ig { background: var(--ig-grad); }

.account-card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  flex-wrap: wrap;
  padding-top: 0.65rem;
  border-top: 1px solid var(--border);
}

.accounts-strip {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  flex-wrap: wrap;
}

.account-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.75rem;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 0.83rem;
  font-weight: 500;
}

.dot-live {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 0 3px var(--success-soft);
}

.dot-paused {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
}

/* ==========================================================================
   Buttons & Badges
   ========================================================================== */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  padding: 0.58rem 1.05rem;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.15s, background 0.15s, border-color 0.15s;
  text-decoration: none;
  white-space: nowrap;
}

.btn:hover {
  text-decoration: none;
  opacity: 0.94;
}

.btn:active {
  transform: scale(0.98);
}

.btn-primary {
  background: var(--primary);
  color: #ffffff;
  box-shadow: 0 4px 14px rgba(99, 102, 241, 0.28);
}

.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-meta {
  background: linear-gradient(135deg, #1877f2 0%, #6366f1 100%);
  color: #ffffff;
  box-shadow: 0 4px 16px rgba(24, 119, 242, 0.32);
}

.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border);
}

.btn-secondary:hover {
  background: var(--surface-hover);
}

.btn-danger {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: rgba(239, 68, 68, 0.3);
}

.btn-sm {
  padding: 0.36rem 0.7rem;
  font-size: 0.8rem;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.22rem 0.6rem;
  border-radius: 999px;
  font-size: 0.73rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.badge-instagram {
  background: rgba(225, 48, 108, 0.16);
  color: #f472b6;
}

[data-theme="light"] .badge-instagram {
  color: #db2777;
}

.badge-facebook {
  background: rgba(24, 119, 242, 0.16);
  color: #60a5fa;
}

[data-theme="light"] .badge-facebook {
  color: #1877f2;
}

.badge-system,
.badge-inactive,
.badge-ignored,
.badge-info {
  background: rgba(148, 163, 184, 0.16);
  color: var(--muted);
}

.badge-active,
.badge-delivered {
  background: var(--success-soft);
  color: var(--success);
}

.badge-awaiting_follow,
.badge-not_following {
  background: var(--warning-soft);
  color: var(--warning);
}

.badge-error {
  background: var(--danger-soft);
  color: var(--danger);
}

/* ==========================================================================
   Tables & Forms
   ========================================================================== */

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

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

th, td {
  text-align: left;
  padding: 0.85rem 0.75rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

th {
  color: var(--muted);
  font-size: 0.73rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 700;
}

tbody tr:hover {
  background: rgba(148, 163, 184, 0.04);
}

.actions-cell {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.actions-cell form {
  margin: 0;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 1rem;
}

.form-group {
  margin-bottom: 1.05rem;
}

.form-group label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 0.38rem;
  color: var(--text-secondary);
}

.form-hint {
  font-size: 0.78rem;
  color: var(--muted);
  margin-top: 0.3rem;
}

input[type="text"],
input[type="password"],
input[type="url"],
select,
textarea {
  width: 100%;
  padding: 0.65rem 0.8rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg);
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
}

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

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

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.checkbox-row input {
  width: 16px;
  height: 16px;
  accent-color: var(--primary);
}

/* ==========================================================================
   ManyChat-Style Split Rule Builder + Live Phone Preview
   ========================================================================== */

.builder-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.45fr) minmax(300px, 0.85fr);
  gap: 1.5rem;
  align-items: start;
}

.step-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 0.78rem;
  font-weight: 800;
  margin-right: 0.45rem;
}

.step-heading {
  display: flex;
  align-items: center;
  font-size: 0.95rem;
  font-weight: 700;
  margin: 0 0 0.95rem;
  padding-bottom: 0.55rem;
  border-bottom: 1px solid var(--border);
}

.phone-sticky {
  position: sticky;
  top: 84px;
}

.phone-frame {
  background: #0b0e14;
  border: 2px solid var(--border-strong);
  border-radius: 30px;
  padding: 0.95rem;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.45);
  color: #f8fafc;
}

.phone-header {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.4rem 0.4rem 0.75rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  margin-bottom: 0.85rem;
}

.phone-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--ig-grad);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 0.8rem;
}

.phone-chat {
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  min-height: 360px;
}

.chat-note {
  text-align: center;
  font-size: 0.72rem;
  color: #94a3b8;
  margin: 0.15rem 0;
}

.dm-card {
  background: #1e2433;
  border-radius: 16px;
  padding: 0.8rem 0.85rem;
  font-size: 0.82rem;
  line-height: 1.45;
  max-width: 92%;
  white-space: pre-wrap;
}

.dm-btn {
  display: block;
  width: 100%;
  text-align: center;
  background: #2c3447;
  color: #f8fafc;
  border-radius: 9px;
  padding: 0.48rem 0.6rem;
  font-size: 0.78rem;
  font-weight: 600;
  margin-top: 0.45rem;
}

.dm-user-bubble {
  align-self: flex-end;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  color: #fff;
  padding: 0.5rem 0.8rem;
  border-radius: 16px;
  font-size: 0.8rem;
  font-weight: 600;
}

/* ==========================================================================
   Flash Messages, Login, Code Blocks, Simulator
   ========================================================================== */

.flash-list {
  margin-bottom: 1.15rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.flash {
  padding: 0.8rem 1rem;
  border-radius: 10px;
  font-size: 0.88rem;
  font-weight: 500;
  border: 1px solid var(--border);
}

.flash-success { background: var(--success-soft); border-color: rgba(16, 185, 129, 0.35); color: var(--success); }
.flash-error { background: var(--danger-soft); border-color: rgba(239, 68, 68, 0.35); color: var(--danger); }
.flash-info { background: var(--primary-soft); border-color: rgba(99, 102, 241, 0.35); color: var(--primary); }

.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: radial-gradient(circle at top right, rgba(99, 102, 241, 0.18), transparent 45%),
              radial-gradient(circle at bottom left, rgba(236, 72, 153, 0.12), transparent 45%),
              var(--bg);
}

.login-card {
  width: 100%;
  max-width: 430px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 18px;
  padding: 2rem;
  box-shadow: var(--shadow);
}

pre.code-block {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.85rem;
  overflow-x: auto;
  font-size: 0.8rem;
  line-height: 1.48;
  margin: 0.5rem 0 0;
}

.sim-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(310px, 1fr));
  gap: 1rem;
}

.filter-bar {
  display: flex;
  gap: 0.75rem;
  align-items: end;
  flex-wrap: wrap;
}

.filter-bar .form-group {
  margin-bottom: 0;
  min-width: 160px;
}

.muted { color: var(--muted); }

.empty-state {
  text-align: center;
  padding: 2.5rem 1rem;
  color: var(--muted);
}

/* Responsive */
@media (max-width: 980px) {
  .builder-layout {
    grid-template-columns: 1fr;
  }
  .phone-sticky {
    position: static;
  }
}

@media (max-width: 820px) {
  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .main-area {
    margin-left: 0;
  }
  .mobile-menu-btn {
    display: inline-flex;
  }
  .workspace {
    padding: 1.1rem 1rem 3rem;
  }
  .topbar {
    padding: 0.75rem 1rem;
  }
}
