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

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

.image-card {
    display: block;
    border-radius: var(--radius-md);
    overflow: hidden;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    cursor: pointer;
    text-decoration: none;
    position: relative;
    aspect-ratio: 1;
    background: var(--bg-muted);
}

.image-card:hover,
.image-card.kb-focus {
    transform: scale(1.02);
    box-shadow: var(--shadow-md);
    z-index: 2;
}

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

.image-card::after {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(to top, rgba(0, 0, 0, 0.2) 0%, transparent 30%);
    opacity: 0;
    transition: opacity 0.2s ease;
    pointer-events: none;
}

.image-card:hover::after,
.image-card.kb-focus::after {
    opacity: 1;
}

.card-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

/* 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.loading-card .card-skeleton {
    display: block;
}

.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: 24px;
    height: 24px;
    border: 2.5px solid var(--border-default);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

.end-message {
    text-align: center;
    padding: 32px 0;
    font-size: 13px;
    color: var(--text-muted);
}

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

.search-toggle-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--text-secondary, #6b7280);
    padding: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm, 8px);
    transition: all 0.2s ease;
    flex-shrink: 0;
}

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

.search-container.expanded .search-toggle-btn {
    color: var(--accent, #6366f1);
}

.search-box {
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    background: var(--bg-surface, white);
    border: 1px solid var(--border-default, #e5e7eb);
    border-radius: 20px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06), 0 1px 3px rgba(0, 0, 0, 0.04);
    overflow: hidden;
    width: 0;
    opacity: 0;
    padding: 0;
    transition: width 0.25s cubic-bezier(0.4, 0, 0.2, 1),
                opacity 0.2s ease,
                padding 0.25s ease,
                box-shadow 0.2s ease;
    pointer-events: none;
}

.search-container.expanded .search-box {
    width: 260px;
    opacity: 1;
    padding: 4px 6px 4px 14px;
    pointer-events: auto;
}

.search-container.expanded .search-box:focus-within {
    border-color: var(--accent, #6366f1);
    box-shadow: 0 4px 16px rgba(99, 102, 241, 0.1), 0 0 0 3px rgba(99, 102, 241, 0.08);
}

.search-input {
    flex: 1;
    padding: 6px 0;
    border: none;
    background: transparent;
    color: var(--text-primary, #111827);
    font-size: 0.8125rem;
    font-family: inherit;
    outline: none;
    min-width: 0;
}

.search-input::placeholder {
    color: var(--text-muted, #9ca3af);
}

.search-clear {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted, #9ca3af);
    padding: 4px;
    display: none;
    align-items: center;
    justify-content: center;
    transition: color 0.15s ease;
    flex-shrink: 0;
}

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

.search-clear:hover {
    color: var(--text-primary, #111827);
}

/* ─── Stats ─── */

/* ─── Upload Tags ─── */
.upload-tags-container {
    position: relative;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.375rem;
}

.upload-option-tags {
    flex-wrap: wrap;
    gap: 10px;
}

.upload-option-tags .upload-tags-container {
    width: 100%;
    padding-left: 26px;
    padding-top: 2px;
}

.upload-tags-selected {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem;
}

.upload-tag-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.25rem 0.625rem;
    background: var(--bg-surface);
    border: 1px solid var(--border-light);
    border-radius: 20px;
    font-size: 0.7rem;
    color: var(--text-primary);
}

.upload-tag-chip-remove {
    cursor: pointer;
    font-size: 0.875rem;
    line-height: 1;
    color: var(--text-muted);
}

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

.upload-tag-add-btn {
    background: transparent;
    border: 1px dashed var(--border-default);
    color: var(--text-muted);
    cursor: pointer;
    min-width: 26px;
    padding: 5px 10px;
    border-radius: 6px;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
}

.upload-tag-add-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: var(--accent-subtle);
}

.upload-tag-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    margin-top: 6px;
    background: var(--bg-surface);
    border: 1px solid var(--border-light);
    border-radius: 10px;
    padding: 0.5rem;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
    z-index: 100;
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem;
    max-width: 280px;
}

.upload-tag-dropdown.hidden {
    display: none;
}

.upload-tag-dropdown-item {
    padding: 0.25rem 0.625rem;
    border-radius: 20px;
    font-size: 0.7rem;
    cursor: pointer;
    color: var(--text-primary);
    border: 1px solid var(--border-light);
    transition: all 0.15s;
}

