:root {
  --paper: #FFFFFF;
  --surface: #FFFFFF;
  --soft: #F3F6F6;
  --ink: #12262B;
  --ink-soft: #3A4F54;
  --muted: #5E7175;
  --line: #DCE4E3;
  --accent: #007F8F;
  --accent-ink: #FFFFFF;
  --accent-wash: #E0F1F2;
  --mark: #CBEFF3;
  --danger: #B3263E;
  --shadow: 0 1px 2px rgba(18, 38, 43, .06), 0 8px 24px rgba(18, 38, 43, .08);
  --read-size: 19px;
  --ui: 'Plus Jakarta Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --serif: 'Literata', Georgia, 'Times New Roman', serif;
  --bar: 56px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #0E1518; --surface: #152126; --ink: #DCE6E5; --ink-soft: #B5C4C3; --muted: #8A9C9D;
    --line: #24343A; --soft: #1B2A30; --accent: #00C9E0; --accent-ink: #062027; --accent-wash: #12313A; --mark: #144450;
    --danger: #F5365C; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 10px 30px rgba(0,0,0,.35); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #0E1518; --surface: #152126; --ink: #DCE6E5; --ink-soft: #B5C4C3; --muted: #8A9C9D;
  --line: #24343A; --soft: #1B2A30; --accent: #00C9E0; --accent-ink: #062027; --accent-wash: #12313A; --mark: #144450;
  --danger: #F5365C; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 10px 30px rgba(0,0,0,.35); color-scheme: dark;
}

/* En modo noche las imágenes (de fondo blanco) se muestran sobre una lámina blanca, para que no parezcan recortes */
:root[data-theme="dark"] .page img { background: #fff; padding: 10px; border-radius: 8px; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .page img { background: #fff; padding: 10px; border-radius: 8px; } }

*, *::before, *::after { box-sizing: border-box; }
html, body { overflow-x: clip; }
html { height: 100%; -webkit-text-size-adjust: 100%; scroll-padding-top: calc(var(--bar) + env(safe-area-inset-top, 0px)); }
body {
  margin: 0; min-height: 100%; background: var(--paper); color: var(--ink);
  font: 400 16px/1.5 var(--ui); -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
img { max-width: 100%; height: auto; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* Botones */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 18px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); font-weight: 600; font-size: 15px; text-decoration: none; color: var(--ink);
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.quiet { background: transparent; border-color: transparent; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn {
  width: 44px; height: 44px; display: inline-grid; place-items: center; border: 0; border-radius: 10px;
  background: transparent; color: var(--ink-soft);
}
.icon-btn:hover { background: var(--accent-wash); color: var(--ink); }
.icon-btn[aria-pressed="true"] { background: var(--accent-wash); color: var(--accent); }
.icon-btn svg, .btn svg { width: 22px; height: 22px; flex: none; }

/* Entrada */
.gate { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.gate-card { width: 100%; max-width: 400px; }
.gate h1 { font: 600 clamp(30px, 6vw, 40px)/1.12 var(--serif); margin: 20px 0 10px; letter-spacing: -.01em; }
.gate p { color: var(--muted); margin: 0 0 28px; }
.gate label { display: block; font-weight: 600; font-size: 14px; margin-bottom: 8px; }
.gate input {
  width: 100%; height: 52px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--soft); font-size: 18px; letter-spacing: .06em; text-transform: uppercase;
}
.gate input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-wash); }
.gate .btn { width: 100%; margin-top: 14px; height: 52px; }
.gate .err { color: var(--danger); font-size: 14px; margin-top: 12px; min-height: 20px; }
.mark { width: 52px; height: 52px; }

/* Barra superior */
.topbar {
  position: sticky; top: 0; z-index: 20; height: calc(var(--bar) + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) max(8px, env(safe-area-inset-right, 0px)) 0 max(8px, env(safe-area-inset-left, 0px));
  display: flex; align-items: center; gap: 4px;
  background: color-mix(in srgb, var(--paper) 88%, transparent); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid transparent; transition: border-color .2s;
}
.topbar.scrolled { border-bottom-color: var(--line); }
.topbar .title { flex: 1; min-width: 0; font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 6px; }
.topbar .title small { display: block; font-weight: 400; color: var(--muted); font-size: 12.5px; }
.progress-line { position: absolute; left: 0; bottom: -1px; height: 2px; background: var(--accent); width: 0; transition: width .15s linear; }
.offline-pill { font-size: 12.5px; font-weight: 600; color: var(--muted); border: 1px solid var(--line); padding: 3px 10px; border-radius: 99px; }

/* Biblioteca */
.shelf { max-width: 1080px; margin: 0 auto; padding: 8px 20px 80px; }
.shelf-head { padding: 28px 0 8px; }
.shelf-head h1 { font: 600 clamp(32px, 5vw, 46px)/1.08 var(--serif); margin: 0; letter-spacing: -.015em; }
.shelf-head p { margin: 8px 0 0; color: var(--muted); }

.resume {
  margin: 28px 0 8px; display: grid; grid-template-columns: auto 1fr auto; gap: 18px; align-items: center;
  padding: 16px; border-radius: 14px; background: var(--soft); border: 1px solid var(--line); text-decoration: none; color: inherit;
}
.resume:hover { border-color: var(--accent); }
.resume .mini-cover { width: 54px; aspect-ratio: 2/3; border-radius: 3px 6px 6px 3px; }
.resume b { display: block; font: 600 17px/1.3 var(--serif); }
.resume span { color: var(--muted); font-size: 14px; }
.bar { height: 4px; border-radius: 4px; background: var(--line); margin-top: 10px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--accent); }

