/* ==========================================================================
   Design tokens — cartographic palette
   Light "Atlas": sky-blue sea, warm grey neighbours, states on a warm
   peach → brick ramp (complementary to the water, so land always reads first).
   Dark "Noche": navy sea, slate neighbours, states on a teal → gold ramp
   where the most populated states glow.
   Each ramp is selected for its theme (low → high), not an automatic inversion.
   ========================================================================== */
:root {
  --app-page: #f4f6f8;
  --app-card: #ffffff;
  --app-ink: #1f2933;
  --app-ink-muted: #52606d;          /* 5.9:1 on page — WCAG AA */
  --app-line: #dde3ea;
  --app-accent: #9e3d2a;             /* 6.6:1 on white */
  --app-accent-rgb: 158, 61, 42;
  --app-accent-hover: #83311f;
  --app-on-accent: #ffffff;

  --map-water: #b5d6ee;
  --map-maritime: #cfe5f5;
  --map-maritime-line: #4a7fb0;      /* 3.3:1 on maritime fill (non-text) */
  --map-land: #ecebe7;
  --map-land-line: #c4c0b8;
  --map-label: #5f5a54;              /* 5.9:1 on land */
  --map-sea-label: #2c5a85;          /* 4.9:1 on water */

  --state-stroke: #ffffff;
  --state-focus: #000000;
  --state-focus-halo: #ffffff;
  --state-dimmed-opacity: 0.22;

  --choropleth-0: #fbe7c6;
  --choropleth-1: #f6c38b;
  --choropleth-2: #ec9560;
  --choropleth-3: #d1653f;
  --choropleth-4: #9e3d2a;

  --tooltip-bg: var(--app-card);
  --tooltip-ink: var(--app-ink);

  --skeleton-base: #e6ebf0;
  --skeleton-shine: #f3f6f9;

  --transition-fast: 160ms ease-out;
  --transition-med: 320ms ease-in-out;

  /* Bootstrap bridge: its components pick up the palette from here. */
  --bs-body-bg: var(--app-page);
  --bs-body-color: var(--app-ink);
  --bs-secondary-color: var(--app-ink-muted);
  --bs-border-color: var(--app-line);
  --bs-tertiary-bg: #eef2f6;
  --bs-primary-rgb: var(--app-accent-rgb);
  --bs-link-color: var(--app-accent);
  --bs-link-color-rgb: var(--app-accent-rgb);
  --bs-link-hover-color: var(--app-accent-hover);
  --bs-link-hover-color-rgb: 131, 49, 31;
}

[data-bs-theme='dark'] {
  --app-page: #0f1720;
  --app-card: #16202c;
  --app-ink: #e6edf3;
  --app-ink-muted: #9fb0c0;          /* 7:1 on card */
  --app-line: #263344;
  --app-accent: #f2cf78;
  --app-accent-rgb: 242, 207, 120;
  --app-accent-hover: #f6dc9a;
  --app-on-accent: #16202c;

  --map-water: #0d2236;
  --map-maritime: #12304b;
  --map-maritime-line: #5b98cc;      /* 4.5:1 on maritime fill */
  --map-land: #232f3d;
  --map-land-line: #344458;
  --map-label: #94a6b8;              /* 5.4:1 on land */
  --map-sea-label: #6f9cc4;          /* 5.5:1 on water */

  --state-stroke: #16202c;
  --state-focus: #ffffff;
  --state-focus-halo: #000000;
  --state-dimmed-opacity: 0.2;

  --choropleth-0: #2b4a5c;
  --choropleth-1: #2f7279;
  --choropleth-2: #3f9a86;
  --choropleth-3: #a3c46d;
  --choropleth-4: #f2cf78;

  --skeleton-base: #1d2936;
  --skeleton-shine: #263344;

  --bs-body-bg: var(--app-page);
  --bs-body-color: var(--app-ink);
  --bs-secondary-color: var(--app-ink-muted);
  --bs-border-color: var(--app-line);
  --bs-tertiary-bg: #1d2936;
  --bs-primary-rgb: var(--app-accent-rgb);
  --bs-link-color: var(--app-accent);
  --bs-link-color-rgb: var(--app-accent-rgb);
  --bs-link-hover-color: var(--app-accent-hover);
  --bs-link-hover-color-rgb: 246, 220, 154;
}

