:root {
  --glow-rgb: 122, 120, 116;   /* grigio fumo */
  --paper: #f7f4ee;
  --paper-2: #efeae0;
  --ink: #141311;
  --ink-soft: #4a463f;
  --ink-faint: #8b857a;
  --accent: #8a1c1c;
  --rule: rgba(20, 19, 17, 0.12);
  --bar-h: 56px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--serif);
}

body { overflow: hidden; overflow-x: clip; }

.bar {
  position: fixed;
  inset: 0 0 auto 0;
  height: var(--bar-h);
  padding: env(safe-area-inset-top) 16px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: color-mix(in srgb, var(--paper) 92%, transparent);
  backdrop-filter: blur(10px);
  z-index: 10;
  border-bottom: 1px solid var(--rule);
}

.bar-left { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.brand {
  font-family: var(--sans);
  font-size: 11px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--ink-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand .dot { color: var(--accent); }
.chapter {
  max-width: 46vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  font-family: var(--sans);
  font-size: 12px;
  color: var(--ink-faint);
}
.bar-right { display: flex; align-items: center; gap: 10px; }
.counter {
  font-family: var(--sans);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--ink-soft);
  letter-spacing: .04em;
}
.icon-btn {
  width: 36px; height: 36px;
  border: 1px solid var(--rule);
  border-radius: 50%;
  background: var(--paper);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  color: var(--ink);
}
.icon-btn[aria-pressed="true"] { background: var(--ink); color: var(--paper); }
.progress {
  position: absolute;
  left: 0; right: 0; bottom: -1px;
  height: 2px;
  background: transparent;
}
.progress span {
  display: block;
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width .25s ease;
}

.deck {
  height: 100dvh;
  overflow-y: auto;
  scroll-snap-type: y mandatory;
  overscroll-behavior-y: contain;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.deck::-webkit-scrollbar { display: none; }

.card {
  min-height: 100dvh;
  scroll-snap-align: start;
  scroll-snap-stop: always;
  padding: calc(var(--bar-h) + env(safe-area-inset-top) + 14px) 18px 26px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
}

.card-media {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.plate {
  /* il riquadro della lastra deve coincidere con l'immagine: la traccia e il
     cursore sono posizionati su di lei, non sul contenitore più largo */
  position: relative;
  display: block;
  width: min(100%, 560px, 62dvh);
  aspect-ratio: 1 / 1;
}
.plate img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  mix-blend-mode: multiply;
}
.halo {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 60%;
  height: 60%;
  transform: translate(-50%, -50%) scale(.98);
  border-radius: 50%;
  background: radial-gradient(closest-side,
    rgba(var(--glow-rgb), 0) 62%,
    rgba(var(--glow-rgb), 0.30) 70%,
    rgba(var(--glow-rgb), 0.09) 82%,
    rgba(var(--glow-rgb), 0) 95%);
  filter: blur(20px);
  opacity: 0;
  transition: opacity .5s ease;
  pointer-events: none;
}
.card.playing .halo {
  opacity: .22;
  /* appare ~350 ms dopo l'attacco del suono, si spegne subito */
  transition: opacity .6s ease .35s;
  animation: halo-breathe 3.6s ease-in-out infinite;
}
/* variante bianca: schiarisce l'inchiostro invece di colorare la carta */
body.accent-violet { --glow-rgb: 168, 110, 255; }

body.halo-white .halo {
  background: radial-gradient(closest-side,
    rgba(255, 255, 255, 0) 60%,
    rgba(255, 255, 255, 0.85) 72%,
    rgba(255, 255, 255, 0.25) 84%,
    rgba(255, 255, 255, 0) 95%);
  mix-blend-mode: screen;
  filter: blur(7px);
}
.trace {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
}
.trace path {
  fill: none;
  stroke: #f3f1ec;
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: .95;
  filter: drop-shadow(0 0 5px rgba(var(--glow-rgb), .9));
}
.trace-head {
  position: absolute;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
  background: #fff;
  box-shadow: 0 0 9px 3px rgba(var(--glow-rgb), .85), 0 0 2px 1px #fff;
  opacity: 0;
  transition: opacity .18s ease;
}
.card.reading .trace-head { opacity: 1; }

@keyframes halo-breathe {
  0%, 100% { transform: translate(-50%, -50%) scale(.98); }
  50%      { transform: translate(-50%, -50%) scale(1.045); }
  }

.cover .plate img { max-height: 52dvh; }
.cover h1 {
  font-size: clamp(34px, 10vw, 56px);
  line-height: 1.02;
  margin: 0 0 8px;
  font-weight: 600;
  letter-spacing: -.01em;
}
.cover .sub {
  margin: 0;
  font-size: 15px;
  color: var(--ink-soft);
  line-height: 1.5;
}
.cover-actions {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}
.cover-actions .nav-inline { margin-left: 0; }
.cover .start {
  margin-top: 18px;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--ink);
  background: var(--ink);
  color: var(--paper);
  border-radius: 999px;
  padding: 12px 22px;
  font-family: var(--sans);
  font-size: 14px;
  letter-spacing: .04em;
  cursor: pointer;
}
.cover .start:active { transform: translateY(1px); }
.cover .note {
  margin-top: 12px;
  font-family: var(--sans);
  font-size: 11px;
  color: var(--ink-faint);
}

.nav-inline { display: inline-flex; gap: 8px; }
.nav-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: 1px solid var(--rule);
  background: var(--paper);
  color: var(--ink);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
}
.nav-btn:active:not(:disabled) { background: var(--ink); color: var(--paper); }
.nav-btn:disabled { opacity: .25; cursor: default; }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

