:root {
  --bg:       #0b0814;
  --surface:  #120f1f;
  --surface-2:#1a1532;
  --border:   #2a2440;
  --fg:       #ede8f7;
  --muted:    #8a83a6;
  --accent:   #a78bfa;
  --accent-2: #ec4899;
  --accent-soft: color-mix(in srgb, var(--accent) 18%, transparent);
  --radius: 10px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }   /* override .loading-screen { display:flex } etc. */
html, body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
}
body { min-height: 100vh; display: flex; flex-direction: column; }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; padding: 0.75rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: rgba(13, 10, 25, 0.75);
  backdrop-filter: blur(8px);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 0.5rem; font-weight: 600; letter-spacing: 0.02em; }
.logo {
  display: inline-grid; place-items: center;
  width: 28px; height: 28px; border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; font-weight: 700;
}

/* Refresh button */
.refresh-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--fg);
  padding: 0.35rem 0.8rem;
  border-radius: 999px;
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.refresh-btn:hover:not(:disabled) {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}
.refresh-btn:disabled { opacity: 0.55; cursor: default; }
.refresh-btn.syncing { color: var(--accent); border-color: var(--accent); }
.refresh-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: #22c55e;
}
.refresh-btn.syncing .refresh-dot {
  background: var(--accent);
  animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

/* Toast */
.refresh-toast {
  position: fixed;
  top: 64px;
  right: 1rem;
  z-index: 50;
  max-width: 420px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  padding: 0.7rem 0.9rem;
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  font-size: 0.9rem;
  line-height: 1.4;
}
.refresh-toast.error   { border-left-color: #f87171; }
.refresh-toast.success { border-left-color: #22c55e; }
.refresh-toast button {
  background: none; border: 0; color: var(--muted);
  cursor: pointer; float: right; font-size: 1.1rem;
  line-height: 1; margin-left: 0.5rem;
}
.refresh-toast code {
  display: block;
  background: var(--bg);
  padding: 0.35rem 0.5rem;
  border-radius: 4px;
  font-size: 0.78rem;
  margin-top: 0.35rem;
  overflow-x: auto;
  white-space: nowrap;
}

/* Tabs */
.tabs { display: flex; gap: 0.25rem; flex-wrap: wrap; }
.tabs a {
  color: var(--muted);
  padding: 0.4rem 0.85rem;
  border-radius: 999px;
  text-decoration: none;
  font-size: 0.9rem;
  transition: background 0.15s, color 0.15s;
}
.tabs a:hover { color: var(--fg); background: var(--accent-soft); }
.tabs a.active {
  color: var(--accent);
  background: var(--accent-soft);
  font-weight: 600;
}

main {
  flex: 1;
  padding: 1.5rem clamp(1rem, 4vw, 2.5rem);
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
}

/* Hero */
.hero { margin: 0.5rem 0 1.5rem; }
.hero h1 {
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-2) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  font-size: clamp(2.4rem, 6vw, 4rem);
  letter-spacing: -0.02em;
  margin: 0;
  line-height: 1;
}
.hero .sub { color: var(--muted); font-size: 1rem; margin-top: 0.4rem; }

h1 { font-size: 2rem; margin: 1rem 0 1rem; letter-spacing: -0.01em; }
h2 { font-size: 1.2rem; margin: 2rem 0 0.75rem; color: var(--fg); }
h3 { font-size: 1rem; margin: 0 0 0.75rem; color: var(--fg); }
.sub { color: var(--muted); margin-bottom: 1rem; }

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin: 0 0 1rem;
}
.card h2, .card h3 { margin-top: 0; }

/* KPI grid */
.grid-kpi {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 0.75rem;
  margin-bottom: 1.5rem;
}
.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
}
.kpi-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
}
.kpi-value {
  font-size: 1.9rem;
  font-weight: 600;
  line-height: 1.1;
  margin-top: 0.35rem;
  font-variant-numeric: tabular-nums;
}

/* Two-column grid */
.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(420px, 1fr));
  gap: 1rem;
}

/* Genre page: pie on the left, scrollable list on the right */
.grid-genre {
  display: grid;
  grid-template-columns: minmax(380px, 1fr) minmax(360px, 1fr);
  gap: 1rem;
  align-items: start;
}
@media (max-width: 900px) {
  .grid-genre { grid-template-columns: 1fr; }
}
.scroll-cap {
  max-height: 640px;
  overflow-y: auto;
}
.scroll-cap table { font-size: 0.9rem; }

