{% extends "base.html" %}

{% block title %}The King James Bible{% endblock %}

{% block description %}Study the KJV (King James Version) Bible with comprehensive resources including verse search, topical index, study guides, reading plans, interlinear text, and theological tools.{% endblock %}

{% block head %}
<style>
/* Title Page - Classical Book Style */
.title-page {
    text-align: center;
    padding: 3rem 0 2rem;
    margin-bottom: 2rem;
    margin-left: -150px;
}

.title-page h1 {
    font-size: 3rem;
    margin-bottom: 0.5rem;
    letter-spacing: 0.08em;
    font-weight: 400;
    text-transform: uppercase;
}

.title-page .subtitle {
    font-size: 1.1rem;
    color: var(--text-secondary);
    margin: 0 0 2rem 0;
    line-height: 1.6;
    letter-spacing: 0.05em;
}

.title-page .divider {
    width: 120px;
    height: 1px;
    background: var(--border-color-darker);
    margin: 0 auto 2rem;
}

.title-page .epigraph {
    max-width: 36rem;
    margin: 0 auto;
    font-size: 1.15rem;
    line-height: 1.8;
    font-style: italic;
    color: var(--text-color);
}

.title-page .epigraph p {
    margin: 0;
}

.title-page .epigraph footer {
    margin-top: 0.75rem;
    font-size: 0.95rem;
    font-style: normal;
}

.title-page .epigraph footer a {
    color: var(--text-secondary);
}

/* Search Section */
.search-section {
    max-width: 38rem;
    margin: 0 auto 3rem;
    margin-left: calc(50% - 19rem - 75px);
}

.search-form {
    display: flex;
    gap: 0;
    border: 1px solid var(--border-color-darker);
}

.search-input {
    flex: 1;
    padding: 1rem 1.25rem;
    font-size: 1.05rem;
    border: none;
    background: var(--bg-color);
    color: var(--text-color);
    font-family: inherit;
}

.search-input:focus {
    outline: none;
}

.search-input::placeholder {
    color: var(--text-tertiary);
}

.search-btn {
    padding: 1rem 2rem;
    font-size: 1rem;
    background: var(--text-color);
    color: var(--bg-color);
    border: none;
    cursor: pointer;
    font-family: inherit;
    letter-spacing: 0.05em;
}

.search-btn:hover {
    opacity: 0.85;
}

.search-help {
    font-size: 0.85rem;
    color: var(--text-tertiary);
    margin-top: 0.75rem;
    text-align: center;
}

/* Quick Links */
.quick-links {
    text-align: center;
    margin: 2rem 0 3rem;
    margin-left: -150px;
    font-size: 1rem;
}

.quick-links a {
    display: inline-block;
    margin: 0.25rem 0.75rem;
    padding: 0.5rem 1rem;
    border: 1px solid var(--border-color);
    color: var(--text-color);
    text-decoration: none;
    transition: all 0.2s;
}

.quick-links a:hover {
    border-color: var(--text-color);
    background: var(--code-bg);
}

.quick-links a.primary {
    background: #4a7c59;
    color: #fff !important;
    border-color: #4a7c59;
}

.quick-links a.primary:hover {
    background: #3d6a4b;
    border-color: #3d6a4b;
}

/* Section Styling */
section {
    margin-bottom: 3rem;
}

section h2 {
    font-size: 1.4rem;
    font-weight: 400;
    letter-spacing: 0.02em;
    margin-bottom: 1.25rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-color);
}

/* Resource Grid */
.resource-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 1.5rem;
    margin: 1.5rem 0;
}

.resource-card {
    display: block;
    padding: 1.5rem;
    border: 1px solid var(--border-color);
    text-decoration: none;
    color: var(--text-color);
    transition: all 0.2s;
}

.resource-card:hover {
    border-color: var(--border-color-darker);
    background: var(--code-bg);
}

.resource-card h3 {
    font-size: 1.1rem;
    font-weight: 600;
    margin: 0 0 0.5rem 0;
}

.resource-card p {
    font-size: 0.95rem;
    color: var(--text-secondary);
    margin: 0;
    line-height: 1.6;
}

/* Topic Links */
.topic-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.75rem;
    margin: 1.5rem 0;
}

.topic-link {
    display: block;
    padding: 0.75rem 1rem;
    text-align: center;
    text-decoration: none;
    color: var(--text-color);
    border: 1px solid var(--border-color);
    font-size: 0.9rem;
    transition: all 0.2s;
}

