/**
 * Custom Gallery & Core Blocks CSS Enhancements
 * Version 3.9 - Caption Styling Improvement
 * Fixes: All "transform: scale()" hover effects have been removed or neutralized to disable the visual zoom feature per user request.
 *        All previous fixes for layouts and responsiveness are retained.
 * Enhancement: Improved the styling of gallery captions for better readability and aesthetics, and explicitly removed text-shadow.
 * Author: Jin
 * Update: 2025-09-14
 */

/* --- 1. CORE WORDPRESS BLOCK FIXES (for AJAX Loading) --- */
.wp-block-columns.is-layout-flex,
.wp-block-columns {
    display: flex !important;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 1.5em;
}

.wp-block-columns > .wp-block-column {
    flex-grow: 1;
    flex-basis: 0;
    min-width: 0;
}

/* --- 2. BASE STYLES FOR CUSTOM GALLERY --- */
.wp-block-gallery.custom-layout {
    display: block;
    margin: 0 auto 1em;
    padding: 0;
    overflow: hidden;
    position: relative;
    transition: opacity 0.3s ease-in-out;
    max-width: 100%;
    width: 100%;
    box-sizing: border-box;
}

.wp-block-gallery.custom-layout img {
    border: none;
    transition: transform 0.3s ease;
}

.wp-block-gallery.custom-layout .wp-block-image {
    margin: 0 !important;
    padding: 0;
    box-sizing: border-box;
    overflow: hidden;
    width: 100% !important;
    height: 100%;
    transition: box-shadow 0.3s ease;
    position: relative;
}

.wp-block-gallery.custom-layout .wp-block-image:hover {
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.wp-block-gallery.custom-layout .wp-block-image img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
    transform: scale(1);
}

/* [修正] 移除通用的滑鼠移過放大效果 */
/*
.wp-block-gallery.custom-layout .wp-block-image:hover img {
    transform: scale(1.05);
}
    
*/
/* [新增] 強制移除圖片及其容器的邊框、輪廓和陰影
 * -------------------------------------------------------------------------- */
/* 
 * 這段程式碼使用 !important 來確保其優先級最高，
 * 可以覆蓋掉任何來自佈景主題 (Theme) 的預設邊框、底線或陰影樣式，
 * 確保圖庫的視覺純淨。
 */
.wp-block-gallery.custom-layout img,
.wp-block-gallery.custom-layout a,
.wp-block-gallery.custom-layout .wp-block-image {
    border: none !important;
    outline: none !important;
    box-shadow: none !important;
    text-decoration: none !important; /* 特別移除連結可能產生的底線 */
    background: none !important; /* 移除可能的背景顏色 */
}


/* --- 3. LOADING & FALLBACK STYLES (最終防閃爍版) --- */

/* 為所有自訂圖庫設定基礎的過渡效果 */
.wp-block-gallery.custom-layout {
    transition: opacity 0.3s ease-in-out;
}

/* [新增] 當 JS 開始計算佈局時，讓容器淡出的 class */
.wp-block-gallery.custom-layout.is-transitioning {
    opacity: 0 !important;
}


/* ==========================================================================
   策略一：針對需要 JS 精確計算的版型 (Slider, Justified, ImageCompare)
   - 保持 Spinner 佔位容器的策略，因為它們不能「即時顯示」。
   ========================================================================== */
.wp-block-gallery.custom-layout[data-layout="slider"]:not(.layout-ready),
.wp-block-gallery.custom-layout[data-layout="justified"]:not(.layout-ready),
.wp-block-gallery.custom-layout[data-layout="imagecompare"]:not(.layout-ready) {
    position: relative;
    opacity: 1;
    min-height: clamp(300px, 40vh, 600px);
    background-color: rgba(0,0,0,0.03);
}
/* Spinner 的 ::before 樣式 */
.wp-block-gallery.custom-layout[data-layout="slider"]:not(.layout-ready)::before,
.wp-block-gallery.custom-layout[data-layout="justified"]:not(.layout-ready)::before,
.wp-block-gallery.custom-layout[data-layout="imagecompare"]:not(.layout-ready)::before {
    content: ''; box-sizing: border-box; position: absolute; top: 50%; left: 50%;
    width: 40px; height: 40px; margin-top: -20px; margin-left: -20px;
    border-radius: 50%; border: 4px solid rgba(0, 0, 0, 0.1); border-top-color: #666;
    animation: gallery-spinner-rotation 0.8s ease infinite; z-index: 1;
}


/* ==========================================================================
   策略二：針對純 CSS 或簡易 JS 的網格版型
   - 保持「即時顯示」策略。
   ========================================================================== */
.wp-block-gallery.custom-layout:not([data-layout="slider"]):not([data-layout="justified"]):not([data-layout="imagecompare"]):not(.layout-ready) {
    display: grid !important;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 10px;
    opacity: 1; /* 初始狀態可見 */
}

/* 
 * ==========================================================================
 * [補齊] 以下是之前被省略的「即時顯示」核心樣式
 * ==========================================================================
 * 這兩條規則至關重要，它們確保了在 JS 計算最終佈局之前，
 * 初始的網格看起來是整齊劃一的方形，從而極大地減少了視覺上的跳動感。
 */
.wp-block-gallery.custom-layout:not([data-layout="slider"]):not([data-layout="justified"]):not([data-layout="imagecompare"]):not(.layout-ready) .wp-block-image {
    aspect-ratio: 1 / 1; /* 讓每個圖片容器預設為正方形 */
    overflow: hidden;    /* 隱藏超出部分的圖片內容 */
}

.wp-block-gallery.custom-layout:not([data-layout="slider"]):not([data-layout="justified"]):not([data-layout="imagecompare"]):not(.layout-ready) .wp-block-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;   /* 讓圖片填滿正方形容器，裁剪掉多餘部分 */
}


/* ==========================================================================
   通用規則：所有圖庫在計算完成後，都應該是完全可見的
   ========================================================================== */
.wp-block-gallery.custom-layout.layout-ready {
    opacity: 1;
    background-color: transparent;
}

.wp-block-gallery.custom-layout.layout-ready::before {
    display: none; /* 確保 Spinner 在計算完成後被隱藏 */
}

