/* ============================================================
   Industrial Compliance Explorer — Stylesheet
   v2.1 — 2026-03-23
   Environmental compliance dashboard with ECHO integration
   and Routine/Consequential activity classification
   Cool slate/steel blue palette, dark mode default
   ============================================================ */

/* --- Reset & Base --- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  -moz-text-size-adjust: none;
  -webkit-text-size-adjust: none;
  text-size-adjust: none;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

/* --- Design Tokens --- */
:root {
  /* Typography */
  --font-body: 'Inter', 'Helvetica Neue', Arial, sans-serif;
  --font-mono: 'JetBrains Mono', 'Consolas', monospace;

  --text-xs:   0.75rem;    /* 12px */
  --text-sm:   0.8125rem;  /* 13px */
  --text-base: 0.875rem;   /* 14px */
  --text-lg:   1rem;       /* 16px */
  --text-xl:   1.25rem;    /* 20px */
  --text-2xl:  1.5rem;     /* 24px */

  /* Spacing */
  --space-1:  0.25rem;
  --space-2:  0.5rem;
  --space-3:  0.75rem;
  --space-4:  1rem;
  --space-5:  1.25rem;
  --space-6:  1.5rem;
  --space-8:  2rem;

  /* Radius */
  --radius-sm: 4px;
  --radius-md: 6px;
  --radius-lg: 8px;

  /* Sidebar */
  --sidebar-width: 264px;

  /* Transitions */
  --transition-fast: 150ms ease;
}

/* --- Light Mode --- */
:root, [data-theme="light"] {
  --color-bg: #f1f5f9;
  --color-surface: #ffffff;
  --color-surface-2: #f8fafc;
  --color-sidebar-bg: #1e293b;
  --color-sidebar-text: #cbd5e1;
  --color-sidebar-text-muted: #64748b;
  --color-sidebar-heading: #e2e8f0;
  --color-sidebar-accent: #38bdf8;
  --color-sidebar-hover: #334155;
  --color-sidebar-input-bg: #334155;
  --color-sidebar-border: #334155;

  --color-header-bg: #0f172a;
  --color-header-text: #f1f5f9;

  --color-text: #0f172a;
  --color-text-secondary: #475569;
  --color-text-muted: #94a3b8;
  --color-border: #e2e8f0;
  --color-divider: #e2e8f0;

  --color-primary: #3b82f6;
  --color-primary-hover: #2563eb;
  --color-primary-subtle: #eff6ff;

  --color-success: #16a34a;
  --color-success-bg: #f0fdf4;
  --color-success-border: #bbf7d0;
  --color-warning: #dc2626;
  --color-warning-bg: #fef2f2;
  --color-warning-border: #fecaca;
  --color-amber: #d97706;
  --color-amber-bg: #fffbeb;
  --color-amber-border: #fde68a;

  --color-kpi-bg: #ffffff;
  --color-kpi-border: #e2e8f0;
  --color-kpi-value: #0f172a;
  --color-kpi-label: #64748b;

  --color-table-header-bg: #e2e8f0;
  --color-table-header-text: #475569;
  --color-table-row-hover: #e2e8f0;
  --color-table-row-alt: #f1f5f9;
  --color-table-text: #1e293b;

  /* Water accent */
  --color-water: #0369a1;
  --color-water-bg: #f0f9ff;
  --color-water-border: #bae6fd;

  /* ECHO status badge tokens */
  --echo-good-bg: #f0fdf4;
  --echo-good-text: #16a34a;
  --echo-good-border: #bbf7d0;
  --echo-warning-bg: #fffbeb;
  --echo-warning-text: #d97706;
  --echo-warning-border: #fde68a;
  --echo-critical-bg: #fef2f2;
  --echo-critical-text: #dc2626;
  --echo-critical-border: #fecaca;
  --echo-neutral-bg: #f1f5f9;
  --echo-neutral-text: #64748b;
  --echo-neutral-border: #e2e8f0;

  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
}

