/* matiadvent: stile globale e classi comuni ai giochi (prefisso g-). */
:root {
  --night: #1b2a49;
  --night-2: #24365c;
  --night-3: #2d4472;
  --snow: #f4f7fb;
  --berry: #c8325a;
  --berry-dark: #a3264a;
  --gold: #f2c14e;
  --frost: #9bb8d9;
  --pine: #2f7d55;
  --ink: #1b2a49;       /* testo sul chiaro */
  --muted: #4a5b7a;     /* testo secondario sul chiaro (contrasto > 6:1) */
  --radius: 10px;
  --font: "Fraunces", Georgia, "Times New Roman", serif;
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--night);
  color: var(--snow);
  font-family: var(--font);
  padding: 2rem 1rem 3rem;
}
header { text-align: center; max-width: 36rem; margin: 0 auto 2rem; }
h1 { font-size: clamp(2.4rem, 8vw, 3.8rem); margin: 0; font-weight: 700; letter-spacing: -0.02em; }
.who { margin: .2rem 0 0; color: var(--gold); font-size: 1.15rem; }
.who:empty { display: none; }
#status { margin: .6rem 0 0; color: var(--frost); font-size: 1.05rem; }
.toast {
  /* fisso in basso: non sposta la griglia ed è visibile anche a pagina scorsa */
  position: fixed; left: 50%; bottom: calc(1rem + env(safe-area-inset-bottom)); transform: translateX(-50%);
  width: max-content; max-width: calc(100vw - 2rem); margin: 0; padding: .6rem 1rem; border-radius: 8px; z-index: 5;
  color: var(--night); background: var(--gold); font: 600 1rem var(--ui); box-shadow: 0 4px 16px rgba(0,0,0,.35);
  opacity: 0; pointer-events: none; transition: opacity .25s ease;
}
.toast.show { opacity: 1; }
.m-confirm { margin-top: 1rem; padding: .8rem; border-radius: 8px; background: #fbeec8; text-align: center; font-family: var(--ui); }
.m-confirm p { margin: 0 0 .6rem; font-weight: 600; }
.m-confirm[hidden] { display: none; }

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

/* ------------------------------------------------------------ griglia */
.grid {
  list-style: none;
  margin: 0 auto;
  padding: 0;
  max-width: 44rem;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: .75rem;
}
@media (min-width: 560px) { .grid { grid-template-columns: repeat(6, 1fr); } }

.door {
  width: 100%;
  aspect-ratio: 1;
  border: 2px solid var(--frost);
  border-radius: 6px 6px 2px 2px;
  background: var(--night-2);
  color: var(--snow);
  font: inherit;
  font-size: 1.6rem;
  font-weight: 700;
  cursor: pointer;
  position: relative;
  transition: transform .15s ease, background .15s ease;
}
.door:hover:not(.locked) { transform: translateY(-2px); background: var(--night-3); }
.door:focus-visible, .close:focus-visible, .g-btn:focus-visible, .game :focus-visible {
  outline: 3px solid var(--gold); outline-offset: 3px;
}
/* nel dialog: contorno bianco + anello scuro, visibile sia sul chiaro sia su sfondi scuri o verdi */
dialog .g-btn:focus-visible, dialog .game :focus-visible, dialog .close:focus-visible, dialog .result-actions :focus-visible {
  outline: 3px solid #fff; outline-offset: 1px; box-shadow: 0 0 0 6px var(--night);
}
.door.locked { opacity: .45; cursor: not-allowed; border-style: dashed; }
.door.opened { background: transparent; border-color: var(--berry); color: var(--frost); }
.door.today { border-color: var(--gold); color: var(--gold); animation: glow 2.4s ease-in-out infinite; }
.door.today.opened { border-color: var(--berry); }
.door .tag { position: absolute; bottom: 4px; left: 0; right: 0; font-size: .7rem; color: var(--gold); }
.door .today-tag { position: absolute; top: 3px; left: 0; right: 0; font: 600 .75rem var(--ui); letter-spacing: .04em; text-transform: uppercase; }
@keyframes glow {
  0%, 100% { box-shadow: 0 0 0 0 rgba(242,193,78,0); }
  50% { box-shadow: 0 0 16px 3px rgba(242,193,78,.45); }
}

/* ------------------------------------------------------------ regalo */
.gift { max-width: 28rem; margin: 2.5rem auto 0; text-align: center; }
.gift h2 { margin: 0; font-size: 1.6rem; color: var(--gold); }
.gift-hint { margin: .3rem 0 1rem; color: var(--frost); }
.gift-frame {
  position: relative; aspect-ratio: 3 / 2; border-radius: var(--radius); overflow: hidden;
  border: 2px solid var(--gold); background: linear-gradient(135deg, var(--berry), var(--night-3));
}
.gift-fallback { position: absolute; inset: 0; display: grid; place-items: center; font-size: 5rem; }
#gift-img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.gift-tiles { position: absolute; inset: 0; display: grid; grid-template-columns: repeat(6, 1fr); grid-template-rows: repeat(4, 1fr); }
.gift-tile {
  background: var(--night-2); border: 1px solid var(--night);
  background-image: radial-gradient(circle at 50% 50%, rgba(242,193,78,.25) 0 2px, transparent 3px);
  transition: opacity .6s ease, transform .6s ease;
}
.gift-tile.gone { opacity: 0; transform: scale(.6); }
.gift-count { margin: .7rem 0 0; color: var(--snow); }

/* ------------------------------------------------------------ dialog */
dialog {
  border: 2px solid var(--gold);
  border-radius: var(--radius);
  background: var(--snow);
  color: var(--ink);
  width: min(34rem, 94vw);
  max-width: 94vw;
  max-height: 94vh;
  max-height: 94dvh;
  padding: 1rem 1.1rem 1.2rem;
  font-family: var(--font);
  overflow-y: auto;
  overscroll-behavior: contain;
}
@media (max-width: 559px) {
  dialog {
    width: 100vw; max-width: 100vw; height: 100vh; height: 100dvh; max-height: 100dvh;
    margin: 0; border-radius: 0; border-width: 0; padding: .8rem .9rem calc(1rem + env(safe-area-inset-bottom));
  }
}
dialog::backdrop { background: rgba(10,16,32,.8); }
.m-head { display: flex; align-items: flex-start; gap: .6rem; }
dialog h2 { margin: .3rem 0 0; font-size: 1.45rem; color: var(--berry-dark); flex: 1; line-height: 1.2; }
.close {
  flex: none; width: 44px; height: 44px; border-radius: 50%;
  background: var(--night); color: var(--snow); border: 0; font: 700 1.1rem var(--ui); cursor: pointer;
}
.instr { margin: .5rem 0 .8rem; font-size: 1.05rem; line-height: 1.45; color: var(--muted); }
.instr:empty { display: none; }
.game-area { position: relative; }
.m-actions { display: flex; gap: .6rem; justify-content: center; flex-wrap: wrap; margin-top: 1rem; }
.m-actions[hidden] { display: none; }

/* ------------------------------------------------------------ elementi comuni ai giochi */
.game { display: flex; flex-direction: column; align-items: center; gap: .8rem; font-family: var(--ui); }
.g-btn {
  min-height: 44px; min-width: 44px; padding: .55rem 1.1rem;
  border: 2px solid var(--night); border-radius: 8px;
  background: #fff; color: var(--ink);
  font: 600 1rem var(--ui); cursor: pointer;
  touch-action: manipulation;
}
.g-btn:hover:not(:disabled) { background: #e6edf7; }
.g-btn.primary { background: var(--berry); border-color: var(--berry); color: #fff; }
.g-btn.primary:hover:not(:disabled) { background: var(--berry-dark); }
.g-btn.skip { border-style: dashed; }
.g-btn:disabled { opacity: .5; cursor: default; }
.g-hud { margin: 0; font: 600 1rem var(--ui); color: var(--ink); text-align: center; min-height: 1.4em; }
.g-msg { margin: 0; font-size: 1.1rem; text-align: center; color: var(--ink); line-height: 1.45; }
.g-sub { margin: 0; color: var(--muted); text-align: center; }
.g-fail { display: flex; flex-direction: column; align-items: center; gap: .8rem; padding: 1.5rem 0; font-family: var(--ui); }
.g-canvas {
  display: block; width: 100%; max-width: 420px; height: auto; border-radius: 8px;
  background: var(--night); user-select: none; -webkit-user-select: none;
}
.g-board { display: grid; gap: 6px; width: 100%; max-width: 420px; }
.g-cell {
  aspect-ratio: 1; min-width: 44px; min-height: 44px; border-radius: 8px; border: 2px solid var(--night-2);
  background: #fff; color: var(--ink); font: 700 1.5rem var(--ui); cursor: pointer; display: grid; place-items: center;
  padding: 0; touch-action: manipulation;
}
.g-dpad {
  display: grid; grid-template-columns: repeat(3, 56px); grid-template-rows: repeat(3, 56px); gap: 4px;
  grid-template-areas: ". up ." "left . right" ". down .";
}
.g-dpad .g-btn { padding: 0; font-size: 1.2rem; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.g-dpad-up { grid-area: up; } .g-dpad-down { grid-area: down; }
.g-dpad-left { grid-area: left; } .g-dpad-right { grid-area: right; }
@media (pointer: fine) and (min-width: 560px) { .g-dpad.hide-on-desktop { display: none; } }

/* ------------------------------------------------------------ risultato */
.result { position: relative; text-align: center; overflow: hidden; padding-bottom: .4rem; }
.result-head { font: 700 1.35rem var(--font); color: var(--berry-dark); margin: .4rem 0 1rem; }
.result-head:focus { outline: none; }
.result-photo { display: block; max-width: 100%; max-height: 50vh; margin: 0 auto 1rem; border-radius: 8px; }
.result .msg, .result .letter { font-size: 1.15rem; line-height: 1.6; margin: 0 0 .8rem; color: var(--ink); }
.result .letter { text-align: left; }
.result-gift { font-size: 1.3rem; color: var(--pine); margin: 0 0 1rem; }
.result-actions { display: flex; gap: .6rem; justify-content: center; margin-top: 1rem; position: relative; z-index: 2; }

.g-confetti { position: absolute; inset: 0; pointer-events: none; overflow: hidden; z-index: 0; }
.result > :not(.g-confetti) { position: relative; z-index: 1; }
.g-confetti i {
  position: absolute; top: -12px; width: 8px; height: 12px; border-radius: 2px; opacity: .9;
  animation: fall linear forwards;
}
.g-confetti.still i { animation: none; opacity: .55; }
@keyframes fall {
  to { top: 105%; transform: rotate(540deg); opacity: .2; }
}

@media (prefers-reduced-motion: reduce) {
  .door, .gift-tile { transition: none; }
  .door.today { animation: none; box-shadow: 0 0 12px 2px rgba(242,193,78,.45); }
  .g-confetti i { animation: none; }
}
.g-btn[aria-disabled="true"] { opacity: .5; cursor: default; }
