/* Palette pensate per letture lunghe: niente nero/bianco puri, contrasto alto ma non abbagliante,
   toni caldi (meno luce blu). Il rosso del punto di fissazione resta ben distinguibile in ogni tema. */
:root, :root[data-theme="notte"] {
  --bg: #1a1815;
  --surface: #221f1b;
  --surface-2: #2b2722;
  --line: #3a352e;
  --text: #e4dac8;
  --text-word: #ece3d3;
  --muted: #968c7b;
  --pivot: #e86a5c;
  --accent: #c9a36a;
  --accent-ink: #1a1815;
  --frame: rgba(201, 163, 106, .42);
  --frame-fill: rgba(255, 240, 215, .025);
  --halo: rgba(255, 236, 205, .07);
  --vignette: rgba(0, 0, 0, .38);
  --shadow: 0 10px 30px rgba(0, 0, 0, .35);
  color-scheme: dark;
}
:root[data-theme="seppia"] {
  --bg: #f2e8d5;
  --surface: #ebdfc8;
  --surface-2: #e3d5ba;
  --line: #d4c4a5;
  --text: #3c3228;
  --text-word: #352c23;
  --muted: #84735d;
  --pivot: #c2392d;
  --accent: #946530;
  --accent-ink: #f8f1e3;
  --frame: rgba(148, 101, 48, .45);
  --frame-fill: rgba(255, 252, 240, .35);
  --halo: rgba(255, 250, 235, .7);
  --vignette: rgba(120, 90, 50, .16);
  --shadow: 0 10px 30px rgba(90, 65, 30, .15);
  color-scheme: light;
}
:root[data-theme="crepuscolo"] {
  --bg: #1c2026;
  --surface: #232830;
  --surface-2: #2b313a;
  --line: #39404b;
  --text: #d3d9e0;
  --text-word: #dde2e8;
  --muted: #8a94a1;
  --pivot: #ef7c6c;
  --accent: #8fb2c7;
  --accent-ink: #1c2026;
  --frame: rgba(143, 178, 199, .4);
  --frame-fill: rgba(220, 235, 255, .025);
  --halo: rgba(200, 225, 255, .06);
  --vignette: rgba(0, 0, 0, .35);
  --shadow: 0 10px 30px rgba(0, 0, 0, .35);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  transition: background .4s, color .4s;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }
svg { width: 22px; height: 22px; flex: none; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }

