/* ==========================================
  Variables & Reset
  ==========================================
*/
:root {
    --bg-color: #f8f9fa;
    --text-color: #212529;
    --card-bg: #ffffff;
    --border-color: #dee2e6;
    --shadow-color: rgba(0, 0, 0, 0.06);
    --shadow-color-hover: rgba(0, 0, 0, 0.12);
    --primary-color: #007bff;
    --success-color: #28a745;
    --tag-bg: #e9ecef;
    --main-padding: 24px;
    --grid-gap: 24px;
}

html { box-sizing: border-box; font-size: 16px; }
*, *:before:after { box-sizing: inherit; }

body {
    font-family: "system-ui", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    margin: 0;
    padding: var(--main-padding);
    min-height: 100vh;
    line-height: 1.6;
}

h1 {
    text-align: center;
    font-size: clamp(2rem, 5vw, 3rem);
    font-weight: 700;
    margin-bottom: 1.5em;
    letter-spacing: -0.5px;
}

/* ==========================================
  Controls (Filter + Buttons)
  ==========================================
*/
.controls-container {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    gap: 16px;
    margin-bottom: var(--grid-gap);
    position: relative;
    z-index: 100;
}

#filter-container { 
    margin-bottom: 0;
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 10px;
    width: 100%;
}

.action-btn {
    background-color: var(--success-color);
    color: white;
    padding: 12px 24px;
    font-size: 1rem;
    border: none;
    border-radius: 30px;
    cursor: pointer;
    font-weight: 600;
}
.action-btn:hover {
    background-color: #218838;
    transform: translateY(-1px);
    box-shadow: 0 6px 12px var(--shadow-color-hover);
}

.secondary-btn {
    background-color: #6c757d;
    color: white;
    padding: 12px 24px;
    font-size: 1rem;
    border: none;
    border-radius: 30px;
    cursor: pointer;
    font-weight: 600;
}
.secondary-btn:hover { background-color: #5a6268; }

/* ==========================================
  Filter Dropdown
  ==========================================
*/
.filter-dropdown { position: relative; display: inline-block; }

.filter-dropbtn {
    background-color: var(--card-bg);
    color: var(--text-color);
    padding: 12px 24px;
    font-size: 1rem;
    border: 1px solid var(--border-color);
    border-radius: 30px;
    cursor: pointer;
    font-weight: 600;
    display: flex; align-items: center; gap: 10px;
    box-shadow: 0 4px 6px var(--shadow-color);
    transition: all 0.2s ease;
}
.filter-dropbtn:hover {
    background-color: #f8f9fa;
    box-shadow: 0 6px 12px var(--shadow-color-hover);
    transform: translateY(-1px);
}

.filter-dropdown-content {
    display: none;
    position: absolute; top: 120%; left: 50%;
    /* 這裡透過 transform 置中，所以需要專用的動畫 */
    transform: translateX(-50%);
    background-color: var(--card-bg);
    min-width: 280px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.15);
    border-radius: 12px;
    z-index: 101;
    border: 1px solid var(--border-color);
    max-height: 400px;
    flex-direction: column;
    overflow: hidden;
}

.filter-scroll-container {
    overflow-y: auto;
    padding: 8px;
    width: 100%;
    scrollbar-width: thin;
    scrollbar-color: #adb5bd transparent;
}

/* 修改 1：下拉選單使用專用的 dropdownFadeIn */
.filter-dropdown.active .filter-dropdown-content {
    display: flex;
    animation: dropdownFadeIn 0.2s ease-out;
}

.filter-checkbox-label {
    display: flex; align-items: center;
    padding: 10px 16px; border-radius: 8px;
    cursor: pointer; font-size: 0.95rem;
    transition: background 0.2s; user-select: none;
    color: var(--text-color);
}
.filter-checkbox-label:hover { background-color: #f1f3f5; }

.filter-checkbox-label input[type="checkbox"] {
    margin-right: 12px; width: 18px; height: 18px;
    accent-color: var(--primary-color); cursor: pointer;
}
.filter-checkbox-label:has(input:checked) {
    background-color: rgba(0, 123, 255, 0.06);
    color: var(--primary-color); font-weight: 600;
}

/* 修改 2：拆分動畫定義 */

/* 一般區塊用的淡入 (修正漂移問題) */
@keyframes sectionFadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* 下拉選單專用的淡入 (包含置中邏輯) */
@keyframes dropdownFadeIn {
    from { opacity: 0; transform: translateX(-50%) translateY(-10px); }
    to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ==========================================
  Grid & Cards
  ==========================================
*/
#catalog-container {
    display: block; 
    max-width: 1600px; 
    margin: 0 auto; 
    padding-bottom: 60px;
}

.series-section {
    margin-bottom: 50px;
    /* 修改 3：改用 sectionFadeIn，避免吃到 translateX(-50%) */
    animation: sectionFadeIn 0.3s ease-out;
}

.series-title {
    font-size: 1.6rem;
    font-weight: 700;
    color: #333;
    margin-bottom: 24px;
    padding-left: 15px;
    border-left: 6px solid var(--primary-color);
    line-height: 1.2;
}

.series-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: var(--grid-gap);
    justify-content: center;
}