/* --- Dark Mode --- */
[data-theme="dark"] {
  --color-bg: #0f172a;
  --color-surface: #1e293b;
  --color-surface-2: #1e293b;
  --color-sidebar-bg: #0b1120;
  --color-sidebar-text: #cbd5e1;
  --color-sidebar-text-muted: #64748b;
  --color-sidebar-heading: #e2e8f0;
  --color-sidebar-accent: #38bdf8;
  --color-sidebar-hover: #1e293b;
  --color-sidebar-input-bg: #1e293b;
  --color-sidebar-border: #1e293b;

  --color-header-bg: #0b1120;
  --color-header-text: #f1f5f9;

  --color-text: #e2e8f0;
  --color-text-secondary: #94a3b8;
  --color-text-muted: #64748b;
  --color-border: #334155;
  --color-divider: #1e293b;

  --color-primary: #60a5fa;
  --color-primary-hover: #3b82f6;
  --color-primary-subtle: rgba(59,130,246,0.1);

  --color-success: #4ade80;
  --color-success-bg: rgba(74,222,128,0.1);
  --color-success-border: rgba(74,222,128,0.2);
  --color-warning: #f87171;
  --color-warning-bg: rgba(248,113,113,0.1);
  --color-warning-border: rgba(248,113,113,0.2);
  --color-amber: #fbbf24;
  --color-amber-bg: rgba(251,191,36,0.1);
  --color-amber-border: rgba(251,191,36,0.25);

  --color-kpi-bg: #1e293b;
  --color-kpi-border: #334155;
  --color-kpi-value: #f1f5f9;
  --color-kpi-label: #94a3b8;

  --color-table-header-bg: #1a2536;
  --color-table-header-text: #94a3b8;
  --color-table-row-hover: rgba(59,130,246,0.06);
  --color-table-row-alt: rgba(255,255,255,0.02);
  --color-table-text: #cbd5e1;

  /* Water accent */
  --color-water: #38bdf8;
  --color-water-bg: rgba(56,189,248,0.08);
  --color-water-border: rgba(56,189,248,0.2);

  /* ECHO status badge tokens */
  --echo-good-bg: rgba(74,222,128,0.1);
  --echo-good-text: #4ade80;
  --echo-good-border: rgba(74,222,128,0.2);
  --echo-warning-bg: rgba(251,191,36,0.1);
  --echo-warning-text: #fbbf24;
  --echo-warning-border: rgba(251,191,36,0.25);
  --echo-critical-bg: rgba(248,113,113,0.1);
  --echo-critical-text: #f87171;
  --echo-critical-border: rgba(248,113,113,0.2);
  --echo-neutral-bg: rgba(100,116,139,0.1);
  --echo-neutral-text: #94a3b8;
  --echo-neutral-border: rgba(100,116,139,0.2);

  --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
  --shadow-md: 0 1px 3px rgba(0,0,0,0.4), 0 1px 2px rgba(0,0,0,0.3);
}

/* --- Layout Shell --- */
body {
  font-family: var(--font-body);
  font-size: var(--text-base);
  color: var(--color-text);
  background: var(--color-bg);
  line-height: 1.5;
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.app-layout {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* --- Header --- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 48px;
  padding: 0 var(--space-5);
  background: var(--color-header-bg);
  color: var(--color-header-text);
  border-bottom: 1px solid rgba(255,255,255,0.06);
  flex-shrink: 0;
  z-index: 100;
}

.app-logo {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.app-logo svg {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
}

.app-logo-text {
  font-size: var(--text-lg);
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--color-header-text);
}

.app-logo-text span {
  color: var(--color-primary);
  font-weight: 700;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.theme-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  cursor: pointer;
  border: none;
  background: none;
  transition: color var(--transition-fast), background var(--transition-fast);
}

.theme-toggle:hover {
  color: var(--color-header-text);
  background: rgba(255,255,255,0.08);
}

/* --- Sidebar --- */
.sidebar {
  width: var(--sidebar-width);
  background: var(--color-sidebar-bg);
  border-right: 1px solid rgba(255,255,255,0.06);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-4) var(--space-4) var(--space-3);
  border-bottom: 1px solid var(--color-sidebar-border);
}

.sidebar-title {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-sidebar-heading);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.filter-count-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--color-primary);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  line-height: 1;
}

.filter-count-badge.hidden { display: none; }

.clear-all-btn {
  font-size: var(--text-xs);
  color: var(--color-sidebar-accent);
  cursor: pointer;
  border: none;
  background: none;
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-sm);
  transition: background var(--transition-fast);
}

.clear-all-btn:hover {
  background: var(--color-sidebar-hover);
}

.clear-all-btn.hidden { display: none; }

.sidebar-scroll {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-3) var(--space-4);
}

.sidebar-scroll::-webkit-scrollbar { width: 5px; }
.sidebar-scroll::-webkit-scrollbar-track { background: transparent; }
.sidebar-scroll::-webkit-scrollbar-thumb { background: var(--color-sidebar-border); border-radius: 3px; }

/* --- Filter Groups --- */
.filter-group {
  margin-bottom: var(--space-4);
}

.filter-group-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: var(--space-2) 0;
  border: none;
  background: none;
  color: var(--color-sidebar-heading);
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
  text-align: left;
}

