/*
  Map component (js/map.js) and map questions (js/questions/map.js). Loaded after app.css; uses its tokens.

  Tokens (on .map and .map-legend, redefined for dark mode):
    --map-sea --map-land --map-land-border   sea, grey context land (and out-of-scope countries), their borders
    --map-country --map-border               in-scope countries and their borders
    --map-hover --map-focus --map-ok --map-bad   fills for hover and the highlight styles
    --map-marker-ring                         ring around microstate dots
    --map-label --map-label-halo              name labels
    --map-fill                                set per country by colorBy(); falls back to --map-country

  Classes:
    .map                        the component root (bounded height, touch-action: none); .map-quiz / .map-learn
    .map-svg                    the SVG; .map-land, .map-country (.is-active | .is-context), .map-marker
    .map-outline .map-focus-ring .map-pulse .map-label    overlays
    .map-controls .map-btn      zoom buttons (44 px)
    states on countries/markers: .is-hover .is-ok .is-bad .is-focus; on .map: .is-locked .is-dragging .is-pointing
    .map-placeholder            the box shown while geo.json loads
    .map-prompt-instruction .map-prompt-name    the two lines of a map question's h2.question-prompt
    .map-legend                 a legend outside the map that shows its fills (gets the tokens)
    .map-regions .map-mastery   added by learn and progress to their coloured maps (selection styles below)
*/

.map,
.map-legend {
  --map-sea: #cfe1ee;
  --map-land: #dddcd5;
  --map-land-border: #c6c5bd;
  --map-country: #fbf9f3;
  --map-border: #8a919c;
  --map-hover: #cbdcf8;
  --map-focus: #8db3ee;
  --map-ok: #4caf68;
  --map-bad: #e2675c;
  --map-marker-ring: #3a404b;
  --map-label: var(--fg);
  --map-label-halo: #ffffff;
}

.map {
  /* Square, like Europe in this projection, and never taller than 70 % of the screen, so on a phone the page
     still scrolls with a finger above or below the map. */
  position: relative;
  width: 100%;
  max-width: min(70vh, 40rem);
  max-width: min(70svh, 40rem);
  aspect-ratio: 1;
  overflow: hidden;
  overflow: clip;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--map-sea);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

@media (prefers-color-scheme: dark) {
  .map,
  .map-legend {
    --map-sea: #15212e;
    --map-land: #2b2f37;
    --map-land-border: #3a3f49;
    --map-country: #4a515e;
    --map-border: #1b1e24;
    --map-hover: #4f6894;
    --map-focus: #5d86cc;
    --map-ok: #3f9c5b;
    --map-bad: #c4584e;
    --map-marker-ring: #dfe3ea;
    --map-label-halo: #14161b;
  }
}

/* overflow: clip, not the default hidden: an SVG scroll container would itself become a keyboard tab stop. */
.map-svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: clip;
  cursor: grab;
}

.map.is-pointing .map-svg {
  cursor: pointer;
}

.map.is-dragging .map-svg {
  cursor: grabbing;
}

.map-land {
  fill: var(--map-land);
}

.map-country {
  fill: var(--map-land);
  stroke: var(--map-land-border);
  stroke-width: 0.5px;
  stroke-linejoin: round;
}

.map-country.is-active {
  fill: var(--map-fill, var(--map-country));
  stroke: var(--map-border);
  stroke-width: 0.75px;
}

.map-marker {
  fill: var(--map-fill, var(--map-country));
  stroke: var(--map-marker-ring);
  stroke-width: 1.5px;
}

/* The focus ring is drawn as an overlay (.map-focus-ring), so neighbours never cover it. */
.map-country:focus,
.map-marker:focus {
  outline: none;
}

/* Hover only on devices that have it; a highlighted country keeps its highlight colour. */
@media (hover: hover) {
  .map:not(.is-locked) :is(.map-country.is-active, .map-marker).is-hover:not(.is-ok, .is-bad, .is-focus) {
    fill: var(--map-hover);
  }
}

