/* =========================================================================
   La Mappa del Mondo — foglio di stile
   Stile da libro illustrato: colori caldi, forme morbide, ombre leggere.
   Organizzazione: 1) variabili  2) layout  3) mappa  4) scheda  5) controlli
   ========================================================================= */

/* ---------- 0. Font delle bandiere ----------
   Windows non disegna le emoji bandiera (mostra le due lettere del codice).
   Questo font, caricato in locale, contiene solo quei simboli: gli altri
   caratteri restano al font di sistema grazie a unicode-range. */
@font-face {
  font-family: "Twemoji Country Flags";
  src: url("../assets/TwemojiCountryFlags.woff2") format("woff2");
  unicode-range: U+1F1E6-1F1FF, U+1F3F4, U+E0062-E007F;
  font-display: swap;
}

/* ---------- 1. Variabili ---------- */
:root {
  --carta: #fdf3e0;          /* sfondo pagina, color carta */
  --carta-scura: #f3e3c8;
  --inchiostro: #4a3b2a;     /* testo principale */
  --inchiostro-tenue: #8d7a63;
  --oceano: #8ed3ea;
  --oceano-fondo: #66bfdd;
  --bordo-terra: #fffaf0;    /* contorno dei paesi */
  --terra-neutra: #cdc4b2;   /* territori senza scheda informativa */

  --raggio: 18px;
  --ombra-morbida: 0 10px 30px rgba(74, 59, 42, .18);
  --transizione: 180ms cubic-bezier(.34, 1.3, .64, 1);

  /* "Twemoji Country Flags" è limitato ai codepoint delle bandiere:
     per tutto il resto vale il primo font di sistema disponibile. */
  --font: "Twemoji Country Flags", "Nunito", "Quicksand",
          "Segoe UI Variable Display", "Trebuchet MS",
          system-ui, -apple-system, sans-serif;
}

/* ---------- 2. Layout della pagina ---------- */
* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  flex-direction: column;
  font-family: var(--font);
  color: var(--inchiostro);
  background: var(--carta);
  /* Puntini appena accennati: danno alla pagina una texture di carta */
  background-image:
    radial-gradient(circle at 50% 50%, rgba(184, 154, 108, .16) 1px, transparent 1.2px);
  background-size: 22px 22px;
  overflow: hidden;                  /* la mappa gestisce da sé lo scorrimento */
  -webkit-text-size-adjust: 100%;
}

.site-header {
  flex: none;
  padding: 14px 20px 10px;
  text-align: center;
}

.site-header h1 {
  margin: 0;
  font-size: clamp(1.35rem, 3.6vw, 2.1rem);
  font-weight: 800;
  letter-spacing: -.01em;
}

.site-header .globe {
  display: inline-block;
  animation: gira 14s linear infinite;
}

@keyframes gira {
  0%, 70% { transform: rotate(0); }
  85%     { transform: rotate(18deg); }
  100%    { transform: rotate(0); }
}

.site-header p {
  margin: 4px 0 0;
  font-size: clamp(.82rem, 2.2vw, .98rem);
  color: var(--inchiostro-tenue);
}

.stage {
  position: relative;                /* riferimento per scheda e tooltip */
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
}

.site-footer {
  flex: none;
  padding: 8px 20px 12px;
  text-align: center;
  font-size: .72rem;
  line-height: 1.5;
  color: var(--inchiostro-tenue);
}

.site-footer p { margin: 0; }
.site-footer strong { font-weight: 700; color: #7a6650; }

/* ---------- 3. La mappa ---------- */
#world {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;                /* zoom e pan sono gestiti da map.js */
  cursor: grab;
  overflow: visible;
}

#world.is-panning { cursor: grabbing; }

#ocean {
  fill: var(--oceano);
  stroke: #ffffff;
  stroke-width: 3;
  stroke-linejoin: round;
}

/* Secondo strato sopra l'oceano: la trama a onde */
#ocean-texture {
  stroke: none;
  pointer-events: none;
}

#graticule {
  fill: none;
  stroke: #ffffff;
  stroke-opacity: .38;
  stroke-width: .8;
  stroke-dasharray: 4 5;
  pointer-events: none;
}

#decor, #countries, #markers { filter: url(#landShadow); }

.landmass {
  fill: var(--terra-neutra);
  stroke: var(--bordo-terra);
  stroke-width: .7;
  stroke-linejoin: round;
  pointer-events: none;              /* territori senza dati: non cliccabili */
}

.country {
  stroke: var(--bordo-terra);
  stroke-width: .7;
  stroke-linejoin: round;
  stroke-linecap: round;
  cursor: pointer;
  transform-box: fill-box;
  transform-origin: center;
  transition: filter var(--transizione), stroke-width var(--transizione);
}

