:root {
  --sfondo: #515558;
  --scuro: #36383a;
  --scuro-hover: #232527;
  --barra: #ffffff;
  --barra-bordo: #e3e3e3;
  --icona: #222222;
  --icona-spenta: #cfcfcf;
  --testo: #1f2328;
  --testo-tenue: #5d6670;
  --evidenzia: #ffd84d;
  --accento: #1d4592;
  --carattere: "Open Sans", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--sfondo);
  color: #ffffff;
  font-family: var(--carattere);
  font-size: 15px;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

button, input { font: inherit; color: inherit; }
svg { width: 20px; height: 20px; flex: none; }
:focus-visible { outline: 3px solid var(--evidenzia); outline-offset: 2px; }
[hidden] { display: none !important; }

.app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* Area del libro */
.palco {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 96px 22px;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.contenitore { position: relative; }
.contenitore.zoomato { visibility: hidden; }

.contenitore.pronto::before {
  content: "";
  position: absolute;
  inset: 0;
  box-shadow: 0 6px 26px rgba(0, 0, 0, 0.38);
  pointer-events: none;
}

.contenitore.copertina::before { left: 50%; }
.contenitore.retro::before { right: 50%; }
.contenitore.singola::before { left: 0; right: 0; }

.contenitore.doppia::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 70px;
  margin-left: -35px;
  background: linear-gradient(90deg, rgba(0,0,0,0) 0%, rgba(0,0,0,0.07) 38%, rgba(0,0,0,0.2) 50%, rgba(0,0,0,0.07) 62%, rgba(0,0,0,0) 100%);
  pointer-events: none;
  z-index: 40;
  transition: opacity 0.15s;
}

.contenitore.sfogliando::after { opacity: 0; }

.pagina {
  background: #ffffff;
  overflow: hidden;
}

.pagina::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 32px;
  height: 32px;
  margin: -16px 0 0 -16px;
  border-radius: 50%;
  border: 3px solid #e2e4e7;
  border-top-color: #8a9096;
  animation: gira 0.9s linear infinite;
}

.pagina img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
  -webkit-user-drag: none;
  opacity: 0;
  transition: opacity 0.2s;
}

.pagina img.caricata { opacity: 1; }

@keyframes gira { to { transform: rotate(360deg); } }

/* Zoom */
.strato-zoom {
  position: absolute;
  inset: 0;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
  z-index: 20;
}

.strato-zoom.trascina { cursor: grabbing; }

.foglio-zoom {
  position: absolute;
  left: 0;
  top: 0;
  display: flex;
  transform-origin: 0 0;
  box-shadow: 0 6px 26px rgba(0, 0, 0, 0.38);
  will-change: transform;
}

.foglio-zoom img {
  display: block;
  width: 50%;
  height: 100%;
  background: #ffffff;
  -webkit-user-drag: none;
  pointer-events: none;
}

.foglio-zoom.una img { width: 100%; }

/* Comandi sovrapposti */
.contatore {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 30;
  min-width: 64px;
  padding: 0 18px;
  height: 46px;
  line-height: 46px;
  border-radius: 23px;
  background: var(--scuro);
  color: #ffffff;
  font-size: 14px;
  text-align: center;
  font-variant-numeric: tabular-nums;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
}

.chiudi-libro {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 30;
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 50%;
  background: var(--scuro);
  color: #ffffff;
  display: grid;
  place-items: center;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
}

.chiudi-libro:hover { background: var(--scuro-hover); }

.freccia {
  position: absolute;
  top: 50%;
  z-index: 30;
  width: 60px;
  height: 60px;
  margin-top: -30px;
  border: 0;
  border-radius: 50%;
  background: rgba(54, 56, 58, 0.55);
  color: #ffffff;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 0.15s;
}

.freccia svg { width: 30px; height: 30px; }
.freccia:hover { background: var(--scuro); }
.freccia:disabled { background: transparent; color: rgba(255, 255, 255, 0.25); cursor: default; }
.freccia-sx { left: 18px; }
.freccia-dx { right: 18px; }

.avviso {
  position: absolute;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px;
  background: var(--sfondo);
  color: #e9ebed;
  font-size: 16px;
  text-align: center;
}

.rotella {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 4px solid rgba(255, 255, 255, 0.2);
  border-top-color: #ffffff;
  animation: gira 0.9s linear infinite;
}

.avviso.errore .rotella { display: none; }

.torna {
  display: inline-block;
  padding: 12px 22px;
  border-radius: 24px;
  background: #ffffff;
  color: var(--testo);
  font-weight: 600;
  text-decoration: none;
}

.torna:hover { background: #e9ebed; }

/* Barra in basso */
.barra {
  flex: none;
  position: relative;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  height: 46px;
  padding: 0 10px env(safe-area-inset-bottom, 0px);
  box-sizing: content-box;
  background: var(--barra);
  border-top: 1px solid var(--barra-bordo);
}

.tasto {
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--icona);
  display: grid;
  place-items: center;
  cursor: pointer;
}