.topic-link:hover {
    border-color: var(--border-color-darker);
    background: var(--code-bg);
}

/* Interlinear Sample */
.interlinear-samples {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    margin: 1.5rem 0;
}

.interlinear-sample {
    padding: 0;
}

.sample-reference {
    font-size: 0.9rem;
    color: var(--text-secondary);
    margin-bottom: 1rem;
    font-style: italic;
}

.sample-words {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.sample-word {
    text-align: center;
    padding: 0.5rem 0.75rem;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 4px;
}

.sample-original {
    display: block;
    font-size: 2.2rem;
    color: var(--text-color);
    margin-bottom: 0.35rem;
}

.sample-original.greek {
    font-family: "SBL Greek", "Gentium Plus", "Times New Roman", serif;
}

.sample-original.hebrew {
    font-family: "SBL Hebrew", "Ezra SIL", "Times New Roman", serif;
    direction: rtl;
}

.sample-english {
    display: block;
    font-size: 0.85rem;
    color: #4a7c59;
    font-weight: 600;
}

.sample-strongs {
    display: block;
    font-size: 0.7rem;
    color: var(--text-tertiary);
    margin-top: 0.15rem;
}

.sample-link {
    font-size: 0.9rem;
}

/* Interactive Resources */
.interactive-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.75rem;
}

/* Testament Columns */
.testament-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 3rem;
    margin: 1.5rem 0;
}

.testament h3 {
    font-size: 1.1rem;
    font-weight: 600;
    margin: 0 0 1rem 0;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-color);
}

.testament p {
    font-size: 0.95rem;
    line-height: 1.7;
    margin: 0 0 1rem 0;
    color: var(--text-secondary);
}

.testament a.view-all {
    font-size: 0.9rem;
    color: var(--text-color);
}

/* Footer */
.home-footer {
    margin-top: 3rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--border-color);
    text-align: center;
}

.home-footer p {
    font-size: 0.85rem;
    color: var(--text-tertiary);
}

.home-footer a {
    color: var(--text-tertiary);
}

/* Selection states */
.selected {
    outline: 2px solid #4a7c59 !important;
    outline-offset: 2px;
    background: rgba(74, 124, 89, 0.05) !important;
}

[data-theme="dark"] .selected {
    outline-color: #6b9b7a !important;
    background: rgba(107, 155, 122, 0.1) !important;
}

/* Mobile */
@media (max-width: 760px) {
    .title-page {
        padding: 2rem 0 1.5rem;
        margin-left: 0;
    }

    .title-page h1 {
        font-size: 2.4rem;
        letter-spacing: 0.04em;
    }

    .title-page .subtitle {
        font-size: 1.15rem;
    }

    .title-page .epigraph {
        font-size: 1.25rem;
        padding: 0 0.5rem;
        line-height: 1.9;
    }

    .title-page .epigraph footer {
        font-size: 1.1rem;
    }

    .search-section {
        max-width: 100%;
        margin-left: auto;
        margin-right: auto;
        padding: 0;
    }

    .search-form {
        flex-direction: column;
    }

    .search-input {
        padding: 1.1rem 1rem;
        font-size: 1.2rem;
    }

    .search-btn {
        width: 100%;
        padding: 1.1rem 2rem;
        font-size: 1.2rem;
        min-height: 50px;
    }

    .search-help {
        font-size: 1rem;
    }

    .quick-links {
        margin-left: 0;
        padding: 0;
    }

    .quick-links a {
        margin: 0.35rem;
        padding: 0.7rem 1rem;
        font-size: 1.1rem;
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .interlinear-samples {
        grid-template-columns: 1fr;
    }

    .resource-grid {
        grid-template-columns: 1fr;
    }

    .resource-card {
        padding: 1.25rem;
        min-height: 44px;
    }

    .resource-card h3 {
        font-size: 1.3rem;
    }

    .resource-card p {
        font-size: 1.15rem;
        line-height: 1.7;
    }

    .topic-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.5rem;
    }

    .topic-link {
        padding: 0.85rem 0.75rem;
        font-size: 1.05rem;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        text-align: center;
    }

    .interactive-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.5rem;
    }

    .interactive-grid a {
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.05rem;
    }

    .testament-grid {
        grid-template-columns: 1fr;
        gap: 2rem;
    }

    section h2 {
        font-size: 1.6rem;
    }

    section p {
        font-size: 1.25rem;
        line-height: 1.9;
    }

    .sample-word {
        padding: 0.4rem 0.5rem;
    }

    .sample-original {
        font-size: 1.8rem;
    }

    .sample-english {
        font-size: 0.9rem;
    }

    .sample-strongs {
        font-size: 0.8rem;
    }

    .sample-link {
        font-size: 1rem;
    }

    .home-footer {
        padding-top: 1rem;
    }

    .home-footer p {
        font-size: 1.05rem;
        line-height: 1.8;
    }

    .home-footer a {
        padding: 0.25rem;
    }
}
</style>
{% endblock %}

