:root {
  --brand: #14284B;
  --accent: #1FA2DE;
  --ink: #1b2233;
  --muted: #5b6475;
  --line: #d9dfe9;
  --bg: #e9edf4;
  --panel: #ffffff;
  --hit: rgba(255, 196, 0, .45);
  --link: #1FA2DE;                     /* colore di evidenziazione dei link (impostabile) */
  --hot: rgba(31, 162, 222, .30);      /* sfondo al passaggio del mouse */
  --hot-soft: rgba(31, 162, 222, .10); /* sfondo con "evidenzia sempre" */
  --hot-glow: rgba(31, 162, 222, .55); /* alone */
  --ph: 800px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100dvh;
  overflow: hidden;
  font: 15px/1.4 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.sr-only {
  position: absolute !important; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.skip { position: absolute; left: 8px; top: -60px; z-index: 50; padding: 10px 14px; background: var(--brand); color: #fff; border-radius: 6px; }
.skip:focus { top: 8px; }

:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

button, .btn, .tool, .nav { font: inherit; color: inherit; }
button { cursor: pointer; }

/* Barra superiore */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 6px 12px; padding-top: max(6px, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line);
  min-height: 56px;
}
.logo { display: flex; align-items: center; text-decoration: none; color: var(--brand); flex: none; }
.logo img { height: 36px; width: auto; }
.logo-text { font-weight: 600; letter-spacing: .04em; font-size: 17px; }
.logo-text b { color: var(--accent); }
.title { flex: 1; margin: 0; font-size: 15px; font-weight: 600; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tools { display: flex; gap: 2px; margin-left: auto; }
/* Validità dell'offerta nella barra in alto (flyer); se scaduta, link al flyer in corso */
.validity { margin: 0; flex: none; font-size: 13px; color: var(--muted); white-space: nowrap; }
.validity.expired { display: flex; align-items: center; gap: 8px; padding: 4px 4px 4px 12px; border-radius: 99px; background: #c0392b; color: #fff; font-weight: 700; }
.validity a { margin-left: 8px; padding: 3px 10px; border-radius: 99px; background: var(--brand); color: #fff; text-decoration: none; font-weight: 600; }
.validity.expired a { margin-left: 0; background: #fff; color: #c0392b; padding: 4px 12px; }
.validity.expired a:hover { background: #fdecea; }
/* Offerte scadute: riquadro sopra la pagina all'apertura */
.expired-notice {
  position: absolute; z-index: 15; left: 50%; top: 18px; transform: translateX(-50%);
  width: min(460px, calc(100% - 32px)); padding: 18px 48px 18px 20px; border-radius: 12px;
  background: #fff; border-top: 6px solid #c0392b; box-shadow: 0 10px 40px rgba(0, 0, 0, .35); text-align: center;
  animation: en-in .25s ease-out;
}
@keyframes en-in { from { opacity: 0; transform: translate(-50%, -10px); } }
@media (prefers-reduced-motion: reduce) { .expired-notice { animation: none; } }
.expired-notice .en-title { margin: 0 0 4px; font-size: 19px; font-weight: 700; color: #c0392b; }
.expired-notice .en-text { margin: 0; color: var(--muted); }
.expired-notice .en-actions:not(:empty) { margin-top: 14px; }
.expired-notice .en-go {
  display: inline-block; padding: 12px 22px; border-radius: 10px; background: var(--brand); color: #fff;
  font-size: 16px; font-weight: 700; text-decoration: none;
}
.expired-notice .en-go:hover { filter: brightness(1.15); }
.expired-notice .en-go:focus-visible, .expired-notice .en-close:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.expired-notice .en-close { position: absolute; top: 8px; right: 8px; width: 36px; height: 36px; display: grid; place-items: center; background: none; border: 0; border-radius: 8px; color: var(--muted); cursor: pointer; }
.expired-notice .en-close svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; }
.validity a:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.tool {
  display: inline-flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  min-width: 52px; height: 44px; padding: 2px 6px;
  background: none; border: 0; border-radius: 8px; color: var(--brand); text-decoration: none; font-size: 11px;
}
.tool:hover { background: #eef2f8; }

/* Area di lettura */
.stage {
  position: relative; overflow: hidden; outline: none;
  touch-action: pan-y pinch-zoom;
  background: radial-gradient(ellipse at center, rgba(255, 255, 255, .45) 0%, transparent 70%), var(--bg);
}
/* Pagina ingrandita con due dita: il dito sposta la vista anche a destra e sinistra (lo swipe non sfoglia) */
.stage.pinched { touch-action: pan-x pan-y pinch-zoom; }
.stage.has-image { background: var(--bg-image) center / cover no-repeat, var(--bg); }
.book {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  perspective: 2600px;
}
.pg, .face {
  position: absolute; top: 0; height: 100%; overflow: hidden;
  background: #fff;
  box-shadow: 0 2px 14px rgba(20, 40, 75, .18);
}
.pg { width: 100%; left: 0; cursor: zoom-in; }
.book.double .pg { width: 50%; }
.book.double .pg.right { left: 50%; }
.pg img, .face img { display: block; width: 100%; height: 100%; user-select: none; -webkit-user-drag: none; }
.book.double .pg.left::after, .book.double .pg.right::after {
  content: ""; position: absolute; top: 0; bottom: 0; width: 6%; pointer-events: none;
}
.book.double .pg.left::after { right: 0; background: linear-gradient(to left, rgba(0, 0, 0, .12), transparent); }
.book.double .pg.right::after { left: 0; background: linear-gradient(to right, rgba(0, 0, 0, .12), transparent); }

/* Animazione di sfoglio */
.leaf {
  position: absolute; top: 0; width: 50%; height: 100%; z-index: 5;
  transform-style: preserve-3d;
  will-change: transform;
  transition: transform .65s cubic-bezier(.35, .1, .25, 1);
}
.leaf.fwd { left: 50%; transform-origin: left center; }
.leaf.fwd.go { transform: rotateY(-180deg); }
.leaf.bwd { left: 0; transform-origin: right center; }
.leaf.bwd.go { transform: rotateY(180deg); }
.face { width: 100%; left: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden; }
.face.back { transform: rotateY(180deg); }
.face.blank { background: transparent; box-shadow: none; }

.pg.enter-next { animation: enter-next .35s ease-out; }
.pg.enter-prev { animation: enter-prev .35s ease-out; }
@keyframes enter-next { from { transform: translateX(40%); opacity: 0; } }
@keyframes enter-prev { from { transform: translateX(-40%); opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
  .leaf, .pg.enter-next, .pg.enter-prev { transition: none; animation: none; }
}

/* Strato di testo (invisibile, per screen reader, selezione e ricerca) */
.txt { position: absolute; inset: 0; line-height: 1; }
.txt span { position: absolute; white-space: pre; color: transparent; transform-origin: 0 0; }
.txt span::selection { background: rgba(31, 162, 222, .35); }
.txt span.hit { background: var(--hit); border-radius: 2px; }

/* Link */
.links { position: absolute; inset: 0; }
.hot { position: absolute; z-index: 1; display: block; border-radius: 3px; cursor: pointer; transition: background-color .12s, box-shadow .12s; }
.hot.card { z-index: 0; border-radius: 6px; }
.hot:hover {
  background: var(--hot);
  box-shadow: 0 0 0 2px var(--link), 0 0 10px var(--hot-glow);
}
.hot:focus-visible { background: var(--hot); outline: 3px solid var(--link); outline-offset: 1px; }
.show-links .hot { background: var(--hot-soft); }

/* Frecce laterali */
.edge {
  position: absolute; top: 50%; transform: translateY(-50%); z-index: 6;
  width: 48px; height: 96px; display: grid; place-items: center;
  background: rgba(255, 255, 255, .8); color: var(--brand);
  border: 1px solid var(--line); border-radius: 10px;
}
.edge:hover { background: #fff; }
.edge:disabled { opacity: 0; pointer-events: none; }
.edge-prev { left: 10px; }
.edge-next { right: 10px; }
.edge svg { width: 28px; height: 28px; }

/* Barra inferiore */
.pager {
  display: flex; align-items: center; justify-content: center; gap: 4px;
  padding: 6px 12px; padding-bottom: max(6px, env(safe-area-inset-bottom));
  background: var(--panel); border-top: 1px solid var(--line); position: relative;
}
.nav {
  width: 44px; height: 44px; display: grid; place-items: center;
  background: none; border: 0; border-radius: 8px; color: var(--brand);
}
.nav:hover { background: #eef2f8; }
.nav:disabled { opacity: .35; cursor: default; background: none; }
.goto { display: flex; align-items: center; gap: 6px; margin: 0 6px; color: var(--muted); }
.goto input {
  width: 4.5em; height: 36px; text-align: center; font: inherit; color: var(--ink);
  border: 1px solid var(--line); border-radius: 6px;
}
.textver { position: absolute; right: 16px; color: var(--muted); font-size: 13px; }

/* Pannelli laterali */
dialog { color: var(--ink); }
dialog::backdrop { background: rgba(12, 22, 40, .5); }
.drawer {
  margin: 0 0 0 auto; padding: 0; border: 0;
  width: min(380px, 100vw); height: 100dvh; max-height: none; max-width: 100vw;
  background: var(--panel);
}
.drawer[open] { display: flex; flex-direction: column; }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 10px 10px 18px; border-bottom: 1px solid var(--line);
}
.drawer-head h2 { margin: 0; font-size: 18px; color: var(--brand); }
.drawer-body { flex: 1; overflow: auto; padding: 14px 18px 24px; }
.close { width: 44px; height: 44px; display: grid; place-items: center; background: none; border: 0; border-radius: 8px; color: var(--brand); }
.close:hover { background: #eef2f8; }

.index-list, .results, .sharelist, .thumbs { list-style: none; margin: 0; padding: 0; }
.index-list button, .results button {
  width: 100%; display: flex; justify-content: space-between; gap: 12px; text-align: left;
  padding: 11px 10px; background: none; border: 0; border-bottom: 1px solid var(--line);
}
.index-list button:hover, .results button:hover, .index-list a:hover { background: #eef2f8; }
.index-list a {
  display: flex; justify-content: space-between; gap: 12px; align-items: center;
  padding: 11px 10px; border-bottom: 1px solid var(--line); color: inherit; text-decoration: none;
}
.index-list a svg { width: 16px; height: 16px; color: var(--muted); }
.index-list .pn, .results .pn { color: var(--muted); white-space: nowrap; }
.results .snip { display: block; color: var(--muted); font-size: 13px; }
/* Risultato per codice prodotto: prima "Vai alla pagina", accanto "Apri la scheda" */
.results .code-hit { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 12px 10px; border-bottom: 1px solid var(--line); background: #f4f8fd; }
.results .code-hit .what { flex: 1 1 100%; font-weight: 600; }
.results .code-hit .btn { width: auto; display: inline-flex; padding: 0 14px; border: 1px solid var(--line); }
.results .code-hit .btn.primary { border-color: var(--brand); }
/* Indice a due livelli (catalogo): sezioni e, aperte, le loro categorie */
.index-list .sec { display: flex; align-items: stretch; border-bottom: 1px solid var(--line); }
.index-list .sec > button:first-child { flex: 1; border-bottom: 0; }
.index-list .sec .toggle { width: 44px; flex: none; justify-content: center; align-items: center; border-bottom: 0; border-left: 1px solid var(--line); color: var(--brand); }
.index-list .sec .toggle svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; transition: transform .15s; }
.index-list .sec .toggle[aria-expanded="true"] svg { transform: rotate(180deg); }
.index-list .sub { list-style: none; margin: 0; padding: 0 0 6px 14px; background: #f7f9fc; border-bottom: 1px solid var(--line); }
.index-list .sub button { padding: 8px 10px; font-size: 14px; border-bottom: 1px solid #e8edf4; }
.results mark { background: var(--hit); color: inherit; }

.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 12px; }
.thumbs button { width: 100%; padding: 0; background: none; border: 2px solid transparent; border-radius: 4px; }
.thumbs img { display: block; width: 100%; aspect-ratio: var(--ar); background: #f1f3f7; box-shadow: 0 1px 4px rgba(0, 0, 0, .15); }
.thumbs span { display: block; font-size: 12px; color: var(--muted); padding: 3px 0; }
.thumbs [aria-current="true"] { border-color: var(--accent); }

label { display: block; }
#search-input, #share-url, #print-from, #print-to {
  font: inherit; height: 40px; padding: 0 10px; border: 1px solid var(--line); border-radius: 6px;
}
#search-input { width: 100%; margin-top: 6px; }
#print-from, #print-to { width: 5em; }
.status { color: var(--muted); min-height: 1.4em; }
.check { display: flex; align-items: center; gap: 8px; margin: 8px 0; }
fieldset { border: 0; padding: 0; margin: 0 0 16px; }
legend { font-weight: 600; margin-bottom: 6px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; min-height: 40px; padding: 0 14px;
  background: #fff; border: 1px solid var(--line); border-radius: 8px; color: var(--brand); text-decoration: none; font-weight: 600;
}
.btn:hover { background: #eef2f8; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.wide { width: 100%; margin: 4px 0; }
.copyrow { display: flex; gap: 8px; margin: 10px 0 4px; }
#share-url { flex: 1; min-width: 0; }

/* Ingrandimento */
/* Livello sopra l'area delle pagine: le barre in alto e in basso restano visibili */
.zoom {
  position: absolute; inset: 0; z-index: 20; overflow: hidden;
  background: rgba(12, 22, 40, .55);
  animation: zoom-in-bg .18s ease-out;
}
@keyframes zoom-in-bg { from { background: rgba(12, 22, 40, 0); } }
.zoom-view { position: absolute; inset: 0; overflow: hidden; touch-action: none; cursor: zoom-out; }
.zoom-view.dragging { cursor: grabbing; }
.zoom-content {
  position: absolute; left: 0; top: 0; transform-origin: 0 0; background: #fff; cursor: grab;
  box-shadow: 0 8px 40px rgba(0, 0, 0, .45);
}
.zoom-content img { display: block; width: 100%; height: 100%; user-select: none; -webkit-user-drag: none; }
.stage.zooming .edge { display: none; }
/* Comandi dello zoom in colonna a destra: chiudi in alto, poi ingrandisci e riduci */
.zoom-tools {
  position: absolute; right: 12px; top: 12px; z-index: 2;
  display: flex; flex-direction: column; gap: 8px;
}
.zbtn {
  width: 44px; height: 44px; border-radius: 50%; border: 0; padding: 0; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--panel) 92%, transparent); color: var(--brand);
  box-shadow: 0 2px 10px rgba(0, 0, 0, .3);
}
.zbtn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.zbtn:hover:not(:disabled) { background: var(--panel); }
.zbtn:disabled { opacity: .45; cursor: default; }
.zbtn:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.zbtn.gap { margin-top: 10px; }
@media (prefers-reduced-motion: reduce) { .zoom { animation: none; } }

/* Finestra sopra il flipbook per i link */
/* Larga (90% dello schermo, almeno 1200 px se ci stanno) perché il sito nella finestra non passi
   all'impaginazione per tablet */
.linkview {
  width: max(90vw, min(1200px, 100vw)); height: min(94dvh, 100dvh); max-width: 100vw; max-height: 100dvh;
  padding: 0; border: 0; border-radius: 10px; overflow: hidden;
}
.linkview[open] { display: flex; flex-direction: column; }
.lv-head { display: flex; align-items: center; gap: 8px; padding: 6px 6px 6px 16px; border-bottom: 1px solid var(--line); }
.lv-head h2 { flex: 1; margin: 0; font-size: 16px; color: var(--brand); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.linkview iframe { flex: 1; width: 100%; border: 0; }

#print-area { display: none; }

@media (max-width: 700px) {
  .title, .tool span, .edge, .textver { display: none; }
  .topbar { gap: 4px; padding-left: 10px; padding-right: 4px; }
  .tool { min-width: 44px; }
  .logo-text { font-size: 15px; }
  .linkview { width: 100vw; height: 100dvh; border-radius: 0; }
  /* Sul telefono non c'è posto: la validità si vede solo se scaduta, ridotta al link al flyer in corso */
  /* Sul telefono la barra in alto non ha posto: la validità non si mostra; se l'offerta è scaduta
     resta il riquadro sopra la pagina con il pulsante per le offerte in corso */
  .validity { display: none !important; }
}
@media (max-width: 400px) {
  .logo-text { font-size: 13px; }
  .tool { min-width: 40px; padding: 0 2px; }
}

@media print {
  @page { margin: 0; }
  html, body { margin: 0; padding: 0; }
  body { display: block; height: auto; overflow: visible; background: #fff; }
  body > *:not(#print-area) { display: none !important; }
  #print-area { display: block; }
  /* Ogni pagina sta dentro un foglio (100vh in stampa = altezza del foglio): se l'immagine superava il
     foglio anche di poco, l'avanzo finiva su un foglio bianco. Salto pagina tra una pagina e l'altra,
     non dopo l'ultima. */
  #print-area img {
    display: block; width: 100%; height: 100vh; object-fit: contain;
    break-inside: avoid; page-break-inside: avoid;
  }
  #print-area img:not(:last-child) { break-after: page; page-break-after: always; }
}
