/* ============================================================================
   THE WORLD CODEX -- mobile. "The night library": the desktop codex is
   ink-on-parchment; the phone is its nocturne. Warm ink-black, candle-gold and
   lore-purple, a high-contrast display serif over a warm reading serif.
   Dark-committed by design (the phone IS the night). One column, never wider
   than a page, never scrolling sideways.
   ============================================================================ */

/* ---- bundled OFL fonts (no CDN; woff2 committed under fonts/) --------------
   Display: Cormorant (high-contrast, candlelit). Body: Spectral (a serif drawn
   for screens). Fallback to Georgia so the app still reads before fonts land. */
@font-face { font-family: "Cormorant"; font-weight: 600; font-style: normal; font-display: swap;
  src: url("fonts/cormorant-semibold.woff2") format("woff2"); }
@font-face { font-family: "Cormorant"; font-weight: 700; font-style: normal; font-display: swap;
  src: url("fonts/cormorant-bold.woff2") format("woff2"); }
@font-face { font-family: "Spectral"; font-weight: 400; font-style: normal; font-display: swap;
  src: url("fonts/spectral-regular.woff2") format("woff2"); }
@font-face { font-family: "Spectral"; font-weight: 600; font-style: normal; font-display: swap;
  src: url("fonts/spectral-medium.woff2") format("woff2"); }
@font-face { font-family: "Spectral"; font-weight: 400; font-style: italic; font-display: swap;
  src: url("fonts/spectral-italic.woff2") format("woff2"); }

:root {
  --bg: #141118;
  --bg-2: #17131c;
  --surface: #1e1824;
  --surface-2: #241d2c;
  --ink: #e9ddc7;
  --ink-dim: #c7b59a;
  --muted: #a89d89;
  --faint: #7d7462;
  --line: #3a3040;
  --line-soft: #2a2432;
  --gold: #c9a86a;
  --gold-deep: #a2854e;
  --purple: #8f7fb8;
  --danger-low: #6f9256;
  --danger-mid: #c07f45;
  --danger-high: #c25049;

  --serif-display: "Cormorant", "Spectral", Georgia, "Times New Roman", serif;
  --serif-body: "Spectral", Georgia, "Times New Roman", serif;

  --max: 38rem;
  --pad: clamp(1rem, 4.5vw, 1.6rem);
  --radius: 12px;
  --nav-h: 4.4rem;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --dur: 190ms;
}

/* ---- reset ---------------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--serif-body);
  font-size: 17px;
  line-height: 1.62;
  overflow-x: hidden;                 /* LAYOUT LAW: never sideways */
  overflow-wrap: break-word;
  word-break: break-word;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  /* candle-lit atmosphere: a warm pool of light up top over the ink */
  background-image:
    radial-gradient(120% 60% at 50% -10%, rgba(201,168,106,0.09), transparent 60%),
    radial-gradient(90% 50% at 100% 100%, rgba(143,127,184,0.06), transparent 60%);
  background-attachment: fixed;
  min-height: 100vh;
}
a { color: inherit; text-decoration: none; }
img { max-width: 100%; display: block; }
h1, h2, h3, h4 { margin: 0; font-weight: 600; }
p { margin: 0; }
button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }
input { font: inherit; }

/* ---- app frame ------------------------------------------------------------ */
#app {
  max-width: var(--max);
  margin: 0 auto;
  padding: calc(var(--safe-top) + 0.5rem) var(--pad)
           calc(var(--nav-h) + var(--safe-bottom) + 2rem);
}
.screen { animation: rise var(--dur) ease both; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ---- splash --------------------------------------------------------------- */
#splash {
  position: fixed; inset: 0; z-index: 50;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 0.4rem; background: var(--bg);
  transition: opacity 400ms ease; text-align: center; padding: 2rem;
}
body:not(.booting) #splash { opacity: 0; pointer-events: none; }
.splash-mark { font-size: 3rem; color: var(--gold); }
.splash-name { font-family: var(--serif-display); font-size: 2rem; letter-spacing: 0.02em; }
.splash-sub { color: var(--muted); font-style: italic; font-size: 0.95rem; }

