/* --- Explicit Light Mode Sidebar Styles (Default) --- */
html:not(.dark) aside#sidebar {
  background-color: #EEF5FF !important;
  border-color: rgba(180, 212, 255, 0.6) !important;
  color: #475569 !important;
}

html:not(.dark) aside#sidebar .sidebar-collapse-btn {
  background-color: #ffffff !important;
  border-color: #B4D4FF !important;
  color: #176B87 !important;
}
html:not(.dark) aside#sidebar .sidebar-collapse-btn:hover {
  background-color: #f8fafc !important;
}

html:not(.dark) aside#sidebar .sidebar-divider {
  border-color: rgba(180, 212, 255, 0.4) !important;
}

html:not(.dark) aside#sidebar .sidebar-footer {
  background-color: rgba(255, 255, 255, 0.5) !important;
  border-color: rgba(180, 212, 255, 0.4) !important;
}

html:not(.dark) aside#sidebar .sidebar-logout-btn {
  color: #64748b !important;
  background-color: transparent !important;
}
html:not(.dark) aside#sidebar .sidebar-logout-btn:hover {
  background-color: #fef2f2 !important;
  color: #dc2626 !important;
}

/* Sidebar Menu Hover Overrides in Light Mode */
html:not(.dark) aside#sidebar a:hover,
html:not(.dark) aside#sidebar button.dropdown-btn:hover {
  background-color: #ffffff !important;
  color: #176B87 !important;
  border-color: rgba(180, 212, 255, 0.6) !important;
  box-shadow: 0 1px 3px rgba(23, 107, 135, 0.08) !important;
}

html:not(.dark) aside#sidebar a:hover i,
html:not(.dark) aside#sidebar button.dropdown-btn:hover i {
  color: #176B87 !important;
}

/* Light Mode Card & Container Overrides */
html:not(.dark) .role-metric-card,
html:not(.dark) .user-metric-card {
  background-color: #ffffff !important;
  border-color: rgba(226, 232, 240, 0.9) !important;
  color: #0f172a !important;
}

html:not(.dark) .role-metric-card:hover,
html:not(.dark) .user-metric-card:hover {
  background-color: #ffffff !important;
  border-color: #cbd5e1 !important;
  box-shadow: 0 10px 25px -5px rgba(15, 23, 42, 0.08) !important;
}

html.dark .role-metric-card,
html.dark .user-metric-card {
  background-color: #1e293b !important;
  border-color: #334155 !important;
}

html:not(.dark) main {
  background-color: #F8FAFC !important;
}

/* Metric Card Numbers High-Contrast Text Colors */
html:not(.dark) #statTotalRoles,
html:not(.dark) #statTotalUsers {
  color: #0f172a !important;
}
html.dark #statTotalRoles,
html.dark #statTotalUsers {
  color: #ffffff !important;
}

html:not(.dark) #statGlobalRoles,
html:not(.dark) #statDepts {
  color: #2563eb !important;
}
html.dark #statGlobalRoles,
html.dark #statDepts {
  color: #60a5fa !important;
}

html:not(.dark) #statActiveRoles,
html:not(.dark) #statActiveUsers {
  color: #059669 !important;
}
html.dark #statActiveRoles,
html.dark #statActiveUsers {
  color: #34d399 !important;
}

html:not(.dark) #statSystemRoles {
  color: #d97706 !important;
}
html.dark #statSystemRoles {
  color: #fcd34d !important;
}

html:not(.dark) #statDeactivatedUsers {
  color: #e11d48 !important;
}
html.dark #statDeactivatedUsers {
  color: #fb7185 !important;
}

/* ============================================================
   CIVENTRAL — UNIFIED DARK MODE DESIGN SYSTEM
   ============================================================
   Synchronization contract used by every rule in this file:

   1) Shell rules (body / main / header / sidebar) keep
      `!important` so every single page renders ONE identical
      background + text colour — no page drifts.

   2) All palette rules are written as
          html.dark :where([class~="token"])
      which has specificity (0,1,1):
        • beats a bare Tailwind utility  (0,1,0)
        • loses to an authored `dark:`   (0,2,1)
      so any `dark:` already written inline in the markup still
      wins, and this layer only ever FILLS THE GAPS. No rule
      fights another rule.

   3) Tokens are matched EXACTLY (`~=`), never by substring —
      `bg-emerald-50` can no longer clobber `bg-emerald-500`,
      `hover:bg-white` is handled explicitly, and `divide-*`
      / `ring-*` / opacity variants are all covered.
   ============================================================ */

html.dark {
  color-scheme: dark;

  /* --- unified surface scale (single source of truth) --- */
  --civ-bg:        #0b1329;  /* page background              */
  --civ-surface:   #0f172a;  /* header, sidebar, inputs, inset */
  --civ-card:      #1e293b;  /* cards, dropdowns, panels     */
  --civ-elev:      #263449;  /* hover surface                */
  --civ-elev-2:    #334155;  /* stronger hover / tracks      */
  --civ-line:      #334155;  /* borders                      */
  --civ-line-soft: #1e293b;  /* subtle dividers              */
  --civ-text:      #f1f5f9;  /* primary text                 */
  --civ-text-mut:  #cbd5e1;  /* secondary text               */
  --civ-text-dim:  #94a3b8;  /* muted / label text           */
  --civ-accent:    #86B6F6;  /* brand accent on dark         */
  --civ-danger:    #fb7185;
}

/* ------------------------------------------------------------
   A. Shell (deterministic — always one value, every page)
   ------------------------------------------------------------ */
html.dark body {
  background-color: var(--civ-bg) !important;
  color: var(--civ-text) !important;
}

html.dark main {
  background-color: var(--civ-bg) !important;
}

html.dark header {
  background-color: var(--civ-surface) !important;
  border-color: var(--civ-line-soft) !important;
}

