@import url('https://fonts.googleapis.com/css2?family=Noto+Serif+TC:wght@400;700&family=Lato:wght@300;400;700&display=swap');

:root {
    --primary: #555555;
    --accent: #bfa47d;
    --bg: #ffffff;
    --card-bg: #fdfdfd;
    --text: #333333;
    --text-light: #777777;
    --border: #e0e0e0;
    --overlay-bg: rgba(255, 255, 255, 0.95);
    --lightbox-bg: rgba(0, 0, 0, 0.9);
    --success-green: #28a745;
    --danger-red: #d9534f;
}

body {
    font-family: 'Lato', 'Noto Serif TC', sans-serif;
    background-color: var(--bg);
    color: var(--text);
    margin: 0;
    padding-top: 80px;
}

/* === 導航欄 === */
.navbar {
    position: fixed; top: 0; left: 0; width: 100%; height: 70px;
    background: rgba(255, 255, 255, 0.98); border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between;
    padding: 0 40px; box-sizing: border-box; z-index: 1000;
    box-shadow: 0 2px 10px rgba(0,0,0,0.03);
}
.nav-logo { height: 50px; display: flex; align-items: center; }
.nav-logo img { height: 100%; object-fit: contain; }
.nav-right { display: flex; align-items: center; gap: 30px; }
.nav-links { display: flex; gap: 20px; }
.nav-item { text-decoration: none; color: var(--text-light); font-size: 15px; letter-spacing: 1px; cursor: pointer; transition: 0.3s; }
.nav-item:hover, .nav-item.active { color: var(--text); }

.api-switch-container { display: flex; align-items: center; gap: 10px; padding: 5px 15px; background: #f0f0f0; border-radius: 20px; border: 1px solid #ddd; }
.api-switch-label { font-size: 12px; color: var(--text-light); font-weight: bold; }
.api-switch-label.active { color: var(--success-green); }
.toggle-switch { position: relative; display: inline-block; width: 40px; height: 20px; flex-shrink: 0; }
.toggle-switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ddd; transition: .4s; border-radius: 34px; }
.slider:before { position: absolute; content: ""; height: 14px; width: 14px; left: 3px; bottom: 3px; background-color: white; transition: .4s; border-radius: 50%; }
.toggle-switch.api-mode input:checked + .slider { background-color: var(--success-green); }
input:checked + .slider { background-color: var(--text); }
input:checked + .slider:before { transform: translateX(20px); }
.hamburger { display: none; cursor: pointer; font-size: 24px; color: var(--text); }

/* === 主佈局 === */
.main-container { max-width: 1200px; margin: 40px auto; padding: 0 20px; }
header { text-align: center; margin-bottom: 40px; }
header h1 { font-family: 'Noto Serif TC', serif; font-size: 2.5em; font-weight: 400; margin-bottom: 10px; letter-spacing: 2px; }
header p { color: var(--text-light); font-weight: 300; letter-spacing: 1px; margin-bottom: 30px; }
.guide-container { display: flex; width: 100%; margin-bottom: 40px; border-radius: 8px; overflow: hidden; box-shadow: 0 4px 15px rgba(0,0,0,0.05); }
.guide-part { flex: 1; width: 33.333%; height: auto; object-fit: cover; display: block; }
.split-container { display: grid; grid-template-columns: 1fr 1fr; gap: 40px; margin-bottom: 50px; }

@media (max-width: 768px) {
    .navbar { padding: 0 20px; height: 60px; } .nav-logo { height: 40px; } .hamburger { display: block; }
    .nav-right { display: none; position: absolute; top: 60px; left: 0; width: 100%; background: white; flex-direction: column; padding: 20px 0; border-bottom: 1px solid var(--border); box-shadow: 0 5px 10px rgba(0,0,0,0.05); gap: 0; }
    .nav-right.active { display: flex; }
    .nav-links { flex-direction: column; width: 100%; gap: 0; }
    .nav-item { padding: 15px 40px; width: 100%; box-sizing: border-box; border-bottom: 1px solid #f0f0f0; }
    .api-switch-container { margin: 20px auto; width: 80%; justify-content: center; }
    body { padding-top: 80px; }
    .split-container { grid-template-columns: 1fr; gap: 30px; }
    .guide-container { flex-direction: column; }
    .guide-part { width: 100%; height: auto; }
}

/* === 面板樣式 === */
.character-panel, .final-stage-panel { background: var(--card-bg); padding: 30px; border: 1px solid var(--border); border-radius: 2px; }
h2 { font-family: 'Noto Serif TC', serif; font-weight: 400; border-bottom: 1px solid var(--border); padding-bottom: 15px; margin-bottom: 25px; font-size: 1.4em; }
.upload-section { margin-bottom: 25px; }
.upload-section label { display: block; margin-bottom: 10px; font-weight: 400; color: var(--text); letter-spacing: 0.5px; }

/* 合照區標題與按鈕容器 */
.section-header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
}
.section-header-row label {
    margin-bottom: 0 !important; /* 覆蓋 upload-section label 的 margin */
    font-weight: bold;
}
/* 主頁面上的檢視按鈕 */
.main-review-btn {
    padding: 5px 15px;
    border: 1px solid var(--text);
    border-radius: 20px;
    background: white;
    font-size: 12px;
    cursor: pointer;
    transition: 0.2s;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 5px;
}
.main-review-btn:hover { background: var(--text); color: white; }