/* ---- bottom nav ----------------------------------------------------------- */
#nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  display: flex; justify-content: center; gap: 0.2rem;
  padding: 0.35rem 0.4rem calc(0.35rem + var(--safe-bottom));
  background: color-mix(in srgb, var(--bg-2) 86%, transparent);
  backdrop-filter: blur(14px) saturate(1.1);
  -webkit-backdrop-filter: blur(14px) saturate(1.1);
  border-top: 1px solid var(--line);
}
.nav-btn {
  flex: 1 1 0; max-width: 6rem;
  display: flex; flex-direction: column; align-items: center; gap: 0.15rem;
  padding: 0.45rem 0.2rem; border-radius: 10px;
  color: var(--faint); transition: color var(--dur) ease, background var(--dur) ease;
}
.nav-btn:active { background: var(--surface); }
.nav-glyph { font-size: 1.3rem; line-height: 1; }
.nav-label { font-size: 0.68rem; letter-spacing: 0.08em; text-transform: uppercase; }
.nav-btn.on { color: var(--gold); }

/* ---- section heads (small-caps rule -- the codex signature) ---------------- */
.sec-head, .sub-head { display: flex; align-items: center; gap: 0.6rem; }
.sec-head h3 {
  font-family: var(--serif-body); font-weight: 600; font-size: 0.86rem;
  letter-spacing: 0.14em; color: var(--gold);
  min-width: 0; overflow-wrap: anywhere;   /* long field-derived titles wrap, never overflow */
}
.sub-head h4 {
  font-family: var(--serif-body); font-weight: 600; font-size: 0.74rem;
  letter-spacing: 0.13em; color: var(--ink-dim);
  min-width: 0; overflow-wrap: anywhere;
}
.sec-rule, .sub-rule { flex: 1; height: 1px; background: linear-gradient(90deg, var(--line), transparent); }
.sub-rule { opacity: 0.6; }

/* ---- cards ---------------------------------------------------------------- */
.card {
  background: linear-gradient(180deg, var(--surface), var(--surface-2));
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  padding: 1.05rem 1.1rem;
  margin: 0.9rem 0;
  box-shadow: 0 1px 0 rgba(255,255,255,0.02) inset, 0 8px 22px -14px rgba(0,0,0,0.7);
}
.block-col { display: flex; flex-direction: column; gap: 0.7rem; }
.facts-col { display: flex; flex-direction: column; gap: 0.9rem; }
.subsection { display: flex; flex-direction: column; gap: 0.45rem; }
.sec-title {
  font-family: var(--serif-body); font-weight: 600; font-size: 0.82rem;
  letter-spacing: 0.14em; color: var(--gold); margin-bottom: 0.5rem;
}

/* ---- prose ---------------------------------------------------------------- */
.prose p { margin: 0 0 0.7rem; color: var(--ink); }
.prose p:last-child { margin-bottom: 0; }
.val { color: var(--ink); }
.muted { color: var(--muted); }
.small { font-size: 0.8rem; }

/* ---- facts grid ----------------------------------------------------------- */
.facts-grid {
  display: grid; grid-template-columns: minmax(6.5rem, 34%) 1fr;
  gap: 0.3rem 0.9rem; margin: 0;
}
.facts-grid dt {
  font-size: 0.74rem; letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--muted); font-weight: 600; align-self: start; padding-top: 0.12rem;
}
.facts-grid dd { margin: 0; color: var(--ink); min-width: 0; }
.facts-grid dd .chip-flow { margin: 0.1rem 0; }

/* ---- chips ---------------------------------------------------------------- */
.chip-flow { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.chip {
  display: inline-flex; align-items: center; gap: 0.36rem;
  padding: 0.2rem 0.55rem 0.2rem 0.3rem; border-radius: 999px;
  font-size: 0.82rem; line-height: 1.2; max-width: 100%;
}
.chip-link {
  background: color-mix(in srgb, var(--kc) 26%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--kc) 55%, transparent);
  color: color-mix(in srgb, var(--kc) 30%, var(--ink));
  transition: background var(--dur) ease, transform var(--dur) ease;
}
.chip-link:active { transform: scale(0.96); background: color-mix(in srgb, var(--kc) 40%, var(--surface)); }
.chip-glyph { color: var(--kc); font-size: 0.85rem; padding-left: 0.2rem; }
.chip-thumb { width: 1.35rem; height: 1.35rem; border-radius: 999px; object-fit: cover;
  border: 1px solid color-mix(in srgb, var(--kc) 60%, transparent); flex: none; }
.chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.chip-inert {
  padding-left: 0.55rem; background: var(--surface-2);
  border: 1px solid var(--line-soft); color: var(--muted);
}