body {
  background: var(--bs-body-bg);
  color: var(--bs-body-color);
}

/* Bootstrap components re-pointed at the tokens. */
.card {
  --bs-card-bg: var(--app-card);
  --bs-card-border-color: var(--app-line);
  --bs-card-cap-bg: transparent;
  border-radius: 12px;
  overflow: hidden;
}

.offcanvas {
  --bs-offcanvas-bg: var(--app-card);
}

.btn-primary,
.btn-outline-primary {
  --bs-btn-hover-color: var(--app-on-accent);
  --bs-btn-hover-bg: var(--app-accent-hover);
  --bs-btn-hover-border-color: var(--app-accent-hover);
  --bs-btn-active-color: var(--app-on-accent);
  --bs-btn-active-bg: var(--app-accent);
  --bs-btn-active-border-color: var(--app-accent);
  --bs-btn-focus-shadow-rgb: var(--app-accent-rgb);
}

.btn-primary {
  --bs-btn-color: var(--app-on-accent);
  --bs-btn-bg: var(--app-accent);
  --bs-btn-border-color: var(--app-accent);
}

.btn-outline-primary {
  --bs-btn-color: var(--app-accent);
  --bs-btn-border-color: var(--app-line);
}

/* Settings dropdown that holds the appearance switch. */
.appearance-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.4rem 0.75rem;
  border: 1px solid var(--app-line);
  border-radius: 999px;
  background: transparent;
  color: var(--app-ink);
  font-size: 0.875rem;
}

.appearance-toggle:hover,
.appearance-toggle[aria-expanded='true'] {
  background: var(--bs-tertiary-bg);
}

.appearance-toggle:focus-visible {
  outline: 2px solid var(--app-accent);
  outline-offset: 2px;
}

