/*
 * The First Life - Book Stylesheet
 * Inspired by Tufte CSS (https://edwardtufte.github.io/tufte-css/)
 * and classic book typography principles
 */

/* ============================================
   CSS VARIABLES / DESIGN TOKENS
   ============================================ */
:root {
    /* Colors */
    --color-bg: #fffff8;
    --color-text: #111111;
    --color-text-light: #454545;
    --color-accent: #8b0000;
    --color-link: #5a0000;
    --color-link-hover: #8b0000;
    --color-border: #c0c0c0;
    --color-sidenote-bg: #f9f9f5;
    --color-code-bg: #f5f5f0;

    /* Typography */
    --font-body: 'et-book', Palatino, 'Palatino Linotype', 'Palatino LT STD',
                 'Book Antiqua', Georgia, serif;
    --font-sans: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
    --font-mono: Consolas, 'Liberation Mono', Menlo, Courier, monospace;

    /* Sizing */
    --content-width: 55%;
    --sidenote-width: 40%;
    --base-font-size: 15px;
    --base-line-height: 1.7;

    /* Spacing */
    --spacing-unit: 1.4rem;
}

/* Dark mode */
@media (prefers-color-scheme: dark) {
    :root {
        --color-bg: #1a1a1a;
        --color-text: #e8e8e8;
        --color-text-light: #b0b0b0;
        --color-accent: #d4a373;
        --color-link: #e9c46a;
        --color-link-hover: #f4a261;
        --color-border: #444444;
        --color-sidenote-bg: #242424;
        --color-code-bg: #2a2a2a;
    }
}

/* ============================================
   BASE STYLES
   ============================================ */
html {
    font-size: var(--base-font-size);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    background-color: var(--color-bg);
    color: var(--color-text);
    font-family: var(--font-body);
    line-height: var(--base-line-height);
    margin: 0;
    padding: 0;
    counter-reset: sidenote-counter;
}

/* ============================================
   LAYOUT
   ============================================ */
article {
    position: relative;
    padding: 5rem 0;
}

section {
    padding-top: 1rem;
    padding-bottom: 1rem;
}

/* Main content column */
article > p,
article > h1,
article > h2,
article > h3,
article > h4,
article > blockquote,
article > ul,
article > ol,
article > pre,
article > table,
article > figure,
article > .epigraph,
section > p,
section > h1,
section > h2,
section > h3,
section > h4,
section > blockquote,
section > ul,
section > ol,
section > pre,
section > table,
section > figure,
section > .epigraph {
    width: var(--content-width);
    margin-left: auto;
    margin-right: auto;
    padding-left: 5%;
    box-sizing: border-box;
}

/* Full-width elements */
.fullwidth {
    width: 100%;
    max-width: none;
    padding-left: 5%;
    padding-right: 5%;
}

/* ============================================
   TYPOGRAPHY
   ============================================ */
h1, h2, h3, h4, h5, h6 {
    font-weight: 400;
    line-height: 1.2;
    margin-top: calc(var(--spacing-unit) * 2);
    margin-bottom: var(--spacing-unit);
}

h1 {
    font-size: 3.2rem;
    margin-top: 0;
    font-weight: 400;
    font-style: italic;
}

h2 {
    font-size: 2.2rem;
    font-style: italic;
}

h3 {
    font-size: 1.7rem;
    font-style: italic;
}

