/* =======================================
   模板画廊样式 (gallery.css)
   ======================================= */

/* 画廊容器 */
.gallery-container {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    padding: 20px;
    box-sizing: border-box;
    overflow-y: auto;
    background: #f8f9fa;
    -webkit-overflow-scrolling: touch;
    /* Smooth scrolling for iOS */
    overscroll-behavior: contain;
    /* Prevent scroll chaining */
}

.gallery-container.hidden {
    display: none;
}

/* 搜索栏 */
.gallery-search-bar {
    margin-bottom: 16px;
    flex-shrink: 0;
}

.gallery-search-bar input {
    width: 100%;
    max-width: 400px;
    padding: 12px 16px;
    border: 1px solid #e5e7eb;
    border-radius: 24px;
    font-size: 14px;
    background: #fff;
    transition: all 0.2s;
}

.gallery-search-bar input:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.gallery-search-bar input::placeholder {
    color: #9ca3af;
}

/* 分类标签栏 */
.gallery-categories {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 20px;
    flex-shrink: 0;

    /* Sticky Header Logic */
    position: sticky;
    top: -20px;
    /* Compensate for container padding if needed, or 0 */
    /* Wait, container has padding 20px. If I want it to stick to the very top visually */
    /* But standard sticky respects container. top:0 is safe. */
    top: 0;
    z-index: 10;
    background: rgba(248, 249, 250, 0.95);
    /* Nearly opaque background */
    backdrop-filter: blur(8px);

    /* Full width trick for padded container */
    margin-left: -20px;
    margin-right: -20px;
    padding: 10px 20px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.category-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: 1px solid #e5e7eb;
    border-radius: 20px;
    background: #fff;
    color: #4b5563;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.category-chip:hover {
    background: #f3f4f6;
    border-color: #d1d5db;
}

.category-chip.active {
    background: #111;
    color: #fff;
    border-color: #111;
}

.chip-icon {
    font-size: 14px;
}

/* 瀑布流网格 -> 改为 Grid 布局以保证横向顺序 */
.gallery-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 16px;
    align-items: start;
    /* 顶部对齐，避免卡片被拉伸 */
    padding-bottom: 40px;
}

/* 移动端优化：增加底部内边距，防止被微信工具栏遮挡 */
@media (max-width: 800px) {
    .gallery-grid {
        padding-bottom: 250px;
    }
}

/* Load More Button (Fallback for Infinite Scroll) */
.load-more-btn {
    display: block;
    width: 100%;
    padding: 12px;
    margin: 20px 0 100px 0;
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    color: #4b5563;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    text-align: center;
    transition: all 0.2s;
}

.load-more-btn:hover {
    background: #f9fafb;
    border-color: #d1d5db;
}

.load-more-btn.hidden {
    display: none;
}

/* 模板卡片 */
.gallery-card {
    /* break-inside: avoid; No longer needed for Grid */
    /* margin-bottom: 16px; Handled by grid gap */
    background: #fff;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    cursor: pointer;
    transition: all 0.2s ease;
}

.gallery-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 12px 24px rgba(0, 0, 0, 0.12);
}

/* 卡片图片区域 */
.card-image {
    position: relative;
    aspect-ratio: 3 / 4;
    background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%);
    overflow: hidden;
}

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

.card-placeholder {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 48px;
    opacity: 0.5;
}

/* 悬浮遮罩 */
.card-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-card:hover .card-overlay {
    opacity: 1;
}

.use-template-btn {
    background: #fff;
    color: #111;
    padding: 10px 20px;
    border: none;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.use-template-btn:hover {
    transform: scale(1.05);
    background: #f0f0f0;
}

/* 卡片内容 */
.card-content {
    padding: 12px;
}

.card-title {
    font-size: 14px;
    font-weight: 600;
    color: #111;
    margin: 0 0 8px 0;
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.card-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.card-tag {
    font-size: 11px;
    color: #6b7280;
    background: #f3f4f6;
    padding: 2px 8px;
    border-radius: 10px;
}

/* 空状态 */
.gallery-empty {
    grid-column: 1 / -1;
    text-align: center;
    padding: 60px 20px;
    color: #9ca3af;
}

.empty-icon {
    font-size: 48px;
    display: block;
    margin-bottom: 12px;
}

.gallery-empty p {
    font-size: 14px;
    margin: 0;
}

/* 返回画廊按钮 */
#back-to-gallery-btn {
    position: absolute;
    top: 20px;
    left: 20px;
    z-index: 200;
    background: rgba(43, 45, 49, 0.85);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: #fff;
    padding: 10px 18px;
    border-radius: 24px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 6px;
}

#back-to-gallery-btn:hover {
    background: rgba(255, 255, 255, 0.9);
    color: #111;
    transform: translateY(-1px);
}

#back-to-gallery-btn.hidden {
    display: none;
}

/* 响应式调整 */
@media (max-width: 800px) {
    .gallery-container {
        padding: 12px;
    }

    .gallery-search-bar input {
        max-width: 100%;
    }

    .gallery-categories {
        flex-wrap: nowrap;
        overflow-x: auto;
        padding-bottom: 8px;
        -webkit-overflow-scrolling: touch;
    }

    .category-chip {
        flex-shrink: 0;
    }

    .gallery-grid {
        grid-template-columns: repeat(2, 1fr);
        /* 手机端强制两列 */
        gap: 10px;
    }

    .gallery-card {
        /* margin-bottom: 10px; Handled by gap */
        border-radius: 8px;
    }

    .card-content {
        padding: 8px;
    }

    .card-title {
        font-size: 12px;
    }

    .card-tag {
        font-size: 10px;
    }

    /* 移动端点击即可使用，不需要悬浮 */
    .card-overlay {
        opacity: 0;
        background: rgba(0, 0, 0, 0.3);
    }

    .gallery-card:active .card-overlay {
        opacity: 1;
    }

    #back-to-gallery-btn {
        top: 10px;
        left: 10px;
        padding: 8px 14px;
        font-size: 12px;
    }
}

/* 加载指示器 */
.gallery-loader {
    grid-column: 1 / -1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    gap: 10px;
    color: #6b7280;
    font-size: 14px;
}

.gallery-loader.hidden {
    display: none;
}

.loader-spinner {
    width: 20px;
    height: 20px;
    border: 2px solid #e5e7eb;
    border-top-color: #3b82f6;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}