.filter-group-toggle .chevron {
  width: 14px;
  height: 14px;
  color: var(--color-sidebar-text-muted);
  transition: transform var(--transition-fast);
}

.filter-group.collapsed .chevron {
  transform: rotate(-90deg);
}

.filter-group.collapsed .filter-group-content {
  display: none;
}

.filter-group-content {
  padding-top: var(--space-2);
}

.filter-search {
  width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--color-sidebar-border);
  border-radius: var(--radius-sm);
  background: var(--color-sidebar-input-bg);
  color: var(--color-sidebar-text);
  font-size: var(--text-xs);
  margin-bottom: var(--space-2);
  outline: none;
  transition: border-color var(--transition-fast);
}

.filter-search::placeholder {
  color: var(--color-sidebar-text-muted);
}

.filter-search:focus {
  border-color: var(--color-primary);
}

.filter-options {
  max-height: 180px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.filter-options::-webkit-scrollbar { width: 4px; }
.filter-options::-webkit-scrollbar-track { background: transparent; }
.filter-options::-webkit-scrollbar-thumb { background: var(--color-sidebar-border); border-radius: 2px; }

.filter-option {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: 4px 6px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: var(--text-xs);
  color: var(--color-sidebar-text);
  transition: background var(--transition-fast);
  line-height: 1.4;
}

.filter-option:hover {
  background: var(--color-sidebar-hover);
}

.filter-option input[type="checkbox"] {
  flex-shrink: 0;
  width: 14px;
  height: 14px;
  accent-color: var(--color-primary);
  cursor: pointer;
}

.filter-option-label {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.filter-option-count {
  flex-shrink: 0;
  font-size: 11px;
  color: var(--color-sidebar-text-muted);
  font-variant-numeric: tabular-nums;
}

/* Cascading filter: dim options with zero matches */
.filter-option-disabled {
  opacity: 0.3;
  pointer-events: none;
  order: 1; /* push to bottom of list */
}

/* --- Main Content Area --- */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* --- KPI Cards --- */
.kpi-bar {
  display: flex;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
  flex-shrink: 0;
  overflow-x: auto;
}

.kpi-card {
  flex: 1;
  min-width: 150px;
  padding: var(--space-3) var(--space-4);
  background: var(--color-kpi-bg);
  border: 1px solid var(--color-kpi-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}

/* Water-emphasis KPI cards */
.kpi-card.kpi-water {
  border-color: var(--color-water-border);
  background: var(--color-water-bg);
}

.kpi-label {
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--color-kpi-label);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 2px;
}

.kpi-card.kpi-water .kpi-label {
  color: var(--color-water);
}

.kpi-value {
  font-size: var(--text-xl);
  font-weight: 700;
  color: var(--color-kpi-value);
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
}

.kpi-value.currency::before {
  content: '$';
}

.kpi-value-sm {
  font-size: var(--text-base);
  font-weight: 600;
}

/* --- Table Area --- */
.table-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-5) var(--space-3);
  flex-shrink: 0;
  flex-wrap: wrap;
  gap: var(--space-2);
}

.table-toolbar-left {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  flex-wrap: wrap;
}

.table-row-count {
  font-size: var(--text-sm);
  color: var(--color-text-secondary);
}

.table-row-count strong {
  color: var(--color-text);
  font-weight: 600;
}

.table-search {
  padding: 6px 10px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  color: var(--color-text);
  font-size: var(--text-sm);
  width: 240px;
  outline: none;
  transition: border-color var(--transition-fast);
}

.table-search:focus {
  border-color: var(--color-primary);
}

.table-container {
  flex: 1;
  overflow: auto;
  padding: 0 var(--space-5) var(--space-4);
}

.table-container::-webkit-scrollbar { width: 8px; height: 8px; }
.table-container::-webkit-scrollbar-track { background: transparent; }
.table-container::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 4px; }
.table-container::-webkit-scrollbar-corner { background: transparent; }

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
  table-layout: auto;
}

.data-table thead {
  position: sticky;
  top: 0;
  z-index: 10;
}

.data-table th {
  background: var(--color-table-header-bg);
  color: var(--color-table-header-text);
  font-weight: 600;
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 10px 12px;
  text-align: left;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  border-bottom: 2px solid var(--color-border);
  position: sticky;
  top: 0;
  z-index: 10;
  transition: color var(--transition-fast);
}

.data-table th:hover {
  color: var(--color-text);
}

.data-table th.sort-asc::after,
.data-table th.sort-desc::after {
  display: inline-block;
  margin-left: 4px;
  font-size: 10px;
}

.data-table th.sort-asc::after { content: '▲'; }
.data-table th.sort-desc::after { content: '▼'; }

