:root {
  /* tavolozza: i temi possono ridefinire --wall, --wall-2, --accent, --text, --muted, --glow (theme.json -> palette) */
  --wall: #0d1226;
  --wall-2: #1a2142;
  --accent: #e3bd6a;
  --text: #f6efe2;
  --muted: #b7bfdd;
  --glow: 255, 196, 102;
  --cream: var(--text);
  --gold: var(--accent);
  --ink: #2a2230;
  --serif: 'Cormorant Garamond', Georgia, 'Times New Roman', serif;
  --script: 'Great Vibes', 'Snell Roundhand', cursive;
  --hand: 'Caveat', 'Bradley Hand', cursive;
  --ui: system-ui, -apple-system, 'Segoe UI', sans-serif;
  --title-font: var(--script);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  /* una parete scura, come in una sala di museo a luci basse */
  background: radial-gradient(120% 80% at 50% 30%, var(--wall-2), var(--wall) 70%) fixed;
  background-color: var(--wall);
  color: var(--text);
  font-family: var(--ui);
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}
body.locked { overflow: hidden; }
button { font: inherit; color: inherit; cursor: pointer; }
#snow { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 5; }

/* ---------- titolo ---------- */
.scene { position: relative; z-index: 2; padding: 3vh 12px 4vh; display: flex; flex-direction: column; align-items: center; }
.title { text-align: center; margin-bottom: 14px; }
.title h1 {
  font-family: var(--title-font); font-weight: 400; margin: 0;
  font-size: clamp(2.4rem, 8vw, 4rem); color: var(--text);
  text-shadow: 0 0 18px rgba(var(--glow), .3);
}
.status { margin: 4px 0 0; font-family: var(--serif); font-style: italic; font-size: 1.1rem; color: var(--muted); min-height: 1.5em; }
.credit { margin: 14px 0 0; font-family: var(--serif); font-style: italic; font-size: .9rem; color: var(--muted); opacity: .8; text-align: center; }

/* ---------- cornice e quadro ---------- */
.frame {
  --ar: .6667; /* larghezza/altezza del quadro, da theme.json */
  --fw: clamp(14px, 5vw, 46px); /* spessore cornice; ogni stile lo scala con --fk */
  /* tutto il quadro deve stare nello schermo: su telefono comanda la larghezza, su computer l'altezza */
  width: min(100%, 560px, calc((100svh - 150px) * var(--ar)));
  box-shadow: 0 26px 50px rgba(0,0,0,.6), 0 6px 12px rgba(0,0,0,.4);
}
@media (min-width: 900px) { .frame { width: min(100%, 900px, calc((100svh - 150px) * var(--ar))); } }
.painting {
  position: relative; width: 100%; aspect-ratio: var(--ar);
  container-type: inline-size; /* i numeri scalano con la larghezza del quadro (cqw) */
  background: #000;
}
.painting::after { /* la cornice sporge: ombra portata sul bordo del quadro (non blocca i tocchi) */
  content: ''; position: absolute; inset: 0; pointer-events: none; z-index: 3;
  box-shadow: inset 0 5px 9px -2px rgba(0,0,0,.55), inset 4px 0 8px -3px rgba(0,0,0,.4), inset -2px 0 5px -3px rgba(0,0,0,.3);
}
.painting > img.base {
  position: absolute; inset: 0; width: 100%; height: 100%; display: block;
  user-select: none; -webkit-user-drag: none; transition: opacity 1.2s ease;
}
.painting.loading > img.base { opacity: 0; }


