:root {
  --bg: #16181b;
  --bar: #1f2226;
  --line: #2c3036;
  --text: #f1f2f4;
  --muted: #a3a9b1;
  --accent: #f26a2e;
  --bar-h: 56px;
  --foot-h: 52px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
}

svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tools svg path[d^="M4 4h6"] { fill: currentColor; stroke: none; }

button, .tools a {
  display: inline-grid;
  place-items: center;
  width: 42px;
  height: 42px;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: inherit;
  cursor: pointer;
  text-decoration: none;
}
button:hover, .tools a:hover { background: rgba(255, 255, 255, .08); }
button:focus-visible, .tools a:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

/* --- Barre ---------------------------------------------------------------- */

.bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 max(12px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
  background: var(--bar);
  flex: none;
}
.bar.top { height: calc(var(--bar-h) + env(safe-area-inset-top)); padding-top: env(safe-area-inset-top); border-bottom: 1px solid var(--line); }
.bar.bottom { height: calc(var(--foot-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom); border-top: 1px solid var(--line); }

.heading { min-width: 0; flex: 1; }
.heading h1 { margin: 0; font-size: 16px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.heading p { margin: 1px 0 0; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tools { display: flex; align-items: center; gap: 2px; }
.badge { margin-right: 6px; padding: 3px 8px; border-radius: 99px; background: var(--accent); color: #fff; font-size: 12px; font-weight: 600; }

#slider { flex: 1; accent-color: var(--accent); min-width: 0; height: 28px; }
#counter { min-width: 72px; text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }

/* --- Libro ---------------------------------------------------------------- */

#stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: 16px 64px;
}
#book-box { position: relative; transition: transform .45s ease; }
#book-box .book {
  min-width: 0 !important;
  min-height: 0 !important;
  touch-action: none !important;
  user-select: none;
  -webkit-user-select: none;
}
.page {
  background: #fff;
  overflow: hidden;
  cursor: zoom-in;
}
.page img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  -webkit-user-drag: none;
}
/* Ombra della piega centrale in modalità doppia pagina. */
.--landscape .page.--left::after,
.--landscape .page.--right::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: 6%;
  pointer-events: none;
}
.--landscape .page.--left::after { right: 0; background: linear-gradient(to left, rgba(0,0,0,.18), transparent); }
.--landscape .page.--right::after { left: 0; background: linear-gradient(to right, rgba(0,0,0,.18), transparent); }

.nav {
  position: absolute;
  top: 50%;
  z-index: 5;
  width: 48px;
  height: 48px;
  margin-top: -24px;
  border-radius: 50%;
  background: rgba(31, 34, 38, .85);
  box-shadow: 0 2px 10px rgba(0, 0, 0, .35);
}
.nav.prev { left: 8px; }
.nav.next { right: 8px; }
.nav:disabled { opacity: 0; pointer-events: none; }
.nav:hover { background: #2a2e33; }

.hint {
  position: absolute;
  bottom: 14px;
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  padding: 6px 14px;
  border-radius: 99px;
  background: rgba(0, 0, 0, .65);
  color: #fff;
  font-size: 13px;
  white-space: nowrap;
  pointer-events: none;
  transition: opacity .4s;
}
.hint.gone { opacity: 0; }

@media (max-width: 700px) {
  #stage { padding: 10px; }
  .nav { width: 40px; height: 40px; margin-top: -20px; background: rgba(31, 34, 38, .7); }
  .nav.prev { left: 4px; }
  .nav.next { right: 4px; }
}
@media (hover: none) and (max-width: 700px) {
  /* Su telefono si sfoglia con lo swipe: le frecce restano piccole e discrete. */
  .nav { opacity: .75; }
}

/* --- Overlay: miniature e zoom -------------------------------------------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

#thumbs .grid {
  flex: 1;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 20px 16px;
  padding: 20px max(16px, env(safe-area-inset-right)) calc(20px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  align-content: start;
}
#thumbs a { color: var(--muted); text-decoration: none; font-size: 13px; text-align: center; }
#thumbs a img {
  display: block;
  width: 100%;
  height: auto;
  margin-bottom: 6px;
  background: #fff;
  border-radius: 3px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .4);
  outline: 3px solid transparent;
  outline-offset: 2px;
}
#thumbs a.current { color: var(--text); font-weight: 600; }
#thumbs a.current img, #thumbs a:hover img { outline-color: var(--accent); }
@media (max-width: 500px) {
  #thumbs .grid { grid-template-columns: repeat(3, 1fr); gap: 14px 10px; }
}

#zoom { background: #0d0e10; }
.zoom-stage { position: relative; flex: 1; overflow: hidden; touch-action: none; }
.zoom-pan { width: 100%; height: 100%; cursor: grab; }
.zoom-pan:active { cursor: grabbing; }
.zoom-pan img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  user-select: none;
  -webkit-user-drag: none;
}
.zoom-tools {
  position: absolute;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px;
  border-radius: 14px;
  background: rgba(31, 34, 38, .92);
  box-shadow: 0 4px 18px rgba(0, 0, 0, .45);
}
.zoom-tools output { min-width: 64px; text-align: center; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 14px; }
.zoom-tools button:disabled { opacity: .3; cursor: default; background: none; }
/* "🔍 Zoom ✕": dice che si è in zoom e che da qui si torna a sfogliare. */
.zoom-close {
  position: absolute;
  top: calc(12px + env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  display: inline-flex;
  gap: 6px;
  width: auto;
  height: 38px;
  padding: 0 10px 0 11px;
  border-radius: 99px;
  background: rgba(31, 34, 38, .92);
  box-shadow: 0 2px 10px rgba(0, 0, 0, .4);
  font-size: 14px;
  font-weight: 600;
  letter-spacing: .01em;
}
.zoom-close svg { width: 18px; height: 18px; }
.zoom-close .zoom-icon { color: var(--accent); stroke-width: 2.2; }
.zoom-close span { padding-right: 6px; border-right: 1px solid rgba(255, 255, 255, .25); }
.zoom-close:hover { background: #2a2e33; }

/* --- Nessun volantino ----------------------------------------------------- */

.empty {
  margin: auto;
  padding: 24px;
  text-align: center;
  color: var(--muted);
}
.empty svg { width: 56px; height: 56px; color: var(--accent); stroke-width: 1.2; }
.empty h1 { margin: 12px 0 6px; color: var(--text); font-size: 22px; }
.empty p { margin: 0; }
