/* ABOUTME: LaTeX-adjacent stylesheet for the NextCat HTML reader.
   ABOUTME: Libertinus Serif body, justified text, numbered headings via counters. */

@font-face {
  font-family: "Libertinus Serif";
  src: url("fonts/LibertinusSerif-Regular.otf") format("opentype");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "Libertinus Serif";
  src: url("fonts/LibertinusSerif-Italic.otf") format("opentype");
  font-weight: 400;
  font-style: italic;
  font-display: swap;
}
@font-face {
  font-family: "Libertinus Serif";
  src: url("fonts/LibertinusSerif-Bold.otf") format("opentype");
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "Libertinus Serif";
  src: url("fonts/LibertinusSerif-BoldItalic.otf") format("opentype");
  font-weight: 700;
  font-style: italic;
  font-display: swap;
}

:root {
  --body: #000;
  --muted: #555;
  --light: #777;
  --faint: #999;
  --rule: #bbb;
  --link: #2244aa;
  --bg: #fbfaf6;
  --code-bg: var(--code-bg);
  --measure: 36rem;
}

/* Dark theme: explicitly set OR system dark while theme=system */
html[data-theme="dark"],
html[data-theme="system"]:where(:root) {
  /* default for system = light; overridden below via media query */
}
html[data-theme="dark"] {
  --body: #e8e4d8;
  --muted: #b3ab94;
  --light: #958e78;
  --faint: #6a6552;
  --rule: #3a372e;
  --link: #9cb4ff;
  --bg: #1c1b17;
  --code-bg: #26251e;
}
@media (prefers-color-scheme: dark) {
  html[data-theme="system"] {
    --body: #e8e4d8;
    --muted: #b3ab94;
    --light: #958e78;
    --faint: #6a6552;
    --rule: #3a372e;
    --link: #9cb4ff;
    --bg: #1c1b17;
    --code-bg: #26251e;
  }
}

* { box-sizing: border-box; }

html {
  font-size: 18px;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--body);
  font-family: "Libertinus Serif", "Linux Libertine", "Times New Roman", serif;
  font-feature-settings: "liga", "onum";
  line-height: 1.55;
  hyphens: auto;
  -webkit-hyphens: auto;
}

::selection {
  background: color-mix(in srgb, var(--link) 28%, transparent);
  color: var(--body);
}

main {
  display: block;
  max-width: var(--measure);
  margin: 3rem auto 6rem;
  padding: 0 1.5rem 2.25rem;
}

main p,
main li,
main blockquote {
  text-align: justify;
  text-justify: inter-word;
}

/* Footer pinned to the viewport bottom */
.pagefoot {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 10;
  padding: 0.4rem 1.25rem;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.2) blur(8px);
  -webkit-backdrop-filter: saturate(1.2) blur(8px);
  border-top: 0.5px solid var(--rule);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  font-size: 0.78rem;
  color: var(--light);
  white-space: nowrap;
}
.pagefoot a { color: var(--light); text-decoration: none; }
.pagefoot a:hover { color: var(--link); text-decoration: underline; }

/* Header */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.2) blur(8px);
  -webkit-backdrop-filter: saturate(1.2) blur(8px);
  border-bottom: 0.5px solid var(--rule);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 1.25rem;
  font-size: 0.9rem;
}
.topbar .brand {
  color: var(--muted);
  text-decoration: none;
  font-variant: small-caps;
  letter-spacing: 0.1em;
  hyphens: none;
  -webkit-hyphens: none;
}
.topbar .brand:hover { color: var(--body); }

.tools { display: inline-flex; gap: 0.15rem; align-items: center; }

