@import url("../../shared/reader.css");

:root {
  --bg: #f2f1ed;
  --paper: #fffdf8;
  --ink: #161d21;
  --ink-soft: #3f4b50;
  --ink-mute: #69777b;
  --rule: #cfd5d3;
  --accent: #c85534;
  --accent-soft: #f1ddd4;
  --cyan: #168798;
  --cyan-soft: #dceff1;
  --code-bg: #11191d;
  --code-fg: #e8eee9;
  --code-key: #f5a472;
  --code-str: #b8d68c;
  --code-com: #8a7a60;
  --code-fn:  #8ec5e0;
  --glosario-bg: #fdf6e3;
  --glosario-border: #d4a72c;
  --trivia-bg: #eaf2f8;
  --trivia-border: #2980b9;
  --metedura-bg: #fbeae5;
  --metedura-border: #c0392b;
  --patron-bg: #eaf7ed;
  --patron-border: #27ae60;
  --cita-bg: #e8eff0;
  --cita-border: #43808a;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  padding: 0;
  background:
    linear-gradient(rgba(255,255,255,0.42), rgba(255,255,255,0.42)),
    var(--bg);
  color: var(--ink);
  font-family: "Iowan Old Style", "Palatino Linotype", "Book Antiqua", Palatino, Georgia, serif;
  font-size: 17px;
  line-height: 1.65;
  font-feature-settings: "kern" 1, "liga" 1, "onum" 1;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  hyphens: auto;
}

