<?php ob_start(); ?>

<div class="content-header">
    <h2><?= __('admin.nav.themes') ?></h2>
    <div class="header-actions">
        <?php if ($zipOk): ?>
        <a href="/admin/themes/upload" class="btn btn-primary"><?= __('admin.themes.install') ?></a>
        <?php else: ?>
        <span class="btn btn-secondary" style="opacity:.55; cursor:not-allowed;"
              title="<?= __('admin.themes.zip_not_available') ?>">⬆ <?= __('admin.themes.install') ?></span>
        <?php endif; ?>
    </div>
</div>

<div class="theme-grid">
    <?php foreach ($themes as $slug => $theme): ?>
    <?php $isActive    = ($slug === $activeTheme); ?>
    <?php $isProtected = in_array($slug, $permanentlyProtected, true); ?>
    <div class="theme-card <?= $isActive ? 'theme-card--active' : '' ?>">

        <?php if ($isActive): ?>
        <div class="theme-card__badge"><?= __('admin.themes.active_badge') ?></div>
        <?php endif; ?>

        <!-- Preview area: thumbnail image or color-dot fallback -->
        <div class="theme-card__preview">
            <?php if (!empty($theme['thumbnailUrl'])): ?>
            <img class="theme-card__thumbnail"
                 src="<?= e($theme['thumbnailUrl']) ?>"
                 alt="<?= e($theme['name']) ?> preview"
                 loading="lazy">
            <?php elseif (!empty($theme['colors'])): ?>
            <div class="theme-color-bar">
                <?php foreach ($theme['colors'] as $colorName => $colorValue): ?>
                <span class="theme-color-dot"
                      style="background:<?= e($colorValue) ?>;"
                      title="<?= e($colorName) ?>: <?= e($colorValue) ?>"></span>
                <?php endforeach; ?>
            </div>
            <?php else: ?>
            <div class="theme-color-bar theme-color-bar--empty"><?= __('admin.themes.no_preview') ?></div>
            <?php endif; ?>

            <!-- Theme name overlay -->
            <div class="theme-card__name-overlay">
                <strong><?= e($theme['name']) ?></strong>
                <span>v<?= e($theme['version']) ?></span>
            </div>
        </div>

        <!-- Meta -->
        <div class="theme-card__body">
            <p class="theme-card__description">
                <?= !empty($theme['description']) ? e($theme['description']) : '<em class="text-muted">' . __('admin.themes.no_description') . '</em>' ?>
            </p>
            <div class="theme-card__meta text-muted">
                <span><?= __('admin.themes.by_author', [':author' => e($theme['author'] ?: 'Unknown')]) ?></span>
                &nbsp;·&nbsp;
                <span><?= __('admin.themes.slug_label', [':slug' => e($slug)]) ?></span>
            </div>
        </div>

        <!-- Actions -->
        <div class="theme-card__footer">
            <?php if ($isActive): ?>
            <span class="btn btn-success" style="cursor:default;">✓ <?= __('admin.themes.currently_active') ?></span>
            <?php else: ?>
            <form method="POST" action="/admin/themes/activate"
                  onsubmit="return confirm('<?= __('admin.themes.confirm_activate', [':name' => e(addslashes($theme['name']))]) ?>')">
                <?= $csrf->field() ?>
                <input type="hidden" name="theme" value="<?= e($slug) ?>">
                <button type="submit" class="btn btn-primary"><?= __('admin.themes.activate') ?></button>
            </form>
            <?php endif; ?>

            <a href="/" target="_blank" class="btn btn-secondary btn-sm" title="<?= __('admin.themes.view_site') ?>"><?= __('admin.themes.view_site') ?></a>

            <?php if (!$isActive && !$isProtected): ?>
            <form method="POST" action="/admin/themes/uninstall"
                  onsubmit="return confirm('<?= __('admin.themes.confirm_uninstall', [':name' => e(addslashes($theme['name']))]) ?>')">
                <?= $csrf->field() ?>
                <input type="hidden" name="theme" value="<?= e($slug) ?>">
                <button type="submit" class="btn btn-danger btn-sm"><?= __('admin.themes.uninstall') ?></button>
            </form>
            <?php elseif ($isProtected && !$isActive): ?>
            <span class="btn btn-secondary btn-sm theme-protected-hint"
                  title="<?= __('admin.themes.protected_hint') ?>"><?= __('admin.themes.protected') ?></span>
            <?php endif; ?>
        </div>

    </div>
    <?php endforeach; ?>

    <?php if (empty($themes)): ?>
    <p class="text-muted"><?= __('admin.themes.no_themes') ?></p>
    <?php endif; ?>
</div>

<style>
.theme-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 1.5rem;
    margin-top: 1rem;
}

.theme-card {
    position: relative;
    border: 2px solid var(--border-color, #e0e0e0);
    border-radius: 8px;
    overflow: hidden;
    background: #fff;
    transition: box-shadow .2s, border-color .2s;
}
.theme-card:hover { box-shadow: 0 4px 16px rgba(0,0,0,.1); }
.theme-card--active { border-color: var(--primary, #2563eb); }

.theme-card__badge {
    position: absolute;
    top: .6rem; right: .6rem;
    background: var(--primary, #2563eb);
    color: #fff;
    font-size: .7rem;
    font-weight: 700;
    padding: .2em .6em;
    border-radius: 12px;
    text-transform: uppercase;
    letter-spacing: .05em;
    z-index: 2;
}

.theme-card__preview {
    position: relative;
    height: 140px;
    background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

/* Thumbnail image preview */
.theme-card__thumbnail {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.theme-color-bar {
    display: flex;
    gap: 8px;
    align-items: center;
}
.theme-color-bar--empty { font-size: .8rem; color: #aaa; }

.theme-color-dot {
    display: block;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 2px solid rgba(255,255,255,.6);
    box-shadow: 0 2px 6px rgba(0,0,0,.15);
}

.theme-card__name-overlay {
    position: absolute;
    bottom: 0; left: 0; right: 0;
    background: linear-gradient(transparent, rgba(0,0,0,.55));
    color: #fff;
    padding: .4rem .75rem;
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    font-size: .85rem;
    z-index: 1;
}
.theme-card__name-overlay span { font-size: .75rem; opacity: .8; }

.theme-card__body {
    padding: .85rem 1rem .6rem;
}
.theme-card__description {
    font-size: .88rem;
    margin: 0 0 .5rem;
    min-height: 2.5em;
}
.theme-card__meta { font-size: .78rem; }
.theme-card__meta code { font-size: .85em; background: #f0f0f0; padding: .1em .35em; border-radius: 3px; }

.theme-card__footer {
    padding: .75rem 1rem;
    border-top: 1px solid var(--border-color, #e0e0e0);
    display: flex;
    gap: .5rem;
    align-items: center;
    flex-wrap: wrap;
}

.theme-protected-hint {
    opacity: .55;
    cursor: default;
    font-size: .75rem;
}
</style>

<?php $content = ob_get_clean(); include dirname(__DIR__) . '/layouts/admin.php'; ?>
