:root {
  --bg:      #07090f;
  --surface: #0d1117;
  --raised:  #131920;
  --border:  #1e2433;
  --accent:  #7c6ff7;
  --cyan:    #22d3ee;
  --green:   #34d399;
  --orange:  #f59e0b;
  --red:     #f87171;
  --text:    #e8eaf0;
  --text-secondary: #9aa3b5;
  --muted:   #4a5568;
  --dim:     #2d3748;
  --divider: #0a0d13;
  --font:    'Space Grotesk', system-ui, sans-serif;
  --mono:    'JetBrains Mono', monospace;
  --sw:      380px;

  /* Type scale — kept intentionally small so nothing drifts by half a pixel */
  --fs-3xs: 8px;   /* tick labels, chip arrows */
  --fs-2xs: 9px;   /* micro labels: rank/id columns, section titles, sort labels */
  --fs-xs:  10px;  /* small values: chip text, pixel values, retry button, kbd */
  --fs-sm:  11px;  /* body-small: list names, tab labels, info copy */
  --fs-base:12px;  /* base: search input, slider date */
  --fs-md:  13px;  /* popup title, map loading text */
  --fs-lg:  14px;  /* stat card values */
  --fs-num: 26px;  /* hero pixel counter */
}

/* ── Light theme ── same semantic roles, re-picked so the brightness ladder
   (bg → dim → muted → text-secondary → text) still runs low-contrast to
   high-contrast — just inverted, not a literal colour flip. Status/accent
   hues are deepened slightly from their dark-mode values for AA contrast
   against a white surface. */
:root[data-theme="light"] {
  --bg:      #f2f3f6;
  --surface: #ffffff;
  --raised:  #eef0f4;
  --border:  #dde1e8;
  --accent:  #6d5ef0;
  --cyan:    #0e8fa6;
  --green:   #059669;
  --orange:  #c2790a;
  --red:     #dc2626;
  --text:    #14171f;
  --text-secondary: #454c5e;
  --muted:   #838ba0;
  --dim:     #c7ccd6;
  --divider: #eef0f4;
}
/* Hover highlights use a white-tint overlay in dark mode (lightens a dark
   surface); on a near-white surface that's invisible, so light mode needs
   the same effect built from black instead. */
:root[data-theme="light"] .tab-btn:hover { background: color-mix(in srgb, black 4%, transparent); }

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); overflow: hidden; }
#app { display: flex; height: 100vh; }

/* ── Focus ring — one shared treatment for every interactive element ─────
   Keyboard users get a visible ring; mouse users don't see a ring on click. */
button:focus-visible,
input:focus-visible,
.li:focus-visible,
.cty-lb-row:focus-visible,
a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}
#searchinput:focus-visible { outline-offset: 0; }

/* ── Sidebar ───────────────────────────────────────────── */
aside {
  width: var(--sw); min-width: var(--sw);
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; overflow: hidden; z-index: 500;
  position: relative;
  transition: width .25s cubic-bezier(0.4,0,0.2,1), min-width .25s cubic-bezier(0.4,0,0.2,1);
}
aside.folded { width: 0 !important; min-width: 0 !important; }

/* Trend panel — a second, narrower aside on the right; uses its own
   .closed class (not .folded) so its open/closed state is independent
   of the left sidebar's. */
#trend-panel {
  width: 320px; min-width: 320px;
  border-right: none; border-left: 1px solid var(--border);
}
#trend-panel.closed { width: 0 !important; min-width: 0 !important; border-left: none; }

#trend-head { padding: 12px 16px; border-bottom: 1px solid var(--border); flex-shrink: 0; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
#trend-title { font-size: var(--fs-sm); font-weight: 600; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#trend-close {
  width: 26px; height: 26px; border-radius: 6px; flex-shrink: 0;
  border: 1px solid var(--border); background: none;
  color: var(--dim); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: color .15s, background .15s, border-color .15s;
}
#trend-close:hover { color: var(--text); background: var(--raised); border-color: var(--muted); }

#trend-body { flex: 1; overflow-y: auto; }
#trend-body::-webkit-scrollbar { width: 3px; }
#trend-body::-webkit-scrollbar-thumb { background: var(--muted); border-radius: 2px; }