{% block content %}
<div class="title-page">
    <h1>The Holy Bible</h1>
    <p class="subtitle">Authorized King James Version · <em>Anno Domini</em> 1611</p>
    <div class="divider"></div>
    <div class="epigraph">
        <p>"{{ daily_verse.text }}"</p>
        <footer><a href="/verse-of-the-day">Verse of the Day</a> — <a href="/book/{{ daily_verse.book }}/chapter/{{ daily_verse.chapter }}#verse-{{ daily_verse.verse }}">{{ daily_verse.reference }}</a></footer>
    </div>
</div>

<div class="search-section" id="search-section">
    <form class="search-form" onsubmit="return handleSearch(event)">
        <input type="text" class="search-input" id="verse-lookup-input"
               placeholder="John 3:16, Romans 8, or search any word..." autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />
        <button type="submit" class="search-btn">Go</button>
    </form>
    <div class="search-help">Navigate to any verse or search the Scriptures</div>
</div>

<div class="quick-links">
    <a href="/books" class="primary">Browse All 66 Books</a>
    <a href="/random-verse">Random Verse</a>
    <a href="/reading-plans">Reading Plans</a>
    <a href="/topics">Topics</a>
    <a href="/resources">Resources</a>
</div>

<section>
    <p><span class="newthought">Welcome to KJV Study</span>, a digital resource for studying the Authorized Version of the Holy Scriptures.<label for="sn-welcome" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-welcome" class="margin-toggle"/><span class="sidenote">This digital edition presents the 1769 Oxford Standard text, enriched with apparatus drawn from judicious commentators and modern scholarship.</span> This site provides the complete text of the King James Bible enriched with original language analysis, cross-references, historical context, and theological commentary—instruments designed to help both the careful scholar and the earnest student understand God's Word more deeply.</p>

    <p><span class="newthought">The King James Bible</span> is not merely a translation—it is the wellspring of English itself.<label for="sn-wellspring" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-wellspring" class="margin-toggle"/><span class="sidenote">When Lincoln spoke of 'a house divided,' when King dreamed from the mountaintop, when the weary whisper 'thy will, not mine'—they drew from this well.</span> For four centuries, its cadences have shaped how we speak of love and death, justice and mercy, the weight of truth and the gravity of oath.</p>
</section>