.books { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 36px 28px; margin-top: 36px; }
.book { display: flex; flex-direction: column; gap: 12px; }
.book a.cover-link { text-decoration: none; color: inherit; display: block; border-radius: 4px 10px 10px 4px; }
.book.soon .cover-link { position: relative; }
.book.soon .cover-link { border-radius: 4px 10px 10px 4px; overflow: hidden; }
/* Libro bloqueado: velo oscuro, candado al centro y etiqueta turquesa */
.soon-lock {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  background: rgba(6, 16, 20, .52); color: #fff; border-radius: 4px 10px 10px 4px;
}
.soon-lock .lock-icon { width: 48px; height: 48px; filter: drop-shadow(0 2px 6px rgba(0,0,0,.45)); }
.soon-badge {
  max-width: calc(100% - 20px); text-align: center; line-height: 1.25;
  background: #00C9E0; color: #062027; font: 700 13px/1 var(--ui); letter-spacing: .01em;
  padding: 8px 14px; border-radius: 99px; box-shadow: 0 2px 10px rgba(0,0,0,.3);
}
.book h2 { font: 600 17px/1.3 var(--serif); margin: 0; }
.book .meta { color: var(--muted); font-size: 13.5px; margin-top: 2px; }
.book .actions { margin-top: auto; display: flex; gap: 6px; flex-wrap: wrap; }
.book .actions .btn { min-height: 38px; padding: 0 12px; font-size: 13.5px; border-radius: 8px; }