#trend-loading { display: flex; align-items: center; gap: 9px; padding: 16px; }
#trend-loading .spin { width: 14px; height: 14px; border: 1.5px solid var(--dim); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; flex-shrink: 0; }
#trend-loading span { font-size: var(--fs-sm); color: var(--muted); }

.trend-summary { padding: 16px 16px 4px; }
.trend-summary-val { font-family: var(--mono); font-size: var(--fs-num); font-weight: 600; color: var(--text); line-height: 1; letter-spacing: -1px; }
.trend-summary-sub { font-size: var(--fs-xs); color: var(--muted); margin-top: 5px; }
.trend-summary-sub.up   { color: var(--green); }
.trend-summary-sub.down { color: var(--red); }

.trend-svg { display: block; width: 100%; height: auto; padding: 4px 4px 16px; box-sizing: border-box; }
.trend-axis-line { stroke: var(--border); stroke-width: 1; }
.trend-axis-label { font-family: var(--mono); font-size: 8px; fill: var(--dim); }
.trend-dot { fill: var(--accent); }
.trend-dot:hover { fill: var(--cyan); }

/* Header */
#s-head { padding: 12px 12px 12px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.s-head-top { display: flex; align-items: center; justify-content: space-between; gap: 6px; margin-bottom: 6px; }
#s-head h1 { font-size: var(--fs-2xs); font-weight: 600; display: flex; align-items: center; gap: 8px; color: var(--muted); letter-spacing: .3px; min-width: 0; text-transform: uppercase; }
#s-head h1 .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 7px var(--accent); flex-shrink: 0; }
.s-head-btns { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
#theme-toggle {
  width: 26px; height: 26px; border-radius: 6px; flex-shrink: 0;
  border: 1px solid var(--border); background: none;
  color: var(--dim); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: color .15s, background .15s, border-color .15s;
}
#theme-toggle:hover { color: var(--text); background: var(--raised); border-color: var(--muted); }
#fold-btn {
  width: 26px; height: 26px; border-radius: 6px; flex-shrink: 0;
  border: 1px solid var(--border); background: none;
  color: var(--dim); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: color .15s, background .15s, border-color .15s;
}
#fold-btn:hover { color: var(--text); background: var(--raised); border-color: var(--muted); }
#fold-btn svg { transition: transform .25s cubic-bezier(0.4,0,0.2,1); }
aside.folded #fold-btn svg { transform: rotate(180deg); }
/* Sidebar reopen tab */
#sidebar-reopen {
  display: none; position: absolute; left: 0; top: 50%; transform: translateY(-50%);
  z-index: 600; width: 18px; height: 56px;
  background: var(--surface); border: 1px solid var(--border); border-left: none;
  border-radius: 0 7px 7px 0; cursor: pointer; color: var(--dim);
  align-items: center; justify-content: center; transition: color .15s, background .15s;
}
#sidebar-reopen:hover { color: var(--text); background: var(--raised); }
#sidebar-reopen-label { display: none; }
/* Resize handle */
#resize-handle {
  position: absolute; top: 0; right: -3px; width: 6px; height: 100%;
  cursor: col-resize; z-index: 510; background: transparent; transition: background .15s;
}
#resize-handle:hover { background: color-mix(in srgb, var(--accent) 35%, transparent); }
#resize-handle.active { background: color-mix(in srgb, var(--accent) 55%, transparent); }
aside.folded #resize-handle { display: none; }
#big-num { font-family: var(--mono); font-size: var(--fs-num); font-weight: 600; letter-spacing: -1px; margin-top: 6px; line-height: 1; color: var(--text); }
#big-num.idle { color: var(--dim); }
#big-label { font-size: var(--fs-2xs); color: var(--muted); text-transform: uppercase; letter-spacing: 0.9px; margin-top: 3px; }

/* Load bar */
#load-bar {
  margin: 8px 12px; padding: 7px 11px; border-radius: 7px;
  background: var(--raised); border: 1px solid var(--border);
  display: flex; align-items: center; gap: 9px; flex-shrink: 0;
}
#load-bar.done { display: none; }
#load-bar .spin { width: 12px; height: 12px; border: 1.5px solid var(--dim); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; flex-shrink: 0; }
#load-msg { font-size: var(--fs-sm); color: var(--muted); flex: 1; }