<section>
    <h2>Original Language Study</h2>

    <p><span class="newthought">Study Scripture</span> word-by-word in Hebrew and Greek with our <a href="/interlinear">interlinear Bible</a> and <a href="/strongs">Strong's Concordance</a>. Each word links to its original text, transliteration, and lexical definition.</p>

    <div class="interlinear-samples">
        <div class="interlinear-sample">
            <div class="sample-reference">Genesis 1:1 (Hebrew)</div>
            <div class="sample-words">
                <div class="sample-word">
                    <span class="sample-original hebrew">בְּרֵאשִׁית</span>
                    <span class="sample-english">In the beginning</span>
                    <span class="sample-strongs">H7225</span>
                </div>
                <div class="sample-word">
                    <span class="sample-original hebrew">בָּרָא</span>
                    <span class="sample-english">created</span>
                    <span class="sample-strongs">H1254</span>
                </div>
                <div class="sample-word">
                    <span class="sample-original hebrew">אֱלֹהִים</span>
                    <span class="sample-english">God</span>
                    <span class="sample-strongs">H430</span>
                </div>
                <div class="sample-word">
                    <span class="sample-original hebrew">הַשָּׁמַיִם</span>
                    <span class="sample-english">the heavens</span>
                    <span class="sample-strongs">H8064</span>
                </div>
                <div class="sample-word">
                    <span class="sample-original hebrew">הָאָרֶץ</span>
                    <span class="sample-english">the earth</span>
                    <span class="sample-strongs">H776</span>
                </div>
            </div>
            <a href="/book/Genesis/chapter/1/verse/1" class="sample-link">View full analysis →</a>
        </div>

        <div class="interlinear-sample">
            <div class="sample-reference">John 1:1 (Greek)</div>
            <div class="sample-words">
                <div class="sample-word">
                    <span class="sample-original greek">Ἐν</span>
                    <span class="sample-english">In</span>
                    <span class="sample-strongs">G1722</span>
                </div>
                <div class="sample-word">
                    <span class="sample-original greek">ἀρχῇ</span>
                    <span class="sample-english">the beginning</span>
                    <span class="sample-strongs">G746</span>
                </div>
                <div class="sample-word">
                    <span class="sample-original greek">ἦν</span>
                    <span class="sample-english">was</span>
                    <span class="sample-strongs">G1510</span>
                </div>
                <div class="sample-word">
                    <span class="sample-original greek">ὁ</span>
                    <span class="sample-english">the</span>
                    <span class="sample-strongs">G3588</span>
                </div>
                <div class="sample-word">
                    <span class="sample-original greek">λόγος</span>
                    <span class="sample-english">Word</span>
                    <span class="sample-strongs">G3056</span>
                </div>
            </div>
            <a href="/book/John/chapter/1/verse/1" class="sample-link">View full analysis →</a>
        </div>
    </div>
</section>

<section>
    <h2>Theological Commentary</h2>

    <p><span class="newthought">Over 20,000 verses</span> include scholarly commentary exploring historical context, theological significance, and practical application.<label for="sn-commentary" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-commentary" class="margin-toggle"/><span class="sidenote">Commentary draws from the Church Fathers, classical expositors, and contemporary biblical scholarship—always in service of the text itself.</span> Cross-references illuminate connections across Scripture, while study notes clarify difficult passages and unfamiliar customs of the ancient world.</p>

    <p>
        <a href="/book/Genesis/chapter/1/verse/1#commentary">Genesis 1:1</a> ·
        <a href="/book/John/chapter/1/verse/1#commentary">John 1:1</a> ·
        <a href="/book/Romans/chapter/8/verse/28#commentary">Romans 8:28</a> ·
        <a href="/book/1%20John/chapter/4/verse/4#commentary">1 John 4:4</a>
    </p>
</section>

<section>
    <h2>Study Resources</h2>

    <div class="resource-grid">
        <a class="resource-card" href="/interlinear">
            <h3>Original Languages</h3>
            <p>Word-by-word Hebrew and Greek analysis with Strong's numbers, transliterations, and lexical definitions.</p>
        </a>
        <a class="resource-card" href="/strongs">
            <h3>Strong's Concordance</h3>
            <p>Search 14,000+ Hebrew and Greek word definitions with Strong's numbers and KJV translations.</p>
        </a>
        <a class="resource-card" href="/topics">
            <h3>Topical Index</h3>
            <p>Scripture organized by subject—Salvation, Grace, Faith, Prayer, and other essential doctrines.</p>
        </a>
        <a class="resource-card" href="/study-guides">
            <h3>Study Guides</h3>
            <p>Thematic studies exploring salvation, Christian living, prayer, and foundational doctrines.</p>
        </a>
        <a class="resource-card" href="/stories">
            <h3>Bible Stories</h3>
            <p>Scripture narratives from Creation to the early Church, with versions for adults and families.</p>
        </a>
        <a class="resource-card" href="/reading-plans">
            <h3>Reading Plans</h3>
            <p>Structured schedules for systematic study: chronological, one-year, Gospels, Psalms & Proverbs.</p>
        </a>
    </div>
</section>

<section>
    <h2>The Sacred Scriptures</h2>

    <p><span class="newthought">The Authorized Version</span>, commissioned by King James I in 1604 and completed in 1611,<label for="sn-translation" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-translation" class="margin-toggle"/><span class="sidenote">The translators worked in six companies at Westminster, Oxford, and Cambridge, consulting the <em>Textus Receptus</em> (Greek) and <em>Masoretic Text</em> (Hebrew).</span> represents the work of forty-seven learned divines and scholars. This translation has shaped English-speaking Christianity for more than four centuries, achieving a remarkable union of scholarly precision with nobility of expression. This digital edition presents the 1769 Oxford Standard text.</p>