/* ---- entry stacks & sub-entries ------------------------------------------- */
.stack { display: flex; flex-direction: column; gap: 0.55rem; }
.entry {
  background: color-mix(in srgb, var(--surface-2) 80%, #000);
  border: 1px solid var(--line-soft); border-left: 2px solid var(--gold-deep);
  border-radius: 8px; padding: 0.6rem 0.75rem;
  display: flex; flex-direction: column; gap: 0.35rem;
}
.entry-title { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.5rem; }
.entry-title strong { font-family: var(--serif-display); font-size: 1.08rem; font-weight: 600; }

/* ---- knowledge tiers (voice quotes) --------------------------------------- */
.tier {
  margin: 0; padding: 0.65rem 0.85rem; border-radius: 8px;
  background: color-mix(in srgb, var(--purple) 12%, var(--surface-2));
  border-left: 2px solid var(--purple);
}
.tier-voice {
  font-size: 0.72rem; letter-spacing: 0.12em; font-weight: 600;
  color: color-mix(in srgb, var(--purple) 55%, var(--ink)); margin-bottom: 0.2rem;
}
.tier p { font-style: italic; color: var(--ink-dim); }

/* ---- threat badge --------------------------------------------------------- */
.threat-band { display: flex; flex-wrap: wrap; align-items: center; gap: 0.6rem; margin-bottom: 0.2rem; }
.threat-badge {
  font-family: var(--serif-body); font-weight: 600; font-size: 0.95rem;
  letter-spacing: 0.06em; color: #f6ecd8; padding: 0.28rem 0.7rem; border-radius: 7px;
  box-shadow: 0 6px 18px -10px rgba(0,0,0,0.9);
}
.threat-low  { background: linear-gradient(180deg, var(--danger-low), #4d6b3c); }
.threat-mid  { background: linear-gradient(180deg, var(--danger-mid), #925c30); }
.threat-high { background: linear-gradient(180deg, var(--danger-high), #8f322c); }
.threat-tier { font-weight: 600; letter-spacing: 0.1em; font-size: 0.8rem; }
.threat-low-ink  { color: var(--danger-low); }
.threat-mid-ink  { color: var(--danger-mid); }
.threat-high-ink { color: var(--danger-high); }

/* ---- route ---------------------------------------------------------------- */
.route { display: flex; flex-wrap: wrap; align-items: center; gap: 0.3rem 0.4rem; }
.route-arrow { color: var(--gold); font-weight: 700; }
.route-stop { color: var(--ink-dim); }

/* ---- topic "see also" chip ------------------------------------------------ */
.topic-chip {
  font-style: italic; font-size: 0.78rem; color: var(--gold-deep); white-space: nowrap;
  min-width: 0; flex-shrink: 1; overflow: hidden; text-overflow: ellipsis;
}
.topic-chip:active { color: var(--gold); }

/* ---- images --------------------------------------------------------------- */
.image-row { display: flex; gap: 0.5rem; overflow-x: auto; scrollbar-width: none; margin: 0.2rem 0; }
.image-row::-webkit-scrollbar { display: none; }
.image-row .plate { margin: 0; flex: 1 1 0; min-width: 0; }
.image-row[data-n="1"] .plate { flex-basis: 100%; }
.plate img {
  width: 100%; border-radius: 8px; border: 1px solid var(--line);
  aspect-ratio: 2 / 3; object-fit: cover; background: var(--surface-2);
}
.image-row[data-n="1"] .plate img { aspect-ratio: auto; max-height: 72vh; object-fit: contain; }
.plate figcaption { font-size: 0.75rem; font-style: italic; color: var(--muted); text-align: center; margin-top: 0.25rem; }
.gallery-head { }

/* ============================================================================
   LIBRARY HOME
   ============================================================================ */
.home-mast { text-align: center; padding: 1.2rem 0 0.4rem; }
.wordmark {
  font-family: var(--serif-display); font-weight: 600;
  font-size: clamp(2.4rem, 12vw, 3.4rem); line-height: 1.02; letter-spacing: 0.01em;
  text-wrap: balance;
  background: linear-gradient(180deg, #f4e8ce, var(--gold) 120%);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.tagline { color: var(--muted); font-style: italic; margin-top: 0.35rem; text-wrap: balance; font-size: 0.95rem; }
.ledger {
  display: flex; flex-wrap: wrap; justify-content: center; gap: 0.2rem 0.55rem;
  margin-top: 0.9rem; color: var(--muted); font-size: 0.82rem;
}
.ledger b { color: var(--ink); font-weight: 600; }
.ledger-dot { color: var(--faint); }

.shelf-head { display: flex; align-items: center; gap: 0.7rem; margin: 1.7rem 0 0.5rem; }
.shelf-head h2 {
  font-family: var(--serif-body); font-weight: 600; font-size: 0.9rem;
  letter-spacing: 0.16em; color: var(--gold);
  min-width: 0; overflow-wrap: anywhere;
}
.shelf-rule { flex: 1; height: 1px; background: linear-gradient(90deg, var(--line), transparent); }

/* the shelf: a horizontal run of book spines, scroll-snapped like a real shelf */
.shelf-rail {
  display: flex; gap: 0.85rem; overflow-x: auto; scroll-snap-type: x proximity;
  padding: 0.5rem 0.2rem 1rem; margin: 0 calc(-1 * var(--pad));
  padding-left: var(--pad); padding-right: var(--pad);
  scrollbar-width: none;
}
.shelf-rail::-webkit-scrollbar { display: none; }
.book-cover {
  --bc: var(--gold);
  flex: 0 0 auto; width: 9.5rem; scroll-snap-align: start;
  display: flex; flex-direction: column; gap: 0.4rem;
  padding: 0.55rem 0.55rem 0.7rem 0.7rem; border-radius: 6px;
  background: linear-gradient(150deg,
    color-mix(in srgb, var(--bc) 42%, #000),
    color-mix(in srgb, var(--bc) 20%, #000));
  border: 1px solid color-mix(in srgb, var(--bc) 45%, transparent);
  border-left: 6px solid color-mix(in srgb, var(--bc) 70%, #000);
  box-shadow: 0 12px 26px -16px rgba(0,0,0,0.9);
  transition: transform var(--dur) ease, box-shadow var(--dur) ease;
}
.book-cover:active { transform: translateY(1px) scale(0.99); }
.cover-art {
  display: block; aspect-ratio: 3 / 2; border-radius: 4px; overflow: hidden;
  border: 1px solid color-mix(in srgb, var(--bc) 55%, transparent);
  background: rgba(0,0,0,0.35); position: relative;
}
.cover-art img { width: 100%; height: 100%; object-fit: cover; }
.cover-glyph { display: flex; align-items: center; justify-content: center; height: 100%;
  font-size: 2.4rem; color: color-mix(in srgb, var(--bc) 60%, var(--ink)); }
.cover-title {
  font-family: var(--serif-display); font-weight: 600; font-size: 1.12rem;
  line-height: 1.12; color: var(--ink); text-wrap: balance;
}
.cover-count { font-size: 0.68rem; color: color-mix(in srgb, var(--ink) 55%, transparent); font-style: italic; }

/* ============================================================================
   BOOK
   ============================================================================ */
.book { --bc: var(--gold); }
.book-hero { display: flex; gap: 1rem; align-items: flex-start; margin: 0.4rem 0 0.5rem; }
.book-hero-art {
  flex: 0 0 6.5rem; border-radius: 8px; overflow: hidden;
  border: 2px solid color-mix(in srgb, var(--bc) 55%, transparent);
  box-shadow: 0 10px 24px -14px rgba(0,0,0,0.9);
}
.book-hero-art img { width: 100%; aspect-ratio: 2 / 3; object-fit: cover; }
.book-hero-text { min-width: 0; display: flex; flex-direction: column; gap: 0.25rem; }
.book-glyph { font-size: 1.7rem; color: color-mix(in srgb, var(--bc) 60%, var(--ink)); line-height: 1; }
.book-hero-text h1 { font-family: var(--serif-display); font-size: clamp(1.9rem, 8vw, 2.5rem);
  line-height: 1.04; text-wrap: balance; }
.book-intro { color: var(--muted); font-style: italic; }
.book-meta { color: var(--faint); font-size: 0.82rem; }

.toc { display: flex; flex-wrap: wrap; gap: 0.4rem; margin: 0.6rem 0 0.4rem;
  padding-bottom: 0.7rem; border-bottom: 2px solid color-mix(in srgb, var(--bc) 40%, var(--line)); }
.toc-chip {
  font-size: 0.78rem; padding: 0.28rem 0.6rem; border-radius: 999px;
  background: color-mix(in srgb, var(--bc) 26%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--bc) 40%, transparent);
  color: var(--ink-dim);
}
.toc-chip:active { background: color-mix(in srgb, var(--bc) 40%, var(--surface)); }

.chapter { margin: 1.4rem 0; scroll-margin-top: 4rem; }
.chapter-head { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.6rem; }
.chapter-head h2 {
  font-family: var(--serif-body); font-weight: 600; font-size: 0.92rem; letter-spacing: 0.12em;
  text-transform: uppercase; color: color-mix(in srgb, var(--bc) 45%, var(--ink));
  min-width: 0; overflow-wrap: anywhere;
}
.chapter-count { color: var(--muted); font-size: 0.82rem; flex: none; }
.chapter-rule { flex: 1; height: 1px; background: var(--line); }
.chapter-text { margin: 0.4rem 0 0.7rem; color: var(--ink-dim); }
.chapter-text p { margin: 0 0 0.5rem; }

/* ---- entry rows ----------------------------------------------------------- */
.row-list { display: flex; flex-direction: column; gap: 0.2rem; }
.row {
  --kc: var(--gold);
  display: flex; align-items: center; gap: 0.75rem;
  padding: 0.5rem 0.4rem; border-radius: 9px; min-width: 0;
  transition: background var(--dur) ease;
}
.row:active { background: var(--surface); }
.row + .row { border-top: 1px solid var(--line-soft); }
.row-thumb {
  flex: 0 0 2.6rem; width: 2.6rem; height: 2.6rem; border-radius: 7px; overflow: hidden;
  background: var(--surface-2); border: 1px solid var(--line-soft);
  display: flex; align-items: center; justify-content: center;
}
.row-thumb img { width: 100%; height: 100%; object-fit: cover; }
.row-glyph { color: var(--kc); font-size: 1.2rem; }
.row-body { min-width: 0; display: flex; flex-direction: column; }
.row-name { font-family: var(--serif-display); font-size: 1.12rem; font-weight: 600; line-height: 1.15;
  color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { font-size: 0.78rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-inert { color: var(--faint); padding: 0.5rem 0.4rem; }

/* ============================================================================
   ARTICLE
   ============================================================================ */
.article { --kc: var(--gold); }
.art-mini {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 0.5rem;
  margin: 0 calc(-1 * var(--pad)); padding: 0.5rem var(--pad);
  padding-top: calc(0.5rem + var(--safe-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line-soft);
}
.art-mini-glyph { color: var(--kc); font-size: 1.1rem; }
.art-mini-name { font-family: var(--serif-display); font-weight: 600; font-size: 1.05rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; }
.art-mini-kind { font-size: 0.68rem; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--kc); white-space: nowrap; }

.art-hero {
  margin: 0.8rem 0; border-radius: var(--radius); overflow: hidden; position: relative;
  border: 1px solid var(--line);
}
.art-hero img { width: 100%; max-height: 68vh; object-fit: contain; background: var(--surface-2); }
.art-hero-vignette { position: absolute; inset: 0; pointer-events: none;
  box-shadow: inset 0 -60px 60px -30px rgba(20,17,24,0.9), inset 0 0 40px rgba(0,0,0,0.35); }

.art-head { display: flex; align-items: flex-start; gap: 0.6rem; margin-top: 0.6rem; }
.art-glyph { font-size: 1.8rem; color: var(--kc); line-height: 1.1; }
.art-titles { min-width: 0; }
.art-titles h1 { font-family: var(--serif-display); font-weight: 700;
  font-size: clamp(2rem, 9vw, 2.8rem); line-height: 1.03; text-wrap: balance; }
.art-sub { color: var(--muted); font-style: italic; margin-top: 0.15rem; }
.art-badges { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; margin: 0.6rem 0 0.2rem; }
.kind-badge {
  font-size: 0.72rem; letter-spacing: 0.1em; text-transform: uppercase; font-weight: 600;
  padding: 0.24rem 0.6rem; border-radius: 999px; color: #f4ead4;
  background: color-mix(in srgb, var(--kc) 55%, #000);
  border: 1px solid color-mix(in srgb, var(--kc) 70%, transparent);
}
.aka { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; margin: 0.4rem 0; }
.aka-label { font-size: 0.68rem; letter-spacing: 0.12em; color: var(--muted); font-weight: 600; }

/* connections */
.conn-group { margin-bottom: 0.7rem; }
.conn-group:last-child { margin-bottom: 0; }
.conn-kind { font-size: 0.7rem; letter-spacing: 0.08em; text-transform: uppercase; font-weight: 600; margin-bottom: 0.35rem; }
.lib-row { display: flex; flex-direction: column; padding: 0.45rem 0; border-top: 1px solid var(--line-soft); }
.lib-row:first-of-type { border-top: none; }
.lib-book { color: var(--gold-deep); font-weight: 600; font-size: 0.9rem; }
.lib-chap { color: var(--muted); font-size: 0.78rem; }

/* ============================================================================
   SEARCH
   ============================================================================ */
.search-bar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 0.5rem;
  margin: 0 calc(-1 * var(--pad)) 0.6rem;
  padding: calc(0.6rem + var(--safe-top)) var(--pad) 0.6rem;
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.search-icon { color: var(--gold); font-size: 1.3rem; }
.search-input {
  flex: 1; min-width: 0; background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: 10px; padding: 0.6rem 0.8rem;
  font-family: var(--serif-display); font-size: 1.15rem;
}
.search-input::placeholder { color: var(--faint); font-style: italic; }
.search-input:focus { outline: none; border-color: var(--gold-deep); }
.filter-pills { display: flex; gap: 0.4rem; overflow-x: auto; padding: 0.1rem 0 0.7rem;
  scrollbar-width: none; margin: 0 calc(-1 * var(--pad)); padding-left: var(--pad); padding-right: var(--pad); }
.filter-pills::-webkit-scrollbar { display: none; }
.pill {
  --kc: var(--gold); flex: 0 0 auto; font-size: 0.8rem; padding: 0.3rem 0.7rem; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--line); color: var(--muted); white-space: nowrap;
}
.pill.on { color: #f4ead4; background: color-mix(in srgb, var(--kc) 45%, #000);
  border-color: color-mix(in srgb, var(--kc) 60%, transparent); }
.result-count { padding: 0 0.4rem 0.3rem; }
.empty { text-align: center; padding: 2rem 1rem; font-style: italic; }

/* ============================================================================
   HISTORY / crumbs / misc
   ============================================================================ */
.page-head { text-align: center; padding: 1rem 0 0.6rem; }
.page-head h1 { font-family: var(--serif-display); font-size: 2.2rem; }
.crumb { display: flex; flex-wrap: wrap; align-items: center; gap: 0.35rem; margin: 0.3rem 0 0.2rem;
  font-size: 0.78rem; }
.crumb-link { color: var(--gold-deep); font-weight: 600; }
.crumb-here { color: var(--muted); }
.crumb-sep { color: var(--faint); }

.loading { display: flex; align-items: center; justify-content: center; gap: 0.6rem;
  padding: 4rem 1rem; color: var(--muted); font-style: italic; }
.spinner { width: 1rem; height: 1rem; border-radius: 999px;
  border: 2px solid var(--line); border-top-color: var(--gold); animation: spin 700ms linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.not-found, .boot-error { text-align: center; padding: 3rem 1rem; }
.not-found h1, .boot-error h1 { font-family: var(--serif-display); font-size: 1.8rem; margin-bottom: 0.5rem; }
.boot-error p { color: var(--muted); max-width: 30rem; margin: 0.5rem auto; }
.back-home { color: var(--gold); font-style: italic; }

/* ---- reduced motion ------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

/* ============================================================================
   CHARACTERS -- the figures dashboard + the character forge (maker).
   Same nocturne: candle-gold actions on ink-black, serif everywhere.
   ============================================================================ */
.sec-count {
  font-family: var(--serif-body); font-size: 0.8rem; color: var(--gold);
  border: 1px solid var(--line); border-radius: 999px; padding: 0.05rem 0.55rem;
}
.search-bar.inset { margin: 0.4rem 0 0.5rem; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
  font-family: var(--serif-body); font-size: 1rem; color: var(--ink);
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 999px; padding: 0.55rem 1.1rem; cursor: pointer;
  text-decoration: none; transition: border-color var(--dur), color var(--dur);
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; pointer-events: none; }
.btn-primary { color: var(--gold); border-color: var(--gold-deep); }
.btn-danger { color: var(--danger-high); border-color: color-mix(in srgb, var(--danger-high) 45%, var(--line)); }
.btn-block { display: flex; width: 100%; margin: 0.5rem 0 0.8rem; }
.btn-row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-top: 0.6rem; }

/* drafts */
.drafts-card { margin-top: 0.4rem; }
.draft-list { display: flex; flex-direction: column; gap: 0.15rem; margin-top: 0.5rem; }
.draft-row { display: flex; align-items: center; gap: 0.6rem; }
.chk { width: 1.15rem; height: 1.15rem; accent-color: var(--gold); flex: none; }
.draft-body {
  flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 0.1rem;
  text-decoration: none; color: var(--ink); padding: 0.45rem 0.5rem;
  border-radius: 8px; border: 1px solid transparent;
}
.draft-body:active { border-color: var(--line); background: var(--surface-2); }
.draft-name { font-weight: 600; }
.draft-line { font-size: 0.85rem; color: var(--muted); font-style: italic;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* export */
.export-box { margin-top: 0.6rem; }
.export-ta {
  width: 100%; font: 0.72rem/1.45 ui-monospace, Consolas, monospace;
  color: var(--ink-dim); background: var(--bg-2); border: 1px solid var(--line);
  border-radius: 8px; padding: 0.6rem; resize: vertical; margin-top: 0.4rem;
}

/* maker form */
.maker-form { display: flex; flex-direction: column; gap: 0.9rem; margin-top: 0.6rem; }
.maker-field { display: flex; flex-direction: column; gap: 0.3rem; }
.field-label {
  font-size: 0.72rem; letter-spacing: 0.14em; color: var(--gold);
  font-family: var(--serif-body);
}
.field-hint { font-size: 0.8rem; color: var(--faint); font-style: italic; }
.f-input {
  width: 100%; font-family: var(--serif-body); font-size: 1rem; color: var(--ink);
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px;
  padding: 0.6rem 0.7rem; outline: none;
}
.f-input:focus { border-color: var(--gold-deep); }
.f-area { line-height: 1.5; resize: vertical; }
select.f-input { appearance: none; }
.save-hint { font-size: 0.82rem; color: var(--faint); font-style: italic; margin-right: auto; }
.maker-btns { margin: 1.2rem 0 0.5rem; }

/* entity picker */
.picker-box { display: flex; flex-direction: column; gap: 0.4rem; }
.tie-slot { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.tie-slot:empty { display: none; }
.tie-chip {
  display: inline-flex; align-items: center; gap: 0.35rem;
  font-size: 0.88rem; color: var(--ink); background: var(--surface-2);
  border: 1px solid var(--gold-deep); border-radius: 999px; padding: 0.2rem 0.3rem 0.2rem 0.7rem;
}
.tie-x { color: var(--muted); font-size: 1rem; padding: 0 0.35rem; }
.picker-pop { display: flex; flex-direction: column; }
.picker-pop:not(:empty) {
  border: 1px solid var(--line); border-radius: 8px; margin-top: 0.25rem;
  background: var(--surface-2); overflow: hidden;
}
.picker-row {
  display: flex; align-items: center; gap: 0.55rem; text-align: left;
  padding: 0.5rem 0.7rem; border-bottom: 1px solid var(--line-soft);
}
.picker-row:last-child { border-bottom: none; }
.picker-row:active { background: var(--surface); }
.picker-glyph { color: var(--kc, var(--gold)); flex: none; }
.picker-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.picker-kind { font-size: 0.72rem; color: var(--faint); flex: none; }

/* inline wikilinks in prose -- lore-purple, wikipedia-style */
a.wikilink {
  color: color-mix(in srgb, var(--purple) 82%, var(--ink));
  text-decoration: underline;
  text-decoration-color: color-mix(in srgb, var(--purple) 40%, transparent);
  text-underline-offset: 0.16em;
}
a.wikilink:active { color: var(--gold); }

/* ==========================================================================
   THE STORY BIBLE -- the author's private book. Distinct cover, readable
   long-form chapters (two-axis audit: prose wraps, tables scroll in their
   own container -- the page never scrolls sideways), and the idea pad.
   ========================================================================== */
.bible-cover .cover-art {
  border: 1px dashed color-mix(in srgb, var(--bc) 65%, transparent);
  border-radius: 10px;
}
.bible-private {
  font-size: 0.82rem; font-style: italic; color: var(--ink-dim);
  border: 1px dashed color-mix(in srgb, #8f3f4f 55%, transparent);
  border-radius: 10px; padding: 0.55rem 0.8rem; margin: 0.6rem 0 0.9rem;
  overflow-wrap: anywhere;
}
.bible-stamp { margin-top: 0.6rem; font-style: italic; }
.idea-row { display: flex; flex-direction: column; min-width: 0; }

.bible-card { padding-top: 0.9rem; }
.bible-prose { min-width: 0; }
.bible-prose p, .bible-prose li { overflow-wrap: anywhere; }
.bible-h2 {
  font-size: 1.06rem; letter-spacing: 0.06em; text-transform: uppercase;
  color: color-mix(in srgb, #c76b7f 60%, var(--ink));
  margin: 1.1rem 0 0.45rem; text-wrap: balance;
}
.bible-h2:first-child { margin-top: 0; }
.bible-h3 {
  font-size: 0.95rem; color: var(--ink);
  margin: 0.9rem 0 0.35rem; text-wrap: balance;
}
.bible-quote {
  margin: 0.7rem 0; padding: 0.45rem 0.85rem; font-style: italic; color: var(--ink);
  border-left: 3px solid color-mix(in srgb, #8f3f4f 70%, transparent);
  background: color-mix(in srgb, #8f3f4f 10%, transparent);
  border-radius: 0 8px 8px 0; overflow-wrap: anywhere;
}
.bible-list { margin: 0 0 0.7rem; padding-left: 1.1rem; }
.bible-list li { margin: 0.25rem 0; color: var(--ink); }

.table-scroll { overflow-x: auto; max-width: 100%; margin: 0.7rem 0; }
.bible-table { border-collapse: collapse; font-size: 0.85rem; min-width: 30rem; }
.bible-table th, .bible-table td {
  border: 1px solid var(--line); padding: 0.4rem 0.55rem;
  text-align: left; vertical-align: top; color: var(--ink);
}
.bible-table th {
  font-size: 0.72rem; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--ink-dim); background: var(--surface-2);
}

/* ==========================================================================
   DESKTOP (>=1024px) — the codex as a real site, not a stretched phone.
   Mobile (<1024) is untouched: everything below lives behind the breakpoint.
   ========================================================================== */
@media (min-width: 1024px) {
  :root { --max: 1240px; --pad: 2rem; }

  /* the bottom tab bar becomes a top bar with the wordmark on the left */
  #nav {
    top: 0; bottom: auto;
    justify-content: flex-end; align-items: center; gap: 0.3rem;
    padding: 0.4rem 2rem;
    border-top: none; border-bottom: 1px solid var(--line);
  }
  #nav::before {
    content: "The World Codex";
    font-family: var(--serif-display); font-size: 1.15rem; color: var(--ink);
    margin-right: auto; letter-spacing: 0.02em;
  }
  .nav-btn {
    flex: 0 0 auto; max-width: none;
    flex-direction: row; gap: 0.45rem; padding: 0.45rem 0.9rem;
  }
  .nav-glyph { font-size: 1rem; }
  #app {
    padding-top: 4.4rem;
    padding-bottom: 3rem;
  }

  /* library: shelves wrap into cover grids, covers grow */
  .shelf-rail { flex-wrap: wrap; overflow-x: visible; scroll-snap-type: none; }
  .book-cover { width: 11.5rem; }
  .home-mast { margin-top: 1rem; }

  /* article: reading column + sticky right rail */
  .screen.article {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 340px;
    column-gap: 2.2rem;
    align-items: start;
  }
  .screen.article > .art-mini,
  .screen.article > .loading,
  .screen.article > .not-found { grid-column: 1 / -1; }
  .art-main { grid-column: 1; min-width: 0; }
  .art-rail {
    grid-column: 2; min-width: 0;
    position: sticky; top: 4.6rem;
    max-height: calc(100vh - 6rem); overflow-y: auto;
    display: flex; flex-direction: column; gap: 1rem;
    scrollbar-width: thin;
  }
  .art-rail:empty { display: none; }
  .art-hero img { max-height: 58vh; }

  /* book: sticky chapter TOC as a left rail */
  .book-body {
    display: grid;
    grid-template-columns: 264px minmax(0, 1fr);
    column-gap: 2.2rem;
    align-items: start;
  }
  .book-body .toc {
    position: sticky; top: 4.6rem;
    max-height: calc(100vh - 6rem); overflow-y: auto;
    display: flex; flex-direction: column; gap: 0.3rem;
    scrollbar-width: thin;
  }
  .book-chapters { min-width: 0; }

  /* search: two result columns */
  .search-results {
    display: grid; grid-template-columns: 1fr 1fr;
    column-gap: 1.4rem; align-items: start;
  }
  .search-results > .result-count,
  .search-results > .empty { grid-column: 1 / -1; }

  /* image galleries breathe on wide screens */
  .image-row[data-n="1"] .plate img { max-height: 64vh; }
}

/* desktop: kill the mobile edge-bleed negative margins — with wrapping grids
   and a centered container they paint OUTSIDE the layout (author: "cortado
   nas laterais"). The bleed is a phone idiom; on desktop content stays in. */
@media (min-width: 1024px) {
  .shelf-rail,
  .art-mini,
  .search-bar,
  .filter-pills {
    margin-left: 0;
    margin-right: 0;
    padding-left: 0.2rem;
    padding-right: 0.2rem;
  }
  .art-mini { border-radius: 10px; }
  .filter-pills { flex-wrap: wrap; overflow-x: visible; }
  .search-bar { padding-left: 0; padding-right: 0; }
}