.retry-btn {
  display: none; flex-shrink: 0;
  font-family: var(--font); font-size: var(--fs-xs); font-weight: 600;
  color: var(--text); background: var(--raised); border: 1px solid var(--border);
  border-radius: 5px; padding: 3px 10px; cursor: pointer;
  transition: border-color .15s, background .15s, color .15s;
}
.retry-btn:hover { border-color: var(--accent); color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); }

/* Tab bar */
#tab-bar { display: flex; flex-shrink: 0; border-bottom: 1px solid var(--border); }
.tab-btn {
  flex: 1; padding: 9px 6px; font-size: var(--fs-sm); font-weight: 600; font-family: var(--font);
  color: var(--muted); background: none; border: none; border-bottom: 2px solid transparent;
  cursor: pointer; transition: color .15s, background .15s, border-color .15s;
  display: flex; align-items: center; justify-content: center; gap: 5px;
  letter-spacing: .2px; margin-bottom: -1px;
}
.tab-btn:hover { color: var(--text); background: color-mix(in srgb, white 2%, transparent); }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); }
.tab-btn svg { flex-shrink: 0; }

/* Tab panels — list uses flex-column for virtual scroll; stats/info scroll themselves */
.tab-panel { display: none; }
#panel-list.active  { display: flex; flex-direction: column; flex: 1; overflow: hidden; }
#panel-stats.active,
#panel-info.active  { display: block; flex: 1; overflow-y: auto; }
#panel-stats::-webkit-scrollbar, #panel-info::-webkit-scrollbar { width: 3px; }
#panel-stats::-webkit-scrollbar-thumb, #panel-info::-webkit-scrollbar-thumb { background: var(--muted); border-radius: 2px; }

/* ── Sort bar ────────────────────────────────────────────── */
#sort-bar {
  display: flex; align-items: center; gap: 5px; flex-wrap: wrap;
  padding: 7px 12px 6px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.sort-label { font-size: var(--fs-2xs); color: var(--dim); text-transform: uppercase; letter-spacing: 0.5px; margin-right: 2px; flex-shrink: 0; }
.sort-chip {
  font-size: var(--fs-xs); font-weight: 500; font-family: var(--font);
  color: var(--muted); background: var(--raised); border: 1px solid var(--border);
  border-radius: 4px; padding: 2px 8px; cursor: pointer; transition: all .15s;
  display: flex; align-items: center; gap: 3px;
}
.sort-chip:hover { border-color: var(--muted); color: var(--text); }
.sort-chip.active { border-color: var(--accent); color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); }
.sort-chip .arr { font-size: var(--fs-3xs); }

/* Column labels */
#col-heads {
  display: flex; align-items: center; gap: 5px;
  padding: 0 10px 0 10px; height: 24px;
  background: color-mix(in srgb, var(--raised) 60%, transparent);
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.col-h { font-size: var(--fs-2xs); color: var(--dim); text-transform: uppercase; letter-spacing: 0.5px; }
#cty-col-heads { display: none; align-items: center; gap: 5px; padding: 0 10px 0 10px; height: 24px; background: color-mix(in srgb, var(--raised) 60%, transparent); border-bottom: 1px solid var(--border); flex-shrink: 0; }

/* Search */
#searchbox { padding: 8px 12px 4px; flex-shrink: 0; position: relative; }
#search-icon { position: absolute; left: 21px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
#searchinput {
  width: 100%; background: var(--raised); border: 1px solid var(--border);
  border-radius: 6px; padding: 5px 9px 5px 27px;
  font-family: var(--font); font-size: var(--fs-base); color: var(--text); outline: none; transition: border-color .15s;
}
#searchinput:focus { border-color: var(--accent); }
#searchinput::placeholder { color: var(--muted); }
#srcount { font-size: var(--fs-xs); color: var(--dim); padding: 2px 12px 4px; flex-shrink: 0; min-height: 16px; }
#srcount.empty-hint { color: var(--muted); }