</section>

<section>
    <h2>Interactive Resources</h2>

    <div class="interactive-grid">
        <a href="/family-tree" class="topic-link">Genealogies & Family Trees</a>
        <a href="/biblical-timeline" class="topic-link">Biblical Timeline</a>
        <a href="/biblical-maps" class="topic-link">Maps</a>
        <a href="/stories" class="topic-link">Bible Stories</a>
    </div>
</section>

<section>
    <h2>Theological Studies</h2>

    <div class="topic-grid">
        <a href="/trinity" class="topic-link">The Trinity</a>
        <a href="/christology" class="topic-link">Christology</a>
        <a href="/pneumatology" class="topic-link">Pneumatology</a>
        <a href="/soteriology" class="topic-link">Soteriology</a>
        <a href="/ecclesiology" class="topic-link">Ecclesiology</a>
        <a href="/eschatology" class="topic-link">Eschatology</a>
        <a href="/kingdom-of-god" class="topic-link">The Kingdom of God</a>
        <a href="/types-and-shadows" class="topic-link">Types and Shadows</a>
        <a href="/messianic-prophecies" class="topic-link">Messianic Prophecies</a>
        <a href="/blood-in-scripture" class="topic-link">The Blood in Scripture</a>
        <a href="/names-of-christ" class="topic-link">Names of Christ</a>
        <a href="/spirits-and-demons" class="topic-link">Spirits & Demons</a>
        <a href="/personifications" class="topic-link">Personifications</a>
        <a href="/names-of-god" class="topic-link">Names of God</a>
        <a href="/tetragrammaton" class="topic-link">The Tetragrammaton</a>
        <a href="/i-am-statements" class="topic-link">I Am Statements</a>
        <a href="/biblical-covenants" class="topic-link">Biblical Covenants</a>
        <a href="/ten-commandments" class="topic-link">Ten Commandments</a>
        <a href="/beatitudes" class="topic-link">The Beatitudes</a>
        <a href="/parables" class="topic-link">Parables of Jesus</a>
        <a href="/miracles-of-jesus" class="topic-link">Miracles of Jesus</a>
        <a href="/prayers-of-the-bible" class="topic-link">Prayers of the Bible</a>
        <a href="/armor-of-god" class="topic-link">Armor of God</a>
        <a href="/fruits-of-the-spirit" class="topic-link">Fruits of the Spirit</a>
        <a href="/biblical-festivals" class="topic-link">Biblical Festivals</a>
        <a href="/the-twelve-apostles" class="topic-link">The Twelve Apostles</a>
        <a href="/biblical-prophets" class="topic-link">The Prophets</a>
        <a href="/women-of-the-bible" class="topic-link">Women of the Bible</a>
        <a href="/biblical-angels" class="topic-link">Angelology</a>
    </div>
</section>

<section>
    <h2>Accessibility</h2>

    <p><span class="newthought">Full keyboard navigation</span> is available throughout the site. Use <kbd>j</kbd>/<kbd>k</kbd> or arrow keys to navigate, <kbd>Enter</kbd> to select, and <kbd>/</kbd> to focus search. Text-to-speech is supported on verse pages for listening to Scripture read aloud.</p>

    <p><a href="/about/accessibility">Learn more →</a></p>
</section>

<div class="home-footer">
    <p>
        <a href="/about">About KJV Study</a> ·
        <a href="/about/accessibility">Accessibility</a> ·
        <a href="/api/docs">API</a> ·
        <a href="https://github.com/kennethreitz/kjvstudy.org" target="_blank" rel="noopener">Source Code</a>
    </p>
</div>