.icon-btn {
  background: none;
  border: 0;
  padding: 0.35rem;
  color: var(--muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  text-decoration: none;
  transition: color 0.15s, background-color 0.15s;
}
.icon-btn:hover {
  color: var(--body);
  background: color-mix(in srgb, var(--body) 8%, transparent);
}
.icon-btn .icon {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linejoin: round;
}
.icon-btn .icon-solid {
  fill: currentColor;
  stroke: none;
}
.theme-toggle .icon { display: none; }
html[data-theme="light"]  .theme-toggle .icon-sun    { display: block; }
html[data-theme="dark"]   .theme-toggle .icon-moon   { display: block; }
html[data-theme="system"] .theme-toggle .icon-system { display: block; }

p { margin: 0 0 1em; text-indent: 1.5em; }
p:first-of-type, h1 + p, h2 + p, h3 + p, blockquote > p:first-child { text-indent: 0; }

a { color: var(--link); text-decoration: underline; text-underline-offset: 2px; }
a:hover { text-decoration-thickness: 2px; }

/* Cover-like opening block on the index */
.cover { text-align: center; margin: 6rem 0 4rem; }
.cover h1 {
  font-size: 2.2rem;
  font-weight: 700;
  line-height: 1.15;
  margin: 0 0 0.4em;
  hyphens: none;
  -webkit-hyphens: none;
  text-wrap: balance;
}
.cover .subtitle {
  font-size: 1.25rem;
  color: var(--muted);
  font-style: italic;
  margin: 0;
  hyphens: none;
  -webkit-hyphens: none;
  text-wrap: balance;
}
.cover p { margin: 0; text-indent: 0; text-align: center; }
.cover .author { margin-top: 2.5rem; color: var(--muted); }
.cover .meta { margin-top: 0.2rem; color: var(--faint); font-size: 0.85rem; }

/* Colophon: plain metadata block, no first-line indent */
.colophon { margin-top: 1.5em; }
.colophon p { text-indent: 0; margin: 0 0 0.8em; }
.colophon .colophon-text { color: var(--muted); margin-bottom: 1.5em; }
.colophon .colophon-license { color: var(--faint); font-size: 0.85rem; margin-top: 1.5em; }

/* Chapter opening (nav chip + title) */
.chapter-head {
  margin: 0 0 2.5rem;
  border-bottom: 0.5px solid var(--rule);
  padding-bottom: 1rem;
}
.chapter-head .chapnum {
  font-variant: small-caps;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-size: 0.85rem;
}
.chapter-head h1 {
  font-size: 1.75rem;
  margin: 0.3rem 0 0;
  font-weight: 700;
  line-height: 1.2;
  hyphens: none;
  -webkit-hyphens: none;
  text-wrap: balance;
}

/* Numbered headings, LaTeX-style.
   Only chapter-body sections participate in numbering. */
.chapter-body { counter-reset: h2; }
.chapter-body h2 {
  counter-increment: h2;
  counter-reset: h3;
  font-size: 1.25rem;
  font-weight: 700;
  margin: 2.5em 0 0.75em;
}
.chapter-body h2::before {
  content: counter(chapter) "." counter(h2) "  ";
}
.chapter-body h3 {
  counter-increment: h3;
  font-size: 1.1rem;
  font-weight: 700;
  margin: 2em 0 0.5em;
}
.chapter-body h3::before {
  content: counter(chapter) "." counter(h2) "." counter(h3) "  ";
}

/* Front matter (prefaci etc.) skips numbering */
.front-body h2 { font-size: 1.25rem; font-weight: 700; margin: 2.5em 0 0.75em; }
.front-body h3 { font-size: 1.1rem;  font-weight: 700; margin: 2em 0 0.5em; }

/* Blockquotes */
blockquote {
  margin: 1.5em 0;
  padding: 0.2em 0 0.2em 1.2em;
  border-left: 2px solid var(--rule);
  color: var(--muted);
  font-style: italic;
}

/* Lists */
ul, ol { padding-left: 1.8em; }
li { margin-bottom: 0.3em; }
li > p { text-indent: 0; margin: 0 0 0.4em; }

/* Tables */
table {
  width: 100%;
  margin: 1.5em 0;
  border-collapse: collapse;
  font-size: 0.95em;
}
th, td {
  padding: 0.5em 0.75em;
  border-bottom: 0.5px solid var(--rule);
  text-align: left;
}
th { font-weight: 700; border-bottom: 1px solid var(--body); }

/* Code */
code, pre, kbd, samp {
  font-family: "Libertinus Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-feature-settings: normal;
}
code {
  background: var(--code-bg);
  padding: 0.1em 0.3em;
  border-radius: 3px;
  font-size: 0.9em;
}
pre {
  background: var(--code-bg);
  padding: 1em 1.25em;
  overflow-x: auto;
  font-size: 0.85em;
  line-height: 1.45;
  border-radius: 3px;
  margin: 1.5em 0;
}
pre code { background: transparent; padding: 0; font-size: inherit; }

/* Horizontal rule */
hr { border: 0; border-top: 0.5px solid var(--rule); margin: 2em 0; }

/* TOC on index */
.toc { margin: 3rem 0; }
.toc h2 {
  text-align: center;
  font-size: 1.1rem;
  letter-spacing: 0.08em;
  font-variant: small-caps;
  font-weight: 700;
  margin: 0 0 1.5rem;
}
.toc ol {
  list-style: none;
  padding: 0;
  counter-reset: ch -1 letters 0;
}
.toc ol > li {
  margin: 0.4em 0;
  display: flex;
  align-items: baseline;
}
.toc ol > li a { flex: 1; text-decoration: none; color: var(--body); }
.toc ol > li a:hover { text-decoration: underline; }

/* Numbered chapter rows (default) */
.toc ol > li:not(.annex-heading):not(.annex-item) {
  counter-increment: ch;
}
.toc ol > li:not(.annex-heading):not(.annex-item)::before {
  content: counter(ch) ".";
  min-width: 2.2em;
  color: var(--muted);
}

/* Annex & Colofó headings: unnumbered, muted, spaced from the previous row */
.toc ol > li.annex-heading {
  margin-top: 1.2em;
  color: var(--muted);
}
.toc ol > li.annex-heading::before {
  content: "";
  min-width: 2.2em;
}
.toc ol > li.annex-heading a { color: var(--body); }

/* Annex items: letter-numbered (A, B, C, …), indented under the Annex label */
.toc ol > li.annex-item {
  counter-increment: letters;
  margin: 0.15em 0;
  padding-left: 2.2em;
}
.toc ol > li.annex-item::before {
  content: counter(letters, upper-alpha) ".";
  min-width: 2.2em;
  color: var(--muted);
}
.toc ol > li.annex-heading + .annex-item { margin-top: 0.3em; }

/* Prev/next nav at the bottom of every chapter */
.pager {
  margin-top: 6rem;
  padding-top: 2rem;
  border-top: 1px solid var(--rule);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: baseline;
  gap: 2rem;
  font-size: 0.9rem;
  color: var(--muted);
}
.pager > a {
  color: var(--muted);
  text-decoration: none;
  min-width: 0;
  display: flex;
  align-items: baseline;
  gap: 0.15em;
  width: 100%;
}
.pager > a:hover { color: var(--link); }
.pager > a:hover .label { text-decoration: underline; }
.pager > a:hover .arrow { color: var(--faint); }
.pager .prev     { justify-content: flex-start; }
.pager .next     { justify-content: flex-end; text-align: right; }
.pager .toc-link { justify-self: center; width: auto; font-variant: small-caps; letter-spacing: 0.08em; font-size: 0.8rem; }
.pager .label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
  flex: 1;
}
.pager .arrow {
  flex: 0 0 auto;
  color: var(--faint);
  pointer-events: none;
}
