/* ==========================================================================
   Novelas — mobile-first (iPhone 14 Pro Max: 430×932 CSS px)
   Reglas: tap targets ≥44px, inputs ≥16px (evita zoom de iOS),
   safe-area insets, sin scroll horizontal.
   ========================================================================== */

:root {
  --bg: #0f1115;
  --bg-soft: #161922;
  --bg-card: #1a1e29;
  --fg: #e7e9ef;
  --fg-dim: #98a1b0;
  --accent: #7aa2f7;
  --accent-ink: #08101f;
  --accent-soft: rgba(122, 162, 247, .13);
  --border: #262b38;
  --ok: #6cc98a;
  --radius: 14px;

  --reader-width: 40rem;
  --reader-size: 19px;
  --reader-line: 1.72;

  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --topbar-h: 52px;
}

html[data-theme='light'] {
  --bg: #fbf9f5;
  --bg-soft: #f2efe8;
  --bg-card: #ffffff;
  --fg: #1a1e26;
  --fg-dim: #5c6573;
  --accent: #2b5bc7;
  --accent-ink: #ffffff;
  --accent-soft: rgba(43, 91, 199, .1);
  --border: #e3ded4;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', system-ui, 'Segoe UI', Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: none; }

.wrap {
  max-width: 62rem;
  margin: 0 auto;
  padding: 0 calc(1rem + var(--safe-l)) calc(3rem + var(--safe-b)) calc(1rem + var(--safe-r));
}

/* ---------- barra superior ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  padding-top: var(--safe-t);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 62rem;
  margin: 0 auto;
  padding: 0 .5rem;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: .5rem;
}
.brand { font-weight: 700; letter-spacing: -.02em; color: var(--fg); font-size: 1.05rem; padding: 0 .5rem; }
.brand span { color: var(--accent); }
.topbar .spacer { flex: 1; }

.icon-btn {
  min-width: 44px; min-height: 44px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 12px; color: var(--fg); font-size: 1.2rem; line-height: 1;
}
.icon-btn:active { background: var(--bg-soft); }

/* ---------- botones ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  min-height: 44px; padding: 0 1rem;
  background: var(--bg-card); color: var(--fg);
  border: 1px solid var(--border); border-radius: 12px;
  font-size: .95rem; font-weight: 500; cursor: pointer;
  transition: background .15s, border-color .15s;
}
.btn:active { transform: scale(.985); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 650; }
.btn.block { width: 100%; }
.btn[disabled] { opacity: .5; }
.sub-btn { min-width: 48px; padding: 0 .7rem; font-size: 1.05rem; }
.sub-btn.on { border-color: var(--ok); color: var(--ok); background: rgba(108, 201, 138, .12); }

/* ---------- tipografía ---------- */
h1.page-title { font-size: 1.45rem; margin: 1.4rem 0 1rem; letter-spacing: -.02em; }
.section-title {
  font-size: .78rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase;
  color: var(--fg-dim); margin: 1.8rem 0 .8rem;
}
.meta { color: var(--fg-dim); font-size: .83rem; line-height: 1.45; }
.note { color: var(--fg-dim); font-size: .82rem; }

/* ---------- biblioteca: una columna en móvil ---------- */
.cards { display: grid; gap: .8rem; grid-template-columns: 1fr; }

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: grid;
  grid-template-columns: 84px 1fr;
}
.card .cover {
  position: relative;
  background: var(--bg-soft) center/cover no-repeat;
  display: flex; align-items: center; justify-content: center;
  min-height: 120px;
}
.cover-fallback { font-size: 2rem; color: var(--fg-dim); font-weight: 700; }
.card .body { padding: .75rem .85rem; display: flex; flex-direction: column; gap: .4rem; min-width: 0; }
.card h3 { font-size: .98rem; margin: 0; line-height: 1.28; }
.card h3 a { color: var(--fg); }
.card .actions { margin-top: auto; display: flex; gap: .5rem; padding-top: .35rem; }
.card .actions .btn { flex: 1; min-height: 40px; font-size: .88rem; }

.progress-track { height: 5px; background: var(--bg-soft); border-radius: 999px; overflow: hidden; }
.progress-track > i { display: block; height: 100%; background: var(--accent); border-radius: 999px; transition: width .3s; }

/* ---------- cabecera de novela ---------- */
.novel-head { padding-top: 1.1rem; }
.novel-head h1 { font-size: 1.4rem; line-height: 1.25; margin: 0 0 .5rem; letter-spacing: -.02em; }
.novel-head .progress-track { margin: .7rem 0 .4rem; }
.head-actions { display: flex; gap: .5rem; margin: 1rem 0 .5rem; }
.head-actions .btn.primary { flex: 1; }

