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

:root {
  --bg:          #0b0f1a;
  --surface:     #111827;
  --surface2:    #1a2236;
  --border:      #1f2d45;
  --text:        #e2e8f0;
  --text-muted:  #64748b;
  --accent:      #3b82f6;
  --opioid:      #a855f7;
  --heroin:      #f97316;
  --stim:        #ef4444;
  --rising:      #ef4444;
  --falling:     #22c55e;
  --neutral:     #f59e0b;
  --panel-width: 400px;
  --header-h:    64px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

/* ── Header ───────────────────────────────────────────────────────────────── */
header {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 100;
}

.header-inner {
  max-width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 14px;
}

.logo-mark { flex-shrink: 0; }

h1 {
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.02em;
}

.subtitle {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 1px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.header-right { display: flex; align-items: center; gap: 10px; }

.badge {
  font-size: 11px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 20px;
  letter-spacing: 0.04em;
}

.badge-live {
  background: rgba(34,197,94,0.12);
  color: #22c55e;
  border: 1px solid rgba(34,197,94,0.3);
  animation: pulse-live 2s ease-in-out infinite;
}

@keyframes pulse-live {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.6; }
}

.badge-date {
  background: rgba(59,130,246,0.1);
  color: #93c5fd;
  border: 1px solid rgba(59,130,246,0.2);
}

/* ── Main layout ──────────────────────────────────────────────────────────── */
main {
  display: flex;
  height: 100vh;
  padding-top: var(--header-h);
}

/* ── Map section ──────────────────────────────────────────────────────────── */
.map-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 20px 24px 16px;
  overflow: hidden;
  transition: margin-right 0.35s cubic-bezier(0.4,0,0.2,1);
}

.map-section.panel-open {
  margin-right: var(--panel-width);
}

.map-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 12px;
  flex-shrink: 0;
}

.map-header h2 {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-muted);
}

.excluded-note {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--text-muted);
}

.dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.excluded-dot { background: #2a2a2a; border: 1px solid #555; }

/* ── Map wrapper ──────────────────────────────────────────────────────────── */
.map-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

#map {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

#map svg {
  width: 100%;
  height: 100%;
  max-height: 100%;
}

.state {
  transition: fill 0.15s ease, stroke-width 0.1s;
}

.state:hover { filter: brightness(1.25); }

/* ── Legend ───────────────────────────────────────────────────────────────── */
.legend {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 10px;
  flex-wrap: wrap;
}

.legend-label {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
}

.legend-scale {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.legend-scale span {
  font-size: 10px;
  color: var(--text-muted);
  margin-right: 8px;
}

.legend-swatch {
  width: 16px;
  height: 16px;
  border-radius: 3px;
  flex-shrink: 0;
}

/* ── Footnotes ────────────────────────────────────────────────────────────── */
.footnotes {
  flex-shrink: 0;
  margin-top: 10px;
  border-top: 1px solid var(--border);
  padding-top: 8px;
}

.footnotes p {
  font-size: 10px;
  color: var(--text-muted);
  line-height: 1.6;
}

.footnotes a {
  color: #60a5fa;
  text-decoration: none;
}
.footnotes a:hover { text-decoration: underline; }

.footnotes code {
  font-size: 10px;
  background: rgba(255,255,255,0.06);
  padding: 1px 5px;
  border-radius: 3px;
  color: #93c5fd;
}

/* ── State detail panel ───────────────────────────────────────────────────── */
#state-panel {
  position: fixed;
  top: var(--header-h);
  right: 0;
  width: var(--panel-width);
  height: calc(100vh - var(--header-h));
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px;
  gap: 16px;
  overflow-y: auto;
  transform: translateX(100%);
  transition: transform 0.35s cubic-bezier(0.4,0,0.2,1);
  z-index: 50;
}

#state-panel.active { transform: translateX(0); }

/* ── Panel header ─────────────────────────────────────────────────────────── */
.panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  flex-shrink: 0;
}

.panel-title-block {
  display: flex;
  align-items: center;
  gap: 12px;
}

#state-panel h2 {
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
}

.state-code-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  background: rgba(59,130,246,0.15);
  color: #93c5fd;
  border: 1px solid rgba(59,130,246,0.25);
  padding: 2px 8px;
  border-radius: 12px;
  margin-top: 4px;
  letter-spacing: 0.08em;
}

#close-panel {
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--border);
  color: var(--text-muted);
  width: 30px;
  height: 30px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}
#close-panel:hover { background: rgba(255,255,255,0.12); color: var(--text); }

/* ── Period label ─────────────────────────────────────────────────────────── */
.period-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
  background: rgba(59,130,246,0.08);
  border: 1px solid rgba(59,130,246,0.15);
  border-radius: 6px;
  padding: 6px 12px;
  flex-shrink: 0;
}

/* ── Stat cards ───────────────────────────────────────────────────────────── */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  flex-shrink: 0;
}

.stat-card {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 10px;
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.stat-icon {
  font-size: 18px;
  line-height: 1;
  margin-top: 2px;
  flex-shrink: 0;
}
.opioid-icon { color: var(--opioid); }
.heroin-icon  { color: var(--heroin); }
.stim-icon    { color: var(--stim); }

.stat-info { min-width: 0; }
.stat-label { font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-muted); }
.stat-value { font-size: 18px; font-weight: 700; line-height: 1.2; margin: 2px 0; }
.stat-sub   { font-size: 9px; color: var(--text-muted); }

.stat-value.rising  { color: var(--rising); }
.stat-value.falling { color: var(--falling); }
.stat-value.neutral { color: var(--neutral); }

/* ── Trend row ────────────────────────────────────────────────────────────── */
.trend-row {
  display: flex;
  gap: 12px;
  flex-shrink: 0;
}

.trend-item {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.trend-label { font-size: 11px; color: var(--text-muted); }
.trend-arrow { font-size: 15px; font-weight: 700; }
.trend-arrow.rising  { color: var(--rising); }
.trend-arrow.falling { color: var(--falling); }
.trend-arrow.neutral { color: var(--neutral); }

/* ── Chart ────────────────────────────────────────────────────────────────── */
.chart-container {
  flex: 1;
  min-height: 220px;
  position: relative;
}

/* ── Panel footnote ───────────────────────────────────────────────────────── */
.panel-footnote {
  font-size: 10px;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  padding-top: 10px;
  flex-shrink: 0;
}

/* ── Tooltip ──────────────────────────────────────────────────────────────── */
#tooltip {
  position: absolute;
  display: none;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 12px;
  color: var(--text);
  pointer-events: none;
  z-index: 200;
  white-space: nowrap;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5);
}

#tooltip strong { display: block; font-size: 13px; margin-bottom: 2px; }
#tooltip em { color: var(--text-muted); font-style: normal; font-size: 10px; }

/* ── Loading spinner ──────────────────────────────────────────────────────── */
.loading-msg {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: var(--text-muted);
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 10px;
}

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

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

/* ── Scrollbar styling ────────────────────────────────────────────────────── */
#state-panel::-webkit-scrollbar { width: 4px; }
#state-panel::-webkit-scrollbar-track { background: transparent; }
#state-panel::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
