/* =====================================================================
   FONT (file nella cartella "fonts" accanto a index.html)
   ===================================================================== */
@font-face {
  font-family: "Fraunces 72pt";
  src: url("fonts/Fraunces_72pt-Regular.ttf") format("truetype");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

/* =====================================================================
   COLORI E FONT
   (i colori vengono poi sostituiti da quelli scritti in config.js)
   ===================================================================== */
:root {
  --sfondo: #f5f2ed;
  --sfondo-chiaro: #ffffff;
  --testo: #2b2622;
  --accento: #6b1a2e;

  --font-titolo: "Fraunces 72pt", Georgia, serif;
  --font-testo: system-ui, sans-serif;

  /* velo del caricamento: stesso colore di --sfondo, con trasparenza */
  --velo: rgba(245, 242, 237, 0.85);

  /* menu a tendina lingua */
  --menu-sfondo: #ffffff;
  --menu-bordo: rgba(0, 0, 0, 0.14);
  --menu-hover: rgba(0, 0, 0, 0.06);

  /* ombra attorno alle pagine del menù */
  --ombra-pagine: 0 8px 20px rgba(0, 0, 0, 0.28);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  overflow: hidden;           /* niente scroll della pagina: scorre solo l'area del menù */
  overscroll-behavior: none;
}

body {
  height: 100vh;
  height: 100dvh;             /* altezza reale su smartphone (barra indirizzi inclusa) */
  display: flex;
  flex-direction: column;
  background: var(--sfondo);
  background-image: radial-gradient(ellipse at 50% 35%, var(--sfondo-chiaro), var(--sfondo) 75%);
  color: var(--testo);
  font-family: var(--font-testo);
}

/* =====================================================================
   INTESTAZIONE
   ===================================================================== */
header {
  position: relative;
  z-index: 20;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(0.9rem, env(safe-area-inset-top)) 5.5rem 0.4rem;
  min-height: 3.6rem;
}

.titolo-box { text-align: center; min-width: 0; max-width: 100%; }

/* Logo al posto del titolo */
.logo {
  display: block;
  margin: 0 auto 0.2rem;
  height: min(var(--logo-altezza, 80px), 13vh);
  max-width: 100%;
  width: auto;
  object-fit: contain;
}
.logo[hidden] { display: none; }

/* Nome nascosto alla vista ma letto da screen reader e motori di ricerca */
.solo-schermo {
  position: absolute !important;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

header h1 {
  font-family: var(--font-titolo);
  font-weight: normal;
  font-size: clamp(1.35rem, 4.5vw, 2.8rem);
  letter-spacing: 0.06em;
  line-height: 1.15;
  color: var(--accento);
  overflow-wrap: anywhere;
}

header p {
  margin-top: 0.15rem;
  font-size: clamp(0.8rem, 2.4vw, 1rem);
  opacity: 0.8;
  font-style: italic;
}
header p:empty { display: none; }

/* =====================================================================
   MENU A TENDINA LINGUA
   ===================================================================== */
.lingua {
  position: absolute;
  top: max(0.7rem, env(safe-area-inset-top));
  right: max(0.8rem, env(safe-area-inset-right));
}

#lingua-btn {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  height: 2.6rem;
  padding: 0 0.85rem;
  background: rgba(255, 255, 255, 0.8);
  color: var(--testo);
  font: inherit;
  font-size: 0.95rem;
  border: 1px solid var(--menu-bordo);
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s;
}
#lingua-btn:hover,
#lingua-btn[aria-expanded="true"] {
  background: #ffffff;
  border-color: var(--accento);
}
#lingua-btn:focus-visible,
#lingua-lista button:focus-visible,
#controlli button:focus-visible {
  outline: 2px solid var(--accento);
  outline-offset: 2px;
}

#lingua-flag, .opzione-flag {
  width: 24px;
  height: 18px;
  object-fit: cover;
  border-radius: 3px;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.2);
  flex: none;
}

.chevron { transition: transform 0.2s; flex: none; }
#lingua-btn[aria-expanded="true"] .chevron { transform: rotate(180deg); }

#lingua-lista {
  position: absolute;
  top: calc(100% + 0.4rem);
  right: 0;
  min-width: 100%;
  list-style: none;
  padding: 0.3rem;
  background: var(--menu-sfondo);
  border: 1px solid var(--menu-bordo);
  border-radius: 16px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.18);
  max-height: 60vh;
  overflow-y: auto;
}
#lingua-lista[hidden] { display: none; }
#lingua-lista:not([hidden]) { animation: apri-menu 0.15s ease-out; }
@keyframes apri-menu { from { opacity: 0; transform: translateY(-6px); } }

#lingua-lista button {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.65rem 0.8rem;
  background: transparent;
  color: var(--testo);
  font: inherit;
  font-size: 0.95rem;
  text-align: left;
  white-space: nowrap;
  border: 0;
  border-radius: 10px;
  cursor: pointer;
}
#lingua-lista button:hover { background: var(--menu-hover); }
#lingua-lista [aria-selected="true"] {
  color: var(--accento);
  background: rgba(0, 0, 0, 0.04);
}
#lingua-lista [aria-selected="true"]::after {
  content: "✓";
  margin-left: auto;
  padding-left: 0.8rem;
}

/* =====================================================================
   AREA DEL MENÙ
   ===================================================================== */
