/* ElectorEra — base UI
 *
 * Surface colours are CSS custom properties rather than literals so the dark
 * theme is a variable swap instead of a parallel set of rules. Bootstrap 5.3
 * restyles its own components from `data-bs-theme` on <html>; everything
 * below is the app's own chrome.
 */

:root {
  --cs-topbar-h: 52px;
  --cs-sidebar-w: 240px;
  --cs-sidebar-w-collapsed: 56px;
  --cs-blue: #0d47a1;
  --cs-blue-dark: #093170;

  /* light theme (default) */
  --cs-bg: #f4f6f9;
  --cs-surface: #ffffff;
  --cs-border: #e3e7ee;
  --cs-border-subtle: #f0f2f6;
  --cs-text: #2b3445;
  --cs-text-muted: #6c7688;
  --cs-heading: #8a93a6;
  --cs-hover-bg: #eef3fb;
  --cs-topbar-bg: var(--cs-blue);
  --cs-accent: var(--cs-blue);
  --cs-guest-from: var(--cs-blue);
  --cs-guest-to: var(--cs-blue-dark);
  --cs-shadow: rgba(0, 0, 0, 0.25);
  --cs-card-shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --cs-chip-bg: #e7effc;
  --cs-chip-text: #0b3d91;
  --cs-focus: #2563eb;
  --cs-tour-dim: rgba(10, 15, 25, 0.55);
  --cs-radius: 10px;

  /* spacing scale (4px base) used by the app's own components */
  --cs-space-1: 0.25rem;
  --cs-space-2: 0.5rem;
  --cs-space-3: 0.75rem;
  --cs-space-4: 1rem;
  --cs-space-5: 1.5rem;
}

[data-bs-theme="dark"] {
  --cs-bg: #12151c;
  --cs-surface: #1b1f2a;
  --cs-border: #2b3140;
  --cs-border-subtle: #232836;
  --cs-text: #dfe3ea;
  --cs-text-muted: #98a1b3;
  --cs-heading: #7d879b;
  --cs-hover-bg: #252c3c;
  --cs-topbar-bg: #0f1624;
  /* The brand blue fails contrast on a dark surface; lift it. */
  --cs-accent: #6ea8fe;
  --cs-guest-from: #14203a;
  --cs-guest-to: #0b1020;
  --cs-shadow: rgba(0, 0, 0, 0.6);
  --cs-card-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
  --cs-chip-bg: #1d2b47;
  --cs-chip-text: #b9d3ff;
  --cs-focus: #8ab4ff;
  --cs-tour-dim: rgba(0, 0, 0, 0.7);
}

body.cs-body {
  background: var(--cs-bg);
  color: var(--cs-text);
  padding-top: var(--cs-topbar-h);
}

