/* =========================================================
   TNHEC — The Never Happy Ending Chronicle
   Dark academia, editorial, book-within-page
   Palette:
     #1a1410 Binding   #f0e8d8 Page    #2a2018 Ink
     #8b2500 Blood     #b8963a Gilt    #a0988a Fog
   Fonts: Playfair Display (display), Crimson Pro (body), Josefin Sans (headers)
   ========================================================= */

:root {
    --binding: #1a1410;
    --binding-deep: #0f0a07;
    --page: #f0e8d8;
    --page-deep: #e3d8c0;
    --ink: #2a2018;
    --blood: #8b2500;
    --gilt: #b8963a;
    --fog: #a0988a;

    --serif-display: "Playfair Display", "Times New Roman", serif;
    --serif-body: "Crimson Pro", "Iowan Old Style", Georgia, serif;
    --sans: "Josefin Sans", "Helvetica Neue", Arial, sans-serif;

    --max-page: 680px;
    --aside-w: 300px;
    --rh-h: 44px;
    --gutter: clamp(1rem, 3vw, 2.25rem);
}

* { box-sizing: border-box; }
html, body {
    margin: 0;
    padding: 0;
    background: var(--binding);
    color: var(--ink);
}

html { scroll-behavior: smooth; }

body {
    font-family: var(--serif-body);
    font-weight: 400;
    font-size: 17px;
    line-height: 1.85;
    background:
        radial-gradient(1200px 800px at 80% -10%, rgba(184,150,58,0.05), transparent 60%),
        radial-gradient(900px 700px at -10% 110%, rgba(139,37,0,0.07), transparent 60%),
        linear-gradient(180deg, #1a1410 0%, #14100c 100%);
    min-height: 100vh;
    overflow-x: hidden;
}

/* ---------- Page Texture (subtle noise on dark binding) ---------- */
.page-texture {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 0;
    opacity: 0.35;
    mix-blend-mode: overlay;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='240' height='240'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.05  0 0 0 0 0.04  0 0 0 0 0.03  0 0 0 0.55 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
}

/* ---------- Running Header (fixed, book convention) ---------- */
.running-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--rh-h);
    z-index: 50;
    background: linear-gradient(180deg, rgba(15,10,7,0.95) 0%, rgba(26,20,16,0.85) 100%);
    border-bottom: 1px solid rgba(184,150,58,0.25);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
}