.btn {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 9px 16px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; user-select: none;
  transition: background .2s, border-color .2s, transform .1s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn.wide { width: 100%; justify-content: center; }
.btn svg { width: 18px; height: 18px; }

.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px;
  border: 0; border-radius: 10px; background: transparent; color: var(--muted); cursor: pointer;
  transition: background .2s, color .2s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.on { color: var(--accent); }
.icon-btn.on svg path { fill: currentColor; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ================= LIBRERIA ================= */
#library { min-height: 100%; }
.lib-header {
  max-width: 1120px; margin: 0 auto; padding: 32px 24px 8px;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
}
.brand { display: flex; align-items: center; gap: 14px; }
.logo { width: 38px; height: 38px; color: var(--accent); }
.brand h1 { margin: 0; font: 500 26px/1.1 Literata, Georgia, serif; letter-spacing: .3px; }
.tagline { margin: 2px 0 0; color: var(--muted); font-size: 13.5px; }
.lib-main { max-width: 1120px; margin: 0 auto; padding: 24px 24px 64px; }
.section-title { display: flex; align-items: baseline; gap: 12px; margin-bottom: 18px; }
.section-title h2 { margin: 0; font: 500 20px Literata, Georgia, serif; }

.book-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(168px, 1fr)); gap: 28px 22px; }
.book {
  position: relative; cursor: pointer; border-radius: 12px; outline-offset: 4px;
}
.book .cover {
  aspect-ratio: 3 / 4.2; border-radius: 8px; overflow: hidden; background: var(--surface-2);
  box-shadow: var(--shadow); border: 1px solid var(--line);
  transition: transform .25s ease, box-shadow .25s ease;
}
.book:hover .cover { transform: translateY(-3px); }
.book .cover img { width: 100%; height: 100%; object-fit: cover; object-position: top; display: block; filter: saturate(.85); }
.book .cover .ph { height: 100%; display: grid; place-items: center; padding: 16px; text-align: center; font: 500 16px Literata, serif; color: var(--muted); }
.book .meta { padding: 10px 2px 0; }
.book .t { font-weight: 500; line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.book .a { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book .prog { height: 3px; background: var(--line); border-radius: 2px; margin-top: 8px; overflow: hidden; }
.book .prog i { display: block; height: 100%; background: var(--accent); }
.book .info { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; margin-top: 5px; }
.book .del {
  position: absolute; top: 8px; right: 8px; width: 32px; height: 32px; border-radius: 8px;
  background: rgba(20, 18, 15, .7); color: #e8dfcf; border: 0; cursor: pointer; opacity: 0;
  display: grid; place-items: center; transition: opacity .2s;
}
.book .del svg { width: 16px; height: 16px; }
.book:hover .del, .book:focus-within .del { opacity: 1; }

.empty {
  display: flex; flex-direction: column; align-items: center; gap: 8px; text-align: center;
  border: 1.5px dashed var(--line); border-radius: 16px; padding: 56px 24px; color: var(--muted); cursor: pointer;
  transition: border-color .2s, background .2s;
}
.empty:hover { border-color: var(--accent); background: var(--surface); }
.empty svg { width: 48px; height: 48px; color: var(--accent); }
.empty strong { color: var(--text); font-weight: 500; font-size: 17px; }
.drop-overlay {
  position: fixed; inset: 12px; border: 2px dashed var(--accent); border-radius: 18px;
  background: color-mix(in srgb, var(--bg) 85%, transparent); display: grid; place-items: center;
  font: 500 20px Literata, serif; z-index: 50; pointer-events: none;
}

/* ================= LETTORE ================= */
#reader { position: fixed; inset: 0; display: flex; flex-direction: column; overflow: hidden; }
.chrome { transition: opacity .8s ease; }
#reader.immersed .chrome { opacity: .1; }
#reader.immersed .chrome:hover { opacity: 1; }
#reader.immersed { cursor: none; }

.r-top { display: flex; align-items: center; gap: 8px; padding: 10px 14px; }
.r-title { flex: 1; min-width: 0; text-align: center; }
.r-book { font: 500 14px Literata, serif; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.r-chapter { color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-height: 18px; }
.r-actions { display: flex; gap: 2px; }

.stage {
  --ws: 54px;
  flex: 1; position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 0 16px 6vh; min-height: 0;
}
.focus {
  position: relative; display: grid; place-items: center;
  width: min(94vw, calc(var(--ws) * 11.5)); height: calc(var(--ws) * 2.7);
}
.word {
  position: relative; z-index: 2;
  display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  width: 100%; font-size: var(--ws); line-height: 1.2; white-space: pre;
  font-family: var(--word-font, Literata), Georgia, serif; color: var(--text-word);
  letter-spacing: .01em; font-variant-ligatures: none;
}
.word .wl { text-align: right; }
.word .wr { text-align: left; }
.word .wp { color: var(--pivot); }
.word.idle { opacity: .9; }

.context {
  max-width: min(720px, 92vw); margin-top: 28px; font: 17px/1.75 Literata, Georgia, serif; color: var(--muted);
  text-align: center; opacity: 0; transform: translateY(6px); transition: opacity .35s, transform .35s; pointer-events: none;
  max-height: 30vh; overflow: hidden;
}
#reader.paused .context { opacity: 1; transform: none; pointer-events: auto; }
.context span { cursor: pointer; border-radius: 4px; padding: 0 1px; }
.context span:hover { color: var(--text); background: var(--surface-2); }
.context span.cur { color: var(--text-word); box-shadow: inset 0 -2px 0 var(--pivot); }
.hint { position: absolute; bottom: 2vh; color: var(--muted); font-size: 12.5px; transition: opacity .5s; text-align: center; padding: 0 16px; }
#reader.playing .hint { opacity: 0; }
kbd {
  font: 11px Inter, sans-serif; padding: 1px 6px; border: 1px solid var(--line); border-bottom-width: 2px;
  border-radius: 5px; background: var(--surface); margin: 0 1px;
}

/* ----- Le 5 cornici ----- */
.frame { position: absolute; inset: 0; pointer-events: none; z-index: 1; transition: opacity .4s; }
.frame i { position: absolute; display: none; }

/* 1. Mirino: due binari orizzontali con tacche che puntano alla lettera rossa (stile Spritz). */
.frame.f-reticle::before, .frame.f-reticle::after {
  content: ""; position: absolute; left: 6%; right: 6%; height: 1px;
  background: linear-gradient(90deg, transparent, var(--frame) 22%, var(--frame) 78%, transparent);
}
.frame.f-reticle::before { top: 8%; }
.frame.f-reticle::after { bottom: 8%; }
.frame.f-reticle .c1, .frame.f-reticle .c2 {
  display: block; left: 50%; width: 1.5px; height: calc(var(--ws) * .32); background: var(--frame); transform: translateX(-50%);
}
.frame.f-reticle .c1 { top: 8%; }
.frame.f-reticle .c2 { bottom: 8%; }

/* 2. Finestra: un riquadro morbido e pieno, come una lente posata sulla pagina. */
.frame.f-window::before {
  content: ""; position: absolute; inset: 4% 3%; border: 1px solid var(--frame); border-radius: calc(var(--ws) * .45);
  background: var(--frame-fill); box-shadow: inset 0 1px 12px rgba(0, 0, 0, .08);
}

/* 3. Angoli: quattro squadrette come la messa a fuoco di una fotocamera; lo spazio resta aperto. */
.frame.f-corners i { display: block; width: calc(var(--ws) * .55); height: calc(var(--ws) * .55); border: 0 solid var(--frame); }
.frame.f-corners .c1 { top: 4%; left: 8%; border-top-width: 1.5px; border-left-width: 1.5px; border-top-left-radius: 6px; }
.frame.f-corners .c2 { top: 4%; right: 8%; border-top-width: 1.5px; border-right-width: 1.5px; border-top-right-radius: 6px; }
.frame.f-corners .c3 { bottom: 4%; left: 8%; border-bottom-width: 1.5px; border-left-width: 1.5px; border-bottom-left-radius: 6px; }
.frame.f-corners .c4 { bottom: 4%; right: 8%; border-bottom-width: 1.5px; border-right-width: 1.5px; border-bottom-right-radius: 6px; }

/* 4. Alone: nessuna linea, solo luce. Il centro è più chiaro e la periferia si scurisce piano. */
.frame.f-halo::before {
  content: ""; position: absolute; inset: -40% -12%;
  background: radial-gradient(ellipse at center, var(--halo) 0%, transparent 65%);
}
.frame.f-halo::after {
  content: ""; position: fixed; inset: 0;
  background: radial-gradient(ellipse 60% 55% at 50% 47%, transparent 35%, var(--vignette) 100%);
}

/* 5. Respiro: un'ellisse sottile che si espande e si ritrae ~5,5 volte al minuto,
      il ritmo del respiro rilassato. Il movimento è lentissimo e periferico. */
.frame.f-breath::before {
  content: ""; position: absolute; inset: -8% 0; border: 1px solid var(--frame); border-radius: 50%;
  animation: breathe 11s ease-in-out infinite;
}
.frame.f-breath .c1 {
  display: block; left: 50%; top: -8%; width: 5px; height: 5px; border-radius: 50%; background: var(--frame);
  transform: translate(-50%, -50%);
}
@keyframes breathe {
  0%, 100% { transform: scale(1); opacity: .55; }
  50% { transform: scale(1.045, 1.09); opacity: 1; }
}
@media (prefers-reduced-motion: reduce) { .frame.f-breath::before { animation: none; } }

/* ----- barra inferiore ----- */
.r-bottom { padding: 0 18px 14px; }
.progress { position: relative; height: 22px; display: flex; align-items: center; cursor: pointer; }
.progress .track { width: 100%; height: 3px; background: var(--line); border-radius: 3px; overflow: hidden; transition: height .15s; }
.progress:hover .track { height: 5px; }
.progress .fill { height: 100%; width: 0; background: var(--accent); }
.marks { position: absolute; inset: 0; pointer-events: none; }
.marks .ch { position: absolute; top: 8px; width: 1px; height: 6px; background: var(--muted); opacity: .45; }
.marks .bm { position: absolute; top: 1px; width: 7px; height: 9px; background: var(--pivot); transform: translateX(-50%); clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 70%, 0 100%); }
.controls { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px; }
.c-left { min-width: 0; }
.pageinfo { font-size: 14px; font-variant-numeric: tabular-nums; }
.pageinfo b { font-weight: 600; }
.c-center { display: flex; align-items: center; gap: 10px; }
.c-right { display: flex; justify-content: flex-end; align-items: center; gap: 6px; }
.play-btn {
  width: 52px; height: 52px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; transition: transform .1s, filter .2s;
}
.play-btn:hover { filter: brightness(1.07); }
.play-btn:active { transform: scale(.96); }
.play-btn .i-pause { display: none; }
#reader.playing .play-btn .i-play { display: none; }
#reader.playing .play-btn .i-pause { display: block; }

.speed { display: flex; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 2px; }
.speed button {
  border: 0; background: transparent; padding: 5px 8px; border-radius: 7px; cursor: pointer; color: var(--muted);
  font-size: 12.5px; font-variant-numeric: tabular-nums;
}
.speed button:hover { color: var(--text); }
.speed button.on { background: var(--surface-2); color: var(--text); box-shadow: inset 0 0 0 1px var(--line); }
.speed .unit { align-self: center; color: var(--muted); font-size: 11px; padding: 0 6px 0 4px; }
.speed.big { flex-wrap: wrap; }
.speed.big button { flex: 1; padding: 8px 6px; font-size: 13.5px; }

/* ----- pannello laterale ----- */
.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 20; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(420px, 100vw); z-index: 21;
  background: var(--surface); border-left: 1px solid var(--line); box-shadow: var(--shadow);
  transform: translateX(102%); transition: transform .3s ease; display: flex; flex-direction: column;
}
.drawer.open { transform: none; }
.tabs { display: flex; align-items: center; gap: 4px; padding: 10px 10px 0; border-bottom: 1px solid var(--line); }
.tabs button[data-tab] {
  border: 0; background: none; padding: 10px 12px; cursor: pointer; color: var(--muted);
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tabs button[data-tab].on { color: var(--text); border-bottom-color: var(--accent); }
.tabs .close { margin-left: auto; }
.tab-body { display: none; overflow-y: auto; padding: 16px 18px 40px; flex: 1; }
.tab-body.on { display: block; }
.tab-body h3 { font: 500 15px Literata, serif; margin: 26px 0 10px; }
.tab-body h3:first-child { margin-top: 4px; }
.note { color: var(--muted); font-size: 13px; margin: 0 0 12px; }

.goto { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.goto label { flex: 1; display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13.5px; }
#gotoInput {
  width: 84px; padding: 6px 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit;
}
.toc { list-style: none; margin: 0; padding: 0; }
.toc li {
  display: flex; gap: 10px; align-items: baseline; padding: 8px 10px; border-radius: 8px; cursor: pointer;
  line-height: 1.35; font-size: 14px;
}
.toc li:hover { background: var(--surface-2); }
.toc li.cur { background: var(--surface-2); color: var(--text); box-shadow: inset 2px 0 0 var(--pivot); }
.toc li .tt { flex: 1; }
.toc li .pg { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.toc li.l1 { padding-left: 24px; font-size: 13.5px; }
.toc li.l2, .toc li.l3 { padding-left: 38px; font-size: 13px; color: var(--muted); }

.marks-list { list-style: none; padding: 0; margin: 14px 0 0; }
.marks-list li {
  display: flex; gap: 10px; align-items: flex-start; padding: 10px; border-radius: 10px; cursor: pointer; border: 1px solid transparent;
}
.marks-list li:hover { background: var(--surface-2); }
.marks-list .ribbon { width: 9px; height: 14px; margin-top: 4px; background: var(--pivot); clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 70%, 0 100%); flex: none; }
.marks-list .body { flex: 1; min-width: 0; }
.marks-list .snip { font: 14.5px/1.4 Literata, serif; }
.marks-list .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.marks-list .x { width: 30px; height: 30px; }
.marks-list .x svg { width: 15px; height: 15px; }
.empty-small { color: var(--muted); font-size: 13.5px; text-align: center; padding: 24px 8px; }

.choices { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.choice {
  text-align: left; border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 10px 11px; cursor: pointer;
  transition: border-color .2s, background .2s;
}
.choice:hover { border-color: var(--muted); }
.choice.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.choice .n { font-weight: 500; font-size: 13.5px; }
.choice .d { color: var(--muted); font-size: 11.5px; line-height: 1.35; margin-top: 3px; }
.frames .choice { padding: 8px; }
.frames .mini {
  --ws: 13px; height: 54px; border-radius: 6px; background: var(--surface); display: grid; place-items: center;
  overflow: hidden; position: relative; margin-bottom: 6px;
}
.frames .mini .focus { width: 90%; height: calc(var(--ws) * 2.7); }
.frames .mini .word { font-size: 13px; }
.frames .mini .frame.f-halo::after { position: absolute; inset: -200%; }

.range { display: flex; align-items: center; gap: 12px; margin-top: 12px; color: var(--muted); font-size: 13.5px; }
.range input { flex: 1; accent-color: var(--accent); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 10px 0; color: var(--muted); font-size: 13.5px; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 9px; padding: 2px; background: var(--bg); }
.seg button { border: 0; background: none; padding: 6px 10px; border-radius: 7px; cursor: pointer; color: var(--muted); font-size: 13px; }
.seg button.on { background: var(--surface-2); color: var(--text); }
.toggle { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin-top: 14px; cursor: pointer; font-size: 14px; }
.toggle input { accent-color: var(--accent); width: 17px; height: 17px; margin: 2px 0 0; }
.toggle small { grid-column: 2; color: var(--muted); font-size: 12px; }

/* ----- importazione / avvisi ----- */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: grid; place-items: center; z-index: 60; padding: 16px; }
.modal-card { width: min(420px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 22px; box-shadow: var(--shadow); }
.modal-title { font: 500 17px Literata, serif; margin-bottom: 14px; word-break: break-word; }
.bar { height: 4px; background: var(--line); border-radius: 3px; overflow: hidden; margin-bottom: 10px; }
.bar div { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.toast {
  position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); z-index: 70; max-width: calc(100vw - 32px);
  background: var(--surface-2); color: var(--text); border: 1px solid var(--line); padding: 10px 16px; border-radius: 10px;
  box-shadow: var(--shadow); font-size: 14px;
}

@media (max-width: 760px) {
  .controls { grid-template-columns: 1fr auto; }
  .c-center { order: 3; grid-column: 1 / -1; justify-content: center; }
  .c-right .speed { display: none; }
  .lib-header { padding: 20px 16px 4px; }
  .lib-main { padding: 18px 16px 48px; }
  .book-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 22px 16px; }
  .tagline { display: none; }
  .book .del { opacity: 1; }
}