/* --- top bar --- */
.cs-topbar {
  background: var(--cs-topbar-bg);
  height: var(--cs-topbar-h);
  box-shadow: 0 1px 4px var(--cs-shadow);
  /* Bootstrap's .navbar wraps by default. With the campaign selector, search
     and the right-hand controls all present, the right cluster dropped onto a
     second row and overlapped the sidebar. Never wrap; let the middle shrink
     and long names truncate instead. */
  flex-wrap: nowrap;
  gap: 0.5rem;
}
.cs-topbar-left { flex: 0 1 auto; min-width: 0; overflow: hidden; }
.cs-topbar-right { flex: 0 0 auto; }
.cs-org-name {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.cs-campaign-select {
  max-width: 240px;
  background: var(--cs-blue-dark);
  color: #fff;
  border-color: rgba(255, 255, 255, 0.3);
}
[data-bs-theme="dark"] .cs-campaign-select { background: #1b2740; }
.cs-campaign-select option { color: #000; }
[data-bs-theme="dark"] .cs-campaign-select option { color: #fff; background: #1b2740; }
.cs-global-search { flex: 0 1 340px; min-width: 0; }
.cs-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #fff;
  color: var(--cs-blue);
  font-weight: 700;
  font-size: 0.8rem;
}
.cs-lang-select { width: 64px; }

/* --- theme switcher --- */
.cs-theme-menu .dropdown-item { display: flex; align-items: center; }
.cs-theme-menu .cs-theme-check { margin-left: auto; visibility: hidden; }
.cs-theme-menu .dropdown-item[aria-checked="true"] { font-weight: 600; }
.cs-theme-menu .dropdown-item[aria-checked="true"] .cs-theme-check { visibility: visible; }

/* --- layout --- */
.cs-layout { display: flex; min-height: calc(100vh - var(--cs-topbar-h)); }

.cs-sidebar {
  width: var(--cs-sidebar-w);
  flex: 0 0 auto;
  background: var(--cs-surface);
  border-right: 1px solid var(--cs-border);
  overflow-y: auto;
  height: calc(100vh - var(--cs-topbar-h));
  position: sticky;
  top: var(--cs-topbar-h);
  transition: width 0.15s ease;
}

/* Icon-rail mode: group headers make no sense at 56px, so hide them and show
   every link as an icon — otherwise a collapsed group would be unreachable. */
.cs-sidebar.collapsed { width: var(--cs-sidebar-w-collapsed); }
.cs-sidebar.collapsed .cs-sidebar-heading { display: none; }
.cs-sidebar.collapsed .cs-sidebar-links { display: block; }
.cs-sidebar.collapsed .cs-link span { display: none; }
.cs-sidebar.collapsed .cs-link { justify-content: center; }

.cs-sidebar-section {
  padding: 0.35rem 0.4rem;
  border-bottom: 1px solid var(--cs-border-subtle);
}

/* The group header is a <button> so it is keyboard- and screen-reader-operable;
   these rules strip the default button chrome. */
.cs-sidebar-heading {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  background: none;
  border: 0;
  text-align: left;
  cursor: pointer;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--cs-heading);
  padding: 0.45rem 0.6rem;
  border-radius: 6px;
}
.cs-sidebar-heading:hover,
.cs-sidebar-heading:focus-visible {
  background: var(--cs-hover-bg);
  color: var(--cs-accent);
}
.cs-group-icon { font-size: 0.95rem; width: 1.2rem; text-align: center; }
.cs-group-label { flex: 1 1 auto; }
.cs-group-caret { font-size: 0.75rem; transition: transform 0.15s ease; }
.cs-sidebar-heading[aria-expanded="true"] .cs-group-caret { transform: rotate(180deg); }
.cs-sidebar-heading[aria-expanded="true"] { color: var(--cs-accent); }

/* Collapsed by default: the group list is what the user opens. Rendered
   closed server-side too, so there is no open-then-close flicker on load. */
.cs-sidebar-links { display: none; padding: 0.1rem 0 0.25rem; }
.cs-sidebar-links.is-open { display: block; }

.cs-link {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.38rem 0.6rem;
  border-radius: 6px;
  color: var(--cs-text);
  text-decoration: none;
  font-size: 0.9rem;
  white-space: nowrap;
}
.cs-link:hover, .cs-link:focus { background: var(--cs-hover-bg); color: var(--cs-accent); }
.cs-link.active {
  background: var(--cs-hover-bg);
  color: var(--cs-accent);
  font-weight: 600;
}
.cs-link i { font-size: 1rem; width: 1.2rem; text-align: center; }
.text-purple { color: #6f42c1 !important; }
[data-bs-theme="dark"] .text-purple { color: #b197fc !important; }

.cs-content {
  flex: 1 1 auto;
  padding: 1.25rem 1.5rem 3rem;
  min-width: 0;
}

.cs-demo-banner { border-radius: 0; font-size: 0.85rem; letter-spacing: 0.04em; }

/* --- guest (login) pages --- */
.cs-guest {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, var(--cs-guest-from) 0%, var(--cs-guest-to) 100%);
  padding: 2rem 1rem;
}
.cs-guest .card { width: 100%; max-width: 420px; }

/* --- dashboard cards --- */
.cs-card-title {
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--cs-text-muted);
}
.cs-metric {
  background: var(--cs-surface);
  border: 1px solid var(--cs-border);
  border-radius: 8px;
  padding: 0.75rem 1rem;
  text-align: center;
}
.cs-metric .value { font-size: 1.5rem; font-weight: 700; color: var(--cs-accent); }
.cs-metric .label { font-size: 0.75rem; color: var(--cs-text-muted); }

.cs-work-link {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.35rem 0;
  color: var(--cs-text);
  text-decoration: none;
  border-bottom: 1px solid var(--cs-border-subtle);
  font-size: 0.9rem;
}
.cs-work-link:hover { color: var(--cs-accent); }

@media (max-width: 991.98px) {
  .cs-sidebar {
    position: fixed;
    z-index: 1040;
    left: 0;
    transform: translateX(-100%);
  }
  .cs-sidebar:not(.collapsed) { transform: translateX(0); }
  .cs-sidebar.collapsed { width: var(--cs-sidebar-w); transform: translateX(-100%); }
  /* Off-canvas is full width, so keep the grouped (not icon-rail) treatment. */
  .cs-sidebar.collapsed .cs-sidebar-heading { display: flex; }
  .cs-sidebar.collapsed .cs-sidebar-links { display: none; }
  .cs-sidebar.collapsed .cs-sidebar-links.is-open { display: block; }
  .cs-sidebar.collapsed .cs-link span { display: inline; }
  .cs-sidebar.collapsed .cs-link { justify-content: flex-start; }
}

/* --- page structure & typography --- */
.cs-page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--cs-space-3);
  margin-bottom: var(--cs-space-4);
}
.cs-content h1, .cs-content .h4:first-child { color: var(--cs-text); font-weight: 650; letter-spacing: -0.01em; }
.cs-page-subtitle {
  margin: var(--cs-space-1) 0 var(--cs-space-3);
  color: var(--cs-text-muted);
  font-size: 0.9rem;
  max-width: 70ch;
}
.cs-page-header .cs-page-subtitle { margin-bottom: 0; }
.cs-content .card { border-color: var(--cs-border); border-radius: var(--cs-radius); }
.cs-content .card.shadow-sm { box-shadow: var(--cs-card-shadow) !important; }
.cs-content .card-header { background: transparent; border-bottom-color: var(--cs-border-subtle); }
.cs-content .table > thead th {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--cs-text-muted);
  font-weight: 650;
}

