/* ./css/main.css*/
/* ========================================= */
/* 1. 核心深色主題與佈局 */
/* ========================================= */
html,
body {
    height: 100%;
    margin: 0;
    background: #121212;
    color: #e0e0e0;
    overflow: hidden;
    /* 確保主頁面沒有滾動條 */
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.main-layout {
    display: flex;
    height: 100vh;
    width: 100vw;
    padding: 10px;
    /* 整體邊距 */
    gap: 10px;
    /* 各區塊之間的間距 */
    background-color: #121212;
    justify-content: center;
    /* 讓所有內容在水平方向居中 */
    align-items: center;
    /* 讓所有內容在垂直方向居中 */
}

/* 確保模態框在深色模式下有更好的顯示效果 */
.modal-content.bg-dark {
    border: 1px solid #333;
}

/* --- 背景圖預覽樣式 (background-options) --- */
.background-options {
    display: flex;
    gap: 10px;
    /* 增加間距 */
    flex-wrap: wrap;
    /* 關鍵: 允許圖片換行，解決跑版問題 */
    max-height: 250px;
    /* 限制模態框內預覽區的高度 */
    overflow-y: auto;
    /* 超出高度時顯示滾動條 */
    padding-bottom: 5px;
    /* 底部空間，防止滾動條貼邊 */
    justify-content: flex-start;
    align-content: flex-start;
}

/* 每個圖片預覽的容器，用於保持 1:1 比例和居中 */
.background-options .modal-image-wrapper {
    position: relative;
    width: 80px;
    padding-bottom: 80px;
    /* 1:1 比例 */
    flex-shrink: 0;
    border: 3px solid transparent;
    border-radius: 5px;
    transition: all 0.2s ease-in-out;
    cursor: pointer;
    overflow: hidden;
    /* 確保圖片不超出邊界 */
}

.background-options .modal-image-wrapper:hover {
    border-color: #ffc107;
}

/* 被選中/激活 (active) 的圖片樣式 */
.background-options .modal-image-wrapper.active {
    border: 3px solid #17a2b8;
    transform: scale(1.05);
}

.background-options img {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 100%;
    /* 填充容器 */
    height: 100%;
    /* 填充容器 */
    object-fit: cover;
    /* 裁剪並填充，保持比例 */
    transform: translate(-50%, -50%);
    /* 居中圖片 */
    border-radius: 2px;
    /* 內部圖片的小圓角 */
}


/* AI 生成的圖片與預設圖片可能有所區分 */
.background-options .modal-image-wrapper.ai-generated-bg {
    border-color: #28a745;
    /* 綠色邊框 */
}

.background-options .modal-image-wrapper.ai-generated-bg.active {
    border: 3px solid #17a2b8;
    /* 選中時的邊框顏色 (info color) */
}

/* ------------------------------------- */
/* 工具欄容器 (獨立在最左側) */
/* ------------------------------------- */
.tool-bar-wrapper {
    background-color: #1e1e1e;
    border-radius: 12px;
    padding: 15px 8px;
    /* 垂直多一點，水平少一點 */
    display: flex;
    flex-direction: column;
    gap: 8px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
    flex-shrink: 0;
    width: 75px;
    /* 固定寬度 */
    height: calc(100vh - 20px);
    /* 讓工具欄的高度填滿可用空間 */
}

.tool-bar-wrapper .btn,
.tool-bar-wrapper .dropdown-toggle {
    width: 55px;
    height: 55px;
    font-size: 1.2rem;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 8px;
}

/* 畫筆設定下拉選單定位 */
.tool-bar-wrapper .brush-dropdown-menu {
    background-color: #2b2b2b;
    padding: 10px;
    min-width: 250px;
    left: 65px !important;
    top: 0 !important;
}

/* [新增] 圖形設定下拉選單定位 (與畫筆設定同級) */
.tool-bar-wrapper .shape-dropdown-menu {
    background-color: #2b2b2b;
    padding: 10px;
    min-width: 250px;
    left: 65px !important;
    top: 0 !important;
}


/* 左右容器 */
.drawing-section,
.result-section {
    background-color: #1e1e1e;
    border-radius: 12px;
    padding: 15px;
    display: flex;
    flex-direction: column;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
    overflow: hidden;

    /* --- 核心調整: 讓兩個區塊平均分配剩餘空間 --- */
    flex: 1 1 50%;
    /* 設置彈性基礎，讓兩區塊寬度相同 */

    /* 設置 max-width 確保在極寬螢幕上保持合理尺寸 */
    max-width: 50%;

    height: calc(100vh - 20px);
    /* 保持高度佔滿 */
}


/* ========================================= */
/* 2. 畫布 RWD 與控制項 (確保 1:1 比例充滿欄位) */
/* ========================================= */
/* 畫布容器 - 應用 1:1 比例 hack */
#canvas-container-wrapper {
    width: 100%;
    position: relative;
    background: #000;
    border: 1px solid #444;
    border-radius: 8px;
    flex-grow: 1;
    flex-shrink: 1;
    flex-basis: auto;
    min-height: 0;
    margin-bottom: 10px;
    /* 與下方控制項隔開 */

    /* 確保 1:1 比例的核心 CSS */
    padding-bottom: 100%;
    height: 0;
}

#doodleCanvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    /* Fabric.js 會根據這個 100% 尺寸來調整其顯示大小，但內部像素大小需要 JS 調整 */
    cursor: crosshair;
}

/* === 背景圖片在畫布區域的覆蓋顯示 (Fabric.js Canvas 自身的樣式) === */
canvas {
    display: block;
    width: 100% !important;
    height: 100% !important;
    /* 修正：應為 100% 填充容器 */
    object-fit: cover;
    /* 等比例覆蓋畫布 */
    background-size: cover !important;
    background-position: center center !important;
}

/* 結果圖片容器 - 應用 1:1 比例 hack */
#result-wrapper {
    width: 100%;
    position: relative;
    border: 1px solid #444;

    /* 確保 1:1 比例的核心 CSS */
    padding-bottom: 100%;
    height: 0;

    /* 🔥 修正：讓 result-wrapper 在 result-section 中佔滿所有可用空間 */
    flex-grow: 1;
    /* 允許它擴展 */
    flex-shrink: 1;
    /* 允許它收縮 */
    flex-basis: auto;
    /* 根據內容決定初始大小 */

    min-height: 0;
    /* 允許縮小到 0 */
    margin-bottom: 10px;
    /* 與下方按鈕隔開 */
}

/* ====================================
   🔥 跑版修正核心區段 (全域生效) 🔥
   ==================================== */

/* 確保 #result-wrapper 的 1:1 比例 hack 能正確作用 */
.result-section #result-wrapper {
    /* 覆蓋 HTML 中 style="height: auto;" 的影響 */
    height: 0 !important;
    padding-top: 100% !important;
    position: relative;
    /* 讓內部的 #result 絕對定位 */
}

/* 確保 #result 佔滿 #result-wrapper 的空間並居中 */
/* 確保 #result 佔滿 #result-wrapper 的空間並居中 (修正後) */
#result {
    position: absolute;
    /* 絕對定位，佔滿父容器 */
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    /* 確保 #result 容器高度是 100% */
    display: flex;
    /* 🚨 核心修正：將內容錨定到頂部 */
    align-items: flex-start;
    justify-content: center;
    overflow: hidden;
    /* 確保內容不溢出 */
}

/* 確保 img 適應 #result 容器 (修正後) */
#result img {
    /* 🚨 核心修正：寬度 100%，高度自動以保持比例 */
    width: 100%;
    height: auto;

    /* 確保邊緣不會被其他規則限制 */
    max-width: none;
    max-height: none;

    /* 保持圖片完整顯示，因為容器可能是正方形，底部可能會留白 */
    object-fit: contain;
    /* 錨點設為頂部，但由於 width: 100%，這裡主要是防止潛在的垂直對齊問題 */
    object-position: top center;

    /* 繼承 rounded shadow 類別的樣式 */
    border-radius: var(--bs-border-radius, 0.25rem);

    background-color: #000;
}

/* 底部控制區 */
#bottom-mode-controls {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: auto;
    flex-shrink: 0;
}

#promptInput {
    resize: none;
    background-color: #333;
    color: white;
    border: 1px solid #555;
    flex-grow: 1;
    height: 40px;
}

#micButton {
    height: 40px;
}

/* RWD 調整 */
@media (max-width: 991.98px) {
    .main-layout {
        flex-direction: column;
        height: auto;
        overflow-y: visible;
        padding: 10px;
        justify-content: flex-start;
        align-items: stretch;
    }

    .tool-bar-wrapper {
        width: auto;
        flex-direction: row;
        flex-wrap: wrap;
        padding: 10px;
        height: auto;
    }

    /* 調整 RWD 下拉選單定位 */
    .tool-bar-wrapper .brush-dropdown-menu,
    .tool-bar-wrapper .shape-dropdown-menu {
        left: 0 !important;
        top: auto !important;
    }

    .drawing-section,
    .result-section {
        flex: 1 1 100%;
        max-width: 100%;
        /* 在手機上佔滿寬度 */
        width: auto;
        height: auto;
        min-height: 0;
        /* 移除桌機模式下的 min-height 限制 */
        margin-bottom: 10px;
    }

    /* 保持畫布和結果區的 1:1 比例 */
    #canvas-container-wrapper,
    #result-wrapper {
        padding-bottom: 100%;
        height: 0;
        margin-bottom: 10px;
    }

    /* ------------------------------------- */
    /* 工具欄底部 Logo 區域樣式 */
    /* ------------------------------------- */
    .logo-section {
        margin-top: auto;
        /* 保持在底部 */
        padding-top: 10px;
        border-top: 1px solid #333;
        /* 與上方按鈕分隔 */
        text-align: center;
    }

    .logo-section .tool-logo {
        width: 45px;
        /* 適合 75px 寬度的工具欄 */
        height: auto;
        border-radius: 6px;
        display: block;
        margin: 0 auto 6px;
        transition: transform 0.2s ease-in-out;
    }

    .logo-section .tool-logo:hover {
        transform: scale(1.1);
    }

}