<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ book }} (KJV)</title>
    <style>
        @page {
            size: letter;
            margin: 0.75in;
            @bottom-center {
                content: counter(page);
                font-family: "et-book", Georgia, serif;
                font-size: 10pt;
                color: #666;
            }
        }

        body {
            font-family: "et-book", Georgia, "Times New Roman", serif;
            font-size: 11pt;
            line-height: 1.55;
            color: #111;
        }

        h1 {
            font-weight: normal;
            letter-spacing: 0.05em;
            font-size: 26pt;
            margin: 0 0 0.15in 0;
        }

        .subtitle {
            font-style: italic;
            color: #666;
            margin-bottom: 0.25in;
        }

        .book-meta {
            font-size: 10pt;
            color: #666;
            margin-bottom: 0.35in;
            border-bottom: 1px solid #ddd;
            padding-bottom: 0.15in;
        }

        .chapter-block {
            page-break-inside: avoid;
            margin-bottom: 0.35in;
            padding-bottom: 0.2in;
            border-bottom: 1px solid #eee;
        }

        .chapter-block:last-of-type {
            border-bottom: none;
        }

        .chapter-title {
            font-size: 16pt;
            margin: 0 0 0.15in 0;
        }

        .verse {
            display: flex;
            gap: 0.15in;
            margin-bottom: 0.06in;
        }

        .verse-number {
            font-variant-numeric: lining-nums;
            font-size: 10pt;
            color: #777;
            min-width: 0.35in;
            text-align: right;
        }

        .verse-text {
            flex: 1;
            text-align: justify;
        }

        .verse-section-heading {
            font-size: 11pt;
            font-weight: 600;
            font-style: italic;
            color: #444;
            margin: 0.2in 0 0.08in 0;
            padding-top: 0.08in;
            border-top: 1px dotted #ccc;
        }

        .verse-section-heading:first-child {
            margin-top: 0;
            padding-top: 0;
            border-top: none;
        }

        /* Poetry formatting */
        .poetry-chapter .verse {
            display: block;
            margin-bottom: 0.06in;
        }

        .poetry-chapter .stanza-break {
            margin-top: 0.25in;
            padding-top: 0.12in;
            border-top: 1px solid #ccc;
        }

        .poetry-chapter .stanza-break-wrapper {
            margin-top: 0.25in;
            padding-top: 0.12in;
            border-top: 1px solid #ccc;
        }

        .poetry-chapter .stanza-break-wrapper .verse-section-heading {
            margin-top: 0;
            padding-top: 0;
            border-top: none;
        }

        .footer {
            margin-top: 0.4in;
            font-size: 9pt;
            color: #888;
            text-align: center;
        }

        /* Introduction sections */
        .intro-section {
            margin-bottom: 0.3in;
            page-break-inside: avoid;
        }

        .intro-section h2 {
            font-size: 14pt;
            margin: 0.2in 0 0.1in 0;
            font-weight: normal;
        }

        .intro-section p {
            margin: 0 0 0.12in 0;
            text-align: justify;
        }

        .intro-section ul {
            margin: 0.1in 0;
            padding-left: 0.25in;
        }

        .intro-section li {
            margin-bottom: 0.08in;
        }

        .intro-section blockquote {
            margin: 0.15in 0.2in;
            padding-left: 0.15in;
            border-left: 2px solid #ddd;
        }

        .intro-section blockquote p {
            font-style: italic;
            margin-bottom: 0.05in;
        }

        .intro-section blockquote footer {
            font-size: 9pt;
            color: #666;
        }

        .chapters-divider {
            margin: 0.4in 0;
            border-bottom: 2px solid #333;
            page-break-before: always;
        }

        /* Style sidenotes as inline notes for PDF */
        .margin-toggle,
        .sidenote-number,
        input[type="checkbox"] {
            display: none !important;
        }

        .sidenote,
        .marginnote {
            display: block;
            float: none;
            width: 100%;
            margin: 0.15in 0;
            padding: 0.1in 0.15in;
            font-size: 9pt;
            font-style: italic;
            color: #555;
            background: #f9f9f9;
            border-left: 2px solid #ccc;
        }

        .sidenote::before {
            content: "Note: ";
            font-weight: 600;
            font-style: normal;
        }
    </style>
