/* ==========================================================================
   Project Page - 2-Unit Modular Grid System
   作品集 2 單位模組化網格排版系統
   支援模塊：2x2, 1x2, 2x1, 1x1
   ========================================================================== */

#project-page {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 0 60px 0 var(--post-right-pos, 480px); /* 留白對齊：Post 右側往右 30px (上下 padding 移交給 container 避免 Safari 裁切 bug) */
    display: flex;
    flex-direction: column;
    align-items: flex-start; /* 改為 flex-start，讓容器齊左對齊於 var(--post-right-pos) */
}

.project-container {
    width: calc(100vw - var(--post-right-pos, 480px) - 60px);
    max-width: calc(100vw - var(--post-right-pos, 480px) - 60px);
    margin-left: 0;
    margin-right: 0;
    padding-top: 100px;
    padding-bottom: 140px;
}

/* 頂部標題區介紹 */
.project-header {
    margin-top: 0.5em;
    margin-bottom: 33.5px;
    border-bottom: 2px solid #111111;
    padding-bottom: 29px; /* 14px + 15px = 29px，確保黑線不動 */
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    flex-wrap: wrap;
    gap: 15px;
}

body.dark-mode .project-header {
    border-bottom-color: #ffffff;
}

.project-title-group h1 {
    font-size: 2.0em;
    font-weight: 600;
    letter-spacing: -1px;
    line-height: 1.1;
    color: #111111;
    margin-bottom: 8px;
    text-transform: uppercase;
}

body.dark-mode .project-title-group h1 {
    color: #ffffff;
}

.project-title-group p {
    font-size: 0.95em;
    color: #666666;
    margin: 25px 0 0 0; /* 原為 40px，往上拉 15px 以精確對齊右側 */
    font-family: 'IBM Plex Mono', monospace;
}

body.dark-mode .project-title-group p {
    color: #aaaaaa;
}

/* 模塊切換說明導覽條 */
.grid-legend {
    display: none; /* 隱藏標題下方的模塊尺寸說明標籤列 */
}

.legend-tag {
    font-family: 'IBM Plex Mono', monospace;
    font-size: 0.8em;
    padding: 4px 10px;
    border: 1px solid #cccccc;
    border-radius: 4px;
    color: #555555;
    background-color: rgba(0, 0, 0, 0.03);
}

body.dark-mode .legend-tag {
    border-color: #444444;
    color: #bbbbbb;
    background-color: rgba(255, 255, 255, 0.05);
}

/* ==========================================================================
   2-Unit Modular Grid Layout (兩單位模組化網格)
   ========================================================================== */
.project-grid {
    display: grid;
    /* 核心 2 欄網格：對應 2x2 (占滿兩欄兩列)、2x1 (占滿兩欄一列)、1x2 (占一欄)、1x1 (占一欄) */
    grid-template-columns: repeat(2, 1fr);
    grid-auto-rows: calc((100vw - var(--post-right-pos, 480px) - 84px) / 2); /* 讓單位長寬保持完美的 1:1 比例 */
    gap: 24px;
    grid-auto-flow: dense; /* 核心：自動將小模塊填補進空隙，形成緊密和諧的排版 */
    width: 100%;
    margin-bottom: 60px; /* 底部多給 60px 滑動範圍 */
}

/* --- 模塊尺寸定義 (2 單位系統) --- */

/* 1x1 模塊 (單格正方，占 1 欄 1 列) */
.mod-1x1 {
    grid-column: span 1;
    grid-row: span 1;
}

/* 2x1 模塊 (橫幅，占滿 2 欄 1 列) */
.mod-2x1 {
    grid-column: span 2;
    grid-row: span 1;
}

/* 1x2 模塊 (縱向長幅，占 1 欄 2 列) */
.mod-1x2 {
    grid-column: span 1;
    grid-row: span 2;
}

/* 2x2 模塊 (大旗艦方塊，占滿 2 欄 2 列) */
.mod-2x2 {
    grid-column: span 2;
    grid-row: span 2;
}

/* ==========================================================================
   Project Item Cards (作品模塊卡片視覺設計)
   ========================================================================== */
.project-item {
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    background-color: #ffffff;
    border: 1px solid #e2e2e2;
    border-radius: 8px;
    padding: 24px;
    overflow: hidden;
    cursor: pointer;
    text-decoration: none;
    color: inherit;
    transform: translateZ(0);
    backface-visibility: hidden;
    transition: transform 0.4s cubic-bezier(0.2, 1, 0.2, 1), 
                box-shadow 0.4s cubic-bezier(0.2, 1, 0.2, 1), 
                border-color 0.4s ease;
}

body.dark-mode .project-item {
    background-color: #141414;
    border-color: #262626;
    color: #eeeeee;
}