::selection { background: #e8b09b; color: var(--ink); }
:focus-visible { outline: 3px solid var(--cyan); outline-offset: 3px; }
a { color: var(--cyan); text-underline-offset: 0.18em; }

@media (min-width: 1100px) {
  body { font-size: 18px; }
}

/* ============== CUBIERTA ============== */
.cubierta {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 4em 2em;
  background:
    radial-gradient(ellipse at center, #fffef9 0%, #f0e6d0 100%),
    linear-gradient(180deg, #c0392b 0%, #2c3e50 100%);
  background-blend-mode: multiply;
  border-bottom: 8px solid var(--accent);
  page-break-after: always;
  break-after: page;
}
.cubierta .categoria {
  text-transform: uppercase;
  letter-spacing: 0.4em;
  font-size: 0.75em;
  color: var(--ink-mute);
  font-family: -apple-system, "Helvetica Neue", sans-serif;
  font-weight: 600;
  margin-bottom: 2em;
}
.cubierta h1 {
  font-size: clamp(2.5em, 6vw, 4.5em);
  line-height: 1.05;
  font-weight: 700;
  margin: 0 0 0.3em;
  color: var(--ink);
  font-family: "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  letter-spacing: -0.01em;
}
.cubierta .subtitulo {
  font-size: clamp(1.1em, 2vw, 1.4em);
  font-style: italic;
  color: var(--ink-soft);
  max-width: 32em;
  margin: 0 auto 2em;
  line-height: 1.4;
}
.cubierta .version {
  font-family: -apple-system, "Helvetica Neue", sans-serif;
  font-size: 0.85em;
  color: var(--ink-mute);
  letter-spacing: 0.1em;
}
.cubierta .autor {
  margin-top: 3em;
  font-size: 0.95em;
  color: var(--ink-soft);
  letter-spacing: 0.2em;
  text-transform: uppercase;
  font-family: -apple-system, "Helvetica Neue", sans-serif;
}

/* ============== FRONT MATTER ============== */
.front-matter, .capitulo, .anexo, .back-matter {
  width: calc(100% - 2rem);
  max-width: var(--reader-line-length, 64em) !important;
  margin: 4em auto !important;
  padding: 0 clamp(1.25em, 3.5vw, 3em);
}

/* ============== TITULOS ============== */
h1, h2, h3, h4, h5 {
  font-family: "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: -0.01em;
  color: var(--ink);
}

h1.parte, h1.capitulo-titulo, h1.anexo-titulo {
  font-size: 2.4em;
  text-align: left;
  margin: 0 0 0.4em;
  color: var(--ink);
}
h1.parte::before {
  content: "";
  display: block;
  width: 3em;
  height: 0;
  border-top: 3px solid var(--accent);
  margin-bottom: 0.6em;
}
h1.parte {
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-size: 1.4em;
  color: var(--ink-mute);
  margin-top: 5em;
  page-break-before: always;
  break-before: page;
}
h1.parte::after {
  content: "";
  display: block;
  width: 100%;
  height: 0;
  border-top: 1px solid var(--rule);
  margin-top: 0.6em;
}
h2 {
  font-size: 1.7em;
  margin: 2.5em 0 0.6em;
  color: var(--ink);
  page-break-after: avoid;
  break-after: avoid;
}
h2::before {
  content: "§";
  color: var(--accent);
  margin-right: 0.4em;
  font-style: italic;
}
h3 {
  font-size: 1.3em;
  margin: 1.8em 0 0.4em;
  color: var(--ink-soft);
  page-break-after: avoid;
  break-after: avoid;
}
h4 {
  font-size: 1.1em;
  margin: 1.4em 0 0.3em;
  color: var(--ink-soft);
  font-style: italic;
}

/* ============== CITA INICIAL (epígrafe) ============== */
.epigrafe {
  font-family: "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  font-style: italic;
  font-size: 1.05em;
  color: var(--ink-soft);
  text-align: center;
  margin: 0 auto 2.5em;
  padding: 1em 2em;
  max-width: 32em;
  border-top: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
  line-height: 1.4;
}
.epigrafe::before { content: "\201C\00A0"; color: var(--ink-mute); }
.epigrafe::after  { content: "\00A0\201D"; color: var(--ink-mute); }

/* ============== METADATA DEL CAPÍTULO ============== */
.cap-meta {
  font-family: -apple-system, "Helvetica Neue", sans-serif;
  font-size: 0.8em;
  text-transform: uppercase;
  letter-spacing: 0.15em;
  color: var(--ink-mute);
  margin: 0 0 1.5em;
  padding-bottom: 0.8em;
  border-bottom: 1px solid var(--rule);
}
.cap-meta .numero { color: var(--accent); font-weight: 700; }

/* ============== DROP CAP ============== */
.capitulo-contenido > p:first-of-type::first-letter,
.capitulo > p:first-of-type::first-letter,
.anexo > p:first-of-type::first-letter {
  font-family: "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  font-size: 4.5em;
  float: left;
  line-height: 0.85;
  margin: 0.05em 0.12em 0 0;
  color: var(--accent);
  font-weight: 700;
}

/* ============== PÁRRAFOS ============== */
p {
  margin: 0 0 1em;
  text-align: justify;
  hyphens: auto;
  text-indent: 1.2em;
}
.capitulo p:first-of-type,
.anexo p:first-of-type,
.front-matter p:first-of-type,
.back-matter p:first-of-type,
.epigrafe + p,
h2 + p, h3 + p, h4 + p,
li p, blockquote p, .caja p,
.front-matter p, .back-matter p {
  text-indent: 0;
}
strong { font-weight: 700; color: var(--ink); }
em { font-style: italic; }
code {
  font-family: "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  font-size: 0.88em;
  background: var(--code-bg);
  color: var(--code-fg);
  padding: 0.1em 0.35em;
  border-radius: 3px;
  word-break: break-word;
}

/* ============== CITAS ============== */
blockquote {
  margin: 1.5em 0;
  padding: 1em 1.25em;
  border-top: 1px solid var(--cita-border);
  border-bottom: 1px solid var(--cita-border);
  background: var(--cita-bg);
  font-style: italic;
  color: var(--ink-soft);
  border-radius: 6px;
  text-indent: 0;
}

/* ============== CAJAS ESPECIALES ============== */
.caja {
  margin: 1.5em 0;
  padding: 1em 1.4em;
  border: 1px solid var(--rule);
  border-radius: 7px;
  box-shadow: 0 5px 18px rgba(17, 25, 29, 0.05);
  font-size: 0.96em;
  line-height: 1.55;
  text-indent: 0;
  page-break-inside: avoid;
  break-inside: avoid;
}
.caja-titulo {
  display: block;
  font-family: -apple-system, "Helvetica Neue", sans-serif;
  font-size: 0.75em;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  margin-bottom: 0.6em;
  color: var(--ink-soft);
}
.caja p:last-child { margin-bottom: 0; }
.caja ul:last-child, .caja ol:last-child { margin-bottom: 0; }

.glosario { background: var(--glosario-bg); border-color: var(--glosario-border); }
.glosario .caja-titulo { color: #8a6914; }

.trivia { background: var(--trivia-bg); border-color: var(--trivia-border); }
.trivia .caja-titulo { color: #1f5e8a; }

.metedura-de-pata { background: var(--metedura-bg); border-color: var(--metedura-border); }
.metedura-de-pata .caja-titulo { color: #8a2a1a; }

.patron-del-capitulo { background: var(--patron-bg); border-color: var(--patron-border); }
.patron-del-capitulo .caja-titulo { color: #1a6a3a; }

.patron-h2 {
  background: var(--patron-bg);
  border: 1px solid var(--patron-border);
  padding: 0.5em 0.8em;
  border-radius: 6px;
  margin-top: 2em;
  color: #1a6a3a;
}
.patron-h2::before { content: none; }

/* ============== LISTAS ============== */
ul, ol {
  margin: 0 0 1em;
  padding-left: 1.6em;
  text-indent: 0;
}
li { margin: 0.3em 0; }
li::marker { color: var(--accent); }

/* ============== CÓDIGO ============== */
pre {
  background: var(--code-bg) !important;
  color: var(--code-fg) !important;
  padding: 1.1em 1.3em;
  border-radius: 6px;
  overflow-x: auto;
  font-family: "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  font-size: 0.83em;
  line-height: 1.55;
  margin: 1.2em 0;
  border: 1px solid #314046;
  border-top: 3px solid var(--accent);
  page-break-inside: avoid;
  break-inside: avoid;
  white-space: pre;
  tab-size: 4;
}
pre code {
  background: transparent !important;
  color: var(--code-fg) !important;
  padding: 0;
  border-radius: 0;
  font-size: inherit;
}

/* ============== SYNTAX HIGHLIGHT (minimal) ============== */
.tok-key { color: #f5a472 !important; font-weight: 600; }
.tok-str { color: #b8d68c !important; }
.tok-com { color: #8a7a60 !important; font-style: italic; }
.tok-fn  { color: #8ec5e0 !important; }
.tok-num { color: #d4a4f0 !important; }

/* ============== TABLAS ============== */
table {
  border-collapse: collapse;
  width: 100%;
  margin: 1.2em 0;
  font-size: 0.92em;
  font-family: -apple-system, "Helvetica Neue", sans-serif;
  page-break-inside: avoid;
}
th, td {
  border: 1px solid var(--rule);
  padding: 0.6em 0.8em;
  text-align: left;
  vertical-align: top;
}
th {
  background: #e3e9e8;
  font-weight: 700;
  text-transform: uppercase;
  font-size: 0.82em;
  letter-spacing: 0.05em;
}
tbody tr:nth-child(even) { background: #f7f8f5; }

/* ============== IMÁGENES ============== */
img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 1.5em auto;
  border-radius: 7px;
  box-shadow: 0 12px 32px rgba(17, 25, 29, 0.12);
}
figcaption {
  text-align: center;
  font-size: 0.85em;
  color: var(--ink-mute);
  font-style: italic;
  margin-top: -0.5em;
  margin-bottom: 1.5em;
}

/* ============== HR ============== */
hr {
  border: none;
  text-align: center;
  margin: 2.5em 0;
  color: var(--ink-mute);
}
hr::before {
  content: "❦ ❦ ❦";
  font-size: 1.2em;
  letter-spacing: 0.5em;
}

/* ============== ÍNDICE LATERAL FLOTANTE ============== */
.indice-lateral {
  position: fixed;
  top: 0;
  left: 0;
  width: 280px;
  height: 100vh;
  background: #11191d;
  border-right: 1px solid #2a3b42;
  padding: 2em 1.4em;
  overflow-y: auto;
  font-family: -apple-system, "Helvetica Neue", sans-serif;
  font-size: 0.82em;
  z-index: 100;
  box-shadow: 8px 0 30px rgba(0,0,0,0.16);
  transition: transform 0.3s ease;
}
.indice-lateral h2 {
  font-size: 0.9em;
  text-transform: uppercase;
  letter-spacing: 0.2em;
  color: #e47a56;
  border: none;
  margin: 0 0 1em;
  padding: 0;
}
.indice-lateral h2::before { content: none; }
.indice-lateral ol, .indice-lateral ul {
  list-style: none;
  padding: 0;
  margin: 0 0 1.2em;
}
.indice-lateral .parte-indice {
  text-transform: uppercase;
  font-weight: 700;
  color: #8fa3a6;
  font-size: 0.78em;
  letter-spacing: 0.1em;
  margin: 1em 0 0.4em;
  padding-top: 0.6em;
  border-top: 1px solid #2a3b42;
}
.indice-lateral .parte-indice:first-child { border-top: none; padding-top: 0; }
.indice-lateral a {
  color: #c6d4d6;
  text-decoration: none;
  display: block;
  padding: 0.2em 0;
  border-bottom: 1px dotted transparent;
  transition: all 0.15s;
}
.indice-lateral a:hover {
  color: #86d8df;
  border-bottom-color: #86d8df;
  padding-left: 0.3em;
}
.indice-toggle {
  position: fixed;
  top: 4.25rem;
  left: 1em;
  z-index: 200;
  background: var(--cyan);
  color: white;
  border: none;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1.3em;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
  display: none;
}

@media (max-width: 1199px) {
  .indice-lateral { transform: translateX(-100%); }
  .indice-lateral.abierta { transform: translateX(0); }
  .indice-toggle { display: block; }
}

@media (max-width: 700px) {
  body { font-size: 16px; }
  .front-matter, .capitulo, .anexo, .back-matter {
    width: calc(100% - 1.5rem);
    margin-block: 2.5em !important;
    padding-inline: 1.15em;
  }
  p { text-align: left; text-indent: 0; }
  h1.parte, h1.capitulo-titulo, h1.anexo-titulo { font-size: 2em; }
  h2 { font-size: 1.45em; }
  table { display: block; overflow-x: auto; }
}

/* ============== PROGRESO DE LECTURA ============== */
.barra-progreso {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--accent) 0%, #e67e22 100%);
  z-index: 1000;
  width: 0%;
  transition: width 0.1s linear;
}

/* ============== FOOTNOTES ============== */
.fn-ref {
  font-size: 0.75em;
  vertical-align: super;
  color: var(--accent);
  text-decoration: none;
  font-weight: 700;
  margin: 0 0.1em;
}

/* ============== UTILIDADES ============== */
.centrado { text-align: center; }
.nota-imprenta {
  font-family: -apple-system, "Helvetica Neue", sans-serif;
  font-size: 0.78em;
  color: var(--ink-mute);
  text-align: center;
  margin: 2em 0;
  padding: 1em;
  border-top: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
  text-indent: 0;
}

/* ============== PRINT ============== */
@media print {
  body { background: white; color: black; font-size: 11pt; }
  .indice-lateral, .indice-toggle, .barra-progreso { display: none !important; }
  .front-matter, .capitulo, .anexo, .back-matter { width: 100%; max-width: 100% !important; margin: 0 !important; padding: 0; }
  h1.parte, h1.capitulo-titulo, h1.anexo-titulo { page-break-before: always; }
  pre { font-size: 8.5pt; border: 1px solid #ccc; }
  .caja { border: 1px solid #ccc; background: #f8f8f8 !important; }
}