h4 {
    font-size: 1.2rem;
    font-style: normal;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

p {
    margin-top: var(--spacing-unit);
    margin-bottom: var(--spacing-unit);
}

/* First paragraph after headings - no indent, drop cap option */
h2 + p,
h3 + p,
hr + p {
    text-indent: 0;
}

/* Subsequent paragraphs - slight indent */
p + p {
    text-indent: 1.5em;
    margin-top: 0;
}

/* ============================================
   LINKS
   ============================================ */
a {
    color: var(--color-link);
    text-decoration: none;
    background: linear-gradient(var(--color-bg), var(--color-bg)),
                linear-gradient(var(--color-bg), var(--color-bg)),
                linear-gradient(currentColor, currentColor);
    background-size: 0.05em 1px, 0.05em 1px, 1px 1px;
    background-repeat: no-repeat, no-repeat, repeat-x;
    background-position: 0% 93%, 100% 93%, 0% 93%;
}

a:hover {
    color: var(--color-link-hover);
}

/* ============================================
   BLOCKQUOTES & EPIGRAPHS
   ============================================ */
blockquote {
    margin: calc(var(--spacing-unit) * 2) 0;
    padding: 0;
    border: none;
    font-style: italic;
}

blockquote p {
    width: 100%;
    padding-left: 2em;
    border-left: 3px solid var(--color-border);
}

blockquote footer,
blockquote cite {
    display: block;
    font-size: 0.9em;
    font-style: normal;
    margin-top: 0.5em;
    padding-left: 2em;
    color: var(--color-text-light);
}

blockquote footer::before,
blockquote cite::before {
    content: '\2014\00a0';
}

/* Epigraph - chapter opening quotes */
.epigraph {
    margin: calc(var(--spacing-unit) * 3) 0;
}

.epigraph blockquote {
    margin: 0;
}

.epigraph blockquote p {
    font-style: italic;
    border-left: none;
    padding-left: 0;
}

/* ============================================
   SIDENOTES & MARGIN NOTES
   ============================================ */
.sidenote,
.marginnote {
    float: right;
    clear: right;
    margin-right: -50%;
    width: 45%;
    margin-top: 0.3rem;
    margin-bottom: 1rem;
    font-size: 0.85rem;
    line-height: 1.4;
    vertical-align: baseline;
    position: relative;
    color: var(--color-text-light);
}

.sidenote-number {
    counter-increment: sidenote-counter;
}

.sidenote-number::after,
.sidenote::before {
    font-family: var(--font-body);
    position: relative;
    vertical-align: baseline;
}

.sidenote-number::after {
    content: counter(sidenote-counter);
    font-size: 0.7rem;
    top: -0.5rem;
    left: 0.1rem;
    color: var(--color-accent);
}

.sidenote::before {
    content: counter(sidenote-counter) ' ';
    font-size: 0.7rem;
    top: -0.5rem;
    color: var(--color-accent);
}

/* Margin notes without numbers */
.marginnote > code,
.sidenote > code {
    font-size: 0.8rem;
}

/* ============================================
   LISTS
   ============================================ */
ul, ol {
    padding-left: 2em;
}

li {
    margin-bottom: 0.5em;
}

li > p {
    width: 100%;
    padding-left: 0;
}

/* ============================================
   CODE
   ============================================ */
code {
    font-family: var(--font-mono);
    font-size: 0.9em;
    background-color: var(--color-code-bg);
    padding: 0.1em 0.3em;
    border-radius: 3px;
}

pre {
    background-color: var(--color-code-bg);
    padding: 1.5em;
    overflow-x: auto;
    border-radius: 4px;
}

pre code {
    background: none;
    padding: 0;
}

/* ============================================
   TABLES
   ============================================ */
table {
    border-collapse: collapse;
    width: 100%;
    margin: calc(var(--spacing-unit) * 2) 0;
    font-size: 0.95em;
}

th, td {
    padding: 0.75em 1em;
    text-align: left;
    border-bottom: 1px solid var(--color-border);
}

th {
    font-weight: 600;
    border-bottom-width: 2px;
}

/* ============================================
   HORIZONTAL RULES
   ============================================ */
hr {
    border: none;
    border-top: 1px solid var(--color-border);
    margin: calc(var(--spacing-unit) * 3) auto;
    width: var(--content-width);
}

/* Decorative section break */
hr.section-break {
    border: none;
    text-align: center;
    margin: calc(var(--spacing-unit) * 4) auto;
}

hr.section-break::before {
    content: '* * *';
    color: var(--color-text-light);
    letter-spacing: 1em;
}

/* ============================================
   BOOK-SPECIFIC COMPONENTS
   ============================================ */

/* Title page */
.title-page {
    text-align: center;
    padding: 10vh 5%;
    min-height: 80vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.title-page h1 {
    font-size: 4rem;
    font-style: normal;
    font-weight: 400;
    margin-bottom: 0.5em;
    letter-spacing: 0.02em;
}

.title-page .subtitle {
    font-size: 1.5rem;
    font-style: italic;
    color: var(--color-text-light);
    margin-bottom: 3em;
}

.title-page .author {
    font-size: 1.3rem;
    font-variant: small-caps;
    letter-spacing: 0.1em;
}

/* Part title pages */
.part-title {
    text-align: center;
    padding: 15vh 5%;
    min-height: 60vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.part-title .part-number {
    font-size: 1rem;
    font-variant: small-caps;
    letter-spacing: 0.2em;
    color: var(--color-text-light);
    margin-bottom: 1em;
}

.part-title h2 {
    font-size: 3rem;
    font-style: normal;
    margin: 0;
}

/* Chapter headings */
.chapter-heading {
    margin-bottom: calc(var(--spacing-unit) * 3);
    width: var(--content-width);
    margin-left: auto;
    margin-right: auto;
    padding-left: 5%;
    box-sizing: border-box;
}

.chapter-heading .chapter-number {
    font-size: 0.9rem;
    font-variant: small-caps;
    letter-spacing: 0.15em;
    color: var(--color-text-light);
    display: block;
    margin-bottom: 0.5em;
}

.chapter-heading h2 {
    margin-top: 0;
    margin-bottom: 0.3em;
}

.chapter-heading .chapter-subtitle {
    font-size: 1.1rem;
    font-style: italic;
    color: var(--color-text-light);
}

/* Table of contents */
.toc {
    list-style: none;
    padding: 0;
}

.toc li {
    margin-bottom: 0.8em;
    padding-left: 0;
}

.toc a {
    display: flex;
    justify-content: space-between;
    background: none;
}

.toc .toc-part {
    font-variant: small-caps;
    letter-spacing: 0.1em;
    margin-top: 2em;
    margin-bottom: 0.5em;
    font-weight: 600;
}

.toc .toc-chapter {
    padding-left: 1.5em;
}

.toc .page-number {
    color: var(--color-text-light);
}

/* Pull quotes */
.pull-quote {
    font-size: 1.4rem;
    font-style: italic;
    color: var(--color-text-light);
    border-left: 4px solid var(--color-accent);
    padding-left: 1.5em;
    margin: calc(var(--spacing-unit) * 2) 0;
}

/* Key concepts / callouts */
.callout {
    background: var(--color-sidenote-bg);
    padding: 1.5em 2em;
    margin: calc(var(--spacing-unit) * 2) 0;
    border-radius: 4px;
}

.callout-title {
    font-weight: 600;
    margin-bottom: 0.5em;
    font-size: 0.9rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-accent);
}

/* ============================================
   NAVIGATION
   ============================================ */
nav.book-nav {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    background: var(--color-bg);
    border-bottom: 1px solid var(--color-border);
    padding: 1em 5%;
    z-index: 100;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

nav.book-nav .book-title {
    font-style: italic;
    font-size: 1rem;
}

nav.book-nav ul {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    gap: 2em;
}

nav.book-nav a {
    font-size: 0.9rem;
    background: none;
}

/* Chapter navigation */
.chapter-nav {
    display: flex;
    justify-content: space-between;
    margin-top: calc(var(--spacing-unit) * 4);
    padding-top: var(--spacing-unit);
    border-top: 1px solid var(--color-border);
    width: var(--content-width);
    margin-left: auto;
    margin-right: auto;
    padding-left: 5%;
}

.chapter-nav a {
    font-size: 0.95rem;
}

.chapter-nav .prev::before {
    content: '\2190\00a0';
}

.chapter-nav .next::after {
    content: '\00a0\2192';
}

/* ============================================
   RESPONSIVE DESIGN
   ============================================ */
@media (max-width: 1200px) {
    article > p,
    article > h1,
    article > h2,
    article > h3,
    article > h4,
    article > blockquote,
    article > ul,
    article > ol,
    article > pre,
    article > table,
    article > figure,
    article > .epigraph,
    section > p,
    section > h1,
    section > h2,
    section > h3,
    section > h4,
    section > blockquote,
    section > ul,
    section > ol,
    section > pre,
    section > table,
    section > figure,
    section > .epigraph {
        width: 75%;
    }

    .sidenote,
    .marginnote {
        display: none;
    }

    /* Show sidenotes inline on smaller screens */
    .sidenote-inline,
    .marginnote-inline {
        display: block;
        float: none;
        width: 100%;
        margin: 1em 0;
        padding: 1em;
        background: var(--color-sidenote-bg);
        border-radius: 4px;
        font-size: 0.9rem;
    }
}

@media (max-width: 768px) {
    :root {
        --base-font-size: 14px;
    }

    article > p,
    article > h1,
    article > h2,
    article > h3,
    article > h4,
    article > blockquote,
    article > ul,
    article > ol,
    article > pre,
    article > table,
    article > figure,
    article > .epigraph,
    section > p,
    section > h1,
    section > h2,
    section > h3,
    section > h4,
    section > blockquote,
    section > ul,
    section > ol,
    section > pre,
    section > table,
    section > figure,
    section > .epigraph {
        width: 90%;
        padding-left: 5%;
        padding-right: 5%;
    }

    h1 {
        font-size: 2.4rem;
    }

    h2 {
        font-size: 1.8rem;
    }

    .title-page h1 {
        font-size: 2.5rem;
    }

    .chapter-nav {
        width: 90%;
        flex-direction: column;
        gap: 1em;
        text-align: center;
    }

    nav.book-nav {
        flex-direction: column;
        gap: 0.5em;
    }
}

/* ============================================
   PRINT STYLES
   ============================================ */
@media print {
    nav.book-nav,
    .chapter-nav {
        display: none;
    }

    article {
        padding: 0;
    }

    h1, h2, h3 {
        page-break-after: avoid;
    }

    p, blockquote {
        orphans: 3;
        widows: 3;
    }

    .title-page,
    .part-title {
        page-break-after: always;
    }
}

/* ============================================
   UTILITIES
   ============================================ */
.sans {
    font-family: var(--font-sans);
}

.small-caps {
    font-variant: small-caps;
    letter-spacing: 0.05em;
}

.text-center {
    text-align: center;
}

.italic {
    font-style: italic;
}

.newthought {
    font-variant: small-caps;
    font-size: 1.1em;
    letter-spacing: 0.05em;
}

/* Reading progress indicator */
.progress-bar {
    position: fixed;
    top: 0;
    left: 0;
    width: 0%;
    height: 3px;
    background: var(--color-accent);
    z-index: 200;
    transition: width 0.1s ease-out;
}

/* ============================================
   READER CONTROLS
   ============================================ */
.reader-controls {
    position: fixed;
    top: 0;
    right: 0;
    display: flex;
    gap: 0.5rem;
    padding: 0.8rem 1rem;
    z-index: 150;
    background: var(--color-bg);
}

.reader-controls button {
    background: var(--color-sidenote-bg);
    border: 1px solid var(--color-border);
    color: var(--color-text);
    padding: 0.4rem 0.8rem;
    font-family: var(--font-sans);
    font-size: 0.85rem;
    cursor: pointer;
    border-radius: 4px;
    transition: background 0.2s, border-color 0.2s;
}

.reader-controls button:hover {
    background: var(--color-border);
}

.reader-controls button.active {
    background: var(--color-accent);
    color: var(--color-bg);
    border-color: var(--color-accent);
}

.font-controls {
    display: flex;
    gap: 0.25rem;
}

.font-controls button {
    min-width: 2.2rem;
    font-weight: bold;
}

/* ============================================
   SLIDE-OUT TABLE OF CONTENTS
   ============================================ */
.toc-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 320px;
    max-width: 85vw;
    height: 100vh;
    background: var(--color-bg);
    border-right: 1px solid var(--color-border);
    z-index: 300;
    transform: translateX(-100%);
    transition: transform 0.3s ease-in-out;
    overflow-y: auto;
    padding: 2rem 1.5rem;
    box-sizing: border-box;
}

.toc-sidebar.open {
    transform: translateX(0);
}

.toc-sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--color-border);
}

