/* =================================================================
   基本設定與排版 (針對所有設備)
   ================================================================= */
body {
    font-family: Arial, sans-serif;
    text-align: center;
    margin: 10px; /* 針對手機優化，縮小邊距 */
    background-color: #000;
    color: #fff; /* 預設文字顏色 */
}
h1 {
    font-size: 1.6em;
    margin: 15px 0;
}
h3 {
    margin-top: 0;
    color: #555;
    font-size: 1.1em;
}

/* =================================================================
   控制面板樣式 (#controls)
   ================================================================= */
#controls {
    margin: 0 auto 20px auto; /* 居中對齊 */
    padding: 15px;
    background-color: rgba(255, 255, 255, 0.9); /* 提高透明度 */
    border-radius: 10px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
    width: 100%; /* 手機上佔滿寬度 */
    max-width: 450px; /* 設定最大寬度，避免在大螢幕上過寬 */
    text-align: left;
    color: #333;
    box-sizing: border-box; /* 確保 padding 不增加寬度 */
}

/* 頂層控制項分組 */
#controls > div:not(.control-group) {
    margin-bottom: 15px;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 5px; /* 增加小間距 */
}

/* 標籤樣式 */
#controls label {
    font-size: 0.9em;
    font-weight: 500;
    min-width: 80px; /* 調整最小寬度以適應手機 */
    text-align: left;
    flex-shrink: 0; /* 防止標籤被壓縮 */
}

/* 值顯示 */
.value-display {
    min-width: 40px;
    text-align: right;
    font-size: 0.9em;
    flex-shrink: 0;
}

/* 滑桿和輸入框 */
#controls input[type="range"] {
    flex-grow: 1; /* 讓滑桿盡量佔滿空間 */
    width: auto;
    margin-right: 5px;
    min-width: 100px;
}
#controls input[type="color"] {
    width: 40px;
    height: 25px;
}
#controls input[type="file"] {
    flex-grow: 1;
}

/* --- 複雜控制項分組 (.control-group) --- */
.control-group {
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    padding: 10px;
    margin-bottom: 15px !important;
    background-color: rgba(255, 255, 255, 0.6);
    display: block !important;
}
.control-group > label {
    font-weight: bold;
    color: #000;
    margin-bottom: 8px;
    display: block;
    text-align: left !important;
}
.control-group > div { /* 內部包裹的div */
    padding-top: 5px;
}
.control-group > div > div { /* 各個獨立的控制項 (R/G/B, 顏色/透明度) */
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    flex-wrap: nowrap; /* 盡量不換行 */
}

/* 線條/底色 Alpha 控制項間隔 */
#lineAlphaControl, #backgroundAlphaControl {
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    padding-bottom: 10px;
    margin-top: -5px;
}

/* --- 下載按鈕樣式 --- */
.process-buttons {
    display: flex;
    flex-direction: column; /* 手機上垂直堆疊 */
    gap: 10px;
    margin-top: 15px;
    padding-top: 15px;
    border-top: 1px solid rgba(0, 0, 0, 0.1);
}
.process-buttons button {
    padding: 10px 15px;
    background-color: #004d99;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 1em;
    transition: background-color 0.3s ease;
    flex-grow: 1;
}
.process-buttons button:hover:not(:disabled) {
    background-color: #007bff;
}
.process-buttons button:disabled {
    background-color: #cccccc;
    cursor: not-allowed;
}

/* =================================================================
   畫布容器樣式 (canvas-container)
   ================================================================= */
.canvas-container {
    display: flex;
    flex-direction: column; /* 垂直排列 */
    align-items: center;
    gap: 30px;
    margin-top: 20px;
}
.canvas-wrapper {
    background-color: #fff;
    border: 1px solid #ccc;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    padding: 10px;
    text-align: center;
    /* 這裡使用 min-width: 0 確保 flex item 不會溢出 */
    min-width: 0; 
    
    /* 棋盤格背景 */
    background-image: linear-gradient(45deg, #eee 25%, transparent 25%), 
                      linear-gradient(-45deg, #eee 25%, transparent 25%),
                      linear-gradient(45deg, transparent 75%, #eee 75%),
                      linear-gradient(-45deg, transparent 75%, #eee 75%);
    background-size: 20px 20px;
    background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
}

/* 所有畫布在小螢幕上都限制最大寬度 */
canvas {
    display: block;
    margin: 0 auto;
    max-width: 100%; 
    height: auto;
}

/* =================================================================
   媒體查詢 (針對桌面/平板等較大螢幕)
   ================================================================= */
@media (min-width: 768px) {
    body {
        margin: 20px;
    }
    h1 {
        font-size: 2em;
    }
    
    /* 桌面版控制面板恢復經典佈局 */
    #controls {
        width: 400px; /* 稍寬一點的固定寬度 */
        display: inline-block;
    }
    
    #controls > div:not(.control-group) {
        display: flex;
        flex-wrap: nowrap;
    }
    
    /* 標籤右對齊，滑桿左側有更多空間 */
    #controls label {
        min-width: 120px;
        text-align: right;
    }
    
    /* 下載按鈕恢復水平排列 */
    .process-buttons {
        flex-direction: row;
        justify-content: space-around;
    }
    
    .canvas-wrapper {
        max-width: unset; /* 移除包裹器的最大寬度限制 */
        width: auto; /* 讓包裹器根據內容自動調整寬度 */
    }

    /* 原始尺寸畫布在桌面/大螢幕上不限制最大寬度 */
    #fullSizeCanvas {
        max-width: unset; /* 移除最大寬度限制 */
        width: auto; /* 讓畫布顯示其原始 CSS 寬度 */
    }

    /* 縮圖畫布仍然受限於 MAX_THUMBNAIL_SIZE */
    #thumbnailCanvas {
        max-width: 800px; /* 這裡可以根據需要設置一個更明確的最大值 */
        width: auto;
    }
}