/* 懸停互動特效 */
.project-item:hover {
    transform: translateZ(0) translateY(-6px);
    box-shadow: 0 16px 32px rgba(0, 0, 0, 0.12);
    border-color: #000000;
    z-index: 5;
}

body.dark-mode .project-item:hover {
    box-shadow: 0 16px 32px rgba(0, 0, 0, 0.6);
    border-color: #ffffff;
}

/* 卡片頂部資訊列 (類別與尺寸徽章) */
.item-top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 2;
}

.item-category {
    font-family: 'IBM Plex Mono', monospace;
    font-size: 0.75em;
    font-weight: 700;
    letter-spacing: 1px;
    color: #777777;
    text-transform: uppercase;
}

body.dark-mode .item-category {
    color: #888888;
}

.module-badge {
    display: none; /* 隱藏卡片上的模塊尺寸小方框標籤 */
}

body.dark-mode .module-badge {
    background-color: rgba(255, 255, 255, 0.1);
    color: #dddddd;
}

.project-item:hover .module-badge {
    background-color: #000000;
    color: #ffffff;
}

body.dark-mode .project-item:hover .module-badge {
    background-color: #ffffff;
    color: #000000;
}

/* 中間視覺展示區域 (抽象圖形與插圖背景) */
.item-visual {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    pointer-events: none;
    opacity: 0.6;
    transition: transform 0.6s cubic-bezier(0.2, 1, 0.2, 1), opacity 0.4s ease;
}

.project-item:hover .item-visual {
    transform: scale(1.05);
    opacity: 0.85;
}