<script>
var bookMap = {
    'genesis': 'Genesis', 'exodus': 'Exodus', 'leviticus': 'Leviticus', 'numbers': 'Numbers',
    'deuteronomy': 'Deuteronomy', 'joshua': 'Joshua', 'judges': 'Judges', 'ruth': 'Ruth',
    '1 samuel': '1 Samuel', '2 samuel': '2 Samuel', '1 kings': '1 Kings', '2 kings': '2 Kings',
    '1 chronicles': '1 Chronicles', '2 chronicles': '2 Chronicles', 'ezra': 'Ezra', 'nehemiah': 'Nehemiah',
    'esther': 'Esther', 'job': 'Job', 'psalms': 'Psalms', 'psalm': 'Psalms', 'proverbs': 'Proverbs',
    'ecclesiastes': 'Ecclesiastes', 'song of solomon': 'Song of Solomon', 'isaiah': 'Isaiah',
    'jeremiah': 'Jeremiah', 'lamentations': 'Lamentations', 'ezekiel': 'Ezekiel', 'daniel': 'Daniel',
    'hosea': 'Hosea', 'joel': 'Joel', 'amos': 'Amos', 'obadiah': 'Obadiah', 'jonah': 'Jonah',
    'micah': 'Micah', 'nahum': 'Nahum', 'habakkuk': 'Habakkuk', 'zephaniah': 'Zephaniah',
    'haggai': 'Haggai', 'zechariah': 'Zechariah', 'malachi': 'Malachi', 'matthew': 'Matthew',
    'mark': 'Mark', 'luke': 'Luke', 'john': 'John', 'acts': 'Acts', 'romans': 'Romans',
    '1 corinthians': '1 Corinthians', '2 corinthians': '2 Corinthians', 'galatians': 'Galatians',
    'ephesians': 'Ephesians', 'philippians': 'Philippians', 'colossians': 'Colossians',
    '1 thessalonians': '1 Thessalonians', '2 thessalonians': '2 Thessalonians',
    '1 timothy': '1 Timothy', '2 timothy': '2 Timothy', 'titus': 'Titus', 'philemon': 'Philemon',
    'hebrews': 'Hebrews', 'james': 'James', '1 peter': '1 Peter', '2 peter': '2 Peter',
    '1 john': '1 John', '2 john': '2 John', '3 john': '3 John', 'jude': 'Jude', 'revelation': 'Revelation',
    'gen': 'Genesis', 'ge': 'Genesis', 'exo': 'Exodus', 'ex': 'Exodus', 'lev': 'Leviticus',
    'le': 'Leviticus', 'num': 'Numbers', 'nu': 'Numbers', 'deut': 'Deuteronomy', 'dt': 'Deuteronomy',
    'josh': 'Joshua', 'jos': 'Joshua', 'judg': 'Judges', 'jdg': 'Judges', 'ru': 'Ruth',
    '1 sam': '1 Samuel', '1sam': '1 Samuel', '1s': '1 Samuel', '2 sam': '2 Samuel', '2sam': '2 Samuel', '2s': '2 Samuel',
    '1 ki': '1 Kings', '1ki': '1 Kings', '1k': '1 Kings', '2 ki': '2 Kings', '2ki': '2 Kings', '2k': '2 Kings',
    '1 chr': '1 Chronicles', '1chr': '1 Chronicles', '1ch': '1 Chronicles', '2 chr': '2 Chronicles', '2chr': '2 Chronicles', '2ch': '2 Chronicles',
    'ezr': 'Ezra', 'neh': 'Nehemiah', 'ne': 'Nehemiah', 'est': 'Esther', 'ps': 'Psalms', 'psa': 'Psalms',
    'prov': 'Proverbs', 'pr': 'Proverbs', 'eccl': 'Ecclesiastes', 'ec': 'Ecclesiastes',
    'song': 'Song of Solomon', 'sos': 'Song of Solomon', 'ss': 'Song of Solomon',
    'isa': 'Isaiah', 'is': 'Isaiah', 'jer': 'Jeremiah', 'je': 'Jeremiah', 'lam': 'Lamentations', 'la': 'Lamentations',
    'ezek': 'Ezekiel', 'eze': 'Ezekiel', 'ezk': 'Ezekiel', 'dan': 'Daniel', 'da': 'Daniel',
    'hos': 'Hosea', 'ho': 'Hosea', 'joe': 'Joel', 'jl': 'Joel', 'am': 'Amos', 'ob': 'Obadiah',
    'jon': 'Jonah', 'mic': 'Micah', 'mi': 'Micah', 'nah': 'Nahum', 'na': 'Nahum',
    'hab': 'Habakkuk', 'hb': 'Habakkuk', 'zep': 'Zephaniah', 'zph': 'Zephaniah',
    'hag': 'Haggai', 'hg': 'Haggai', 'zech': 'Zechariah', 'zec': 'Zechariah', 'zch': 'Zechariah',
    'mal': 'Malachi', 'mat': 'Matthew', 'mt': 'Matthew', 'mar': 'Mark', 'mk': 'Mark', 'mrk': 'Mark',
    'luk': 'Luke', 'lk': 'Luke', 'joh': 'John', 'jn': 'John', 'act': 'Acts', 'ac': 'Acts',
    'rom': 'Romans', 'ro': 'Romans', '1 cor': '1 Corinthians', '1cor': '1 Corinthians', '1co': '1 Corinthians',
    '2 cor': '2 Corinthians', '2cor': '2 Corinthians', '2co': '2 Corinthians',
    'gal': 'Galatians', 'ga': 'Galatians', 'eph': 'Ephesians', 'ep': 'Ephesians',
    'phil': 'Philippians', 'php': 'Philippians', 'ph': 'Philippians',
    'col': 'Colossians', 'co': 'Colossians', '1 thess': '1 Thessalonians', '1thess': '1 Thessalonians', '1th': '1 Thessalonians',
    '2 thess': '2 Thessalonians', '2thess': '2 Thessalonians', '2th': '2 Thessalonians',
    '1 tim': '1 Timothy', '1tim': '1 Timothy', '1ti': '1 Timothy', '2 tim': '2 Timothy', '2tim': '2 Timothy', '2ti': '2 Timothy',
    'tit': 'Titus', 'ti': 'Titus', 'phm': 'Philemon', 'pm': 'Philemon',
    'heb': 'Hebrews', 'he': 'Hebrews', 'jam': 'James', 'jas': 'James', 'jm': 'James',
    '1 pet': '1 Peter', '1pet': '1 Peter', '1pe': '1 Peter', '1p': '1 Peter',
    '2 pet': '2 Peter', '2pet': '2 Peter', '2pe': '2 Peter', '2p': '2 Peter',
    '1 joh': '1 John', '1joh': '1 John', '1jn': '1 John', '2 joh': '2 John', '2joh': '2 John', '2jn': '2 John',
    '3 joh': '3 John', '3joh': '3 John', '3jn': '3 John', 'jud': 'Jude',
    'rev': 'Revelation', 're': 'Revelation'
};