/* 請確保動畫定義存在 */
@keyframes gallery-spinner-rotation {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

/* --- 4. SPECIFIC LAYOUT STYLES (DESKTOP FIRST) --- */

/* A. Justified Layout */
/* =============================================================================
   對齊式 (Justified) - 強制滿版 CSS
   ============================================================================= */

.wp-block-gallery.custom-layout[data-layout="justified"] {
    display: block !important;
    width: 100%;
}

.wp-block-gallery.custom-layout[data-layout="justified"] .gallery-row-justified {
    display: flex;
    margin-bottom: 10px;
    width: 100%; /* 確保每行都是滿版 */
}

.wp-block-gallery.custom-layout[data-layout="justified"] .gallery-row-justified:last-child {
    margin-bottom: 0;
}

.wp-block-gallery.custom-layout[data-layout="justified"] .justified-item {
    flex-shrink: 0;
    display: block;
    height: 100%;
    box-sizing: border-box;
    overflow: hidden;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.wp-block-gallery.custom-layout[data-layout="justified"] .justified-item img {
    width: 100%;
    height: 100%;
    display: block;
    border: none !important;
    
    /* 🎯 關鍵：使用 cover 填滿容器，確保對齊 */
    object-fit: cover;
    object-position: center;
}

/* 懸停效果 */
.wp-block-gallery.custom-layout[data-layout="justified"] .justified-item:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
    z-index: 2;
}

/* =============================================================================
   均分列 (Equal Row) - 完整顯示 CSS
   ============================================================================= */

.wp-block-gallery.custom-layout[data-layout="equal-row"] {
    display: flex !important;
    width: 100%;
    align-items: stretch;
}

.wp-block-gallery.custom-layout[data-layout="equal-row"] .gallery-row-equal {
    display: flex;
    gap: 10px;
    width: 100%;
}

.wp-block-gallery.custom-layout[data-layout="equal-row"] .equal-row-item {
    display: block;
    height: 100%;
    box-sizing: border-box;
    overflow: hidden;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.wp-block-gallery.custom-layout[data-layout="equal-row"] .equal-row-item img {
    width: 100%;
    height: 100%;
    display: block;
    border: none !important;
    
    /* 🎯 關鍵：使用 contain 完整顯示，不裁切 */
    object-fit: contain;
    object-position: center;
    
    /* 添加淺色背景，避免透明圖片顯示問題 */
    background: #f8f8f8;
}

/* 懸停效果 */
.wp-block-gallery.custom-layout[data-layout="equal-row"] .equal-row-item:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    z-index: 2;
}

/* =============================================================================
   響應式設計
   ============================================================================= */

/* 平板 */
@media (max-width: 768px) {
    /* Justified - 減少間距 */
    .wp-block-gallery.custom-layout[data-layout="justified"] .gallery-row-justified {
        gap: 6px;
        margin-bottom: 6px;
    }
    
    /* Equal Row - 減少間距 */
    .wp-block-gallery.custom-layout[data-layout="equal-row"] .gallery-row-equal {
        gap: 6px;
    }
}

/* 手機 */
@media (max-width: 480px) {
    /* Justified - 最小間距 */
    .wp-block-gallery.custom-layout[data-layout="justified"] .gallery-row-justified {
        gap: 4px;
        margin-bottom: 4px;
    }
    
    /* Equal Row - 改為垂直堆疊（可選） */
    .wp-block-gallery.custom-layout[data-layout="equal-row"] {
        flex-direction: column !important;
    }
    
    .wp-block-gallery.custom-layout[data-layout="equal-row"] .gallery-row-equal {
        flex-direction: column !important;
        gap: 10px;
        height: auto !important;
    }
    
    .wp-block-gallery.custom-layout[data-layout="equal-row"] .equal-row-item {
        width: 100% !important;
        max-width: 100% !important;
        flex: 0 0 auto !important;
        height: auto !important;
    }
    
    .wp-block-gallery.custom-layout[data-layout="equal-row"] .equal-row-item img {
        height: auto !important;
        max-height: 60vh;
    }
}

/* =============================================================================
   列印樣式
   ============================================================================= */

@media print {
    .wp-block-gallery.custom-layout[data-layout="justified"],
    .wp-block-gallery.custom-layout[data-layout="equal-row"] {
        display: block !important;
    }
    
    .wp-block-gallery.custom-layout .gallery-row-justified,
    .wp-block-gallery.custom-layout .gallery-row-equal {
        display: block !important;
        page-break-inside: avoid;
    }
    
    .wp-block-gallery.custom-layout .justified-item,
    .wp-block-gallery.custom-layout .equal-row-item {
        display: inline-block !important;
        width: auto !important;
        height: auto !important;
        page-break-inside: avoid;
    }
}


/* B. Grid-based layouts (2-7, twocols) */
.wp-block-gallery.custom-layout[data-layout^="2"],
.wp-block-gallery.custom-layout[data-layout^="3"],
.wp-block-gallery.custom-layout[data-layout^="4"],
.wp-block-gallery.custom-layout[data-layout^="5"],
.wp-block-gallery.custom-layout[data-layout^="6"],
.wp-block-gallery.custom-layout[data-layout^="7"],
.wp-block-gallery.custom-layout[data-layout="twocols"] { display: grid !important; }
.wp-block-gallery.custom-layout[data-layout^="2"] > *,
.wp-block-gallery.custom-layout[data-layout^="3"] > *,
.wp-block-gallery.custom-layout[data-layout^="4"] > *,
.wp-block-gallery.custom-layout[data-layout^="5"] > *,
.wp-block-gallery.custom-layout[data-layout^="6"] > *,
.wp-block-gallery.custom-layout[data-layout^="7"] > *,
.wp-block-gallery.custom-layout[data-layout="twocols"] > * { height: 100%; width: 100%; display: block; margin: 0; padding: 0; }

/* C. Two Cols */
.wp-block-gallery.custom-layout[data-layout="twocols"] { grid-template-columns: 1fr 1fr; gap: 10px; height: auto; }
.wp-block-gallery.custom-layout[data-layout="twocols"] > .wp-block-image { height: 100%; }

/* D. Masonry (Waterfall) */
.wp-block-gallery.custom-layout[data-layout="masonry"] { display: block !important; column-count: 3; column-gap: 10px; }
.wp-block-gallery.custom-layout[data-layout="masonry"] .wp-block-image { break-inside: avoid; margin-bottom: 10px !important; }

/* E. Mosaic (Tiled) */
.wp-block-gallery.custom-layout[data-layout="mosaic"] { display: grid !important; gap: 10px; grid-auto-flow: row dense; grid-auto-rows: 150px; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
.wp-block-gallery.custom-layout[data-layout="mosaic"] .wp-block-image:nth-child(3n) { grid-column: span 2; }
.wp-block-gallery.custom-layout[data-layout="mosaic"] .wp-block-image:nth-child(4n) { grid-row: span 2; }

/* F. Instagram (Square Grid) */
.wp-block-gallery.custom-layout[data-layout="insta"] { display: grid !important; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 6px; }
.wp-block-gallery.custom-layout[data-layout="insta"] .wp-block-image { aspect-ratio: 1 / 1; }

/* G. Fullwidth (Horizontal Scroll) */
.wp-block-gallery.custom-layout[data-layout="fullwidth"] { display: flex !important; flex-wrap: nowrap; overflow-x: auto; gap: 10px; margin-left: calc(50% - 50vw); margin-right: calc(50% - 50vw); width: 100vw; padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); box-sizing: border-box; scroll-snap-type: x mandatory; }
.wp-block-gallery.custom-layout[data-layout="fullwidth"] .wp-block-image { flex: 0 0 80vw; height: 60vh; scroll-snap-align: center; }

/* H. Coverflow */
.wp-block-gallery.custom-layout[data-layout="coverflow"] { display: flex !important; align-items: center; overflow-x: auto; padding: 2rem 0; perspective: 1200px; }
.wp-block-gallery.custom-layout[data-layout="coverflow"] .wp-block-image { flex: 0 0 40%; height: 400px; transition: transform 0.5s ease; transform: rotateY(45deg) scale(0.8); }
/* [修正] 移除 Coverflow 的放大效果，但保留翻轉 */
.wp-block-gallery.custom-layout[data-layout="coverflow"] .wp-block-image:hover { transform: rotateY(0) scale(1); z-index: 10; }

/* I. Polaroid */
.wp-block-gallery.custom-layout[data-layout="polaroid"] { display: flex !important; flex-wrap: wrap; gap: 20px; justify-content: center; padding: 20px; }
.wp-block-gallery.custom-layout[data-layout="polaroid"] .wp-block-image { background: white; padding: 10px 10px 30px 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); transform: rotate(-2deg); transition: transform 0.3s ease; width: 200px !important; height: auto !important; position: relative; }
.wp-block-gallery.custom-layout[data-layout="polaroid"] .wp-block-image:nth-child(even) { transform: rotate(2deg); }
/* [修正] 移除 Polaroid 的放大效果，但保留回正 */
.wp-block-gallery.custom-layout[data-layout="polaroid"] .wp-block-image:hover { transform: rotate(0deg) scale(1); z-index: 10; }
.wp-block-gallery.custom-layout[data-layout="polaroid"] .wp-block-image img { object-fit: cover; }

/* J. Featured Big */
.wp-block-gallery.custom-layout[data-layout="featuredbig"] { display: grid !important; gap: 10px; grid-template-columns: 2fr 1fr; grid-template-rows: 1fr 1fr; height: 400px; }
.wp-block-gallery.custom-layout[data-layout="featuredbig"] > *:first-child { grid-row: 1 / 3; grid-column: 1 / 2; }
.wp-block-gallery.custom-layout[data-layout="featuredbig"] .wp-block-image img { object-fit: cover; }