.appearance-toggle svg {
  width: 1rem;
  height: 1rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.appearance-toggle .appearance-caret {
  color: var(--app-ink-muted);
  transition: transform var(--transition-fast);
}

.appearance-toggle[aria-expanded='true'] .appearance-caret {
  transform: rotate(180deg);
}

.appearance-menu {
  --bs-dropdown-bg: var(--app-card);
  --bs-dropdown-border-color: var(--app-line);
  padding: 0.75rem;
  border-radius: 14px;
  box-shadow: 0 12px 32px rgb(0 0 0 / 0.18);
}

.appearance-menu-title {
  margin: 0 0 0.5rem 0.25rem;
  font-size: 0.8125rem;
  color: var(--app-ink-muted);
}

/* Appearance switch: pill track, icon + label options, active one filled. */
.theme-switch {
  display: inline-flex;
  gap: 2px;
  margin: 0;
  padding: 4px;
  border: 0;
  border-radius: 999px;
  background: var(--bs-tertiary-bg);
}

.theme-switch-input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.theme-switch-option {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0.85rem;
  border-radius: 999px;
  font-size: 0.8125rem;
  color: var(--app-ink-muted);
  cursor: pointer;
  transition: background-color var(--transition-fast), color var(--transition-fast);
}

.theme-switch-option svg {
  width: 1rem;
  height: 1rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.theme-switch-option:hover {
  color: var(--app-ink);
}

.theme-switch-input:checked + .theme-switch-option {
  background: var(--app-accent);
  color: var(--app-on-accent);
}

.theme-switch-input:focus-visible + .theme-switch-option {
  outline: 2px solid var(--app-accent);
  outline-offset: 2px;
}

.skip-link {
  position: absolute;
  top: 0.5rem;
  left: 0.5rem;
  z-index: 1080;
  padding: 0.5rem 1rem;
  background: var(--bs-body-bg);
  border-radius: 0.375rem;
}

.app-header {
  background: var(--app-card);
}

/* ==========================================================================
   Map
   ========================================================================== */
.map-container {
  position: relative;
}

/* Reserve space only while loading; once drawn the SVG sets its own height. */
.map-container[aria-busy='true'] {
  min-height: 280px;
}

.map-svg {
  display: block;
  width: 100%;
  height: auto;
  max-height: 72vh;
  overflow: hidden;
  background: var(--map-water); /* the sea also fills any letterboxing */
}

/* Geographic context (decorative, aria-hidden; described in .map-borders). */
.map-maritime {
  fill: var(--map-maritime);
  fill-rule: evenodd;
  stroke: var(--map-maritime-line);
  stroke-width: 1.3;
  stroke-dasharray: 7 5;
  vector-effect: non-scaling-stroke;
}

.map-neighbor {
  fill: var(--map-land);
  fill-rule: evenodd;
  stroke: var(--map-land-line);
  stroke-width: 0.9;
  vector-effect: non-scaling-stroke;
}

.map-labels {
  pointer-events: none;
}

.map-label {
  paint-order: stroke;
  stroke-linejoin: round;
  stroke-width: 3px;
}

.map-label--country {
  font: 600 15px system-ui, "Segoe UI", sans-serif;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  fill: var(--map-label);
  stroke: var(--map-land);
}

.map-label--country.map-label--small {
  font-size: 10px;
  letter-spacing: 0.1em;
}

.map-label--sea {
  font: italic 400 17px Georgia, "Times New Roman", serif;
  letter-spacing: 0.06em;
  fill: var(--map-sea-label);
  stroke: var(--map-water);
}

.map-label--maritime {
  font: 500 12px system-ui, "Segoe UI", sans-serif;
  fill: var(--map-sea-label);
  stroke: var(--map-maritime);
}

/* The SVG shrinks ~2.7× on phones: enlarge names in user units, drop the line note. */
@media (max-width: 575.98px) {
  .map-label--country { font-size: 26px; }
  .map-label--country.map-label--small { display: none; } /* no room to place it legibly on a phone */
  .map-label--sea { font-size: 22px; letter-spacing: 0; }
  .map-label--maritime { display: none; }
}

.state {
  stroke: var(--state-stroke);
  stroke-width: 0.8;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke; /* borders stay crisp while zooming */
  fill-rule: evenodd;
  cursor: pointer;
  transition: fill var(--transition-med), opacity var(--transition-med), stroke var(--transition-fast);
  outline: none;
}

.state--c0 { fill: var(--choropleth-0); }
.state--c1 { fill: var(--choropleth-1); }
.state--c2 { fill: var(--choropleth-2); }
.state--c3 { fill: var(--choropleth-3); }
.state--c4 { fill: var(--choropleth-4); }

.state:hover,
.state.is-highlighted {
  stroke: var(--state-focus);
  stroke-width: 2;
}

/* Keyboard focus is always visible, independent of hover. */
/* A halo keeps the ring visible against both ends of the colour ramp. */
.state:focus-visible {
  stroke: var(--state-focus);
  stroke-width: 3;
  filter: drop-shadow(0 0 1px var(--state-focus-halo)) drop-shadow(0 0 1px var(--state-focus-halo));
}

.state.is-dimmed {
  opacity: var(--state-dimmed-opacity);
  pointer-events: none;
}

.map-spinner {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
}

.map-tooltip {
  position: fixed;
  z-index: 1070;
  max-width: 260px;
  padding: 0.5rem 0.75rem;
  border-radius: 0.5rem;
  background: var(--tooltip-bg);
  color: var(--tooltip-ink);
  border: 1px solid var(--app-line);
  font-size: 0.875rem;
  line-height: 1.35;
  pointer-events: none;
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.18);
  transform: translate(-50%, calc(-100% - 12px));
}

.map-tooltip strong {
  display: block;
  font-size: 0.9375rem;
}

/* Legend: one continuous bar split into quintiles, range text under each step. */
.map-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem 2px;
  padding: 1rem 1rem 0;
  font-size: 0.8125rem;
  color: var(--app-ink-muted);
}

.legend-title {
  font-weight: 600;
  color: var(--bs-body-color);
  width: 100%;
}

.legend-item {
  display: flex;
  flex: 1 1 0;
  min-width: 0;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
}