.running-header-inner {
    max-width: 1280px;
    height: 100%;
    margin: 0 auto;
    padding: 0 1.25rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-family: var(--sans);
    font-weight: 300;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.15em;
    color: var(--fog);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.rh-title {
    color: var(--page);
    font-weight: 400;
}

.rh-divider { color: var(--gilt); opacity: 0.7; }

.rh-chapter {
    color: var(--gilt);
    flex: 0 1 auto;
    overflow: hidden;
    text-overflow: ellipsis;
}

.rh-folio {
    margin-left: auto;
    color: var(--fog);
    font-feature-settings: "tnum" 1;
}

/* ---------- Volume layout (binding holds aside + book) ---------- */
.volume {
    position: relative;
    z-index: 1;
    max-width: 1280px;
    margin: 0 auto;
    padding: calc(var(--rh-h) + 1.5rem) var(--gutter) 5rem;
    display: grid;
    grid-template-columns: var(--aside-w) minmax(0, 1fr);
    gap: clamp(1.5rem, 3vw, 3rem);
    align-items: start;
}

/* ---------- Chapter Index (sidebar) ---------- */
.chapter-index {
    position: sticky;
    top: calc(var(--rh-h) + 1.5rem);
    color: var(--fog);
    padding: 0;
}

.ci-inner {
    border: 1px solid rgba(184,150,58,0.18);
    border-radius: 2px;
    padding: 1.75rem 1.5rem 1.5rem;
    background:
        linear-gradient(180deg, rgba(26,20,16,0.7), rgba(15,10,7,0.7));
    box-shadow:
        inset 0 0 0 1px rgba(240,232,216,0.03),
        0 20px 40px -20px rgba(0,0,0,0.8);
}

.ci-mark {
    font-family: var(--sans);
    font-weight: 400;
    font-size: 0.65rem;
    letter-spacing: 0.4em;
    color: var(--gilt);
    text-transform: uppercase;
    margin-bottom: 0.5rem;
}

.ci-eyebrow {
    font-family: var(--sans);
    font-weight: 300;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.2em;
    color: var(--fog);
    margin-bottom: 0.5rem;
}

.ci-title {
    font-family: var(--serif-display);
    font-weight: 700;
    color: var(--page);
    font-size: clamp(1.5rem, 2vw, 1.85rem);
    line-height: 1.05;
    margin: 0 0 1rem;
    letter-spacing: 0.005em;
}

.ci-rule {
    height: 1px;
    background: linear-gradient(90deg, transparent, var(--gilt), transparent);
    opacity: 0.5;
    margin: 1.1rem 0;
}

.ci-nav { padding: 0.25rem 0; }

.ci-list {
    list-style: none;
    margin: 0;
    padding: 0;
    counter-reset: chap;
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
}

.ci-link {
    display: grid;
    grid-template-columns: 2.25rem 1fr;
    align-items: baseline;
    gap: 0.5rem;
    padding: 0.55rem 0.25rem;
    color: var(--fog);
    text-decoration: none;
    border-bottom: 1px dashed rgba(160,152,138,0.15);
    transition: color 220ms ease, background 220ms ease, padding 220ms ease;
}

.ci-link:last-child { border-bottom: none; }

.ci-link .ci-num {
    font-family: var(--sans);
    font-weight: 300;
    font-size: 0.7rem;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    color: var(--gilt);
    text-align: right;
    padding-right: 0.5rem;
    border-right: 1px solid rgba(184,150,58,0.25);
}

.ci-link .ci-name {
    font-family: var(--serif-body);
    font-style: italic;
    font-size: 0.95rem;
    color: var(--page-deep);
    line-height: 1.3;
}

.ci-link:hover {
    color: var(--page);
    background: linear-gradient(90deg, rgba(184,150,58,0.06), transparent 80%);
    padding-left: 0.55rem;
}

.ci-link:hover .ci-name { color: var(--gilt); }

.ci-link.active {
    background: linear-gradient(90deg, rgba(139,37,0,0.18), transparent 90%);
    padding-left: 0.55rem;
}
.ci-link.active .ci-num { color: var(--blood); }
.ci-link.active .ci-name {
    color: var(--page);
    font-style: normal;
    font-weight: 500;
}

.ci-note {
    font-family: var(--serif-body);
    font-style: italic;
    font-size: 0.85rem;
    line-height: 1.6;
    color: var(--fog);
    margin: 0.25rem 0 1.1rem;
    opacity: 0.85;
}

.ci-press {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-family: var(--sans);
    font-weight: 300;
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.2em;
    color: var(--fog);
    border-top: 1px solid rgba(184,150,58,0.18);
    padding-top: 0.85rem;
}

.ci-press-mark { color: var(--gilt); font-size: 0.85rem; }
.ci-press-name { color: var(--page-deep); }
.ci-press-year { margin-left: auto; color: var(--gilt); }

/* ---------- The Book (light page on dark binding) ---------- */
.book {
    background: var(--page);
    color: var(--ink);
    max-width: var(--max-page);
    margin: 0 auto;
    padding: clamp(2rem, 4vw, 3rem);
    border-radius: 2px;
    position: relative;
    box-shadow:
        0 0 0 1px rgba(184,150,58,0.25),
        0 10px 30px -8px rgba(0,0,0,0.55),
        0 40px 80px -20px rgba(0,0,0,0.8);
    background-image:
        linear-gradient(180deg, rgba(184,150,58,0.04), transparent 200px),
        url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='p'><feTurbulence type='fractalNoise' baseFrequency='1.2' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.16  0 0 0 0 0.12  0 0 0 0 0.09  0 0 0 0.02 0'/></filter><rect width='100%' height='100%' filter='url(%23p)'/></svg>");
}

/* binding edge — left thin gilt line */
.book::before {
    content: "";
    position: absolute;
    top: 0; bottom: 0;
    left: 0;
    width: 3px;
    background: linear-gradient(180deg, transparent, var(--gilt) 18%, var(--gilt) 82%, transparent);
    opacity: 0.45;
}
/* fore-edge soft shadow on right */
.book::after {
    content: "";
    position: absolute;
    top: 1.5rem; bottom: 1.5rem;
    right: -3px;
    width: 6px;
    background: linear-gradient(90deg, transparent, rgba(0,0,0,0.35));
    pointer-events: none;
    border-radius: 2px;
}

/* ---------- Page (each chapter article) ---------- */
.page {
    padding: 0;
    margin: 0 0 4.5rem;
    scroll-margin-top: calc(var(--rh-h) + 1.25rem);
}
.page:last-child { margin-bottom: 1rem; }

/* ---------- Frontispiece ---------- */
.frontispiece {
    text-align: center;
    padding: 2rem 0 3.5rem;
    border-bottom: 1px solid rgba(42,32,24,0.18);
    margin-bottom: 3rem;
}

.fp-rule {
    width: 60%;
    height: 1px;
    margin: 0 auto 1rem;
    background: linear-gradient(90deg, transparent, var(--ink), transparent);
    opacity: 0.6;
}
.fp-rule + .fp-rule { margin-top: 1rem; }

.fp-mark {
    font-family: var(--serif-display);
    color: var(--blood);
    font-size: 1.25rem;
    margin: 0.4rem 0;
}

.fp-eyebrow {
    font-family: var(--sans);
    font-weight: 300;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.32em;
    color: var(--blood);
    margin: 1.25rem 0 0.5rem;
}

.fp-title {
    font-family: var(--serif-display);
    font-weight: 700;
    font-size: clamp(2.5rem, 6vw, 4.25rem);
    line-height: 1.02;
    color: var(--ink);
    margin: 0.4rem 0 1rem;
    letter-spacing: 0.005em;
}

.fp-subtitle {
    font-family: var(--serif-body);
    font-style: italic;
    font-size: 1.05rem;
    line-height: 1.6;
    color: var(--ink);
    max-width: 32em;
    margin: 0.5rem auto 2rem;
    opacity: 0.85;
}

.fp-byline {
    margin: 1.5rem 0 2rem;
    font-family: var(--serif-body);
}

.fp-by {
    font-family: var(--sans);
    font-weight: 300;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.25em;
    color: var(--ink);
    opacity: 0.55;
    display: block;
    margin-bottom: 0.4rem;
}

.fp-author {
    font-family: var(--serif-display);
    font-weight: 700;
    font-size: 1.4rem;
    color: var(--ink);
    letter-spacing: 0.04em;
}

.fp-imprint {
    font-family: var(--sans);
    font-weight: 300;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.22em;
    color: var(--ink);
    opacity: 0.75;
    margin: 1.75rem 0 0.5rem;
    line-height: 2;
}

.fp-press { color: var(--blood); font-weight: 400; letter-spacing: 0.28em; }
.fp-place { opacity: 0.7; }
.fp-year { color: var(--gilt); font-weight: 400; letter-spacing: 0.4em; }

/* ---------- Chapter Opening ---------- */
.chapter-opening {
    text-align: center;
    margin: 0 0 2.5rem;
}

.chapter-eyebrow {
    font-family: var(--sans);
    font-weight: 300;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.28em;
    color: var(--blood);
    margin: 0 0 1rem;
}

.chapter-title {
    font-family: var(--serif-display);
    font-weight: 700;
    font-size: clamp(1.75rem, 3vw, 2.5rem);
    line-height: 1.1;
    color: var(--ink);
    margin: 0 0 0.75rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1.25rem;
    letter-spacing: 0.005em;
}

.ct-rule {
    flex: 1 1 auto;
    max-width: 6rem;
    height: 1px;
    background: linear-gradient(90deg, transparent, var(--ink), transparent);
    opacity: 0.45;
}

.chapter-dateline {
    font-family: var(--serif-body);
    font-style: italic;
    font-size: 0.95rem;
    color: var(--ink);
    opacity: 0.65;
    margin: 0.5rem 0 0;
}

/* ---------- Body Text ---------- */
.book p {
    font-family: var(--serif-body);
    font-size: 1.0625rem;
    line-height: 1.85;
    color: var(--ink);
    margin: 0 0 1.35rem;
    text-align: justify;
    hyphens: auto;
    -webkit-hyphens: auto;
    text-rendering: optimizeLegibility;
}

.book p + p {
    text-indent: 1.5em;
}

/* first paragraph after chapter-opening keeps no indent (drop cap) */
.chapter-opening + p { text-indent: 0; }

/* ---------- Drop Cap (Playfair, 4em, Blood) ---------- */
.dropcap {
    font-family: var(--serif-display);
    font-weight: 700;
    font-size: 4em;
    line-height: 0.85;
    float: left;
    color: var(--blood);
    padding: 0.05em 0.12em 0 0;
    margin: 0.05em 0.05em -0.05em 0;
    text-shadow: 0 1px 0 rgba(184,150,58,0.18);
}

/* ---------- Pull Quote (Gilt left rule, italic) ---------- */
.pullquote {
    border-left: 3px solid var(--gilt);
    padding: 0.25rem 0 0.25rem 1.5rem;
    margin: 2rem 0 2rem 0.5rem;
    font-style: italic;
    color: var(--ink);
}

.pullquote p {
    font-family: var(--serif-body);
    font-style: italic;
    font-size: 1.18rem;
    line-height: 1.55;
    margin: 0;
    text-align: left;
    text-indent: 0;
    color: var(--ink);
}

.pullquote p em {
    font-style: normal;
    color: var(--blood);
    font-weight: 500;
}

.pullquote cite {
    display: block;
    margin-top: 0.75rem;
    font-style: normal;
    font-family: var(--sans);
    font-weight: 300;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.22em;
    color: var(--ink);
    opacity: 0.6;
}

/* ---------- Ornament (chapter end) ---------- */
.ornament {
    text-align: center;
    color: var(--gilt);
    font-family: var(--serif-display);
    letter-spacing: 0.6em;
    margin: 2rem 0 0.5rem;
    opacity: 0.7;
}
.ornament.final {
    color: var(--blood);
    margin-top: 1rem;
}

/* ---------- Colophon ---------- */
.colophon-grid {
    margin: 2rem 0;
    padding: 1.25rem 0;
    border-top: 1px solid rgba(42,32,24,0.18);
    border-bottom: 1px solid rgba(42,32,24,0.18);
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    font-family: var(--sans);
}

.cg-row {
    display: grid;
    grid-template-columns: 7rem 1fr;
    gap: 1rem;
    font-size: 0.78rem;
    letter-spacing: 0.06em;
}

.cg-key {
    font-weight: 300;
    text-transform: uppercase;
    letter-spacing: 0.2em;
    font-size: 0.7rem;
    color: var(--ink);
    opacity: 0.6;
}

.cg-val {
    font-family: var(--serif-body);
    font-style: italic;
    color: var(--ink);
    font-size: 0.95rem;
    letter-spacing: 0;
}

.finis {
    font-family: var(--serif-display);
    font-weight: 700;
    text-align: center;
    letter-spacing: 0.5em;
    color: var(--blood);
    margin: 2.5rem 0 1rem !important;
    text-indent: 0 !important;
    font-size: 1.05rem;
}

/* ---------- Reader toggle (mobile chapter index button) ---------- */
.reader-toggle {
    display: none;
    position: fixed;
    bottom: 1.25rem;
    right: 1.25rem;
    z-index: 60;
    border: 1px solid rgba(184,150,58,0.5);
    background: linear-gradient(180deg, rgba(26,20,16,0.95), rgba(15,10,7,0.95));
    color: var(--gilt);
    font-family: var(--sans);
    font-weight: 300;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.2em;
    padding: 0.7rem 1rem;
    border-radius: 2px;
    cursor: pointer;
    box-shadow: 0 8px 20px -6px rgba(0,0,0,0.6);
    align-items: center;
    gap: 0.5rem;
    transition: color 200ms ease, border-color 200ms ease, transform 200ms ease;
}

.reader-toggle .rt-mark { font-family: var(--serif-display); font-size: 1rem; line-height: 1; }
.reader-toggle:hover { color: var(--page); border-color: var(--gilt); }
.reader-toggle:active { transform: translateY(1px); }

/* ---------- Reading progress bar ---------- */
.progress-bar {
    position: fixed;
    top: var(--rh-h);
    left: 0; right: 0;
    height: 1px;
    z-index: 49;
    background: rgba(184,150,58,0.08);
    pointer-events: none;
}
.progress-bar > span {
    display: block;
    height: 100%;
    width: 0;
    background: linear-gradient(90deg, var(--blood), var(--gilt));
    transition: width 120ms linear;
    box-shadow: 0 0 6px rgba(184,150,58,0.6);
}

/* ---------- Selection ---------- */
::selection {
    background: var(--blood);
    color: var(--page);
}
.book ::selection {
    background: var(--blood);
    color: var(--page);
}

/* ---------- Italics, Emphasis ---------- */
.book em, .book i {
    color: var(--ink);
    font-style: italic;
}

/* ---------- Responsive ---------- */
@media (max-width: 960px) {
    :root {
        --aside-w: 240px;
    }
}

@media (max-width: 800px) {
    .volume {
        grid-template-columns: 1fr;
        gap: 1.5rem;
    }
    .chapter-index {
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        width: min(86vw, 340px);
        z-index: 70;
        transform: translateX(100%);
        transition: transform 320ms ease;
        padding: calc(var(--rh-h) + 0.75rem) 0.9rem 1rem;
        background: linear-gradient(180deg, #0f0a07, #1a1410);
        overflow-y: auto;
        box-shadow: -20px 0 40px rgba(0,0,0,0.6);
    }
    body.index-open .chapter-index {
        transform: translateX(0);
    }
    body.index-open::after {
        content: "";
        position: fixed;
        inset: 0;
        background: rgba(0,0,0,0.55);
        z-index: 65;
    }
    .reader-toggle { display: inline-flex; }
    .running-header-inner { font-size: 0.62rem; gap: 0.5rem; padding: 0 0.85rem; }
    .rh-title { display: none; }
    .book { padding: 1.5rem; }
    .fp-title { font-size: clamp(2rem, 9vw, 3rem); }
    .pullquote { margin-left: 0; }
    .pullquote p { font-size: 1.05rem; }
}

@media (max-width: 480px) {
    .book p { text-align: left; }
    .book p + p { text-indent: 1.2em; }
    .dropcap { font-size: 3.4em; }
    .ct-rule { max-width: 2.5rem; }
    .chapter-title { gap: 0.6rem; }
    .colophon-grid .cg-row { grid-template-columns: 5.5rem 1fr; }
}

/* ---------- Print (a small dignity) ---------- */
@media print {
    body { background: #fff; }
    .running-header, .progress-bar, .reader-toggle, .chapter-index, .page-texture { display: none; }
    .volume { display: block; padding: 0; }
    .book {
        box-shadow: none;
        max-width: none;
        background: #fff;
    }
}