/* K. Circle Grid */
.wp-block-gallery.custom-layout[data-layout="circlegrid"] {
    display: grid !important;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 20px;
    padding: 20px;
    justify-content: center;
}
.wp-block-gallery.custom-layout[data-layout="circlegrid"] .wp-block-image { aspect-ratio: 1 / 1; border-radius: 50%; overflow: hidden; transition: transform 0.3s ease, box-shadow 0.3s ease; }
/* [修正] 移除 Circle Grid 的放大效果，但保留陰影 */
.wp-block-gallery.custom-layout[data-layout="circlegrid"] .wp-block-image:hover {
    transform: scale(1);
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
.wp-block-gallery.custom-layout[data-layout="circlegrid"] .wp-block-image img { width: 100%; height: 100%; object-fit: cover; }

/* L. Image Compare */
/* L. Image Compare - 修正版 */
/* ============================================================================
   L. Image Compare - 純 CSS 實現（高效能版本）
   使用原生 range input + CSS clip-path，無外部依賴
   ============================================================================ */
.wp-block-gallery.custom-layout[data-layout="imagecompare"] { 
    position: relative; 
    width: 100%;
    --compare-position: 50%; /* CSS 變數控制分割位置 */
}

/* 圖片比較容器 */
.wp-block-gallery.custom-layout[data-layout="imagecompare"] .image-compare-wrapper {
    position: relative;
    width: 100%;
    overflow: hidden;
    border-radius: 8px;
    background: #f8f8f8;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    aspect-ratio: 16 / 9; /* 預設比例，會被 JS 覆蓋 */
    user-select: none;
    -webkit-user-select: none;
}

/* 後圖（右側/底層）*/
.wp-block-gallery.custom-layout[data-layout="imagecompare"] .image-compare-after {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    z-index: 1;
}

/* 前圖（左側/上層）- 使用 clip-path 裁切 */
.wp-block-gallery.custom-layout[data-layout="imagecompare"] .image-compare-before {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    z-index: 2;
    clip-path: inset(0 calc(100% - var(--compare-position)) 0 0);
    -webkit-clip-path: inset(0 calc(100% - var(--compare-position)) 0 0);
}

/* 滑桿軌道（透明，覆蓋整個區域） */
.wp-block-gallery.custom-layout[data-layout="imagecompare"] .image-compare-range {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
    opacity: 0; /* 隱藏原生滑桿 */
    cursor: ew-resize;
    margin: 0;
    -webkit-appearance: none;
    appearance: none;
    background: transparent;
}

/* 分割線 */
.wp-block-gallery.custom-layout[data-layout="imagecompare"] .image-compare-divider {
    position: absolute;
    top: 0;
    left: var(--compare-position);
    width: 3px;
    height: 100%;
    background: rgba(255, 255, 255, 0.9);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
    z-index: 5;
    transform: translateX(-50%);
    pointer-events: none;
}

/* 拖動手把 */
.wp-block-gallery.custom-layout[data-layout="imagecompare"] .image-compare-handle {
    position: absolute;
    top: 50%;
    left: var(--compare-position);
    width: 44px;
    height: 44px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 50%;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
    z-index: 6;
    transform: translate(-50%, -50%);
    pointer-events: none;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* 手把箭頭圖標 */
.wp-block-gallery.custom-layout[data-layout="imagecompare"] .image-compare-handle::before,
.wp-block-gallery.custom-layout[data-layout="imagecompare"] .image-compare-handle::after {
    content: '';
    position: absolute;
    width: 0;
    height: 0;
    border-style: solid;
}

.wp-block-gallery.custom-layout[data-layout="imagecompare"] .image-compare-handle::before {
    border-width: 7px 10px 7px 0;
    border-color: transparent #555 transparent transparent;
    left: 8px;
}

.wp-block-gallery.custom-layout[data-layout="imagecompare"] .image-compare-handle::after {
    border-width: 7px 0 7px 10px;
    border-color: transparent transparent transparent #555;
    right: 8px;
}

/* 標籤 */
.wp-block-gallery.custom-layout[data-layout="imagecompare"] .image-compare-label {
    position: absolute;
    bottom: 15px;
    padding: 6px 14px;
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    font-size: 13px;
    font-weight: 500;
    border-radius: 4px;
    z-index: 4;
    pointer-events: none;
}

.wp-block-gallery.custom-layout[data-layout="imagecompare"] .image-compare-label-before {
    left: 15px;
}

.wp-block-gallery.custom-layout[data-layout="imagecompare"] .image-compare-label-after {
    right: 15px;
}

/* 手機觸控優化 */
@media (max-width: 768px) {
    .wp-block-gallery.custom-layout[data-layout="imagecompare"] .image-compare-handle {
        width: 50px;
        height: 50px;
    }
    
    .wp-block-gallery.custom-layout[data-layout="imagecompare"] .image-compare-label {
        font-size: 11px;
        padding: 4px 10px;
        bottom: 10px;
    }
}

/* M. Film Strip */
.wp-block-gallery.custom-layout[data-layout="filmstrip"] { background: #222; padding: 20px 10px; display: flex !important; gap: 5px; overflow-x: auto; border-top: 10px solid #333; border-bottom: 10px solid #333; position: relative; }
.wp-block-gallery.custom-layout[data-layout="filmstrip"]::before,
.wp-block-gallery.custom-layout[data-layout="filmstrip"]::after { content: ''; position: absolute; left: 0; right: 0; height: 8px; background: repeating-linear-gradient( to right, #444 0px, #444 15px, #666 15px, #666 25px ); }
.wp-block-gallery.custom-layout[data-layout="filmstrip"]::before { top: 2px; }
.wp-block-gallery.custom-layout[data-layout="filmstrip"]::after { bottom: 2px; }
.wp-block-gallery.custom-layout[data-layout="filmstrip"] .wp-block-image { flex: 0 0 200px; height: 150px !important; border: 2px solid #555; }
.wp-block-gallery.custom-layout[data-layout="filmstrip"] .wp-block-image img { object-fit: cover; }

/* N. Honeycomb */
.wp-block-gallery.custom-layout[data-layout="honeycomb"] { display: flex !important; flex-wrap: wrap; gap: 5px; justify-content: center; }
.wp-block-gallery.custom-layout[data-layout="honeycomb"] .wp-block-image { width: 150px !important; height: 130px !important; clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%); margin: 10px 5px; transition: transform 0.3s ease; }
/* [修正] 移除 Honeycomb 的放大效果 */
.wp-block-gallery.custom-layout[data-layout="honeycomb"] .wp-block-image:hover {
    transform: scale(1);
    z-index: 10;
}
.wp-block-gallery.custom-layout[data-layout="honeycomb"] .wp-block-image img { object-fit: cover; }

/* O. Offset Columns */
.wp-block-gallery.custom-layout[data-layout="offsetcols"] { display: grid !important; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px; }
.wp-block-gallery.custom-layout[data-layout="offsetcols"] .wp-block-image:nth-child(even) { transform: translateY(30px); }
.wp-block-gallery.custom-layout[data-layout="offsetcols"] .wp-block-image { transition: transform 0.3s ease; }
/* [修正] 移除 Offset Columns 的放大效果，但保留上移 */
.wp-block-gallery.custom-layout[data-layout="offsetcols"] .wp-block-image:hover {
    transform: translateY(0px) scale(1);
}

/* P. Split Focus */
.wp-block-gallery.custom-layout[data-layout="splitfocus"] { display: grid !important; grid-template-columns: 3fr 2fr; gap: 15px; height: 400px; }
.wp-block-gallery.custom-layout[data-layout="splitfocus"] > *:first-child { grid-row: 1 / -1; }
.wp-block-gallery.custom-layout[data-layout="splitfocus"] > *:not(:first-child) .wp-block-image { height: calc(50% - 7.5px) !important; }
.wp-block-gallery.custom-layout[data-layout="splitfocus"] .wp-block-image img { object-fit: cover; }

/* --- Q. Diagonal Stack (最終響應式版) --- */
.wp-block-gallery.custom-layout[data-layout="diagstack"] {
    position: relative;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    perspective: 1000px;
    cursor: pointer;

    /* [根本性修改] 移除固定的 height，改用 aspect-ratio */
    /* 這會讓容器的高度根據其寬度，保持 16:9 的黃金比例 */
    aspect-ratio: 16 / 9;

    /* [新增] 添加最小和最大高度，防止在極端螢幕尺寸下變形 */
    /* 確保在手機上不會太扁，在超寬螢幕上不會過高 */
    min-height: 350px;
    max-height: 65vh; /* 最大高度不超過螢幕可見高度的 65% */
    margin: 2em auto; /* 為容器上下增加一些間距，使其呼吸空間更大 */
}

.wp-block-gallery.custom-layout[data-layout="diagstack"] .wp-block-image {
    position: absolute;
    /* [微調] 略微調整卡片的大小，以更好地適應新的容器尺寸 */
    width: clamp(220px, 45%, 350px) !important;
    height: auto !important;
    aspect-ratio: 4 / 3;
    transition: transform 0.5s ease-in-out, opacity 0.5s ease;
    box-shadow: 0 5px 15px rgba(0,0,0,0.1);
    border-radius: 8px;
    overflow: hidden;
}

/* 不同的卡片有不同的初始旋轉角度 (保持不變) */
.wp-block-gallery.custom-layout[data-layout="diagstack"] .wp-block-image:nth-of-type(3n+1) { transform: rotate(-4deg) translateX(-10px); }
.wp-block-gallery.custom-layout[data-layout="diagstack"] .wp-block-image:nth-of-type(3n+2) { transform: rotate(2deg) translateX(5px) translateY(10px); }
.wp-block-gallery.custom-layout[data-layout="diagstack"] .wp-block-image:nth-of-type(3n+3) { transform: rotate(5deg) translateX(10px) translateY(-5px); }

/* 滑鼠懸停在最上方的卡片時，給予回饋 (保持不變) */
.wp-block-gallery.custom-layout[data-layout="diagstack"] .wp-block-image:first-child:hover {
    transform: rotate(0deg) scale(1.03);
}

/* 當卡片被點擊並準備移到最後時的動畫 (保持不變) */
.wp-block-gallery.custom-layout[data-layout="diagstack"] .wp-block-image.is-moving {
    transform: translateX(150%) rotate(30deg) !important;
    opacity: 0;
}

/* R. Slider */
.wp-block-gallery.custom-layout[data-layout="slider"] { height: 50vh; max-height: 500px; min-height: 300px; background: #f0f0f0; display: block; opacity: 1; }
.wp-block-gallery.custom-layout[data-layout="slider"] .flickity-viewport,
.wp-block-gallery.custom-layout[data-layout="slider"] .flickity-slider { height: 100%; }
.wp-block-gallery.custom-layout[data-layout="slider"] .gallery-cell { height: 100%; margin-right: 10px; box-sizing: border-box; }
.wp-block-gallery.custom-layout[data-layout="slider"][data-slider-cols="1"] .gallery-cell { width: 100%; }
.wp-block-gallery.custom-layout[data-layout="slider"][data-slider-cols="2"] .gallery-cell { width: 50%; }
.wp-block-gallery.custom-layout[data-layout="slider"][data-slider-cols="3"] .gallery-cell { width: 33.3333%; }
.wp-block-gallery.custom-layout[data-layout="slider"][data-slider-cols="4"] .gallery-cell { width: 25%; }
.wp-block-gallery.custom-layout[data-layout="slider"]:not([data-slider-cols]) .gallery-cell { width: 100%; }
.wp-block-gallery.custom-layout[data-layout="slider"] .gallery-cell img { display: block; width: 100%; height: 100%; object-fit: contain; }
.wp-block-gallery.custom-layout[data-layout="slider"] .flickity-enabled { outline: none; }
.wp-block-gallery.custom-layout[data-layout="slider"] .flickity-page-dots { bottom: -40px; }
.wp-block-gallery.custom-layout[data-layout="slider"] .flickity-prev-next-button { background: rgba(255, 255, 255, 0.8); border-radius: 50%; }

/* --- COMBO LAYOUTS --- */
.wp-block-gallery.custom-layout[data-layout^="hero-"] > *,
.wp-block-gallery.custom-layout[data-layout^="symmetrical-"] > *,
.wp-block-gallery.custom-layout[data-layout^="diamond-"] > *,
.wp-block-gallery.custom-layout[data-layout^="asymmetric-"] > *,
.wp-block-gallery.custom-layout[data-layout^="cinema-"] > *,
.wp-block-gallery.custom-layout[data-layout^="puzzle-"] > *,
.wp-block-gallery.custom-layout[data-layout^="t-shape-"] > *,
.wp-block-gallery.custom-layout[data-layout="featuredbig"] > *,
.wp-block-gallery.custom-layout[data-layout="splitfocus"] > * {
    height: 100%;
    width: 100%;
    overflow: hidden;
}

/* --- AE. 均分列 (Equal Row) - [升級版：完整顯示，高度一致] --- */
.wp-block-gallery.custom-layout[data-layout="equal-row"] {
    display: flex !important;
    gap: 10px;
    width: 100%;
    /* [修改前] align-items: flex-start; */
    align-items: stretch; /* [修改後] 確保所有項目從頂部對齊 */
}

.wp-block-gallery.custom-layout[data-layout="equal-row"] .wp-block-image {
    /* 
     * 核心：讓每個 flex item (圖片容器) 的寬度
     * 根據其內容 (圖片) 的原始寬度來按比例分配空間。
     * flex-grow: 1; -> 允許放大以填滿空間
     * flex-shrink: 1; -> 允許縮小
     * flex-basis: 0; -> 初始寬度為0，完全依賴比例計算
     */
    flex: 1 1 0;
    min-width: 0; /* 防止 flex item 在內容過多時溢出 */
}

.wp-block-gallery.custom-layout[data-layout="equal-row"] .wp-block-image a,
.wp-block-gallery.custom-layout[data-layout="equal-row"] .wp-block-image img {
    display: block;
    width: 100%;   /* 圖片寬度填滿其容器 */
    /* [修改前] height: auto; */
    height: 100%;  /* [修改後] 關鍵：讓圖片高度自動根據其寬度和原始比例調整 */
    
    /* [重要] 移除 object-fit，因為我們現在要完整顯示圖片 */
    object-fit: cover; /* [新增] 讓圖片填滿容器，可能會裁切部分內容 */
}

/* --- AF. 結構化網格系統 (Structured Grid System) - [完整顯示版] --- */
.wp-block-gallery.custom-layout[data-layout^="grid-"] {
    display: grid !important;
    width: 100%;
    
    /* [核心修改] 分別定義行和列的間距 */
    row-gap: 50px;    /* 增加行間距，為說明文字預留足夠的垂直空間 */
    column-gap: 10px; /* 保持列間距不變 */
}

.wp-block-gallery.custom-layout[data-layout^="grid-"] .wp-block-image {
    background-color: #f8f8f8;
}

.wp-block-gallery.custom-layout[data-layout^="grid-"] .wp-block-image img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain; /* 核心：所有網格都完整顯示圖片 */
}


/* --- 個別網格定義 --- */

/* 方格佈局 (2x2) */
.wp-block-gallery.custom-layout[data-layout="grid-2x2"] {
    grid-template-columns: repeat(2, 1fr);
    /* 高度由內容決定 */
}

/* [新增] 網格 3x2 (3列, 2行) */
.wp-block-gallery.custom-layout[data-layout="grid-3x2"] {
    grid-template-columns: repeat(3, 1fr);
}

/* [新增] 網格 2x3 (2列, 3行) */
.wp-block-gallery.custom-layout[data-layout="grid-2x3"] {
    grid-template-columns: repeat(2, 1fr);
}

/* [新增] 方格佈局 (3x3) */
.wp-block-gallery.custom-layout[data-layout="grid-3x3"] {
    grid-template-columns: repeat(3, 1fr);
}

/* [新增] 網格 4x2 (4列, 2行) */
.wp-block-gallery.custom-layout[data-layout="grid-4x2"] {
    grid-template-columns: repeat(4, 1fr);
}

/* [新增] 網格 2x4 (2列, 4行) */
.wp-block-gallery.custom-layout[data-layout="grid-2x4"] {
    grid-template-columns: repeat(2, 1fr);
}

/* [新增] 方格佈局 (4x4) */
.wp-block-gallery.custom-layout[data-layout="grid-4x4"] {
    grid-template-columns: repeat(4, 1fr);
}

/* [優化] 為方形網格 (2x2, 3x3, 4x4) 添加 aspect-ratio，使其趨向於正方形 */
.wp-block-gallery.custom-layout[data-layout="grid-2x2"],
.wp-block-gallery.custom-layout[data-layout="grid-3x3"],
.wp-block-gallery.custom-layout[data-layout="grid-4x4"] {
   /* aspect-ratio: 1 / 1;
    max-height: 90vh;*/
}


/* S. Hero Left Stack */
.wp-block-gallery.custom-layout[data-layout="hero-left-stack"] { display: grid !important; grid-template-columns: 2fr 1fr; grid-template-rows: 1fr 1fr; gap: 10px; height: 400px; }
.wp-block-gallery.custom-layout[data-layout="hero-left-stack"] > *:first-child { grid-row: 1 / 3; grid-column: 1 / 2; }
.wp-block-gallery.custom-layout[data-layout="hero-left-stack"] .wp-block-image img { object-fit: cover; }

/* T. Hero Top Four */
.wp-block-gallery.custom-layout[data-layout="hero-top-four"] { display: grid !important; grid-template-columns: repeat(4, 1fr); grid-template-rows: 2fr 1fr; gap: 10px; /*height: 450px;*/ }
.wp-block-gallery.custom-layout[data-layout="hero-top-four"] > *:first-child { grid-column: 1 / 5; grid-row: 1 / 2; }
.wp-block-gallery.custom-layout[data-layout="hero-top-four"] .wp-block-image img { object-fit: cover; }

/* U. Symmetrical Sides */
.wp-block-gallery.custom-layout[data-layout="symmetrical-sides"] { display: grid !important; grid-template-columns: 1fr 2fr 1fr; gap: 10px; height: 350px; }
.wp-block-gallery.custom-layout[data-layout="symmetrical-sides"] > *:nth-child(2) { grid-column: 2 / 3; }
.wp-block-gallery.custom-layout[data-layout="symmetrical-sides"] .wp-block-image img { object-fit: cover; }

/* V. Diamond Focus */
.wp-block-gallery.custom-layout[data-layout="diamond-focus"] { display: grid !important; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); gap: 10px; height: 400px; }
.wp-block-gallery.custom-layout[data-layout="diamond-focus"] > *:nth-child(1) { grid-column: 2 / 3; grid-row: 1 / 2; }
.wp-block-gallery.custom-layout[data-layout="diamond-focus"] > *:nth-child(2) { grid-column: 1 / 2; grid-row: 2 / 3; }
.wp-block-gallery.custom-layout[data-layout="diamond-focus"] > *:nth-child(3) { grid-column: 2 / 3; grid-row: 2 / 3; }
.wp-block-gallery.custom-layout[data-layout="diamond-focus"] > *:nth-child(4) { grid-column: 3 / 4; grid-row: 2 / 3; }
.wp-block-gallery.custom-layout[data-layout="diamond-focus"] > *:nth-child(5) { grid-column: 2 / 3; grid-row: 3 / 4; }
.wp-block-gallery.custom-layout[data-layout="diamond-focus"] .wp-block-image img { object-fit: cover; }

/* W. Asymmetric Duo */
.wp-block-gallery.custom-layout[data-layout="asymmetric-duo"] { display: grid !important; grid-template-columns: 3fr 2fr; grid-template-rows: 1fr 1fr 1fr; gap: 10px; height: 450px; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-duo"] > *:nth-child(1) { grid-column: 1 / 2; grid-row: 1 / 3; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-duo"] > *:nth-child(2) { grid-column: 2 / 3; grid-row: 1 / 2; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-duo"] > *:nth-child(3) { grid-column: 1 / 2; grid-row: 3 / 4; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-duo"] > *:nth-child(4) { grid-column: 2 / 3; grid-row: 2 / 4; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-duo"] .wp-block-image img { object-fit: cover; }


/* ==========================================================================
   [新增] 全系列不對稱版型 (3-9+ 張圖片)
   ========================================================================== */

/* AA. 不對稱三圖 (Asymmetric Trio) */
.wp-block-gallery.custom-layout[data-layout="asymmetric-tri"] {
    display: grid !important;
    grid-template-columns: 2fr 1fr;
    grid-template-rows: 1fr 1fr;
    gap: 10px;
    height: clamp(350px, 50vh, 450px);
}
.wp-block-gallery.custom-layout[data-layout="asymmetric-tri"] > *:nth-child(1) {
    grid-column: 1 / 2;
    grid-row: 1 / 3;
}
.wp-block-gallery.custom-layout[data-layout="asymmetric-tri"] .wp-block-image img { object-fit: cover; }

/* AB. 不對稱四圖 (Asymmetric Quad Stack) */
.wp-block-gallery.custom-layout[data-layout="asymmetric-quad-stack"] {
    display: grid !important;
    grid-template-columns: 2fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    gap: 10px;
    height: clamp(450px, 70vh, 600px);
}
.wp-block-gallery.custom-layout[data-layout="asymmetric-quad-stack"] > *:nth-child(1) {
    grid-column: 1 / 2;
    grid-row: 1 / 4;
}
.wp-block-gallery.custom-layout[data-layout="asymmetric-quad-stack"] .wp-block-image img { object-fit: cover; }

/* AC. 不對稱五圖 (Asymmetric Penta) */
.wp-block-gallery.custom-layout[data-layout="asymmetric-penta"] {
    display: grid !important;
    grid-template-columns: 2fr 1.2fr;
    grid-template-rows: 1fr 1fr 1fr;
    gap: 10px;
    height: clamp(450px, 65vh, 650px);
}
.wp-block-gallery.custom-layout[data-layout="asymmetric-penta"] > *:nth-child(1) { grid-column: 1 / 2; grid-row: 1 / 3; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-penta"] > *:nth-child(2) { grid-column: 2 / 3; grid-row: 1 / 2; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-penta"] > *:nth-child(3) { grid-column: 2 / 3; grid-row: 2 / 3; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-penta"] > *:nth-child(4) { grid-column: 1 / 2; grid-row: 3 / 4; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-penta"] > *:nth-child(5) { grid-column: 2 / 3; grid-row: 3 / 4; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-penta"] .wp-block-image img { object-fit: cover; }


/* AD. 不對稱六/七圖 (Asymmetric Hex/Sept) - 此版型對6或7張圖片效果都很好 */
.wp-block-gallery.custom-layout[data-layout="asymmetric-hex"] {
    display: grid !important;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 10px;
    height: clamp(500px, 75vh, 700px);
}
.wp-block-gallery.custom-layout[data-layout="asymmetric-hex"] > *:nth-child(1) { grid-column: 1 / 2; grid-row: 1 / 3; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-hex"] > *:nth-child(2) { grid-column: 2 / 4; grid-row: 1 / 2; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-hex"] > *:nth-child(3) { grid-column: 2 / 3; grid-row: 2 / 3; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-hex"] > *:nth-child(4) { grid-column: 3 / 4; grid-row: 2 / 3; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-hex"] > *:nth-child(5) { grid-column: 1 / 2; grid-row: 3 / 4; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-hex"] > *:nth-child(6) { grid-column: 2 / 3; grid-row: 3 / 4; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-hex"] > *:nth-child(7) { grid-column: 3 / 4; grid-row: 3 / 4; }
.wp-block-gallery.custom-layout[data-layout="asymmetric-hex"] .wp-block-image img { object-fit: cover; }


/* AE. 動態不對稱 (Asymmetric Dynamic) - 適用於 7-12+ 張圖片 */
.wp-block-gallery.custom-layout[data-layout="asymmetric-dynamic"] {
    display: grid !important;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    grid-auto-rows: clamp(150px, 20vh, 200px);
    grid-auto-flow: dense; /* 關鍵：允許項目自動填充空白，創造拼圖感 */
    gap: 10px;
}
/* 透過 nth-child 讓特定圖片變大，產生不規則的視覺焦點 */
.wp-block-gallery.custom-layout[data-layout="asymmetric-dynamic"] > *:nth-child(5n + 1) {
    grid-column: span 2;
    grid-row: span 1;
}
.wp-block-gallery.custom-layout[data-layout="asymmetric-dynamic"] > *:nth-child(7n + 2) {
    grid-column: span 1;
    grid-row: span 2;
}
/* 確保第1張圖總是最大的，作為視覺起點 */
.wp-block-gallery.custom-layout[data-layout="asymmetric-dynamic"] > *:first-child {
    grid-column: span 2;
    grid-row: span 2;
}
.wp-block-gallery.custom-layout[data-layout="asymmetric-dynamic"] .wp-block-image img { object-fit: cover; }


/* X. Cinema Strip */
.wp-block-gallery.custom-layout[data-layout="cinema-strip"] { display: grid !important; grid-template-columns: repeat(4, 1fr); grid-template-rows: 3fr 1fr; gap: 10px; height: 400px; }
.wp-block-gallery.custom-layout[data-layout="cinema-strip"] > *:first-child { grid-column: 1 / 5; grid-row: 1 / 2; }
.wp-block-gallery.custom-layout[data-layout="cinema-strip"] .wp-block-image img { object-fit: cover; }

/* Y. Puzzle Grid */
.wp-block-gallery.custom-layout[data-layout="puzzle-grid"] { display: grid !important; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(2, 1fr); gap: 10px; height: 350px; }
.wp-block-gallery.custom-layout[data-layout="puzzle-grid"] > *:nth-child(1) { grid-column: 1 / 3; grid-row: 1 / 2; }
.wp-block-gallery.custom-layout[data-layout="puzzle-grid"] > *:nth-child(2) { grid-column: 3 / 5; grid-row: 1 / 2; }
.wp-block-gallery.custom-layout[data-layout="puzzle-grid"] > *:nth-child(3) { grid-column: 1 / 2; grid-row: 2 / 3; }
.wp-block-gallery.custom-layout[data-layout="puzzle-grid"] > *:nth-child(4) { grid-column: 2 / 4; grid-row: 2 / 3; }
.wp-block-gallery.custom-layout[data-layout="puzzle-grid"] > *:nth-child(5) { grid-column: 4 / 5; grid-row: 2 / 3; }
.wp-block-gallery.custom-layout[data-layout="puzzle-grid"] .wp-block-image img { object-fit: cover; }

/* Z. T-Shape Layout */
.wp-block-gallery.custom-layout[data-layout="t-shape-layout"] { display: grid !important; grid-template-columns: repeat(2, 1fr); grid-template-rows: 2fr 1fr; gap: 10px; height: 400px; }
.wp-block-gallery.custom-layout[data-layout="t-shape-layout"] > *:first-child { grid-column: 1 / 3; grid-row: 1 / 2; }
.wp-block-gallery.custom-layout[data-layout="t-shape-layout"] .wp-block-image img { object-fit: cover; }


/* --- 5. SMART CAPTION STYLING (Whitelist Version) --- */
/*
 * 最終策略：白名單模式。
 * 1. 預設隱藏所有自訂圖庫的圖片說明，確保佈局絕對穩定。
 * 2. 只為被允許的、佈局安全的排版（如雙欄）恢復說明的可見性。
 */

/* ==========================================================================
   Part 5.1: 全局預設 - 徹底隱藏所有說明
   - 這是我們的基礎規則，確保像 Justified 這類敏感佈局不受任何影響。
   - 使用 display: none !important; 確保最高優先級。
   ========================================================================== */

.wp-block-gallery.custom-layout .wp-block-image figcaption {
    display: none !important;
}

/* ==========================================================================
   Part 5.2: 白名單 - 恢復特定佈局的說明可見性 (最終修正版)
   ========================================================================== */
   
/* 步驟 1: 這是核心。將圖片容器本身變成一個 Grid，
   以便我們能將圖片和說明放在同一單元格內重疊。*/
.wp-block-gallery.custom-layout[data-layout="twocols"] .wp-block-image,
.wp-block-gallery.custom-layout[data-layout="masonry"] .wp-block-image,
.wp-block-gallery.custom-layout[data-layout="offsetcols"] .wp-block-image,
.wp-block-gallery.custom-layout[data-layout^="grid-"] .wp-block-image, /* 針對所有 grid-* 版型 */
.wp-block-gallery.custom-layout[data-layout^="1"] .wp-block-image,
.wp-block-gallery.custom-layout[data-layout^="2"] .wp-block-image,
.wp-block-gallery.custom-layout[data-layout^="3"] .wp-block-image,
.wp-block-gallery.custom-layout[data-layout^="4"] .wp-block-image,
.wp-block-gallery.custom-layout[data-layout^="5"] .wp-block-image,
.wp-block-gallery.custom-layout[data-layout^="6"] .wp-block-image,
.wp-block-gallery.custom-layout[data-layout^="7"] .wp-block-image {
    position: relative; /* 仍然需要相對定位作為絕對定位的錨點 */
    overflow: visible; /* 允許絕對定位的說明文字顯示在容器外部 */
}

/* 步驟 2: 讓圖片和說明都佔據網格的第一行第一列，使它們重疊 */
.wp-block-gallery.custom-layout .wp-block-image > img,
.wp-block-gallery.custom-layout .wp-block-image > figcaption {
    grid-row: 1 / 2;
    grid-column: 1 / 2;
}

/* 步驟 3: 圖片樣式 - 讓圖片撐滿整個容器 */
.wp-block-gallery.custom-layout .wp-block-image > img {
    width: 100%;
    height: 100%;
    /* 根據您的需求，可以是 contain 或 cover */
    object-fit: contain; 
}


/* 步驟 4: 說明的樣式 - [優化版] 移除陰影並美化顯示 */
.wp-block-gallery.custom-layout[data-layout="twocols"] .wp-block-image figcaption,
.wp-block-gallery.custom-layout[data-layout="masonry"] .wp-block-image figcaption,
.wp-block-gallery.custom-layout[data-layout="offsetcols"] .wp-block-image figcaption,
.wp-block-gallery.custom-layout[data-layout^="grid-"] .wp-block-image figcaption, /* 針對所有 grid-* 版型 */
.wp-block-gallery.custom-layout[data-layout^="1"] .wp-block-image figcaption,
.wp-block-gallery.custom-layout[data-layout^="2"] .wp-block-image figcaption,
.wp-block-gallery.custom-layout[data-layout^="3"] .wp-block-image figcaption,
.wp-block-gallery.custom-layout[data-layout^="4"] .wp-block-image figcaption,
.wp-block-gallery.custom-layout[data-layout^="5"] .wp-block-image figcaption,
.wp-block-gallery.custom-layout[data-layout^="6"] .wp-block-image figcaption,
.wp-block-gallery.custom-layout[data-layout^="7"] .wp-block-image figcaption {
    display: block !important;
    
    /* --- 核心定位 --- */
    position: absolute;
    top: 100%; /* 定位到容器底部外側 */
    left: 0;
    width: 100%;
    
    /* --- [改善] 視覺美化 --- */
    background: none;
    color: #555; /* 使用深灰色，比純黑色更柔和，增加質感 */
    font-size: 0.85em; /* 略微縮小字體，使其作為輔助說明文字更精緻 */
    font-style: italic; /* 採用斜體，增添文藝感 */
    line-height: 1.4;
    padding: 10px 5px 0; /* 微調上方間距，增加呼吸感 */
    text-align: center;
    text-shadow: none !important; /* [核心要求] 強制移除任何可能繼承自佈景主題的文字陰影 */
    z-index: 2; /* 確保說明文字在其他內容之上 */
}

/* ==========================================================================
   Part 5.3: 全域性規則 (Global Rules)
   - 這些規則適用於所有自訂圖庫，無論說明是否可見。
   ========================================================================== */

/* 如果圖片有說明（即使它被設為 display: none），
   依然禁用滑鼠懸停的放大效果。這個邏輯可以保持不變。*/
.wp-block-gallery.custom-layout .wp-block-image:has(figcaption):hover img,
.wp-block-gallery.custom-layout .wp-block-image:has(.wp-element-caption):hover img {
    transform: scale(1);
}


/* --- 6. RESPONSIVE DESIGN (RWD) --- */

/**
 * 6.1 TABLET STYLES (螢幕寬度 <= 768px)
 */
@media (max-width: 768px) {
    .wp-block-gallery.custom-layout[data-layout="slider"] {
        width: 100vw;
        max-width: 100vw;
        margin-left: calc(50% - 50vw);
        margin-right: calc(50% - 50vw);
    }
    
    /* 基本網格 */
    .wp-block-gallery.custom-layout[data-layout="twocols"],
    .wp-block-gallery.custom-layout[data-layout="3"],
    .wp-block-gallery.custom-layout[data-layout="4"] { grid-template-columns: repeat(2, 1fr) !important; }
    .wp-block-gallery.custom-layout[data-layout="5"],
    .wp-block-gallery.custom-layout[data-layout="6"],
    .wp-block-gallery.custom-layout[data-layout="7"] { grid-template-columns: repeat(3, 1fr) !important; }

    /* 特殊版型 */
    .wp-block-gallery.custom-layout[data-layout="masonry"] { column-count: 2; }
    .wp-block-gallery.custom-layout[data-layout="circlegrid"] { 
        /* 修改7: 平板上調整最小寬度和內邊距 */
        grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
        padding: 20px 15px;
        gap: 15px;
    }
    
    .wp-block-gallery.custom-layout[data-layout="circlegrid"] .wp-block-image {
        min-width: 100px;
        max-width: 200px;
    }
}

/**
 * 6.2 MOBILE STYLES (螢幕寬度 <= 480px)
 */
@media (max-width: 480px) {
    /* Slider 強制變為單欄 */
    .wp-block-gallery.custom-layout[data-layout="slider"][data-slider-cols="2"] .gallery-cell,
    .wp-block-gallery.custom-layout[data-layout="slider"][data-slider-cols="3"] .gallery-cell,
    .wp-block-gallery.custom-layout[data-layout="slider"][data-slider-cols="4"] .gallery-cell {
        width: 100%;
    }

    /* 基本網格全部變為單欄 (除了 twocols) */
    .wp-block-gallery.custom-layout[data-layout^="2"],
    .wp-block-gallery.custom-layout[data-layout^="3"],
    .wp-block-gallery.custom-layout[data-layout^="4"],
    .wp-block-gallery.custom-layout[data-layout^="5"],
    .wp-block-gallery.custom-layout[data-layout^="6"],
    .wp-block-gallery.custom-layout[data-layout^="7"] { 
        grid-template-columns: 1fr !important; 
        gap: 10px; 
    }

    /* 特殊版型 */
    .wp-block-gallery.custom-layout[data-layout="masonry"] { column-count: 1; }
    .wp-block-gallery.custom-layout[data-layout="diagstack"] { height: auto; }
    .wp-block-gallery.custom-layout[data-layout="diagstack"] .wp-block-image { position: static !important; width: 100% !important; height: auto !important; transform: none !important; margin-bottom: 10px; }

    .wp-block-gallery.custom-layout[data-layout="circlegrid"] { 
        /* 修改8: 手機上進一步調整，確保至少能放下2個圓形 */
        grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
        padding: 15px 10px;
        gap: 10px;
    }
    
    .wp-block-gallery.custom-layout[data-layout="circlegrid"] .wp-block-image {
        min-width: 80px;
        max-width: 150px;
    }
}

/* 修改9: 添加容器最小寬度保護 */
@media (max-width: 320px) {
    .wp-block-gallery.custom-layout[data-layout="circlegrid"] {
        grid-template-columns: repeat(2, 1fr);
        padding: 10px 5px;
    }
}

/* --- 7. ADDITIONAL UTILITY CLASSES --- */
.wp-block-gallery.custom-layout.layout-ready { opacity: 1; }
.wp-block-gallery.custom-layout a { display: block; text-decoration: none; color: inherit; }
.wp-block-gallery.custom-layout a:hover { text-decoration: none; }
body.baguetteBox-open { overflow: hidden; }
body.baguetteBox-open .baguetteBox-overlay { transition: opacity 0.3s ease; }
.wp-block-gallery.custom-layout .wp-block-image:focus-within,
.wp-block-gallery.custom-layout a:focus { outline: 2px solid #0073aa; outline-offset: 2px; }

@media print {
    .wp-block-gallery.custom-layout { display: block !important; overflow: visible !important; height: auto !important; page-break-inside: avoid; }
    .wp-block-gallery.custom-layout .wp-block-image { display: block !important; width: 100% !important; height: auto !important; margin-bottom: 10px !important; break-inside: avoid; }
    .wp-block-gallery.custom-layout .wp-block-image img { max-width: 100% !important; height: auto !important; object-fit: contain !important; }
}

@media (prefers-contrast: high) {
    .wp-block-gallery.custom-layout[data-layout="polaroid"] .wp-block-image { border: 2px solid black; }
    .wp-block-gallery.custom-layout[data-layout="filmstrip"] { border-top: 2px solid black; border-bottom: 2px solid black; }
}

@media (prefers-reduced-motion: reduce) {
    .wp-block-gallery.custom-layout .wp-block-image,
    .wp-block-gallery.custom-layout img,
    .wp-block-gallery.custom-layout [data-layout] .wp-block-image,
    .wp-block-gallery.custom-layout [data-layout] .wp-block-image:hover,
    .wp-block-gallery.custom-layout [data-layout] .wp-block-image:hover img {
        transition: none !important;
        transform: none !important;
    }
    .wp-block-gallery.custom-layout:not(.layout-ready)::before { animation: none !important; }
}


/**
 * Lightbox Vertical Padding Fix
 * Adds 40px of space to the top and bottom of the lightbox overlay
 * to prevent the image from touching the screen edges.
 */
#baguetteBox-slider {
    padding-top: 40px !important;
    padding-bottom: 40px !important;
}

#baguetteBox-slider img {
    max-height: calc(100vh - 80px) !important;
}

/* --- [新增] Lightbox 體驗優化 --- */

/**
 * 為燈箱中的透明圖片 (如 PNG) 添加白色背景。
 * 這可以確保無論圖片內容是什麼顏色，
 * 在深色的燈箱蒙版上都清晰可見。
 * 這個樣式對普通照片 (JPG) 沒有任何負面影響。
 */
#baguetteBox-slider .full-image img {
    background-color: #FFFFFF;
    border-radius: 4px; /* 可選：為背景添加一個小圓角，更精緻 */
    box-shadow: 0 0 15px rgba(0,0,0,0.15); /* 可選：為圖片添加一點陰影，增加立體感 */
}


/* ==========================================================================
   [新增 2025-12] 8 種新版型 CSS 樣式
   ========================================================================== */

/* --- A1. Carousel 輪播圖 --- */
/* 基於 Slider，但有不同的視覺風格 */
.wp-block-gallery.custom-layout[data-layout="carousel"] {
    height: 60vh;
    max-height: 550px;
    min-height: 350px;
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
    border-radius: 12px;
    padding: 20px;
    display: block;
    opacity: 1;
}
.wp-block-gallery.custom-layout[data-layout="carousel"] .flickity-viewport,
.wp-block-gallery.custom-layout[data-layout="carousel"] .flickity-slider { height: 100%; }
.wp-block-gallery.custom-layout[data-layout="carousel"] .gallery-cell {
    height: 100%;
    margin-right: 15px;
    box-sizing: border-box;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}
.wp-block-gallery.custom-layout[data-layout="carousel"] .gallery-cell img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.wp-block-gallery.custom-layout[data-layout="carousel"] .flickity-page-dots {
    bottom: -30px;
}
.wp-block-gallery.custom-layout[data-layout="carousel"] .flickity-page-dots .dot {
    background: rgba(255, 255, 255, 0.5);
}
.wp-block-gallery.custom-layout[data-layout="carousel"] .flickity-page-dots .dot.is-selected {
    background: #fff;
}
.wp-block-gallery.custom-layout[data-layout="carousel"] .flickity-prev-next-button {
    background: rgba(255, 255, 255, 0.9);
    border-radius: 50%;
}


/* --- A2. Panorama 全景滾動 --- */
.wp-block-gallery.custom-layout[data-layout="panorama"] {
    display: block !important;
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    padding: 20px 0;
    background: linear-gradient(to right, #0f0c29, #302b63, #24243e);
    border-radius: 8px;
}
.wp-block-gallery.custom-layout[data-layout="panorama"] .wp-block-image {
    display: inline-block !important;
    width: auto !important;
    height: 400px !important;
    margin-right: 5px;
    scroll-snap-align: start;
    vertical-align: top;
    white-space: normal;
    flex: none !important;
}
.wp-block-gallery.custom-layout[data-layout="panorama"] .wp-block-image img {
    height: 100%;
    width: auto !important;
    max-width: none;
    object-fit: cover;
}
/* 滾動條美化 */
.wp-block-gallery.custom-layout[data-layout="panorama"]::-webkit-scrollbar {
    height: 8px;
}
.wp-block-gallery.custom-layout[data-layout="panorama"]::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
}
.wp-block-gallery.custom-layout[data-layout="panorama"]::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.4);
    border-radius: 4px;
}


/* --- A3. Staggered 錯落排列 --- */
.wp-block-gallery.custom-layout[data-layout="staggered"] {
    display: grid !important;
    grid-template-columns: repeat(4, 1fr);
    gap: 15px;
    padding: 20px;
}
.wp-block-gallery.custom-layout[data-layout="staggered"] .wp-block-image {
    border-radius: 8px;
    overflow: hidden;
    transition: transform 0.4s ease, box-shadow 0.4s ease;
}
.wp-block-gallery.custom-layout[data-layout="staggered"] .wp-block-image:nth-child(odd) {
    transform: translateY(20px);
}
.wp-block-gallery.custom-layout[data-layout="staggered"] .wp-block-image:nth-child(even) {
    transform: translateY(-20px);
}
.wp-block-gallery.custom-layout[data-layout="staggered"] .wp-block-image:hover {
    transform: translateY(0) scale(1.02);
    box-shadow: 0 15px 40px rgba(0, 0, 0, 0.2);
    z-index: 10;
}
.wp-block-gallery.custom-layout[data-layout="staggered"] .wp-block-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}


/* --- A4. Spotlight 聚光燈效果 --- */
.wp-block-gallery.custom-layout[data-layout="spotlight"] {
    display: grid !important;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 10px;
    background: #111;
    padding: 20px;
    border-radius: 12px;
}
.wp-block-gallery.custom-layout[data-layout="spotlight"] .wp-block-image {
    border-radius: 8px;
    overflow: hidden;
    transition: opacity 0.4s ease, transform 0.4s ease, filter 0.4s ease;
}
.wp-block-gallery.custom-layout[data-layout="spotlight"] .wp-block-image.spotlight-dimmed {
    opacity: 0.3;
    filter: grayscale(80%);
    transform: scale(0.95);
}
.wp-block-gallery.custom-layout[data-layout="spotlight"] .wp-block-image:not(.spotlight-dimmed) {
    z-index: 10;
    box-shadow: 0 0 30px rgba(255, 255, 255, 0.3);
}
.wp-block-gallery.custom-layout[data-layout="spotlight"] .wp-block-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}