.upload-tag-dropdown-item:hover {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

.upload-tag-dropdown-empty {
    font-size: 0.75rem;
    color: var(--text-secondary);
    padding: 0.25rem 0.5rem;
}

/* ─── Responsive ─── */
@media (max-width: 640px) {
    .top-bar {
        padding: 0 12px;
        height: 52px;
        flex-wrap: wrap;
    }

    .search-container.expanded {
        position: fixed;
        inset: 0;
        z-index: 200;
        background: rgba(0, 0, 0, 0.5);
        display: flex;
        align-items: flex-start;
        justify-content: center;
        padding: 60px 16px 0;
    }

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

    .search-container.expanded .search-box {
        position: relative;
        top: auto;
        left: auto;
        right: auto;
        width: 100%;
        max-width: 400px;
        border-radius: 14px;
        transform: none;
        opacity: 1;
        pointer-events: auto;
        padding: 10px 12px 10px 16px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
    }

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

    .search-container.expanded .search-clear {
        padding: 8px;
    }

    .btn-upload-text {
        display: none;
    }

    .btn-upload {
        padding: 8px 10px;
        gap: 0;
    }

    .category-nav-desktop {
        display: none !important;
    }

    .category-nav-mobile {
        display: block !important;
    }

    .gallery-main {
        padding: 8px;
    }

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

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

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


    .upload-modal {
        margin: 12px;
        padding: 22px;
        max-height: 90vh;
        overflow-y: auto;
    }

    .file-input-label {
        padding: 24px 16px;
    }

    .button-group {
        flex-direction: column;
    }

    #uploadBtn, #clearBtn {
        width: 100%;
    }
}

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

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

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

@media (min-width: 641px) {
    .search-container.expanded {
        position: relative;
        inset: auto;
        z-index: auto;
        background: none;
        backdrop-filter: none;
        padding: 0;
    }

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

    .search-container.expanded .search-box {
        width: 280px;
        position: absolute;
        right: 0;
        padding: 4px 6px 4px 14px;
    }

    .search-container.expanded .search-input {
        font-size: 0.8125rem;
        padding: 6px 0;
    }
}

@media (min-width: 1400px) {
    .gallery-grid {
        grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    }
}

/* ─── More Menu (mobile) ─── */
.more-menu-btn {
    display: none;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    padding: 8px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.2s ease;
}

.more-menu-btn.active {
    background: var(--bg-hover);
    color: var(--text-primary);
}

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

.more-dropdown {
    display: none;
    position: fixed;
    top: 52px;
    right: 12px;
    background: var(--bg-surface);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    min-width: 180px;
    z-index: 100;
    animation: dropIn 0.12s ease;
}

.more-dropdown.open {
    display: block;
}

.more-dropdown-item {
    display: block;
    width: 100%;
    text-align: left;
    padding: 12px 16px;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-primary);
    font-size: 13px;
    font-weight: 500;
    text-decoration: none;
    transition: all 0.2s ease;
    border-radius: 0;
}

.more-dropdown-item:hover {
    background: var(--bg-hover);
}

.more-dropdown-divider {
    height: 1px;
    background: var(--border-light);
}

.more-dropdown-email {
    display: block;
    padding: 10px 16px;
    font-size: 12px;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border-light);
}

.more-dropdown-logout {
    color: #ef4444;
}

.more-dropdown-theme {
    border-top: 1px solid var(--border-light);
}

.theme-label-dark {
    display: none;
}

:root[data-theme="dark"] .theme-label-light {
    display: none;
}

:root[data-theme="dark"] .theme-label-dark {
    display: inline;
}

/* ─── Login Modal ─── */
.login-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.15s ease;
}

.login-overlay:not(.hidden) {
    display: flex;
}

:root[data-theme="dark"] .login-overlay {
    background: rgba(0, 0, 0, 0.6);
}

.login-modal {
    background: var(--bg-surface);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-lg);
    padding: 24px;
    width: 100%;
    max-width: 380px;
    box-shadow: var(--shadow-xl);
    position: relative;
    animation: modalIn 0.2s ease;
}

:root[data-theme="dark"] .login-modal {
    background: rgba(24, 24, 27, 0.98);
    border-color: var(--border-default);
}

.login-title {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0 0 18px 0;
    text-align: center;
}

.login-close-btn {
    position: absolute;
    top: 14px;
    right: 14px;
    background: var(--bg-hover);
    border: none;
    font-size: 18px;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px 10px;
    border-radius: var(--radius-sm);
    line-height: 1;
    transition: all 0.2s ease;
}