/* Portada tipográfica */
.cover {
  --c: #12262B;
  position: relative; aspect-ratio: 2/3; border-radius: 4px 10px 10px 4px; overflow: hidden;
  background: var(--c); color: #F3F5F4; box-shadow: var(--shadow);
  display: flex; flex-direction: column; justify-content: space-between; padding: 18px 16px 16px 24px;
}
.cover::before { content: ''; position: absolute; left: 0; top: 0; bottom: 0; width: 9px; background: linear-gradient(90deg, rgba(0,0,0,.28), rgba(255,255,255,.08) 70%, transparent); }
.cover .ct { font: 700 clamp(20px, 2.4vw, 25px)/1.08 var(--serif); letter-spacing: -.01em; text-wrap: balance; position: relative; }
.cover .cs { font: 400 13px/1.35 var(--serif); font-style: italic; opacity: .82; margin-top: 8px; position: relative; }
.cover .ca { font: 600 12px/1 var(--ui); letter-spacing: .02em; opacity: .9; position: relative; }
.cover .rule { width: 28px; height: 3px; background: #00C9E0; border-radius: 2px; margin-bottom: 12px; }
.cover img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.mini-cover.cover { padding: 0; box-shadow: none; }
.mini-cover.cover::before { width: 4px; }

.empty { text-align: center; padding: 80px 20px; color: var(--muted); }
.empty h2 { font: 600 24px/1.2 var(--serif); color: var(--ink); margin: 0 0 8px; }

/* Lector */
.reader-wrap { display: flex; }
.reader { flex: 1; min-width: 0; padding: 24px 22px 160px; }
.page { overflow-wrap: break-word; max-width: 34em; margin: 0 auto; font-family: var(--serif); font-size: var(--read-size); line-height: 1.68; color: var(--ink); }
.page .ch-part { font: 600 13.5px/1.4 var(--ui); color: var(--accent); margin: 32px 0 4px; }
.page .ch-kicker { font: 500 14px/1.4 var(--ui); color: var(--muted); margin: 8px 0 6px; }
.page .ch-part + .ch-kicker { margin-top: 0; }
.page h1 { font: 700 1.75em/1.15 var(--serif); letter-spacing: -.012em; margin: 0 0 1.4em; text-wrap: balance; }
.page h2 { font-size: 1.22em; line-height: 1.3; margin: 2em 0 .6em; }
.page h3 { font-size: 1.06em; margin: 1.6em 0 .5em; }
.page h4 { font: 700 1.14em/1.3 var(--serif); margin: 2em 0 .6em; }
.page h5 { font-size: 1em; margin: 1.5em 0 .4em; }
.page .frase { font-style: italic; color: var(--ink-soft); margin: 0 0 .3em; padding-left: 1.1em; border-left: 3px solid var(--accent); }
.page .frase + p:not(.frase) { margin-top: 1.4em; }
.page .portadilla img { max-height: 46vh; width: auto; margin: 0 auto 1.5em; }
.page .titulo-portada { font: 700 1.5em/1.15 var(--serif); margin: 0 0 .2em; }
.page .autor { font: 600 1em var(--ui); }
.page a { color: var(--accent); text-underline-offset: 3px; overflow-wrap: anywhere; word-break: break-word; }
.page p { margin: 0 0 1em; }
.page blockquote { margin: 1.6em 0; padding: .2em 0 .2em 1.1em; border-left: 3px solid var(--accent); font-style: italic; color: var(--ink-soft); }
.page blockquote.destacada { font-size: 1.12em; border-left-width: 5px; }
.page ul, .page ol { padding-left: 1.3em; margin: 0 0 1em; }
.page li { margin-bottom: .35em; }
.page table { width: 100%; border-collapse: collapse; font-size: .88em; margin: 1.4em 0; display: block; overflow-x: auto; }
.page td, .page th { border: 1px solid var(--line); padding: .45em .6em; vertical-align: top; }
.page img { display: block; margin: 1.4em auto; height: auto; }
.page .etiqueta { font: 600 .78em/1.4 var(--ui); color: var(--muted); text-align: center; margin: -.6em 0 1.2em; }
.page .atribucion { font: 500 .85em/1.4 var(--ui); color: var(--muted); padding-left: 1.3em; margin: .2em 0 1.6em; }
.page .banner img { margin-bottom: .4em; max-height: 90px; width: auto; }
.page .flash { background: var(--mark); border-radius: 4px; transition: background 1.6s ease; }
.page ::selection { background: var(--mark); }
.copy-note { font: 400 13px/1.45 var(--ui); color: var(--ink-soft); text-align: center; margin: 3em 0 0; padding: 10px 16px; background: var(--soft); border-radius: 8px; }

.ch-nav { max-width: 34em; margin: 3em auto 0; display: grid; grid-template-columns: 1fr 1fr; gap: 12px; font-family: var(--ui); line-height: 1.35; }
.ch-nav a { display: block; padding: 14px 16px; border: 1px solid var(--line); border-radius: 12px; text-decoration: none; color: inherit; background: var(--soft); }
.ch-nav a:hover { border-color: var(--accent); }
.ch-nav small { display: block; color: var(--muted); font-size: 12.5px; }
.ch-nav b { font-weight: 600; font-size: 14.5px; }
.ch-nav .next { text-align: right; grid-column: 2; }

/* Paneles laterales y hojas */
.scrim { position: fixed; inset: 0; background: rgba(8, 18, 21, .35); z-index: 40; opacity: 0; pointer-events: none; transition: opacity .2s; }
.scrim.on { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; z-index: 50; background: var(--surface); box-shadow: var(--shadow);
  display: flex; flex-direction: column; transition: transform .25s cubic-bezier(.2,.8,.2,1);
}
.sheet.left { top: 0; bottom: 0; left: 0; width: min(360px, 88vw); transform: translateX(-102%); padding-top: env(safe-area-inset-top, 0px); }
.sheet.right { top: 0; bottom: 0; right: 0; width: min(420px, 100vw); transform: translateX(102%); padding-top: env(safe-area-inset-top, 0px); border-left: 1px solid var(--line); }
.sheet.open { transform: none; }
.sheet-head { display: flex; align-items: center; gap: 8px; padding: 8px 8px 8px 18px; border-bottom: 1px solid var(--line); min-height: 60px; }
.sheet-head h2 { flex: 1; margin: 0; font: 600 17px/1.3 var(--ui); }
.sheet-body { flex: 1; overflow-y: auto; overscroll-behavior: contain; }

.toc { list-style: none; margin: 0; padding: 8px 0; }
.toc a { display: grid; grid-template-columns: 1fr auto; gap: 10px; padding: 12px 18px; text-decoration: none; color: inherit; border-left: 3px solid transparent; }
.toc a:hover { background: var(--accent-wash); }
.toc a[aria-current="true"] { border-left-color: var(--accent); background: var(--accent-wash); font-weight: 600; }
.toc small { color: var(--muted); font-size: 12.5px; white-space: nowrap; }
.toc .toc-label { display: block; font-size: 12px; }
.toc .toc-part { padding: 18px 18px 6px; font-weight: 700; font-size: 13.5px; color: var(--accent); border-top: 1px solid var(--line); margin-top: 6px; }
.toc li:first-child.toc-part { border-top: 0; margin-top: 0; }
.toc a.in-part { padding-left: 26px; }
.toc .done { color: var(--accent); }

.settings { position: fixed; z-index: 50; top: calc(var(--bar) + env(safe-area-inset-top, 0px) + 6px); right: 10px; width: min(280px, calc(100vw - 20px)); padding: 18px; border-radius: 14px; background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); display: none; }
.settings.open { display: block; }
.settings h3 { margin: 0 0 10px; font-size: 13.5px; font-weight: 600; color: var(--muted); }
.settings .row { display: flex; gap: 8px; margin-bottom: 18px; }
.settings .row:last-child { margin-bottom: 0; }
.seg { flex: 1; min-height: 40px; border-radius: 8px; border: 1px solid var(--line); background: transparent; font-weight: 600; font-size: 14px; }
.seg[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-wash); color: var(--accent); }
.size-row { display: flex; align-items: center; gap: 10px; }
.size-row input { flex: 1; accent-color: var(--accent); }