function capitalizeBook(bookName) {
    return bookMap[bookName.toLowerCase()] || bookName;
}

function parseVerseReference(input) {
    var match = input.match(/^(.+)\s+(\d+):(\d+)$/i);
    if (match) {
        var book = capitalizeBook(match[1].trim());
        return '/book/' + encodeURIComponent(book) + '/chapter/' + match[2] + '/verse/' + match[3];
    }
    match = input.match(/^(.+)\s+(\d+)$/i);
    if (match) {
        var book = capitalizeBook(match[1].trim());
        return '/book/' + encodeURIComponent(book) + '/chapter/' + match[2];
    }
    return null;
}

function handleSearch(event) {
    event.preventDefault();
    var input = document.getElementById('verse-lookup-input').value.trim();
    if (!input) return false;

    var verseUrl = parseVerseReference(input);
    if (verseUrl) {
        window.location.href = verseUrl;
        return false;
    }

    var hasNumberPrefix = /^[123]\s+/i.test(input);
    var commonBooks = ['genesis', 'exodus', 'leviticus', 'numbers', 'deuteronomy',
        'joshua', 'judges', 'ruth', 'samuel', 'kings', 'chronicles', 'ezra', 'nehemiah',
        'esther', 'job', 'psalms', 'proverbs', 'ecclesiastes', 'isaiah', 'jeremiah',
        'lamentations', 'ezekiel', 'daniel', 'hosea', 'joel', 'amos', 'obadiah', 'jonah',
        'micah', 'nahum', 'habakkuk', 'zephaniah', 'haggai', 'zechariah', 'malachi',
        'matthew', 'mark', 'luke', 'john', 'acts', 'romans', 'corinthians', 'galatians',
        'ephesians', 'philippians', 'colossians', 'thessalonians', 'timothy', 'titus',
        'philemon', 'hebrews', 'james', 'peter', 'jude', 'revelation'];

    if (hasNumberPrefix || commonBooks.includes(input.toLowerCase())) {
        window.location.href = '/book/' + encodeURIComponent(capitalizeBook(input));
        return false;
    }

    window.location.href = '/search?q=' + encodeURIComponent(input);
    return false;
}

// Keyboard navigation with 2D grid support for topic-grid
var navItems = Array.from(document.querySelectorAll('.resource-card, .topic-link, .quick-links a, .interactive-grid a'));
var selectedIndex = -1;

