{% extends "base.html" %}

{% block title %}Strong's {{ entry.strongs }} - {{ entry.word }} ({{ entry.transliteration }}) - {{ entry.language }} Definition{% endblock %}

{% block description %}Strong's {{ entry.strongs }}: {{ entry.word }} ({{ entry.transliteration }}) means "{{ entry.definition[:120] }}". {{ entry.language }} word study with KJV Bible occurrences.{% endblock %}

{% block head %}
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "DefinedTerm",
  "name": "{{ entry.word }}",
  "termCode": "{{ entry.strongs }}",
  "description": "{{ entry.definition | e }}",
  "inDefinedTermSet": {
    "@type": "DefinedTermSet",
    "name": "Strong's Concordance",
    "description": "{{ entry.language }} Lexicon"
  }
}
</script>
<style>
.strongs-page {
    max-width: 800px;
}

/* Main word card */
.word-card {
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 2.5rem;
    margin-bottom: 2rem;
    text-align: center;
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
}

.word-card .strongs-number {
    font-family: monospace;
    font-size: 1.5rem;
    color: var(--text-color);
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.02em;
}

.lang-badge {
    display: inline-block;
    font-size: 0.65rem;
    padding: 0.2rem 0.5rem;
    border-radius: 3px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    font-weight: 600;
}

.lang-badge.hebrew {
    background: #8B4513;
    color: #fff;
}

.lang-badge.greek {
    background: #4169E1;
    color: #fff;
}

.word-card .original-word {
    font-size: 5rem;
    line-height: 1.2;
    margin: 0 0 0.75rem 0;
    color: var(--text-color);
    font-weight: normal;
}

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

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

.word-card .transliteration {
    font-size: 1.4rem;
    color: var(--text-secondary);
    font-style: italic;
    margin-bottom: 0.25rem;
}

.word-card .pronunciation {
    font-size: 1rem;
    color: var(--text-secondary);
}

.word-card .quick-def {
    margin-top: 1.5rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--border-color);
    font-size: 1.15rem;
    color: var(--text-color);
    line-height: 1.7;
    max-width: 550px;
    margin-left: auto;
    margin-right: auto;
}

/* Info cards grid */
.info-cards {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
    margin-bottom: 2rem;
}

.info-card {
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 1.5rem;
    transition: all 0.15s;
}

.info-card.selected {
    border-color: var(--link-color);
    box-shadow: 0 0 0 2px rgba(74, 124, 89, 0.3);
    background: #f8fff8;
}