/* Reproductor */
.player {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  background: color-mix(in srgb, var(--surface) 94%, transparent); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
  padding: 10px max(14px, env(safe-area-inset-right, 0px)) calc(10px + env(safe-area-inset-bottom, 0px)) max(14px, env(safe-area-inset-left, 0px));
  transform: translateY(110%); transition: transform .25s;
}
.player.show { transform: none; }
.player-inner { max-width: 760px; margin: 0 auto; display: grid; grid-template-columns: auto 1fr auto; gap: 10px 14px; align-items: center; }
.player .play { width: 48px; height: 48px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; }
.player .play svg { width: 22px; height: 22px; }
.player .track { min-width: 0; }
.player .track b { display: block; font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player .scrub { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.player input[type=range] { flex: 1; accent-color: var(--accent); height: 20px; }
.player .ctrls { display: flex; align-items: center; gap: 2px; }
.player .speed { min-width: 52px; height: 36px; border-radius: 8px; border: 1px solid var(--line); background: transparent; font-weight: 600; font-size: 13px; }
.listen-btn { position: fixed; right: 18px; bottom: calc(18px + env(safe-area-inset-bottom, 0px)); z-index: 29; box-shadow: var(--shadow); border-radius: 99px; }

/* Mentor */
.mentor .sheet-body { padding: 16px 18px; display: flex; flex-direction: column; gap: 14px; }
.mentor-intro { color: var(--muted); font-size: 14.5px; }
.mentor-intro b { color: var(--ink); font: 600 19px/1.3 var(--serif); display: block; margin-bottom: 6px; }
.suggests { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }
.suggests button { text-align: left; padding: 10px 14px; border-radius: 10px; border: 1px solid var(--line); background: transparent; font-size: 14px; }
.suggests button:hover { border-color: var(--accent); }
.msg { max-width: 92%; font-size: 15px; line-height: 1.55; }
.msg.user { align-self: flex-end; background: var(--accent-wash); padding: 10px 14px; border-radius: 14px 14px 4px 14px; }
.msg.bot { align-self: flex-start; }
.msg.bot p { margin: 0 0 .7em; }
.msg.bot p:last-child { margin-bottom: 0; }
.msg.bot ul, .msg.bot ol { margin: 0 0 .7em; padding-left: 1.2em; }
.msg .quote { display: block; font-size: 13px; color: var(--muted); border-left: 2px solid var(--accent); padding-left: 8px; margin-bottom: 6px; font-style: italic; }
.cite { display: inline-grid; place-items: center; min-width: 22px; height: 20px; padding: 0 5px; margin: 0 1px; border-radius: 6px; border: 0; background: var(--accent-wash); color: var(--accent); font-size: 12px; font-weight: 700; vertical-align: 1px; }
.sources { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.sources button { font-size: 12.5px; text-align: left; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--line); background: transparent; color: var(--ink-soft); }
.sources button:hover { border-color: var(--accent); color: var(--ink); }
.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.composer { border-top: 1px solid var(--line); padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px)); }
.context-chip { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); margin: 0 4px 8px; }
.context-chip button { border: 0; background: transparent; color: var(--accent); font-weight: 600; font-size: 12.5px; padding: 2px 4px; }
.composer form { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; min-height: 46px; max-height: 140px; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--soft); line-height: 1.4; font-size: 15px; }
.composer textarea:focus { outline: none; border-color: var(--accent); }
.composer .send { width: 46px; height: 46px; border-radius: 12px; border: 0; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; flex: none; }
.composer .send svg { width: 20px; height: 20px; }
.composer .note { font-size: 11.5px; color: var(--muted); margin: 8px 4px 0; }