.data-table th.num-col {
  text-align: right;
}

.data-table td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--color-divider);
  color: var(--color-table-text);
  vertical-align: middle;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.data-table td.num-col {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.data-table tbody tr:nth-child(even) {
  background: var(--color-table-row-alt);
}

.data-table tbody tr:hover {
  background: var(--color-table-row-hover);
}

/* Water-emphasis columns */
.col-water {
  background: var(--color-water-bg);
}

.data-table thead th.col-water {
  background: var(--color-water-bg);
  color: var(--color-water);
  border-bottom-color: var(--color-water-border);
}

/* Truncated text cells */
.cell-truncate {
  max-width: 200px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Facility name - slightly wider */
.cell-facility {
  max-width: 220px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 500;
}

/* --- Badges --- */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  line-height: 1.4;
  white-space: nowrap;
}

.badge-yes {
  background: var(--color-warning-bg);
  color: var(--color-warning);
  border: 1px solid var(--color-warning-border);
}

.badge-no {
  background: var(--color-success-bg);
  color: var(--color-success);
  border: 1px solid var(--color-success-border);
}

/* ============================================================
   ECHO Status Badges — multi-level compliance status
   ============================================================ */
.echo-badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  line-height: 1.4;
  white-space: nowrap;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.echo-badge-good {
  background: var(--echo-good-bg);
  color: var(--echo-good-text);
  border: 1px solid var(--echo-good-border);
}

.echo-badge-warning {
  background: var(--echo-warning-bg);
  color: var(--echo-warning-text);
  border: 1px solid var(--echo-warning-border);
}

.echo-badge-critical {
  background: var(--echo-critical-bg);
  color: var(--echo-critical-text);
  border: 1px solid var(--echo-critical-border);
}

.echo-badge-neutral {
  background: var(--echo-neutral-bg);
  color: var(--echo-neutral-text);
  border: 1px solid var(--echo-neutral-border);
}

.echo-badge-none {
  background: var(--echo-neutral-bg);
  color: var(--echo-neutral-text);
  border: 1px solid var(--echo-neutral-border);
}

/* ============================================================
   ECHO Compliance Summary Section (Site Detail Page)
   ============================================================ */
.echo-section {
  border-color: var(--color-water-border);
}

.echo-section .section-header {
  background: var(--color-water-bg);
}

.echo-section .section-title {
  color: var(--color-water);
}

.echo-summary-body {
  padding: var(--space-4) var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
}

/* Overview row — key cross-program stats */
.echo-overview-row {
  display: flex;
  gap: var(--space-5);
  flex-wrap: wrap;
  padding-bottom: var(--space-4);
  border-bottom: 1px solid var(--color-border);
}

.echo-overview-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 120px;
}

.echo-overview-label {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.echo-overview-value {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-text);
}

/* Statute comparison cards */
.echo-cards-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-4);
}

