:root {
  --bg: #0f1419;
  --surface: #1a2332;
  --surface2: #243044;
  --border: #2d3a4f;
  --text: #e8edf4;
  --text-muted: #8b9cb3;
  --accent: #3b82f6;
  --green: #22c55e;
  --orange: #f59e0b;
  --red: #ef4444;
  --gray: #6b7280;
  --purple: #a855f7;
  --radius: 10px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.35);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

/* Header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.logo { font-size: 2rem; }

.header h1 {
  font-size: 1.25rem;
  font-weight: 700;
}

.subtitle {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.badge {
  background: var(--surface2);
  border: 1px solid var(--border);
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* Breadcrumb */
.breadcrumb {
  display: flex;
  gap: 0.25rem;
  padding: 0.75rem 1.5rem;
  flex-wrap: wrap;
}

.crumb {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 0.9rem;
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
}

.crumb:hover { background: var(--surface2); }
.crumb.active { color: var(--text); font-weight: 600; cursor: default; }
.crumb:not(:last-child)::after {
  content: " ›";
  color: var(--text-muted);
  margin-left: 0.25rem;
}

/* KPIs */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 0.75rem;
  padding: 0 1.5rem 1rem;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.kpi-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.kpi-value {
  font-size: 1.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.kpi-card.certified .kpi-value { color: var(--purple); }
.kpi-card.on-wme .kpi-value { color: var(--green); }
.kpi-card.missing .kpi-value { color: var(--red); }
.kpi-card.unknown .kpi-value { color: var(--gray); }
.kpi-card.pct .kpi-value { color: var(--accent); }

/* Toolbar */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0 1.5rem 1rem;
  flex-wrap: wrap;
}

.search-box {
  position: relative;
  flex: 1;
  max-width: 360px;
}

.search-box input {
  width: 100%;
  padding: 0.6rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.95rem;
}

.search-box input:focus {
  outline: none;
  border-color: var(--accent);
}

.search-results {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-top: 4px;
  max-height: 280px;
  overflow-y: auto;
  z-index: 1000;
  list-style: none;
  box-shadow: var(--shadow);
}

.search-results.hidden { display: none; }

.search-results li {
  padding: 0.6rem 1rem;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}

.search-results li:hover { background: var(--surface2); }
.search-results .dept-tag {
  color: var(--text-muted);
  font-size: 0.8rem;
  margin-left: 0.5rem;
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.view-tabs {
  display: flex;
  gap: 0.25rem;
  background: var(--surface2);
  padding: 3px;
  border-radius: 8px;
}

.view-tabs .tab {
  background: none;
  border: none;
  color: var(--text-muted);
  padding: 0.4rem 0.85rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
}

.view-tabs .tab.active {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

.view-tabs .tab:hover:not(.active) {
  color: var(--text);
}

.rankings-panel {
  margin: 0 1.5rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  box-shadow: var(--shadow);
}

.rankings-panel h2 {
  font-size: 0.95rem;
  font-weight: 600;
  margin-bottom: 0;
  color: var(--text-muted);
}

.rankings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}

.pct-filter {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  flex-wrap: wrap;
}

.pct-filter input,
.pct-filter-inline input,
.table-controls input[type="number"] {
  width: 4.5rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.35rem 0.5rem;
  border-radius: 6px;
  font-size: 0.85rem;
}

.pct-filter-inline {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.pct-presets {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}

.pct-presets .preset,
.btn-sm {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: 0.25rem 0.6rem;
  border-radius: 6px;
  font-size: 0.75rem;
  cursor: pointer;
}

.pct-presets .preset:hover,
.btn-sm:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.rank-count,
.table-count {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: normal;
}

.ranking-scroll {
  max-height: 420px;
  overflow-y: auto;
}

.rankings-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

.ranking-col h3 {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.ranking-col h3 small {
  text-transform: none;
  font-weight: normal;
  opacity: 0.8;
}

.ranking-col ol {
  list-style: none;
  counter-reset: rank;
}

.ranking-col li {
  counter-increment: rank;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
  cursor: pointer;
}

.ranking-col li:hover {
  color: var(--accent);
}

.ranking-col li::before {
  content: counter(rank);
  min-width: 1.4rem;
  height: 1.4rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface2);
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--text-muted);
}

.ranking-col li .rank-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ranking-col li .rank-pct {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

@media (max-width: 900px) {
  .rankings-grid {
    grid-template-columns: 1fr;
  }
}

.map-mode select {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius);
  font-size: 0.85rem;
}

/* Main grid */
.main-grid {
  display: grid;
  grid-template-columns: 1fr 340px;
  grid-template-rows: auto auto;
  gap: 1rem;
  padding: 0 1.5rem 1.5rem;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}

.panel h2 {
  font-size: 0.95rem;
  font-weight: 600;
  margin-bottom: 0.75rem;
  color: var(--text-muted);
}

.map-panel {
  grid-row: span 2;
}

#map {
  height: 520px;
  border-radius: 8px;
  background: #1e2a3a;
}

.map-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
  margin-top: 0.75rem;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  border: 1px solid rgba(255,255,255,0.2);
}

.chart-wrap {
  position: relative;
  height: 200px;
  margin-bottom: 1.25rem;
}

.chart-bar { height: 220px; }

.table-panel {
  grid-column: 1 / -1;
}

.table-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
}

.table-controls {
  display: flex;
  gap: 0.5rem;
}

.table-controls input,
.table-controls select {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.4rem 0.75rem;
  border-radius: 6px;
  font-size: 0.85rem;
}

.table-scroll {
  overflow: auto;
  max-height: 400px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

thead {
  position: sticky;
  top: 0;
  background: var(--surface2);
  z-index: 1;
}

th, td {
  padding: 0.5rem 0.75rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.rank-num {
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 700;
  width: 2rem;
  text-align: center;
}

th {
  font-weight: 600;
  color: var(--text-muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

tbody tr {
  cursor: pointer;
  transition: background 0.15s;
}

tbody tr:hover { background: var(--surface2); }

.pct-badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
}

.pct-high { background: rgba(34,197,94,0.2); color: var(--green); }
.pct-mid { background: rgba(245,158,11,0.2); color: var(--orange); }
.pct-low { background: rgba(239,68,68,0.2); color: var(--red); }
.pct-zero { background: rgba(107,114,128,0.2); color: var(--gray); }

/* Detail panel */
.detail-panel {
  grid-column: 1 / -1;
}

.detail-panel.hidden { display: none; }

.detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
}

.btn-close {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  width: 32px;
  height: 32px;
  border-radius: 6px;
  font-size: 1.25rem;
  cursor: pointer;
  line-height: 1;
}

.btn-close:hover { background: var(--border); }

.detail-kpis {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  margin-bottom: 1rem;
  font-size: 0.9rem;
}

.detail-kpis span { color: var(--text-muted); }
.detail-kpis strong { color: var(--text); margin-left: 0.25rem; }

.footer {
  text-align: center;
  padding: 1rem;
  font-size: 0.75rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
}

/* Leaflet overrides */
.leaflet-container { background: #1e2a3a; font-family: inherit; }
.leaflet-tooltip {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 0.4rem 0.6rem;
  font-size: 0.8rem;
  box-shadow: var(--shadow);
}

.leaflet-tooltip::before { border-top-color: var(--border); }

/* Responsive */
@media (max-width: 1100px) {
  .main-grid {
    grid-template-columns: 1fr;
  }
  .map-panel { grid-row: auto; }
  #map { height: 400px; }
}

@media (max-width: 600px) {
  .header { flex-direction: column; align-items: flex-start; gap: 0.5rem; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
}
