/* ─── Gallery ─── */
.gallery-main {
    max-width: 1600px;
    margin: 0 auto;
    padding: 20px 24px 48px;
}

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

.image-card {
    position: relative;
    display: block;
    aspect-ratio: 1;
    overflow: hidden;
    border-radius: var(--radius-md);
    background: var(--bg-muted);
    cursor: pointer;
    text-decoration: none;
    isolation: isolate;
}

.image-card::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: inherit;
    box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.06);
    background-color: transparent;
    pointer-events: none;
    transition: background-color 0.2s var(--ease);
}

:root[data-theme="dark"] .image-card::after {
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.06);
}

.image-card:hover::after,
.image-card.kb-focus::after {
    background-color: rgba(0, 0, 0, 0.08);
}

.image-card.kb-focus {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.card-image {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.4s var(--ease), opacity 0.3s ease;
}

.image-card:hover .card-image,
.image-card.kb-focus .card-image {
    transform: scale(1.04);
}

/* Loading skeleton */
.image-card.loading-card .card-image {
    opacity: 0;
}

.card-skeleton {
    position: absolute;
    inset: 0;
    background: linear-gradient(90deg, var(--bg-muted) 0%, var(--bg-hover) 50%, var(--bg-muted) 100%);
    background-size: 200% 100%;
    animation: loading-pulse 1.5s ease-in-out infinite;
}

.image-card:not(.loading-card) .card-skeleton {
    display: none;
}

@keyframes loading-pulse {
    0%, 100% { background-position: 200% 0; }
    50% { background-position: 0 0; }
}

/* Scroll loader */
.scroll-loader {
    display: flex;
    justify-content: center;
    padding: 40px 0;
}

.scroll-loader-spinner {
    width: 22px;
    height: 22px;
    border: 2px solid var(--border-default);
    border-top-color: var(--text-secondary);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

.end-message {
    display: flex;
    align-items: center;
    gap: 16px;
    max-width: 360px;
    margin: 0 auto;
    padding: 40px 0 8px;
    font-size: 12px;
    font-weight: 500;
    color: var(--text-muted);
    white-space: nowrap;
}

.end-message::before,
.end-message::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border-default);
}

/* ─── Search ─── */
.search-container {
    position: relative;
    display: flex;
    align-items: center;
    margin-left: auto;
}

.search-container + .top-bar-right {
    margin-left: 0;
}

.search-toggle-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    height: 36px;
    padding: 0 8px 0 10px;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-secondary);
}

.search-toggle-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.search-box {
    position: absolute;
    top: 50%;
    right: 0;
    z-index: 2;
    display: flex;
    align-items: center;
    gap: 8px;
    width: 0;
    height: 40px;
    padding: 0 8px 0 12px;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
    background: var(--bg-surface);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-md);
    transform: translateY(-50%);
    transition: width 0.25s var(--ease), opacity 0.15s ease, border-color 0.15s var(--ease), box-shadow 0.15s var(--ease);
}

.search-container.expanded .search-box {
    width: min(360px, 40vw);
    opacity: 1;
    pointer-events: auto;
}

.search-box:focus-within {
    border-color: var(--accent);
    box-shadow: var(--shadow-md), 0 0 0 3px var(--accent-glow);
}

.search-icon {
    flex-shrink: 0;
    color: var(--text-muted);
}

.search-input {
    flex: 1;
    min-width: 0;
    height: 100%;
    padding: 0;
    border: none;
    background: transparent;
    color: var(--text-primary);
    font-size: 14px;
    font-family: inherit;
    outline: none;
}

.search-input::-webkit-search-cancel-button {
    -webkit-appearance: none;
}

.search-input::placeholder {
    color: var(--text-muted);
}

.search-kbd {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 20px;
    height: 20px;
    padding: 0 5px;
    border: 1px solid var(--border-default);
    border-radius: 5px;
    background: var(--bg-input);
    color: var(--text-muted);
    font-family: inherit;
    font-size: 11px;
    font-weight: 500;
}

