<div class="image-gallery-component">
    <!-- Gallery Header with Controls -->
    <div class="gallery-header">
        <div class="gallery-header-left">
            <h3 class="gallery-title">Image Gallery</h3>
            <p class="gallery-subtitle">{{showing_text}}</p>
        </div>

        <div class="gallery-header-right" if="has_selection">
            <span class="selection-count">{{selection_count}} selected</span>
            <button
                class="btn btn-danger btn-sm"
                data-live-action="deleteSelected"
                title="Delete selected images"
            >
                🗑️ Delete Selected
            </button>
            <button
                class="btn btn-secondary btn-sm"
                data-live-action="clearSelection"
                title="Clear selection"
            >
                Clear
            </button>
        </div>
    </div>

    <!-- Search and Filter Controls -->
    <div class="gallery-controls">
        <!-- Search -->
        <div class="control-group search-group">
            <label>Search:</label>
            <input
                type="text"
                class="search-input"
                placeholder="Search by filename or alt text..."
                value="{{search_query}}"
                data-live-action="search"
                data-param-query-from="value"
                data-debounce="500"
            />
        </div>

        <!-- Filter by Type -->
        <div class="control-group">
            <label>Filter by Type:</label>
            <select
                class="filter-select"
                data-live-action="filterByType"
                data-param-mime-type-from="value"
            >
                <for items="mime_type_options" as="option">
                    <option value="{{option.value}}" {{option.selected}}>
                        {{option.label}}
                    </option>
                </for>
            </select>
        </div>

        <!-- Sort -->
        <div class="control-group">
            <label>Sort By:</label>
            <select
                class="sort-select"
                data-live-action="sortBy"
                data-param-field-from="value"
                data-param-direction="{{sort_direction}}"
            >
                <for items="sort_options" as="option">
                    <option value="{{option.value}}" {{option.selected}}>
                        {{option.label}}
                    </option>
                </for>
            </select>
            <button
                class="btn-icon"
                data-live-action="toggleSortDirection"
                title="Toggle sort direction"
            >
                {{sort_direction_icon}}
            </button>
        </div>
    </div>

    <!-- Image Grid -->
    <div class="gallery-grid" if="has_images">
        <for items="prepared_images" as="image">
            <div class="gallery-item {{image.selected_class}}">
                <!-- Selection Checkbox -->
                <div class="item-selection" if="can_select">
                    <input
                        type="checkbox"
                        class="selection-checkbox"
                        data-live-action="toggleSelection"
                        data-param-image-ulid="{{image.ulid}}"
                        checked="{{image.is_selected}}"
                    />
                </div>

                <!-- Image -->
                <div class="item-image">
                    <img
                        src="{{image.thumbnail_url}}"
                        alt="{{image.alt_text}}"
                        loading="lazy"
                    />
                    <div class="item-overlay">
                        <div class="item-actions">
                            <button
                                class="action-btn"
                                title="View details"
                                data-image-ulid="{{image.ulid}}"
                            >
                                👁️
                            </button>
                            <button
                                class="action-btn"
                                data-live-action="deleteImage"
                                data-param-image-ulid="{{image.ulid}}"
                                title="Delete image"
                                if="can_delete"
                            >
                                🗑️
                            </button>
                        </div>
                    </div>
                </div>

                <!-- Image Info -->
                <div class="item-info">
                    <div class="item-name" title="{{image.original_filename}}">
                        {{image.filename}}
                    </div>
                    <div class="item-meta">
                        <span class="meta-item">{{image.mime_type_label}}</span>
                        <span class="meta-item">{{image.dimensions}}</span>
                        <span class="meta-item">{{image.file_size_formatted}}</span>
                    </div>
                </div>
            </div>
        </for>
    </div>

    <!-- Empty State -->
    <div class="gallery-empty" if="is_empty">
        <div class="empty-icon">📷</div>
        <h3 class="empty-title">No Images Found</h3>
        <p class="empty-text">
            <span if="search_query">No images match your search "{{search_query}}".</span>
            <span if="!search_query">Upload some images to get started.</span>
        </p>
    </div>

    <!-- Load More Button -->
    <div class="gallery-pagination" if="has_more">
        <button
            class="btn btn-primary load-more-btn"
            data-live-action="loadMore"
        >
            Load More Images
        </button>
    </div>
</div>

<style>
.image-gallery-component {
    background: white;
    border-radius: 8px;
    padding: 1.5rem;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

/* Header */
.gallery-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    padding-bottom: 1rem;
    border-bottom: 2px solid #e0e0e0;
    flex-wrap: wrap;
    gap: 1rem;
}

.gallery-header-left {
    flex: 1;
}

.gallery-title {
    margin: 0 0 0.25rem 0;
    font-size: 1.5rem;
    font-weight: 600;
    color: #333;
}