.echo-card {
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

/* Water-emphasis card (CWA) */
.echo-card.echo-card-water {
  border-color: var(--color-water-border);
  background: var(--color-water-bg);
  box-shadow: 0 0 0 1px var(--color-water-border);
}

.echo-card.echo-card-inactive {
  opacity: 0.5;
}

.echo-card-header {
  display: flex;
  align-items: baseline;
  gap: var(--space-2);
}

.echo-card-label {
  font-size: var(--text-lg);
  font-weight: 700;
  color: var(--color-text);
}

.echo-card-water .echo-card-label {
  color: var(--color-water);
}

.echo-card-full-label {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

.echo-card-status {
  margin: var(--space-1) 0;
}

.echo-card-metrics {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-2);
}

.echo-metric {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.echo-metric-val {
  font-size: var(--text-base);
  font-weight: 700;
  color: var(--color-text);
  font-variant-numeric: tabular-nums;
}

.echo-metric-label {
  font-size: 10px;
  font-weight: 500;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.echo-card-footer {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-4);
  padding-top: var(--space-2);
  border-top: 1px solid var(--color-border);
}

.echo-card-detail {
  font-size: var(--text-xs);
  color: var(--color-text-secondary);
}

/* Year-by-year trend section */
.echo-trend-section {
  padding-top: var(--space-3);
}

.echo-trend-title {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: var(--space-3);
}

/* --- Loading State --- */
.loading-overlay {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  gap: var(--space-4);
  color: var(--color-text-muted);
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading-text {
  font-size: var(--text-sm);
  font-weight: 500;
}

/* --- Footer --- */
.app-footer {
  padding: var(--space-2) var(--space-5);
  text-align: center;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  border-top: 1px solid var(--color-border);
  flex-shrink: 0;
  background: var(--color-surface);
}

.app-footer a {
  color: var(--color-text-muted);
  text-decoration: none;
  transition: color var(--transition-fast);
}

.app-footer a:hover {
  color: var(--color-primary);
}

/* --- Responsive: sidebar collapse --- */
.sidebar-toggle-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  color: var(--color-header-text);
  cursor: pointer;
  border: none;
  background: none;
}

@media (max-width: 900px) {
  .sidebar-toggle-btn {
    display: flex;
  }

  .sidebar {
    position: fixed;
    top: 48px;
    left: 0;
    bottom: 0;
    z-index: 200;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-overlay {
    display: none;
    position: fixed;
    inset: 48px 0 0 0;
    background: rgba(0,0,0,0.5);
    z-index: 199;
  }

  .sidebar-overlay.open {
    display: block;
  }
}

/* --- Utility --- */
.hidden { display: none !important; }

::selection {
  background: rgba(59,130,246,0.25);
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* ============================================================
   Site Detail Page Styles
   ============================================================ */

/* --- Body override for scrollable detail page --- */
.site-detail-body {
  overflow: auto;
  height: auto;
  min-height: 100vh;
}

/* --- Back arrow link in header --- */
.back-to-explorer {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  text-decoration: none;
  transition: color var(--transition-fast), background var(--transition-fast);
}

.back-to-explorer:hover {
  color: var(--color-header-text);
  background: rgba(255,255,255,0.08);
}

/* --- Breadcrumb in header --- */
.breadcrumb-sep {
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  margin: 0 2px;
}

.breadcrumb-current {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--color-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 340px;
}

/* --- Scrollable main area for detail page --- */
.site-detail-main {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-5) var(--space-6);
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}

/* --- Site Content Wrapper --- */
.site-content {
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
}

/* --- Error State --- */
.site-error-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-8) 0;
  gap: var(--space-4);
  color: var(--color-text-muted);
  text-align: center;
}

.site-error-icon { color: var(--color-warning); }
.site-error-title { font-size: var(--text-xl); font-weight: 700; color: var(--color-text); }
.site-error-msg { font-size: var(--text-base); color: var(--color-text-secondary); }

.site-error-link {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  margin-top: var(--space-2);
  padding: var(--space-2) var(--space-4);
  background: var(--color-primary);
  color: #fff;
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  font-weight: 600;
  text-decoration: none;
  transition: background var(--transition-fast);
}

.site-error-link:hover {
  background: var(--color-primary-hover);
}

/* --- Facility Identity Card --- */
.identity-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-5) var(--space-6);
  box-shadow: var(--shadow-md);
}

.identity-header {
  display: flex;
  align-items: baseline;
  gap: var(--space-4);
  margin-bottom: var(--space-4);
  flex-wrap: wrap;
}

.identity-name {
  font-size: var(--text-xl);
  font-weight: 700;
  color: var(--color-text);
  line-height: 1.3;
}

.identity-frs {
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  background: var(--color-surface-2);
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
}

.identity-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: var(--space-3) var(--space-5);
}

.identity-field {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.identity-label {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.identity-value {
  font-size: var(--text-sm);
  color: var(--color-text);
  font-weight: 500;
}

/* --- Site KPI Bar --- */
.site-kpi-bar {
  display: flex;
  gap: var(--space-3);
  flex-wrap: wrap;
}

.site-kpi-bar .kpi-card {
  flex: 1;
  min-width: 130px;
}

/* --- Collapsible Sections --- */
.site-section {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: var(--space-3) var(--space-5);
  border: none;
  background: none;
  cursor: pointer;
  color: var(--color-text);
  text-align: left;
  transition: background var(--transition-fast);
}

.section-header:hover {
  background: var(--color-table-row-hover);
}

.section-header-left {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.section-chevron {
  flex-shrink: 0;
  color: var(--color-text-muted);
  transition: transform var(--transition-fast);
}

.site-section.collapsed .section-chevron {
  transform: rotate(-90deg);
}

.site-section.collapsed .section-body {
  display: none;
}

.section-title {
  font-size: var(--text-sm);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-text);
}

.section-count-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 20px;
  padding: 0 7px;
  border-radius: 10px;
  background: var(--color-primary-subtle);
  color: var(--color-primary);
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
}

.section-body {
  border-top: 1px solid var(--color-border);
}

.section-empty {
  padding: var(--space-5);
  text-align: center;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  font-style: italic;
}

/* --- Section table wrapper --- */
.section-table-wrap {
  overflow-x: auto;
}

.section-table-wrap::-webkit-scrollbar { height: 6px; }
.section-table-wrap::-webkit-scrollbar-track { background: transparent; }
.section-table-wrap::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 3px; }