.control-row { display: flex; align-items: center; margin-bottom: 20px; padding: 10px; border: 1px solid var(--border); }
.height-input-group { display: flex; align-items: center; gap: 15px; margin-bottom: 20px; padding-bottom: 10px; border-bottom: 1px dashed var(--border); }
.height-input-group label { margin: 0; min-width: 80px; }
.height-input-group input { flex: 1; padding: 8px; border: 1px solid var(--border); font-family: 'Lato', sans-serif; text-align: center; background: transparent; }

.preset-selector { display: flex; gap: 12px; margin-bottom: 15px; flex-wrap: wrap; }
.img-box-common { position: relative; width: 100px; height: 100px; cursor: pointer; transition: all 0.3s; border: 1px solid transparent; overflow: hidden; background: #f9f9f9; flex-shrink: 0; }
.img-box-common:hover { transform: translateY(-3px); box-shadow: 0 5px 15px rgba(0,0,0,0.1); }
.preset-img { width: 100%; height: 100%; object-fit: cover; }
.img-box-common.selected { border: 2px solid var(--text); } .img-box-common.selected .preset-img { opacity: 0.9; }

.custom-upload-box { position: relative; width: 100px; height: 100px; border: 1px dashed var(--text-light); display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--text-light); cursor: pointer; transition: 0.3s; background: #fafafa; flex-shrink: 0; }
.custom-upload-box:hover { border-color: var(--text); color: var(--text); background: #fff; }
.custom-upload-box i { font-size: 24px; margin-bottom: 5px; font-style: normal; }
.custom-upload-box span { font-size: 12px; }
.custom-upload-box img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; display: none; }
.custom-upload-box img.active { display: block; }
.preview-area { width: 100%; aspect-ratio: 9 / 16; background: #f5f5f5; display: flex; align-items: center; justify-content: center; margin: 20px 0; overflow: hidden; position: relative; border: 1px solid var(--border); }
.preview-area img { width: 100%; height: 100%; object-fit: cover; }

.collection-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 15px; margin-bottom: 20px; }
.collection-card { position: relative; aspect-ratio: 1; border: 1px solid var(--border); cursor: pointer; overflow: hidden; transition: 0.3s; text-align: center; }
.collection-card:hover { transform: translateY(-3px); box-shadow: 0 5px 15px rgba(0,0,0,0.1); }
.collection-card img { width: 100%; height: 100%; object-fit: cover; transition: 0.3s; display: block; }
.collection-card.active { border: 2px solid var(--text); } .collection-card.active img { filter: brightness(0.4); }
.collection-card .label { position: absolute; bottom: 0; left: 0; width: 100%; background: rgba(255,255,255,0.9); padding: 5px 0; font-size: 12px; color: var(--text); display: flex; justify-content: center; align-items: center; }
.collection-card.active::after { content: "編輯"; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: white; font-size: 14px; border: 1px solid white; padding: 6px 16px; letter-spacing: 2px; z-index: 2; }

.collection-add-btn { border: 1px dashed var(--text-light); background: #fafafa; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--text-light); position: relative; }
.collection-add-btn:hover { border-color: var(--text); color: var(--text); background: #fff; }
.collection-add-btn .plus { font-size: 40px; font-weight: 300; }
.collection-add-btn img.custom-cover { position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; z-index: 1; display: none; }
.collection-add-btn img.custom-cover.visible { display: block; }
.collection-add-btn.has-image .plus, .collection-add-btn.has-image .label-text { display: none; }
.collection-add-btn .trash-btn { position: absolute; bottom: 5px; left: 5px; width: 28px; height: 28px; background-color: rgba(255, 255, 255, 0.9); border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; z-index: 10; box-shadow: 0 2px 5px rgba(0,0,0,0.2); opacity: 0; transition: opacity 0.2s; }
.collection-add-btn:hover .trash-btn { opacity: 1; }
.collection-add-btn .trash-btn:hover { background-color: var(--danger-red); } .collection-add-btn .trash-btn:hover svg { fill: white; }

/* === 模態框 (Album Modal) - 關鍵修復 === */
.modal-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(255, 255, 255, 0.98); z-index: 2000;
    display: none; flex-direction: column;
    padding: 20px; box-sizing: border-box;
}

.modal-header {
    display: flex; justify-content: space-between; align-items: center;
    margin-bottom: 20px; border-bottom: 1px solid var(--border); padding-bottom: 15px;
    flex-shrink: 0; /* 防止 Header 被壓縮 */
}

.modal-title { font-family: 'Noto Serif TC'; font-size: 1.5em; display: flex; align-items: center; }

/* 模態框內的篩選按鈕 */
.filter-btn { padding: 5px 12px; border: 1px solid #ddd; border-radius: 15px; font-size: 12px; background: white; cursor: pointer; color: #666; transition: 0.2s; margin-left: 15px; }
.filter-btn:hover { background: #f0f0f0; color: #333; }
.modal-actions button { padding: 8px 20px; border: 1px solid var(--text); background: transparent; cursor: pointer; transition: 0.3s; margin-left: 10px; }
.modal-actions button.primary { background: var(--text); color: white; }

/* 【關鍵修復】圖片網格排版 */
/* === style.css 更新 === */

.album-grid {
    display: grid;
    /* 這裡保持不變 */
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); 
    gap: 10px;
    
    /* 讓內容可以滾動 */
    overflow-y: auto; 
    
    /* 確保內容緊湊排列，不會因為高度計算錯誤而拉開間距 */
    align-content: start; 

    /* 確保容器本身有高度 */
    flex: 1; 
    height: 0; /* 配合 flex:1 使用，防止父容器被撐破 */
    
    padding: 10px;
    border: 1px solid #eee;
    border-radius: 4px;
    background: #fafafa;
}

/* === 強制正方形排版 (解決堆疊問題) === */
.album-item {
    position: relative;
    width: 100%;
    
    /* 【關鍵】不使用 aspect-ratio，改用 padding 撐開高度 */
    /* 這能保證即使圖片還沒下載，格子也已經佔好了位置，絕對不會疊在一起 */
    height: 0;
    padding-bottom: 100%; 
    
    cursor: pointer;
    border-radius: 4px;
    overflow: hidden;
    background: #eee;
    border: 2px solid transparent;
    transition: all 0.2s;
}

.album-item:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 10px rgba(0,0,0,0.1);
}

/* 圖片改為絕對定位，填滿那個由 padding 撐開的空間 */
.album-item img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    opacity: 0;
    transition: opacity 0.3s;
}

.album-item img.loaded {
    opacity: 1;
}

/* 選中狀態 */
.album-item.selected {
    border-color: var(--text);
}

.album-item.selected img {
    filter: brightness(0.7);
    transform: scale(1.05);
}

/* 修正 Spinner 位置 (因為父層 height 是 0，所以要調整定位基準) */
.album-item .spinner {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    /* 修正偏移量 */
    margin-top: -10px; 
    margin-left: -10px;
    
    border: 3px solid #ddd;
    border-top: 3px solid var(--text);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    z-index: 1;
}

/* 修正打勾圖示位置 */
.album-item .check-icon {
    position: absolute;
    bottom: 8px;
    right: 8px;
    width: 24px;
    height: 24px;
    background: var(--accent);
    color: white;
    border-radius: 50%;
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    z-index: 3;
}

.album-item.selected .check-icon {
    display: flex;
}

.overlay-btn { position: absolute; width: 28px; height: 28px; background: rgba(255, 255, 255, 0.9); border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; opacity: 0; transition: all 0.2s; z-index: 20; box-shadow: 0 2px 5px rgba(0,0,0,0.15); }
.img-box-common:hover .overlay-btn, .custom-upload-box:hover .overlay-btn, .album-item:hover .overlay-btn, .preview-area:hover .overlay-btn, .gallery-item:hover .overlay-btn { opacity: 1; }
.overlay-btn:hover { background: var(--text); } .overlay-btn:hover svg { fill: white; }
.overlay-btn svg { width: 50%; height: 50%; fill: var(--text); }
.zoom-btn { top: 5px; right: 5px; } .dl-btn { bottom: 5px; right: 5px; } .trash-btn { bottom: 5px; left: 5px; } .trash-btn:hover { background: var(--danger-red); }

.action-btn { width: 100%; padding: 14px; background: transparent; color: var(--text); border: 1px solid var(--text); cursor: pointer; font-size: 15px; letter-spacing: 2px; text-transform: uppercase; transition: all 0.3s; margin-top: 15px; font-family: 'Noto Serif TC', serif; }
.action-btn:hover:not(:disabled) { background: var(--text); color: white; }
.action-btn:disabled { color: #aaa; border-color: #ddd; cursor: not-allowed; }
.main-btn { background: var(--text); color: white; border: 1px solid var(--text); } .main-btn:hover:not(:disabled) { background: white; color: var(--text); }
.batch-dl-btn { background: var(--accent); border-color: var(--accent); color: white; display: none; margin-top: 20px; } .batch-dl-btn:hover { background: #a88d68; border-color: #a88d68; }
.status-box { margin-top: 20px; color: var(--text-light); font-size: 0.9em; text-align: center; }
.progress-bar-container { width: 100%; background: #eee; height: 2px; margin-top: 10px; }
.progress-bar { width: 0%; height: 100%; background: var(--text); transition: width 0.3s; }
.gallery-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 20px; margin-top: 30px; min-height: 200px; background: #fafafa; border: 1px dashed var(--border); padding: 20px; border-radius: 4px; }
.gallery-item { position: relative; border: 1px solid var(--border); display: flex; flex-direction: column; background: white; }
.gallery-item .img-wrapper { position: relative; width: 100%; aspect-ratio: 3 / 4; overflow: hidden; }
.gallery-item img { width: 100%; height: 100%; object-fit: cover; display: block; }
.timer-text { font-family: monospace; font-size: 14px; margin-top: 5px; color: var(--primary); }
.version-controls { display: flex; border-top: 1px solid var(--border); }
.v-btn { flex: 1; padding: 8px 0; text-align: center; cursor: pointer; font-size: 12px; color: var(--text-light); transition: 0.2s; background: #f9f9f9; }
.v-btn:hover { background: #eee; } .v-btn.active { background: var(--text); color: white; } .v-btn:first-child { border-right: 1px solid var(--border); }

/* 底部清除按鈕區 */
.panel-footer-actions { margin-top: 40px; padding-top: 20px; border-top: 1px dashed var(--border); display: flex; justify-content: center; }
.reset-btn { background: transparent; border: none; color: var(--text-light); font-size: 0.9em; cursor: pointer; display: flex; align-items: center; gap: 8px; padding: 10px 20px; border-radius: 4px; transition: all 0.3s ease; font-family: 'Lato', sans-serif; }
.reset-btn svg { fill: var(--text-light); transition: fill 0.3s ease; }
.reset-btn:hover { background-color: #fff5f5; color: var(--danger-red); } .reset-btn:hover svg { fill: var(--danger-red); }

/* 燈箱 */
.lightbox-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: var(--lightbox-bg); display: none; justify-content: center; align-items: center; z-index: 9999; backdrop-filter: blur(5px); opacity: 0; transition: opacity 0.3s ease; }
.lightbox-overlay.active { display: flex; opacity: 1; }
.lightbox-img { max-width: 95%; max-height: 95%; object-fit: contain; border-radius: 4px; box-shadow: 0 5px 30px rgba(0,0,0,0.5); transform: scale(0.9); transition: transform 0.3s ease; display: none; }
.lightbox-img.loaded { display: block; transform: scale(1); }
.lightbox-loader { border: 5px solid #333; border-top: 5px solid #fff; border-radius: 50%; width: 50px; height: 50px; animation: spin 1s linear infinite; position: absolute; }
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
.lightbox-nav { position: absolute; top: 50%; transform: translateY(-50%); width: 50px; height: 50px; background: rgba(255, 255, 255, 0.2); border-radius: 50%; color: white; display: flex; justify-content: center; align-items: center; cursor: pointer; font-size: 30px; user-select: none; transition: background 0.3s; z-index: 10002; }
.lightbox-nav:hover { background: rgba(255, 255, 255, 0.5); }
.nav-prev { left: 20px; } .nav-next { right: 20px; }
.lightbox-close { position: absolute; top: 20px; right: 20px; font-size: 40px; color: white; cursor: pointer; font-weight: bold; line-height: 1; opacity: 0.8; z-index: 10001; }

.disclaimer-section { margin-top: 50px; padding: 30px; background-color: #f8f8f8; border-top: 1px solid var(--border); color: var(--text-light); font-size: 0.85em; line-height: 1.6; }
.disclaimer-section h3 { font-family: 'Noto Serif TC', serif; font-size: 1.1em; color: var(--text); margin-bottom: 15px; margin-top: 20px; }
.disclaimer-section h3:first-child { margin-top: 0; }
.disclaimer-section p { margin-bottom: 10px; }
.disclaimer-section ul { padding-left: 20px; margin-bottom: 15px; }
.disclaimer-section li { margin-bottom: 5px; }