.tasto:hover { background: #f0f0f0; }
.tasto:disabled { color: var(--icona-spenta); background: transparent; cursor: default; }
.tasto[aria-expanded="true"], .tasto.attivo { background: #ececec; }

.ancora { position: relative; }

.menu {
  position: absolute;
  bottom: calc(100% + 10px);
  left: 50%;
  transform: translateX(-50%);
  min-width: 270px;
  padding: 6px;
  border-radius: 10px;
  background: #ffffff;
  color: var(--testo);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.28);
}

.voce {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 42px;
  padding: 0 12px;
  border: 0;
  border-radius: 6px;
  background: none;
  color: var(--testo);
  font-size: 14.5px;
  text-align: left;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
}

.voce:hover { background: #f1f3f5; }
.voce svg { color: #4a4f55; }

.vai {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
  padding: 8px 12px 6px;
  border-top: 1px solid #eceef0;
  font-size: 14.5px;
}

.vai label { flex: 1; white-space: nowrap; }

.vai input {
  width: 64px;
  height: 36px;
  padding: 0 6px;
  border: 1px solid #c9ced4;
  border-radius: 6px;
  color: var(--testo);
  text-align: center;
}

.vai button {
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 6px;
  background: var(--scuro);
  color: #ffffff;
  display: grid;
  place-items: center;
  cursor: pointer;
}

/* Pannelli laterali */
.laterale {
  position: fixed;
  top: 0;
  left: 0;
  bottom: calc(47px + env(safe-area-inset-bottom, 0px));
  z-index: 55;
  width: min(380px, 100%);
  display: flex;
  flex-direction: column;
  background: #ffffff;
  color: var(--testo);
  box-shadow: 8px 0 30px rgba(0, 0, 0, 0.3);
}

.laterale-testa {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 10px 10px 18px;
  border-bottom: 1px solid #eceef0;
}

.laterale-testa h2 { margin: 0; font-size: 17px; font-weight: 600; }

.chiudi-pannello {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: #f1f3f5;
  color: var(--testo);
  display: grid;
  place-items: center;
  cursor: pointer;
}

.chiudi-pannello:hover { background: #e3e6e9; }

.campo-cerca { padding: 14px 16px 6px; }

.campo-cerca input {
  width: 100%;
  height: 46px;
  padding: 0 14px;
  border: 2px solid #c9ced4;
  border-radius: 8px;
  color: var(--testo);
  font-size: 16px;
}

.campo-cerca input:focus { border-color: var(--scuro); outline: none; }

.stato-cerca { margin: 6px 18px 10px; color: var(--testo-tenue); font-size: 14px; line-height: 1.45; }

.risultati {
  list-style: none;
  margin: 0;
  padding: 0 10px 16px;
  overflow-y: auto;
  flex: 1;
}

.risultati li + li { margin-top: 4px; }

.risultato {
  width: 100%;
  display: grid;
  grid-template-columns: 62px 1fr;
  gap: 10px;
  align-items: start;
  padding: 10px 8px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--testo);
  text-align: left;
  cursor: pointer;
}

.risultato:hover { background: #f1f3f5; }
.risultato .num { color: var(--testo-tenue); font-size: 13px; font-variant-numeric: tabular-nums; padding-top: 1px; }
.risultato .nome { display: block; font-weight: 600; font-size: 14.5px; line-height: 1.3; }
.risultato .dettagli { display: block; margin-top: 2px; color: var(--testo-tenue); font-size: 13.5px; line-height: 1.4; }
.risultato mark { background: var(--evidenzia); color: inherit; border-radius: 2px; }

.griglia {
  flex: 1;
  overflow-y: auto;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px 12px;
  padding: 16px;
  align-content: start;
}

.miniatura {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 6px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--testo-tenue);
  font-size: 13px;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
}

.miniatura:hover { background: #f1f3f5; }

.miniatura img {
  width: 100%;
  aspect-ratio: var(--rapporto, 0.71);
  object-fit: cover;
  background: #f1f3f5;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.22);
}

.miniatura.attuale { background: #e8ecf3; color: var(--testo); font-weight: 600; }
.miniatura.attuale img { outline: 3px solid var(--scuro); }

.messaggio {
  position: fixed;
  left: 50%;
  bottom: calc(64px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  z-index: 70;
  padding: 10px 18px;
  border-radius: 20px;
  background: var(--scuro);
  color: #ffffff;
  font-size: 14px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
}

@media (max-width: 760px) {
  .palco { padding: 70px 8px 12px; }
  .freccia { display: none; }
  .contatore { height: 40px; line-height: 40px; padding: 0 14px; font-size: 13px; }
  .chiudi-libro { width: 42px; height: 42px; }
  .barra { gap: 0; padding-left: 4px; padding-right: 4px; }
  .tasto { width: 40px; height: 40px; }
  .menu {
    position: fixed;
    left: 8px;
    right: 8px;
    bottom: calc(56px + env(safe-area-inset-bottom, 0px));
    transform: none;
    min-width: 0;
  }
  .laterale { width: 100%; }
  .griglia { grid-template-columns: 1fr 1fr 1fr; }
}

@media (max-height: 520px) and (max-width: 1000px) {
  .palco { padding: 10px 64px; }
  .freccia { display: grid; width: 44px; height: 44px; margin-top: -22px; }
  .freccia-sx { left: 8px; }
  .freccia-dx { right: 8px; }
  .contatore { top: 8px; left: 8px; }
  .chiudi-libro { top: 8px; right: 8px; }
}

@media (prefers-reduced-motion: reduce) {
  .pagina::before, .rotella { animation-duration: 3s; }
  .pagina img { transition: none; }
}