/* --- A5. Timeline 時間軸 --- */
.wp-block-gallery.custom-layout[data-layout="timeline"] {
    display: flex !important;
    flex-direction: column;
    position: relative;
    padding-left: 60px;
    gap: 30px;
}
/* 時間軸線條 */
.wp-block-gallery.custom-layout[data-layout="timeline"]::before {
    content: '';
    position: absolute;
    left: 25px;
    top: 0;
    bottom: 0;
    width: 4px;
    background: linear-gradient(to bottom, #667eea 0%, #764ba2 100%);
    border-radius: 2px;
}
.wp-block-gallery.custom-layout[data-layout="timeline"] .wp-block-image {
    position: relative;
    width: 100% !important;
    max-width: 500px;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease;
}
/* 時間軸節點 */
.wp-block-gallery.custom-layout[data-layout="timeline"] .wp-block-image::before {
    content: '';
    position: absolute;
    left: -47px;
    top: 50%;
    transform: translateY(-50%);
    width: 16px;
    height: 16px;
    background: #667eea;
    border: 4px solid #fff;
    border-radius: 50%;
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.4);
    z-index: 2;
}
.wp-block-gallery.custom-layout[data-layout="timeline"] .wp-block-image:nth-child(even) {
    align-self: flex-end;
}
.wp-block-gallery.custom-layout[data-layout="timeline"] .wp-block-image:hover {
    transform: scale(1.02);
}
.wp-block-gallery.custom-layout[data-layout="timeline"] .wp-block-image img {
    width: 100%;
    height: auto;
    object-fit: cover;
}