.legend-item .legend-swatch {
  width: 100%;
  height: 0.55rem;
  border-radius: 0;
  box-shadow: none;
}

.legend-item:first-of-type .legend-swatch { border-radius: 4px 0 0 4px; }
.legend-item:last-of-type .legend-swatch { border-radius: 0 4px 4px 0; }

/* Borders panel: text twin of the map's neighbour and maritime layers. */
.map-borders {
  padding: 0.875rem 1rem 1rem;
  font-size: 0.8125rem;
}

.map-borders-title {
  font-size: 0.8125rem;
  font-weight: 600;
  margin: 0 0 0.4rem;
}

.map-borders-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 1.25rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.map-key {
  display: inline-block;
  width: 1.1rem;
  height: 0.75rem;
  margin-right: 0.4rem;
  border-radius: 2px;
  vertical-align: -0.1em;
}

.map-key--land {
  background: var(--map-land);
  border: 1px solid var(--map-land-line);
}

.map-key--maritime {
  background: var(--map-maritime);
  border: 1.5px dashed var(--map-maritime-line);
}

.legend-swatch,
.list-swatch {
  display: inline-block;
  width: 0.875rem;
  height: 0.875rem;
  border-radius: 3px;
  flex-shrink: 0;
  box-shadow: inset 0 0 0 1px rgb(0 0 0 / 0.08);
}

.swatch--c0 { background: var(--choropleth-0); }
.swatch--c1 { background: var(--choropleth-1); }
.swatch--c2 { background: var(--choropleth-2); }
.swatch--c3 { background: var(--choropleth-3); }
.swatch--c4 { background: var(--choropleth-4); }

/* ==========================================================================
   State list (table view of the same data)
   ========================================================================== */
.state-list {
  max-height: 70vh;
  overflow-y: auto;
  counter-reset: rank;
}

.state-list {
  --bs-list-group-bg: transparent;
}

.state-list .list-group-item {
  transition: opacity var(--transition-med), background-color var(--transition-fast);
}

.state-list .list-group-item-action {
  color: var(--bs-body-color);
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 0.25rem 0.625rem;
  align-items: center;
}

.state-list .rank {
  min-width: 1.75rem;
  color: var(--app-ink-muted);
  font-variant-numeric: tabular-nums;
}

.state-list .population {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.state-list .region-tag {
  grid-column: 2 / 3;
  font-size: 0.75rem;
  color: var(--app-ink-muted);
}

.state-list .is-entering {
  animation: fade-in var(--transition-med);
}

@keyframes fade-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

/* ==========================================================================
   Skeleton loaders
   ========================================================================== */
.skeleton {
  border-radius: 0.375rem;
  background: linear-gradient(90deg, var(--skeleton-base) 25%, var(--skeleton-shine) 37%, var(--skeleton-base) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.4s ease infinite;
}

.skeleton-map {
  width: 100%;
  aspect-ratio: 16 / 10;
}

.skeleton-line {
  height: 1.25rem;
}

.skeleton-line.short { width: 55%; }

.skeleton-block {
  height: 5rem;
}

@keyframes shimmer {
  0% { background-position: 100% 50%; }
  100% { background-position: 0 50%; }
}

/* ==========================================================================
   Detail panel
   ========================================================================== */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.75rem;
}

.stat-tile {
  padding: 0.75rem;
  border-radius: 0.5rem;
  background: var(--bs-tertiary-bg);
}

.stat-tile .label {
  font-size: 0.75rem;
  color: var(--app-ink-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.stat-tile .value {
  font-size: 1.25rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.share-bar {
  display: flex;
  height: 0.625rem;
  border-radius: 999px;
  overflow: hidden;
  gap: 2px;
  background: var(--bs-tertiary-bg);
}

.share-bar > span:first-child { background: var(--choropleth-3); }
.share-bar > span:last-child { background: var(--choropleth-1); }

.weather-temp {
  font-size: 2.25rem;
  font-weight: 600;
  line-height: 1;
}

/* ==========================================================================
   Motion preferences
   ========================================================================== */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