.country:hover,
.country.is-active {
  stroke-width: 1.8;
  filter: brightness(1.12) saturate(1.15);
}

/* Piccolo rimbalzo al click, prima che compaia la scheda */
.country.is-bumping,
.marker.is-bumping { animation: rimbalzo 320ms cubic-bezier(.34, 1.56, .64, 1); }

@keyframes rimbalzo {
  0%   { transform: scale(1); }
  45%  { transform: scale(1.035); }
  100% { transform: scale(1); }
}

/* Marker dei micro-stati: punto visibile + area d'ingaggio più larga */
.marker {
  cursor: pointer;
  transform-box: fill-box;
  transform-origin: center;
  /* Compensa lo zoom: i punti non crescono insieme alla mappa */
  scale: var(--scala-marker, 1);
}

.marker-hit {
  fill: transparent;
  pointer-events: all;               /* cliccabile anche se non dipinto */
}

.marker-dot {
  stroke: var(--bordo-terra);
  stroke-width: 1.4;
  pointer-events: none;
  transition: r var(--transizione);
}

.marker:hover .marker-dot,
.marker.is-active .marker-dot {
  r: 5;
  stroke-width: 2;
}

/* --- Tavolozza: una famiglia di colori per continente, 5 tinte ciascuna.
       La tinta è precalcolata nei dati in modo che due paesi confinanti
       non ricevano mai lo stesso colore. --- */