/* --- A6. Flip Cards 翻轉卡片 --- */
.wp-block-gallery.custom-layout[data-layout="flip-cards"] {
    display: grid !important;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 20px;
    padding: 20px;
    perspective: 1000px;
}
.wp-block-gallery.custom-layout[data-layout="flip-cards"] .wp-block-image {
    height: 300px !important;
    position: relative;
    perspective: 1000px;
    cursor: pointer;
}
.wp-block-gallery.custom-layout[data-layout="flip-cards"] .flip-card-inner {
    position: relative;
    width: 100%;
    height: 100%;
    transition: transform 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    transform-style: preserve-3d;
}
.wp-block-gallery.custom-layout[data-layout="flip-cards"] .wp-block-image:hover .flip-card-inner {
    transform: rotateY(180deg);
}
.wp-block-gallery.custom-layout[data-layout="flip-cards"] .flip-card-front,
.wp-block-gallery.custom-layout[data-layout="flip-cards"] .flip-card-back {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    border-radius: 12px;
    overflow: hidden;
}
.wp-block-gallery.custom-layout[data-layout="flip-cards"] .flip-card-front {
    z-index: 2;
}
.wp-block-gallery.custom-layout[data-layout="flip-cards"] .flip-card-front img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.wp-block-gallery.custom-layout[data-layout="flip-cards"] .flip-card-back {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    transform: rotateY(180deg);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    box-sizing: border-box;
}
.wp-block-gallery.custom-layout[data-layout="flip-cards"] .flip-card-content {
    color: #fff;
    font-size: 1.1em;
    text-align: center;
    line-height: 1.6;
}