@media (min-width: 760px) {
  .card { padding-bottom: 40px; }
  .card-media img { max-height: 76dvh; }
}

@media (prefers-reduced-motion: reduce) {
  .deck { scroll-behavior: auto; }
  .progress span { transition: none; }
  .card.playing .halo { animation: none; }
}

/* ---------- indice dei capitoli ---------- */
.drawer {
  position: fixed; z-index: 40; top: var(--bar-h); right: 0; bottom: 0;
  width: min(420px, 88vw); background: var(--paper);
  border-left: 1px solid var(--rule); box-shadow: -18px 0 40px rgba(20,19,17,.10);
  display: flex; flex-direction: column; overflow: hidden;
}
.drawer[hidden] { display: none; }
.drawer-head { display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--rule);
  font-family: var(--sans); font-size: 13px; letter-spacing: .08em; text-transform: uppercase; }
.drawer-sub { margin: 10px 16px 4px; font-family: var(--sans); font-size: 12px; color: var(--ink-faint); }
.cap-list { list-style: none; margin: 0; padding: 4px 8px 12px; overflow-y: auto; flex: 1; }
.cap-item { width: 100%; display: grid; grid-template-columns: 46px 1fr; gap: 2px 10px;
  align-items: baseline; text-align: left; background: transparent; border: 0;
  border-bottom: 1px solid var(--rule); padding: 9px 8px; cursor: pointer;
  font-family: var(--serif); color: var(--ink); }
.cap-item:hover:not(.spento) { background: var(--paper-2); }
.cap-item.spento { color: var(--ink-faint); cursor: default; }
.cap-item.attuale { background: var(--paper-2); box-shadow: inset 2px 0 0 var(--accent); }
.cap-rom { font-size: 15px; }
.cap-stato { font-family: var(--sans); font-size: 11.5px; color: var(--ink-faint); }
.cap-qui { grid-column: 2; font-family: var(--sans); font-size: 10.5px; color: var(--accent);
  letter-spacing: .06em; text-transform: uppercase; }
.cap-vuoto { padding: 16px; font-family: var(--sans); font-size: 12.5px; color: var(--ink-faint); }
.drawer-note { margin: 0; padding: 10px 16px 16px; border-top: 1px solid var(--rule);
  font-family: var(--sans); font-size: 11.5px; color: var(--ink-faint); }

/* ---------- icone e controlli (stesso tratto dell'app) ---------- */
.icon-btn svg, .nav-btn svg, .play svg { display: block; }
.nav-btn { width: 34px; height: 34px; display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--rule); border-radius: 50%; background: transparent; color: var(--ink);
  cursor: pointer; padding: 0; transition: border-color .15s, color .15s, background .15s; }
.nav-btn:hover:not([disabled]) { border-color: var(--ink-soft); background: var(--paper-2); }
.nav-btn[disabled] { opacity: .3; cursor: default; }
.play.icon-only { width: 34px; height: 34px; display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--rule); border-radius: 50%; background: transparent; color: var(--ink);
  cursor: pointer; padding: 0; }
