{% extends "base.html" %}

{% block title %}{{ story.kids_title }} - Bible Stories for Kids - KJV Study{% endblock %}
{% block description %}{{ story.kids_description }}{% endblock %}

{% block og_type %}article{% endblock %}
{% block og_title %}{{ story.kids_title }} - Bible Story for Kids{% endblock %}
{% block og_description %}{{ story.kids_description }}{% endblock %}

{% block head %}
<style>
.kids-header {
    background: linear-gradient(135deg, #dbeafe 0%, #ede9fe 50%, #fce7f3 100%);
    padding: 2.5rem;
    border-radius: 16px;
    margin-bottom: 2rem;
    text-align: center;
}
.kids-header .category-link {
    display: inline-block;
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: #7c3aed;
    text-decoration: none;
    background: rgba(255,255,255,0.7);
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    margin-bottom: 0.75rem;
}
.kids-header h1 {
    color: #4338ca;
    margin: 0.5rem 0;
    font-size: 2.5rem;
}
.kids-header .subtitle {
    color: #6366f1;
    font-size: 1.1rem;
    margin: 0;
}

.story-actions {
    display: flex;
    justify-content: center;
    gap: 0.75rem;
    margin-top: 1rem;
}
.action-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.5rem 1rem;
    font-size: 0.85rem;
    border: 2px solid #c4b5fd;
    border-radius: 20px;
    background: rgba(255,255,255,0.8);
    color: #7c3aed;
    cursor: pointer;
    transition: all 0.2s;
}
.action-btn:hover {
    background: #f5f0ff;
    border-color: #8b5cf6;
}
.action-btn svg {
    width: 16px;
    height: 16px;
}
.action-btn.playing {
    background: #8b5cf6;
    color: white;
    border-color: #8b5cf6;
}

.kids-meta {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 1rem;
    margin: 1.5rem 0 2rem;
}
.kids-meta-item {
    background: #fff;
    border: 2px solid #e5e7eb;
    border-radius: 12px;
    padding: 1rem 1.5rem;
    text-align: center;
    min-width: 140px;
}
.kids-meta-item .label {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: #9ca3af;
    margin-bottom: 0.25rem;
}
.kids-meta-item .value {
    font-size: 0.95rem;
    color: #374151;
}
.kids-meta-item .value a {
    color: #6366f1;
    text-decoration: none;
}
.kids-meta-item .value a:hover {
    text-decoration: underline;
}

.characters-row {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.5rem;
    margin: 1rem 0;
}
.character-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    background: linear-gradient(135deg, #dbeafe, #e0e7ff);
    padding: 0.4rem 0.8rem;
    border-radius: 20px;
    font-size: 0.9rem;
    color: #3730a3;
}
.character-badge::before {
    content: "★";
    font-size: 0.7rem;
}

/* Story divider */
hr.story-divider {
    border: 0;
    height: 0;
    text-align: center;
    margin: 2rem 0;
}
hr.story-divider::before {
    content: "* * *";
    font-size: 1rem;
    letter-spacing: 0.6em;
    color: #c4b5fd;
}

.themes-section {
    background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
    border-radius: 16px;
    padding: 2rem;
    margin: 2.5rem 0;
    text-align: center;
}
.themes-section h3 {
    color: #92400e;
    margin: 0 0 1rem 0;
    font-size: 1.3rem;
}
.themes-list {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.75rem;
    list-style: none;
    padding: 0;
    margin: 0;
}
.themes-list li {
    background: rgba(255,255,255,0.8);
    padding: 0.5rem 1rem;
    border-radius: 20px;
    font-size: 0.95rem;
    color: #78350f;
}

.adult-callout {
    background: #fff;
    border: 2px dashed #c4b5fd;
    border-radius: 12px;
    padding: 1.5rem 2rem;
    margin: 2rem 0;
    text-align: center;
}
.adult-callout p {
    margin: 0;
    color: #6b7280;
}
.adult-callout a {
    color: #7c3aed;
    font-weight: 600;
    text-decoration: none;
}
.adult-callout a:hover {
    text-decoration: underline;
}

