/* ==========================================================================
   Memory di Natale – Crea Solution
   Le durate delle animazioni (--t-*) vengono impostate da js/game.js
   leggendo js/config.js: non modificarle qui.
   ========================================================================== */

@font-face {
  font-family: "Poppins";
  src: url("../assets/fonts/poppins-bold-italic.woff") format("woff");
  font-weight: 700;
  font-style: italic;
  font-display: swap;
}

:root {
  --navy: #020367;
  --lavender: #8383b4;
  --ice: #ceebfd;
  --white: #ffffff;
  --gold: #f2c14e;

  --font-ui: "Poppins", system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  --font-script: "Great Vibes", "Snell Roundhand", "Apple Chancery", cursive;

  --t-flip: 520ms;
  --t-vanish: 560ms;
  --t-fade: 700ms;
  --ease-flip: cubic-bezier(0.45, 0.05, 0.25, 1);
  --ease-out: cubic-bezier(0.2, 0.8, 0.2, 1);

  /* Colonna "telefono": a tutto schermo su smartphone, 9:16 centrata su schermi larghi */
  --col-h: 100vh;
  --col-h: 100dvh;
  --col-w: min(100vw, calc(var(--col-h) * 9 / 16));
  --col-r: 0px;
}

@media (min-width: 600px) and (min-height: 600px) and (min-aspect-ratio: 3/4) {
  :root {
    --col-h: calc(100vh - 48px);
    --col-h: calc(100dvh - 48px);
    --col-r: 28px;
  }
}

*,
*::before,
*::after { box-sizing: border-box; }

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--navy);
}

/* Su schermi larghi lo sfondo esterno è un po' più scuro, così la colonna resta riconoscibile */
@media (min-width: 600px) and (min-height: 600px) and (min-aspect-ratio: 3/4) {
  html, body { background: radial-gradient(ellipse at center, #0a0c7a 0%, #01023f 100%); }
}

body {
  position: fixed;
  inset: 0;
  font-family: var(--font-ui);
  color: var(--navy);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

img { -webkit-user-drag: none; }

.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* --------------------------------------------------------------------------
   Livelli: sfondo colonna (0) → particelle (1) → contenuto (2)
   -------------------------------------------------------------------------- */
.stage-bg,
.stage {
  position: fixed;
  left: 50%;
  top: 50%;
  width: var(--col-w);
  height: var(--col-h);
  transform: translate(-50%, -50%);
  border-radius: var(--col-r);
  overflow: hidden;
  container-type: size;
}

.stage-bg {
  z-index: 0;
  background: var(--white);
  color: var(--ice);
  transition: background-color var(--t-fade) ease, color var(--t-fade) ease;
}

@media (min-width: 600px) and (min-height: 600px) and (min-aspect-ratio: 3/4) {
  .stage-bg { box-shadow: 0 30px 80px rgba(0, 0, 0, 0.35); }
}

#fx {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  pointer-events: none;
}

.stage { z-index: 2; }

body[data-theme="dark"] .stage-bg {
  background: var(--navy);
  color: var(--lavender);
}

/* Fiocchi grandi decorativi, ruotano lentamente */
.deco {
  position: absolute;
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  animation: spin 140s linear infinite;
  opacity: 0.95;
}
.deco--1 { left: 31%; top: 5.5%;  width: 30cqw; }
.deco--2 { left: 90%; top: 9%;    width: 36cqw; animation-direction: reverse; animation-duration: 170s; }
.deco--3 { left: 10%; top: 9%;    width: 10cqw; animation-duration: 90s; }
.deco--4 { left: 18%; top: 21%;   width: 19cqw; animation-direction: reverse; animation-duration: 110s; }
.deco--5 { left: 2%;  top: 41%;   width: 48cqw; animation-duration: 200s; }
.deco--6 { left: 98%; top: 45%;   width: 42cqw; animation-direction: reverse; }
.deco--7 { left: 9%;  top: 87%;   width: 22cqw; animation-duration: 120s; }
.deco--8 { left: 92%; top: 92%;   width: 52cqw; animation-direction: reverse; animation-duration: 220s; }

@keyframes spin {
  from { transform: translate(-50%, -50%) rotate(0deg); }
  to   { transform: translate(-50%, -50%) rotate(360deg); }
}

/* --------------------------------------------------------------------------
   Schermate
   -------------------------------------------------------------------------- */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding:
    max(env(safe-area-inset-top), 2cqh)
    max(env(safe-area-inset-right), 0px)
    max(env(safe-area-inset-bottom), 4cqh)
    max(env(safe-area-inset-left), 0px);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--t-fade) ease, visibility 0s linear var(--t-fade);
}

.screen.is-active {
  opacity: 1;
  visibility: visible;
  transition: opacity var(--t-fade) ease, visibility 0s;
}

/* ---------- Gioco ---------- */
.game-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: min(2.6cqh, 5cqw);
  margin-bottom: min(4.6cqh, 8cqw);
}

.logo { display: block; object-fit: contain; }
.logo--game { height: min(9cqh, 17cqw); width: auto; max-width: 60cqw; }

.game-title {
  margin: 0;
  font-weight: 700;
  font-style: italic;
  font-size: min(4.6cqw, 2.6cqh);
  line-height: 1.22;
  text-align: center;
  color: var(--navy);
  white-space: pre-line;
  letter-spacing: -0.005em;
}