/* Timeline heatmaps are intentionally wider than the card; let them scroll */
#chart-month-year, #chart-country-year, #chart-genre-year {
  overflow-x: auto;
  max-width: 100%;
}
#chart-month-year svg, #chart-country-year svg, #chart-genre-year svg { display: block; }

/* Genres over time + top-5 tiles layout */
.grid-genre-year {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  align-items: start;
}
@media (max-width: 1200px) {
  .grid-genre-year { grid-template-columns: 1fr; }
}
.year-genres-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 0.75rem;
  max-height: 620px;
  overflow-y: auto;
  padding-right: 0.25rem;
}
.year-genres-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
}
.year-genres-card .year-label {
  color: var(--muted);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-bottom: 0.4rem;
  font-weight: 600;
}
.year-genre-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  padding: 0.22rem 0.4rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.82rem;
  transition: background 0.12s, color 0.12s;
}
.year-genre-row:hover { background: var(--accent-soft); color: var(--accent); }
.year-genre-row .genre-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.year-genre-row .genre-plays { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 0.78rem; }

/* Pie / donut + legend */
.pie-wrap { display: flex; flex-direction: column; align-items: center; gap: 1rem; }
.pie-wrap svg { max-width: 100%; height: auto; }
.pie-legend {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.2rem 0.6rem;
  width: 100%;
  margin-top: 0.25rem;
}
.pie-legend-item {
  display: grid;
  grid-template-columns: 14px 1fr auto;
  gap: 0.5rem;
  align-items: center;
  padding: 0.3rem 0.5rem;
  border-radius: 6px;
  font-size: 0.85rem;
}
.pie-legend-item.clickable { cursor: pointer; }
.pie-legend-item.clickable:hover { background: var(--accent-soft); color: var(--accent); }
.pie-swatch { width: 12px; height: 12px; border-radius: 3px; }
.pie-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pie-pct { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 0.78rem; }

/* Year-artist tiles */
.grid-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 0.75rem;
}
.year-tile {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
}
.year-tile .year-label {
  color: var(--muted);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
}
.year-tile .year-artist { font-size: 1.2rem; font-weight: 600; margin-top: 0.2rem; }
.year-tile .year-plays { color: var(--muted); font-size: 0.85rem; }

/* Tables */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
  font-variant-numeric: tabular-nums;
}
table th, table td {
  padding: 0.4rem 0.7rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
table th {
  color: var(--muted);
  font-weight: 500;
  text-transform: uppercase;
  font-size: 0.72rem;
  letter-spacing: 0.08em;
}
table tbody tr:hover { background: var(--accent-soft); }
td.num, th.num { text-align: right; }

/* Search input */
.search {
  width: 100%;
  padding: 0.55rem 0.85rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--fg);
  font-family: inherit;
  font-size: 0.95rem;
  margin-bottom: 0.75rem;
}
.search:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* Pagination */
.pager {
  display: flex; gap: 0.5rem; justify-content: flex-end; align-items: center;
  padding: 0.5rem 0; color: var(--muted); font-size: 0.85rem;
}
.pager button {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--fg);
  padding: 0.3rem 0.7rem;
  border-radius: 6px;
  cursor: pointer;
}
.pager button:hover:not(:disabled) { background: var(--accent-soft); color: var(--accent); }
.pager button:disabled { opacity: 0.4; cursor: default; }

/* Pending banner */
.pending-banner {
  background: var(--accent-soft);
  border-left: 3px solid var(--accent);
  padding: 0.7rem 1rem;
  border-radius: var(--radius);
  margin-bottom: 1.25rem;
}
.pending-banner a { color: var(--accent); font-weight: 600; }

/* Footer */
.foot {
  padding: 1.25rem;
  color: var(--muted);
  font-size: 0.8rem;
  text-align: center;
  border-top: 1px solid var(--border);
  display: flex; gap: 0.5rem; justify-content: center;
}
.foot a { color: var(--muted); }

/* Loading screen */
.loading-screen {
  display: flex; flex-direction: column; align-items: center; gap: 1rem;
  color: var(--muted);
  padding: 4rem 0;
}
.spinner {
  width: 40px; height: 40px;
  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); } }

/* Observable Plot overrides for dark theme */
svg text { fill: var(--muted); }
svg [aria-label="x-axis"] text,
svg [aria-label="y-axis"] text { fill: var(--muted); }
svg [aria-label="x-axis tick"] line,
svg [aria-label="y-axis tick"] line,
svg [aria-label="x-grid"] line,
svg [aria-label="y-grid"] line { stroke: var(--border); }
svg [aria-label="x-rule"] line,
svg [aria-label="y-rule"] line { stroke: var(--border); }

