/* ═══ Dark theme overrides — authed pages only ═══
 * Loaded by includes/dashboard-head.php, so public pages never see it.
 * Activated when <html data-theme="dark"> is present (set by the bootstrap
 * script also in dashboard-head.php, synchronously reading localStorage
 * to avoid FOUC).
 *
 * Strategy: override the existing CSS custom properties so every
 * component built on --bg / --ink / --surface / --line / --accent / etc.
 * flips automatically. Hardcoded hex/rgba in individual module CSS
 * (brands, cert-inventory, etc.) gets patched via scoped overrides at
 * the bottom of this file.
 * ─────────────────────────────────────────────────────────────── */

[data-theme="dark"] {
  /* ── Surfaces (background layers, darkest → lightest) ─────────── */
  --bg: #0E1116;
  --surface: #151A22;
  --surface-2: #1C222D;
  --surface-3: #242B38;

  /* ── Ink (text colours, strongest → weakest) ──────────────────── */
  --ink: #E6E9EE;
  --ink-2: #B5BBC6;
  --ink-3: #7E8695;
  --ink-4: #565D6A;

  /* ── Lines / borders (translucent so they ride the surface tone) ── */
  --line: rgba(230, 233, 238, 0.08);
  --line-2: rgba(230, 233, 238, 0.14);

  /* ── Accent (brighter blue reads better on dark) ──────────────── */
  --accent: #6A95FF;
  --accent-hover: #8FB1FF;
  --accent-ink: #8FB1FF;
  --accent-light: #8FB1FF;
  --accent-subtle: rgba(106, 149, 255, 0.12);
  --accent-soft: rgba(106, 149, 255, 0.12);

  /* ── Status palette (slightly lightened; original values too
         saturated against a dark surface) ───────────────────────── */
  --ok: #22C58D;
  --warn: #F59E0B;
  --danger: #F87171;
  --success: #22C58D;
  --success-light: #34D399;
  --error: #F87171;
  --error-text: #F87171;
  --purple: #818CF8;
  --purple-light: #A5B4FC;

  --status-operational: #22C58D;
  --status-degraded: #F59E0B;
  --status-partial: #FB923C;
  --status-major: #F87171;
  --status-maintenance: #6A95FF;
  --status-unknown: #7E8695;

  /* Legacy token aliases — keep in sync with the dark palette above
     so per-page stylesheets that still use the old names render
     correctly. */
  --bg-primary: #0E1116;
  --bg-secondary: #151A22;
  --bg-tertiary: #1C222D;
  --bg-elevated: #1C222D;
  --text-primary: #E6E9EE;
  --text-secondary: #B5BBC6;
  --text-tertiary: #7E8695;
  --border-color: rgba(230, 233, 238, 0.14);
  --border-subtle: rgba(230, 233, 238, 0.08);
  --shadow-color: rgba(0, 0, 0, 0.45);

  /* Glow effects — darker base + slightly stronger so highlights read */
  --glow-operational: 0 6px 18px rgba(34, 197, 141, 0.18);
  --glow-degraded: 0 6px 18px rgba(245, 158, 11, 0.20);
  --glow-partial: 0 6px 18px rgba(251, 146, 60, 0.22);
  --glow-major: 0 6px 20px rgba(248, 113, 113, 0.24);
  --glow-maintenance: 0 6px 18px rgba(106, 149, 255, 0.20);
  --glow-unknown: 0 3px 10px rgba(126, 134, 149, 0.16);
  --glow-operational-hover: 0 8px 22px rgba(34, 197, 141, 0.24);
  --glow-degraded-hover: 0 8px 22px rgba(245, 158, 11, 0.26);
  --glow-partial-hover: 0 8px 22px rgba(251, 146, 60, 0.28);
  --glow-major-hover: 0 8px 24px rgba(248, 113, 113, 0.30);
  --glow-maintenance-hover: 0 8px 22px rgba(106, 149, 255, 0.26);
  --glow-unknown-hover: 0 6px 16px rgba(126, 134, 149, 0.20);
}

/* ═══ Global body flip ═══
   body color-scheme hint tells the UA to render form controls,
   scrollbars, and ::selection in dark. */
[data-theme="dark"] body {
  color-scheme: dark;
  background: var(--bg);
  color: var(--ink);
}

/* Dark-aware focus/selection */
[data-theme="dark"] *::selection { background: rgba(106, 149, 255, 0.30); color: var(--ink); }

/* ═══ Module-specific patches for hardcoded colours ═══
   Each block below neutralises hex/rgba values that are baked into the
   original module CSS so they render sensibly on dark surfaces. We
   don't rewrite the original files — just override at higher specificity. */