.kids-nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 3rem;
    padding-top: 1.5rem;
    border-top: 2px dashed #e5e7eb;
    gap: 1rem;
}
.kids-nav .nav-prev,
.kids-nav .nav-next {
    flex: 1;
    max-width: 40%;
}
.kids-nav .nav-prev {
    text-align: left;
}
.kids-nav .nav-next {
    text-align: right;
}
.kids-nav .nav-center {
    text-align: center;
}
.kids-nav .nav-label {
    display: block;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: #9ca3af;
    margin-bottom: 0.25rem;
}
.kids-nav .nav-link {
    font-size: 0.95rem;
    color: #7c3aed;
    text-decoration: none;
}
.kids-nav .nav-link:hover {
    text-decoration: underline;
}
.kids-nav .nav-disabled {
    font-size: 0.95rem;
    color: #d1d5db;
}

/* Dark mode styles */
[data-theme="dark"] .kids-header {
    background: linear-gradient(135deg, #1e3a5f 0%, #2d1b4e 50%, #3b1f3b 100%);
}
[data-theme="dark"] .kids-header .category-link {
    background: rgba(0,0,0,0.3);
    color: #c4b5fd;
}
[data-theme="dark"] .kids-header h1 {
    color: #a5b4fc;
}
[data-theme="dark"] .kids-header .subtitle {
    color: #818cf8;
}
[data-theme="dark"] .action-btn {
    background: rgba(0,0,0,0.3);
    border-color: #6d28d9;
    color: #c4b5fd;
}
[data-theme="dark"] .action-btn:hover {
    background: rgba(109,40,217,0.3);
    border-color: #8b5cf6;
}
[data-theme="dark"] .kids-meta-item {
    background: var(--bg-color);
    border-color: #374151;
}
[data-theme="dark"] .kids-meta-item .label {
    color: #9ca3af;
}
[data-theme="dark"] .kids-meta-item .value {
    color: #e5e7eb;
}
[data-theme="dark"] .kids-meta-item .value a {
    color: #a5b4fc;
}
[data-theme="dark"] .character-badge {
    background: linear-gradient(135deg, #1e3a5f, #312e81);
    color: #c7d2fe;
}
[data-theme="dark"] hr.story-divider::before {
    color: #6d28d9;
}
[data-theme="dark"] .themes-section {
    background: linear-gradient(135deg, #422006 0%, #78350f 100%);
}
[data-theme="dark"] .themes-section h3 {
    color: #fcd34d;
}
[data-theme="dark"] .themes-section .themes-list li {
    background: rgba(0,0,0,0.3);
    color: #fde68a;
}
[data-theme="dark"] .adult-callout {
    background: var(--bg-color);
    border-color: #6d28d9;
}
[data-theme="dark"] .adult-callout p {
    color: #9ca3af;
}
[data-theme="dark"] .adult-callout a {
    color: #a5b4fc;
}
[data-theme="dark"] .kids-nav {
    border-top-color: #374151;
}
[data-theme="dark"] .kids-nav .nav-link {
    color: #a5b4fc;
}
[data-theme="dark"] .kids-nav .nav-disabled {
    color: #4b5563;
}

/* Print styles */
@media print {
    .kids-header {
        background: none;
        padding: 0;
        text-align: left;
    }

    .kids-header .category-link,
    .kids-meta,
    .characters-row,
    .themes-section,
    .adult-callout,
    .story-actions,
    .kids-nav {
        display: none;
    }

    .kids-header h1 {
        color: #000;
        font-size: 1.8rem;
    }

    .kids-header .subtitle {
        color: #666;
    }

    hr.story-divider {
        display: none;
    }

    section p {
        font-size: 11pt;
        line-height: 1.6;
        color: #000;
    }
}
</style>
{% endblock %}

{% block content %}
<article>
    <header class="kids-header">
        <a href="/stories#{{ story.category_slug }}" class="category-link">{{ story.category_name }}</a>
        <h1>{{ story.kids_title }}</h1>
        <p class="subtitle">A Bible Story for Kids</p>
    </header>

    <div class="kids-meta">
        <div class="kids-meta-item">
            <div class="label">Find it in the Bible</div>
            <div class="value">
                {% set verse = story.verses[0] %}
                {% set ref_parts = verse.split(' ') %}
                {% if ref_parts|length >= 2 %}
                    {% set chapter_verse = ref_parts[-1] %}
                    {% set book = ' '.join(ref_parts[:-1]) %}
                    {% if ':' in chapter_verse %}
                        {% set chapter = chapter_verse.split(':')[0] %}
                        {% set verse_part = chapter_verse.split(':')[1] %}
                        {% if '-' in verse_part %}
                            {# Verse range like "16:23-31" - use anchor syntax #}
                            <a href="/book/{{ book }}/chapter/{{ chapter }}#verse-{{ verse_part }}">{{ verse }}</a>
                        {% else %}
                            {# Single verse like "3:16" #}
                            <a href="/book/{{ book }}/chapter/{{ chapter }}#verse-{{ verse_part }}">{{ verse }}</a>
                        {% endif %}
                    {% elif '-' in chapter_verse %}
                        {# Chapter range like "1-2" - link to first chapter #}
                        {% set chapter = chapter_verse.split('-')[0] %}
                        <a href="/book/{{ book }}/chapter/{{ chapter }}">{{ verse }}</a>
                    {% else %}
                        {# Just a chapter number #}
                        <a href="/book/{{ book }}/chapter/{{ chapter_verse }}">{{ verse }}</a>
                    {% endif %}
                {% else %}
                    {{ verse }}
                {% endif %}
                {% if story.verses|length > 1 %}
                    <span style="color: #9ca3af;"> +{{ story.verses|length - 1 }} more</span>
                {% endif %}
            </div>
        </div>
    </div>

    <div class="characters-row">
        {% for character in story.characters %}
        <span class="character-badge">{{ character }}</span>
        {% endfor %}
    </div>

    <hr class="story-divider">

    <section>
        {% for paragraph in story.kids_narrative.split('\n\n') %}
        {% if loop.first %}
        <p><span class="newthought">{{ paragraph.split(' ')[:3] | join(' ') }}</span> {{ paragraph.split(' ')[3:] | join(' ') }}</p>
        {% else %}
        <p>{{ paragraph }}</p>
        {% endif %}
        {% endfor %}
    </section>

    <aside class="adult-callout">
        <p>Want to read the full story with more details?<br/>
        Check out the <a href="/stories/{{ story.slug }}">grown-up version</a> of this story!</p>
    </aside>

    <div class="story-actions">
        <button class="action-btn listen-btn" id="story-listen-btn">
            <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="M15.536 8.464a5 5 0 010 7.072m2.828-9.9a9 9 0 010 12.728M5.586 15H4a1 1 0 01-1-1v-4a1 1 0 011-1h1.586l4.707-4.707C10.923 3.663 12 4.109 12 5v14c0 .891-1.077 1.337-1.707.707L5.586 15z" />
            </svg>
            Listen
        </button>
        <a href="/stories/{{ story.slug }}/kids/pdf" class="action-btn">
            <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 PDF
        </a>
    </div>
</article>

<nav class="kids-nav">
    <div class="nav-prev">
        {% if prev_story %}
        <span class="nav-label">Previous Story</span>
        <a href="/stories/{{ prev_story.slug }}/kids" class="nav-link">← {{ prev_story.kids_title or prev_story.title }}</a>
        {% else %}
        <span class="nav-label">Previous Story</span>
        <span class="nav-disabled">← No previous story</span>
        {% endif %}
    </div>
    <div class="nav-center">
        <a href="/stories" class="nav-link">All Stories</a>
    </div>
    <div class="nav-next">
        {% if next_story %}
        <span class="nav-label">Next Story</span>
        <a href="/stories/{{ next_story.slug }}/kids" class="nav-link">{{ next_story.kids_title or next_story.title }} →</a>
        {% else %}
        <span class="nav-label">Next Story</span>
        <span class="nav-disabled">No next story →</span>
        {% endif %}
    </div>
</nav>

<script>
(function() {
    // Listen button handler
    var listenBtn = document.getElementById('story-listen-btn');
    var isListening = false;
    if (listenBtn) {
        listenBtn.addEventListener('click', function() {
            if (isListening) {
                if (window.KJVSpeech) window.KJVSpeech.stop();
                listenBtn.innerHTML = '<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="M15.536 8.464a5 5 0 010 7.072m2.828-9.9a9 9 0 010 12.728M5.586 15H4a1 1 0 01-1-1v-4a1 1 0 011-1h1.586l4.707-4.707C10.923 3.663 12 4.109 12 5v14c0 .891-1.077 1.337-1.707.707L5.586 15z" /></svg> Listen';
                isListening = false;
                return;
            }
            var paragraphs = document.querySelectorAll('article section p');
            var text = Array.from(paragraphs).map(function(p) {
                return p.textContent.trim();
            }).join(' ');
            if (window.KJVSpeech && text) {
                window.KJVSpeech.speak(text);
                listenBtn.innerHTML = '<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="M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 10a1 1 0 011-1h4a1 1 0 011 1v4a1 1 0 01-1 1h-4a1 1 0 01-1-1v-4z" /></svg> Stop';
                isListening = true;
            }
        });
    }

    // Collect paragraphs, adult callout, and action buttons for up/down navigation
    var elements = Array.from(document.querySelectorAll('article section p, .adult-callout, .story-actions .action-btn'));
    // Find the index of the first paragraph (to start selection there)
    var firstParagraphIndex = elements.findIndex(function(el) { return el.tagName === 'P'; });
    var selectedIndex = -1;

    function selectElement(index) {
        // Remove previous selection
        if (selectedIndex >= 0 && selectedIndex < elements.length) {
            elements[selectedIndex].style.outline = '';
            elements[selectedIndex].style.outlineOffset = '';
            elements[selectedIndex].classList.remove('selected');
        }

        selectedIndex = Math.max(0, Math.min(index, elements.length - 1));

        // Add selection to new element
        elements[selectedIndex].style.outline = '2px solid #8b5cf6';
        elements[selectedIndex].style.outlineOffset = '8px';
        elements[selectedIndex].classList.add('selected');
        elements[selectedIndex].scrollIntoView({ behavior: 'auto', block: 'center' });
    }

    document.addEventListener('keydown', function(e) {
        if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return;
        if (KJVNav.sidebarActive) return;

        if (e.key === 'ArrowDown' || e.key === 'j') {
            e.preventDefault();
            if (KJVNav.isSelectionOffScreen(elements, selectedIndex)) {
                selectElement(KJVNav.findFirstVisibleIndex(elements));
            } else {
                // Start at first paragraph, not metadata
                selectElement(selectedIndex < 0 ? firstParagraphIndex : selectedIndex + 1);
            }
        } else if (e.key === 'ArrowUp' || e.key === 'k') {
            e.preventDefault();
            if (KJVNav.isSelectionOffScreen(elements, selectedIndex)) {
                selectElement(KJVNav.findFirstVisibleIndex(elements));
            } else if (selectedIndex <= 0) {
                selectElement(0);
            } else {
                selectElement(selectedIndex - 1);
            }
        } else if (e.key === 'ArrowLeft' || e.key === 'h') {
            e.preventDefault();
            history.back();
        } else if (e.key === 'ArrowRight' || e.key === 'l') {
            e.preventDefault();
            var nextLink = document.querySelector('.nav-next .nav-link');
            if (nextLink) window.location.href = nextLink.href;
        } else if (e.key === '[') {
            e.preventDefault();
            var prevLink = document.querySelector('.nav-prev .nav-link');
            if (prevLink) window.location.href = prevLink.href;
        } else if (e.key === ']') {
            e.preventDefault();
            var nextLink = document.querySelector('.nav-next .nav-link');
            if (nextLink) window.location.href = nextLink.href;
        } else if (e.key === 'Enter' && selectedIndex >= 0) {
            e.preventDefault();
            var el = elements[selectedIndex];
            // Check if the element itself is a link (like PDF button)
            if (el.tagName === 'A' && el.href) {
                window.location.href = el.href;
            } else {
                var link = el.querySelector('a');
                if (link) window.location.href = link.href;
            }
        } else if (e.key === 'Escape') {
            e.preventDefault();
            if (selectedIndex >= 0 && selectedIndex < elements.length) {
                elements[selectedIndex].style.outline = '';
                elements[selectedIndex].style.outlineOffset = '';
                elements[selectedIndex].classList.remove('selected');
            }
            selectedIndex = -1;
        }
    });
})();
</script>
{% endblock %}