.catalog-card {
    border-radius: 12px;
    box-shadow: 0 2px 8px var(--shadow-color);
    transition: box-shadow 0.3s ease;
    background-color: var(--card-bg); overflow: hidden;
}
@media (hover: hover) {
    .catalog-card:not(.is-owned):hover { box-shadow: 0 4px 12px var(--shadow-color-hover); }
}

.catalog-image-wrapper {
    width: 100%; aspect-ratio: 1 / 1;
    position: relative; cursor: pointer; overflow: hidden;
}

.catalog-image {
    width: 100%; height: 100%; object-fit: contain;
    display: block; transition: opacity 0.3s ease;
}

.image-count-indicator {
    position: absolute; top: 8px; right: 8px;
    background-color: rgba(0, 0, 0, 0.7); color: white;
    font-size: 0.75rem; font-weight: 600;
    padding: 2px 6px; border-radius: 10px;
    pointer-events: none; z-index: 2;
}

.own-it-btn {
    position: absolute; bottom: 8px; right: 8px; z-index: 5;
    background-color: rgba(255, 255, 255, 0.9); color: var(--text-color);
    border: 1px solid var(--border-color);
    width: 32px; height: 32px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 1.2rem; font-weight: bold;
    cursor: pointer; opacity: 0.8; transition: all 0.2s ease;
}
.catalog-card:hover .own-it-btn { opacity: 1; }
.catalog-card.is-owned { box-shadow: 0 0 0 2px var(--success-color), 0 4px 12px rgba(40, 167, 69, 0.3); }
.catalog-card.is-owned .own-it-btn, .own-it-btn.active { opacity: 1; background-color: var(--success-color); color: white; border-color: var(--success-color); }

/* ==========================================
  Modal Common Styles
  ==========================================
*/
#modal-backdrop, #preview-modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    width: auto;
    height: auto;
    background-color: rgba(0, 0, 0, 0.85);
    align-items: center;
    justify-content: center;
    padding: 20px;
    box-sizing: border-box;
    overflow: hidden;
}
#preview-modal { z-index: 1500; }

#modal-content, .preview-content {
    position: relative;
    background-color: #fff;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    width: 95%;
    max-width: 900px;
    max-height: 90vh;
}

#modal-content { align-items: center; }

#modal-close-btn {
    position: absolute; top: -15px; right: -15px;
    background: white; color: #333;
    font-size: 28px; font-weight: bold;
    width: 36px; height: 36px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer; box-shadow: 0 2px 10px rgba(0,0,0,0.2);
    z-index: 1001;
}

#modal-main-image {
    max-width: 100%;
    flex-shrink: 1;
    object-fit: contain;
    border-radius: 4px;
    margin-bottom: 15px;
    min-height: 0;
}

#modal-thumbnail-gallery {
    display: flex; flex-wrap: wrap; justify-content: center;
    gap: 10px; width: 100%;
    overflow-y: auto;
    max-height: 80px;
    flex-shrink: 0;
}

#modal-tag-list {
    display: flex; flex-wrap: wrap; justify-content: center;
    gap: 8px; width: 100%; max-width: 600px; margin-top: 15px;
    flex-shrink: 0;
    overflow-y: auto; max-height: 100px;
}

.modal-tag-item {
    font-size: 0.85rem; font-weight: 500;
    background-color: var(--tag-bg); color: #495057;
    padding: 4px 10px; border-radius: 6px;
    cursor: pointer; transition: background-color 0.2s ease;
}

.modal-thumbnail-wrapper {
    width: 60px; height: 60px; position: relative;
    border: 2px solid var(--border-color); border-radius: 4px;
    cursor: pointer; overflow: hidden; flex-shrink: 0;
}
.modal-thumbnail { width: 100%; height: 100%; object-fit: contain; }

/* ==========================================
  Preview Modal & Generated Image
  ==========================================
*/
.preview-content { overflow: hidden; }