/* View toggle */
#view-toggle { display: flex; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.vtog { flex: 1; padding: 7px 8px; font-size: var(--fs-sm); font-weight: 500; font-family: var(--font); color: var(--muted); background: none; border: none; cursor: pointer; border-bottom: 2px solid transparent; transition: all .15s; }
.vtog:hover { color: var(--text); }
.vtog.on { color: var(--accent); border-bottom-color: var(--accent); }
/* Virtual scroll area */
#lscroll { flex: 1; position: relative; overflow: hidden; }
#lr-panel { position: absolute; inset: 0; overflow-y: auto; }
#lr-panel::-webkit-scrollbar { width: 3px; }
#lr-panel::-webkit-scrollbar-thumb { background: var(--muted); border-radius: 2px; }
#lc-panel { position: absolute; inset: 0; overflow-y: auto; display: none; }
#lc-panel::-webkit-scrollbar { width: 3px; }
#lc-panel::-webkit-scrollbar-thumb { background: var(--muted); border-radius: 2px; }
.cty-lb-row { display: flex; align-items: center; gap: 5px; padding: 0 10px 0 10px; height: 36px; flex-shrink: 0; border-bottom: 1px solid var(--divider); transition: background .1s, border-left-color .1s; cursor: pointer; border-left: 2px solid transparent; }
.cty-lb-row:hover { background: var(--raised); }
.cty-lb-row.selected { background: color-mix(in srgb, var(--accent) 10%, transparent); border-left-color: var(--accent); }
.cty-lb-row.selected .lname { color: var(--text); font-weight: 600; }
.cty-lb-row.rank-gold   { border-left-color: #fbbf24; }
.cty-lb-row.rank-silver { border-left-color: #94a3b8; }
.cty-lb-row.rank-bronze { border-left-color: #92400e; }
.cty-lb-row.rank-gold   .lrank { color: #fbbf24; }
.cty-lb-row.rank-silver .lrank { color: #94a3b8; }
.cty-lb-row.rank-bronze .lrank { color: #b45309; }
/* Country region-count chip — size to content, never clip */
.cty-reg-cnt { font-family: var(--mono); font-size: var(--fs-2xs); color: var(--dim); white-space: nowrap; flex-shrink: 0; width: 64px; text-align: right; }
/* Country sort bar — same look as the regions sort bar */
#cty-sort-bar { display: none; align-items: center; gap: 5px; flex-wrap: wrap; padding: 7px 12px 6px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
/* Shared flag glyph — same box for both the Regions and Countries lists so
   flags never render at two different sizes depending on which view you're in. */
.flag-ic { font-size: 14px; flex-shrink: 0; width: 20px; text-align: center; line-height: 1; }
/* Empty-state row shown when a search yields zero matches */
.empty-row { padding: 22px 16px; text-align: center; color: var(--dim); font-size: var(--fs-sm); line-height: 1.5; }
.empty-row b { color: var(--muted); font-weight: 500; }

/* List items */
.li {
  display: flex; align-items: center; gap: 5px;
  padding: 0 10px 0 10px; height: 36px; flex-shrink: 0;
  border-bottom: 1px solid var(--divider); transition: background .1s, border-left-color .1s;
  cursor: pointer; border-left: 2px solid transparent;
}
.li:hover { background: var(--raised); }
.li.selected { background: color-mix(in srgb, var(--accent) 8%, transparent); border-left-color: var(--accent); }
.li.selected .lname { color: var(--text); }
.li.rank-gold   { border-left-color: #fbbf24; }
.li.rank-silver { border-left-color: #94a3b8; }
.li.rank-bronze { border-left-color: #92400e; }
.li.rank-gold   .lrank { color: #fbbf24; }
.li.rank-silver .lrank { color: #94a3b8; }
.li.rank-bronze .lrank { color: #b45309; }
/* Column widths — sized for up to 6-digit ranks / IDs */
.lrank { font-family: var(--mono); font-size: var(--fs-2xs); color: var(--dim); width: 36px; text-align: right; flex-shrink: 0; }
.lid   { font-family: var(--mono); font-size: var(--fs-2xs); color: var(--muted); width: 54px; text-align: right; flex-shrink: 0; }
.lname { font-size: var(--fs-sm); color: var(--text-secondary); flex: 1; min-width: 0; display: flex; align-items: center; gap: 4px; }
.lname-txt { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1 1 auto; }
.lbar-w { width: 26px; flex-shrink: 0; }
.lbar { height: 3px; background: var(--accent); border-radius: 2px; opacity: 0.6; }
.lval { font-family: var(--mono); font-size: var(--fs-xs); color: var(--muted); width: 50px; text-align: right; flex-shrink: 0; white-space: nowrap; }
.lgo, .lwp, .ltrend {
  flex-shrink: 0; width: 20px; height: 20px; border-radius: 4px;
  border: 1px solid var(--border); background: transparent;
  color: var(--dim); cursor: pointer; font-size: var(--fs-xs);
  display: flex; align-items: center; justify-content: center; transition: all .1s;
}
.lgo:hover, .lwp:hover, .ltrend:hover { border-color: var(--accent); color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); }
.lwp-off { opacity: .25; pointer-events: none; }

/* ── Stats tab ───────────────────────────────────────────── */
.sect { padding: 12px 12px 0; }
.sect-title {
  font-size: var(--fs-2xs); color: var(--muted); text-transform: uppercase; letter-spacing: 0.7px;
  margin-bottom: 8px; display: flex; align-items: center; gap: 6px;
}
.sect-title::after { content: ''; flex: 1; height: 1px; background: var(--border); }

.scard-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 0 12px 10px; }
.scard { background: var(--raised); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; }
.scard.wide { grid-column: 1 / -1; }
.scard .sl { font-size: var(--fs-2xs); color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 3px; }
.scard .sv { font-family: var(--mono); font-size: var(--fs-lg); font-weight: 600; color: var(--cyan); line-height: 1.3; }
.scard .sv.accent { color: var(--accent); }
.scard .sv.green  { color: var(--green); }
.scard .sv.orange { color: var(--orange); }
.scard .sv.muted  { color: var(--muted); }
.scard .sd { font-size: var(--fs-xs); color: var(--dim); margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Distribution bar */
.dist-wrap { padding: 0 12px 10px; }
.dist-labels { display: flex; justify-content: space-between; font-size: var(--fs-2xs); color: var(--dim); font-family: var(--mono); margin-bottom: 5px; }
.dist-bar { display: flex; height: 16px; border-radius: 5px; overflow: hidden; gap: 1px; }
.dist-seg { flex: 1; position: relative; cursor: default; }
.dist-seg:hover::after {
  content: attr(data-tip);
  position: absolute; bottom: calc(100% + 5px); left: 50%; transform: translateX(-50%);
  background: var(--surface); border: 1px solid var(--border); border-radius: 5px;
  padding: 4px 7px; font-size: var(--fs-2xs); font-family: var(--mono); color: var(--text);
  white-space: nowrap; z-index: 20; pointer-events: none;
}

/* ── Info tab ────────────────────────────────────────────── */
.info-block { padding: 0 12px 14px; font-size: var(--fs-sm); line-height: 1.65; color: var(--text-secondary); }
.info-block a { color: var(--accent); text-decoration: none; border-bottom: 1px solid color-mix(in srgb, var(--accent) 45%, transparent); }
.info-block a:hover { border-bottom-color: var(--accent); }
.info-callout {
  margin: 0 12px 14px; padding: 10px 12px; border-radius: 8px;
  background: color-mix(in srgb, var(--orange) 8%, var(--raised));
  border: 1px solid color-mix(in srgb, var(--orange) 35%, var(--border));
  font-size: var(--fs-sm); line-height: 1.6; color: var(--text-secondary);
}
.info-callout b { color: var(--orange); }
.info-steps { list-style: none; padding: 0 12px 4px; display: flex; flex-direction: column; gap: 10px; }
.info-steps li { display: flex; gap: 9px; font-size: var(--fs-sm); color: var(--text-secondary); line-height: 1.55; }
.istep-num {
  flex-shrink: 0; width: 18px; height: 18px; border-radius: 5px; margin-top: 1px;
  background: var(--raised); border: 1px solid var(--border); color: var(--accent);
  font-family: var(--mono); font-size: var(--fs-2xs); display: flex; align-items: center; justify-content: center;
}
kbd {
  font-family: var(--mono); font-size: var(--fs-xs); color: var(--text);
  background: var(--raised); border: 1px solid var(--border); border-bottom-width: 2px;
  border-radius: 4px; padding: 1px 5px;
}
.credit-row { display: flex; justify-content: space-between; align-items: baseline; padding: 7px 0; border-bottom: 1px solid var(--surface); }
.credit-row:last-child { border-bottom: none; }
.credit-role { font-size: var(--fs-2xs); color: var(--dim); text-transform: uppercase; letter-spacing: .5px; }
.credit-name { font-size: var(--fs-sm); color: var(--text-secondary); }
.credit-name a { color: var(--accent); text-decoration: none; }
.credit-name a:hover { text-decoration: underline; }

/* ── Map ─────────────────────────────────────────────────── */
#mwrap { flex: 1; position: relative; background: var(--bg); }
#map { width: 100%; height: 100%; background: var(--bg); }

#minfo-pill {
  position: absolute; top: 14px; right: 14px; z-index: 400;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px); border: 1px solid var(--border);
  border-radius: 7px; padding: 6px 12px; text-align: right;
  cursor: pointer; transition: border-color .15s, background .15s;
}
#minfo-pill:hover { border-color: var(--muted); }
#minfo-count { font-family: var(--mono); font-size: var(--fs-sm); color: var(--muted); }
#minfo-date  { font-family: var(--mono); font-size: var(--fs-2xs); color: var(--dim); margin-top: 2px; }

/* Heatmap-only toggle */
#heatmap-toggle {
  position: absolute; top: 14px; left: 14px; z-index: 400;
  width: 34px; height: 34px; border-radius: 7px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px); border: 1px solid var(--border);
  color: var(--dim); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: color .15s, background .15s, border-color .15s;
}
#heatmap-toggle:hover { color: var(--text); border-color: var(--muted); }
#heatmap-toggle.on { color: var(--accent); border-color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--surface)); }

/* Heatmap-visibility toggle — hides the coloured overlay itself while
   keeping the base map/labels, e.g. to see a country's region shapes
   without the pixel-count colouring in the way. */
#heatmap-vis-toggle {
  position: absolute; top: 54px; left: 14px; z-index: 400;
  width: 34px; height: 34px; border-radius: 7px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px); border: 1px solid var(--border);
  color: var(--dim); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: color .15s, background .15s, border-color .15s;
}
#heatmap-vis-toggle:hover { color: var(--text); border-color: var(--muted); }
#heatmap-vis-toggle.off { color: var(--accent); border-color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--surface)); }

/* Heatmap-mode toggle — switches the overlay's colour source between
   cumulative total pixels and change since the previous snapshot, since
   a purely cumulative map barely changes once a region is mostly filled. */
#heatmap-mode-toggle {
  position: absolute; top: 94px; left: 14px; z-index: 400;
  width: 34px; height: 34px; border-radius: 7px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px); border: 1px solid var(--border);
  color: var(--dim); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font); font-size: 16px; font-weight: 700; line-height: 1;
  transition: color .15s, background .15s, border-color .15s;
}
#heatmap-mode-toggle:hover { color: var(--text); border-color: var(--muted); }
#heatmap-mode-toggle.on { color: var(--accent); border-color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--surface)); }

/* Hides the base map tiles + place labels, leaving just the coloured region overlay */
#map.heatmap-only .leaflet-tile-pane,
#map.heatmap-only .leaflet-labels-pane {
  display: none;
}

/* Gradient legend */
#mlegend {
  position: absolute; bottom: 30px; right: 14px; z-index: 400;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px); border: 1px solid var(--border);
  border-radius: 7px; padding: 7px 10px 6px;
}
#mlegend-mode {
  font-size: var(--fs-2xs); color: var(--muted); text-transform: uppercase;
  letter-spacing: 0.4px; margin-bottom: 5px; white-space: nowrap;
}
#mlegend-bar {
  width: 110px; height: 5px; border-radius: 3px;
  background: linear-gradient(to right,
    rgb(26,5,64), rgb(91,33,182), rgb(124,111,247),
    rgb(34,211,238), rgb(245,158,11), rgb(255,45,111));
}
.mleg-labels {
  display: flex; justify-content: space-between;
  font-size: var(--fs-2xs); color: var(--muted); font-family: var(--mono); margin-top: 3px;
}