function getGridInfo(item) {
    var parent = item.parentElement;
    if (parent && parent.classList.contains('topic-grid')) {
        var siblings = Array.from(parent.querySelectorAll('.topic-link'));
        var indexInGrid = siblings.indexOf(item);
        var cols = 4; // topic-grid has 4 columns
        return { isGrid: true, siblings: siblings, indexInGrid: indexInGrid, cols: cols, parent: parent };
    }
    if (parent && parent.classList.contains('interactive-grid')) {
        var siblings = Array.from(parent.querySelectorAll('a'));
        var indexInGrid = siblings.indexOf(item);
        var cols = 4;
        return { isGrid: true, siblings: siblings, indexInGrid: indexInGrid, cols: cols, parent: parent };
    }
    return { isGrid: false };
}

function selectItem(index) {
    if (selectedIndex >= 0 && selectedIndex < navItems.length) {
        navItems[selectedIndex].classList.remove('selected');
    }
    selectedIndex = Math.max(0, Math.min(index, navItems.length - 1));
    navItems[selectedIndex].classList.add('selected');
    navItems[selectedIndex].scrollIntoView({ behavior: 'auto', block: 'center' });
}

function selectByElement(el) {
    var idx = navItems.indexOf(el);
    if (idx >= 0) selectItem(idx);
}

document.addEventListener('keydown', function(e) {
    var el = document.activeElement;
    var isTyping = el.tagName === 'INPUT' || el.tagName === 'TEXTAREA';

    if (e.key === '/' && !e.metaKey && !e.ctrlKey && !e.altKey && !isTyping) {
        e.preventDefault();
        document.getElementById('verse-lookup-input').focus();
    }

    // Don't handle if sidebar navigation is active
    if (KJVNav.sidebarActive) return;

    if (!isTyping) {
        var currentItem = selectedIndex >= 0 ? navItems[selectedIndex] : null;
        var gridInfo = currentItem ? getGridInfo(currentItem) : { isGrid: false };

        if (e.key === 'ArrowDown' || e.key === 'j') {
            e.preventDefault();
            if (gridInfo.isGrid) {
                // Move down one row in grid
                var newGridIndex = gridInfo.indexInGrid + gridInfo.cols;
                if (newGridIndex < gridInfo.siblings.length) {
                    selectByElement(gridInfo.siblings[newGridIndex]);
                } else {
                    // Exit grid, go to next item after grid
                    var lastInGrid = gridInfo.siblings[gridInfo.siblings.length - 1];
                    var lastIdx = navItems.indexOf(lastInGrid);
                    selectItem(lastIdx + 1);
                }
            } else {
                selectItem(selectedIndex < 0 ? 0 : selectedIndex + 1);
            }
        } else if (e.key === 'ArrowUp' || e.key === 'k') {
            e.preventDefault();
            if (gridInfo.isGrid) {
                // Move up one row in grid
                var newGridIndex = gridInfo.indexInGrid - gridInfo.cols;
                if (newGridIndex >= 0) {
                    selectByElement(gridInfo.siblings[newGridIndex]);
                } else {
                    // Exit grid, go to item before grid
                    var firstInGrid = gridInfo.siblings[0];
                    var firstIdx = navItems.indexOf(firstInGrid);
                    selectItem(firstIdx - 1);
                }
            } else {
                selectItem(selectedIndex - 1);
            }
        } else if (e.key === 'ArrowRight' || e.key === 'l') {
            e.preventDefault();
            if (gridInfo.isGrid && gridInfo.indexInGrid < gridInfo.siblings.length - 1) {
                selectByElement(gridInfo.siblings[gridInfo.indexInGrid + 1]);
            } else {
                selectItem(selectedIndex < 0 ? 0 : selectedIndex + 1);
            }
        } else if (e.key === 'ArrowLeft' || e.key === 'h') {
            e.preventDefault();
            if (gridInfo.isGrid && gridInfo.indexInGrid > 0) {
                selectByElement(gridInfo.siblings[gridInfo.indexInGrid - 1]);
            } else {
                selectItem(selectedIndex - 1);
            }
        } else if (e.key === 'Enter' && selectedIndex >= 0) {
            e.preventDefault();
            window.location.href = navItems[selectedIndex].href;
        } else if (e.key === 'Escape') {
            if (selectedIndex >= 0) {
                navItems[selectedIndex].classList.remove('selected');
                selectedIndex = -1;
            }
        }
    }
});
</script>
{% endblock %}