pre {
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 0.85rem 1rem;
  border-radius: var(--radius);
  overflow-x: auto;
  font-size: 0.82rem;
}
code { font-family: ui-monospace, Menlo, monospace; }

.flag { font-size: 1.05em; }
.rank { color: var(--muted); }

/* Drill-down panel */
.drill-panel {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: min(560px, 100vw);
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: -16px 0 48px rgba(0, 0, 0, 0.45);
  z-index: 60;
  transform: translateX(110%);
  transition: transform 0.22s cubic-bezier(0.2, 0.8, 0.25, 1);
  display: flex; flex-direction: column;
  overflow: hidden;
}
.drill-panel.open { transform: translateX(0); }
.drill-overlay {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 55;
  opacity: 0;
  transition: opacity 0.22s;
}
.drill-overlay.open { opacity: 1; }

.drill-header {
  display: flex; align-items: flex-start; justify-content: space-between;
  padding: 1rem 1.25rem 0.75rem;
  border-bottom: 1px solid var(--border);
  gap: 1rem;
}
.drill-title {
  font-size: 1.4rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.drill-sub {
  color: var(--muted);
  font-size: 0.85rem;
  margin-top: 0.15rem;
}
.drill-close {
  background: transparent; border: 0; color: var(--muted);
  font-size: 1.6rem; line-height: 1; cursor: pointer; padding: 0 0.25rem;
  border-radius: 6px;
}
.drill-close:hover { color: var(--accent); background: var(--accent-soft); }

.drill-stats {
  display: flex; gap: 1.5rem; padding: 0.75rem 1.25rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.8rem; color: var(--muted);
}
.drill-stats strong { color: var(--fg); font-weight: 600; font-variant-numeric: tabular-nums; }

.drill-body {
  flex: 1; overflow-y: auto;
  padding: 0.5rem 0.5rem 1rem;
}
.drill-body table { font-size: 0.85rem; }
.drill-body td, .drill-body th { padding: 0.35rem 0.6rem; }

/* Clickable affordance */
.clickable, .year-tile.clickable, .row-clickable {
  cursor: pointer;
  transition: background 0.12s;
}
.year-tile.clickable:hover { background: var(--accent-soft); border-color: var(--accent); }
tbody tr.row-clickable:hover td { background: var(--accent-soft); }
svg .country-path.has-data { cursor: pointer; }
svg .country-path.has-data:hover { stroke: var(--accent); stroke-width: 1.2; }

/* Insights section */
.grid-insights {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(380px, 1fr));
  gap: 1rem;
}
.insight-card { min-height: 220px; display: flex; flex-direction: column; }
.insight-head { display: flex; justify-content: space-between; align-items: baseline; gap: 0.5rem; }
.insight-body {
  font-size: 1rem;
  line-height: 1.55;
  color: var(--fg);
  padding: 0.75rem 0;
  flex: 1;
}
.insight-body strong, .insight-body em { color: var(--accent); font-weight: 600; font-style: normal; }
.pill-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--fg);
  padding: 0.35rem 0.85rem;
  border-radius: 999px;
  font-size: 0.8rem;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}
.pill-btn:hover:not(:disabled) { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.pill-btn.active { background: var(--accent); color: #0b0814; border-color: var(--accent); font-weight: 600; }
.pill-btn.active:hover { background: var(--accent); color: #0b0814; }

.trend-toolbar {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
}

.grid-3-trend {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
  margin-top: 1rem;
}
@media (max-width: 1100px) { .grid-3-trend { grid-template-columns: 1fr; } }

/* Ask form */
.insight-ask form {
  display: flex;
  gap: 0.5rem;
  margin: 0.5rem 0 0.35rem;
}
.insight-ask input[type="text"] {
  flex: 1;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--fg);
  padding: 0.6rem 0.9rem;
  border-radius: 8px;
  font-family: inherit;
  font-size: 0.95rem;
}
.insight-ask input[type="text"]:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.insight-ask button {
  background: var(--accent);
  color: #0b0814;
  border: 0;
  padding: 0 1.1rem;
  border-radius: 8px;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}
.insight-ask button:hover { filter: brightness(1.1); }
.example-ask { color: var(--accent); text-decoration: none; }
.example-ask:hover { text-decoration: underline; }

/* Scrollbar (webkit) */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: var(--accent-soft); }