/* Visible keyboard focus everywhere (Bootstrap's glow is faint in dark mode). */
.cs-body a:focus-visible,
.cs-body button:focus-visible,
.cs-body .btn:focus-visible,
.cs-body .cs-link:focus-visible,
.cs-body .form-check-input:focus-visible {
  outline: 2px solid var(--cs-focus);
  outline-offset: 2px;
}

/* --- Bootstrap's yellow and cyan text on a light surface is about 1.6:1 —
   unreadable. Use the theme-aware emphasis shades (dark amber in light mode,
   pale yellow in dark mode) for text and outline buttons. --- */
.text-warning { color: var(--bs-warning-text-emphasis) !important; }
.text-info { color: var(--bs-info-text-emphasis) !important; }
.btn-outline-warning { --bs-btn-color: var(--bs-warning-text-emphasis); }
.btn-outline-info { --bs-btn-color: var(--bs-info-text-emphasis); }

/* --- Bootstrap keeps .bg-light / .bg-white / .table-light light in dark
   mode while body text turns light: light-on-light. Follow the theme. --- */
[data-bs-theme="dark"] .bg-light,
[data-bs-theme="dark"] .bg-white { background-color: var(--bs-tertiary-bg) !important; }
[data-bs-theme="dark"] .table-light {
  --bs-table-bg: var(--bs-tertiary-bg);
  --bs-table-color: var(--bs-body-color);
  --bs-table-border-color: var(--bs-border-color);
}

/* Contextual table rows (table-primary, table-warning, …) are fixed light
   colours in Bootstrap 5.3; in dark mode muted text on them was invisible. */
[data-bs-theme="dark"] .table-primary { --bs-table-bg: var(--bs-primary-bg-subtle); --bs-table-color: var(--bs-body-color); }
[data-bs-theme="dark"] .table-success { --bs-table-bg: var(--bs-success-bg-subtle); --bs-table-color: var(--bs-body-color); }
[data-bs-theme="dark"] .table-warning { --bs-table-bg: var(--bs-warning-bg-subtle); --bs-table-color: var(--bs-body-color); }
[data-bs-theme="dark"] .table-danger { --bs-table-bg: var(--bs-danger-bg-subtle); --bs-table-color: var(--bs-body-color); }
[data-bs-theme="dark"] .table-info { --bs-table-bg: var(--bs-info-bg-subtle); --bs-table-color: var(--bs-body-color); }
[data-bs-theme="dark"] .table-secondary { --bs-table-bg: var(--bs-secondary-bg-subtle); --bs-table-color: var(--bs-body-color); }

/* --- phones: nothing may push the page sideways. Page headers and tool
   rows wrap, and a table not already in .table-responsive scrolls inside
   its own box instead of widening the page. --- */
@media (max-width: 575.98px) {
  .cs-content .d-flex.justify-content-between,
  .cs-content .d-flex.gap-1,
  .cs-content .d-flex.gap-2 { flex-wrap: wrap; row-gap: var(--cs-space-2); }
  .cs-content :not(.table-responsive) > table.table {
    display: block; max-width: 100%; overflow-x: auto;
  }
}

/* --- tooltips never block a click: a tip left showing (hover/focus) over
   the next button used to swallow the click meant for that button --- */
.tooltip { pointer-events: none; }

