/* ============================================================
   DESIGN TOKENS
   ============================================================ */
:root {
  --bg:        #0d1117;
  --surface:   #161b22;
  --surface2:  #1c2128;
  --surface3:  #22272e;
  --border:    #30363d;
  --border2:   #444c56;
  --text:      #e6edf3;
  --muted:     #8b949e;
  --muted2:    #6e7681;
  --accent:    #f0a500;
  --accent2:   #d4920a;
  --blue:      #58a6ff;
  --green:     #3fb950;
  --red:       #f85149;
  --purple:    #bc8cff;
  --teal:      #39d353;
  --sidebar-w: 288px;
  --radius:    10px;
  --radius-sm: 6px;
  --transition: 200ms ease;
}

/* ============================================================
   RESET & BASE
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 14px; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  overflow-x: hidden;
}

/* ============================================================
   LOADING SPINNER
   ============================================================ */
.loading-overlay {
  position: fixed; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: var(--bg);
  z-index: 999;
  gap: 20px;
  color: var(--muted);
}

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

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

/* ============================================================
   APP LAYOUT
   ============================================================ */
.app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ============================================================
   SIDEBAR
   ============================================================ */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  height: 100vh;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 20px 18px 16px;
  border-bottom: 1px solid var(--border);
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-emoji { font-size: 28px; }

.logo-title {
  font-size: 1rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.01em;
}

.logo-sub {
  font-size: 0.72rem;
  color: var(--muted2);
  font-weight: 400;
  margin-top: 2px;
}

/* --- Results badge --- */
.sidebar-results {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.results-badge {
  display: flex;
  align-items: baseline;
  gap: 4px;
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--text);
}

#results-filtered { color: var(--accent); }

.results-badge .sep { color: var(--muted2); font-size: 1rem; }

.results-label {
  font-size: 0.72rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.btn-clear {
  margin-top: 8px;
  padding: 6px 14px;
  font-size: 0.75rem;
  font-weight: 500;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
  align-self: flex-start;
}

.btn-clear:hover { color: var(--text); border-color: var(--border2); background: var(--surface2); }

/* --- Active filter pills --- */
.active-pills {
  padding: 0 18px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease, padding 0.3s ease;
}

.active-pills.has-pills {
  max-height: 120px;
  padding: 12px 18px 0;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px 3px 8px;
  background: rgba(240, 165, 0, 0.12);
  border: 1px solid rgba(240, 165, 0, 0.3);
  color: var(--accent);
  border-radius: 20px;
  font-size: 0.72rem;
  font-weight: 500;
  max-width: 200px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pill-x {
  cursor: pointer;
  opacity: 0.7;
  flex-shrink: 0;
  font-size: 0.85rem;
  line-height: 1;
}

.pill-x:hover { opacity: 1; }

/* --- Filters scroll area --- */
.filters-scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding-bottom: 16px;
}

.filters-scroll::-webkit-scrollbar { width: 4px; }
.filters-scroll::-webkit-scrollbar-track { background: transparent; }
.filters-scroll::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* --- Filter group --- */
.filter-group {
  border-bottom: 1px solid var(--border);
}

.filter-group-hd {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 18px;
  cursor: pointer;
  user-select: none;
  transition: background var(--transition);
}

.filter-group-hd:hover { background: var(--surface2); }

.filter-group-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text);
}

.filter-group-meta {
  display: flex;
  align-items: center;
  gap: 6px;
}

.filter-sel-count {
  font-size: 0.7rem;
  background: var(--accent);
  color: #000;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 10px;
  display: none;
}

.filter-sel-count.visible { display: inline; }

.filter-chevron {
  color: var(--muted2);
  font-size: 0.65rem;
  transition: transform var(--transition);
}

.filter-group.open .filter-chevron { transform: rotate(180deg); }

.filter-group-body {
  display: none;
  padding: 0 18px 12px;
}

.filter-group.open .filter-group-body { display: block; }

