/* ============================================================
   CWA Site Explorer — Stylesheet
   Environmental compliance dashboard
   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-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;

  --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-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;

  --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;
}

/* --- 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);
}

.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-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: '$';
}

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

.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: relative;
  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;
}

.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);
}

/* 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);
}

/* --- 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: none;
  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;
  }
}