.toc-sidebar-header h3 {
    margin: 0;
    font-size: 1.2rem;
    font-style: italic;
}

.toc-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--color-text-light);
    padding: 0;
    line-height: 1;
}

.toc-close:hover {
    color: var(--color-text);
}

.toc-sidebar .toc {
    padding: 0;
}

.toc-sidebar .toc li {
    margin-bottom: 0.6em;
}

.toc-sidebar .toc .toc-part {
    margin-top: 1.5em;
    font-size: 0.9rem;
}

.toc-sidebar .toc .toc-chapter {
    font-size: 0.95rem;
}

.toc-sidebar .toc .toc-chapter a {
    display: block;
    padding: 0.3em 0;
}

.toc-sidebar .toc .toc-chapter a:hover {
    color: var(--color-accent);
}

/* Overlay when TOC is open */
.toc-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.4);
    z-index: 250;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s, visibility 0.3s;
}

.toc-overlay.open {
    opacity: 1;
    visibility: visible;
}

/* ============================================
   FONT SIZE VARIATIONS
   ============================================ */
html.font-size-small {
    --base-font-size: 13px;
}

html.font-size-medium {
    --base-font-size: 16px;
}

html.font-size-large {
    --base-font-size: 19px;
}

html.font-size-xlarge {
    --base-font-size: 22px;
}

/* Adjust nav position when controls are visible */
nav.book-nav {
    padding-right: 200px;
}

@media (max-width: 768px) {
    .reader-controls {
        padding: 0.5rem;
        gap: 0.3rem;
    }

    .reader-controls button {
        padding: 0.3rem 0.5rem;
        font-size: 0.8rem;
    }

    .font-controls button {
        min-width: 1.8rem;
    }

    nav.book-nav {
        padding-right: 5%;
    }

    .toc-sidebar {
        width: 280px;
    }
}