</head>
<body>
    <h1>{{ book }}</h1>
    <p class="subtitle">Authorized King James Version (KJV)</p>
    <p class="book-meta">
        {% if book_intro and book_intro.author %}Author: {{ book_intro.author }}{% endif %}
        {% if book_intro and book_intro.date_written %} &middot; Written: {{ book_intro.date_written }}{% endif %}
        {% if book_intro and book_intro.category %} &middot; Category: {{ book_intro.category }}{% endif %}
    </p>

    {% if book_intro %}
    <!-- Introduction Sections -->
    {% if book_intro.introduction %}
    <section class="intro-section">
        <h2>Introduction</h2>
        {{ book_intro.introduction|md|safe }}
    </section>
    {% endif %}

    {% if book_intro.outline %}
    <section class="intro-section">
        <h2>Book Outline</h2>
        <ul>
            {% for item in book_intro.outline %}
            <li><strong>{{ item.section }}</strong> ({{ item.chapters }}) — {{ item.description|mdi|safe }}</li>
            {% endfor %}
        </ul>
    </section>
    {% endif %}

    {% if book_intro.key_themes %}
    <section class="intro-section">
        <h2>Key Themes</h2>
        <ul>
            {% for theme in book_intro.key_themes %}
            {% if theme is mapping %}
            <li><strong>{{ theme.theme }}</strong>: {{ theme.description|mdi|safe }}</li>
            {% else %}
            <li>{{ theme }}</li>
            {% endif %}
            {% endfor %}
        </ul>
    </section>
    {% endif %}

    {% if book_intro.key_verses %}
    <section class="intro-section">
        <h2>Key Verses</h2>
        {% for verse in book_intro.key_verses %}
        <blockquote>
            <p>{{ verse.text|mdi|safe }}</p>
            <footer>— {{ verse.reference }}{% if verse.significance %} ({{ verse.significance|mdi|safe }}){% endif %}</footer>
        </blockquote>
        {% endfor %}
    </section>
    {% endif %}

    {% if book_intro.historical_context %}
    <section class="intro-section">
        <h2>Historical Context</h2>
        {{ book_intro.historical_context|md|safe }}
    </section>
    {% endif %}

    {% if book_intro.literary_style %}
    <section class="intro-section">
        <h2>Literary Style</h2>
        {{ book_intro.literary_style|md|safe }}
    </section>
    {% endif %}

    {% if book_intro.theological_significance %}
    <section class="intro-section">
        <h2>Theological Significance</h2>
        {{ book_intro.theological_significance|md|safe }}
    </section>
    {% endif %}

    {% if book_intro.christ_in_book %}
    <section class="intro-section">
        <h2>Christ in {{ book }}</h2>
        {{ book_intro.christ_in_book|md|safe }}
    </section>
    {% endif %}

    {% if book_intro.relationship_to_new_testament %}
    <section class="intro-section">
        <h2>Relationship to the New Testament</h2>
        {{ book_intro.relationship_to_new_testament|md|safe }}
    </section>
    {% endif %}

    {% if book_intro.practical_application %}
    <section class="intro-section">
        <h2>Practical Application</h2>
        {{ book_intro.practical_application|md|safe }}
    </section>
    {% endif %}

    <!-- Divider before chapters -->
    <div class="chapters-divider"></div>
    {% endif %}

    {% for chapter in chapters %}
    <section class="chapter-block{% if chapter.is_poetry %} poetry-chapter{% endif %}">
        <h2 class="chapter-title">Chapter {{ chapter.chapter }}</h2>
        {% for verse in chapter.verses %}
        {% set has_stanza_break = chapter.is_poetry and verse.verse in chapter.stanza_breaks %}
        {% set has_section_heading = chapter.section_headings and verse.verse in chapter.section_headings %}
        {% if has_stanza_break and has_section_heading %}
        <div class="stanza-break-wrapper">
        <h3 class="verse-section-heading">{{ chapter.section_headings[verse.verse] }}</h3>
        {% elif has_section_heading %}
        <h3 class="verse-section-heading">{{ chapter.section_headings[verse.verse] }}</h3>
        {% endif %}
        <div class="verse{% if has_stanza_break and not has_section_heading %} stanza-break{% endif %}">
            <span class="verse-number">{{ verse.verse }}</span>
            <span class="verse-text">{{ verse.text }}</span>
        </div>
        {% if has_stanza_break and has_section_heading %}
        </div>
        {% endif %}
        {% endfor %}
    </section>
    {% endfor %}

    <div class="footer">
        From KJV Study &bull; kjvstudy.org
    </div>
</body>
</html>
