/* Colori dal tema di Telegram (chiaro e scuro); i valori dopo la virgola servono solo fuori da Telegram. */
:root {
  color-scheme: light dark;
  --bg: var(--tg-theme-bg-color, #ffffff);
  --testo: var(--tg-theme-text-color, #111111);
  --tenue: var(--tg-theme-hint-color, #7d7d85);
  --bottone: var(--tg-theme-button-color, #2481cc);
  --bottone-testo: var(--tg-theme-button-text-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #efeff3);
  --link: var(--tg-theme-link-color, var(--bottone));
  --errore: var(--tg-theme-destructive-text-color, #d93a3a);
  --bordo: color-mix(in srgb, var(--tenue) 30%, transparent);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--tg-theme-bg-color, #17212b);
    --testo: var(--tg-theme-text-color, #f5f5f5);
    --bg2: var(--tg-theme-secondary-bg-color, #232e3c);
    --bottone: var(--tg-theme-button-color, #5288c1);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--testo); }
body {
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
  overflow-wrap: anywhere;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; touch-action: manipulation; }
button:disabled { opacity: 0.5; cursor: default; }
h2, h3 { margin: 0; }

/* Barra dei filtri: chip che scorrono in orizzontale, sempre in vista */
.barra {
  position: sticky; top: 0; z-index: 5; background: var(--bg);
  border-bottom: 1px solid var(--bordo);
}
.filtri {
  display: flex; gap: 8px; overflow-x: auto; padding: 8px 10px; scrollbar-width: none;
}
.filtri::-webkit-scrollbar { display: none; }
.chip {
  flex: none; min-height: 40px; padding: 0 16px; border-radius: 20px;
  background: var(--bg2); white-space: nowrap; max-width: 60vw; overflow: hidden; text-overflow: ellipsis;
}
.chip.attivo { background: var(--bottone); color: var(--bottone-testo); }
.chip.azzera { color: var(--link); background: none; border: 1px solid var(--bordo); }

/* Griglia a 3 colonne: le celle hanno sempre la stessa forma, quindi niente salti */
.griglia { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px; }
.cella {
  position: relative; aspect-ratio: 1 / 1; overflow: hidden; background: var(--bg2); display: block; width: 100%;
}
.cella img, .anteprima img {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: opacity 0.15s;
  -webkit-touch-callout: none; user-select: none;
}
.cella.pronta img, .anteprima.pronta img { opacity: 1; }
.cella.scheletro { animation: pulsa 1.2s ease-in-out infinite; }
.cella.senza-anteprima::before, .anteprima.senza-anteprima::before {
  content: "🖼"; position: absolute; inset: 0; display: grid; place-items: center; font-size: 28px; opacity: 0.35;
}
@keyframes pulsa { 50% { opacity: 0.55; } }
@media (prefers-reduced-motion: reduce) { .cella.scheletro { animation: none; } }
.badge {
  position: absolute; right: 4px; bottom: 4px; padding: 1px 6px; border-radius: 8px; font-size: 12px;
  background: rgb(0 0 0 / 0.6); color: #fff; line-height: 1.5;
}

.piede { padding: 16px; text-align: center; display: flex; flex-direction: column; gap: 12px; align-items: center; }
.sentinella { height: 1px; }
.vuoto { margin: 24px 0; text-align: center; }
.tenue { color: var(--tenue); }

/* Bottoni principali */
.primario {
  min-height: 48px; padding: 0 12px; border-radius: 12px; background: var(--bottone); color: var(--bottone-testo);
  font-weight: 600; flex: 1;
}
.primario.secondario { background: var(--bg2); color: var(--testo); }
.link { color: var(--link); min-height: 44px; padding: 0 4px; }

/* Pannello dal basso */
.pannello { position: fixed; inset: 0; z-index: 20; display: flex; flex-direction: column; justify-content: flex-end; }
.sfondo { position: absolute; inset: 0; background: rgb(0 0 0 / 0.45); }
.foglio {
  position: relative; background: var(--bg); border-radius: 16px 16px 0 0; max-height: 85vh; display: flex; flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom);
}
.foglio-testa { display: flex; justify-content: space-between; align-items: center; padding: 8px 16px; }
.opzioni { overflow-y: auto; overscroll-behavior: contain; padding: 0 8px 12px; display: flex; flex-direction: column; }
.opzione {
  min-height: 48px; padding: 0 12px; text-align: left; border-radius: 10px; display: flex; justify-content: space-between; align-items: center; gap: 12px; width: 100%;
}
.opzione.attiva { background: var(--bg2); font-weight: 600; }
.opzione.attiva::after { content: "✓"; }
.opzione .conteggio { color: var(--tenue); font-size: 14px; }
.creator-corpo { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.ricerca {
  margin: 0 12px 8px; min-height: 44px; padding: 0 14px; border-radius: 10px; border: 1px solid var(--bordo);
  background: var(--bg2); color: var(--testo); font: inherit; font-size: 16px; /* 16px: iOS non zooma */
}
.gruppo { padding: 8px 12px 2px; color: var(--tenue); font-size: 14px; }

/* Dettaglio a tutto schermo */
.dettaglio { position: fixed; inset: 0; z-index: 10; background: var(--bg); overflow-y: auto; overscroll-behavior: contain; }
.dettaglio-corpo { max-width: 640px; margin: 0 auto; padding: 0 0 24px; display: flex; flex-direction: column; gap: 12px; }
.anteprima { position: relative; width: 100%; aspect-ratio: 1 / 1; max-height: 70vh; background: var(--bg2); }
.anteprima img { object-fit: contain; }
.didascalia, .dati, .azioni, .esito, .suggerimento { margin: 0 16px; }
.didascalia { white-space: pre-wrap; }
.dati { color: var(--tenue); font-size: 14px; }
.tag { color: var(--link); min-height: 32px; display: inline; }
.azioni { display: flex; gap: 10px; }
.esito { min-height: 1.4em; }
.esito.errore { color: var(--errore); }
.suggerimento { color: var(--tenue); font-size: 14px; }

/* Errore a tutto schermo */
.fatale { min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 20px; padding: 24px; text-align: center; }
.fatale-testo { font-size: 18px; margin: 0; }
.fatale .primario { flex: none; }