/* Map loading overlay */
#mload {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%);
  z-index: 300; display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  justify-content: center; max-width: min(320px, 86vw);
  background: color-mix(in srgb, var(--surface) 95%, transparent);
  border: 1px solid var(--border); border-radius: 10px; padding: 14px 20px;
}
#mload.done { display: none; }
#mload .spin { width: 18px; height: 18px; border: 2px solid var(--dim); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
#mload span { font-size: var(--fs-md); color: var(--muted); }

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

/* Popup */
.leaflet-popup-content-wrapper { background: var(--surface) !important; border: 1px solid var(--border) !important; border-radius: 10px !important; box-shadow: 0 12px 40px rgba(0,0,0,.7) !important; padding: 0 !important; }
.leaflet-popup-tip-container { display: none; }
.leaflet-popup-content { margin: 0 !important; }
.pu { padding: 12px 14px; min-width: 160px; }
.pu-name { font-size: var(--fs-md); font-weight: 600; margin-bottom: 6px; line-height: 1.3; }
.pu-row { font-size: var(--fs-sm); color: var(--muted); margin-bottom: 2px; }
.pu-row b { color: var(--text-secondary); font-weight: 500; }
.pu-link { display: inline-block; margin-top: 8px; background: var(--accent); color: #fff; padding: 4px 10px; border-radius: 5px; font-size: var(--fs-sm); font-weight: 500; text-decoration: none; }
.pu-link:hover { opacity: .85; }
.pu-trend-btn {
  display: block; width: 100%; box-sizing: border-box; margin-top: 6px;
  background: none; border: 1px solid var(--border); color: var(--text-secondary);
  padding: 4px 10px; border-radius: 5px; font-size: var(--fs-sm); font-weight: 500;
  font-family: var(--font); cursor: pointer; text-align: center;
  transition: border-color .15s, color .15s;
}
.pu-trend-btn:hover { border-color: var(--accent); color: var(--accent); }

.leaflet-control-zoom a { background: var(--surface) !important; border-color: var(--border) !important; color: var(--muted) !important; }
.leaflet-control-zoom a:hover { background: var(--raised) !important; color: var(--text) !important; }

.filled-overlay { image-rendering: pixelated; }

/* ── Date timeline slider ──────────────────────────────── */
#date-slider-bar {
  display: none; /* shown via .has-slider on #mwrap */
  position: absolute; bottom: 0; left: 0; right: 0; z-index: 401;
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--border);
  padding: 8px 16px 10px;
  user-select: none;
}
#mwrap.has-slider #date-slider-bar { display: block; }
#mwrap.has-slider #mlegend { bottom: 94px; }
#mwrap.has-slider .leaflet-bottom { bottom: 84px; }