/* --- Site detail tables (reuse data-table) --- */
.site-table {
  font-size: var(--text-xs);
}

.site-table th {
  font-size: 11px;
  padding: 8px 10px;
  cursor: default;
}

.site-table td {
  padding: 6px 10px;
  font-size: var(--text-xs);
}

.site-table code {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--color-primary);
}

/* --- Cell helpers --- */
.cell-truncate-wide {
  display: inline-block;
  max-width: 280px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  vertical-align: middle;
}

/* --- Exceedance highlight --- */
.exceedance-high {
  color: var(--color-warning);
  font-weight: 700;
}

/* --- Penalty highlight --- */
.penalty-highlight {
  color: var(--color-warning);
  font-weight: 600;
}

/* --- Mini badge for flags --- */
.mini-badge {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 8px;
  font-size: 10px;
  font-weight: 600;
  background: var(--color-primary-subtle);
  color: var(--color-primary);
  margin-right: 2px;
}

/* Water-emphasis mini badge */
.mini-badge-water {
  background: var(--color-water-bg);
  color: var(--color-water);
  border: 1px solid var(--color-water-border);
}

/* --- Text color utilities --- */
.text-muted { color: var(--color-text-muted); }
.text-warning { color: var(--color-warning); font-weight: 600; }
.text-success { color: var(--color-success); font-weight: 500; }

/* --- Table links --- */
.table-link {
  color: var(--color-primary);
  text-decoration: none;
  font-weight: 500;
  font-size: var(--text-xs);
  transition: color var(--transition-fast);
}

.table-link:hover {
  color: var(--color-primary-hover);
  text-decoration: underline;
}

/* --- Facility name link in explorer table --- */
.facility-link {
  color: var(--color-primary);
  text-decoration: none;
  font-weight: 500;
  transition: color var(--transition-fast);
}

.facility-link:hover {
  color: var(--color-primary-hover);
  text-decoration: underline;
}

/* ============================================================
   Activity Classification Toggle & Badges
   ============================================================ */
.activity-toggle {
  display: inline-flex;
  border-radius: var(--radius-md);
  border: 1px solid var(--color-border);
  overflow: hidden;
}

.activity-btn {
  padding: 4px 12px;
  border: none;
  background: transparent;
  color: var(--color-text-secondary);
  font-size: var(--text-xs);
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition-fast), color var(--transition-fast);
  white-space: nowrap;
}

.activity-btn:not(:last-child) {
  border-right: 1px solid var(--color-border);
}

.activity-btn:hover {
  background: var(--color-table-row-hover);
}

.activity-btn.active {
  background: var(--color-primary);
  color: #fff;
}

/* Activity badge (R/C inline indicator) */
.activity-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  font-size: 10px;
  font-weight: 700;
  line-height: 1;
  flex-shrink: 0;
}

.activity-badge-cons {
  background: var(--echo-warning-bg);
  color: var(--echo-warning-text);
  border: 1px solid var(--echo-warning-border);
}

.activity-badge-routine {
  background: var(--echo-neutral-bg);
  color: var(--echo-neutral-text);
  border: 1px solid var(--echo-neutral-border);
}

/* Consequential count highlight in explorer table */
.cons-highlight {
  color: var(--echo-warning-text);
  font-weight: 700;
}

/* Site detail page activity bar */
.site-activity-bar {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) 0;
}

.site-activity-label {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* --- Responsive detail page --- */
@media (max-width: 768px) {
  .site-detail-main {
    padding: var(--space-3) var(--space-3);
  }

  .identity-card {
    padding: var(--space-3) var(--space-4);
  }

  .identity-grid {
    grid-template-columns: 1fr 1fr;
  }

  .site-kpi-bar {
    gap: var(--space-2);
  }

  .site-kpi-bar .kpi-card {
    min-width: 100px;
    padding: var(--space-2) var(--space-3);
  }

  .breadcrumb-current {
    max-width: 160px;
  }

  /* ECHO cards stack on mobile */
  .echo-cards-row {
    grid-template-columns: 1fr;
  }

  .echo-summary-body {
    padding: var(--space-3);
  }

  .echo-overview-row {
    gap: var(--space-3);
  }
}

/* --- Responsive ECHO cards at tablet width --- */
@media (max-width: 1024px) and (min-width: 769px) {
  .echo-cards-row {
    grid-template-columns: 1fr 1fr;
  }
}

/* ============================================================
   Consequential Summary — Explorer Row & Site Detail Card
   ============================================================ */

/* Explorer: summary row beneath each data row in Consequential mode */
.summary-row {
  background: none !important;
}

.summary-row:hover {
  background: none !important;
}

.summary-cell {
  padding: 0 var(--space-3) var(--space-3) var(--space-5) !important;
  border-bottom: 1px solid var(--border);
  font-size: 0.8rem;
  line-height: 1.5;
  color: var(--text-secondary);
  font-style: italic;
}

.summary-icon {
  color: var(--amber);
  font-style: normal;
  margin-right: 4px;
}

/* Site Detail: summary card at top of sections in Consequential mode */
.cons-summary-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--amber);
  border-radius: var(--radius);
  padding: var(--space-3) var(--space-4);
  margin-bottom: var(--space-4);
}