/* --- A7. Minimal 極簡風格 --- */
.wp-block-gallery.custom-layout[data-layout="minimal"] {
    display: flex !important;
    flex-direction: column;
    align-items: center;
    gap: 60px;
    padding: 40px 20px;
    background: #fafafa;
}
.wp-block-gallery.custom-layout[data-layout="minimal"] .wp-block-image {
    max-width: 800px;
    width: 100% !important;
    border: 1px solid #eee;
    padding: 20px;
    background: #fff;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
    transition: box-shadow 0.3s ease;
}
.wp-block-gallery.custom-layout[data-layout="minimal"] .wp-block-image:hover {
    box-shadow: 0 5px 30px rgba(0, 0, 0, 0.1);
}
.wp-block-gallery.custom-layout[data-layout="minimal"] .wp-block-image img {
    width: 100%;
    height: auto;
    object-fit: contain;
}
/* 極簡風格允許顯示說明 */
.wp-block-gallery.custom-layout[data-layout="minimal"] .wp-block-image figcaption {
    display: block !important;
    text-align: center;
    margin-top: 15px;
    font-size: 0.9em;
    color: #666;
    font-style: italic;
}


/* --- A8. Overlap 重疊堆積 --- */
.wp-block-gallery.custom-layout[data-layout="overlap"] {
    display: flex !important;
    justify-content: center;
    align-items: center;
    padding: 40px;
    min-height: 500px;
    position: relative;
}
.wp-block-gallery.custom-layout[data-layout="overlap"] .wp-block-image {
    position: absolute !important;
    width: clamp(200px, 40%, 350px) !important;
    height: auto !important;
    border-radius: 12px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
    transition: transform 0.4s ease, z-index 0s 0.2s;
    overflow: hidden;
}
/* 不同位置的圖片 */
.wp-block-gallery.custom-layout[data-layout="overlap"] .wp-block-image:nth-child(1) {
    transform: rotate(-8deg) translate(-30%, -20%);
    z-index: 1;
}
.wp-block-gallery.custom-layout[data-layout="overlap"] .wp-block-image:nth-child(2) {
    transform: rotate(5deg) translate(25%, 10%);
    z-index: 2;
}
.wp-block-gallery.custom-layout[data-layout="overlap"] .wp-block-image:nth-child(3) {
    transform: rotate(-3deg) translate(-15%, 25%);
    z-index: 3;
}
.wp-block-gallery.custom-layout[data-layout="overlap"] .wp-block-image:nth-child(4) {
    transform: rotate(8deg) translate(20%, -25%);
    z-index: 4;
}
.wp-block-gallery.custom-layout[data-layout="overlap"] .wp-block-image:nth-child(5) {
    transform: rotate(-5deg) translate(5%, 0);
    z-index: 5;
}
.wp-block-gallery.custom-layout[data-layout="overlap"] .wp-block-image:hover {
    transform: rotate(0deg) scale(1.1) !important;
    z-index: 100 !important;
    transition: transform 0.4s ease, z-index 0s;
}
.wp-block-gallery.custom-layout[data-layout="overlap"] .wp-block-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}