.ds-header { display: flex; align-items: baseline; gap: 8px; margin-bottom: 6px; }
.ds-header-label { font-size: var(--fs-2xs); color: var(--dim); text-transform: uppercase; letter-spacing: 0.5px; flex-shrink: 0; }
#ds-date { font-family: var(--mono); font-size: var(--fs-base); font-weight: 600; color: var(--text); flex: 1; }
#ds-pos  { font-family: var(--mono); font-size: var(--fs-2xs); color: var(--dim); }

.ds-row { display: flex; align-items: center; gap: 10px; }
.ds-nav {
  flex-shrink: 0; width: 22px; height: 22px; border-radius: 5px;
  border: 1px solid var(--border); background: var(--raised);
  color: var(--muted); cursor: pointer; font-size: 15px; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  transition: all .12s;
}
.ds-nav:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); }
.ds-nav:disabled { opacity: 0.25; cursor: not-allowed; }

.ds-track-wrap { flex: 1; position: relative; }
#date-range {
  -webkit-appearance: none; appearance: none; display: block;
  width: 100%; height: 4px; border-radius: 2px; outline: none; cursor: pointer;
  background: linear-gradient(to right,
    var(--accent) 0%, var(--accent) var(--fill, 100%),
    var(--dim)    var(--fill, 100%), var(--dim) 100%);
}
#date-range::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--accent); cursor: pointer;
  border: 2px solid var(--surface);
  box-shadow: 0 0 0 2px var(--accent), 0 2px 6px rgba(0,0,0,.5);
  transition: box-shadow .12s;
}
#date-range::-webkit-slider-thumb:hover {
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 30%, transparent), 0 2px 6px rgba(0,0,0,.5);
}
#date-range::-moz-range-thumb {
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--accent); cursor: pointer; border: 2px solid var(--surface);
  box-shadow: 0 2px 6px rgba(0,0,0,.5);
}
#date-range::-moz-range-track { height: 4px; background: transparent; }

