:root {
  --bg: #111214;
  --surface: #1c1d21;
  --surface-2: #26282d;
  --text: #ecebe7;
  --muted: #8b8d94;
  --accent: #ff5a4e;
  --line: #34363c;
  --word-size: 48px;
  color-scheme: dark;
}
:root.light {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #ebe8e1;
  --text: #1d1d1f;
  --muted: #6c6c70;
  --accent: #d8342a;
  --line: #d9d5cc;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.screen {
  min-height: 100dvh;
  padding: var(--safe-area-inset-top, env(safe-area-inset-top)) var(--safe-area-inset-right, env(safe-area-inset-right))
    var(--safe-area-inset-bottom, env(safe-area-inset-bottom)) var(--safe-area-inset-left, env(safe-area-inset-left));
}

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 10px 16px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; font-weight: 600; user-select: none;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.icon {
  background: none; border: 0; min-width: 44px; min-height: 44px; font-size: 22px;
  border-radius: 12px; cursor: pointer;
}
.icon:active, .btn:active { opacity: .6; }
.icon.small { font-size: 15px; font-weight: 600; color: var(--muted); }

/* ---------- Biblioteca ---------- */
.lib-top {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 20px 18px 8px;
}
.lib-top { flex-wrap: wrap; }
.lib-top h1 { margin: 0; font-size: 26px; letter-spacing: -.02em; white-space: nowrap; }
.lib-actions { display: flex; align-items: center; gap: 2px; margin-left: auto; }
.lib-actions .btn { white-space: nowrap; }
.sync-btn { color: var(--muted); font-size: 20px; position: relative; }
.sync-btn.on { color: var(--text); }
.sync-btn.on::after {
  content: ""; position: absolute; top: 9px; right: 8px; width: 7px; height: 7px; border-radius: 50%; background: #3ecf8e;
}
.sync-btn.warn::after { background: var(--accent); }
.book-list { list-style: none; margin: 8px 0; padding: 0 12px; }
.book {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border-radius: 16px; padding: 14px; margin-bottom: 10px; cursor: pointer;
}
.book .badge {
  flex: none; width: 44px; height: 56px; border-radius: 6px; display: grid; place-items: center;
  background: var(--surface-2); color: var(--muted); font-size: 11px; font-weight: 700; letter-spacing: .05em;
}
.book .meta { flex: 1; min-width: 0; }
.book .t { font-weight: 600; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.book .a { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book .bar { height: 4px; background: var(--surface-2); border-radius: 2px; margin-top: 8px; overflow: hidden; }
.book .bar > i { display: block; height: 100%; background: var(--accent); }
.book .del { color: var(--muted); font-size: 18px; }
.empty { text-align: center; color: var(--muted); margin-top: 18vh; padding: 0 24px; }
.error { margin: 12px 18px; padding: 12px 14px; border-radius: 12px; background: color-mix(in srgb, var(--accent) 18%, transparent); }
.install-tip {
  display: flex; align-items: center; gap: 8px; margin: 8px 12px; padding: 10px 6px 10px 14px; border-radius: 12px;
  background: var(--surface); border: 1px solid var(--line); font-size: 14px; color: var(--muted);
}
.install-tip b { color: var(--text); }
.install-tip .icon { flex: none; font-size: 16px; }
.loading { text-align: center; padding: 30px 18px; color: var(--muted); }
.loading progress { width: 70%; accent-color: var(--accent); }
.spinner {
  width: 32px; height: 32px; margin: 0 auto 10px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent); animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Lector ---------- */
#reader { display: flex; flex-direction: column; height: 100dvh; min-height: 0; }
.reader-top { display: flex; align-items: center; gap: 4px; padding: 6px 6px 0; }
.titles { flex: 1; min-width: 0; text-align: center; }
.book-title { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chapter-title { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.stage {
  flex: 1; min-height: 0; display: flex; flex-direction: column; justify-content: center;
  position: relative; user-select: none; cursor: pointer; overflow: hidden;
}
.word-box { position: relative; padding: 22px 0; }
.guide { position: absolute; left: 50%; width: 2px; height: 14px; background: var(--accent); opacity: .7; transform: translateX(-1px); }
.guide.top { top: 0; }
.guide.bottom { bottom: 0; }
.word-box::before, .word-box::after {
  content: ""; position: absolute; left: 8%; right: 8%; height: 1px; background: var(--line);
}
.word-box::before { top: 0; }
.word-box::after { bottom: 0; }
.word {
  display: flex; align-items: baseline; font-size: var(--word-size); line-height: 1.2;
  font-family: Georgia, "Iowan Old Style", "Times New Roman", serif; white-space: pre;
}
.word .l { flex: 1; text-align: right; overflow: hidden; }
.word .p { color: var(--accent); }
.word .r { flex: 1; text-align: left; overflow: hidden; }

.context {
  margin: 16px 16px 0; max-height: 40%; overflow-y: auto; padding: 14px 16px;
  background: var(--surface); border-radius: 14px; font-family: Georgia, serif; font-size: 17px; line-height: 1.6;
  user-select: none;
}
.context span { border-radius: 4px; cursor: pointer; }
.context span.cur { background: var(--accent); color: #fff; padding: 0 2px; }
.context .pb { display: block; height: .6em; }
.hint { text-align: center; color: var(--muted); font-size: 13px; margin: 14px 20px 0; }

.reader-bottom { padding: 6px 16px 14px; }
#progress { width: 100%; accent-color: var(--accent); margin: 0; }
.info { display: flex; justify-content: space-between; color: var(--muted); font-size: 13px; margin: 2px 2px 6px; }
.controls { display: flex; align-items: center; justify-content: center; gap: 10px; }
.play {
  width: 68px; height: 68px; border-radius: 50%; border: 0; background: var(--accent); color: #fff;
  font-size: 26px; cursor: pointer;
}
.play:active { transform: scale(.95); }
.wpm { display: flex; align-items: center; justify-content: center; gap: 14px; margin-top: 6px; color: var(--muted); }
.wpm strong { color: var(--text); font-size: 18px; }

/* ---------- Hojas (dialog) ---------- */
dialog.sheet {
  width: min(560px, 100%); max-height: 80dvh; margin: auto auto 0; border: 0; padding: 0 0 var(--safe-area-inset-bottom, env(safe-area-inset-bottom));
  border-radius: 20px 20px 0 0; background: var(--surface); color: var(--text);
}
dialog.sheet::backdrop { background: rgba(0, 0, 0, .5); }
dialog.sheet header { display: flex; align-items: center; justify-content: space-between; padding: 10px 10px 0 20px; position: sticky; top: 0; background: var(--surface); }
dialog.sheet h2 { margin: 0; font-size: 18px; }
.toc-list { margin: 0; padding: 6px 12px 16px 44px; }
.toc-list li { padding: 10px 6px; border-bottom: 1px solid var(--line); cursor: pointer; }
.toc-list li.cur { color: var(--accent); font-weight: 600; }
.field { display: block; padding: 12px 20px; }
.field output { float: right; color: var(--muted); }
.field input { width: 100%; accent-color: var(--accent); }
.check { display: flex; gap: 10px; align-items: center; padding: 10px 20px; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }
.sync-body { padding: 6px 20px 22px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; }
.sync-body .btn { white-space: normal; text-align: center; }
.sync-body p { margin: 0; }
.sync-lead, .sync-hint, .sync-or { color: var(--muted); font-size: 14px; }
.btn.wide { width: 100%; }
.btn.ghost { background: none; color: var(--muted); }
.sync-join { display: flex; gap: 8px; margin-top: 8px; }
.sync-join .btn, .sync-code .btn { flex: none; }
.sync-join input, .sync-code code {
  flex: 1; min-width: 0; width: 100%; padding: 10px 14px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg); color: var(--text); font: 16px ui-monospace, "SF Mono", Consolas, monospace;
}
.sync-join input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
#syncOff { display: grid; grid-template-columns: minmax(0, 1fr); gap: 4px; }
#syncOn { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
.sync-label { font-size: 12px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.sync-code { display: flex; gap: 8px; align-items: center; }
.sync-code code { user-select: all; overflow-wrap: anywhere; }
.sync-status { font-size: 14px; }
.sync-status.bad { color: var(--accent); }
.sync-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.sync-msg { padding: 10px 12px; border-radius: 12px; background: var(--surface-2); font-size: 14px; }
.headers-info { padding: 8px 20px 20px; color: var(--muted); font-size: 14px; }
.headers-info ul { margin: 6px 0 0; padding-left: 18px; }
.headers-info li { overflow-wrap: anywhere; }

/* ---------- Horizontal (celular de lado): controles compactos, más espacio para la palabra ---------- */
@media (orientation: landscape) and (max-height: 560px) {
  .reader-top { padding-top: 0; }
  .reader-top .icon { min-height: 38px; font-size: 20px; }
  .book-title { font-size: 14px; }
  .chapter-title { font-size: 12px; }
  .word-box { padding: 16px 0; }
  .context { margin-top: 8px; max-height: 55%; font-size: 16px; }
  .hint { display: none; }
  .reader-bottom {
    display: flex; flex-wrap: wrap; align-items: center; justify-content: center;
    column-gap: 28px; padding: 0 16px 6px;
  }
  #progress, .info { flex-basis: 100%; }
  .info { margin-bottom: 0; }
  .controls { gap: 6px; }
  .play { width: 52px; height: 52px; font-size: 20px; }
  .wpm { margin-top: 0; }
}
