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

        @page:first {
            @top-center { content: none; }
        }

        body {
            font-family: Georgia, "Times New Roman", serif;
            font-size: 11pt;
            line-height: 1.7;
            color: #1a1a1a;
        }

        /* Title Page / Header */
        .title-section {
            text-align: center;
            margin-bottom: 0.4in;
            padding-bottom: 0.25in;
            border-bottom: 2px solid #333;
        }

        h1 {
            font-weight: normal;
            font-size: 32pt;
            margin: 0 0 0.08in 0;
            letter-spacing: 0.02em;
        }

        .book-title {
            font-size: 14pt;
            font-style: italic;
            color: #555;
            margin: 0 0 0.15in 0;
        }

        .chapter-info {
            font-size: 10pt;
            color: #666;
        }

        /* Book Introduction */
        .book-intro {
            margin-bottom: 0.35in;
            padding: 0.2in;
            background: #f8f8f8;
            border-left: 3px solid #4a7c59;
        }

        .book-intro p {
            margin: 0;
            font-size: 10pt;
            line-height: 1.5;
            color: #444;
        }

        .book-meta {
            margin-top: 0.1in;
            font-size: 9pt;
            color: #666;
        }

        /* Scripture Text */
        .scripture-section {
            margin-bottom: 0.3in;
        }

        .section-heading {
            font-size: 12pt;
            font-weight: 600;
            color: #333;
            margin: 0.3in 0 0.15in 0;
            padding-bottom: 0.05in;
            border-bottom: 1px solid #ddd;
            letter-spacing: 0.03em;
            text-transform: uppercase;
        }

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

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

        .verses {
            columns: 1;
        }

        .verse {
            display: block;
            margin-bottom: 0.12in;
            text-align: justify;
            text-indent: 0;
        }

        /* Poetry formatting */
        .poetry-verses .verse {
            text-indent: -0.25in;
            padding-left: 0.25in;
            margin-bottom: 0.08in;
            text-align: left;
        }

        .poetry-verses .stanza-break {
            margin-top: 0.3in;
            padding-top: 0.15in;
            border-top: 1px solid #ccc;
        }

        .poetry-verses .stanza-break-wrapper {
            margin-top: 0.3in;
            padding-top: 0.15in;
            border-top: 1px solid #ccc;
        }

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

        .verse-number {
            font-size: 8pt;
            font-weight: bold;
            color: #4a7c59;
            vertical-align: super;
            margin-right: 0.03in;
        }

        .verse-text {
            /* inline with verse number */
        }

        /* Red letter for words of Christ */
        .red-letter {
            color: #8b0000;
        }

        /* Word Study Markers */
        .word-marker {
            font-size: 7pt;
            vertical-align: super;
            color: #4a7c59;
            font-weight: bold;
        }

        /* Cross Reference Section */
        .cross-references {
            margin-top: 0.35in;
            padding-top: 0.2in;
            border-top: 1px solid #ccc;
        }

        .ref-group {
            margin-bottom: 0.15in;
        }

        .ref-verse {
            font-weight: 600;
            color: #333;
        }

        .ref-list {
            font-size: 9.5pt;
            color: #555;
            margin-left: 0.15in;
        }

        .ref-description {
            font-weight: 600;
            color: #444;
        }

        /* Glossary / Word Studies Section */
        .glossary {
            margin-top: 0.4in;
            padding-top: 0.2in;
            border-top: 2px solid #333;
            page-break-before: auto;
        }

        .glossary-grid {
            columns: 2;
            column-gap: 0.3in;
        }

        .glossary-entry {
            break-inside: avoid;
            margin-bottom: 0.2in;
            padding-bottom: 0.1in;
            border-bottom: 1px dotted #ccc;
        }

        .glossary-word {
            font-weight: 600;
            font-size: 10.5pt;
            color: #1a1a1a;
        }

        .glossary-original {
            font-size: 10pt;
            color: #4a7c59;
        }

        .glossary-translit {
            font-style: italic;
            color: #666;
        }

        .glossary-meaning {
            font-size: 9.5pt;
            color: #333;
            margin-top: 0.03in;
        }

        .glossary-note {
            font-size: 9pt;
            color: #555;
            margin-top: 0.03in;
            line-height: 1.4;
        }

        /* Footer */
        .footer {
            margin-top: 0.5in;
            padding-top: 0.15in;
            border-top: 1px solid #ddd;
            text-align: center;
            font-size: 8.5pt;
            color: #888;
        }

        .footer a {
            color: #4a7c59;
            text-decoration: none;
        }

        /* Hide sidenote checkboxes */
        .margin-toggle,
        .sidenote-number,
        input[type="checkbox"] {
            display: none !important;
        }

        .sidenote,
        .marginnote {
            display: none !important;
        }

        /* Utility */
        .small-caps {
            font-variant: small-caps;
        }
    </style>