#ds-ticks { position: relative; height: 20px; margin-top: 5px; }
.ds-tick { position: absolute; transform: translateX(-50%); display: flex; flex-direction: column; align-items: center; }
.ds-tick-mark { width: 1px; height: 4px; background: var(--dim); margin-bottom: 2px; }
.ds-tick-label { font-size: var(--fs-3xs); font-family: var(--mono); color: var(--dim); white-space: nowrap; }
.ds-tick.current .ds-tick-mark { background: var(--accent); }
.ds-tick.current .ds-tick-label { color: var(--accent); }

/* ── Mobile layout ──────────────────────────────────────────
   Below 768px the sidebar becomes a full-screen slide-up panel
   instead of a fixed-width side column, so the map gets the
   whole screen and the leaderboard is one tap away. */
@media (max-width: 768px) {
  aside {
    position: fixed; inset: 0; z-index: 700;
    width: 100% !important; min-width: 0 !important;
    transform: translateY(0);
    transition: transform .3s cubic-bezier(0.4,0,0.2,1);
    padding-bottom: env(safe-area-inset-bottom);
  }
  aside.folded { transform: translateY(100%); width: 100% !important; }
  #resize-handle { display: none; }
  #s-head { padding-top: max(12px, env(safe-area-inset-top)); }

  /* Trend panel overrides the generic aside rules above: slides in from
     the right instead of up from the bottom, and sits above the left
     sidebar (higher z-index) since only one full-screen panel makes
     sense open at a time on a phone-width screen. */
  #trend-panel {
    z-index: 750;
    transform: translateX(100%);
  }
  #trend-panel:not(.closed) { transform: translateX(0); }
  #trend-panel.closed { transform: translateX(100%); }
  #trend-head { padding-top: max(12px, env(safe-area-inset-top)); }

  /* Reopen control becomes a centered floating pill, not an edge tab */
  #sidebar-reopen {
    position: fixed; left: 50%; top: auto; bottom: 18px;
    transform: translateX(-50%);
    width: auto; height: 46px; padding: 0 18px 0 16px; gap: 8px;
    border-radius: 23px; border: 1px solid var(--border);
    background: var(--accent); color: #fff;
    box-shadow: 0 8px 24px rgba(0,0,0,.45);
    margin-bottom: env(safe-area-inset-bottom);
  }
  #sidebar-reopen:hover { background: var(--accent); color: #fff; }
  #sidebar-reopen svg { transform: rotate(-90deg); }
  #sidebar-reopen-label { display: inline; font-family: var(--font); font-size: var(--fs-md); font-weight: 600; white-space: nowrap; }
  body.has-slider #sidebar-reopen { bottom: 96px; }

  /* Bigger touch targets on the timeline slider */
  .ds-nav { width: 34px; height: 34px; font-size: 18px; }
  #date-range::-webkit-slider-thumb { width: 20px; height: 20px; }
  #date-range::-moz-range-thumb { width: 20px; height: 20px; }
  #date-slider-bar { padding: 10px 14px max(10px, env(safe-area-inset-bottom)); }

  /* Free up bottom-right for the legend; zoom control moves to bottom-left */
  .leaflet-bottom.leaflet-right { right: auto !important; left: 10px !important; }
  #minfo-pill { top: max(14px, env(safe-area-inset-top)); }
  #heatmap-toggle { top: max(14px, env(safe-area-inset-top)); left: max(14px, env(safe-area-inset-left)); }
  #heatmap-vis-toggle { top: max(54px, calc(env(safe-area-inset-top) + 40px)); left: max(14px, env(safe-area-inset-left)); }
  #heatmap-mode-toggle { top: max(94px, calc(env(safe-area-inset-top) + 80px)); left: max(14px, env(safe-area-inset-left)); }
}

/* Respect reduced-motion preferences: cut transitions/animations down to
   the minimum needed for state to still register. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}