/* ── Dashboard first-run welcome panel (dashboard.php inline styles) ── */
[data-theme="dark"] .first-run-welcome {
  background: linear-gradient(135deg, rgba(106, 149, 255, 0.10), rgba(34, 197, 141, 0.08));
  border-color: rgba(106, 149, 255, 0.22);
}
[data-theme="dark"] .first-run-eyebrow { background: var(--accent); }
[data-theme="dark"] .first-run-card {
  background: var(--surface-2);
  border-color: var(--line);
}
[data-theme="dark"] .first-run-card:hover {
  border-color: var(--accent);
  box-shadow: 0 4px 18px rgba(106, 149, 255, 0.20);
}

/* ── Settings danger zones (settings.php inline #ef4444 rgba) ──
   The Danger Zone + delete-account UI has 10 inline style="...#ef4444"
   rules hardcoded. Rather than rewriting them (would risk breaking
   the light theme), override with a precedent-wins rule scoped to the
   delete-account modal + btn-danger, plus a catch-all for any text
   that happens to be inline-coloured #ef4444. */
[data-theme="dark"] h2[style*="#ef4444"],
[data-theme="dark"] .settings-card-title[style*="#ef4444"],
[data-theme="dark"] [style*="color: #ef4444"],
[data-theme="dark"] [style*="color:#ef4444"] {
  color: var(--danger) !important;
}
[data-theme="dark"] button[style*="background: #ef4444"],
[data-theme="dark"] button[style*="background:#ef4444"] {
  background: var(--danger) !important;
  color: #fff !important;
}
[data-theme="dark"] code[style*="rgba(239,68,68"] {
  background: rgba(248, 113, 113, 0.14) !important;
  color: var(--danger) !important;
}

/* ── Dashboard-modern utility patches (shadows + near-black inks) ── */
[data-theme="dark"] .modern-dashboard .dph-btn.primary { background: var(--ink); color: var(--bg); }
[data-theme="dark"] .modern-dashboard .dph-btn.primary:hover { background: #F0F3F8; }
[data-theme="dark"] .modern-dashboard .footer-newsletter-form .btn-primary { background: var(--ink); color: var(--bg); }
[data-theme="dark"] .modern-dashboard .footer-newsletter-form .btn-primary:hover:not(:disabled) { background: #F0F3F8; }

/* ── Segmented tab bars (.tabs) on the Monitors card + Watched SaaS card ──
   Light-mode design assumes `--surface-2` is RECESSED vs `--surface`
   (F5F4F0 under FFFFFF) so the active tab reads as elevated. In dark
   mode our `--surface-2` is LIGHTER than `--surface` (elevation goes
   the other way), which inverted the relationship. We also explicitly
   reset the UA button chrome on inactive tabs — without it, Chrome /
   Safari render <button> with a buttonface background that sits visibly
   lighter than our container once color-scheme: dark is set, making
   inactive tabs look like off-mode light chips (the bug in the screenshot). */
[data-theme="dark"] .tabs {
  background: var(--bg);
  border: 1px solid var(--line);
}
[data-theme="dark"] .tab {
  background: transparent;
  border: 0;
  color: var(--ink-2);
  -webkit-appearance: none;
          appearance: none;
}
[data-theme="dark"] .tab:hover {
  color: var(--ink);
  background: rgba(230, 233, 238, 0.04);
}
[data-theme="dark"] .tab.on {
  background: var(--surface-2);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.35), inset 0 0 0 1px var(--line);
}
/* Pip counters inside inactive tabs: need their own bg since tabs
   container is now --bg (darker). Active-state pips keep their
   colour-mix background from the original rule. */
[data-theme="dark"] .pip {
  background: var(--surface-3);
  color: var(--ink-2);
}
/* .monitor-filter-btn — same segmented-filter pattern used on
   /monitor, /monitor/incidents, /monitor/maintenance. */
[data-theme="dark"] .monitor-filter-bar {
  background: var(--surface);
  border: 1px solid var(--line);
}
[data-theme="dark"] .monitor-filter-btn {
  background: transparent;
  border: 0;
  color: var(--ink-2);
}
[data-theme="dark"] .monitor-filter-btn:hover {
  color: var(--ink);
  background: rgba(230, 233, 238, 0.04);
}
[data-theme="dark"] .monitor-filter-btn.active {
  background: var(--surface-2);
  color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--line);
}