</head>
<body>
    <!-- Title Section -->
    <div class="title-section">
        <h1>{{ book }} {{ chapter }}</h1>
        {% if book_data and book_data.full_title %}
        <p class="book-title">{{ book_data.full_title }}</p>
        {% endif %}
        <p class="chapter-info">
            Chapter {{ chapter }} of {{ total_chapters }} · {{ verse_count }} Verses · Authorized King James Version
        </p>
    </div>

    {% if book_data and book_data.description and chapter == 1 %}
    <!-- Book Introduction (only on chapter 1) -->
    <div class="book-intro">
        <p>{{ book_data.description }}</p>
        {% if book_data.author or book_data.date_written %}
        <p class="book-meta">
            {% if book_data.author %}Author: {{ book_data.author }}{% endif %}
            {% if book_data.author and book_data.date_written %} · {% endif %}
            {% if book_data.date_written %}Written: {{ book_data.date_written }}{% endif %}
        </p>
        {% endif %}
    </div>
    {% endif %}

    <!-- Scripture Text -->
    <div class="scripture-section">
        <div class="verses{% if is_poetry %} poetry-verses{% endif %}">
            {% for verse in verses %}
            {% set has_stanza_break = is_poetry and verse.verse in stanza_breaks %}
            {% set has_section_heading = section_headings and verse.verse in section_headings %}
            {% if has_stanza_break and has_section_heading %}
            <div class="stanza-break-wrapper">
            <h3 class="verse-section-heading">{{ section_headings[verse.verse] }}</h3>
            {% elif has_section_heading %}
            <h3 class="verse-section-heading">{{ section_headings[verse.verse] }}</h3>
            {% endif %}
            <p 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 | safe }}</span>
            </p>
            {% if has_stanza_break and has_section_heading %}
            </div>
            {% endif %}
            {% endfor %}
        </div>
    </div>

    {% if glossary %}
    <!-- Word Studies / Glossary -->
    <div class="glossary">
        <h2 class="section-heading">Hebrew & Greek Word Studies</h2>
        <div class="glossary-grid">
            {% for study in glossary %}
            <div class="glossary-entry">
                <div>
                    <span class="glossary-word">{{ study.word }}</span>
                    —
                    <span class="glossary-original">{{ study.term }}</span>
                    <span class="glossary-translit">({{ study.translit }})</span>
                </div>
                <div class="glossary-meaning">{{ study.meaning }}</div>
                {% if study.note %}
                <div class="glossary-note">{{ study.note | strip_links | safe }}</div>
                {% endif %}
            </div>
            {% endfor %}
        </div>
    </div>
    {% endif %}

    <!-- Cross References -->
    <div class="cross-references">
        <h2 class="section-heading">Cross References</h2>
        {% for verse in verses %}
        {% set commentary = commentaries[verse.verse] if commentaries and verse.verse in commentaries else none %}
        {% if commentary and commentary.cross_reference_groups and commentary.cross_reference_groups|length > 0 %}
        <div class="ref-group">
            <span class="ref-verse">{{ book }} {{ chapter }}:{{ verse.verse }}</span>
            <span class="ref-list">
                {% for group in commentary.cross_reference_groups %}
                <span class="ref-description">{{ group.description }}:</span>
                {% for ref in group.refs %}{{ ref }}{% if not loop.last %}; {% endif %}{% endfor %}{% if not loop.last %}. {% endif %}
                {% endfor %}
            </span>
        </div>
        {% endif %}
        {% endfor %}
    </div>

    <!-- Footer -->
    <div class="footer">
        <p>From <strong>KJV Study</strong> · kjvstudy.org</p>
        <p>The Authorized King James Version · Public Domain</p>
    </div>
</body>
</html>