/* ------------------------------------------------------------
   B. Sidebar
   ------------------------------------------------------------ */
html.dark aside#sidebar {
  background-color: var(--civ-surface) !important;
  border-color: var(--civ-line-soft) !important;
  color: var(--civ-text-dim) !important;
}

html.dark aside#sidebar .sidebar-collapse-btn {
  background-color: var(--civ-card) !important;
  border-color: var(--civ-line) !important;
  color: var(--civ-accent) !important;
}
html.dark aside#sidebar .sidebar-collapse-btn:hover {
  background-color: var(--civ-elev-2) !important;
}

html.dark aside#sidebar .sidebar-divider {
  border-color: var(--civ-line-soft) !important;
}

html.dark aside#sidebar .sidebar-footer {
  background-color: var(--civ-bg) !important;
  border-color: var(--civ-line-soft) !important;
}

html.dark aside#sidebar .sidebar-logout-btn {
  color: var(--civ-danger) !important;
  background-color: transparent !important;
}
html.dark aside#sidebar .sidebar-logout-btn:hover {
  background-color: rgba(136, 19, 55, 0.4) !important;
  color: #fda4af !important;
}

html.dark aside#sidebar a.bg-white,
html.dark aside#sidebar button.bg-white {
  background-color: var(--civ-card) !important;
  border-color: var(--civ-line) !important;
  color: var(--civ-accent) !important;
}

html.dark aside#sidebar a:hover,
html.dark aside#sidebar button.dropdown-btn:hover,
html.dark aside#sidebar [class~="hover:bg-white"]:hover {
  background-color: rgba(30, 41, 59, 0.85) !important;
  color: var(--civ-accent) !important;
  border-color: rgba(51, 65, 85, 0.6) !important;
}

html.dark aside#sidebar a:hover i,
html.dark aside#sidebar button.dropdown-btn:hover i {
  color: var(--civ-accent) !important;
}

html.dark aside#sidebar i.text-brand-medium {
  color: var(--civ-accent) !important;
}

/* ------------------------------------------------------------
   C. Neutral surfaces — cards, panels, table heads, insets
   ------------------------------------------------------------ */
html.dark :where(.bg-white) {
  background-color: var(--civ-card);
  border-color: var(--civ-line);
}

html.dark :where(
  [class~="bg-slate-50"],
  [class^="bg-slate-50/"], [class*=" bg-slate-50/"],
  [class~="bg-[#F8FAFC]"]
) {
  background-color: var(--civ-surface);
  border-color: var(--civ-line-soft);
}

html.dark :where(
  [class~="bg-slate-100"],
  [class^="bg-slate-100/"], [class*=" bg-slate-100/"]
) {
  background-color: var(--civ-card);
  border-color: var(--civ-line);
}

html.dark :where(
  [class~="bg-slate-200"],
  [class^="bg-slate-200/"], [class*=" bg-slate-200/"]
) {
  background-color: var(--civ-elev-2);
  border-color: var(--civ-line);
}

html.dark :where(
  [class~="bg-brand-light"],
  [class^="bg-brand-light/"], [class*=" bg-brand-light/"],
  [class~="bg-[#EEF5FF]"]
) {
  background-color: var(--civ-card);
  border-color: var(--civ-line);
  color: var(--civ-accent);
}

/* ------------------------------------------------------------
   D. Pale (light-only) gradients are flattened to a card surface.
      Vivid gradients (from-violet-600, from-brand-dark …) are
      intentionally left untouched — they read well on dark.
   ------------------------------------------------------------ */
html.dark :where(
  [class^="from-brand-light"], [class*=" from-brand-light"],
  [class^="to-blue-50"],       [class*=" to-blue-50"],
  [class^="from-blue-50"],     [class*=" from-blue-50"],
  [class^="via-indigo-50"],    [class*=" via-indigo-50"]
) {
  background-image: none;
  background-color: var(--civ-card);
  border-color: var(--civ-line);
}


/* ------------------------------------------------------------
   E. Text — slate / gray scale
   ------------------------------------------------------------ */
html.dark :where(
  [class~="text-slate-900"], [class~="text-slate-800"],
  [class~="text-slate-700"], [class^="text-slate-700/"], [class*=" text-slate-700/"],
  [class~="text-gray-900"],  [class~="text-gray-800"],  [class~="text-gray-700"],
  [class~="text-[#0f172a]"]
) { color: var(--civ-text); }

html.dark :where(
  [class~="text-slate-600"], [class~="text-slate-500"],
  [class~="text-slate-300"], [class~="text-gray-600"],
  [class~="text-[#1e293b]"], [class~="text-[#334155]"], [class~="text-[#475569]"],
  [class~="text-slate-650"]  /* legacy misspelling — also corrected in markup */
) { color: var(--civ-text-mut); }

html.dark :where(
  [class~="text-slate-400"], [class~="text-gray-500"],
  [class^="text-gray-400/"], [class*=" text-gray-400/"],
  [class~="text-slate-450"], [class~="text-slate-350"]
) { color: var(--civ-text-dim); }

/* Brand literals — #176B87 is too dark to read on navy */
html.dark :where(
  [class~="text-brand-dark"], [class~="text-[#176B87]"],
  [class~="hover:text-brand-dark"]:hover,
  [class~="hover:text-[#176B87]"]:hover,
  [class~="hover:text-[#1e3a8a]"]:hover,
  [class~="hover:text-[#0f172a]"]:hover
) { color: var(--civ-accent); }

html.dark :where(
  [class~="hover:text-slate-800"]:hover,
  [class~="hover:text-slate-700"]:hover,
  [class~="hover:text-slate-600"]:hover,
  [class~="hover:text-slate-500"]:hover
) { color: var(--civ-text); }