#area {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  overflow: hidden;           /* zoom 100%: nessuna barra laterale, anche durante il giro pagina */
  padding: 28px;              /* spazio attorno al libro: serve perché l'ombra non venga tagliata */
  -webkit-overflow-scrolling: touch;
}

/* con lo zoom attivo si può scorrere: barre sottili e discrete */
#area.zoomato {
  overflow: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--accento) transparent;
}
#area.zoomato::-webkit-scrollbar { width: 8px; height: 8px; }
#area.zoomato::-webkit-scrollbar-thumb { background: var(--accento); border-radius: 8px; }
#area.zoomato::-webkit-scrollbar-track { background: transparent; }

#box {
  flex: none;
  margin: auto;               /* centra il libro; se è più grande si può scorrere */
  position: relative;
}

#libro { width: 100%; height: 100%; }

.pagina {
  background: #fff;
  overflow: hidden;
  box-shadow: var(--ombra-pagine);   /* ombra attorno al menù */
}

.pagina img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #fff;
  user-select: none;
  -webkit-user-select: none;
  -webkit-user-drag: none;
  pointer-events: none;
}

/* =====================================================================
   CARICAMENTO
   ===================================================================== */
#stato {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 2rem;
  text-align: center;
  background: var(--velo);
}
#stato[hidden] { display: none; }
#stato-testo { opacity: 0.85; max-width: 28rem; line-height: 1.5; }

.spinner {
  width: 2.4rem;
  height: 2.4rem;
  border: 3px solid rgba(0, 0, 0, 0.12);
  border-top-color: var(--accento);
  border-radius: 50%;
  animation: gira 0.9s linear infinite;
}
#stato.errore .spinner { display: none; }
@keyframes gira { to { transform: rotate(360deg); } }

/* =====================================================================
   BARRA COMANDI (pagine, zoom, schermo intero)
   ===================================================================== */
#controlli {
  flex: none;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.5rem 1.6rem;
  padding: 0.6rem 0.8rem max(0.8rem, env(safe-area-inset-bottom));
}
#controlli[hidden] { display: none; }

.gruppo { display: flex; align-items: center; gap: 0.5rem; }

#controlli button {
  display: grid;
  place-items: center;
  min-width: 2.8rem;
  height: 2.8rem;
  padding: 0 0.6rem;
  background: rgba(255, 255, 255, 0.7);
  color: var(--accento);
  font: inherit;
  font-size: 0.9rem;
  border: 1px solid var(--accento);
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.2s, color 0.2s, opacity 0.2s;
}
#controlli button:hover:not(:disabled) { background: var(--accento); color: var(--sfondo); }
#controlli button:disabled { opacity: 0.35; cursor: default; }
#controlli svg {
  width: 1.3rem;
  height: 1.3rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#contatore { min-width: 4.8rem; text-align: center; font-size: 0.95rem; opacity: 0.9; }
#zoom-valore { min-width: 2.6rem; }

/* ===== Ombra interna al centro del libro ===== */
:root { --ombra-centro: rgba(0, 0, 0, 0.38); }   /* più alto = più scura */

.pagina::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.pagina.--right::after {
  background: linear-gradient(to right,
    var(--ombra-centro) 0%,
    rgba(0, 0, 0, 0.12) 4%,
    transparent 14%);
}

/* =====================================================================
   AVVISO
   ===================================================================== */
#avviso {
  position: fixed;
  left: 50%;
  bottom: calc(5.5rem + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 50;
  max-width: calc(100vw - 2rem);
  padding: 0.7rem 1.1rem;
  background: #ffffff;
  color: var(--testo);
  border: 1px solid var(--accento);
  border-radius: 12px;
  font-size: 0.9rem;
  text-align: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}
#avviso[hidden] { display: none; }

/* =====================================================================
   SMARTPHONE
   ===================================================================== */
@media (max-width: 600px) {
  :root { --ombra-pagine: 0 4px 12px rgba(0, 0, 0, 0.28); }

  header {
    padding-left: 0.8rem;
    padding-right: 0.8rem;
    flex-direction: column;
    gap: 0.5rem;
    padding-top: max(0.6rem, env(safe-area-inset-top));
  }
  .lingua { position: relative; top: auto; right: auto; order: -1; align-self: flex-end; }
  #lingua-btn { height: 2.4rem; font-size: 0.9rem; padding: 0 0.75rem; }
  #lingua-lista { right: 0; }
  #controlli { gap: 0.4rem 1rem; padding-top: 0.4rem; }
  #controlli button { min-width: 2.7rem; height: 2.7rem; }
  #area { padding: 16px; }
}

/* Smartphone in orizzontale: poco spazio verticale */
@media (max-height: 480px) and (orientation: landscape) {
  :root { --ombra-pagine: 0 3px 9px rgba(0, 0, 0, 0.28); }

  header { padding: 0.3rem 5.5rem 0.2rem; min-height: 2.8rem; flex-direction: row; }
  header p { display: none; }
  header h1 { font-size: 1.2rem; }
  .lingua { position: absolute; top: 0.3rem; right: 0.6rem; order: 0; align-self: auto; }
  #lingua-btn { height: 2.2rem; }
  #controlli { padding: 0.3rem 0.6rem 0.4rem; flex-wrap: nowrap; }
  #controlli button { min-width: 2.4rem; height: 2.4rem; }
  #area { padding: 12px; }
}

@media (max-width: 600px) {
  .pagina::after { display: none; }
}