.preview-image-container {
    flex: 1;
    min-height: 0;
    display: block;
    overflow-y: auto;
    text-align: center;
    background-color: transparent;
    padding: 0;
    margin-bottom: 12px;
    border: 1px solid #f1f1f1;
    border-radius: 4px;
}

#generated-preview-img {
    max-width: 100%;
    height: auto;
    max-height: none;
    object-fit: unset;
    display: inline-block;
    box-shadow: none;
}

.preview-actions {
    width: 100%;
    flex-shrink: 0;
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    padding-top: 8px;
    border-top: 1px solid var(--border-color);
}

#loading-overlay {
    display: none;
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(255,255,255,0.9);
    z-index: 2000;
    flex-direction: column; align-items: center; justify-content: center;
}
.spinner {
    width: 40px; height: 40px;
    border: 4px solid #f3f3f3; border-top: 4px solid var(--primary-color);
    border-radius: 50%; animation: spin 1s linear infinite;
    margin-bottom: 15px;
}
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }

.filter-close-btn { display: none; }

.danger-btn {
    background-color: #dc3545;
    color: white;
    padding: 12px 24px;
    font-size: 1rem;
    border: none;
    border-radius: 30px;
    cursor: pointer;
    font-weight: 600;
    transition: all 0.2s ease;
}

.danger-btn:hover {
    background-color: #c82333;
    transform: translateY(-1px);
    box-shadow: 0 6px 12px rgba(220, 53, 69, 0.2);
}

/* ==========================================
  Mobile Responsiveness
  ==========================================
*/
@media (max-width: 600px) {
    :root { --main-padding: 12px; --grid-gap: 12px; }

    .series-grid {
        grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
        gap: 16px;
    }
    
    .series-title {
        font-size: 1.3rem;
        margin-bottom: 16px;
    }

    #modal-backdrop, #preview-modal {
        padding: 10px;
        align-items: center;
        justify-content: center;
    }

    #modal-content, .preview-content {
        width: 100%;
        max-width: 500px;
        max-height: 85dvh;
        padding: 15px;
        margin: 0 auto;
        display: flex;
        flex-direction: column;
        position: relative;
    }

    #modal-content { overflow-y: auto; overflow-x: hidden; }

    .preview-content { overflow: hidden; }

    .preview-image-container {
        flex: 1;
        min-height: 0;
        overflow-y: auto;
        margin-bottom: 12px;
        background-color: transparent;
        padding: 0;
        border: none;
        display: block;
        text-align: center;
    }

    #generated-preview-img {
        max-width: 100%;
        height: auto;
        display: inline-block;
        box-shadow: none;
    }

    .preview-actions {
        width: 100%;
        flex-shrink: 0;
        display: flex;
        gap: 10px;
        padding-top: 10px;
        border-top: 1px solid #eee;
    }

    .preview-actions button { flex: 1; padding: 12px 0; font-size: 0.95rem; }

    #modal-main-image {
        max-height: 40vh;
        width: auto;
        max-width: 100%;
        margin-bottom: 15px;
        align-self: center;
    }

    #modal-close-btn {
        top: 8px; right: 8px;
        width: 32px; height: 32px;
        font-size: 24px;
        background-color: rgba(255,255,255,0.95);
        border: 1px solid #eee;
        box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        z-index: 1002;
    }

    #modal-thumbnail-gallery { margin-bottom: 15px; flex-shrink: 0; }
    #modal-tag-list { margin-bottom: 10px; flex-shrink: 0; }

    .filter-dropdown { position: static; }

    .filter-dropdown-content {
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 85%;
        max-width: 320px;
        max-height: 60vh;
        z-index: 2000;
        box-shadow: 0 10px 25px rgba(0,0,0,0.2), 0 0 0 100vmax rgba(0,0,0,0.5);
    }

    .filter-scroll-container { padding-top: 12px; }

    /* 手機版下拉動畫保持獨立，不需要修改 */
    .filter-dropdown.active .filter-dropdown-content {
        animation: mobileFadeIn 0.2s ease-out;
    }

    .filter-close-btn {
        display: flex;
        align-items: center;
        justify-content: center;
        position: absolute;
        top: 8px;
        right: 12px;
        width: 32px;
        height: 32px;
        font-size: 28px;
        line-height: 1;
        font-weight: bold;
        color: #333;
        background: rgba(255,255,255,0.9);
        border-radius: 50%;
        cursor: pointer;
        z-index: 2005;
    }
}

@keyframes mobileFadeIn {
    from { opacity: 0; transform: translate(-50%, -40%); }
    to { opacity: 1; transform: translate(-50%, -50%); }
}