{% extends "base.html.j2" %}
{% from "decor.html.j2" import motif_divider, page_badge %}

{% block title %}{{ t.vid_title | default('Videos') }}{% endblock %}

{% block extra_styles %}
        .vid-header {
            padding: 1.5rem 0 1rem;
            margin-bottom: 1.5rem;
            position: relative;
            padding-left: 1.5rem;
        }
        .vid-header::before {
            content: '';
            position: absolute;
            left: 0;
            top: 1.5rem;
            width: 4px;
            height: 48px;
            background: var(--td-color-primary);
            border-radius: 2px;
        }
        .vid-header h1 {
            margin: 0 0 0.5rem 0;
        }
        .vid-header .subtitle {
            color: var(--td-color-text-muted);
            margin: 0;
        }
        .vid-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 1.5rem;
            margin-bottom: 3rem;
        }
        .vid-card {
            background: var(--td-color-surface);
            border: 1px solid var(--td-color-border);
            border-radius: var(--td-radius);
            overflow: hidden;
            transition: transform 0.2s, box-shadow 0.2s;
            text-decoration: none;
            color: inherit;
            display: block;
        }
        .vid-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 24px rgba(0,0,0,0.12);
        }
        .vid-card-thumb {
            position: relative;
            aspect-ratio: 16/9;
            overflow: hidden;
            background: var(--td-color-bg-light);
        }
        .vid-card-thumb img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        .vid-play-icon {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 56px;
            height: 56px;
            background: rgba(0,0,0,0.65);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            opacity: 0.85;
            transition: opacity 0.2s, background 0.2s;
        }
        .vid-card:hover .vid-play-icon {
            opacity: 1;
            background: rgba(200,31,45,0.85);
        }
        .vid-play-icon svg {
            width: 24px;
            height: 24px;
            fill: white;
            margin-left: 3px;
        }
        .vid-card-body {
            padding: 1rem 1.25rem;
        }
        .vid-card-title {
            font-family: var(--td-font-headings);
            font-weight: 600;
            font-size: 1rem;
            color: var(--td-color-primary);
            margin: 0 0 0.35rem 0;
            line-height: 1.35;
        }
        .vid-card-meta {
            font-size: 0.8rem;
            color: var(--td-color-text-muted);
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }
        .vid-category-tag {
            font-size: 0.72rem;
            background: var(--td-color-accent-light);
            color: var(--td-color-accent-muted);
            padding: 0.15rem 0.55rem;
            border-radius: 999px;
            text-transform: capitalize;
        }
        .vid-filter-bar {
            display: flex;
            align-items: center;
            gap: 1rem;
            margin-bottom: 1.5rem;
            flex-wrap: wrap;
        }
        .vid-filter-btn {
            padding: 0.35rem 1rem;
            border-radius: 999px;
            border: 1px solid var(--td-color-border);
            background: var(--td-color-surface);
            font-size: 0.85rem;
            cursor: pointer;
            transition: all 0.15s;
            color: var(--td-color-text-secondary);
            font-family: var(--td-font-family);
        }
        .vid-filter-btn:hover {
            border-color: var(--td-color-primary);
            color: var(--td-color-primary);
        }
        .vid-filter-btn.active {
            background: var(--td-color-primary);
            border-color: var(--td-color-primary);
            color: white;
        }

        @media (max-width: 600px) {
            .vid-grid {
                grid-template-columns: 1fr;
            }
        }
{% endblock %}

{% block content %}
    <div class="vid-header">
        <h1>{{ t.vid_title | default('Videos') }} {{ page_badge(badge_type=theme.page_badge|default("moiety")) }}</h1>
        <p class="subtitle">{{ t.vid_subtitle | default('') }} &mdash; {{ data | length }} {{ t.vid_count_label | default('videos') }}</p>
    </div>

    <div class="vid-filter-bar" id="vid-filters">
        <button class="vid-filter-btn active" data-cat="all">{{ t.vid_filter_all | default('All') }}</button>
    </div>

    <div class="vid-grid" id="vid-grid">
        {% for video in data %}
        <a href="https://www.youtube.com/watch?v={{ video.youtube_id }}" target="_blank" rel="noopener" class="vid-card" data-category="{{ video.category | default('') }}">
            <div class="vid-card-thumb">
                {% if video.thumbnail %}
                <img src="{{ base_path | default('') }}{{ video.thumbnail }}" alt="{{ video.title }}" loading="lazy">
                {% endif %}
                <div class="vid-play-icon">
                    <svg viewBox="0 0 24 24"><polygon points="8,5 20,12 8,19"/></svg>
                </div>
            </div>
            <div class="vid-card-body">
                <h3 class="vid-card-title">{{ video.title }}</h3>
                <div class="vid-card-meta">
                    {% if video.category %}
                    <span class="vid-category-tag">{{ video.category }}</span>
                    {% endif %}
                    {% if video.duration %}
                    <span>{{ video.duration }}</span>
                    {% endif %}
                </div>
            </div>
        </a>
        {% endfor %}
    </div>
{% endblock %}

{% block scripts %}
    <script>
    (function() {
        var cards = document.querySelectorAll('.vid-card');
        var filterBar = document.getElementById('vid-filters');

        // Build category filters
        var cats = {};
        cards.forEach(function(c) {
            var cat = c.getAttribute('data-category');
            if (cat) cats[cat] = true;
        });
        Object.keys(cats).sort().forEach(function(cat) {
            var btn = document.createElement('button');
            btn.className = 'vid-filter-btn';
            btn.setAttribute('data-cat', cat);
            btn.textContent = cat.charAt(0).toUpperCase() + cat.slice(1);
            filterBar.appendChild(btn);
        });

        filterBar.addEventListener('click', function(e) {
            var btn = e.target.closest('.vid-filter-btn');
            if (!btn) return;
            filterBar.querySelectorAll('.vid-filter-btn').forEach(function(b) { b.classList.remove('active'); });
            btn.classList.add('active');
            var cat = btn.getAttribute('data-cat');
            cards.forEach(function(c) {
                c.style.display = (cat === 'all' || c.getAttribute('data-category') === cat) ? '' : 'none';
            });
        });
    })();
    </script>
{% endblock %}