/* ==========================================================================
   [新增版型] 響應式設計
   ========================================================================== */

@media (max-width: 768px) {
    /* Panorama */
    .wp-block-gallery.custom-layout[data-layout="panorama"] .wp-block-image {
        height: 300px !important;
    }
    
    /* Staggered */
    .wp-block-gallery.custom-layout[data-layout="staggered"] {
        grid-template-columns: repeat(2, 1fr);
    }
    .wp-block-gallery.custom-layout[data-layout="staggered"] .wp-block-image:nth-child(odd),
    .wp-block-gallery.custom-layout[data-layout="staggered"] .wp-block-image:nth-child(even) {
        transform: none;
    }
    
    /* Timeline */
    .wp-block-gallery.custom-layout[data-layout="timeline"] {
        padding-left: 40px;
    }
    .wp-block-gallery.custom-layout[data-layout="timeline"]::before {
        left: 15px;
    }
    .wp-block-gallery.custom-layout[data-layout="timeline"] .wp-block-image::before {
        left: -33px;
        width: 12px;
        height: 12px;
    }
    
    /* Overlap */
    .wp-block-gallery.custom-layout[data-layout="overlap"] {
        min-height: 400px;
    }
    .wp-block-gallery.custom-layout[data-layout="overlap"] .wp-block-image {
        width: clamp(150px, 50%, 280px) !important;
    }
}