/* ---------- índice de capítulos ---------- */
.chapter-grid {
  display: grid;
  gap: .4rem;
  grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));
}
.chapter-link {
  min-height: 46px;
  display: flex; align-items: center; gap: .35rem;
  padding: .4rem .55rem;
  border-radius: 10px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--fg);
  font-size: .88rem;
  font-variant-numeric: tabular-nums;
}
.chapter-link:active { border-color: var(--accent); }
.chapter-link .num { font-weight: 650; }
.chapter-link .ctitle {
  color: var(--fg-dim); font-size: .72rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chapter-link.read { color: var(--fg-dim); }
.chapter-link.read .num::after { content: ' ✓'; color: var(--ok); font-size: .8em; }

/* ---------- lector ---------- */
.reading-progress {
  position: fixed; top: 0; left: 0;
  height: 3px; width: 0;
  background: var(--accent);
  z-index: 100;
  transition: width .25s ease-out;
}
.reader-wrap { padding-bottom: calc(4rem + var(--safe-b)); }

.reader-tools {
  position: sticky;
  top: calc(var(--topbar-h) + var(--safe-t));
  z-index: 40;
  display: flex; gap: .3rem; align-items: center; justify-content: center;
  padding: .45rem 0;
  margin-bottom: .4rem;
  background: color-mix(in srgb, var(--bg) 94%, transparent);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}
.tool {
  min-width: 44px; min-height: 40px;
  display: inline-flex; align-items: center; justify-content: center;
  background: transparent; color: var(--fg-dim);
  border: 1px solid transparent; border-radius: 10px;
  font-size: .9rem; cursor: pointer;
}
.tool:active { background: var(--accent-soft); color: var(--accent); }

.chapter {
  max-width: var(--reader-width);
  margin: 0 auto 2.5rem;
  font-family: 'Iowan Old Style', 'Palatino Linotype', Georgia, 'Times New Roman', serif;
  font-size: var(--reader-size);
  line-height: var(--reader-line);
  overflow-wrap: break-word;
}
.chapter h2 {
  font-family: -apple-system, system-ui, sans-serif;
  font-size: .82rem; font-weight: 700;
  color: var(--fg-dim);
  text-transform: uppercase; letter-spacing: .09em;
  margin: 1.6rem 0 1.3rem;
  padding-bottom: .55rem;
  border-bottom: 1px solid var(--border);
}
.chapter p { margin: 0 0 1.05em; text-align: justify; -webkit-hyphens: auto; hyphens: auto; }
.chapter hr.scene { border: 0; margin: 1.7em 0; text-align: center; }
.chapter hr.scene::after { content: '• • •'; color: var(--fg-dim); letter-spacing: .5em; }

.loader {
  max-width: var(--reader-width);
  margin: 0 auto;
  padding: 1.5rem 0 3rem;
  text-align: center;
  color: var(--fg-dim);
  font-size: .88rem;
}
.end-of-novel { padding: 2.5rem 1rem; text-align: center; color: var(--fg-dim); }

/* ---------- login ---------- */
.login-wrap {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid; place-items: center;
  padding: calc(1rem + var(--safe-t)) 1rem calc(1rem + var(--safe-b));
}
.login-box {
  width: 100%; max-width: 22rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 1.75rem 1.4rem;
}
.login-logo { font-size: 2.2rem; line-height: 1; margin-bottom: .6rem; }
.login-box h1 { margin: 0 0 .2rem; font-size: 1.35rem; letter-spacing: -.02em; }
.login-box p.sub { margin: 0 0 1.4rem; color: var(--fg-dim); font-size: .88rem; }

.field { margin-bottom: .9rem; }
.field label { display: block; font-size: .8rem; color: var(--fg-dim); margin-bottom: .35rem; }
.field input {
  width: 100%;
  min-height: 48px;
  padding: .7rem .8rem;
  border-radius: 12px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--fg);
  font-size: 16px; /* 16px evita el zoom automático de iOS */
  -webkit-appearance: none;
  appearance: none;
}
.field input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
}

.error { color: #f7768e; font-size: .85rem; margin-bottom: .8rem; }
.ok-msg { color: var(--ok); font-size: .85rem; margin-bottom: .8rem; }

.empty { text-align: center; padding: 3rem 1rem; color: var(--fg-dim); }
.empty h1 { color: var(--fg); font-size: 1.3rem; }

/* iOS: inercia en el scroll del lector */
.chapter, .chapter-grid { -webkit-overflow-scrolling: touch; }

/* ---------- pantallas grandes ---------- */
@media (min-width: 720px) {
  .cards { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); }
  .card { grid-template-columns: 112px 1fr; }
  .card .cover { min-height: 150px; }
  .chapter-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); }
  .novel-head h1 { font-size: 1.8rem; }
}

/* ---------- modo compacto en horizontal ---------- */
@media (orientation: landscape) and (max-height: 500px) {
  .topbar { position: static; }
  .reader-tools { top: 0; }
}