.gallery-subtitle {
    margin: 0;
    font-size: 0.875rem;
    color: #666;
}

.gallery-header-right {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.selection-count {
    font-size: 0.875rem;
    font-weight: 600;
    color: #2196F3;
    padding: 0.5rem 1rem;
    background: #E3F2FD;
    border-radius: 4px;
}

/* Controls */
.gallery-controls {
    display: flex;
    gap: 1rem;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
    padding: 1rem;
    background: #f9f9f9;
    border-radius: 6px;
}

.control-group {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.control-group label {
    font-weight: 600;
    font-size: 0.875rem;
    color: #555;
}

.search-group {
    flex: 1;
    min-width: 250px;
}

.search-input,
.filter-select,
.sort-select {
    padding: 0.5rem;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 0.875rem;
    transition: border-color 0.2s;
}

.search-input {
    flex: 1;
    min-width: 200px;
}

.search-input:focus,
.filter-select:focus,
.sort-select:focus {
    outline: none;
    border-color: #2196F3;
}

.btn-icon {
    padding: 0.5rem;
    border: 1px solid #ddd;
    background: white;
    border-radius: 4px;
    cursor: pointer;
    font-size: 1rem;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2.5rem;
    height: 2.5rem;
}

.btn-icon:hover {
    background: #f5f5f5;
    border-color: #2196F3;
}

/* Gallery Grid */
.gallery-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 1.5rem;
    margin-bottom: 1.5rem;
}

.gallery-item {
    background: white;
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    overflow: hidden;
    transition: all 0.2s;
    position: relative;
}

.gallery-item:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.gallery-item.gallery__item--selected {
    border-color: #2196F3;
    box-shadow: 0 0 0 2px rgba(33, 150, 243, 0.2);
}

/* Selection */
.item-selection {
    position: absolute;
    top: 0.5rem;
    left: 0.5rem;
    z-index: 10;
}

.selection-checkbox {
    width: 1.25rem;
    height: 1.25rem;
    cursor: pointer;
}

/* Image */
.item-image {
    position: relative;
    aspect-ratio: 16 / 10;
    overflow: hidden;
    background: #f5f5f5;
}

.item-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.2s;
}

.gallery-item:hover .item-image img {
    transform: scale(1.05);
}

.item-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.2s;
}

.gallery-item:hover .item-overlay {
    opacity: 1;
}

.item-actions {
    display: flex;
    gap: 0.5rem;
}

.action-btn {
    background: rgba(255, 255, 255, 0.9);
    border: none;
    width: 2.5rem;
    height: 2.5rem;
    border-radius: 50%;
    cursor: pointer;
    font-size: 1.125rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.action-btn:hover {
    background: white;
    transform: scale(1.1);
}

/* Info */
.item-info {
    padding: 1rem;
}

.item-name {
    font-weight: 500;
    margin-bottom: 0.5rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 0.875rem;
    color: #333;
}

.item-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.meta-item {
    background: #f0f0f0;
    padding: 0.125rem 0.5rem;
    border-radius: 3px;
    font-size: 0.75rem;
    color: #666;
}

/* Empty State */
.gallery-empty {
    text-align: center;
    padding: 4rem 2rem;
    color: #666;
}

.empty-icon {
    font-size: 4rem;
    margin-bottom: 1rem;
}

.empty-title {
    margin: 0 0 0.5rem 0;
    font-size: 1.25rem;
    color: #333;
}

.empty-text {
    margin: 0;
    font-size: 0.875rem;
}

/* Pagination */
.gallery-pagination {
    text-align: center;
    margin-top: 1.5rem;
}

.load-more-btn {
    padding: 0.75rem 2rem;
    font-size: 0.875rem;
}

/* Buttons */
.btn {
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 4px;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}

.btn-primary {
    background: #2196F3;
    color: white;
}

.btn-primary:hover {
    background: #1976D2;
}

.btn-secondary {
    background: #e0e0e0;
    color: #333;
}

.btn-secondary:hover {
    background: #d0d0d0;
}

.btn-danger {
    background: #f44336;
    color: white;
}

.btn-danger:hover {
    background: #d32f2f;
}

.btn-sm {
    padding: 0.375rem 0.75rem;
    font-size: 0.8125rem;
}

/* Responsive */
@media (max-width: 768px) {
    .gallery-header {
        flex-direction: column;
        align-items: stretch;
    }

    .gallery-controls {
        flex-direction: column;
    }

    .control-group {
        flex-direction: column;
        align-items: stretch;
    }

    .gallery-grid {
        grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
        gap: 1rem;
    }
}

@media (max-width: 480px) {
    .gallery-grid {
        grid-template-columns: 1fr;
    }
}
</style>