@media (max-width: 480px) {
    /* Carousel */
    .wp-block-gallery.custom-layout[data-layout="carousel"] {
        height: 45vh;
        min-height: 280px;
        padding: 10px;
    }
    
    /* Panorama */
    .wp-block-gallery.custom-layout[data-layout="panorama"] .wp-block-image {
        height: 220px !important;
    }
    
    /* Staggered - 變為單欄 */
    .wp-block-gallery.custom-layout[data-layout="staggered"] {
        grid-template-columns: 1fr;
    }
    
    /* Timeline */
    .wp-block-gallery.custom-layout[data-layout="timeline"] {
        padding-left: 30px;
    }
    .wp-block-gallery.custom-layout[data-layout="timeline"]::before {
        left: 10px;
        width: 3px;
    }
    .wp-block-gallery.custom-layout[data-layout="timeline"] .wp-block-image::before {
        left: -27px;
        width: 10px;
        height: 10px;
    }
    
    /* Flip Cards */
    .wp-block-gallery.custom-layout[data-layout="flip-cards"] .wp-block-image {
        height: 250px !important;
    }
    
    /* Minimal */
    .wp-block-gallery.custom-layout[data-layout="minimal"] {
        gap: 40px;
        padding: 20px 10px;
    }
    
    /* Overlap - 變為堆疊顯示 */
    .wp-block-gallery.custom-layout[data-layout="overlap"] {
        flex-direction: column;
        min-height: auto;
        gap: 20px;
        padding: 20px;
    }
    .wp-block-gallery.custom-layout[data-layout="overlap"] .wp-block-image {
        position: relative !important;
        width: 100% !important;
        transform: none !important;
    }
}