/* ── Brand-monitor frontend (brands.css has ~109 hardcoded) ── */
[data-theme="dark"] .brand-panel,
[data-theme="dark"] .brand-card,
[data-theme="dark"] .brand-list-item {
  background: var(--surface);
  border-color: var(--line);
  color: var(--ink);
}
[data-theme="dark"] .brand-pill-critical,
[data-theme="dark"] .brand-badge-critical { background: rgba(248,113,113,0.14); color: var(--danger); border-color: rgba(248,113,113,0.32); }
[data-theme="dark"] .brand-pill-high,
[data-theme="dark"] .brand-badge-high { background: rgba(251,146,60,0.16); color: #FB923C; }
[data-theme="dark"] .brand-pill-medium,
[data-theme="dark"] .brand-badge-medium { background: rgba(245,158,11,0.16); color: var(--warn); }
[data-theme="dark"] .brand-pill-low,
[data-theme="dark"] .brand-badge-low { background: rgba(34,197,141,0.14); color: var(--ok); }

/* ── Cert-monitor frontend (cert-inventory.css has ~127 hardcoded) ── */
[data-theme="dark"] .cert-summary-card,
[data-theme="dark"] .cert-risk-card,
[data-theme="dark"] .cert-histogram-card,
[data-theme="dark"] .cert-modal {
  background: var(--surface);
  border-color: var(--line);
  color: var(--ink);
}
[data-theme="dark"] .cert-summary-danger .cert-summary-value { color: var(--danger); }
[data-theme="dark"] .cert-summary-warning .cert-summary-value { color: var(--warn); }

/* Risk-tier bar segments */
[data-theme="dark"] .cert-risk-high    { background: var(--danger); }
[data-theme="dark"] .cert-risk-medium  { background: var(--warn); }
[data-theme="dark"] .cert-risk-low     { background: #FACC15; }
[data-theme="dark"] .cert-risk-healthy { background: var(--ok); }

/* Modal overlay — original is rgba(0,0,0,0.5) which is fine on both,
   but lighten it slightly on dark so it feels like a layer, not a void. */
[data-theme="dark"] .cert-modal-overlay { background: rgba(0, 0, 0, 0.65); }

/* ── Monitor detail page charts panel ── */
[data-theme="dark"] .monitor-detail-panel,
[data-theme="dark"] .pmd-card,
[data-theme="dark"] .settings-card {
  background: var(--surface);
  border-color: var(--line);
  color: var(--ink);
}

/* ── Settings nav items (sidebar chrome) ── */
[data-theme="dark"] .settings-nav-item {
  color: var(--ink-2);
}
[data-theme="dark"] .settings-nav-item:hover {
  background: var(--surface-2);
  color: var(--ink);
}
[data-theme="dark"] .settings-nav-item.active {
  background: var(--surface-2);
  color: var(--ink);
}

/* Theme toggle in the top navbar (dashboard page only). Circular
   icon button with subtle hover surface. Global rule so it looks
   right in both themes. */
.sa-theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--border-color, rgba(15, 15, 14, 0.14));
  border-radius: 8px;
  background: transparent;
  color: var(--text-secondary, var(--ink-2, #3F3F3C));
  cursor: pointer;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
  padding: 0;
  margin-right: 6px;
}
.sa-theme-toggle:hover {
  background: var(--bg-secondary, var(--surface-2, #F5F4F0));
  color: var(--text-primary, var(--ink, #0F0F0E));
  border-color: var(--border-color, rgba(15, 15, 14, 0.24));
}
.sa-theme-toggle:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.sa-theme-toggle #sa-theme-icon {
  flex-shrink: 0;
}
/* Hide on mobile widths where the nav collapses to a hamburger */
@media (max-width: 820px) {
  .sa-theme-toggle { display: none; }
}

/* ── Exposure hero card (fine — already token-driven) ── */
/* ── Asset Inventory (already token-driven, no patch needed) ── */

/* ── Dashboard hero card (exposure-hero, mons rows, etc.)
       — token-driven, no patch needed ── */

/* ── Inline form controls (inputs/selects/textareas) that use
       white bg by default via UA style. The body color-scheme: dark
       hint above handles most of this, but a few hand-styled inputs
       need the surface token. ── */
[data-theme="dark"] input[type="text"]:not([style]),
[data-theme="dark"] input[type="email"]:not([style]),
[data-theme="dark"] input[type="url"]:not([style]),
[data-theme="dark"] input[type="search"]:not([style]),
[data-theme="dark"] input[type="password"]:not([style]),
[data-theme="dark"] textarea:not([style]),
[data-theme="dark"] select:not([style]) {
  background: var(--surface);
  color: var(--ink);
  border-color: var(--line-2);
}

/* ── Chart.js default text / gridline colours. Our charts use
       Chart.defaults globals set by the chart-theme helper in
       /auth/chart-theme.js — see that file for the runtime flip. ── */