/* 視覺展示特效主題 (用於預設卡片呈現) */
.visual-grid-pattern {
    background-image: radial-gradient(#cccccc 1px, transparent 1px);
    background-size: 20px 20px;
}

body.dark-mode .visual-grid-pattern {
    background-image: radial-gradient(#333333 1px, transparent 1px);
}

.visual-gradient-1 {
    background: linear-gradient(135deg, rgba(0,0,0,0.02) 0%, rgba(0,0,0,0.08) 100%);
}
body.dark-mode .visual-gradient-1 {
    background: linear-gradient(135deg, rgba(255,255,255,0.02) 0%, rgba(255,255,255,0.08) 100%);
}

.visual-gradient-2 {
    background: radial-gradient(circle at 80% 20%, rgba(100,100,100,0.15) 0%, transparent 60%);
}

/* 卡片底部內容區 (標題與摘要) */
.item-content {
    z-index: 2;
    margin-top: auto;
    padding-top: 15px;
}

.item-title {
    font-size: 1.4em;
    font-weight: 800;
    line-height: 1.2;
    margin-bottom: 6px;
    color: #111111;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

body.dark-mode .item-title {
    color: #ffffff;
}

.item-arrow {
    font-size: 0.9em;
    opacity: 0;
    transform: translate(-10px, 10px);
    transition: opacity 0.3s ease, transform 0.3s cubic-bezier(0.2, 1, 0.2, 1);
}

.project-item:hover .item-arrow {
    opacity: 1;
    transform: translate(0, 0);
}

.item-desc {
    font-size: 0.9em;
    color: #666666;
    margin: 0;
    line-height: 1.5;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

body.dark-mode .item-desc {
    color: #999999;
}

/* 深色圖片背景主題的文字覆寫 (適用於照片偏暗時強制白字) */
.theme-dark-image .item-title,
.theme-dark-image .item-arrow {
    color: #ffffff !important;
}

.theme-dark-image .item-category,
.theme-dark-image .item-desc {
    color: rgba(255, 255, 255, 0.8) !important;
}

/* 移除預設的半透明遮罩，還原圖片真實色彩 */
.theme-dark-image .item-visual {
    opacity: 1 !important;
}


/* 特殊尺寸模塊字體微調 */
.mod-2x2 .item-title {
    font-size: 2.2em;
    margin-bottom: 12px;
}

.mod-2x2 .item-desc {
    font-size: 1.05em;
    -webkit-line-clamp: 3;
    max-width: 85%;
}

.mod-1x1 .item-title {
    font-size: 1.15em;
}

.mod-1x1 .item-desc {
    display: none; /* 1x1 空間緊湊，僅顯示標題與分類 */
}

/* ==========================================================================
   Responsive Adaptation (響應式調整)
   ========================================================================== */


/* 螢幕小於 1000px (手機與小螢幕)：精準安全區、對齊留白與現代自適應長寬比 */
@media (max-width: 1000px) {
    #project-page {
        padding: 0 var(--mobile-gutter, 16px);
        align-items: flex-start;
    }

    .project-container {
        padding: calc(75px + env(safe-area-inset-top, 0px)) 0 0 0;
        width: 100%;
        max-width: 100%;
        margin: 0 auto;
    }

    /* 底部安全緩衝區，精確避開全域導覽列，消除多餘巨額留白 */
    .project-container::after {
        content: "";
        display: block;
        height: calc(75px + env(safe-area-inset-bottom, 0px));
        width: 100%;
    }

    .project-header {
        margin-top: 0;
        margin-bottom: 24px;
        padding-bottom: 16px;
        border-bottom-width: 1.5px;
    }

    .project-title-group h1 {
        font-size: 1.6em;
        letter-spacing: -0.5px;
        margin-bottom: 4px;
    }

    .project-title-group p {
        font-size: 0.85em;
        margin-top: 8px;
    }

    .project-grid {
        grid-template-columns: 1fr;
        grid-auto-rows: auto;
        gap: 16px;
        margin-bottom: 20px;
    }

    /* 所有模塊在手機上一律改為單欄，並解除 span 2 避免高達 700px 的視覺失衡 */
    .mod-2x2, .mod-1x2, .mod-2x1, .mod-1x1 {
        grid-column: span 1 !important;
        grid-row: auto !important;
        width: 100%;
    }

    /* 精確賦予每種模塊最符合內容的手機幾何長寬比 */
    .mod-1x1 {
        aspect-ratio: 1 / 1;
        min-height: 260px;
    }

    .mod-2x1 {
        aspect-ratio: 16 / 9;
        min-height: 190px;
    }

    .mod-2x2 {
        aspect-ratio: 4 / 3;
        min-height: 280px;
        max-height: 380px;
    }

    .mod-1x2 {
        aspect-ratio: 4 / 5;
        min-height: 300px;
        max-height: 380px;
    }
    
    .project-item {
        padding: 18px;
    }

    .project-item .item-title {
        font-size: 1.15em;
    }

    .mod-1x1 .item-desc {
        display: -webkit-box;
        -webkit-line-clamp: 2;
    }

    /* 觸控按壓反饋 */
    .project-item:active {
        transform: scale(0.985);
    }

    /* 強制 GPU 合成層，避免快速滾動時標題與首張卡片來不及渲染 */
    .project-header,
    .project-grid {
        will-change: transform;
        transform: translateZ(0);
        backface-visibility: hidden;
    }
}

/* ==========================================================================
   Project 雙欄動態閱讀模式 (類 Post)
   ========================================================================== */

/* Hover 循環播放動態預覽影片支援 */
.project-item img.preview-image {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    z-index: 1;
    transition: opacity 0.4s ease;
    transform: translateZ(0);
    backface-visibility: hidden;
}

.project-item video.preview-video {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    pointer-events: none;
    z-index: 1;
    opacity: 0;
    transition: opacity 0.4s ease;
    transform: translateZ(0);
    backface-visibility: hidden;
}
@media (hover: hover) {
    .project-item:hover video.preview-video {
        opacity: 1;
    }
}

@media (min-width: 1001px) {
    /* 取消原本 page 級別的 padding，改由 flex 與 margin-left 定位 */
    #project-page {
        padding: 0 !important;
        align-items: flex-start !important;
        overflow: hidden !important; /* 鎖定頁面不滾動 */
        height: 100vh !important;
    }
    
    #project-page .project-container {
        display: flex;
        gap: 50px;
        align-items: flex-start;
        justify-content: flex-start;
        width: 100%;
        max-width: none !important;
        height: 100vh !important;
        max-height: 100vh !important;
        overflow: hidden !important;
        margin: 0 !important;
        padding: 0 !important; /* 移除雙重 padding，避免上面出現白邊 */
    }
    
    #project-page .project-list-wrapper {
        position: relative;
        height: 100vh !important;
        max-height: 100vh !important;
        padding-top: 100px !important;
        padding-bottom: 140px;
        overflow-y: auto !important;
        overscroll-behavior: contain;
        scrollbar-width: none;
        -ms-overflow-style: none;
        box-sizing: border-box;
        transition: margin-left 0.6s cubic-bezier(0.16, 1, 0.3, 1), 
                    padding-left 0.6s cubic-bezier(0.16, 1, 0.3, 1), 
                    padding-right 0.6s cubic-bezier(0.16, 1, 0.3, 1), 
                    width 0.6s cubic-bezier(0.16, 1, 0.3, 1), 
                    flex 0.6s cubic-bezier(0.16, 1, 0.3, 1),
                    padding-bottom 0.6s ease;
    }
    #project-page .project-list-wrapper::-webkit-scrollbar {
        display: none;
    }
    
    /* 狀態 A：未點選作品時（純列表模式） */
    #project-page:not(.has-active-project) .project-list-wrapper {
        margin-left: 0;
        padding-left: var(--post-right-pos, 480px); /* 用 padding 取代 margin，確保可滑動範圍覆蓋右側空區 */
        padding-right: 160px;

        flex: 0 0 100vw;
        width: 100vw;
    }
    #project-page:not(.has-active-project) .inline-post-view {
        flex: 0 0 0px;
        width: 0px;
        opacity: 0;
        pointer-events: none;
        transform: translateX(60px);
        overflow: hidden !important;
        padding: 0 !important;
        margin: 0 !important;
        border: none !important;
        transition: opacity 0.3s ease, 
                    transform 0.4s cubic-bezier(0.16, 1, 0.3, 1), 
                    flex 0.6s cubic-bezier(0.16, 1, 0.3, 1), 
                    width 0.6s cubic-bezier(0.16, 1, 0.3, 1);
    }

    /* 狀態 B：點選作品後（雙欄閱讀模式） */
    #project-page.has-active-project .project-list-wrapper {
        margin-left: 42px; /* 齊左對齊下列的 42px */
        padding-left: 0;
        padding-right: 0;
        flex: 0 0 var(--post-col-width, 420px);
        width: var(--post-col-width, 420px);
        padding-bottom: 40px;
    }
    /* 進入閱讀模式後，左側卡片網格一律改為單欄（一排一個） */
    #project-page.has-active-project .project-grid {
        grid-template-columns: 1fr !important;
    }
    #project-page.has-active-project .project-grid .project-item {
        grid-column: span 1 !important;
    }
    
    /* 為了消除網格排版改變瞬間的生硬跳動，導入淡出重繪過場 */
    #project-page .project-grid {
        transition: opacity 0.5s ease, transform 0.6s cubic-bezier(0.16, 1, 0.3, 1);
    }
    #project-page.is-switching .project-grid {
        opacity: 0;
        transform: translateY(15px);
    }
    
    /* 點開文章時的左側列表，同樣維持 1:1 幾何比例 */
    #project-page.has-active-project .project-grid {
        grid-auto-rows: var(--post-col-width, 420px);
    }

    #project-page.has-active-project .inline-post-view {
        flex: 1 1 0;
        width: auto;
        max-width: none !important; /* 右邊文章右側靠右！ */
        opacity: 1;
        pointer-events: auto;
        transform: translateX(0);
        padding-left: 0; /* 左側位置保持不變（由 gap: 50px 控制） */
        padding-right: 30px; /* 右側靠右，距離視窗邊緣 30px */
        border-left: none;
        height: 100vh !important;
        max-height: 100vh !important;
        padding-top: 0 !important; /* 上下 padding 移交給 content 避免 Safari 裁切 bug */
        padding-bottom: 0;
        overflow-y: auto !important; /* 獨立滾動右側文章 */
        overscroll-behavior: contain;
        box-sizing: border-box;
    }
    #project-page.has-active-project .inline-post-content {
        padding-top: 100px;
        padding-bottom: 120px;
    }
    #project-page.has-active-project .inline-post-view {
        transition: opacity 0.5s ease 0.15s, 
                    transform 0.6s cubic-bezier(0.16, 1, 0.3, 1) 0.1s, 
                    flex 0.6s cubic-bezier(0.16, 1, 0.3, 1), 
                    width 0.6s cubic-bezier(0.16, 1, 0.3, 1);
    }
}