.play.icon-only:hover { border-color: var(--ink-soft); background: var(--paper-2); }
.nav-inline { display: inline-flex; gap: 6px; align-items: center; }
.nav-inline.dir { margin-left: 8px; padding-left: 10px; border-left: 1px solid var(--rule); }
.dir-btn.on { border-color: var(--accent); color: var(--accent); }

/* ---------- karaoke: la parola segue la direzione dell'animazione ---------- */
.karaoke { font-family: var(--serif); font-size: 13px; line-height: 1.5; text-align: center;
  color: var(--ink-faint); background: var(--paper-2); border: 1px solid var(--rule);
  border-radius: 10px; padding: 8px 12px; margin: 0 0 10px; max-height: 6.2em; overflow-y: auto;
  overflow-wrap: anywhere; transition: color .2s; }
.karaoke span { display: inline; }
.karaoke[hidden] { display: none; }
.karaoke span { transition: color .18s ease-out, opacity .18s; opacity: .45; }
.karaoke span.on { color: var(--ink); opacity: 1; }

/* la colonna di testo deve occupare la card, altrimenti il karaoke va fuori schermo */
.card-body { width: 100%; max-width: 560px; }
.karaoke { width: 100%; }
.meta { width: 100%; display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap; }
.karaoke, .karaoke span { white-space: normal !important; word-break: break-word; }

/* ---------- karaoke sopra il testo inciso nel logogramma ---------- */
.karaoke-sotto { width: 100%; text-align: center; margin: 0 0 10px; }
.karaoke-sotto[hidden] { display: none; }
.ko-label { font-family: var(--sans); font-size: 7.8px; letter-spacing: .14em; text-transform: uppercase;
  color: var(--ink-faint); }
.ko-text { font-family: var(--serif); font-size: 13px; line-height: 1.5; color: var(--ink-faint);
  overflow-wrap: anywhere; }
.ko-text span { transition: color .16s ease-out, opacity .16s; opacity: .42; }
.ko-text span.on { color: var(--ink); opacity: 1; }

/* ---------- autoplay, miniature, avviso ---------- */
.icon-btn.on { color: var(--accent); border-color: var(--accent); }
.cap-item { grid-template-columns: 40px 2.4em 1fr; column-gap: 10px; align-items: center; }
.cap-qui { grid-column: 3; }
.cap-stato { grid-column: 3; }
.cap-thumb { width: 34px; height: 34px; border-radius: 6px; object-fit: cover; background: var(--paper-2);
  border: 1px solid var(--rule); }
.cap-thumb-off { display: block; }
.cap-rom { font-size: 14px; }
#toast { position: fixed; left: 50%; bottom: 22px; transform: translate(-50%, 12px); z-index: 60;
  background: var(--ink); color: var(--paper); font-family: var(--sans); font-size: 12px;
  padding: 8px 14px; border-radius: 20px; opacity: 0; pointer-events: none;
  transition: opacity .25s, transform .25s; }
#toast.on { opacity: .95; transform: translate(-50%, 0); }

/* ---------- ricerca nel testo ---------- */
.drawer-search { position: relative; margin: 6px 16px 10px; }
.drawer-search input { width: 100%; box-sizing: border-box; font-family: var(--sans); font-size: 13px;
  padding: 9px 34px 9px 12px; border: 1px solid var(--rule); border-radius: 8px;
  background: var(--paper-2); color: var(--ink); }
.drawer-search input:focus { outline: none; border-color: var(--ink-soft); }
.drawer-search .icon-btn { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); }

/* selettore lingua nella barra */
#lingua { font-family: var(--sans); font-size: 11px; color: var(--ink-soft); background: var(--paper-2);
  border: 1px solid var(--rule); border-radius: 6px; padding: 4px 6px; max-width: 150px; }

/* ---------- testo a fronte italiano <-> Neo ---------- */
.fronte-chip {
  font-family: var(--sans); font-size: 10.5px; letter-spacing: .04em;
  color: var(--ink-soft); background: var(--paper-2); border: 1px solid var(--rule);
  border-radius: 999px; padding: 3px 9px; cursor: pointer; transition: border-color .15s, color .15s;
}
.fronte-chip[hidden] { display: none; }
.fronte-chip:hover { border-color: var(--ink-soft); color: var(--ink); }