.info-card h2 {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.1em;
    margin: 0 0 0.75rem 0;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.info-card h2::before {
    content: '';
    display: inline-block;
    width: 3px;
    height: 1em;
    border-radius: 2px;
}

.info-card.definition h2::before { background: #4a7c59; }
.info-card.kjv h2::before { background: #8B4513; }
.info-card.derivation h2::before { background: #4169E1; }

.info-card p {
    margin: 0;
    line-height: 1.7;
}

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

.info-card.kjv p {
    font-size: 1.15rem;
    color: var(--text-color);
    line-height: 1.7;
}

/* KJV Translation Tags */
.kjv-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.25rem;
}

.kjv-tag {
    display: inline-block;
    padding: 0.35rem 0.7rem;
    background: var(--code-bg);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 0.9rem;
    color: var(--text-color);
    transition: all 0.15s;
}

.kjv-tag:hover {
    border-color: var(--link-color);
    background: var(--bg-color);
}

.kjv-tag.compare {
    background: transparent;
    border-style: dashed;
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.kjv-tag.compare a {
    color: var(--link-color);
    text-decoration: none;
}

.kjv-tag.compare a:hover {
    text-decoration: underline;
}

.info-card.derivation p {
    font-size: 1.15rem;
    color: var(--text-color);
    line-height: 1.7;
}

/* Related entries embedded in derivation */
.related-entries {
    margin-top: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.related-entry-card {
    padding: 0.75rem 1rem;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    border-left: 3px solid #6495ED;
}

.related-entry-card:hover {
    border-color: #4169E1;
    box-shadow: 0 1px 4px rgba(0,0,0,0.05);
}

.related-entry-card.selected {
    border-color: var(--link-color);
    box-shadow: 0 0 0 2px rgba(74, 124, 89, 0.3);
    background: #f8fff8;
}

.related-entry-header {
    display: flex;
    align-items: baseline;
    gap: 0.75rem;
    margin-bottom: 0.35rem;
}

.related-entry-header a {
    font-weight: 600;
    font-size: 1rem;
    color: #4169E1;
    text-decoration: none;
}

.related-entry-header a:hover {
    text-decoration: underline;
}

.related-entry-lemma {
    font-size: 1.1rem;
}

.related-entry-translit {
    font-size: 0.9rem;
    color: var(--text-secondary);
    font-style: italic;
}

.related-entry-def {
    font-size: 0.9rem;
    color: var(--text-secondary);
    margin: 0;
    line-height: 1.5;
}

[data-theme="dark"] .related-entry-card {
    background: #1a1a1a;
    border-color: #333;
}

[data-theme="dark"] .related-entry-card.selected {
    background: #1a2a1a;
    box-shadow: 0 0 0 2px rgba(107, 155, 122, 0.3);
}

[data-theme="dark"] .related-entry-header a {
    color: #6495ED;
}

.strongs-ref {
    color: var(--link-color);
    text-decoration: none;
    font-weight: 500;
}

.strongs-ref:hover {
    text-decoration: underline;
}

/* Navigation */
.nav-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    flex-wrap: wrap;
}

.nav-btn {
    padding: 0.6rem 1.2rem;
    background: var(--code-bg);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    text-decoration: none;
    color: var(--text-color);
    font-size: 0.9rem;
    transition: all 0.2s;
}

.nav-btn:hover {
    border-color: var(--link-color);
    color: var(--link-color);
    background: var(--bg-color);
}

.nav-btn.primary,
.nav-btn.primary:link,
.nav-btn.primary:visited {
    background: #4a7c59;
    border-color: #4a7c59;
    color: #fff !important;
}

.nav-btn.primary:hover {
    background: #3d6a4b;
    border-color: #3d6a4b;
    color: #fff !important;
}

.prev-next {
    display: flex;
    gap: 0.5rem;
}

/* Occurrences section */
.occurrences-section {
    margin-bottom: 2rem;
}

.occurrences-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.occurrences-section > h2,
.occurrences-header h2 {
    font-size: 1.1rem;
    margin: 0;
    color: var(--text-color);
}

.occurrences-count {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin: 0;
    background: var(--code-bg);
    padding: 0.25rem 0.6rem;
    border-radius: 4px;
}

.occurrences-grid {
    display: grid;
    gap: 0.75rem;
}

.occurrence-card {
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 1rem 1.25rem;
    transition: all 0.15s;
}

.occurrence-card:hover {
    border-color: var(--link-color);
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.occurrence-card.selected {
    border-color: var(--link-color);
    box-shadow: 0 0 0 2px rgba(74, 124, 89, 0.3);
    background: #f8fff8;
}

.occurrence-card.hidden {
    display: none;
}

.occurrence-card .occ-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
}

.occurrence-card .occ-reference {
    color: var(--link-color);
    text-decoration: none;
    font-weight: 600;
    font-size: 0.95rem;
}

.occurrence-card .occ-reference:hover {
    text-decoration: underline;
}

.occurrence-card .occ-original {
    font-size: 1.1rem;
    color: var(--text-secondary);
    font-family: "SBL Hebrew", "Ezra SIL", "Times New Roman", serif;
}

.occurrence-card .occ-verse-text {
    font-size: 0.95rem;
    line-height: 1.65;
    color: var(--text-color);
    margin: 0;
}

.occurrence-card .occ-verse-text mark {
    background: linear-gradient(180deg, transparent 60%, #fff3cd 60%);
    color: inherit;
    padding: 0;
    border-radius: 0;
    font-weight: 600;
}

/* Show more button */
.show-more-container {
    text-align: center;
    margin-top: 1rem;
}

.show-more-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1.5rem;
    background: var(--code-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-size: 0.9rem;
    font-family: inherit;
    color: var(--text-color);
    cursor: pointer;
    transition: all 0.15s;
    -webkit-appearance: none;
    appearance: none;
}

.show-more-btn:hover {
    border-color: var(--link-color);
    color: var(--link-color);
}

.show-more-btn .count {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

[data-theme="dark"] .occurrence-card {
    background: #1a1a1a;
    border-color: #333;
}

[data-theme="dark"] .occurrence-card:hover {
    border-color: var(--link-color);
}

[data-theme="dark"] .occurrence-card.selected {
    background: #1a2a1a;
    box-shadow: 0 0 0 2px rgba(107, 155, 122, 0.3);
}

[data-theme="dark"] .occurrence-card .occ-verse-text mark {
    background: linear-gradient(180deg, transparent 60%, #5a4a00 60%);
    color: #fff;
}

/* Dark mode */
[data-theme="dark"] .word-card,
[data-theme="dark"] .info-card {
    background: #1a1a1a;
    border-color: #333;
}

[data-theme="dark"] .info-card.selected {
    background: #1a2a1a;
    box-shadow: 0 0 0 2px rgba(107, 155, 122, 0.3);
}

[data-theme="dark"] .kjv-tag {
    background: #2a2a2a;
    border-color: #444;
}

[data-theme="dark"] .nav-btn {
    background: #2a2a2a;
    border-color: #444;
}

[data-theme="dark"] .nav-btn:hover {
    background: #333;
}

[data-theme="dark"] .show-more-btn {
    background: #2a2a2a;
    border-color: #444;
    color: var(--text-color);
}

[data-theme="dark"] .show-more-btn .count {
    color: var(--text-secondary);
}

[data-theme="dark"] .occurrences-count {
    background: #2a2a2a;
}

@media (max-width: 600px) {
    .word-card {
        padding: 2rem 1.5rem;
        border-radius: 12px;
    }

    .word-card .original-word {
        font-size: 3.5rem;
    }

    .word-card .transliteration {
        font-size: 1.2rem;
    }

    .word-card .quick-def {
        font-size: 1rem;
    }

    .nav-row {
        flex-direction: column;
        align-items: stretch;
    }

    .prev-next {
        justify-content: center;
    }

    .kjv-tags {
        gap: 0.4rem;
    }

    .kjv-tag {
        font-size: 0.8rem;
        padding: 0.3rem 0.5rem;
    }
}

/* Large font mode */
[data-font-size="large"] .occurrence-card .occ-reference {
    font-size: 1.2rem;
}
[data-font-size="large"] .occurrence-card .occ-original {
    font-size: 1.4rem;
}
[data-font-size="large"] .occurrence-card .occ-verse-text {
    font-size: 1.2rem;
    line-height: 1.75;
}
[data-font-size="large"] .word-card .quick-def {
    font-size: 1.4rem;
}
[data-font-size="large"] .info-card p {
    font-size: 1.3rem;
}
[data-font-size="large"] .info-card.definition p,
[data-font-size="large"] .info-card.kjv p,
[data-font-size="large"] .info-card.derivation p {
    font-size: 1.3rem;
}
[data-font-size="large"] .kjv-tag {
    font-size: 1.1rem;
    padding: 0.4rem 0.8rem;
}
[data-font-size="large"] .related-entry-def {
    font-size: 1.1rem;
}
[data-font-size="large"] .related-entry-header a {
    font-size: 1.15rem;
}
[data-font-size="large"] .related-entry-lemma {
    font-size: 1.3rem;
}
</style>
{% endblock %}

{% block content %}
<article class="strongs-page">
    <section class="word-card">
        <div class="strongs-number">
            {{ entry.strongs }}
            <span class="lang-badge {% if entry.language == 'Hebrew' %}hebrew{% else %}greek{% endif %}">{{ entry.language }}</span>
        </div>
        <h1 class="original-word {% if entry.language == 'Hebrew' %}hebrew{% else %}greek{% endif %}" lang="{% if entry.language == 'Hebrew' %}he{% else %}el{% endif %}">{{ entry.word }}</h1>
        <div class="transliteration">{{ entry.transliteration }}{% if entry.pronunciation %} ({{ entry.pronunciation }}){% endif %}</div>
        <div class="quick-def">{{ entry.definition }}</div>
    </section>

    <section class="info-cards">
        {% if entry.kjv_usage %}
        <div class="info-card kjv">
            <h2>KJV Translations of {{ entry.strongs }}</h2>
            <p>{{ entry.kjv_usage | linkify_strongs | safe }}</p>
        </div>
        {% endif %}

        {% if entry.derivation %}
        <div class="info-card derivation">
            <h2>Word Origin &amp; Derivation</h2>
            <p>{{ entry.derivation | linkify_strongs | safe }}</p>
            {% if related_entries %}
            <div class="related-entries">
                {% for rel in related_entries %}
                <div class="related-entry-card">
                    <div class="related-entry-header">
                        <a href="/strongs/{{ rel.strongs }}">{{ rel.strongs }}</a>
                        <span class="related-entry-lemma">{{ rel.word }}</span>
                        {% if rel.transliteration %}<span class="related-entry-translit">({{ rel.transliteration }})</span>{% endif %}
                    </div>
                    <p class="related-entry-def">{{ rel.definition[:150] }}{% if rel.definition|length > 150 %}...{% endif %}</p>
                </div>
                {% endfor %}
            </div>
            {% endif %}
        </div>
        {% endif %}
    </section>

    {% if verse_occurrences %}
    <section class="occurrences-section">
        <div class="occurrences-header">
            <h2>{{ entry.strongs }} in the King James Bible</h2>
            <span class="occurrences-count">{{ total_occurrences }} verse{% if total_occurrences != 1 %}s{% endif %}</span>
        </div>
        <div class="occurrences-grid" id="occurrences-grid">
            {% for occ in verse_occurrences %}
            <div class="occurrence-card{% if loop.index > 10 %} hidden{% endif %}" data-index="{{ loop.index }}">
                <div class="occ-header">
                    <a href="{{ occ.url }}" class="occ-reference">{{ occ.reference }}</a>
                    <span class="occ-original">{{ occ.original }}</span>
                </div>
                <p class="occ-verse-text">{{ occ.verse_text | safe }}</p>
            </div>
            {% endfor %}
        </div>
        {% if total_occurrences > 10 %}
        <div class="show-more-container" id="show-more-container">
            <button class="show-more-btn" id="show-more-btn" onclick="showMoreOccurrences()">
                Show more <span class="count">(<span id="remaining-count">{{ total_occurrences - 10 }}</span> remaining)</span>
            </button>
        </div>
        {% endif %}
    </section>
    {% endif %}

    <nav class="nav-row" aria-label="Strong's number navigation">
        <a href="/strongs" class="nav-btn primary">← Search Strong's</a>
        <div class="prev-next">
            {% set num = entry.strongs[1:] | int %}
            {% set prefix = entry.strongs[0] %}
            {% if num > 1 %}
            <a href="/strongs/{{ prefix }}{{ num - 1 }}" class="nav-btn" rel="prev" title="Previous: Strong's {{ prefix }}{{ num - 1 }}">← {{ prefix }}{{ num - 1 }}</a>
            {% endif %}
            <a href="/strongs/{{ prefix }}{{ num + 1 }}" class="nav-btn" rel="next" title="Next: Strong's {{ prefix }}{{ num + 1 }}">{{ prefix }}{{ num + 1 }} →</a>
        </div>
    </nav>
</article>

<script>
// Show more occurrences
let visibleCount = 10;
const batchSize = 20;

function showMoreOccurrences() {
    const cards = document.querySelectorAll('.occurrence-card.hidden');
    const total = document.querySelectorAll('.occurrence-card').length;

    // Show next batch
    let shown = 0;
    cards.forEach(card => {
        if (shown < batchSize) {
            card.classList.remove('hidden');
            shown++;
        }
    });

    visibleCount += shown;

    // Update remaining count
    const remaining = total - visibleCount;
    const remainingEl = document.getElementById('remaining-count');
    if (remainingEl) {
        remainingEl.textContent = remaining;
    }

    // Hide button if all shown
    if (remaining <= 0) {
        const container = document.getElementById('show-more-container');
        if (container) container.style.display = 'none';
    }
}

(function() {
    // Include info cards, related entries, and occurrence cards in keyboard nav
    function getAllNavigableCards() {
        const infoCards = document.querySelectorAll('.info-card');
        const related = document.querySelectorAll('.related-entry-card');
        const occurrences = document.querySelectorAll('.occurrence-card:not(.hidden)');
        return [...infoCards, ...related, ...occurrences];
    }

    let selectedIndex = -1;

    function selectCard(index) {
        const cards = getAllNavigableCards();
        if (cards.length === 0) return;

        // Remove previous selection
        const prevCard = document.querySelector('.info-card.selected, .related-entry-card.selected, .occurrence-card.selected');
        if (prevCard) prevCard.classList.remove('selected');

        // Update index with bounds checking
        selectedIndex = Math.max(0, Math.min(index, cards.length - 1));

        // Add selection to new card
        cards[selectedIndex].classList.add('selected');

        // Scroll into view
        cards[selectedIndex].scrollIntoView({
            behavior: 'auto',
            block: 'center'
        });
    }

    document.addEventListener('keydown', function(e) {
        // Don't handle if user is typing in an input
        if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return;
        if (typeof KJVNav !== 'undefined' && KJVNav.sidebarActive) return;

        const cards = getAllNavigableCards();
        if (cards.length === 0) return;

        if (e.key === 'ArrowDown' || e.key === 'j') {
            e.preventDefault();
            selectCard(selectedIndex + 1);
        } else if (e.key === 'ArrowUp' || e.key === 'k') {
            e.preventDefault();
            selectCard(selectedIndex - 1);
        } else if (e.key === 'Enter' && selectedIndex >= 0 && selectedIndex < cards.length) {
            e.preventDefault();
            // Check for link in any card type
            const link = cards[selectedIndex].querySelector('.occ-reference, .related-entry-header a, .strongs-ref');
            if (link) window.location.href = link.href;
        } else if (e.key === 'ArrowLeft' || e.key === 'h') {
            // Navigate back in browser history
            e.preventDefault();
            history.back();
        } else if (e.key === '[') {
            // Navigate to previous Strong's number
            e.preventDefault();
            const prevLink = document.querySelector('.nav-btn[rel="prev"]');
            if (prevLink) window.location.href = prevLink.href;
        } else if (e.key === 'ArrowRight' || e.key === 'l' || e.key === ']') {
            // Navigate to next Strong's number
            e.preventDefault();
            const nextLink = document.querySelector('.nav-btn[rel="next"]');
            if (nextLink) window.location.href = nextLink.href;
        }
    });

    // Click to select - attach to all card types
    document.querySelectorAll('.info-card, .related-entry-card, .occurrence-card').forEach((card) => {
        card.addEventListener('click', function(e) {
            if (e.target.tagName !== 'A') {
                const allCards = getAllNavigableCards();
                const cardIndex = allCards.indexOf(card);
                if (cardIndex >= 0) selectCard(cardIndex);
            }
        });
    });
})();
</script>
{% endblock %}