.login-close-btn:hover {
    color: var(--text-primary);
    background: var(--bg-muted);
}

.login-form {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.login-form-group {
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.login-form-group label {
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
}

.login-form-group input {
    padding: 10px 12px;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 14px;
    font-family: inherit;
    transition: all 0.2s ease;
}

.login-form-group input:hover {
    border-color: var(--text-muted);
}

.login-form-group input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

.login-form-group input::placeholder {
    color: var(--text-muted);
}

.login-error {
    font-size: 13px;
    color: #ef4444;
    min-height: 18px;
}

.login-submit {
    width: 100%;
    padding: 10px 16px;
    margin-top: 4px;
    font-size: 14px;
    font-weight: 500;
}

.login-submit:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.auth-toggle-text {
    text-align: center;
    font-size: 13px;
    color: var(--text-secondary);
    margin-top: 16px;
}

.auth-toggle-btn {
    background: none;
    border: none;
    color: var(--accent);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    padding: 0;
    margin-left: 4px;
}

.auth-toggle-btn:hover {
    color: var(--accent-hover);
    text-decoration: underline;
}

.forgot-description {
    font-size: 13px;
    color: var(--text-secondary);
    line-height: 1.5;
    margin-bottom: 4px;
}

.login-success {
    font-size: 13px;
    color: #10b981;
    min-height: 18px;
}

:root[data-theme="dark"] .login-success {
    color: #6ee7b7;
}

/* ─── My Images Page ─── */
.my-images-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0 auto;
}

#anonymousToggleWrapper {
    display: none;
}

@media (max-width: 640px) {
    .login-modal {
        margin: 12px;
        max-width: none;
    }
}

/* ─── User Menu ─── */
.user-menu-btn {
    position: relative;
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--text-secondary);
    padding: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    transition: all 0.2s ease;
}

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

.user-menu-btn.active {
    color: var(--text-primary);
    background: var(--bg-hover);
}

.user-dropdown {
    display: none;
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    background: var(--bg-surface);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    min-width: 200px;
    z-index: 100;
    animation: dropIn 0.12s ease;
}

.user-menu-btn.active ~ .user-dropdown {
    display: block;
}

.user-email {
    padding: 12px 16px;
    color: var(--text-secondary);
    font-size: 13px;
    border-bottom: 1px solid var(--border-light);
    word-break: break-all;
}

.user-email-btn {
    width: 100%;
    text-align: left;
    padding: 10px 16px;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-secondary);
    font-size: 13px;
    font-weight: 400;
    border-radius: 0;
    transition: all 0.2s ease;
    border-bottom: 1px solid var(--border-light);
}

.user-email-btn:hover {
    background: var(--bg-hover);
    color: var(--accent);
}

.user-dropdown-divider {
    height: 1px;
    background: var(--border-light);
    margin: 4px 0;
}

.user-view-btn {
    width: 100%;
    text-align: left;
    padding: 10px 16px;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-primary);
    font-size: 13px;
    font-weight: 500;
    border-radius: 0;
    transition: all 0.2s ease;
}

.user-view-btn:hover {
    background: var(--bg-hover);
}

.user-view-btn.active {
    color: var(--accent);
    background: var(--accent-subtle);
}

.user-dropdown-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    color: var(--text-primary);
    font-size: 13px;
    cursor: pointer;
    user-select: none;
    transition: all 0.2s ease;
}

.user-dropdown-item:hover {
    background: var(--bg-hover);
}

.user-dropdown-item input {
    accent-color: var(--accent);
    cursor: pointer;
}

.user-dropdown-link {
    display: block;
    padding: 10px 16px;
    color: var(--text-primary);
    font-size: 13px;
    text-decoration: none;
    transition: all 0.2s ease;
}

.user-dropdown-link:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.logout-btn {
    width: 100%;
    text-align: left;
    padding: 10px 16px;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-primary);
    font-size: 13px;
    font-weight: 500;
    border-radius: 0;
    transition: all 0.2s ease;
}

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

/* ─── Mobile Overrides (must be last to win cascade) ─── */
@media (max-width: 640px) {
    .user-menu-btn {
        display: none;
    }

    .theme-toggle-btn {
        display: none;
    }

    .top-bar-right > a.icon-btn[href="/stats.html"],
    .top-bar-right > a.icon-btn[href="/disclaimer.html"] {
        display: none;
    }

    .more-menu-btn {
        display: flex;
        min-width: 44px;
        min-height: 44px;
    }
}