.board {
  --board-w: min(84cqw, 47cqh);
  --gap: calc(var(--board-w) * 0.032);
  --card-r: calc(var(--board-w) * 0.034);
  width: var(--board-w);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--gap);
  opacity: 0;
  transition: opacity 300ms ease;
}
.board.is-ready { opacity: 1; }

/* ---------- Carta ---------- */
.card {
  position: relative;
  aspect-ratio: 205 / 240;
  padding: 0;
  margin: 0;
  border: 0;
  background: none;
  cursor: pointer;
  perspective: 900px;
  -webkit-tap-highlight-color: transparent;
  outline: none;
}

.card:focus-visible::after {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: calc(var(--card-r) + 4px);
  outline: 3px solid var(--gold);
  pointer-events: none;
}

.card-inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  -webkit-transform-style: preserve-3d;
  transition: transform var(--t-flip) var(--ease-flip);
}

.card.is-flipped .card-inner { transform: rotateY(180deg); }

.face {
  position: absolute;
  inset: 0;
  border-radius: var(--card-r);
  overflow: hidden;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  background: var(--navy);
  box-shadow: 0 2px 6px rgba(2, 3, 103, 0.16);
  transition: box-shadow var(--t-flip) ease, filter 200ms ease;
}

.face img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}

.face--front {
  transform: rotateY(180deg);
  background: var(--white);
}

/* Piccolo "sollevamento" durante la rotazione */
.card.is-lifting { animation: lift var(--t-flip) var(--ease-flip); }
.card.is-lifting .face { box-shadow: 0 10px 22px rgba(2, 3, 103, 0.28); }

@keyframes lift {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.08); }
  100% { transform: scale(1); }
}

@media (hover: hover) and (pointer: fine) {
  .card:not(.is-flipped):hover .face--back { filter: brightness(1.25); }
}

/* Coppia trovata: bagliore e scomparsa */
.card.is-matched {
  pointer-events: none;
  animation: vanish var(--t-vanish) var(--ease-out) forwards;
}
.card.is-matched .face--front { box-shadow: 0 0 0 3px var(--gold), 0 0 24px 6px rgba(242, 193, 78, 0.7); }

@keyframes vanish {
  0%   { transform: scale(1);    opacity: 1; }
  30%  { transform: scale(1.1);  opacity: 1; }
  100% { transform: scale(0.15) rotate(10deg); opacity: 0; visibility: hidden; }
}

/* Carte che entrano all'inizio della partita */
.card.is-dealing {
  animation: deal 520ms var(--ease-out) backwards;
  animation-delay: calc(var(--i, 0) * 38ms);
}

@keyframes deal {
  from { opacity: 0; transform: translateY(14px) scale(0.82); }
  to   { opacity: 1; transform: none; }
}

/* ---------- Schermata finale ---------- */
.screen--final { color: var(--white); }

.logo--final {
  width: min(19cqw, 11cqh);
  height: auto;
  margin-bottom: min(5.5cqh, 10cqw);
}

.final-message {
  margin: 0;
  font-family: var(--font-script);
  font-weight: 400;
  font-size: min(11cqw, 6.2cqh);
  line-height: 1.12;
  text-align: center;
  white-space: pre-line;
  color: var(--white);
}

.btn-replay {
  margin-top: min(8cqh, 14cqw);
  min-height: 44px;
  padding: 0.7em 1.5em;
  display: inline-flex;
  align-items: center;
  gap: 0.55em;
  font: 700 italic min(3.5cqw, 2cqh) / 1 var(--font-ui);
  font-size: max(14px, min(3.5cqw, 2cqh));
  color: var(--white);
  background: rgba(255, 255, 255, 0.08);
  border: 1.5px solid rgba(255, 255, 255, 0.55);
  border-radius: 999px;
  cursor: pointer;
  transition: background-color 200ms ease, border-color 200ms ease, transform 150ms ease;
}
.btn-replay svg { width: 1.1em; height: 1.1em; }
.btn-replay:hover { background: rgba(255, 255, 255, 0.16); border-color: var(--white); }
.btn-replay:active { transform: scale(0.96); }
.btn-replay:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }

/* Entrata a cascata degli elementi finali */
.screen--final > * { opacity: 0; transform: translateY(16px); }
.screen--final.is-active > * {
  opacity: 1;
  transform: none;
  transition: opacity 800ms var(--ease-out), transform 800ms var(--ease-out);
}
.screen--final.is-active > :nth-child(1) { transition-delay: 250ms; }
.screen--final.is-active > :nth-child(2) { transition-delay: 550ms; }
.screen--final.is-active > :nth-child(3) { transition-delay: 1300ms; }

/* --------------------------------------------------------------------------
   Movimento ridotto (impostazione di accessibilità del dispositivo)
   -------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  .deco { animation: none; }
  .card.is-lifting,
  .card.is-dealing { animation: none; }
  .card.is-matched { animation: fade-out var(--t-vanish) ease forwards; }
  .screen--final > *,
  .screen--final.is-active > * { transform: none; transition-delay: 0ms !important; }
}

@keyframes fade-out { to { opacity: 0; visibility: hidden; } }