.filter-search {
  width: 100%;
  padding: 6px 10px;
  background: var(--surface3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.78rem;
  margin-bottom: 8px;
  outline: none;
  transition: border-color var(--transition);
}

.filter-search:focus { border-color: var(--accent); }
.filter-search::placeholder { color: var(--muted2); }

.filter-options { max-height: 200px; overflow-y: auto; }
.filter-options::-webkit-scrollbar { width: 3px; }
.filter-options::-webkit-scrollbar-thumb { background: var(--border); }

.filter-opt {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 4px;
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: background var(--transition);
}

.filter-opt:hover { background: var(--surface2); }

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

.filter-opt-label {
  font-size: 0.76rem;
  color: var(--muted);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.filter-opt.checked .filter-opt-label { color: var(--text); }

.filter-opt-count {
  font-size: 0.68rem;
  color: var(--muted2);
  flex-shrink: 0;
}

/* ============================================================
   MAIN CONTENT
   ============================================================ */
.main {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.main::-webkit-scrollbar { width: 6px; }
.main::-webkit-scrollbar-track { background: transparent; }
.main::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ============================================================
   STATS GRID
   ============================================================ */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 14px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  transition: border-color var(--transition);
}

.stat-card:hover { border-color: var(--border2); }

.stat-card.accent { border-color: rgba(240,165,0,0.3); background: rgba(240,165,0,0.05); }

.stat-val {
  font-size: 2rem;
  font-weight: 700;
  color: var(--accent);
  line-height: 1;
  margin-bottom: 6px;
}

.stat-lbl {
  font-size: 0.75rem;
  color: var(--muted);
  font-weight: 500;
}

.stat-card.stat-small .stat-val {
  font-size: 1.3rem;
  line-height: 1.2;
}

/* ============================================================
   SECTIONS
   ============================================================ */
.section {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.section-hd {
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}

.section-hd h2 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.01em;
}

.section-desc {
  font-size: 0.72rem;
  color: var(--muted2);
  margin-top: 3px;
}

/* ============================================================
   GRID
   ============================================================ */
.grid {
  display: grid;
  gap: 14px;
}

.grid.cols-1 { grid-template-columns: 1fr; }
.grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid.cols-3 { grid-template-columns: repeat(3, 1fr); }

.span-1 { grid-column: span 1; }
.span-2 { grid-column: span 2; }
.span-3 { grid-column: span 3; }

/* ============================================================
   CARDS
   ============================================================ */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  transition: border-color var(--transition);
}

.card:hover { border-color: var(--border2); }

.card-title {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 14px;
}

/* ============================================================
   CHARTS
   ============================================================ */
.chart      { width: 100%; height: 280px; }
.chart.h-sm { height: 200px; }
.chart.h-md { height: 320px; }
.chart.h-lg { height: 380px; }
.chart.h-xl { height: 460px; }
.chart.h-map { height: 840px; }

/* ============================================================
   VIEW TABS
   ============================================================ */
.view-tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
}

.view-tab {
  padding: 8px 22px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  font-family: inherit;
  letter-spacing: 0.01em;
}

.view-tab:hover {
  color: var(--text);
  border-color: var(--border2);
  background: var(--surface2);
}

.view-tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #000;
  font-weight: 700;
}

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 1400px) {
  .stats-grid { grid-template-columns: repeat(3, 1fr); }
}

@media (max-width: 1100px) {
  :root { --sidebar-w: 248px; }
  .grid.cols-3 { grid-template-columns: repeat(2, 1fr); }
  .span-2 { grid-column: span 1; }
}

@media (max-width: 860px) {
  .app { flex-direction: column; }
  .sidebar { width: 100%; min-width: 100%; height: auto; max-height: 50vh; }
  .main { height: 50vh; }
  .grid.cols-2, .grid.cols-3 { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ============================================================
   AUTH GATE
   ============================================================ */
#auth-gate {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
  z-index: 1000;
  padding: 24px;
}

.auth-card {
  width: 100%; max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px 36px;
  text-align: center;
}

.auth-logo { font-size: 36px; margin-bottom: 12px; }

.auth-title {
  font-size: 20px; font-weight: 600;
  color: var(--text); margin: 0 0 4px;
}

.auth-subtitle {
  font-size: 13px; color: var(--muted);
  margin: 0 0 28px;
}

.auth-hint {
  font-size: 13px; color: var(--muted);
  margin: 0 0 20px; line-height: 1.5;
}

.auth-error {
  font-size: 13px; color: var(--red);
  margin: 0 0 20px; line-height: 1.5;
}

.auth-input {
  display: block; width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 14px; font-family: inherit;
  padding: 10px 14px;
  margin-bottom: 12px;
  outline: none;
  transition: border-color var(--transition);
}
.auth-input:focus { border-color: var(--accent); }
.auth-input::placeholder { color: var(--muted2); }

.btn-primary {
  display: block; width: 100%;
  background: var(--accent); color: #0d1117;
  border: none; border-radius: var(--radius-sm);
  font-size: 14px; font-weight: 600; font-family: inherit;
  padding: 11px 20px;
  cursor: pointer;
  transition: background var(--transition), opacity var(--transition);
}
.btn-primary:hover { background: var(--accent2); }
.btn-primary:disabled { opacity: 0.55; cursor: not-allowed; }

.btn-secondary {
  display: block; width: 100%;
  background: transparent; color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  font-size: 14px; font-weight: 500; font-family: inherit;
  padding: 10px 20px;
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
}
.btn-secondary:hover { background: var(--accent); color: #0d1117; }

/* Logout button in sidebar header */
.sidebar-header {
  display: flex; align-items: flex-start;
  justify-content: space-between;
}

.btn-logout {
  background: transparent; border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--muted); font-size: 14px;
  padding: 4px 8px; cursor: pointer;
  flex-shrink: 0; margin-top: 2px;
  transition: border-color var(--transition), color var(--transition);
}
.btn-logout:hover { border-color: var(--border2); color: var(--text); }
