/* ==========================================================================
   AG Meteo — base.css
   1. Design tokens (colori, spazi, tipografia) + tema chiaro/scuro
   2. Reset moderno
   3. Tipografia e utility
   4. Layout globale, header, footer
   5. Bottoni

   Strategia: TUTTO ciò che cambia con il tema è una CSS variable. Passare da chiaro a scuro
   significa cambiare `data-theme` su <html>: nessun JS di styling, nessun CSS duplicato.

   Contrasti (WCAG 2.2 AA verificati): testo normale ≥ 4.5:1, testo grande e componenti UI ≥ 3:1.
   ========================================================================== */

/* ---------- 1. TOKENS ---------- */
:root {
  color-scheme: light;

  /* Tipografia: stack di sistema = zero font da scaricare, rendering istantaneo e nativo. */
  --font: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif;
  --fs-xs: 0.8125rem;
  --fs-sm: 0.9375rem;
  --fs-md: 1rem;
  --fs-lg: 1.125rem;
  --fs-xl: clamp(1.5rem, 1.15rem + 1.5vw, 2.125rem);
  --fs-hero: clamp(3.75rem, 3rem + 3.4vw, 5.5rem);

  /* Scala spaziale (base 4px) */
  --s-1: 0.25rem;
  --s-2: 0.5rem;
  --s-3: 0.75rem;
  --s-4: 1rem;
  --s-5: 1.5rem;
  --s-6: 2rem;
  --s-7: 3rem;
  --s-8: 5rem;

  --r-sm: 0.5rem;
  --r-md: 0.875rem;
  --r-lg: 1.375rem;
  --r-pill: 999px;

  /* Movimento */
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  --dur-fast: 0.15s;
  --dur: 0.25s;

  --header-h: 4rem;
  --chat-w: 24.5rem; /* larghezza della chat laterale su desktop */

  /* Colori — TEMA CHIARO */
  --bg: #f2f7fb;
  --surface: #ffffff;
  --surface-2: #e6eff7;
  --border: #c9d9e8;
  --text: #0c1a2b; /* su --bg 16:1 */
  --text-muted: #41546a; /* su --surface 8:1, su --bg 7:1 */
  --text-muted-strong: #31465c; /* usato su fondi colorati (card meteo) */
  --primary: #17689f; /* blu del logo, scurito: su bianco 6:1 */
  --primary-hover: #125a88;
  --on-primary: #ffffff;
  --focus: #17689f;
  --input-bg: #ffffff;
  --input-border: #6b82a0; /* bordo dei campi: ≥ 3:1 su bianco (WCAG 1.4.11) */
  --warning: #7a4b00;
  --warning-bg: #fff1cf;
  --danger: #b42318;
  --danger-bg: #fdecea;
  --success: #067647;
  --success-bg: #e6f6ee;
  --info-bg: #e1f0fa;

  --shadow-sm: 0 1px 2px rgb(15 23 42 / 0.06), 0 1px 3px rgb(15 23 42 / 0.05);
  --shadow-md: 0 6px 18px rgb(15 23 42 / 0.08), 0 2px 6px rgb(15 23 42 / 0.05);
  --shadow-lg: 0 24px 60px rgb(15 23 42 / 0.2);
  --scrim: rgb(15 23 42 / 0.5);

  /* Sfondi card meteo per condizione (testo scuro: contrasto > 9:1) */
  --grad-sunny: linear-gradient(135deg, #fff5d9, #ffdf9a);
  --grad-partly: linear-gradient(135deg, #eaf3ff, #ffefc8);
  --grad-cloudy: linear-gradient(135deg, #eef2f8, #d6deeb);
  --grad-rain: linear-gradient(135deg, #e6eefc, #bcd2f2);
  --grad-storm: linear-gradient(135deg, #e3e5f4, #c0c5e4);
  --grad-snow: linear-gradient(135deg, #eef4fb, #d3e1f2);
  --grad-fog: linear-gradient(135deg, #f0f2f4, #d9dde2);

  /* Icone meteo (≥ 3:1 sui fondi sopra) */
  --ic-sun: #c25e05;
  --ic-cloud: #5a6a82;
  --ic-rain: #1d55d6;
  --ic-bolt: #854d0e;
  --ic-moon: #475a8c;
  --ic-snow: #3a64a8;
  --ic-fog: #5d6b80;
}

[data-theme='dark'] {
  color-scheme: dark;

  --bg: #07131f;
  --surface: #0d2033;
  --surface-2: #14304a;
  --border: #24415c;
  --text: #e9f2fa; /* su --surface 14:1 */
  --text-muted: #a9bdd1; /* su --surface 8:1 */
  --text-muted-strong: #c3d3e3;
  --primary: #5ebbe6; /* azzurro del logo: su --surface 7.6:1 */
  --primary-hover: #8ccff0;
  --on-primary: #06131f;
  --focus: #5ebbe6;
  --input-bg: #09182a;
  --input-border: #7491b0;
  --warning: #fcd34d;
  --warning-bg: #3a2c0a;
  --danger: #ff9b8f;
  --danger-bg: #3a1a1a;
  --success: #6ee7a8;
  --success-bg: #11301f;
  --info-bg: #10304a;

  --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.4);
  --shadow-md: 0 8px 22px rgb(0 0 0 / 0.4);
  --shadow-lg: 0 24px 60px rgb(0 0 0 / 0.6);
  --scrim: rgb(2 6 16 / 0.7);

  --grad-sunny: linear-gradient(135deg, #3a2a0b, #5a3f0e);
  --grad-partly: linear-gradient(135deg, #10304a, #4a3a14);
  --grad-cloudy: linear-gradient(135deg, #12293f, #1f3a55);
  --grad-rain: linear-gradient(135deg, #0e2a4a, #174a73);
  --grad-storm: linear-gradient(135deg, #161d3a, #27305a);
  --grad-snow: linear-gradient(135deg, #16283d, #2a4260);
  --grad-fog: linear-gradient(135deg, #1c2530, #313c4a);

  --ic-sun: #fbbf24;
  --ic-cloud: #b9c5d9;
  --ic-rain: #86b6ff;
  --ic-bolt: #fcd34d;
  --ic-moon: #d5defa;
  --ic-snow: #cfe3ff;
  --ic-fog: #c0cad8;
}

/* ---------- 2. RESET ---------- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scroll-behavior: smooth;
  scroll-padding-top: calc(var(--header-h) + var(--s-4));
}

body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font);
  font-size: var(--fs-md);
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* Transizione fluida del tema: crossfade dell'intera pagina via View Transitions API (vedi theme.js).
   Nessuno stato intermedio incoerente, quindi i contrasti restano sempre corretti. */
::view-transition-old(root),
::view-transition-new(root) {
  animation-duration: 0.4s;
  animation-timing-function: var(--ease);
}

img,
svg {
  display: block;
  max-width: 100%;
}
h1, h2, h3, h4, p, ul, ol, dl, dd, figure, fieldset {
  margin: 0;
  padding: 0;
}
ul[role='list'], ol[role='list'], ul.reset {
  list-style: none;
}
button, input, select, textarea {
  font: inherit;
  color: inherit;
}
button {
  cursor: pointer;
}
a {
  color: var(--primary);
  text-underline-offset: 0.2em;
  text-decoration-thickness: 0.08em;
}
a:hover {
  color: var(--primary-hover);
}
[hidden] {
  display: none !important;
}

/* Focus visibile, sempre: requisito WCAG 2.4.7 */
:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  border-radius: 4px;
}

/* "Riduci animazioni": le animazioni si DISATTIVANO (non si accorciano: una transizione da 0,01 ms su ogni
   proprietà farebbe restare per un frame, ad esempio, la visibilità sul valore iniziale). Gli stati finali
   restano quelli normali (le animazioni di ingresso usano fill "both"). */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation: none !important; transition: none !important; }
  /* Eccezione: il "failsafe" che mostra la pagina se la traduzione non parte */
  html.i18n-pending body { animation: i18n-failsafe 0s 1.5s forwards !important; }
}

/* Lingua non italiana: la pagina resta nascosta finché i testi non sono tradotti (evita il "lampo" in italiano).
   Se per qualsiasi motivo la traduzione non parte, dopo 1,5 s la pagina compare comunque. */
html.i18n-pending body { visibility: hidden; animation: i18n-failsafe 0s 1.5s forwards; }
@keyframes i18n-failsafe { to { visibility: visible; } }

/* Contenitore di header + contenuto + footer. Su desktop, con la chat laterale aperta, si restringe a sinistra
   (margin-right) lasciando la colonna destra alla chat: nulla finisce nascosto sotto di essa. */
.page { position: relative; z-index: 1; transition: margin-right 0.4s var(--ease); }

/* ---------- 3. TIPOGRAFIA E UTILITY ---------- */
h1, h2, h3 {
  line-height: 1.15;
  letter-spacing: -0.02em;
  text-wrap: balance;
}
.h1 { font-size: var(--fs-xl); font-weight: 750; }
.h2 { font-size: clamp(1.375rem, 1.1rem + 1vw, 1.75rem); font-weight: 720; }
.h3 { font-size: var(--fs-lg); font-weight: 680; }
.muted { color: var(--text-muted); }
.eyebrow {
  font-size: var(--fs-xs);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--primary);
}
small { font-size: 0.85em; }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.container {
  width: min(100% - 2rem, 76rem);
  margin-inline: auto;
}
@media (min-width: 40rem) {
  .container { width: min(100% - 3rem, 76rem); }
}

.icon { width: 1.25em; height: 1.25em; flex: none; }

.skip-link {
  position: fixed;
  top: var(--s-2);
  left: var(--s-2);
  z-index: 100;
  padding: var(--s-3) var(--s-4);
  border-radius: var(--r-sm);
  background: var(--primary);
  color: var(--on-primary);
  font-weight: 650;
  transform: translateY(-200%);
  transition: transform var(--dur) var(--ease);
}
.skip-link:focus { transform: none; color: var(--on-primary); }

/* ---------- 4. HEADER & FOOTER ---------- */
.site-header {
  position: sticky;
  top: 0;
  z-index: 40;
  height: var(--header-h);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: saturate(1.4) blur(14px);
  -webkit-backdrop-filter: saturate(1.4) blur(14px);
  border-bottom: 1px solid var(--border);
}
.site-header__inner {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-3);
}
.site-header__actions { display: flex; align-items: center; gap: var(--s-2); }
@media (max-width: 26rem) { .site-header__actions { gap: var(--s-1); } }

.brand {
  flex: none;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: var(--s-3);
  color: var(--text);
  text-decoration: none;
  font-weight: 760;
  font-size: var(--fs-lg);
  letter-spacing: -0.02em;
}
.brand:hover { color: var(--text); }
.brand img { width: auto; height: 2.1rem; transition: transform var(--dur) var(--ease); }
.brand:hover img { transform: rotate(-4deg) scale(1.06); }

/* Il toggle mostra sole o luna in base al tema attivo */
#theme-toggle .icon { grid-area: 1 / 1; transition: transform var(--dur) var(--ease), opacity var(--dur) var(--ease); }
#theme-toggle { display: inline-grid; place-items: center; }
#theme-toggle [data-icon='moon'] { display: contents; }
[data-theme='light'] #theme-toggle [data-icon='sun'] .icon { opacity: 0; transform: rotate(-90deg) scale(0.5); }
[data-theme='dark'] #theme-toggle [data-icon='moon'] .icon { opacity: 0; transform: rotate(90deg) scale(0.5); }
#theme-toggle [data-icon='sun'] { display: contents; }

.site-footer {
  margin-top: var(--s-8);
  background: var(--surface);
  border-top: 1px solid var(--border);
  font-size: var(--fs-sm);
}
.footer__grid { display: grid; grid-template-columns: 1fr; gap: var(--s-6) var(--s-5); padding-block: var(--s-7) var(--s-6); }
@media (min-width: 40rem) {
  .footer__grid { grid-template-columns: repeat(3, 1fr); }
  .footer__brand { grid-column: 1 / -1; }
}
@media (min-width: 64rem) {
  .footer__grid { grid-template-columns: 1.7fr repeat(3, 1fr); gap: var(--s-6); }
  .footer__brand { grid-column: auto; }
}
.footer__brand a { display: inline-block; }
.footer__brand img { width: 7rem; height: auto; }
@media (max-width: 47.99rem) { .footer__brand img { width: 5rem; } } /* telefono: logo più piccolo */
.footer__brand p { max-width: 22rem; margin-top: var(--s-3); color: var(--text-muted); }
.footer__brand strong { color: var(--text); }
.footer__col h2 { margin-bottom: var(--s-3); font-size: 0.75rem; font-weight: 750; letter-spacing: 0.08em; text-transform: uppercase; color: var(--text-muted); }
.footer__col ul { display: grid; gap: 0.1rem; list-style: none; }
.site-footer a, .link-button {
  display: inline-block;
  padding: 0.35rem 0; /* area di tocco più comoda */
  color: var(--text);
  text-decoration: none;
  background: none;
  border: 0;
  font-size: inherit;
  font-weight: 550;
  transition: color var(--dur-fast), transform var(--dur-fast) var(--ease);
}
.site-footer a:hover, .link-button:hover { color: var(--primary); transform: translateX(3px); }
.footer__bottom {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-3) var(--s-5);
  padding-block: var(--s-4);
  border-top: 1px solid var(--border);
  font-size: var(--fs-xs);
  color: var(--text-muted);
}
.footer__bottom a { color: var(--text-muted); }
.footer__top { display: inline-flex; align-items: center; gap: var(--s-2); }
.footer__top [data-icon] { display: inline-flex; }
.footer__top .icon { width: 1em; height: 1em; }

/* ---------- 5. BOTTONI ---------- */
.btn {
  --btn-bg: var(--surface);
  --btn-fg: var(--text);
  --btn-bd: var(--border);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s-2);
  min-height: 2.75rem; /* 44px: target touch WCAG */
  padding: 0.55rem 1.1rem;
  border: 1px solid var(--btn-bd);
  border-radius: var(--r-md);
  background: var(--btn-bg);
  color: var(--btn-fg);
  font-weight: 650;
  font-size: var(--fs-sm);
  line-height: 1.2;
  text-decoration: none;
  cursor: pointer;
  transition: background-color var(--dur-fast), border-color var(--dur-fast), color var(--dur-fast),
    transform var(--dur-fast) var(--ease), box-shadow var(--dur) var(--ease);
}
.btn:hover { --btn-bd: color-mix(in srgb, var(--text) 35%, var(--border)); color: var(--btn-fg); }
.btn:active { transform: scale(0.97); }
.btn:disabled, .btn[aria-disabled='true'] { opacity: 0.55; cursor: not-allowed; transform: none; }

.btn--primary {
  --btn-bg: var(--primary);
  --btn-fg: var(--on-primary);
  --btn-bd: var(--primary);
}
.btn--primary:hover { --btn-bg: var(--primary-hover); --btn-bd: var(--primary-hover); box-shadow: var(--shadow-md); }
.btn--secondary { --btn-bg: var(--surface); }
.btn--secondary:hover { --btn-bg: var(--surface-2); }
.btn--ghost { --btn-bg: transparent; --btn-bd: transparent; }
.btn--ghost:hover { --btn-bg: var(--surface-2); --btn-bd: transparent; }
.btn--icon { width: 2.75rem; padding: 0; }
.btn--sm { min-height: 2.25rem; padding: 0.35rem 0.8rem; }
.btn--block { width: 100%; }

.btn[aria-busy='true'] { position: relative; color: transparent; pointer-events: none; }
.btn[aria-busy='true']::after {
  content: '';
  position: absolute;
  inset: 0;
  margin: auto;
  width: 1.1rem; height: 1.1rem;
  border: 2px solid var(--btn-fg);
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes rise { from { opacity: 0; transform: translateY(10px); } }
@keyframes fade { from { opacity: 0; } }

.avatar {
  display: inline-grid;
  place-items: center;
  width: 1.5rem; height: 1.5rem;
  border-radius: 50%;
  background: var(--primary);
  color: var(--on-primary);
  font-size: 0.8rem;
  font-weight: 750;
}

/* Toast */
.toast-region {
  position: fixed;
  inset: auto 0 calc(var(--s-5) + env(safe-area-inset-bottom)) 0;
  z-index: 90;
  display: grid;
  justify-items: center;
  gap: var(--s-2);
  pointer-events: none;
  padding-inline: var(--s-4);
}
.toast {
  max-width: 28rem;
  padding: var(--s-3) var(--s-4);
  border-radius: var(--r-md);
  background: var(--text);
  color: var(--bg);
  font-size: var(--fs-sm);
  font-weight: 550;
  box-shadow: var(--shadow-lg);
  animation: toast-in 0.3s var(--ease);
}
.toast--out { animation: toast-out 0.25s var(--ease) forwards; }
@keyframes toast-in { from { opacity: 0; transform: translateY(12px) scale(0.97); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(8px); } }

/* Controlli visibili solo su schermi larghi (su telefono vivono nel menu Impostazioni) e viceversa */
@media (max-width: 47.99rem) {
  .only-desktop { display: none !important; }
}
@media (min-width: 48rem) {
  .only-mobile { display: none !important; }
}

/* ---------- Icone meteo (condivise da tutte le pagine) ---------- */
.wicon { flex: none; overflow: visible; }
.wi-sun { fill: var(--ic-sun); }
.wi-ray { fill: none; stroke: var(--ic-sun); stroke-width: 1.7; stroke-linecap: round; }
.wi-cloud { fill: var(--ic-cloud); }
.wi-rain { fill: none; stroke: var(--ic-rain); stroke-width: 1.9; stroke-linecap: round; }
.wi-bolt { fill: var(--ic-bolt); }
.wi-moon { fill: var(--ic-moon); }
.wi-snow { fill: none; stroke: var(--ic-snow); stroke-width: 2.8; stroke-linecap: round; }
.wi-fog { fill: none; stroke: var(--ic-fog); stroke-width: 1.9; stroke-linecap: round; }


/* Colori dei grafici (stessa famiglia delle icone meteo) */
:root { --chart-temp: var(--ic-sun); --chart-rain: var(--ic-rain); --chart-hum: #0b7f86; }
[data-theme='dark'] { --chart-hum: #5fd3d0; }


/* Scritture non latine (cinese, giapponese, hindi): niente spaziatura tra le lettere, né positiva né negativa
   (rovina i caratteri larghi e, in devanagari, spezza i legamenti tra le lettere) */
:lang(zh) *, :lang(ja) *, :lang(hi) * { letter-spacing: normal !important; }
:lang(zh) body, :lang(ja) body { word-break: normal; line-break: strict; overflow-wrap: anywhere; }