html.dark .group:hover :where(
  [class~="group-hover:text-[#176B87]"],
  [class~="group-hover:text-brand-dark"]
) { color: var(--civ-accent); }

/* ------------------------------------------------------------
   F. Text — accent hues
      Light-mode shades (500-900) are too dark to read on navy,
      so each hue is lifted to its dark-safe equivalent.
      Shades 100-400 are already light and are left alone.
   ------------------------------------------------------------ */
html.dark :where([class~="text-blue-500"],   [class~="text-blue-600"])   { color: #60a5fa; }
html.dark :where([class~="text-blue-700"],   [class~="text-blue-800"],
                 [class~="text-blue-900"],   [class~="text-blue-950"])   { color: #93c5fd; }
html.dark :where([class~="text-indigo-500"], [class~="text-indigo-600"]) { color: #818cf8; }
html.dark :where([class~="text-indigo-700"], [class~="text-indigo-800"], [class~="text-indigo-900"]) { color: #a5b4fc; }
html.dark :where([class~="text-violet-500"], [class~="text-violet-600"]) { color: #a78bfa; }
html.dark :where([class~="text-violet-700"], [class~="text-violet-800"], [class~="text-violet-900"]) { color: #c4b5fd; }
html.dark :where([class~="text-purple-500"], [class~="text-purple-600"]) { color: #c084fc; }
html.dark :where([class~="text-purple-700"], [class~="text-purple-800"], [class~="text-purple-900"]) { color: #d8b4fe; }
html.dark :where([class~="text-pink-500"],   [class~="text-pink-600"])   { color: #f472b6; }
html.dark :where([class~="text-pink-700"],   [class~="text-pink-800"])   { color: #f9a8d4; }
html.dark :where([class~="text-rose-500"],   [class~="text-rose-600"])   { color: #fb7185; }
html.dark :where([class~="text-rose-700"],   [class~="text-rose-800"],
                 [class~="text-rose-900"])                               { color: #fda4af; }
html.dark :where([class~="text-red-500"],    [class~="text-red-600"])    { color: #f87171; }
html.dark :where([class~="text-red-700"],    [class~="text-red-800"],
                 [class~="text-red-900"],    [class~="text-red-950"])    { color: #fca5a5; }
html.dark :where([class~="text-orange-500"], [class~="text-orange-600"]) { color: #fb923c; }
html.dark :where([class~="text-orange-700"], [class~="text-orange-800"], [class~="text-orange-900"]) { color: #fdba74; }
html.dark :where([class~="text-amber-500"],  [class~="text-amber-600"])  { color: #fbbf24; }
html.dark :where([class~="text-amber-700"],  [class~="text-amber-800"],
                 [class~="text-amber-900"])                              { color: #fcd34d; }
html.dark :where([class~="text-yellow-500"], [class~="text-yellow-600"],
                 [class~="text-yellow-700"])                             { color: #fde047; }
html.dark :where([class~="text-lime-500"],   [class~="text-lime-600"],
                 [class~="text-lime-700"])                               { color: #a3e635; }
html.dark :where([class~="text-green-500"],  [class~="text-green-600"],
                 [class~="text-green-700"])                              { color: #4ade80; }
html.dark :where([class~="text-emerald-500"], [class~="text-emerald-600"]) { color: #34d399; }
html.dark :where([class~="text-emerald-700"], [class~="text-emerald-800"],
                 [class~="text-emerald-900"])                            { color: #6ee7b7; }
html.dark :where([class~="text-teal-500"],   [class~="text-teal-600"])   { color: #2dd4bf; }
html.dark :where([class~="text-teal-700"],   [class~="text-teal-800"])   { color: #5eead4; }
html.dark :where([class~="text-cyan-500"],   [class~="text-cyan-600"])   { color: #22d3ee; }
html.dark :where([class~="text-cyan-700"],   [class~="text-cyan-800"])   { color: #67e8f9; }
html.dark :where([class~="text-sky-500"],    [class~="text-sky-600"])    { color: #38bdf8; }
html.dark :where([class~="text-sky-700"],    [class~="text-sky-800"])    { color: #7dd3fc; }
html.dark :where([class~="text-fuchsia-500"], [class~="text-fuchsia-600"]) { color: #e879f9; }
html.dark :where([class~="text-fuchsia-700"], [class~="text-fuchsia-800"]) { color: #f0abfc; }

/* ------------------------------------------------------------
   G. Borders — slate/gray/brand + every accent hue at 100-300
   ------------------------------------------------------------ */
html.dark :where(
  [class~="border-slate-100"], [class^="border-slate-100/"], [class*=" border-slate-100/"],
  [class~="border-slate-200"], [class^="border-slate-200/"], [class*=" border-slate-200/"],
  [class~="border-slate-300"], [class~="border-slate-150"],  [class~="border-slate-350"],
  [class~="border-slate-700"], [class~="border-slate-800"],  [class~="border-slate-900"],
  [class~="border-gray-100"],  [class~="border-gray-200"],   [class~="border-gray-300"]
) { border-color: var(--civ-line); }

html.dark :where(
  [class~="border-brand-border"], [class~="border-[#B4D4FF]"],
  [class~="hover:border-brand-border"]:hover
) { border-color: rgba(134, 182, 246, 0.35); }

html.dark :where(
  [class~="border-brand-medium"], [class~="border-brand-dark"], [class~="border-[#72a6eb]"]
) { border-color: rgba(134, 182, 246, 0.5); }

html.dark :where(
  [class~="border-blue-100"],   [class^="border-blue-100/"],   [class*=" border-blue-100/"],
  [class~="border-blue-150"],   [class~="border-blue-200"],    [class^="border-blue-200/"],   [class*=" border-blue-200/"],
  [class~="border-indigo-100"], [class~="border-indigo-200"],  [class^="border-indigo-200/"],
  [class~="border-violet-100"], [class~="border-violet-200"],
  [class~="border-purple-100"], [class^="border-purple-100/"], [class*=" border-purple-100/"],
  [class~="border-purple-150"], [class~="border-purple-200"],  [class^="border-purple-200/"],
  [class~="border-pink-100"],   [class~="border-pink-200"],
  [class~="border-rose-100"],   [class^="border-rose-100/"],   [class*=" border-rose-100/"],
  [class~="border-rose-150"],   [class~="border-rose-200"],    [class^="border-rose-200/"],   [class*=" border-rose-200/"]
) { border-color: rgba(129, 140, 248, 0.35); }

html.dark :where(
  [class~="border-red-100"],    [class~="border-red-200"],    [class^="border-red-200/"],
  [class~="border-orange-100"], [class~="border-orange-200"],
  [class~="border-amber-100"],  [class^="border-amber-100/"],  [class*=" border-amber-100/"],
  [class~="border-amber-150"],  [class~="border-amber-200"],   [class^="border-amber-200/"],  [class*=" border-amber-200/"],
  [class~="border-amber-250"],
  [class~="border-yellow-100"], [class~="border-yellow-200"],
  [class~="border-lime-100"],   [class~="border-lime-200"],
  [class~="border-green-100"],  [class~="border-green-200"]
) { border-color: rgba(245, 158, 11, 0.35); }

html.dark :where(
  [class~="border-emerald-100"], [class^="border-emerald-100/"], [class*=" border-emerald-100/"],
  [class~="border-emerald-150"], [class~="border-emerald-200"],  [class^="border-emerald-200/"], [class*=" border-emerald-200/"]
) { border-color: rgba(16, 185, 129, 0.35); }

html.dark :where(
  [class~="border-teal-100"],  [class~="border-teal-200"],  [class^="border-teal-200/"],
  [class~="border-cyan-100"],  [class~="border-cyan-200"],  [class^="border-cyan-200/"],
  [class~="border-sky-100"],   [class~="border-sky-200"],   [class^="border-sky-200/"]
) { border-color: rgba(34, 211, 238, 0.35); }

/* ------------------------------------------------------------
   H. Dividers — divide-* writes border-color on the children
   ------------------------------------------------------------ */
html.dark :where(
  [class~="divide-slate-100"], [class^="divide-slate-100/"], [class*=" divide-slate-100/"],
  [class~="divide-slate-150"], [class~="divide-gray-100"], [class~="divide-gray-200"]
) > :not(:last-child) { border-color: var(--civ-line-soft); }

/* ------------------------------------------------------------
   I. Focus — dark focus borders/rings would be invisible
   ------------------------------------------------------------ */
html.dark :where(
  [class~="focus:border-slate-900"]:focus,
  [class~="focus:border-slate-800"]:focus,
  [class~="focus:border-slate-700"]:focus,
  [class~="focus:border-[#0f172a]"]:focus,
  [class~="focus:border-brand-dark"]:focus
) { border-color: rgba(134, 182, 246, 0.7); }

html.dark :where(
  [class~="focus:ring-[#0f172a]"]:focus,
  [class~="focus:ring-slate-900/10"]:focus,
  [class~="focus:ring-slate-600/10"]:focus,
  [class~="focus:ring-slate-600/20"]:focus
) { --tw-ring-color: rgba(134, 182, 246, 0.45); }

/* ------------------------------------------------------------
   J. Table hover — fallback for rows with no hover class of their own
   ------------------------------------------------------------ */
html.dark tr:hover { background-color: rgba(30, 41, 59, 0.7); }

/* ------------------------------------------------------------
   K. Accent chips & badges — bg-{hue}-50/100/200
      Light pastel chips stay pastel in dark mode, which makes
      their -700 labels unreadable. Every chip is re-tinted to a
      translucent version of its OWN hue so it reads as a dark
      badge with a light label. `-400/-500/-600+` (vivid fills)
      are deliberately NOT touched.
   ------------------------------------------------------------ */
html.dark :where(
  [class~="bg-blue-50"],  [class^="bg-blue-50/"],  [class*=" bg-blue-50/"],
  [class~="bg-blue-100"], [class^="bg-blue-100/"], [class*=" bg-blue-100/"],
  [class~="bg-blue-200"]
) { background-color: rgba(59, 130, 246, 0.16); }

html.dark :where(
  [class~="bg-indigo-50"],  [class^="bg-indigo-50/"],  [class*=" bg-indigo-50/"],
  [class~="bg-indigo-100"], [class^="bg-indigo-100/"], [class*=" bg-indigo-100/"],
  [class~="bg-indigo-200"]
) { background-color: rgba(99, 102, 241, 0.16); }

html.dark :where(
  [class~="bg-violet-50"],  [class^="bg-violet-50/"],  [class*=" bg-violet-50/"],
  [class~="bg-violet-100"], [class^="bg-violet-100/"], [class*=" bg-violet-100/"],
  [class~="bg-violet-200"]
) { background-color: rgba(139, 92, 246, 0.16); }

html.dark :where(
  [class~="bg-purple-50"],  [class^="bg-purple-50/"],  [class*=" bg-purple-50/"],
  [class~="bg-purple-100"], [class^="bg-purple-100/"], [class*=" bg-purple-100/"],
  [class~="bg-purple-200"]
) { background-color: rgba(168, 85, 247, 0.16); }

html.dark :where(
  [class~="bg-pink-50"],  [class^="bg-pink-50/"],  [class*=" bg-pink-50/"],
  [class~="bg-pink-100"], [class^="bg-pink-100/"], [class*=" bg-pink-100/"],
  [class~="bg-pink-200"]
) { background-color: rgba(236, 72, 153, 0.16); }

html.dark :where(
  [class~="bg-rose-50"],  [class^="bg-rose-50/"],  [class*=" bg-rose-50/"],
  [class~="bg-rose-100"], [class^="bg-rose-100/"], [class*=" bg-rose-100/"],
  [class~="bg-rose-200"]
) { background-color: rgba(244, 63, 94, 0.16); }

html.dark :where(
  [class~="bg-red-50"],   [class^="bg-red-50/"],   [class*=" bg-red-50/"],
  [class~="bg-red-100"],  [class^="bg-red-100/"],  [class*=" bg-red-100/"],
  [class~="bg-red-200"]
) { background-color: rgba(239, 68, 68, 0.16); }

html.dark :where(
  [class~="bg-orange-50"],  [class^="bg-orange-50/"],  [class*=" bg-orange-50/"],
  [class~="bg-orange-100"], [class^="bg-orange-100/"], [class*=" bg-orange-100/"],
  [class~="bg-orange-200"]
) { background-color: rgba(249, 115, 22, 0.16); }

html.dark :where(
  [class~="bg-amber-50"],  [class^="bg-amber-50/"],  [class*=" bg-amber-50/"],
  [class~="bg-amber-100"], [class^="bg-amber-100/"], [class*=" bg-amber-100/"],
  [class~="bg-amber-200"]
) { background-color: rgba(245, 158, 11, 0.16); }

html.dark :where(
  [class~="bg-yellow-50"],  [class^="bg-yellow-50/"],  [class*=" bg-yellow-50/"],
  [class~="bg-yellow-100"], [class^="bg-yellow-100/"], [class*=" bg-yellow-100/"],
  [class~="bg-yellow-200"]
) { background-color: rgba(234, 179, 8, 0.16); }

html.dark :where(
  [class~="bg-lime-50"],  [class^="bg-lime-50/"],  [class*=" bg-lime-50/"],
  [class~="bg-lime-100"], [class^="bg-lime-100/"], [class*=" bg-lime-100/"],
  [class~="bg-lime-200"]
) { background-color: rgba(163, 230, 53, 0.16); }

html.dark :where(
  [class~="bg-green-50"],  [class^="bg-green-50/"],  [class*=" bg-green-50/"],
  [class~="bg-green-100"], [class^="bg-green-100/"], [class*=" bg-green-100/"],
  [class~="bg-green-200"]
) { background-color: rgba(34, 197, 94, 0.16); }

html.dark :where(
  [class~="bg-emerald-50"],  [class^="bg-emerald-50/"],  [class*=" bg-emerald-50/"],
  [class~="bg-emerald-100"], [class^="bg-emerald-100/"], [class*=" bg-emerald-100/"],
  [class~="bg-emerald-200"]
) { background-color: rgba(16, 185, 129, 0.16); }

html.dark :where(
  [class~="bg-teal-50"],  [class^="bg-teal-50/"],  [class*=" bg-teal-50/"],
  [class~="bg-teal-100"], [class^="bg-teal-100/"], [class*=" bg-teal-100/"],
  [class~="bg-teal-200"]
) { background-color: rgba(20, 184, 166, 0.16); }

html.dark :where(
  [class~="bg-cyan-50"],  [class^="bg-cyan-50/"],  [class*=" bg-cyan-50/"],
  [class~="bg-cyan-100"], [class^="bg-cyan-100/"], [class*=" bg-cyan-100/"],
  [class~="bg-cyan-200"]
) { background-color: rgba(6, 182, 212, 0.16); }

html.dark :where(
  [class~="bg-sky-50"],  [class^="bg-sky-50/"],  [class*=" bg-sky-50/"],
  [class~="bg-sky-100"], [class^="bg-sky-100/"], [class*=" bg-sky-100/"],
  [class~="bg-sky-200"]
) { background-color: rgba(14, 165, 233, 0.16); }

html.dark :where(
  [class~="bg-gray-50"],  [class^="bg-gray-50/"],  [class*=" bg-gray-50/"],
  [class~="bg-gray-100"], [class^="bg-gray-100/"], [class*=" bg-gray-100/"],
  [class~="bg-gray-200"]
) { background-color: var(--civ-card); border-color: var(--civ-line); }

/* ------------------------------------------------------------
   L. Hover surfaces — a light hover in dark mode paints a white
      block with light text. Every light hover is remapped.
   ------------------------------------------------------------ */
html.dark :where(
  [class~="hover:bg-white"],
  [class~="hover:bg-slate-50"], [class^="hover:bg-slate-50/"], [class*=" hover:bg-slate-50/"],
  [class~="hover:bg-brand-light"], [class^="hover:bg-brand-light/"],
  [class~="hover:bg-[#EEF5FF]"]
):hover { background-color: var(--civ-elev); }

html.dark :where(
  [class~="hover:bg-slate-100"], [class^="hover:bg-slate-100/"], [class*=" hover:bg-slate-100/"],
  [class~="hover:bg-slate-200"], [class^="hover:bg-slate-200/"], [class*=" hover:bg-slate-200/"]
):hover { background-color: var(--civ-elev-2); }

html.dark :where(
  [class~="hover:bg-blue-50"],  [class^="hover:bg-blue-50/"],  [class*=" hover:bg-blue-50/"],
  [class~="hover:bg-blue-100"], [class~="hover:bg-indigo-50"], [class~="hover:bg-indigo-100"],
  [class~="hover:bg-violet-50"], [class~="hover:bg-violet-100"],
  [class~="hover:bg-purple-50"], [class~="hover:bg-purple-100"],
  [class~="hover:bg-pink-50"],   [class~="hover:bg-pink-100"],
  [class~="hover:bg-rose-50"],   [class^="hover:bg-rose-50/"],   [class*=" hover:bg-rose-50/"],
  [class~="hover:bg-rose-100"],  [class~="hover:bg-red-50"],  [class~="hover:bg-red-100"],
  [class~="hover:bg-orange-50"], [class~="hover:bg-orange-100"],
  [class~="hover:bg-amber-50"],  [class^="hover:bg-amber-50/"],  [class*=" hover:bg-amber-50/"],
  [class~="hover:bg-amber-100"],
  [class~="hover:bg-yellow-50"], [class~="hover:bg-yellow-100"],
  [class~="hover:bg-lime-50"],   [class~="hover:bg-lime-100"],
  [class~="hover:bg-green-50"],  [class~="hover:bg-green-100"],
  [class~="hover:bg-emerald-50"], [class^="hover:bg-emerald-50/"], [class*=" hover:bg-emerald-50/"],
  [class~="hover:bg-emerald-100"],
  [class~="hover:bg-teal-50"],   [class~="hover:bg-teal-100"],
  [class~="hover:bg-cyan-50"],   [class~="hover:bg-cyan-100"],
  [class~="hover:bg-sky-50"],    [class~="hover:bg-sky-100"],
  [class~="hover:bg-gray-50"],   [class~="hover:bg-gray-100"]
):hover { background-color: rgba(134, 182, 246, 0.14); }

/* ------------------------------------------------------------
   M. Hover text — only dark shades need lifting
   ------------------------------------------------------------ */
html.dark :where(
  [class~="hover:text-red-600"]:hover,    [class~="hover:text-red-700"]:hover,
  [class~="hover:text-orange-600"]:hover, [class~="hover:text-orange-700"]:hover,
  [class~="hover:text-amber-600"]:hover,  [class~="hover:text-amber-700"]:hover,
  [class~="hover:text-emerald-600"]:hover,[class~="hover:text-emerald-700"]:hover,
  [class~="hover:text-teal-600"]:hover,   [class~="hover:text-teal-700"]:hover,
  [class~="hover:text-cyan-600"]:hover,   [class~="hover:text-sky-600"]:hover,
  [class~="hover:text-blue-600"]:hover,   [class~="hover:text-blue-700"]:hover,
  [class~="hover:text-indigo-600"]:hover, [class~="hover:text-indigo-700"]:hover,
  [class~="hover:text-violet-600"]:hover, [class~="hover:text-violet-500"]:hover,
  [class~="hover:text-purple-600"]:hover, [class~="hover:text-purple-700"]:hover,
  [class~="hover:text-pink-600"]:hover,   [class~="hover:text-pink-700"]:hover,
  [class~="hover:text-rose-600"]:hover,   [class~="hover:text-rose-700"]:hover,
  [class~="hover:text-green-600"]:hover,  [class~="hover:text-green-700"]:hover,
  [class~="hover:text-lime-600"]:hover
) { color: var(--civ-accent); }

/* ------------------------------------------------------------
   N. group-hover — Tailwind writes these as ONE class token, so
      the `text-*` / `bg-*` rules above never match them.
   ------------------------------------------------------------ */
html.dark .group:hover :where(
  [class~="group-hover:text-emerald-500"], [class~="group-hover:text-emerald-600"], [class~="group-hover:text-emerald-700"],
  [class~="group-hover:text-blue-500"],     [class~="group-hover:text-blue-600"],
  [class~="group-hover:text-amber-500"],    [class~="group-hover:text-amber-600"],   [class~="group-hover:text-amber-700"],
  [class~="group-hover:text-rose-500"],     [class~="group-hover:text-rose-600"],     [class~="group-hover:text-rose-700"],
  [class~="group-hover:text-red-600"],
  [class~="group-hover:text-orange-600"],
  [class~="group-hover:text-purple-500"],   [class~="group-hover:text-purple-600"],
  [class~="group-hover:text-indigo-500"],   [class~="group-hover:text-indigo-600"],
  [class~="group-hover:text-violet-600"],
  [class~="group-hover:text-pink-500"],     [class~="group-hover:text-pink-600"],
  [class~="group-hover:text-teal-600"],
  [class~="group-hover:text-cyan-600"],
  [class~="group-hover:text-sky-600"],
  [class~="group-hover:text-green-600"],
  [class~="group-hover:text-slate-700"],    [class~="group-hover:text-slate-600"]
) { color: var(--civ-accent); }

html.dark .group:hover :where(
  [class~="group-hover:bg-white"],
  [class~="group-hover:bg-slate-50"],  [class^="group-hover:bg-slate-50/"], [class*=" group-hover:bg-slate-50/"],
  [class~="group-hover:bg-slate-100"], [class^="group-hover:bg-slate-100/"],
  [class~="group-hover:bg-emerald-50"], [class~="group-hover:bg-emerald-100"],
  [class~="group-hover:bg-rose-50"],   [class~="group-hover:bg-rose-100"],
  [class~="group-hover:bg-amber-50"],  [class~="group-hover:bg-amber-100"],
  [class~="group-hover:bg-blue-50"],   [class~="group-hover:bg-blue-100"],
  [class~="group-hover:bg-purple-50"], [class~="group-hover:bg-purple-100"],
  [class~="group-hover:bg-teal-50"],   [class~="group-hover:bg-cyan-50"],
  [class~="group-hover:bg-pink-50"],   [class~="group-hover:bg-violet-50"],
  [class~="group-hover:bg-indigo-50"], [class~="group-hover:bg-red-50"],
  [class~="group-hover:bg-orange-50"], [class~="group-hover:bg-green-50"]
) { background-color: var(--civ-elev); }

/* ------------------------------------------------------------
   O. Form controls
      The old blanket rule painted EVERY input dark navy, which
      destroyed checkboxes, radios, range sliders and file inputs.
      It is now scoped to text-like controls only.
   ------------------------------------------------------------ */
html.dark :where(
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"])
       :not([type="color"]):not([type="submit"]):not([type="button"])
       :not([type="reset"]):not([type="image"]):not([type="hidden"]),
  select,
  textarea
) {
  background-color: var(--civ-surface);
  color: var(--civ-text);
  border-color: var(--civ-line);
}

html.dark :where(
  input::placeholder,
  textarea::placeholder,
  [class~="placeholder:text-slate-300"]::placeholder,
  [class~="placeholder:text-slate-400"]::placeholder,
  [class~="placeholder:text-slate-500"]::placeholder
) { color: var(--civ-text-dim); }

html.dark :where(option) {
  background-color: var(--civ-surface);
  color: var(--civ-text);
}

/* checkboxes / radios / range keep a real, brand-tinted affordance */
html.dark :where(input[type="checkbox"], input[type="radio"], input[type="range"]) {
  accent-color: var(--civ-accent);
}

/* file inputs */
html.dark :where(input[type="file"]) { color: var(--civ-text-mut); }
html.dark :where(input[type="file"]::file-selector-button) {
  background-color: var(--civ-card);
  border: 1px solid var(--civ-line);
  color: var(--civ-accent);
  border-radius: 8px;
  padding: 6px 12px;
  font-weight: 700;
  cursor: pointer;
}
html.dark :where(input[type="file"]::file-selector-button:hover) {
  background-color: var(--civ-elev);
}

/* ------------------------------------------------------------
   P. Scrollbars
      `.custom-scrollbar` was referenced 13x in the markup but was
      NEVER defined in any stylesheet, and native scrollbars stayed
      light in dark mode. Now defined for both themes.
   ------------------------------------------------------------ */
.custom-scrollbar {
  scrollbar-width: thin;
  scrollbar-color: #cbd5e1 transparent;
}
.custom-scrollbar::-webkit-scrollbar { width: 8px; height: 8px; }
.custom-scrollbar::-webkit-scrollbar-track { background: transparent; }
.custom-scrollbar::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 999px; }
.custom-scrollbar::-webkit-scrollbar-thumb:hover { background: #94a3b8; }
.custom-scrollbar::-webkit-scrollbar-corner { background: transparent; }

html.dark .custom-scrollbar {
  scrollbar-color: #475569 transparent;
}
html.dark .custom-scrollbar::-webkit-scrollbar-thumb { background: #475569; }
html.dark .custom-scrollbar::-webkit-scrollbar-thumb:hover { background: #64748b; }

html.dark ::-webkit-scrollbar { width: 10px; height: 10px; }
html.dark ::-webkit-scrollbar-track { background: var(--civ-bg); }
html.dark ::-webkit-scrollbar-thumb {
  background: var(--civ-line);
  border-radius: 999px;
  border: 2px solid var(--civ-bg);
}
html.dark ::-webkit-scrollbar-thumb:hover { background: #475569; }
html.dark ::-webkit-scrollbar-corner { background: var(--civ-bg); }

/* ------------------------------------------------------------
   Q. Selection + smooth theme switch + Firefox scrollbar
   ------------------------------------------------------------ */
html.dark { scrollbar-color: #475569 var(--civ-bg); }
html.dark ::selection { background-color: rgba(134, 182, 246, 0.35); color: #ffffff; }

html.dark body,
html.dark main,
html.dark header,
html.dark aside#sidebar {
  transition: background-color 0.25s ease, border-color 0.25s ease, color 0.25s ease;
}

/* ------------------------------------------------------------
   S. Tailwind token top-up — the last light utilities that were
      still unmapped (verified by re-running the coverage audit).
   ------------------------------------------------------------ */
html.dark :where(
  [class~="border-emerald-50"], [class~="border-rose-50"], [class~="border-amber-50"],
  [class~="border-blue-50"],   [class~="border-cyan-50"],   [class~="border-indigo-50"],
  [class~="border-red-50"],    [class~="border-teal-50"],   [class~="border-purple-50"],
  [class~="border-sky-50"],    [class~="border-violet-50"], [class~="border-pink-50"],
  [class~="border-green-50"],  [class~="border-yellow-50"], [class~="border-lime-50"],
  [class~="border-orange-50"], [class~="border-slate-50"],  [class~="border-gray-50"],
  [class~="hover:border-emerald-50"]:hover, [class~="hover:border-rose-50"]:hover,
  [class~="hover:border-amber-50"]:hover,   [class~="hover:border-cyan-50"]:hover,
  [class~="hover:border-sky-50"]:hover,     [class~="hover:border-purple-50"]:hover,
  [class~="hover:border-teal-50"]:hover,    [class~="hover:border-indigo-50"]:hover,
  [class~="hover:border-red-50"]:hover,     [class~="hover:border-blue-50"]:hover,
  [class~="hover:border-slate-200"]:hover,  [class~="hover:border-slate-300"]:hover,
  [class~="hover:border-rose-100"]:hover,   [class~="hover:border-blue-100"]:hover,
  [class~="hover:border-amber-100"]:hover,  [class~="hover:border-emerald-100"]:hover,
  [class^="hover:border-slate-100/"]:hover, [class^="hover:border-slate-200/"]:hover
) { border-color: var(--civ-line); }

html.dark :where(
  [class~="bg-slate-300"], [class^="bg-slate-300/"], [class*=" bg-slate-300/"]
) { background-color: var(--civ-elev-2); }

/* focus states must not paint a white box in dark mode */
html.dark :where([class~="focus:bg-white"]:focus) { background-color: var(--civ-surface); }

/* legacy class names that Tailwind does not generate (kept as dark fallbacks
   so they still resolve instead of inheriting arbitrarily) */
html.dark :where([class~="bg-slate-150"]) { background-color: var(--civ-elev-2); }
html.dark :where([class~="text-amber-850"]) { color: #fcd34d; }

/* near-opaque white panels (nav bars, sheets) are solid surfaces, not overlays */
html.dark :where([class~="bg-white/95"]) { background-color: rgba(15, 23, 42, 0.92); }

/* ------------------------------------------------------------
   T. Inline `style="…"` colour literals
      8 files write hex colours straight into inline styles, e.g.
      style="background:#eef5ff;color:#176B87;". Inline styles beat
      normal declarations, so these need `!important`.
      Verified across the whole repo: NO inline `background-color:`
      exists, therefore matching `color:#…` cannot collide (the
      `background:` shorthand never contains `color:`).
   ------------------------------------------------------------ */
/* pastel chip backgrounds → dark translucent chip of the same hue */
html.dark [style*="#d1fae5"], html.dark [style*="#a7f3d0"], html.dark [style*="#ecfdf5"] {
  background-image: none !important; background-color: rgba(16, 185, 129, 0.16) !important;
}
html.dark [style*="#eef5ff"] {
  background-image: none !important; background-color: var(--civ-card) !important;
  color: var(--civ-accent) !important;
}
html.dark [style*="#fef3c7"] {
  background-image: none !important; background-color: rgba(245, 158, 11, 0.16) !important;
}
html.dark [style*="#dbeafe"] {
  background-image: none !important; background-color: rgba(59, 130, 246, 0.16) !important;
}
html.dark [style*="#f0e7ff"], html.dark [style*="#e4d5ff"] {
  background-image: none !important; background-color: rgba(139, 92, 246, 0.16) !important;
}
html.dark [style*="#fce7f3"] {
  background-image: none !important; background-color: rgba(236, 72, 153, 0.16) !important;
}
html.dark [style*="#e0f2fe"] {
  background-image: none !important; background-color: rgba(14, 165, 233, 0.16) !important;
}
html.dark [style*="#fee2e2"] {
  background-image: none !important; background-color: rgba(239, 68, 68, 0.16) !important;
}
html.dark [style*="#fef9c3"] {
  background-image: none !important; background-color: rgba(234, 179, 8, 0.16) !important;
}

/* dashed rules drawn straight into inline styles */
html.dark [style*="#e2e8f0"] { border-color: var(--civ-line) !important; }

/* inline text colours → dark-safe equivalents (declared AFTER the chip
   rules so a paired `background:#…;color:#…` resolves to the text value) */
html.dark [style*="color:#059669"], html.dark [style*="color: #059669"] { color: #34d399 !important; }
html.dark [style*="color:#176B87"], html.dark [style*="color: #176B87"],
html.dark [style*="color: var(--brand-dark)"]                                { color: var(--civ-accent) !important; }
html.dark [style*="color:#d97706"], html.dark [style*="color: #d97706"] { color: #fbbf24 !important; }
html.dark [style*="color:#7c3aed"], html.dark [style*="color: #7c3aed"] { color: #a78bfa !important; }
html.dark [style*="color:#dc2626"], html.dark [style*="color: #dc2626"],
html.dark [style*="color:#DC2626"], html.dark [style*="color: #DC2626"] { color: #f87171 !important; }
html.dark [style*="color:#2563eb"], html.dark [style*="color: #2563eb"] { color: #60a5fa !important; }
html.dark [style*="color:#0f172a"], html.dark [style*="color: #0f172a"],
html.dark [style*="color:#1e293b"], html.dark [style*="color: #1e293b"],
html.dark [style*="color:#334155"], html.dark [style*="color: #334155"] { color: var(--civ-text) !important; }
html.dark [style*="color:#64748b"], html.dark [style*="color: #64748b"] { color: var(--civ-text-mut) !important; }
html.dark [style*="color:#0284c7"], html.dark [style*="color: #0284c7"] { color: #7dd3fc !important; }
html.dark [style*="color:#db2777"], html.dark [style*="color: #db2777"] { color: #f9a8d4 !important; }
html.dark [style*="color:#ec4899"], html.dark [style*="color: #ec4899"] { color: #f472b6 !important; }
html.dark [style*="color:#ca8a04"], html.dark [style*="color: #ca8a04"] { color: #fde047 !important; }
html.dark [style*="color:#047857"], html.dark [style*="color: #047857"] { color: #6ee7b7 !important; }

/* ------------------------------------------------------------
   R. PRINT SAFETY
      Dark mode must never reach paper: reports, ID cards, the
      printable referral slip and exported HTML all print light.
   ------------------------------------------------------------ */
@media print {
  html.dark { color-scheme: light; }

  html.dark,
  html.dark body,
  html.dark main,
  html.dark header,
  html.dark aside#sidebar,
  html.dark :where(.bg-white),
  html.dark :where([class~="bg-slate-50"]),  html.dark :where([class~="bg-slate-100"]),
  html.dark :where([class~="bg-slate-200"]), html.dark :where([class~="bg-[#F8FAFC]"]),
  html.dark :where([class~="bg-brand-light"]), html.dark :where([class~="bg-[#EEF5FF]"]) {
    background-color: #ffffff !important;
  }

  html.dark, html.dark body, html.dark main { color: #111111 !important; }

  /* Revert every dark-mode text lift. `text-white` is excluded because
     it always sits on a coloured / gradient header that keeps its own
     background when printed. */
  html.dark :where([class^="text-"], [class*=" text-"])
    :not([class*="text-white"]) {
    color: #111111 !important;
  }

  html.dark :where([class^="border-"], [class*=" border-"])
    :not([class*="border-white"]):not([class*="border-transparent"]) {
    border-color: #d1d5db !important;
  }

  html.dark :where([class^="divide-"], [class*=" divide-"]) > :not(:last-child) {
    border-color: #e5e7eb !important;
  }
}

/* Icon-only hover effects for top bar header action buttons */
.header-action-btn {
  background: transparent !important;
  border: none !important;
  outline: none !important;
  box-shadow: none !important;
  transition: color 0.2s ease, transform 0.2s ease;
}

.header-action-btn:hover i {
  transform: scale(1.15);
}
