:root {
  --verde: #5BC47A;
  --verde-scuro: #3E9C5B;
  --sfondo: #F6FBF4;
  --carta: #FFFFFF;
  --testo: #2B2D42;
  --tenue: #6B7280;
  --bordo: #E3EEE0;
  --rosa: #FF8FA3;
  --giallo: #F4D35E;
  --rosso: #E84855;
  --ombra: 0 6px 20px rgba(43, 45, 66, .08);
  --raggio: 22px;
  --font: "Nunito", system-ui, sans-serif;
  --titoli: "Baloo 2", "Nunito", system-ui, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root { --sfondo: #151A17; --carta: #1F2621; --testo: #EEF3EC; --tenue: #A3ADA5; --bordo: #2E3830; --ombra: 0 6px 20px rgba(0, 0, 0, .3); color-scheme: dark; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--sfondo); color: var(--testo); font-family: var(--font); font-size: 16px; -webkit-tap-highlight-color: transparent; }
#app { max-width: 760px; margin: 0 auto; min-height: 100%; display: flex; flex-direction: column; }
h1, h2 { font-family: var(--titoli); margin: 0; line-height: 1.15; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; margin-bottom: .5rem; }
a { color: var(--verde-scuro); }
.piccolo { font-size: .88rem; color: var(--tenue); }
.centro { text-align: center; }
.vuoto { text-align: center; color: var(--tenue); padding: 2rem 1rem; }
.caricamento { font-size: 4rem; text-align: center; margin-top: 40vh; animation: salta 1s infinite; }

/* Bottoni */
button, .primario, .bottone, .secondario, .pericolo {
  font: inherit; font-weight: 700; border: 2px solid var(--bordo); background: var(--carta); color: var(--testo);
  padding: .7rem 1.1rem; border-radius: 999px; cursor: pointer; text-decoration: none; display: inline-flex;
  align-items: center; justify-content: center; gap: .4rem; transition: transform .1s, background .2s;
}
button:active, .primario:active { transform: scale(.96); }
button:disabled { opacity: .5; }
.primario { background: var(--verde); border-color: var(--verde); color: #fff; }
.primario.grande { display: flex; margin: 1.2rem auto; font-size: 1.1rem; padding: 1rem 1.6rem; width: fit-content; box-shadow: var(--ombra); }
.pericolo { color: var(--rosso); border-color: color-mix(in srgb, var(--rosso) 35%, transparent); }
.tondo { width: 44px; height: 44px; padding: 0; border-radius: 50%; font-size: 1.2rem; flex: none; display: inline-flex; align-items: center; justify-content: center; border: 2px solid var(--bordo); background: var(--carta); text-decoration: none; cursor: pointer; }
.tondo.primario { background: var(--verde); border-color: var(--verde); color: #fff; }
.chip { display: inline-flex; align-items: center; gap: .3rem; padding: .25rem .7rem; border-radius: 999px; background: var(--bordo); font-size: .85rem; font-weight: 700; text-decoration: none; color: var(--testo); }
.chip.ok { background: color-mix(in srgb, var(--verde) 25%, transparent); }
.chip button { border: 0; padding: 0 .2rem; background: none; }
.bottoni { display: flex; flex-wrap: wrap; gap: .5rem; }

/* Moduli */
.modulo { display: flex; flex-direction: column; gap: .8rem; }
.modulo.riga { flex-direction: row; }
.modulo.riga input { flex: 1; }
label { display: flex; flex-direction: column; gap: .3rem; font-weight: 600; font-size: .92rem; }
label.spunta { flex-direction: row; align-items: center; font-weight: 400; }
input[type="checkbox"], input[type="range"] { width: auto; }
input, select, textarea {
  font: inherit; color: var(--testo); background: var(--carta); border: 2px solid var(--bordo); border-radius: 14px; padding: .7rem .9rem; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--verde); }
fieldset { border: 2px solid var(--bordo); border-radius: 14px; }

/* Struttura */
.testata { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: .7rem; padding: calc(.7rem + env(safe-area-inset-top)) 1rem .7rem; background: color-mix(in srgb, var(--sfondo) 88%, transparent); backdrop-filter: blur(10px); }
.testata h1 { flex: 1; }
.testata .logo svg { width: 40px; height: 44px; }
.indietro { font-size: 2.2rem; text-decoration: none; color: var(--testo); line-height: 1; padding: 0 .4rem; }
.contenuto { flex: 1; padding: .5rem 1rem calc(6rem + env(safe-area-inset-bottom)); }
.barra { position: fixed; bottom: 0; left: 0; right: 0; display: flex; justify-content: center; background: var(--carta); border-top: 2px solid var(--bordo); padding-bottom: env(safe-area-inset-bottom); z-index: 5; }
.barra a { flex: 1; max-width: 190px; display: flex; flex-direction: column; align-items: center; padding: .5rem 0 .6rem; font-size: .75rem; font-weight: 700; color: var(--tenue); text-decoration: none; }
.barra a span { font-size: 1.4rem; }
.barra a.attivo { color: var(--verde-scuro); }
.scheda { background: var(--carta); border-radius: var(--raggio); padding: 1.1rem; margin: 0 0 1rem; box-shadow: var(--ombra); display: flex; flex-direction: column; gap: .7rem; }
.griglia { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr)); gap: 1rem; }
.azioni { display: flex; justify-content: space-between; gap: 1rem; margin-top: 1.5rem; }
.azioni .primario { margin-left: auto; }

/* Personaggi */
.personaggio { width: 90px; height: 99px; flex: none; overflow: visible; }
.personaggio.grande { width: 180px; height: 198px; }
.personaggio.avatar { width: 46px; height: 50px; }
.personaggio.mini { width: 44px; height: 48px; }
.personaggio.salta { animation: salta 1.4s ease-in-out infinite; }
.umore-lavora, .umore-pensa { animation: ondeggia 1.6s ease-in-out infinite; transform-origin: 50% 90%; }
.bocca-parla { animation: parla .25s ease-in-out infinite alternate; transform-origin: 100px 118px; }
@keyframes salta { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-10px); } }
@keyframes ondeggia { 0%, 100% { transform: rotate(-3deg); } 50% { transform: rotate(3deg); } }
@keyframes parla { from { transform: scaleY(.3); } to { transform: scaleY(1.1); } }