.cons-summary-label {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--amber);
  margin-bottom: var(--space-1);
}

.cons-summary-text {
  font-size: 0.9rem;
  line-height: 1.6;
  color: var(--text-primary);
}


/* ============================================================
   v2.3 — Sidebar Collapse, Column Manager, CSV Export
   ============================================================ */

/* ---- Toolbar right cluster ---- */
.table-toolbar {
  flex-wrap: nowrap !important;
  gap: var(--space-3) !important;
}
.table-toolbar-right {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-shrink: 0;
}

.toolbar-icon-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  height: 30px;
  transition: color var(--transition-interactive),
              background var(--transition-interactive),
              border-color var(--transition-interactive);
}
.toolbar-icon-btn:hover {
  background: var(--color-surface-offset);
  color: var(--color-text);
  border-color: var(--color-text-faint);
}
.toolbar-icon-btn:active {
  background: var(--color-surface-dynamic);
}
.toolbar-export-btn {
  border-color: var(--color-primary);
  color: var(--color-primary);
}
.toolbar-export-btn:hover {
  background: var(--color-primary-highlight);
  border-color: var(--color-primary-hover);
  color: var(--color-primary-hover);
}
.toolbar-export-btn:active {
  background: color-mix(in oklch, var(--color-primary) 15%, var(--color-surface));
}

/* ---- Sidebar Collapse (desktop) ---- */
.sidebar-toggle-btn[aria-pressed="true"] svg {
  opacity: 0.6;
}

/* The collapsed state drives layout via CSS Grid column widths */
#appLayout {
  --sidebar-width: 220px;
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  transition: grid-template-columns 280ms cubic-bezier(0.16, 1, 0.3, 1);
}

/* Overlay must not participate in the grid layout on desktop */
.sidebar-overlay {
  display: none;
}

#appLayout.sidebar-collapsed {
  --sidebar-width: 0px;
  grid-template-columns: 0px 1fr;
}

#appLayout.sidebar-collapsed #sidebar {
  overflow: hidden;
  width: 0;
  min-width: 0;
  padding: 0;
  border-right-color: transparent;
}

#appLayout.sidebar-collapsed #sidebar * {
  pointer-events: none;
  opacity: 0;
  transition: opacity 180ms ease;
}

#sidebar {
  transition: width 280ms cubic-bezier(0.16, 1, 0.3, 1),
              padding 280ms cubic-bezier(0.16, 1, 0.3, 1),
              opacity 180ms ease;
  overflow: hidden;
}

/* Mobile: sidebar collapse becomes show/hide (existing open class behavior),
   so the desktop grid transition should not apply */
@media (max-width: 767px) {
  #appLayout,
  #appLayout.sidebar-collapsed {
    grid-template-columns: 1fr !important;
  }
  #appLayout.sidebar-collapsed #sidebar {
    width: auto;
    min-width: unset;
    overflow: unset;
  }
  #appLayout.sidebar-collapsed #sidebar * {
    pointer-events: auto;
    opacity: 1;
  }
}

/* ---- Column Manager Panel ---- */
#colManagerContainer {
  position: fixed;
  inset: 0;
  z-index: 500;
  pointer-events: none;
}

.col-manager-backdrop {
  position: absolute;
  inset: 0;
  background: oklch(0 0 0 / 0.35);
  pointer-events: auto;
  z-index: 1;
}

.col-manager-panel {
  position: absolute;
  top: 52px;
  right: var(--space-4);
  width: 320px;
  max-height: calc(100vh - 80px);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  pointer-events: auto;
  z-index: 2;
  transform: translateY(-8px) scale(0.97);
  opacity: 0;
  transition: transform 200ms cubic-bezier(0.16, 1, 0.3, 1),
              opacity 160ms ease;
}

.col-manager-panel.open {
  transform: translateY(0) scale(1);
  opacity: 1;
}

.col-manager-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--color-divider);
  flex-shrink: 0;
}