.fronte { position: fixed; z-index: 35; background: var(--paper); display: flex; flex-direction: column; min-height: 0; }
.fronte[hidden] { display: none; }
.fronte-head { position: relative; display: flex; align-items: flex-start; justify-content: space-between;
  gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--rule); }
.fronte-head-text { min-width: 0; }
#fronte-titolo { font-family: var(--sans); font-size: 11px; letter-spacing: .14em; text-transform: uppercase;
  color: var(--ink-soft); font-weight: 600; }
.fronte-sub { margin: 3px 0 0; font-family: var(--sans); font-size: 11px; color: var(--ink-faint); line-height: 1.4; }
.fronte-corpo { flex: 1; overflow-y: auto; padding: 6px 14px 18px; overscroll-behavior: contain; }
.fronte-capitolo { margin: 12px 0 14px; font-family: var(--sans); font-size: 10.5px; letter-spacing: .1em;
  text-transform: uppercase; color: var(--ink-faint); line-height: 1.5; }
.fronte-blocco { margin: 0 -8px 2px; padding: 6px 8px; border-radius: 8px; border-left: 2px solid transparent;
  transition: background .2s, border-color .2s; }
.fronte-blocco.is-active { background: var(--paper-2); border-left-color: var(--accent); }
.fronte-p { margin: 0 0 8px; font-size: 14.5px; line-height: 1.62; color: var(--ink); }
.fronte-blocco.aggiunta .fronte-p { color: var(--ink-soft); }
.fronte-it { margin: 0 0 8px; font-size: 14px; line-height: 1.6; color: var(--ink-soft); font-style: italic; }
.fronte-tag { display: inline-block; font-family: var(--sans); font-size: 9px; letter-spacing: .12em;
  text-transform: uppercase; color: var(--accent); border: 1px solid var(--rule); border-radius: 999px;
  padding: 2px 7px; margin: 0 0 6px; }
.fronte-vuoto { padding: 20px 4px; font-family: var(--sans); font-size: 12.5px; color: var(--ink-faint); line-height: 1.5; }
.fronte-nota { margin: 0; padding: 9px 14px calc(9px + env(safe-area-inset-bottom)); border-top: 1px solid var(--rule);
  font-family: var(--sans); font-size: 10.5px; color: var(--ink-faint); line-height: 1.45; }

@media (min-width: 900px) {
  .fronte { top: var(--bar-h); right: 0; bottom: 0; width: min(38vw, 480px); border-left: 1px solid var(--rule);
    box-shadow: -18px 0 40px rgba(20, 19, 17, .08); }
  body.fronte-on .deck { margin-right: min(38vw, 480px); }
}
@media (max-width: 899px) {
  .fronte { left: 0; right: 0; bottom: 0; max-height: 62dvh; border-top: 1px solid var(--rule);
    border-radius: 16px 16px 0 0; box-shadow: 0 -16px 40px rgba(20, 19, 17, .14); }
}

/* barra compatta: con i cinque comandi la riga non deve rompersi */
@media (max-width: 560px) {
  .bar { padding-left: 12px; padding-right: 12px; }
  .counter { display: none; }
  .brand { display: none; }
  .bar-right { gap: 6px; }
  .bar-right .icon-btn { width: 31px; height: 31px; }
  #lingua { max-width: 84px; font-size: 10px; padding: 4px 4px; }
  .chapter { max-width: 42vw; }
}
@media (max-width: 380px) {
  #lingua { max-width: 68px; }
  .bar-right .icon-btn { width: 29px; height: 29px; }
}
.fronte-avviso { margin: 10px 0 12px; padding: 8px 10px; border: 1px solid var(--rule); border-left: 2px solid var(--accent);
  border-radius: 6px; font-family: var(--sans); font-size: 11.5px; line-height: 1.5; color: var(--ink-soft);
  background: var(--paper-2); }
/* la traccia luminosa esiste solo in lettura: a riposo non deve disegnare un segno bianco sopra l'inchiostro */
.trace { opacity: 0; transition: opacity .25s ease; }
.card.playing .trace, .card.reading .trace { opacity: 1; }
