:root {
  --sfondo: #fff4e6;
  --superficie: #ffffff;
  --testo: #2b2233;
  --testo-tenue: #7a6d80;
  --bordo: #f0dfcb;
  --accento: #ff5a36;
  --accento-testo: #ffffff;
  --ok: #2e9e6a;
  --ombra: 0 2px 0 rgba(43, 34, 51, .08), 0 6px 18px rgba(43, 34, 51, .08);
  --t0: #ffd6c9; --t1: #ffe7a3; --t2: #c9ecd6; --t3: #cfe0ff; --t4: #e6d5ff; --t5: #ffd1e6;
  --raggio: 22px;
  --sotto: env(safe-area-inset-bottom, 0px);
  --sopra: env(safe-area-inset-top, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --sfondo: #1d1a24;
    --superficie: #2a2633;
    --testo: #f4eef7;
    --testo-tenue: #a89eb0;
    --bordo: #3a3445;
    --accento: #ff6f4f;
    --ombra: 0 2px 0 rgba(0, 0, 0, .3), 0 6px 18px rgba(0, 0, 0, .3);
    --t0: #5a3328; --t1: #57491f; --t2: #24493a; --t3: #27395c; --t4: #3e2f5c; --t5: #5a2b43;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--sfondo);
  color: var(--testo);
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 17px;
  line-height: 1.35;
  overscroll-behavior-y: none;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
input, select {
  width: 100%;
  padding: 14px 16px;
  border-radius: 14px;
  border: 2px solid var(--bordo);
  background: var(--superficie);
  outline: none;
  font-size: 17px;
}
input:focus, select:focus { border-color: var(--accento); }
[hidden] { display: none !important; }

.caricamento { display: grid; place-items: center; height: 100vh; font-size: 56px; animation: pulsa 1s ease-in-out infinite; }

/* ---- Accesso ---- */
.accesso {
  min-height: 100vh;
  display: grid;
  place-content: center;
  gap: 14px;
  padding: calc(24px + var(--sopra)) 24px calc(24px + var(--sotto));
  max-width: 420px;
  margin: 0 auto;
}
.accesso .logo { font-size: 72px; text-align: center; }
.accesso h1 { margin: 0; text-align: center; font-size: 30px; }
.accesso p { margin: 0 0 8px; text-align: center; color: var(--testo-tenue); }
.errore { color: var(--accento); font-weight: 600; min-height: 1.4em; margin: 0; }

.primario {
  background: var(--accento);
  color: var(--accento-testo);
  font-weight: 700;
  padding: 15px 20px;
  border-radius: 16px;
  box-shadow: 0 3px 0 rgba(0, 0, 0, .15);
  width: 100%;
}
.primario:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0, 0, 0, .15); }
.primario:disabled, .secondario:disabled { opacity: .45; }
.secondario {
  background: var(--superficie);
  border: 2px solid var(--bordo);
  font-weight: 600;
  padding: 13px 18px;
  border-radius: 16px;
  width: 100%;
}
.pericolo { color: var(--accento); font-weight: 700; padding: 12px; width: 100%; }

/* ---- Schermata principale ---- */
.testata {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--sfondo);
  padding: calc(10px + var(--sopra)) 16px 0;
}
.testata-riga { display: flex; align-items: center; gap: 10px; }
.testata h1 { margin: 0; font-size: 24px; flex: 1; letter-spacing: -.3px; }
.testata h1 small { display: block; font-size: 13px; color: var(--testo-tenue); font-weight: 500; letter-spacing: 0; }
.icona-pulsante {
  width: 44px; height: 44px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--superficie);
  box-shadow: var(--ombra);
  font-size: 20px;
}
.offline-segno { font-size: 12px; font-weight: 700; color: var(--testo-tenue); background: var(--superficie); padding: 4px 8px; border-radius: 99px; }

