:root { --bg: #33304a; --card: #443f5c; --card2: #544e6e; --ink: #fbf2e2; --soft: #cdbfae; --gold: #f3d9a0; --pink: #eeb2c6; }
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); font-family: "Avenir Next", "Segoe UI", system-ui, sans-serif; overscroll-behavior: none; }
body { padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
button { font: inherit; color: inherit; border: 0; cursor: pointer; }
.hidden { display: none !important; }

/* ---------- home ---------- */
#home { min-height: 100dvh; padding: 22px 18px 40px; max-width: 640px; margin: 0 auto; }
#home h1 { font-size: 28px; margin: 6px 0 4px; letter-spacing: .3px; }
#home .sub { color: var(--soft); margin: 0 0 18px; }
.cards { display: grid; gap: 12px; }
.card { display: flex; align-items: center; gap: 14px; background: var(--card); border-radius: 20px; padding: 14px 16px; text-align: left; width: 100%; border: 1px solid #ffffff14; }
.card:active { transform: scale(.98); background: var(--card2); }
.card .em { font-size: 38px; width: 54px; height: 54px; display: grid; place-items: center; background: #ffffff10; border-radius: 16px; flex: none; }
.card b { display: block; font-size: 17px; }
.card small { color: var(--soft); display: block; margin-top: 2px; line-height: 1.3; }
.card.magic { background: linear-gradient(135deg, #4b2b9b, #b23f9a); }
.card.auto { background: linear-gradient(135deg, #1f6f8b, #3b3fa0); }
.badge { margin-left: auto; font-size: 12px; background: #ffffff1f; padding: 3px 9px; border-radius: 12px; white-space: nowrap; }
.sec { margin: 22px 4px 10px; color: var(--soft); font-size: 13px; text-transform: uppercase; letter-spacing: 1.2px; }
.row { display: flex; gap: 10px; margin-top: 22px; flex-wrap: wrap; }
.ghost { background: #ffffff14; padding: 10px 14px; border-radius: 14px; font-size: 14px; }
#status { color: var(--soft); font-size: 13px; margin-top: 10px; min-height: 18px; }

/* ---------- player ---------- */
#player { height: 100dvh; display: flex; flex-direction: column; max-width: 640px; margin: 0 auto; }
.stage { background: #fbf2e2 !important; position: relative; flex: 0 0 56dvh; overflow: hidden; border-radius: 0 0 26px 26px; background: #000; }
#scene { width: 100%; height: 100%; display: block; touch-action: manipulation; }
.topbar { position: absolute; top: 8px; left: 8px; right: 8px; display: flex; justify-content: space-between; z-index: 3; pointer-events: none; }
.topbar button { pointer-events: auto; width: 42px; height: 42px; border-radius: 50%; color: #5a4638; background: #fbf2e2cc; backdrop-filter: blur(6px); font-size: 19px; }
.bubbles { position: absolute; left: 0; right: 0; bottom: 8px; display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: center; gap: 6px; padding: 0 8px; z-index: 3; pointer-events: none; }
#player.free #bPrev, #player.free #bNext, #player.free #bNarr { display: none; }
#player.free .controls { justify-content: center; }
.bubble { font-size: 15px; background: #fbf2e2e6; color: #5a4638; font-family: Georgia, serif; padding: 4px 12px; border-radius: 16px; font-weight: 700; animation: pop .35s ease-out, fadeout .6s 2.6s forwards; }
.panel { flex: 1; overflow-y: auto; padding: 16px 20px 6px; -webkit-overflow-scrolling: touch; }
.title { font-size: 13px; color: var(--soft); letter-spacing: 1px; text-transform: uppercase; margin-bottom: 8px; }
#text { font-size: 18px; line-height: 1.5; font-family: Georgia, "Iowan Old Style", serif; }
#text .w { color: #a89c98; transition: color .4s; cursor: pointer; }
#text .w.read { color: #fbf2e2; }
#text .w.kw { border-bottom: 2px dotted var(--gold); }
#text .w.cur { background: #ffd66b33; border-radius: 6px; }
.hint { color: var(--soft); line-height: 1.5; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.chip { background: #fbf2e2; border-radius: 22px; width: 62px; height: 62px; font-size: 32px; animation: floaty 5s ease-in-out infinite; }
.chip:nth-child(2n) { animation-delay: -1.6s; } .chip:nth-child(3n) { animation-delay: -3.1s; }
.chip:active { background: var(--gold); }
.controls { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 18px 14px; }
.controls button { width: 58px; height: 58px; border-radius: 50%; background: var(--card2); font-size: 24px; }
.controls button.on { background: #b79ac0; box-shadow: 0 0 0 6px #b79ac044; animation: pulse 3s ease-in-out infinite; }
.controls #bNarr.on { background: #9fb89a; box-shadow: 0 0 0 6px #9fb89a44; }
.dots { text-align: center; color: var(--soft); font-size: 12px; letter-spacing: 3px; }
.end { position: absolute; inset: 0; z-index: 4; display: grid; place-items: center; text-align: center; font-family: Georgia, serif; background: #2a2a4acc; opacity: 0; pointer-events: none; transition: opacity 2s; font-size: 30px; }
.end.show { opacity: 1; pointer-events: auto; }
.end small { display: block; font-size: 15px; color: var(--soft); margin-top: 8px; }

/* ---------- editor ---------- */
#editor { padding: 18px; max-width: 640px; margin: 0 auto; }
#editor input, #editor textarea { width: 100%; background: var(--card); color: var(--ink); border: 1px solid #ffffff22; border-radius: 12px; padding: 10px; font: inherit; margin: 6px 0 12px; }
#editor textarea { min-height: 280px; font-size: 15px; line-height: 1.4; }
#editor pre { background: #00000040; padding: 10px; border-radius: 10px; white-space: pre-wrap; color: var(--soft); font-size: 13px; }

/* ---------- animazioni SVG ---------- */
.fade { transition: opacity 1.6s; opacity: 0; }
.setting, .celestial, .clouds { transition: opacity 1.8s, transform 2.4s ease-in-out; }
.setting { opacity: 0; }
.twinkle { animation: twinkle 3s infinite ease-in-out; }
@keyframes twinkle { 50% { opacity: .2; } }
.spin { animation: spin 40s linear infinite; transform-box: fill-box; transform-origin: center; }
@keyframes spin { to { transform: rotate(360deg); } }
.drift { animation: drift 90s linear infinite; }
@keyframes drift { from { transform: translateX(-160px); } to { transform: translateX(480px); } }
.clouds.windy .drift { animation-duration: 12s !important; }
.waves { animation: wave 5s ease-in-out infinite alternate; }
@keyframes wave { to { transform: translateX(40px); } }
.boat { animation: boat 4s ease-in-out infinite; transform-box: fill-box; transform-origin: center bottom; }
@keyframes boat { 50% { transform: translateY(4px) rotate(3deg); } }
.drop { animation: fall .7s linear infinite; }
@keyframes fall { from { transform: translateY(70px); } to { transform: translateY(470px); } }
.flake { animation: snowfall 7s linear infinite; }
@keyframes snowfall { from { transform: translate(0, 70px); } 50% { transform: translate(14px, 270px); } to { transform: translate(-6px, 480px); } }
.firefly { animation: ffly 6s ease-in-out infinite; filter: drop-shadow(0 0 4px #f7ff8a); }
@keyframes ffly { 0%,100% { transform: translate(0,0); opacity: .1; } 25% { transform: translate(14px,-12px); opacity: 1; } 50% { transform: translate(-8px,-26px); opacity: .3; } 75% { transform: translate(10px,-10px); opacity: 1; } }

.slot .inner { opacity: 0; transform: translateY(26px); transition: opacity 1.4s, transform 1.4s ease-out; }
.slot.show .inner { opacity: 1; transform: translateY(0); }
.idle { animation: bob 3.2s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-3px); } }
.slot.hop .idle { animation: hop 1.1s ease-in-out; }
@keyframes hop { 30% { transform: translateY(-16px); } 55% { transform: translateY(0); } 70% { transform: translateY(-8px); } 100% { transform: translateY(0); } }
.sleeping .idle { animation: breathe 3.6s ease-in-out infinite; }
@keyframes breathe { 50% { transform: translateY(2px) scaleY(.98); } }
.eye { transform-box: fill-box; transform-origin: center; animation: blink 5s infinite; }
@keyframes blink { 0%, 94%, 100% { transform: scaleY(1); } 97% { transform: scaleY(.1); } }
.lid { opacity: 0; stroke: #2b2b3a; fill: none; stroke-width: 2.2; stroke-linecap: round; }
.sleeping .lid { opacity: 1; }
.sleeping .eye { opacity: 0; }
.ear { transform-box: fill-box; transform-origin: bottom center; animation: ear 4s infinite; }
@keyframes ear { 0%,90%,100% { transform: rotate(0); } 94% { transform: rotate(-12deg); } }
.wing, .flap { transform-box: fill-box; transform-origin: 0% 100%; animation: flap 1.6s ease-in-out infinite; }
.flap { transform-origin: center; animation-duration: .35s; }
@keyframes flap { 50% { transform: rotate(-14deg) scaleY(.9); } }
.swim { animation: swim 3.4s ease-in-out infinite; }
@keyframes swim { 0%,100% { transform: translate(-14px,0) rotate(-4deg); } 50% { transform: translate(14px,-8px) rotate(4deg); } }
.hover { animation: hover 2.6s ease-in-out infinite; }
@keyframes hover { 50% { transform: translateY(-9px); } }

.spark { transform-box: fill-box; transform-origin: center; animation: spark 1.6s ease-out both; }
@keyframes spark { 0% { transform: scale(0) rotate(0); opacity: 0; } 30% { opacity: 1; } 100% { transform: scale(1.4) rotate(120deg) translateY(-18px); opacity: 0; } }
.float { animation: floatup 4s ease-out both; }
@keyframes floatup { 0% { transform: translateY(0); opacity: 0; } 15% { opacity: 1; } 100% { transform: translateY(-190px); opacity: 0; } }
.zz { animation: zfloat 3.2s ease-out both; }
@keyframes zfloat { 0% { transform: translate(0,0) scale(.6); opacity: 0; } 20% { opacity: 1; } 100% { transform: translate(18px,-60px) scale(1.5); opacity: 0; } }
.flash.go { animation: flash 1.6s ease-in-out; }
@keyframes flash { 0% { opacity: 0; } 30% { opacity: .28; } 100% { opacity: 0; } }
.bolt { animation: bolt .7s ease-out both; opacity: .7; }
@keyframes bolt { 0% { opacity: 0; } 8%,35% { opacity: 1; } 100% { opacity: 0; } }
.shoot { animation: shoot 1.5s ease-in both; }
@keyframes shoot { from { transform: translate(380px, 90px); opacity: 1; } to { transform: translate(60px, 250px); opacity: 0; } }
.leaf { animation: leaf 6s ease-in-out both; }
@keyframes leaf { from { transform: translate(0,0) rotate(0); opacity: 1; } to { transform: translate(460px,-60px) rotate(500deg); opacity: 0; } }
.flame { transform-box: fill-box; transform-origin: left center; animation: flame .5s ease-in-out infinite alternate; }
@keyframes flame { from { transform: scale(.7, .9); opacity: .8; } to { transform: scale(1.15, 1.1); opacity: 1; } }
.dimmer { transition: opacity 2.4s; }

@keyframes pop { from { transform: scale(.4); opacity: 0; } }
@keyframes fadeout { to { opacity: 0; transform: translateY(-10px); } }
@keyframes pulse { 50% { box-shadow: 0 0 0 12px #b79ac000; } }
@media (prefers-reduced-motion: reduce) { .drift, .spin, .waves, .boat { animation: none; } }

/* ---------- home morbida per i piccoli ---------- */
body::before { content: ""; position: fixed; inset: 0; z-index: -1; pointer-events: none;
  background: radial-gradient(1.5px 1.5px at 12% 18%, #fff8e0 50%, transparent 51%), radial-gradient(1.5px 1.5px at 78% 9%, #fff8e0 50%, transparent 51%), radial-gradient(2px 2px at 55% 30%, #fff8e0 50%, transparent 51%), radial-gradient(1.5px 1.5px at 30% 62%, #fff8e0 50%, transparent 51%), radial-gradient(2px 2px at 88% 55%, #fff8e0 50%, transparent 51%), radial-gradient(1.5px 1.5px at 8% 85%, #fff8e0 50%, transparent 51%), radial-gradient(1.5px 1.5px at 66% 82%, #fff8e0 50%, transparent 51%), linear-gradient(#33304a, #5a4f66);
  animation: skyglow 7s ease-in-out infinite alternate; }
@keyframes skyglow { from { opacity: .75; } to { opacity: 1; } }
#home h1 { font-size: 24px; text-align: center; font-family: Georgia, 'Iowan Old Style', serif; font-weight: 500; }
#home h1 .moon { display: inline-block; animation: floaty 6s ease-in-out infinite; }
#home .sub { text-align: center; font-size: 14px; }
.cards { grid-template-columns: 1fr 1fr; gap: 14px; }
.card { flex-direction: column; text-align: center; padding: 18px 10px 14px; border-radius: 28px; gap: 8px; animation: floaty 6s ease-in-out infinite; background: #ffffff14; }
.card:nth-child(2n) { animation-delay: -2s; } .card:nth-child(3n) { animation-delay: -4s; }
.card .em { font-size: 60px; width: 104px; height: 104px; border-radius: 50%; background: #fbf2e2; box-shadow: 0 0 30px #f3d9a033; }
.card b { font-size: 15px; } .card small { font-size: 12px; }
.card.magic { background: linear-gradient(135deg, #8d7a9e, #c9a7b4); } .card.auto { background: linear-gradient(135deg, #7d9a9e, #9c94b0); }
.badge { margin: 0; }
.sec { text-align: center; }
.row { justify-content: center; opacity: .7; }
@keyframes floaty { 50% { transform: translateY(-5px); } }

/* ---------- stile albo illustrato ---------- */
.idle ellipse, .idle path, .idle circle, .idle rect { stroke: #8a6a55; stroke-width: 1.1; stroke-linejoin: round; stroke-linecap: round; }
.idle [opacity], .idle .eye { stroke: none; }
.idle .lid { stroke: #5a4638; stroke-width: 2; }
.setting path, .setting rect, .setting ellipse { stroke: #8a6a5566; stroke-width: 1; }
.idle { animation-duration: 4.6s; }
.sleeping .idle { animation-duration: 5.2s; }
.drift { animation-duration: 140s; }
.drop { opacity: .7; }
.dimmer { fill: #2a2a4a; }

/* ---------- inventa una storia ---------- */
#invent { padding: 18px; max-width: 640px; margin: 0 auto; text-align: center; }
#invent h2 { font-family: Georgia, serif; font-weight: 500; margin: 10px 0 4px; }
#invent .ghost { float: left; }
#invent .chips { justify-content: center; }
.bigmic { width: 120px; height: 120px; border-radius: 50%; font-size: 54px; background: #fbf2e2; margin: 14px 0 6px; box-shadow: 0 0 40px #f3d9a044; }
.bigmic.on { animation: glow 3s ease-in-out infinite; }
@keyframes glow { 50% { box-shadow: 0 0 0 18px #f3d9a033, 0 0 50px #f3d9a066; transform: scale(1.04); } }
.words { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; min-height: 48px; margin: 10px 0 4px; }
.wchip { background: var(--gold); color: #5a4638; border-radius: 20px; padding: 8px 14px; font-size: 17px; font-family: Georgia, serif; animation: pop .5s ease-out; }
.go { display: block; width: 100%; margin: 18px 0 8px; padding: 18px; border-radius: 26px; font-size: 20px; color: #5a4638; background: linear-gradient(135deg, #f3d9a0, #eeb2c6); }
.go:disabled { opacity: .5; }
.go.busy { animation: floaty 2s ease-in-out infinite; }
.card.invent { background: linear-gradient(135deg, #a98f7a, #c9a98a); }
#specials .card.invent { grid-column: 1 / -1; flex-direction: row; text-align: left; }
#specials .card.invent .em { width: 76px; height: 76px; font-size: 42px; }

/* ---------- impostazioni (ingranaggio) ---------- */
#home { position: relative; }
.gear { position: absolute; top: 18px; right: 14px; width: 44px; height: 44px; border-radius: 50%; background: #ffffff14; font-size: 21px; opacity: .8; }
#home h1 { padding: 0 44px; }
.sheet { position: fixed; inset: 0; z-index: 10; background: #1e1c2ecc; display: grid; place-items: center; padding: 20px; }
.sheet .box { background: var(--card); border-radius: 28px; padding: 22px; width: 100%; max-width: 340px; text-align: center; }
.sheet h2 { font-family: Georgia, serif; font-weight: 500; margin: 0 0 6px; }
.opt { display: block; width: 100%; margin: 10px 0; padding: 16px; border-radius: 20px; font-size: 19px; background: #ffffff14; border: 2px solid transparent; }
.opt.sel { background: #fbf2e2; color: #5a4638; border-color: var(--gold); }
.sheet .ghost { margin-top: 12px; }

/* ---------- voci e registrazione ---------- */
.topbar span { display: flex; gap: 8px; pointer-events: none; }
#player.free #bVoice { display: none; }
.opt small { display: block; font-size: 12px; opacity: .7; margin-top: 2px; }
.recbar { display: none; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 18px 16px; }
.recbar div { flex: 1; text-align: center; } .recbar small { display: block; color: var(--soft); margin-top: 3px; }
#bRec { width: 72px; height: 72px; border-radius: 50%; font-size: 30px; background: #fbf2e2; color: #5a4638; }
#bRec.on { background: #e9a7a0; animation: glow 2s ease-in-out infinite; }
#player.rec .recbar { display: flex; }
#player.rec .controls, #player.rec .dots, #player.rec #bVoice, #player.rec #bHeart { display: none; }

/* ---------- personaggi più naturali ---------- */
.idle ellipse, .idle path, .idle circle, .idle rect { stroke: #7a5e4c; stroke-width: .7; }
.idle .sh, .idle .ns, .idle .eye *, .idle [opacity] { stroke: none; }
.idle .sh { pointer-events: none; }
.idle .lid { stroke: #4a3a30; stroke-width: 1.8; fill: none; }
.idle .bub { fill: none; stroke: #fff; opacity: .7; }
.wag { transform-box: fill-box; transform-origin: 100% 100%; animation: wag 2.2s ease-in-out infinite; }
@keyframes wag { 50% { transform: rotate(14deg); } }
.sleeping .wag, .sleeping .ear { animation: none; }
/* ---------- laboratorio personaggi ---------- */
#chars { padding: 18px; max-width: 640px; margin: 0 auto; text-align: center; }
#chars h2 { font-family: Georgia, serif; font-weight: 500; margin: 10px 0 4px; }
#chars > .ghost:first-child { float: left; }
#chars input { width: 100%; background: var(--card); color: var(--ink); border: 1px solid #ffffff22; border-radius: 14px; padding: 12px; font: inherit; margin: 6px 0; text-align: center; }
#chars .chips { justify-content: center; margin-top: 0; }
#chars .chip { animation: none; }
.cprev { width: 190px; height: 160px; margin: 8px auto; background: #fbf2e2; border-radius: 28px; }
.thumb { width: 100%; height: 100%; display: block; }
.chip .thumb { width: 50px; height: 44px; margin: auto; }
.wchip .thumb { width: 34px; height: 28px; display: inline-block; vertical-align: middle; }
.sw { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.sw button { width: 38px; height: 38px; border-radius: 50%; border: 2px solid #ffffff33; }
.sw button.sel, #chars .chip.sel { outline: 3px solid var(--gold); outline-offset: 2px; }

/* ---------- campione di voce ---------- */
.smtext { font-family: Georgia, serif; font-size: 17px; line-height: 1.5; text-align: left; background: #fbf2e2; color: #5a4638; border-radius: 18px; padding: 14px; max-height: 38dvh; overflow-y: auto; }
.bigrec { width: 76px; height: 76px; border-radius: 50%; font-size: 32px; background: #fbf2e2; color: #5a4638; margin: 4px 0 8px; }
.bigrec.on { background: #e9a7a0; animation: glow 2s ease-in-out infinite; }
#sample .box { max-width: 420px; } #vRec { flex-wrap: wrap; justify-content: center; }

/* ---------- schermo largo: telefono in orizzontale o TV (AirPlay / trasmissione schermo) ---------- */
@media (orientation: landscape) and (min-width: 640px) {
  #player { max-width: none; display: grid; grid-template-columns: auto 1fr; grid-template-rows: 1fr auto auto auto; }
  #player .stage { grid-row: 1 / -1; height: 100dvh; aspect-ratio: 400 / 430; flex: none; border-radius: 0 28px 28px 0; }
  #player .panel { padding: 4dvh 4vw 0; }
  #player #text { font-size: clamp(20px, 2.6vw, 44px); line-height: 1.5; }
  #player .title { font-size: clamp(12px, 1.2vw, 20px); }
  #home, #invent, #chars, #editor { max-width: 900px; }
  #home .cards { grid-template-columns: repeat(4, 1fr); }
  #specials .card.invent { grid-column: span 2; }
}

/* ---------- personaggi 3D ---------- */
.stage .g3d { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 0; }
#scene { position: relative; z-index: 1; }
/* sfondo in 3D: il disegno 2D di cielo e luoghi sparisce, restano sopra pioggia, neve, stelle ed effetti */
.env3d .sky2d, .env3d .land2d, .env3d .celestial, .env3d .clouds, .env3d .storm { opacity: 0 !important; pointer-events: none; }
.env3d .paper2d { opacity: .35; }
.env3d .stars { mix-blend-mode: screen; }
.has3d .slot .idle, .has3d .slot .inner > ellipse { opacity: 0; }   /* il disegno 2D resta invisibile, solo per il tocco */
img.thumb { object-fit: contain; }

/* ---------- mescola personaggi e scenari ---------- */
#player.free #bMix, #player.rec #bMix { display: none; }
#mix .chips.mx { justify-content: center; margin-top: 0; }
#mix .chip { animation: none; }
#mix .chip.sel { outline: 3px solid var(--gold); outline-offset: 2px; }
#mix .chip .thumb { width: 52px; height: 46px; margin: auto; }
#title.edit { cursor: pointer; }

/* ---------- scelta della voce ---------- */
.vrow { display: flex; gap: 8px; align-items: center; }
.vrow select { flex: 1; min-width: 0; background: #ffffff14; color: var(--ink); border: 1px solid #ffffff33; border-radius: 14px; padding: 12px 10px; font: inherit; font-size: 15px; }
.vrow select option { color: #222; }
#settings .box { max-height: 92dvh; overflow-y: auto; }

/* ---------- chiavi IA ---------- */
#keys .box { max-width: 420px; max-height: 92dvh; overflow-y: auto; text-align: left; }
#keys h2 { text-align: center; }
.kitem { margin: 14px 0; }
.kitem > small { margin-left: 8px; color: var(--soft); }
.kitem input { flex: 1; min-width: 0; background: #ffffff14; color: var(--ink); border: 1px solid #ffffff33; border-radius: 12px; padding: 11px 10px; font: inherit; font-size: 14px; margin-top: 6px; }
.kitem .ghost { margin-top: 6px; }
.khelp { display: block; color: var(--soft); font-size: 12px; margin-top: 4px; line-height: 1.4; }
.khelp a { color: var(--gold); }
#keys .go { margin-top: 6px; }
.kok { color: #b7e0a8 !important; } .kno { color: #f2b0a8 !important; }