/* ---------- cornici (immagini generate da tools/make_frames.py, fascia = 150px) ---------- */
.framed {
  --fk: 1;
  border: calc(var(--fw) * var(--fk)) solid #6b4e1f; /* colore di riserva se l'immagine non carica */
  border-image: var(--frame-img) 150 stretch;
  background: var(--mat, transparent);
  padding: var(--mat-w, 0);
}
.framed > img, .framed > .painting { position: relative; }
.framed[data-frame="oro-semplice"] { --frame-img: url("frames/oro-semplice.webp"); }
.framed[data-frame="oro-barocco"]  { --frame-img: url("frames/oro-barocco.webp"); --fk: 1.3; }
.framed[data-frame="oro-anticato"] { --frame-img: url("frames/oro-anticato.webp"); }
.framed[data-frame="ebano-onde"]   { --frame-img: url("frames/ebano-onde.webp"); --fk: 1.1; }
.framed[data-frame="noce"]         { --frame-img: url("frames/noce.webp"); --fk: .9; }
.framed[data-frame="argento-deco"] { --frame-img: url("frames/argento-deco.webp"); }
.framed[data-frame="legno-chiaro"] { --frame-img: url("frames/legno-chiaro.webp"); --fk: .55; }
.framed[data-frame="galleria"]     { --frame-img: url("frames/galleria.webp"); --fk: .45; }
/* passe-partout: cartoncino con taglio a 45° (il bordo chiaro del taglio + ombra della cornice) */
.framed[data-mat] { --mat-w: calc(var(--fw) * 1.1); }
.framed[data-mat="crema"]  { --mat: #efe6d2; }
.framed[data-mat="bianco"] { --mat: #f7f5f0; }
.framed[data-mat="lino"]   { --mat: repeating-linear-gradient(0deg, #e4dccb 0 2px, #ddd4c1 2px 3px), #e4dccb; }
.framed[data-mat] > img, .framed[data-mat] > .painting {
  box-shadow: 0 0 0 2px rgba(255,255,255,.85), 0 0 0 3px rgba(0,0,0,.12), -1px -1px 3px 3px rgba(0,0,0,.08);
}
.m-frame.framed > img { display: block; }

/* ---------- finestre (e porta): left/top/width/height in % dal theme.json ---------- */
.win { position: absolute; padding: 0; border: 0; background: none; }
.win::before { /* area di tocco minima ~40px anche se nel quadro la finestra è piccola */
  content: ''; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: max(100%, 40px); height: max(100%, 40px);
}
.light { /* ritaglio della versione "accesa" esattamente sopra la finestra */
  position: absolute; inset: 0; border-radius: 2px;
  background-image: var(--lit); background-size: var(--bgsize); background-position: var(--bgpos); background-repeat: no-repeat;
  opacity: 0; transition: opacity 1.4s ease, box-shadow 1.4s ease;
}
.win.unlocked .light { animation: candle 3.2s ease-in-out infinite; }
.win.today.unlocked .light { animation-duration: 2s; }
@keyframes candle { 0%, 100% { opacity: .08; } 45% { opacity: .42; } 55% { opacity: .3; } 65% { opacity: .45; } }
.win.open .light { opacity: 1; box-shadow: 0 0 2.2cqw .6cqw rgba(var(--glow), .55); animation: none; }
.win.igniting .light { animation: ignite 1.3s ease-out forwards; }
@keyframes ignite {
  0% { opacity: .1; } 18% { opacity: .7; } 28% { opacity: .35; } 45% { opacity: .9; } 60% { opacity: .6; }
  100% { opacity: 1; box-shadow: 0 0 2.2cqw .6cqw rgba(var(--glow), .55); }
}
.win-num {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  min-width: 1.9em; padding: .05em .35em; border-radius: 999px;
  font-family: var(--serif); font-weight: 500; font-size: max(11px, 2.6cqw); line-height: 1.4; text-align: center;
  color: var(--text); background: rgba(10, 12, 30, .55); box-shadow: 0 0 0 1px rgba(255,255,255,.15);
  text-shadow: 0 1px 2px #000; pointer-events: none; transition: all .6s ease;
}
.win.locked .win-num { opacity: .55; }
.win.today.unlocked .win-num { background: rgba(var(--glow), .85); color: #2a1a05; text-shadow: none; box-shadow: 0 0 12px rgba(var(--glow), .8); }
.win.open .win-num { top: auto; bottom: 0; transform: translate(-50%, 50%) scale(.8); opacity: .8; background: rgba(10,12,30,.7); }
.win.door .win-num { font-size: max(12px, 3.2cqw); color: var(--accent); }
.shake { animation: shake .45s; }
@keyframes shake { 20%, 60% { transform: translateX(-3px); } 40%, 80% { transform: translateX(3px); } }
.win:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

/* ---------- scelta del quadro ---------- */
.theme-btn {
  margin-top: 14px; padding: 9px 18px; border-radius: 999px;
  border: 1px solid rgba(var(--glow), .45); background: rgba(255,255,255,.04);
  font-family: var(--serif); font-style: italic; font-size: 1.05rem; color: var(--accent);
}
.theme-btn:hover { background: rgba(var(--glow), .12); }
.theme-btn[hidden], .theme-menu[hidden] { display: none; }
.theme-menu {
  position: fixed; inset: 0; z-index: 10; display: flex; align-items: center; justify-content: center;
  background: rgba(5, 9, 24, .86); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
  padding: 56px 16px 24px; overflow-y: auto; animation: fade .4s ease both;
}
.t-card { width: min(100%, 720px); margin: auto; text-align: center; }
.t-card h2 { font-family: var(--script); font-weight: 400; font-size: clamp(1.8rem, 6vw, 2.6rem); margin: 0 0 18px; color: var(--text); }
.theme-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 16px; }
.theme-opt {
  padding: 6px 6px 10px; border: 0; border-radius: 4px; background: linear-gradient(135deg, #f3dc9c, #a87d2c 50%, #ecca78);
  box-shadow: 0 10px 24px rgba(0,0,0,.5); transition: transform .25s ease;
}
.theme-opt:hover { transform: translateY(-3px); }
.theme-opt img { display: block; width: 100%; aspect-ratio: 2 / 3; object-fit: cover; background: #111; }
.theme-opt span { display: block; margin-top: 8px; font-family: var(--serif); font-size: 1.05rem; color: #2a1d08; }
.theme-opt.current { outline: 3px solid var(--accent); outline-offset: 3px; }
.theme-opt.current span::after { content: ' ✓'; }

.no-theme { padding: 40px 20px; text-align: center; font-family: var(--serif); font-style: italic; color: var(--muted); }

/* ---------- modale contenuto ---------- */
.modal, .xmas {
  position: fixed; inset: 0; z-index: 10; display: flex; align-items: center; justify-content: center;
  background: rgba(5, 9, 24, .86); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  overflow-y: auto; animation: fade .5s ease both;
}
.modal[hidden], .xmas[hidden], .x-stage[hidden], .toast[hidden] { display: none; }
@keyframes fade { from { opacity: 0; } }
.m-card { max-width: 760px; width: 100%; text-align: center; margin: auto; animation: rise .7s cubic-bezier(.2, .7, .2, 1) both .1s; }
@keyframes rise { from { opacity: 0; transform: translateY(18px) scale(.98); } }
.m-day { font-family: var(--script); font-size: 2rem; margin: 0 0 10px; color: var(--gold); }
.m-frame { --fw: clamp(12px, 3.6vw, 34px); margin: 0 auto; display: inline-block; position: relative; box-shadow: 0 20px 44px rgba(0,0,0,.6), 0 4px 10px rgba(0,0,0,.4); }
.m-frame img { display: block; max-width: min(86vw, 720px); max-height: 58vh; width: auto; height: auto; }
.m-caption { font-family: var(--serif); font-style: italic; font-size: .95rem; color: #aeb8da; margin: 12px 0 0; }
.m-quote {
  font-family: var(--serif); font-style: italic; font-weight: 500; font-size: clamp(1.25rem, 4.2vw, 1.75rem);
  line-height: 1.4; margin: 22px auto 8px; max-width: 34em; white-space: pre-line; color: var(--cream);
}
.m-author { font-family: var(--serif); font-variant: small-caps; letter-spacing: .08em; color: var(--gold); margin: 0; }
.m-author:not(:empty)::before { content: '— '; }
.m-close {
  position: fixed; top: max(10px, env(safe-area-inset-top)); right: 12px; z-index: 12;
  width: 44px; height: 44px; border: 0; border-radius: 50%; background: rgba(255,255,255,.08); font-size: 1.8rem; line-height: 1;
}
.m-nav {
  position: fixed; top: 50%; transform: translateY(-50%); z-index: 12; width: 44px; height: 64px;
  border: 0; border-radius: 10px; background: rgba(255,255,255,.06); font-size: 2.2rem; line-height: 1;
}
.m-nav.prev { left: 6px; } .m-nav.next { right: 6px; }
.m-nav[disabled] { opacity: 0; pointer-events: none; }
.m-close:hover, .m-nav:hover { background: rgba(255,255,255,.16); }

/* ---------- 25 dicembre ---------- */
.x-stage { display: flex; flex-direction: column; align-items: center; margin: auto; animation: fade .8s both; }
.x-hint { font-family: var(--serif); font-style: italic; color: #cdd6f5; margin-top: 18px; }
.envelope {
  position: relative; width: min(82vw, 380px); aspect-ratio: 3 / 2; padding: 0; border: 0; background: none;
  perspective: 900px; animation: float 3.5s ease-in-out infinite;
}
@keyframes float { 50% { transform: translateY(-8px); } }
.env-back, .env-front, .env-flap, .env-letter { position: absolute; inset: 0; }
.env-back { background: #e9dcc0; border-radius: 6px; box-shadow: 0 20px 40px rgba(0,0,0,.5); }
.env-letter { inset: 8% 6% 10%; background: #fbf6ea; border-radius: 3px; transition: transform 1s ease .5s; }
.env-front { background: linear-gradient(160deg, #f1e6cd, #e2d2b0); clip-path: polygon(0 0, 50% 55%, 100% 0, 100% 100%, 0 100%); border-radius: 6px; z-index: 2; }
.env-flap {
  background: linear-gradient(180deg, #e4d4b2, #d6c29a); clip-path: polygon(0 0, 100% 0, 50% 58%);
  transform-origin: top; transition: transform .8s ease; z-index: 3;
}
.seal {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 4;
  width: 54px; height: 54px; border-radius: 50%; display: grid; place-items: center;
  background: radial-gradient(circle at 40% 35%, #c2384a, #7e1222); color: #f3c3c8; font-size: 1.4rem;
  box-shadow: 0 2px 6px rgba(0,0,0,.5); transition: opacity .4s;
}
.envelope.opening { animation: none; }
.envelope.opening .seal { opacity: 0; }
.envelope.opening .env-flap { transform: rotateX(180deg); z-index: 1; }
.envelope.opening .env-letter { transform: translateY(-55%); }

.letter {
  width: min(92vw, 640px); background: #fbf6ea; color: var(--ink); border-radius: 4px;
  padding: 2rem clamp(22px, 6vw, 48px);
  /* righe ogni 2rem (= line-height), alzate di poco perché la grafia "poggi" sulla riga */
  background-image: repeating-linear-gradient(180deg, transparent 0 calc(2rem - 1px), rgba(80, 100, 160, .14) calc(2rem - 1px) 2rem);
  background-position: 0 -.3rem;
  box-shadow: 0 25px 60px rgba(0,0,0,.6);
  font-family: var(--hand); font-size: clamp(1.35rem, 4.6vw, 1.7rem); line-height: 2rem;
}
.letter p { margin: 0 0 2rem; opacity: 0; transform: translateY(6px); transition: opacity 1.4s ease, transform 1.4s ease; white-space: pre-line; }
.letter p.shown { opacity: 1; transform: none; }
.letter p:last-child { margin-bottom: 0; }
.x-next {
  margin-top: 26px; padding: 12px 26px; border-radius: 999px; border: 1px solid var(--gold);
  background: rgba(227, 189, 106, .12); color: var(--gold); font-family: var(--serif); font-style: italic; font-size: 1.2rem;
  animation: fade .8s both;
}
.photo-stage .m-frame img { max-height: 70vh; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 20;
  max-width: min(90vw, 420px); padding: 12px 18px; border-radius: 14px; text-align: center;
  background: rgba(20, 28, 60, .92); border: 1px solid rgba(227, 189, 106, .35);
  font-family: var(--serif); font-style: italic; font-size: 1.1rem; color: var(--cream);
  box-shadow: 0 10px 30px rgba(0,0,0,.4); animation: rise .4s both;
}

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