<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>VidServe - Video Library</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            background: #1a1a1a;
            color: #fff;
            padding: 20px;
        }

        header {
            text-align: center;
            margin-bottom: 40px;
        }

        h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            color: #fff;
        }

        .video-count {
            color: #888;
            font-size: 1rem;
        }

        .video-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
            gap: 30px;
            max-width: 1600px;
            margin: 0 auto;
        }

        .video-card {
            background: #2a2a2a;
            border-radius: 12px;
            overflow: hidden;
            transition: transform 0.2s, box-shadow 0.2s;
            cursor: pointer;
        }

        .video-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
        }

        .video-thumbnail {
            width: 100%;
            height: 180px;
            object-fit: cover;
            background: #1a1a1a;
        }

        .video-info {
            padding: 15px;
        }

        .video-name {
            font-size: 0.95rem;
            margin-bottom: 8px;
            word-break: break-word;
            color: #fff;
        }

        .video-duration {
            font-size: 0.85rem;
            color: #888;
        }

        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.95);
            z-index: 1000;
            padding: 20px;
        }

        .modal.active {
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .modal-content {
            max-width: 90%;
            max-height: 90%;
            position: relative;
        }

        .modal-video {
            width: 100%;
            max-height: 85vh;
            outline: none;
        }

        .modal-close {
            position: fixed;
            top: 20px;
            right: 20px;
            background: rgba(255, 255, 255, 0.1);
            border: none;
            color: #fff;
            font-size: 2rem;
            width: 50px;
            height: 50px;
            border-radius: 50%;
            cursor: pointer;
            transition: background 0.2s;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .modal-close:hover {
            background: rgba(255, 255, 255, 0.2);
        }

        .modal-title {
            color: #fff;
            margin-top: 15px;
            text-align: center;
            font-size: 1.1rem;
        }

        @media (max-width: 768px) {
            .video-grid {
                grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
                gap: 20px;
            }

            h1 {
                font-size: 2rem;
            }
        }
    </style>
</head>
<body>
    <header>
        <h1>VidServe</h1>
        <div class="video-count">{{ videos|length }} video{% if videos|length != 1 %}s{% endif %}</div>
    </header>

    <div class="video-grid">
        {% for video in videos %}
        <div class="video-card" onclick="playVideo('{{ video.name }}')">
            {% if video.has_thumbnail %}
            <img src="/thumbnail/{{ video.name }}" alt="{{ video.name }}" class="video-thumbnail">
            {% else %}
            <div class="video-thumbnail"></div>
            {% endif %}
            <div class="video-info">
                <div class="video-name">{{ video.name }}</div>
                <div class="video-duration">{{ video.duration }}</div>
            </div>
        </div>
        {% endfor %}
    </div>

    <div class="modal" id="videoModal" onclick="closeModal(event)">
        <button class="modal-close" onclick="closeModal()">&times;</button>
        <div class="modal-content">
            <video id="modalVideo" class="modal-video" controls autoplay>
                <source id="videoSource" src="" type="video/mp4">
                Your browser does not support the video tag.
            </video>
            <div class="modal-title" id="modalTitle"></div>
        </div>
    </div>

    <script>
        function playVideo(filename) {
            const modal = document.getElementById('videoModal');
            const video = document.getElementById('modalVideo');
            const source = document.getElementById('videoSource');
            const title = document.getElementById('modalTitle');

            source.src = `/video/${encodeURIComponent(filename)}`;
            title.textContent = filename;
            video.load();

            modal.classList.add('active');
            document.body.style.overflow = 'hidden';
        }

        function closeModal(event) {
            if (event && event.target !== event.currentTarget && event.target.className !== 'modal-close') {
                return;
            }

            const modal = document.getElementById('videoModal');
            const video = document.getElementById('modalVideo');

            video.pause();
            video.currentTime = 0;
            modal.classList.remove('active');
            document.body.style.overflow = 'auto';
        }

        // Close modal on Escape key
        document.addEventListener('keydown', function(event) {
            if (event.key === 'Escape') {
                closeModal();
            }
        });
    </script>
</body>
</html>