.ask-pop {
  position: absolute; z-index: 35; display: none; align-items: center; gap: 6px;
  padding: 8px 12px; border-radius: 10px; border: 0; background: var(--ink); color: var(--paper);
  font: 600 13.5px/1 var(--ui); box-shadow: var(--shadow); transform: translate(-50%, -100%);
}
.ask-pop.on { display: inline-flex; }
.ask-pop svg { width: 16px; height: 16px; }

#toast {
  position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom, 0px)); transform: translate(-50%, 20px);
  background: var(--ink); color: var(--paper); padding: 11px 16px; border-radius: 10px; font-size: 14px; max-width: min(440px, calc(100vw - 32px));
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 80;
}
#toast.on { opacity: 1; transform: translate(-50%, 0); }

/* Escritorio: el mentor se acomoda al lado del texto en vez de taparlo */
@media (min-width: 1100px) {
  body.mentor-docked .reader { margin-right: 420px; }
  body.mentor-docked .scrim { display: none; }
  body.mentor-docked .player, body.mentor-docked .listen-btn { right: 420px; }
}
@media (max-width: 560px) {
  .books { grid-template-columns: repeat(2, 1fr); gap: 28px 16px; }
  .cover { padding: 14px 12px 12px 18px; }
  .cover .ct { font-size: 18px; }
  .reader { padding: 12px 20px 160px; }
  .page h1 { font-size: 1.45em; }
  .player-inner { grid-template-columns: auto 1fr; }
  .player .ctrls { grid-column: 1 / -1; justify-content: space-between; }
  .resume { grid-template-columns: auto 1fr; }
  .resume .btn { grid-column: 1 / -1; }
  .ch-nav { grid-template-columns: 1fr; }
  .topbar .lib-title { white-space: normal; line-height: 1.2; font-size: 14.5px; text-wrap: balance; }
  .ch-nav .next { grid-column: 1; }
  .ch-nav > span:empty { display: none; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { transition: none !important; animation: none !important; } }
@media print { #app, .player { display: none !important; } body::after { content: 'Los libros de esta biblioteca no se pueden imprimir.'; display: block; padding: 40px; font: 16px var(--ui); } }