/* --- friendly empty states --- */
.cs-empty { text-align: center; padding: var(--cs-space-5) var(--cs-space-4); }
.cs-empty-icon { display: block; font-size: 2rem; color: var(--cs-accent); opacity: 0.8; margin-bottom: var(--cs-space-2); }
.cs-empty-title { font-weight: 650; margin-bottom: var(--cs-space-1); color: var(--cs-text); }
.cs-empty-text { color: var(--cs-text-muted); font-size: 0.9rem; margin: 0 auto var(--cs-space-3); max-width: 46ch; }

/* --- top-bar guide & tips controls --- */
.cs-guide-btn { font-weight: 600; color: var(--cs-blue); }
[data-bs-theme="dark"] .cs-guide-btn { color: #0f1624; }
.cs-help-toggle.is-off { opacity: 0.7; }
.cs-help-toggle .bi-lightbulb-fill { color: #ffd54f; }

/* Inline "?" hints sit next to labels; they disappear with help mode off. */
.cs-hint {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.15rem;
  height: 1.15rem;
  margin-left: var(--cs-space-1);
  padding: 0;
  border: 1px solid var(--cs-border);
  border-radius: 50%;
  background: var(--cs-surface);
  color: var(--cs-accent);
  font-size: 0.7rem;
  font-weight: 700;
  line-height: 1;
  vertical-align: middle;
  text-transform: none;
  letter-spacing: 0;
  cursor: help;
}
[data-cs-help="off"] .cs-hint { display: none; }

/* --- the Guide panel --- */
.cs-guide { --bs-offcanvas-width: 380px; background: var(--cs-surface); color: var(--cs-text); }
.cs-guide-section { padding-bottom: var(--cs-space-4); margin-bottom: var(--cs-space-4); border-bottom: 1px solid var(--cs-border-subtle); }
.cs-guide-section:last-child { border-bottom: 0; }
.cs-guide-group { margin-bottom: var(--cs-space-3); }
.cs-guide-group-title {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--cs-heading);
  margin: var(--cs-space-2) 0 var(--cs-space-1);
}
.cs-guide-link {
  display: flex;
  gap: var(--cs-space-2);
  padding: var(--cs-space-2);
  border-radius: 8px;
  color: var(--cs-text);
  text-decoration: none;
}
.cs-guide-link:hover, .cs-guide-link:focus-visible { background: var(--cs-hover-bg); color: var(--cs-accent); }
.cs-guide-link i { font-size: 1.05rem; width: 1.3rem; text-align: center; padding-top: 0.1rem; }
.cs-guide-link strong { display: block; font-size: 0.9rem; font-weight: 600; }
.cs-guide-link small { display: block; color: var(--cs-text-muted); line-height: 1.35; }

/* --- guided tour --- */
.cs-tour { position: fixed; inset: 0; z-index: 1090; }
/* The spotlight is a transparent box whose huge shadow dims everything else. */
.cs-tour-spot {
  position: fixed;
  border-radius: 8px;
  box-shadow: 0 0 0 9999px var(--cs-tour-dim), 0 0 0 3px var(--cs-focus);
  transition: top 0.2s ease, left 0.2s ease, width 0.2s ease, height 0.2s ease;
  pointer-events: none;
}
.cs-tour-card {
  position: fixed;
  width: min(360px, calc(100vw - 32px));
  background: var(--cs-surface);
  color: var(--cs-text);
  border: 1px solid var(--cs-border);
  border-radius: var(--cs-radius);
  box-shadow: 0 10px 30px var(--cs-shadow);
  padding: var(--cs-space-4);
  transition: top 0.2s ease, left 0.2s ease;
}
.cs-tour-card.is-docked {
  left: 16px !important;
  right: 16px;
  bottom: 16px;
  top: auto !important;
  width: auto;
}
.cs-tour-counter { font-size: 0.75rem; color: var(--cs-text-muted); margin-bottom: var(--cs-space-1); }
.cs-tour-title { font-size: 1.05rem; font-weight: 650; margin-bottom: var(--cs-space-2); }
.cs-tour-text { font-size: 0.9rem; margin-bottom: var(--cs-space-3); }
.cs-tour-actions { display: flex; align-items: center; gap: var(--cs-space-2); }
.cs-tour-skip { margin-right: auto; padding-left: 0; }
.cs-tour-open { overflow-x: hidden; }

.cs-tour-prompt {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 1060;
  width: min(300px, calc(100vw - 32px));
  background: var(--cs-surface);
  color: var(--cs-text);
  border: 1px solid var(--cs-border);
  border-left: 4px solid var(--cs-accent);
  border-radius: var(--cs-radius);
  box-shadow: 0 8px 24px var(--cs-shadow);
  padding: var(--cs-space-3) var(--cs-space-4);
}
.cs-tour-prompt[hidden] { display: none; }
.cs-tour-prompt-close { position: absolute; top: 10px; right: 10px; font-size: 0.65rem; }

/* --- filter chips --- */
.cs-chips { display: flex; flex-wrap: wrap; gap: var(--cs-space-2); }
.cs-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.2rem 0.65rem;
  border-radius: 999px;
  background: var(--cs-chip-bg);
  color: var(--cs-chip-text);
  font-size: 0.82rem;
  text-decoration: none;
  border: 1px solid transparent;
}
.cs-chip:hover, .cs-chip:focus-visible { border-color: var(--cs-chip-text); color: var(--cs-chip-text); }
.cs-chip-label { font-weight: 600; }
.cs-chip .bi-x-lg { font-size: 0.7rem; }

