<?php
// Scan subfolders and look for preview images
$dir = __DIR__ . '/';
$themes = [];

if (is_dir($dir)) {
    $handle = opendir($dir);
    while (($file = readdir($handle)) !== false) {
        if ($file === '.' || $file === '..') continue;
        if ($file[0] === '.') continue;
        if ($file === 'index.php') continue;

        $fullPath = $dir . $file;

        if (is_dir($fullPath)) {
            // Look for a preview screenshot inside the folder
            $preview = null;
            $previewNames = ['screenshot.png','screenshot.jpg','preview.png','preview.jpg','thumbnail.png','thumbnail.jpg'];
            foreach ($previewNames as $img) {
                if (file_exists($fullPath . '/' . $img)) {
                    $preview = '/balimeen/' . $file . '/' . $img;
                    break;
                }
            }
            $themes[] = [
                'name'    => $file,
                'url'     => '/balimeen/' . $file . '/',
                'preview' => $preview,
                'type'    => 'folder',
                'modified'=> date('Y-m-d', filemtime($fullPath)),
            ];
        }
    }
    closedir($handle);
}

usort($themes, fn($a,$b) => strcasecmp($a['name'], $b['name']));
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Index of /balimeen — Themes</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

        body {
            font-family: 'Inter', sans-serif;
            background: #0f0f14;
            color: #e2e8f0;
            min-height: 100vh;
        }

        /* ── Header ── */
        header {
            background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
            border-bottom: 1px solid rgba(255,255,255,0.07);
            padding: 28px 40px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            flex-wrap: wrap;
            gap: 12px;
        }

        .header-left h1 {
            font-size: 1.5rem;
            font-weight: 700;
            color: #fff;
            letter-spacing: -0.3px;
        }

        .header-left h1 span {
            color: #6366f1;
        }

        .breadcrumb {
            display: flex;
            align-items: center;
            gap: 6px;
            font-size: 0.8rem;
            color: #64748b;
            margin-top: 4px;
        }

        .breadcrumb a {
            color: #6366f1;
            text-decoration: none;
            transition: color 0.2s;
        }

        .breadcrumb a:hover { color: #818cf8; }

        .badge {
            background: rgba(99,102,241,0.15);
            border: 1px solid rgba(99,102,241,0.3);
            color: #818cf8;
            padding: 4px 12px;
            border-radius: 999px;
            font-size: 0.78rem;
            font-weight: 500;
        }

        /* ── Main container ── */
        main {
            max-width: 1300px;
            margin: 0 auto;
            padding: 40px 24px 80px;
        }

        /* ── Section title ── */
        .section-title {
            font-size: 0.78rem;
            font-weight: 600;
            letter-spacing: 0.12em;
            text-transform: uppercase;
            color: #475569;
            margin-bottom: 20px;
        }

        /* ── Empty state ── */
        .empty-state {
            text-align: center;
            padding: 80px 20px;
            color: #475569;
        }

        .empty-state svg {
            width: 64px;
            height: 64px;
            margin-bottom: 16px;
            opacity: 0.4;
        }

        .empty-state p {
            font-size: 1rem;
        }

        /* ── Grid ── */
        .themes-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 24px;
        }

        /* ── Card ── */
        .theme-card {
            background: #1a1a2e;
            border: 1px solid rgba(255,255,255,0.07);
            border-radius: 16px;
            overflow: hidden;
            text-decoration: none;
            color: inherit;
            display: flex;
            flex-direction: column;
            transition: transform 0.25s ease, box-shadow 0.25s ease, border-color 0.25s ease;
            cursor: pointer;
        }

        .theme-card:hover {
            transform: translateY(-6px);
            box-shadow: 0 20px 60px rgba(0,0,0,0.5);
            border-color: rgba(99,102,241,0.4);
        }

        /* Preview area */
        .card-preview {
            position: relative;
            width: 100%;
            aspect-ratio: 16/9;
            background: linear-gradient(135deg, #1e1e3a 0%, #0f2850 100%);
            overflow: hidden;
            flex-shrink: 0;
        }

        .card-preview img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: transform 0.4s ease;
        }

        .theme-card:hover .card-preview img {
            transform: scale(1.05);
        }

        /* No screenshot placeholder */
        .card-preview-placeholder {
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            gap: 12px;
        }

        .card-preview-placeholder .folder-icon {
            width: 52px;
            height: 52px;
            opacity: 0.6;
        }

        .card-preview-placeholder span {
            font-size: 0.78rem;
            color: #475569;
            letter-spacing: 0.05em;
        }

        /* Overlay on hover */
        .card-overlay {
            position: absolute;
            inset: 0;
            background: rgba(99,102,241,0.15);
            opacity: 0;
            transition: opacity 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .theme-card:hover .card-overlay { opacity: 1; }

        .open-btn {
            background: rgba(99,102,241,0.9);
            backdrop-filter: blur(8px);
            color: #fff;
            padding: 10px 24px;
            border-radius: 999px;
            font-size: 0.85rem;
            font-weight: 600;
            letter-spacing: 0.02em;
            transform: translateY(6px);
            transition: transform 0.3s ease;
        }

        .theme-card:hover .open-btn { transform: translateY(0); }

        /* Card body */
        .card-body {
            padding: 16px 18px 18px;
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 12px;
        }

        .card-info {}

        .card-name {
            font-size: 0.95rem;
            font-weight: 600;
            color: #e2e8f0;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            max-width: 180px;
        }

        .card-meta {
            font-size: 0.75rem;
            color: #475569;
            margin-top: 3px;
        }

        .card-arrow {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            border: 1px solid rgba(255,255,255,0.08);
            display: flex;
            align-items: center;
            justify-content: center;
            flex-shrink: 0;
            transition: background 0.2s, border-color 0.2s;
        }

        .theme-card:hover .card-arrow {
            background: #6366f1;
            border-color: #6366f1;
        }

        .card-arrow svg { width: 14px; height: 14px; }

        /* ── Footer ── */
        footer {
            border-top: 1px solid rgba(255,255,255,0.06);
            padding: 20px 40px;
            font-size: 0.78rem;
            color: #334155;
            text-align: center;
        }
    </style>
</head>
<body>

<header>
    <div class="header-left">
        <h1>Index of <span>/balimeen</span></h1>
        <div class="breadcrumb">
            <a href="/">balimeen.com</a>
            <svg width="12" height="12" fill="none" viewBox="0 0 24 24"><path stroke="#64748b" stroke-width="2" d="M9 18l6-6-6-6"/></svg>
            <span>balimeen</span>
        </div>
    </div>
    <span class="badge"><?= count($themes) ?> theme<?= count($themes) !== 1 ? 's' : '' ?></span>
</header>

<main>
    <?php if (empty($themes)): ?>
    <div class="empty-state">
        <svg fill="none" viewBox="0 0 24 24"><path stroke="#64748b" stroke-width="1.5" d="M3 7a2 2 0 012-2h4l2 2h8a2 2 0 012 2v8a2 2 0 01-2 2H5a2 2 0 01-2-2V7z"/></svg>
        <p>No themes found in this directory.</p>
    </div>
    <?php else: ?>
    <p class="section-title">Available Themes</p>
    <div class="themes-grid">
        <?php foreach ($themes as $theme): ?>
        <a class="theme-card" href="<?= htmlspecialchars($theme['url']) ?>">
            <div class="card-preview">
                <?php if ($theme['preview']): ?>
                    <img src="<?= htmlspecialchars($theme['preview']) ?>" alt="<?= htmlspecialchars($theme['name']) ?> preview" loading="lazy">
                <?php else: ?>
                    <div class="card-preview-placeholder">
                        <svg class="folder-icon" viewBox="0 0 24 24" fill="none">
                            <path d="M3 7a2 2 0 012-2h4l2 2h8a2 2 0 012 2v8a2 2 0 01-2 2H5a2 2 0 01-2-2V7z" fill="#6366f1" opacity="0.3"/>
                            <path d="M3 9h18" stroke="#6366f1" stroke-width="1.5"/>
                        </svg>
                        <span>NO PREVIEW</span>
                    </div>
                <?php endif; ?>
                <div class="card-overlay">
                    <div class="open-btn">Open Theme →</div>
                </div>
            </div>
            <div class="card-body">
                <div class="card-info">
                    <div class="card-name"><?= htmlspecialchars($theme['name']) ?></div>
                    <div class="card-meta">Modified <?= $theme['modified'] ?></div>
                </div>
                <div class="card-arrow">
                    <svg fill="none" viewBox="0 0 24 24"><path stroke="#e2e8f0" stroke-width="2" d="M9 18l6-6-6-6"/></svg>
                </div>
            </div>
        </a>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
</main>

<footer>
    Apache/2.4.62 (Win64) OpenSSL/3.3.0 PHP/8.2.12 Server at balimeen.com Port 80
</footer>

</body>
</html>