.country.eu-0, .marker.eu-0 .marker-dot { fill: #7ac97f; }
.country.eu-1, .marker.eu-1 .marker-dot { fill: #a8dc79; }
.country.eu-2, .marker.eu-2 .marker-dot { fill: #5bbc92; }
.country.eu-3, .marker.eu-3 .marker-dot { fill: #c3e592; }
.country.eu-4, .marker.eu-4 .marker-dot { fill: #8ad4ab; }

.country.as-0, .marker.as-0 .marker-dot { fill: #f7a95e; }
.country.as-1, .marker.as-1 .marker-dot { fill: #fbc76c; }
.country.as-2, .marker.as-2 .marker-dot { fill: #ef8d59; }
.country.as-3, .marker.as-3 .marker-dot { fill: #fcdd8e; }
.country.as-4, .marker.as-4 .marker-dot { fill: #e9a074; }

.country.af-0, .marker.af-0 .marker-dot { fill: #f3d059; }
.country.af-1, .marker.af-1 .marker-dot { fill: #e5b444; }
.country.af-2, .marker.af-2 .marker-dot { fill: #f8e18b; }
.country.af-3, .marker.af-3 .marker-dot { fill: #d9a03c; }
.country.af-4, .marker.af-4 .marker-dot { fill: #f0c86f; }

.country.am-0, .marker.am-0 .marker-dot { fill: #f4897f; }
.country.am-1, .marker.am-1 .marker-dot { fill: #f8ab9f; }
.country.am-2, .marker.am-2 .marker-dot { fill: #e97164; }
.country.am-3, .marker.am-3 .marker-dot { fill: #fbc4b3; }
.country.am-4, .marker.am-4 .marker-dot { fill: #ee9a86; }

.country.oc-0, .marker.oc-0 .marker-dot { fill: #b49ce0; }
.country.oc-1, .marker.oc-1 .marker-dot { fill: #cbb6ee; }
.country.oc-2, .marker.oc-2 .marker-dot { fill: #9b81cf; }
.country.oc-3, .marker.oc-3 .marker-dot { fill: #dccbf5; }
.country.oc-4, .marker.oc-4 .marker-dot { fill: #a78ada; }

.country.an-0, .marker.an-0 .marker-dot,
.country.an-1, .marker.an-1 .marker-dot,
.country.an-2, .marker.an-2 .marker-dot,
.country.an-3, .marker.an-3 .marker-dot,
.country.an-4, .marker.an-4 .marker-dot { fill: #e4f1f8; }

/* ---------- 4. Scheda informativa ---------- */
.card {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 20;
  width: 268px;
  max-width: calc(100vw - 20px);
  padding: 14px 16px 6px;
  background: #fffdf7;
  border: 2px solid #f0dfbe;
  border-radius: var(--raggio);
  box-shadow: var(--ombra-morbida);
  /* Il posizionamento arriva da map.js tramite queste due variabili */
  transform: translate(var(--card-x, 0), var(--card-y, 0));
  transform-origin: var(--origine, 50% 100%);
  animation: apri-scheda 200ms cubic-bezier(.34, 1.4, .64, 1);
}

.card[hidden] { display: none; }

@keyframes apri-scheda {
  from { opacity: 0; scale: .88; }
  to   { opacity: 1; scale: 1; }
}

.card.is-closing {
  animation: chiudi-scheda 130ms ease-in forwards;
  pointer-events: none;
}

@keyframes chiudi-scheda {
  to { opacity: 0; scale: .9; }
}

.card-head {
  display: flex;
  align-items: center;
  gap: 11px;
  padding-right: 22px;
}

.card-flag {
  font-size: 2.1rem;
  line-height: 1;
}

.card-title { min-width: 0; }

.card-title h2 {
  margin: 0;
  font-size: 1.12rem;
  font-weight: 800;
  line-height: 1.2;
}

.card-title p {
  margin: 2px 0 0;
  font-size: .74rem;
  font-weight: 700;
  letter-spacing: .03em;
  text-transform: uppercase;
  color: var(--inchiostro-tenue);
}

.card-facts {
  margin: 12px 0 8px;
  display: grid;
  gap: 7px;
}

.card-facts > div {
  display: grid;
  grid-template-columns: 88px 1fr;
  gap: 8px;
  align-items: baseline;
}

.card-facts dt {
  font-size: .72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .03em;
  color: var(--inchiostro-tenue);
}

.card-facts dd {
  margin: 0;
  font-size: .87rem;
  font-weight: 600;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.card-close {
  position: absolute;
  top: 7px;
  right: 8px;
  width: 27px;
  height: 27px;
  display: grid;
  place-items: center;
  font-size: 1.25rem;
  line-height: 1;
  color: var(--inchiostro-tenue);
  background: var(--carta-scura);
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  transition: background var(--transizione), transform var(--transizione);
}

.card-close:hover {
  background: #e8d3ad;
  transform: rotate(90deg);
}

/* Freccina che collega la scheda al paese selezionato */
.card-arrow {
  position: absolute;
  width: 16px;
  height: 16px;
  background: #fffdf7;
  border: 2px solid #f0dfbe;
  transform: rotate(45deg);
}

.card-arrow[data-lato="sotto"] { border-width: 0 2px 2px 0; }
.card-arrow[data-lato="sopra"] { border-width: 2px 0 0 2px; }
.card-arrow[data-lato="destra"] { border-width: 0 0 2px 2px; }
.card-arrow[data-lato="sinistra"] { border-width: 2px 2px 0 0; }

/* ---------- 5. Controlli e tooltip ---------- */
.zoom {
  position: absolute;
  right: 14px;
  bottom: 14px;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.zoom button {
  width: 40px;
  height: 40px;
  font-size: 1.25rem;
  font-weight: 700;
  font-family: inherit;
  color: var(--inchiostro);
  background: #fffdf7;
  border: 2px solid #f0dfbe;
  border-radius: 13px;
  box-shadow: 0 4px 12px rgba(74, 59, 42, .16);
  cursor: pointer;
  transition: transform var(--transizione), background var(--transizione);
}

.zoom button:hover { background: var(--carta-scura); transform: translateY(-2px); }
.zoom button:active { transform: translateY(0) scale(.94); }

.tooltip {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 15;
  padding: 5px 11px;
  font-size: .82rem;
  font-weight: 700;
  white-space: nowrap;
  color: #fffdf7;
  background: rgba(74, 59, 42, .92);
  border-radius: 999px;
  pointer-events: none;
  opacity: 0;
  transform: translate(var(--tip-x, 0), var(--tip-y, 0));
  transition: opacity 120ms ease;
}

.tooltip.is-visible { opacity: 1; }

.hint {
  position: absolute;
  left: 50%;
  bottom: 10px;
  z-index: 10;
  margin: 0;
  padding: 5px 14px;
  font-size: .74rem;
  color: var(--inchiostro-tenue);
  background: rgba(255, 253, 247, .82);
  border-radius: 999px;
  transform: translateX(-50%);
  pointer-events: none;
  transition: opacity 400ms ease;
}

.hint.is-hidden { opacity: 0; }

:focus-visible {
  outline: 3px solid #4a3b2a;
  outline-offset: 2px;
}

/* ---------- Schermi piccoli ---------- */
@media (max-width: 640px) {
  .site-header { padding: 10px 14px 6px; }
  .site-header p { display: none; }
  .site-footer { font-size: .66rem; padding: 6px 14px 10px; }
  .card { width: 244px; padding: 12px 14px 4px; }
  .card-facts > div { grid-template-columns: 80px 1fr; }
  .zoom button { width: 36px; height: 36px; }
  .tooltip { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}