/* --- Data Explorer --- */
.cs-explorer-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--cs-space-2);
}
.cs-explorer-total { font-size: 1.35rem; font-weight: 700; color: var(--cs-accent); }
.cs-explorer-scroll { max-height: 70vh; }
.cs-explorer-table { font-size: 0.82rem; }
.cs-explorer-table th, .cs-explorer-table td { white-space: nowrap; }
.cs-explorer-table thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--cs-surface);
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.78rem;
}
.cs-explorer-table thead tr + tr th { top: 1.9rem; }
.cs-explorer-groups th {
  color: var(--cs-accent) !important;
  border-bottom: 2px solid var(--cs-accent);
  text-transform: uppercase !important;
  font-size: 0.7rem !important;
}
.cs-explorer-table .cs-sticky-col {
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--cs-surface);
  border-right: 1px solid var(--cs-border);
  max-width: 14rem;
  overflow: hidden;
  text-overflow: ellipsis;
}
.cs-explorer-table thead .cs-sticky-col { z-index: 3; }
.cs-sort { color: inherit; text-decoration: none; }
.cs-sort.is-active { color: var(--cs-accent); }
.cs-null { color: var(--cs-text-muted); opacity: 0.6; }
.cs-cell-long { display: inline-block; max-width: 22rem; white-space: normal; }
.cs-cell-num, .cs-cell-score, .cs-cell-pct { text-align: right; font-variant-numeric: tabular-nums; }

.cs-count-list { list-style: none; margin: 0; padding: 0; columns: 2 18rem; column-gap: var(--cs-space-5); }
.cs-count-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 6rem auto;
  align-items: center;
  gap: var(--cs-space-2);
  padding: 0.2rem var(--cs-space-2);
  border-radius: 6px;
  color: var(--cs-text);
  text-decoration: none;
  font-size: 0.85rem;
  break-inside: avoid;
}
.cs-count-row:hover, .cs-count-row:focus-visible { background: var(--cs-hover-bg); color: var(--cs-accent); }
.cs-count-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cs-count-bar { height: 0.45rem; background: var(--cs-border-subtle); border-radius: 999px; overflow: hidden; }
.cs-count-bar > span { display: block; height: 100%; background: var(--cs-accent); border-radius: 999px; }
.cs-count-n { font-variant-numeric: tabular-nums; font-weight: 600; text-align: right; }

/* --- dashboard KPI cards that open a filtered view --- */
a.cs-metric { display: block; text-decoration: none; transition: border-color 0.15s ease, transform 0.15s ease; }
a.cs-metric:hover, a.cs-metric:focus-visible { border-color: var(--cs-accent); transform: translateY(-1px); }
.cs-metric .cs-metric-go { font-size: 0.7rem; color: var(--cs-text-muted); }

@media (max-width: 575.98px) {
  .cs-content { padding: 1rem 16px 3rem; }
  .cs-page-header { margin-bottom: var(--cs-space-3); }
  .cs-topbar .btn { padding-left: 0.4rem; padding-right: 0.4rem; }
  .cs-guide { --bs-offcanvas-width: 100vw; }
  .cs-explorer-table .cs-sticky-col { max-width: 9rem; }
}

@media (prefers-reduced-motion: reduce) {
  .cs-sidebar, .cs-group-caret { transition: none; }
  .cs-tour-spot, .cs-tour-card, a.cs-metric { transition: none; }
  a.cs-metric:hover { transform: none; }
}

/* "More filters" disclosure in the Data Explorer */
.cs-more-filters > summary {
  display: inline-block;
  cursor: pointer;
  color: var(--cs-accent);
  font-size: 0.85rem;
  font-weight: 600;
}
.cs-more-filters > summary::marker { color: var(--cs-accent); }