.categorie {
  display: flex; gap: 8px;
  overflow-x: auto;
  padding: 12px 16px 12px;
  margin: 0 -16px;
  scrollbar-width: none;
}
.categorie::-webkit-scrollbar { display: none; }
.chip {
  flex: none;
  padding: 9px 16px;
  border-radius: 99px;
  background: var(--superficie);
  border: 2px solid var(--bordo);
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
}
.chip[aria-pressed="true"] { background: var(--testo); color: var(--sfondo); border-color: var(--testo); }

.griglia {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
  padding: 4px 16px calc(120px + var(--sotto));
}
@media (max-width: 360px) { .griglia { grid-template-columns: 1fr 1fr; gap: 10px; } }

.tormentone {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: var(--raggio);
  background: var(--t0);
  box-shadow: var(--ombra);
  padding: 14px 12px 12px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  touch-action: manipulation;
  overflow: hidden;
  transition: transform .12s ease;
}
.tormentone:active { transform: scale(.96); }
.tormentone[data-colore="1"] { background: var(--t1); }
.tormentone[data-colore="2"] { background: var(--t2); }
.tormentone[data-colore="3"] { background: var(--t3); }
.tormentone[data-colore="4"] { background: var(--t4); }
.tormentone[data-colore="5"] { background: var(--t5); }
.tormentone .emoji { font-size: 46px; line-height: 1; margin-bottom: 8px; }
.tormentone .titolo { font-weight: 700; font-size: 16px; line-height: 1.2; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.tormentone .volte { font-size: 13px; color: var(--testo-tenue); margin-top: 6px; font-weight: 600; }
.tormentone .autore {
  position: absolute; top: 8px; left: 8px;
  width: 24px; height: 24px; border-radius: 50%;
  display: grid; place-items: center;
  background: rgba(255, 255, 255, .6);
  font-size: 12px; font-weight: 800;
}
@media (prefers-color-scheme: dark) { .tormentone .autore { background: rgba(0, 0, 0, .25); } }
.tormentone .altro {
  position: absolute; top: 2px; right: 2px;
  width: 40px; height: 40px;
  font-size: 22px; font-weight: 800; color: var(--testo-tenue);
}
.tormentone .avanzamento {
  position: absolute; left: 0; bottom: 0; height: 6px;
  width: calc(var(--avanzamento, 0) * 100%);
  background: var(--accento);
}
.tormentone.suona { outline: 4px solid var(--accento); outline-offset: -4px; }
.tormentone.suona .emoji { animation: parla .35s ease-in-out infinite alternate; }
.tormentone.non-pronto { opacity: .55; }

.vuoto { text-align: center; color: var(--testo-tenue); padding: 48px 24px; grid-column: 1 / -1; }
.vuoto .grande { font-size: 56px; }

/* ---- Barra in basso ---- */
.barra {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: flex; align-items: center; justify-content: space-around;
  padding: 10px 12px calc(10px + var(--sotto));
  background: color-mix(in srgb, var(--sfondo) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--bordo);
}
.interruttore {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  min-width: 64px; padding: 6px 4px; border-radius: 14px;
  font-size: 12px; font-weight: 700; color: var(--testo-tenue);
}
.interruttore span:first-child { font-size: 24px; filter: grayscale(1); opacity: .6; }
.interruttore[aria-pressed="true"] { color: var(--accento); background: var(--superficie); box-shadow: var(--ombra); }
.interruttore[aria-pressed="true"] span:first-child { filter: none; opacity: 1; }
.registra {
  width: 68px; height: 68px; border-radius: 50%;
  background: var(--accento); color: #fff;
  font-size: 30px;
  display: grid; place-items: center;
  box-shadow: 0 4px 0 rgba(0, 0, 0, .15), 0 8px 20px rgba(255, 90, 54, .35);
  margin-top: -26px;
}
.registra:active { transform: translateY(2px); }

/* ---- Fogli (dialog) ---- */
dialog.foglio {
  border: 0; padding: 0;
  width: 100%; max-width: 560px;
  margin: auto auto 0;
  max-height: 92vh;
  border-radius: 28px 28px 0 0;
  background: var(--sfondo);
  color: var(--testo);
  box-shadow: 0 -10px 40px rgba(0, 0, 0, .2);
}
@media (min-width: 600px) { dialog.foglio { margin: auto; border-radius: 28px; } }
dialog.foglio::backdrop { background: rgba(20, 14, 24, .45); }
dialog.foglio[open] { animation: sali .22s ease-out; }
.foglio-corpo { padding: 10px 20px calc(24px + var(--sotto)); display: grid; gap: 14px; }
.foglio-maniglia { width: 44px; height: 5px; border-radius: 3px; background: var(--bordo); margin: 8px auto 0; }
.foglio h2 { margin: 4px 0 0; font-size: 22px; display: flex; align-items: center; gap: 8px; }
.foglio h2 .chiudi { margin-left: auto; font-size: 18px; width: 36px; height: 36px; border-radius: 50%; background: var(--superficie); }
.etichetta { font-size: 14px; font-weight: 700; color: var(--testo-tenue); margin-bottom: -8px; }

.emoji-scelta { display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px; }
.emoji-scelta button { font-size: 26px; aspect-ratio: 1; border-radius: 12px; background: var(--superficie); }
.emoji-scelta button[aria-pressed="true"] { outline: 3px solid var(--accento); }

.microfono {
  display: grid; place-items: center; gap: 10px; padding: 8px 0;
}
.microfono .pulsante-rec {
  position: relative;
  width: 132px; height: 132px; border-radius: 50%;
  background: var(--accento); color: #fff;
  font-size: 18px; font-weight: 800;
  box-shadow: 0 0 0 calc(var(--livello, 0) * 28px) color-mix(in srgb, var(--accento) 25%, transparent), 0 6px 0 rgba(0, 0, 0, .15);
  transition: box-shadow .08s linear;
}
.microfono .pulsante-rec.attivo { background: #d63a1a; }
.microfono .tempo { font-variant-numeric: tabular-nums; font-size: 15px; color: var(--testo-tenue); font-weight: 700; }
.riga { display: flex; gap: 10px; }
.riga > * { flex: 1; }

.classifica-lista { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.classifica-lista li {
  display: flex; align-items: center; gap: 12px;
  background: var(--superficie); border-radius: 16px; padding: 12px 14px;
}
.classifica-lista .pos { font-size: 22px; width: 32px; text-align: center; }
.classifica-lista .nome { flex: 1; font-weight: 700; }
.classifica-lista .n { font-weight: 800; color: var(--accento); }
.sfida { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px; text-align: center; }
.sfida .lato { background: var(--superficie); border-radius: 20px; padding: 14px 8px; }
.sfida .lato.vince { outline: 3px solid var(--accento); }
.sfida .numero { font-size: 36px; font-weight: 800; }
.sfida .vs { font-weight: 800; color: var(--testo-tenue); }
.nota { color: var(--testo-tenue); font-size: 14px; text-align: center; margin: 0; }

.categoria-riga { display: flex; gap: 8px; align-items: center; }
.categoria-riga input { flex: 1; }
.categoria-riga button { width: 48px; height: 48px; border-radius: 14px; background: var(--superficie); font-size: 18px; flex: none; }

#avviso {
  position: fixed; left: 50%; bottom: calc(100px + var(--sotto)); z-index: 20;
  transform: translateX(-50%);
  background: var(--testo); color: var(--sfondo);
  padding: 12px 18px; border-radius: 14px; font-weight: 600;
  max-width: calc(100vw - 32px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
  animation: sali .2s ease-out;
}
.suggerimento-home {
  margin: 0 16px 12px; padding: 12px 14px; border-radius: 16px;
  background: var(--superficie); box-shadow: var(--ombra);
  display: flex; gap: 10px; align-items: center; font-size: 14px;
}
.suggerimento-home p { margin: 0; flex: 1; }

@keyframes pulsa { 50% { transform: scale(1.12); } }
@keyframes parla { from { transform: scale(1) rotate(-4deg); } to { transform: scale(1.12) rotate(4deg); } }
@keyframes sali { from { transform: translateY(30px); opacity: 0; } }
#avviso { animation-name: compari; }
@keyframes compari { from { opacity: 0; transform: translate(-50%, 10px); } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