/* 手機版 (max-width: 1000px)：滑動進出 (列表往左滑出、文章從右滑入) */
@media (max-width: 1000px) {
    #project-page .project-container {
        overflow-x: hidden; /* 改為 overflow-x: hidden，讓 y 軸正常由 #project-page 捲動 */
        position: relative;
        min-height: 400px;
        margin: 0;
    }
    
    #project-page .project-list-wrapper {
        transform: translateX(0);
        opacity: 1;
        pointer-events: auto;
        position: relative;
        width: 100%;
        transition: transform 0.6s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.5s ease;
    }
    #project-page .inline-post-view {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        min-height: 100%;
        transform: translateX(100%);
        opacity: 0;
        pointer-events: none;
        padding: calc(75px + env(safe-area-inset-top, 0px)) var(--mobile-gutter, 16px) calc(85px + env(safe-area-inset-bottom, 0px)) var(--mobile-gutter, 16px) !important;
        box-sizing: border-box;
        transition: transform 0.6s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.5s ease;
    }

    #project-page.has-active-project {
        overflow-y: auto !important;
        overflow-x: hidden !important;
    }
    #project-page.has-active-project .project-container {
        overflow: visible !important;
    }
    #project-page.has-active-project .project-container::after {
        display: none !important;
    }
    #project-page.has-active-project .project-list-wrapper {
        transform: translateX(-100%);
        opacity: 0;
        pointer-events: none;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 0;
        max-height: 0;
        overflow: hidden;
    }
    #project-page.has-active-project .inline-post-view {
        transform: translateX(0);
        opacity: 1;
        pointer-events: auto;
        position: relative;
        width: 100%;
        min-height: auto;
        height: auto;
        overflow: visible;
    }
}