.map-country.is-focus,
.map-marker.is-focus {
  fill: var(--map-focus);
}

.map-country.is-ok,
.map-marker.is-ok {
  fill: var(--map-ok);
}

.map-country.is-bad,
.map-marker.is-bad {
  fill: var(--map-bad);
}

.map-outline,
.map-focus-ring,
.map-pulse,
.map-label {
  pointer-events: none;
}

.map-outline {
  fill: none;
  stroke-width: 1.5px;
  stroke-linejoin: round;
}

.map-outline.is-ok {
  stroke: var(--ok);
}

.map-outline.is-bad {
  stroke: var(--bad);
}

.map-outline.is-focus {
  stroke: var(--accent);
}

.map-focus-ring {
  fill: none;
  stroke: var(--focus);
  stroke-width: 3px;
  stroke-linejoin: round;
}

.map-pulse {
  fill: none;
  stroke: var(--ok);
  stroke-linejoin: round;
  animation: map-pulse 0.8s ease-out 2;
}

.map-pulse.is-ring {
  transform-box: fill-box;
  transform-origin: center;
  animation-name: map-pulse-ring;
}

@keyframes map-pulse {
  from {
    stroke-width: 2px;
    stroke-opacity: 0.9;
  }
  to {
    stroke-width: 16px;
    stroke-opacity: 0;
  }
}

@keyframes map-pulse-ring {
  from {
    stroke-width: 2px;
    opacity: 0.9;
    transform: scale(1);
  }
  to {
    stroke-width: 2px;
    opacity: 0;
    transform: scale(4.5);
  }
}

.map-label {
  fill: var(--map-label);
  stroke: var(--map-label-halo);
  stroke-linejoin: round;
  paint-order: stroke;
  font-weight: 700;
  text-anchor: middle;
  dominant-baseline: central;
}

/* ---- controls */

.map-controls {
  position: absolute;
  top: var(--space-2);
  right: var(--space-2);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.map-btn {
  display: grid;
  place-items: center;
  width: var(--tap);
  height: var(--tap);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--fg);
  box-shadow: var(--shadow);
  cursor: pointer;
}

.map-btn:hover {
  background: var(--surface-2);
}

.map-btn[aria-disabled="true"] {
  color: var(--muted);
  opacity: 0.5;
  cursor: default;
}

.map-btn svg {
  width: 1.25rem;
  height: 1.25rem;
}

/* ---- loading box and map questions */

.map-placeholder {
  display: grid;
  place-items: center;
  color: var(--muted);
}

.map-placeholder p {
  margin: 0;
}

.map-prompt-instruction {
  display: block;
  margin-bottom: var(--space-1);
  color: var(--muted);
  font-size: 1rem;
  font-weight: 600;
}

.map-prompt-name {
  display: block;
  font-size: 1.5rem;
}

/* ---- coloured maps (colorBy)

  Learn (.map-regions) colours the countries by region; the chosen one takes a strong fill of its own and a dark
  ring. The mastery map (.map-mastery, progress) keeps the chosen country's colour, which is the data, and draws a
  heavy outline instead. */

.map-regions {
  --map-focus: #1f5fbf;
  --map-selected-ring: #0f2f61;
}

@media (prefers-color-scheme: dark) {
  .map-regions {
    --map-focus: #f5f4ef;
    --map-selected-ring: #14161b;
  }
}

.map-regions .map-outline.is-focus {
  stroke: var(--map-selected-ring);
  stroke-width: 2px;
}

.map-regions .map-marker.is-focus {
  stroke: var(--map-selected-ring);
  stroke-width: 3px;
}

.map-mastery :is(.map-country, .map-marker).is-focus {
  fill: var(--map-fill, var(--map-country));
}

.map-mastery .map-outline.is-focus {
  stroke: var(--fg);
  stroke-width: 3px;
}

.map-mastery .map-marker.is-focus {
  stroke: var(--fg);
  stroke-width: 3.5px;
}

@media (prefers-reduced-motion: reduce) {
  .map-pulse {
    animation: none;
    opacity: 0;
  }
}
