{% extends "base.html" %}

{% block title %}{{ content.title }} - KJV Study{% endblock %}
{% block description %}The sacred four-letter name of God (YHWH) - its meaning, pronunciation, and theological significance{% endblock %}

{% block head %}
<style>
.verse-text {
    color: var(--text-secondary);
    font-style: italic;
}

.toc {
    max-width: 55%;
    margin: 2rem 0;
    padding: 1.5rem;
    border-left: 3px solid var(--border-color-darker);
}

.toc h2 {
    margin-top: 0;
    font-size: 1.1rem;
    font-weight: 600;
    margin-bottom: 1rem;
}

.toc ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.toc li {
    margin: 0.5rem 0;
    line-height: 1.6;
}

.toc a {
    color: var(--text-color);
    text-decoration: none;
    border-bottom: 1px solid transparent;
}

.toc a:hover {
    color: var(--link-color);
    border-bottom-color: var(--link-color);
}

.toc li.toc-h3 {
    padding-left: 1.5rem;
    font-size: 0.95rem;
}

.tetragrammaton-actions {
    margin: 1rem 0 1.5rem;
}

.tetragrammaton-download-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.35rem 0.75rem;
    font-size: 0.85rem;
    color: var(--text-secondary, #666);
    background: var(--code-bg, #f8f8f8);
    border: 1px solid var(--border-color, #ddd);
    border-radius: 4px;
    text-decoration: none;
    transition: all 0.2s;
}

.tetragrammaton-download-btn:hover {
    background: var(--bg-color, #fff);
    border-color: var(--link-color);
    color: var(--link-color);
}

.tetragrammaton-download-btn svg {
    width: 14px;
    height: 14px;
}

@media print {
    .tetragrammaton-actions,
    .tetragrammaton-download-btn,
    .toc {
        display: none !important;
    }

    section {
        page-break-inside: avoid;
    }

    .sidenote,
    .marginnote {
        display: block;
        float: none;
        width: 100%;
        margin: 0.5rem 0;
        font-size: 0.9rem;
        color: #666;
    }
}
</style>
{% endblock %}

{% block content %}
<h1>{{ content.title }}</h1>
<p class="subtitle">{{ content.subtitle }}</p>

{% if pdf_available %}
<div class="tetragrammaton-actions">
    <a class="tetragrammaton-download-btn" href="/tetragrammaton/pdf">
        <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
        </svg>
        Download Study (PDF)
    </a>
</div>
{% endif %}

<nav class="toc" id="toc">
    <h2>Contents</h2>
    <ul id="toc-list"></ul>
</nav>

<section>
    <p>{{ content.introduction | safe }}</p>
</section>

{% for section in content.sections %}
<section>
    <h2>{{ section.heading }}</h2>

    <p>{{ section.content | safe }}</p>

    {% if section.verses %}
    <h3>Key Scripture</h3>
    {% for verse in section.verses %}
    <p style="margin: 1rem 0; padding-left: 1.5rem; border-left: 2px solid var(--border-color-dark);">
        {% set _rp = verse.reference|rsplit(' ', 1) %}<strong><a href="/book/{{ _rp[0] }}/chapter/{{ _rp[1].split(':')[0] }}/verse/{{ _rp[1].split(':')[1] if ':' in verse.reference else '1' }}">{{ verse.reference }}</a></strong><br/>
        <em class="verse-text">{{ verse.text }}</em>
    </p>
    {% endfor %}
    {% endif %}
</section>
{% endfor %}

<section>
    <h2>Conclusion</h2>
    <p>{{ content.conclusion | safe }}</p>
</section>

<section>
    <p><a href="/resources">← Back to Resources</a></p>
</section>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // Generate TOC from h2 and h3 headings
    const tocList = document.getElementById('toc-list');
    const headings = document.querySelectorAll('section h2, section h3');

    headings.forEach((heading, index) => {
        if (!heading.id) {
            heading.id = 'section-' + heading.textContent.toLowerCase().replace(/[^a-z0-9]+/g, '-');
        }
        const li = document.createElement('li');
        if (heading.tagName === 'H3') li.classList.add('toc-h3');
        const a = document.createElement('a');
        a.href = '#' + heading.id;
        a.textContent = heading.textContent;
        li.appendChild(a);
        tocList.appendChild(li);
    });

    // Simple keyboard navigation
    KJVNav.initSimpleNav('.tetragrammaton-download-btn, section > p, section h2, section h3');
});
</script>
{% endblock %}