.carta-agente { display: flex; align-items: center; gap: 1rem; padding: 1rem; background: var(--carta); border-radius: var(--raggio); box-shadow: var(--ombra); text-decoration: none; color: inherit; }
.carta-agente h2 { margin-bottom: .2rem; }
.stato { display: inline-block; font-size: .8rem; font-weight: 700; padding: .15rem .6rem; border-radius: 999px; background: var(--bordo); }
.stato.pronto { background: color-mix(in srgb, var(--verde) 25%, transparent); color: var(--verde-scuro); }
.stato.errore, .stato.sospeso_credito { background: color-mix(in srgb, var(--rosso) 18%, transparent); color: var(--rosso); }
.stato.creazione, .stato.installazione { background: color-mix(in srgb, var(--giallo) 35%, transparent); }
.errore { color: var(--rosso); }
.ok { color: var(--verde-scuro); font-weight: 700; }

.personaggi { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
.carta-personaggio { flex-direction: column; border-radius: var(--raggio); padding: 1rem .5rem; gap: .3rem; }
.carta-personaggio em { font-style: normal; font-size: .75rem; color: #B7791F; }
.scelto { border-color: var(--verde); background: color-mix(in srgb, var(--verde) 12%, var(--carta)); }
.piani { display: flex; flex-direction: column; gap: .8rem; }
.carta-piano { flex-direction: column; align-items: flex-start; border-radius: var(--raggio); padding: 1rem 1.2rem; text-align: left; gap: .2rem; font-weight: 400; }
.carta-piano strong { font-family: var(--titoli); font-size: 1.15rem; }
.prezzo { font-weight: 800; color: var(--verde-scuro); }

/* Accesso */
.accesso { padding: 2rem 1.5rem; display: flex; flex-direction: column; align-items: center; gap: 1rem; text-align: center; }
.accesso .modulo { width: 100%; max-width: 380px; text-align: left; }
.accesso h1 { font-size: 2.4rem; color: var(--verde-scuro); }
.sottotitolo { margin: 0; color: var(--tenue); }
.eroe .personaggio { width: 170px; height: 187px; }

/* Chat */
body[data-vista="chat"] #app { height: 100dvh; }
.testata-chat .titolo { flex: 1; min-width: 0; }
.testata-chat h1 { font-size: 1.2rem; }
.messaggi { flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: .5rem; }
.msg { display: flex; }
.msg.mio { justify-content: flex-end; }
.bolla { max-width: 82%; padding: .65rem .9rem .4rem; border-radius: 20px; background: var(--carta); box-shadow: var(--ombra); overflow-wrap: anywhere; line-height: 1.4; }
.mio .bolla { background: var(--verde); color: #fff; border-bottom-right-radius: 6px; }
.suo .bolla { border-bottom-left-radius: 6px; }
.err .bolla { background: color-mix(in srgb, var(--rosso) 12%, var(--carta)); }
.bolla time { display: block; text-align: right; font-size: .7rem; opacity: .6; margin-top: .2rem; }
.bolla pre { background: rgba(0, 0, 0, .06); padding: .6rem; border-radius: 10px; overflow-x: auto; font-size: .82rem; }
.bolla code { background: rgba(0, 0, 0, .06); padding: 0 .3rem; border-radius: 5px; }
.mio .bolla a { color: #fff; }
.bolla .file { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem; margin-top: .5rem; padding: .5rem; border-radius: 14px; background: var(--sfondo); }
.bolla .file span { flex: 1; font-weight: 700; color: var(--testo); }
.bolla .file em { font-weight: 400; color: var(--tenue); font-style: normal; font-size: .8rem; }
.bolla .file button { padding: .35rem .7rem; font-size: .85rem; }
.allegato { display: inline-block; margin: .3rem .3rem 0 0; font-size: .85rem; opacity: .9; }
.avanzamento { margin: 0 1rem; padding: .5rem .9rem; border-radius: 14px; background: color-mix(in srgb, var(--giallo) 25%, var(--carta)); font-size: .9rem; display: flex; align-items: center; gap: .5rem; }
.avanzamento[hidden] { display: none; }
.avanzamento button { margin-left: auto; padding: .2rem .6rem; }
.puntini i { display: inline-block; width: 6px; height: 6px; margin: 0 1px; border-radius: 50%; background: var(--verde-scuro); animation: salta .9s infinite; }
.puntini i:nth-child(2) { animation-delay: .15s; } .puntini i:nth-child(3) { animation-delay: .3s; }
.allegati { display: flex; flex-wrap: wrap; gap: .4rem; padding: 0 1rem; }
.compositore { display: flex; align-items: flex-end; gap: .5rem; padding: .6rem .8rem calc(.6rem + env(safe-area-inset-bottom)); background: var(--carta); border-top: 2px solid var(--bordo); }
.compositore textarea { resize: none; border-radius: 22px; max-height: 160px; }
.microfono.registra { background: var(--rosso); border-color: var(--rosso); animation: salta .6s infinite; }

/* Chiamata */
.chiamata { min-height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1rem; padding: 2rem; background: radial-gradient(circle at 50% 35%, color-mix(in srgb, var(--verde) 30%, var(--sfondo)), var(--sfondo) 70%); text-align: center; }
.chiamata .nome { font-family: var(--titoli); font-size: 2rem; margin: 0; }
.volto { border: 0; background: none; padding: 0; }
.volto .personaggio { width: 240px; height: 264px; }
.livello { width: 180px; height: 8px; border-radius: 4px; background: var(--bordo); overflow: hidden; }
.livello i { display: block; height: 100%; background: var(--verde); transform-origin: left; transform: scaleX(0); transition: transform .08s; }
.stato-chiamata { font-weight: 700; margin: 0; }
.sottotitoli { min-height: 3em; max-width: 520px; color: var(--tenue); margin: 0; }
.riattacca { background: var(--rosso); border-color: var(--rosso); color: #fff; padding: 1rem 2rem; font-size: 1.1rem; }

/* Impostazioni */
.motore { display: flex; flex-wrap: wrap; justify-content: space-between; gap: .6rem; padding: .7rem 0; border-bottom: 2px dashed var(--bordo); }
.motore > div:first-child { flex: 1 1 260px; }
.motore p { margin: .3rem 0 0; }
.gecky-riga { display: flex; flex-wrap: wrap; align-items: center; gap: .8rem; padding: .5rem 0; }
.gecky-riga label { flex-direction: row; align-items: center; }
.terminale { background: #1E2420; color: #D8F3DC; font-family: ui-monospace, monospace; font-size: .8rem; white-space: pre-wrap; padding: .8rem; border-radius: 12px; max-height: 40vh; overflow: auto; min-height: 6rem; }
.terminale a { color: #8FE3A8; }
pre.comando { background: #1E2420; color: #D8F3DC; padding: .8rem; border-radius: 12px; white-space: pre-wrap; word-break: break-all; }

/* Squadre e credito */
.membri { display: flex; gap: .3rem; flex-wrap: wrap; }
.eventi { list-style: none; padding: 0; margin: 0; max-height: 240px; overflow-y: auto; font-size: .88rem; display: flex; flex-direction: column; gap: .3rem; }
.eventi li { padding: .3rem .6rem; border-left: 4px solid var(--bordo); }
.eventi .fase-finito { border-color: var(--verde); font-weight: 700; }
.eventi .fase-errore { border-color: var(--rosso); }
.saldo { font-family: var(--titoli); font-size: 2.6rem; margin: 0; color: var(--verde-scuro); }
.movimenti { list-style: none; padding: 0; margin: 0; }
.movimenti li { display: flex; justify-content: space-between; gap: 1rem; padding: .5rem 0; border-bottom: 1px solid var(--bordo); font-size: .9rem; }
.meno { color: var(--rosso); white-space: nowrap; }
.piu { color: var(--verde-scuro); white-space: nowrap; font-weight: 700; }

/* Finestre e avvisi */
.finestra { border: 0; border-radius: var(--raggio); padding: 1.4rem; width: min(92vw, 520px); background: var(--carta); color: var(--testo); box-shadow: 0 20px 60px rgba(0, 0, 0, .25); }
.finestra::backdrop { background: rgba(20, 30, 20, .45); }
.finestra .chiudi { position: absolute; top: .6rem; right: .6rem; width: 36px; height: 36px; padding: 0; }
.finestra h2 { margin-right: 2rem; }
.toast { position: fixed; left: 50%; bottom: calc(5.5rem + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 50; background: var(--testo); color: var(--sfondo); padding: .7rem 1.1rem; border-radius: 16px; max-width: 90vw; box-shadow: var(--ombra); transition: opacity .5s; font-weight: 600; }
.toast-errore { background: var(--rosso); color: #fff; }
.toast.via { opacity: 0; }
.avviso-rete { display: none; position: fixed; top: env(safe-area-inset-top); left: 50%; transform: translateX(-50%); background: var(--giallo); color: #2B2D42; font-weight: 700; font-size: .8rem; padding: .2rem .8rem; border-radius: 0 0 10px 10px; z-index: 20; }
body.scollegato .avviso-rete { display: block; }