.search-clear {
    display: none;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 26px;
    height: 26px;
    padding: 0;
    border-radius: var(--radius-sm);
    background: none;
    color: var(--text-muted);
}

.search-clear.visible {
    display: flex;
}

.search-clear:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

/* ─── Upload Tags ─── */
.upload-option-tags {
    flex-wrap: wrap;
    cursor: default;
}

.upload-tags-container {
    position: relative;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
    width: 100%;
    padding-left: 28px;
}

.upload-tags-selected {
    display: contents;
}

.upload-tag-chip,
.upload-tag-dropdown-item {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    height: 26px;
    padding: 0 10px;
    border: 1px solid var(--border-default);
    border-radius: 999px;
    background: var(--bg-surface);
    color: var(--text-primary);
    font-size: 12px;
    font-weight: 500;
}

.upload-tag-chip .tag-icon,
.upload-tag-dropdown-item .tag-icon {
    width: 13px;
    height: 13px;
    flex-shrink: 0;
}

.upload-tag-chip-remove {
    margin-left: 2px;
    cursor: pointer;
    font-size: 15px;
    line-height: 1;
    color: var(--text-muted);
}

.upload-tag-chip-remove:hover {
    color: var(--text-primary);
}

.upload-tag-add-btn {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    height: 26px;
    padding: 0 10px;
    border: 1px dashed var(--border-default);
    border-radius: 999px;
    background: transparent;
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 500;
}

.upload-tag-add-btn:hover {
    border-color: var(--text-muted);
    color: var(--text-primary);
}

.upload-tag-dropdown {
    width: 100%;
    margin-top: 2px;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 8px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    animation: dropIn 0.14s var(--ease);
}

.upload-tag-dropdown-item {
    cursor: pointer;
}

.upload-tag-dropdown-item:hover {
    border-color: var(--text-muted);
    background: var(--bg-hover);
}

.upload-tag-dropdown-empty {
    font-size: 12px;
    color: var(--text-muted);
    padding: 4px;
}

#anonymousToggleWrapper {
    display: none;
}

/* ─── Responsive ─── */
@media (min-width: 1600px) {
    .gallery-grid {
        grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    }
}

@media (min-width: 641px) and (max-width: 1024px) {
    .gallery-grid {
        grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
        gap: 10px;
    }

    .gallery-main {
        padding: 16px 16px 40px;
    }
}

@media (max-width: 640px) {
    .gallery-main {
        padding: 8px 8px 32px;
    }

    .gallery-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 4px;
    }

    .image-card {
        border-radius: var(--radius-sm);
    }

    .image-card:hover .card-image {
        transform: none;
    }

    /* backdrop-filter would trap the fixed search overlay inside the header */
    .site-header {
        background: var(--bg-surface);
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
    }

    .search-toggle-btn {
        width: 40px;
        height: 40px;
        padding: 0;
        justify-content: center;
    }

    .search-kbd {
        display: none;
    }

    .search-container.expanded {
        position: fixed;
        inset: 0;
        z-index: 200;
        max-width: none;
        margin: 0;
        padding: 12px;
        background: var(--overlay);
        align-items: flex-start;
    }

    .search-container.expanded .search-toggle-btn {
        display: none;
    }

    .search-container.expanded .search-box {
        position: relative;
        top: auto;
        right: auto;
        width: 100%;
        height: 48px;
        transform: none;
        background: var(--bg-elevated);
        border-color: var(--border-default);
        box-shadow: var(--shadow-xl);
        animation: dropIn 0.15s var(--ease);
    }

    .search-container.expanded .search-input {
        font-size: 16px;
    }

    .search-container.expanded .search-clear {
        width: 36px;
        height: 36px;
    }
}

@media (min-width: 480px) and (max-width: 640px) {
    .gallery-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}