.col-manager-title {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-text);
}

.col-manager-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  cursor: pointer;
  background: none;
  border: none;
  transition: background var(--transition-interactive), color var(--transition-interactive);
}
.col-manager-close:hover {
  background: var(--color-surface-offset);
  color: var(--color-text);
}

.col-manager-body {
  overflow-y: auto;
  flex: 1;
  padding: var(--space-3) var(--space-4);
  overscroll-behavior: contain;
}

.col-manager-actions {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  margin-bottom: var(--space-3);
  padding-bottom: var(--space-2);
  border-bottom: 1px solid var(--color-divider);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}
.col-manager-sep { opacity: 0.5; }

.col-mgr-link {
  background: none;
  border: none;
  padding: 0;
  color: var(--color-primary);
  font-size: var(--text-xs);
  cursor: pointer;
  font-weight: 500;
}
.col-mgr-link:hover { text-decoration: underline; }

.col-group-block {
  margin-bottom: var(--space-4);
}

.col-group-label {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--color-text-faint);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: var(--space-2);
}

.col-option {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) 0;
  cursor: pointer;
}
.col-option:hover .col-option-label {
  color: var(--color-text);
}

.col-checkbox {
  width: 14px;
  height: 14px;
  accent-color: var(--color-primary);
  flex-shrink: 0;
  cursor: pointer;
}

.col-option-label {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  transition: color var(--transition-interactive);
}

/* ============================================================
   VIEW TOGGLE (Table | Map)
   ============================================================ */
.view-toggle {
  display: inline-flex;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow: hidden;
  margin-left: var(--space-3);
}

.view-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 5px 12px;
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--color-text-muted);
  background: transparent;
  border: none;
  cursor: pointer;
  transition: background var(--transition-interactive), color var(--transition-interactive);
}

.view-btn:hover {
  color: var(--color-text);
  background: var(--color-bg-hover);
}

.view-btn.active {
  color: var(--color-text);
  background: var(--color-accent);
}

/* ============================================================
   MAP CONTAINER
   ============================================================ */
.map-container {
  position: relative;
  flex: 1;
  min-height: 500px;
  border-radius: var(--radius-lg);
  overflow: hidden;
  border: 1px solid var(--color-border);
}

.map-container.fullscreen {
  position: fixed;
  inset: 0;
  z-index: 1000;
  min-height: unset;
  border-radius: 0;
  border: none;
}

/* Full-screen button */
.map-fullscreen-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 1001;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  color: var(--color-text);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: background var(--transition-interactive);
}

.map-fullscreen-btn:hover {
  background: var(--color-bg-hover);
}

/* Map legend */
.map-legend {
  position: absolute;
  bottom: 20px;
  left: 10px;
  z-index: 1001;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 10px 14px;
  box-shadow: var(--shadow-sm);
  font-size: var(--text-xs);
}

.map-legend-title {
  font-weight: 600;
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-muted);
  margin-bottom: 6px;
}

.map-legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 2px 0;
  color: var(--color-text);
}

.map-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

.map-dot-critical { background: #ef4444; }
.map-dot-warning  { background: #f59e0b; }
.map-dot-good     { background: #22c55e; }
.map-dot-neutral  { background: #64748b; }

/* Leaflet popup overrides for dark theme */
[data-theme="dark"] .leaflet-popup-content-wrapper {
  background: var(--color-surface);
  color: var(--color-text);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
}

[data-theme="dark"] .leaflet-popup-tip {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
}

[data-theme="dark"] .leaflet-popup-close-button {
  color: var(--color-text-muted);
}

.leaflet-popup-content {
  margin: 10px 14px;
  font-family: 'Inter', sans-serif;
  font-size: 13px;
  line-height: 1.5;
}

.map-popup-name {
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 4px;
}

.map-popup-name a {
  color: var(--color-accent);
  text-decoration: none;
}

.map-popup-name a:hover {
  text-decoration: underline;
}

.map-popup-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 1px 0;
}

.map-popup-label {
  color: var(--color-text-muted);
}

.map-popup-value {
  font-weight: 500;
  text-align: right;
}

/* Cluster icon overrides for dark theme */
[data-theme="dark"] .marker-cluster div {
  background: rgba(59, 130, 246, 0.7);
  color: #fff;
  font-family: 'Inter', sans-serif;
  font-weight: 600;
}

[data-theme="dark"] .marker-cluster {
  background: rgba(59, 130, 246, 0.3);
}

/* Leaflet tile filter for dark mode */
[data-theme="dark"] .leaflet-tile-pane {
  filter: invert(1) hue-rotate(180deg) brightness(0.9) contrast(1.1);
}
