:root {
    --bg-app: #050505;
    --panel-bg: #111111;
    --panel-border: #333333;
    
    --text-primary: #ffffff;
    --text-muted: #888888;
    
    --accent-color: #00E5FF; /* 科技藍 */
    --accent-hover: #00B8CC;
    --solution-color: #FFD60A; /* 鮮黃色 */
    --danger-color: #FF2D55;
    
    --font-ui: 'Inter', sans-serif;
    --font-tech: 'Chakra Petch', sans-serif;
    --font-mono: 'JetBrains Mono', monospace;
    
    --radius: 12px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

body {
    margin: 0;
    padding: 0;
    background-color: var(--bg-app);
    color: var(--text-primary);
    font-family: var(--font-ui);
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

/* --- 全局網格佈局 --- */
.app-grid {
    display: grid;
    /* 左側 320px，右側自適應 */
    grid-template-columns: 320px 1fr;
    grid-template-rows: 100%;
    height: 100%;
    width: 100%;
}

/* --- 區域 1: 左側控制面板 --- */
.panel-controls {
    background-color: var(--panel-bg);
    border-right: 1px solid var(--panel-border);
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 24px;
    overflow-y: auto;
    z-index: 10;
}

.panel-header h2 {
    font-family: var(--font-tech);
    font-size: 24px;
    margin: 0;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-primary);
}

.subtitle {
    font-size: 12px;
    color: var(--text-muted);
    font-family: var(--font-mono);
}

.control-section {
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding-bottom: 16px;
    border-bottom: 1px solid rgba(255,255,255,0.05);
}

.input-group label {
    display: block;
    font-size: 13px;
    color: var(--text-muted);
    margin-bottom: 8px;
    font-weight: 600;
}

/* 下拉選單樣式修復 */
.select-wrapper {
    position: relative;
}

.styled-select {
    width: 100%;
    background-color: #1a1a1a;
    border: 1px solid #444;
    color: #fff;
    /* [修正] iOS 為了防止輸入時自動縮放，字體至少需 16px */
    font-size: 16px;
    padding: 10px 12px;
    border-radius: 8px;
    font-family: var(--font-mono);
    appearance: none;
    -webkit-appearance: none;
    cursor: pointer;
    transition: border-color 0.2s;
}

.styled-select:focus {
    border-color: var(--accent-color);
    outline: none;
}

/* 修復選項文字顏色 */
.styled-select option {
    background-color: #1a1a1a;
    color: #ffffff;
    padding: 10px;
}

.select-wrapper::after {
    content: "▼";
    position: absolute;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    font-size: 10px;
    pointer-events: none;
}

/* 提示文字 */
.hint-text {
    font-size: 12px;
    color: #666;
    line-height: 1.5;
}

/* 按鈕 */
.action-buttons {
    margin-top: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.btn {
    height: 50px;
    border: none;
    border-radius: 8px;
    font-family: var(--font-tech);
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    transition: all 0.2s;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.btn-calc {
    background: var(--accent-color);
    color: #000;
    box-shadow: 0 0 15px rgba(0, 229, 255, 0.2);
}
.btn-calc:hover { background: var(--accent-hover); }

.btn-reset {
    background: #222;
    color: var(--danger-color);
    border: 1px solid #333;
}
.btn-reset:hover { background: #333; }

.btn-scramble {
    background: #7B1FA2;
    color: #fff;
    border: 1px solid #9C27B0;
}
.btn-scramble:hover { background: #9C27B0; }

.footer-note {
    font-size: 10px;
    color: #444;
    text-align: center;
    margin-top: 10px;
    font-family: var(--font-mono);
}

/* --- 右側主區域 --- */
.main-column {
    display: grid;
    /* 修正：改為 auto 1fr，讓上方 Solution 區塊可以根據內容長高，不會被推出去 */
    grid-template-rows: auto 1fr;
    height: 100vh;
    overflow: hidden;
}

/* --- 區域 2: 公式顯示區 --- */
.panel-solution {
    background-color: #0d0d0d;
    border-bottom: 1px solid var(--panel-border);
    display: flex;
    flex-direction: column;
    padding: 20px;
    position: relative;
    background-image: radial-gradient(#222 1px, transparent 1px);
    background-size: 20px 20px;
    justify-content: center;
    align-items: center;
    min-height: 25%; /* 確保最小高度 */
}

.solution-header {
    font-family: var(--font-mono);
    color: var(--text-muted);
    font-size: 12px;
    letter-spacing: 2px;
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
    /* 修正：移除絕對定位，避免遮擋或跑版 */
    align-self: flex-start;
    margin-bottom: 20px;
}

.indicator-dot {
    width: 8px;
    height: 8px;
    background-color: var(--accent-color);
    border-radius: 50%;
    box-shadow: 0 0 8px var(--accent-color);
}

.solution-content {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 12px;
    flex-grow: 1;
}

#solution-text {
    font-family: var(--font-mono);
    /* [修正] 使用 clamp 讓字體在不同裝置自動調整大小，最小 1.2rem，最大 2rem */
    font-size: clamp(1.2rem, 5vw, 2rem);
    font-weight: 700;
    color: var(--solution-color);
    text-align: center;
    line-height: 1.3;
    text-shadow: 0 0 20px rgba(255, 214, 10, 0.2);
    width: 100%;
    max-width: 95%;
    word-wrap: break-word;
    /* [修正] 限制最大高度，避免公式太長把整個畫面撐壞，太長可滑動 */
    max-height: 120px;
    overflow-y: auto;
}

.formula-details {
    width: 100%;
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid rgba(255,255,255,0.1);
    display: flex;
    flex-direction: column;
    gap: 6px;
    max-width: 600px; /* 限制寬度閱讀較佳 */
}

.detail-row {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
    font-size: 11px;
    font-family: var(--font-mono);
}

.detail-row .label {
    color: var(--text-muted);
    font-weight: bold;
    text-transform: uppercase;
}

.detail-row .value {
    color: #ccc;
    word-break: break-all;
    line-height: 1.4;
    background: rgba(0,0,0,0.3);
    padding: 4px 8px;
    border-radius: 4px;
    width: 100%;
}

/* --- 區域 3: 工作區 --- */
.panel-3d {
    background-color: #000;
    position: relative;
    display: flex;
    flex-direction: column;
    /* [修正] 增加底部 padding 到 40px，避免播放器或調色盤太貼近螢幕邊緣 */
    padding: 16px 16px 40px 16px; 
    height: 100%;
    overflow: hidden;
}

.viewport-header {
    font-family: var(--font-mono);
    font-size: 10px;
    color: #444;
    margin-bottom: 10px;
    flex-shrink: 0;
}

.workspace-wrapper {
    display: flex;
    flex-direction: row;
    gap: 16px;
    flex-grow: 1;
    height: 100%;
    min-height: 0;
}

.workspace-split {
    flex: 1;
    background: #080808;
    border: 1px solid #222;
    border-radius: var(--radius);
    position: relative;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.pane-label {
    position: absolute;
    top: 10px;
    left: 10px;
    font-size: 10px;
    color: #555;
    font-family: var(--font-mono);
    z-index: 5;
    pointer-events: none;
}

.left-pane {
    background: radial-gradient(circle at center, #151515 0%, #080808 100%);
    display: flex;
    justify-content: center;
    align-items: center;
}

twisty-player {
    width: 100%;
    height: 100%;
    --twisty-player-control-panel-background: rgba(60, 60, 60, 0.95);
    --twisty-player-foreground-color: #ffffff;
    --twisty-player-button-fill: #ffffff;
    --twisty-player-button-disabled-fill: #AAAAAA;
}

.right-pane {
    display: flex;
    flex-direction: column;
}

#canvas-wrapper {
    flex-grow: 1;
    width: 100%;
    position: relative;
    background: radial-gradient(circle at center, #1a1a1a 0%, #000 70%);
    overflow: hidden;
}

.rotate-controls {
    position: absolute;
    top: 16px;
    right: 16px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    z-index: 10;
}

.rot-row { display: flex; gap: 4px; }
.middle { margin: 0; }

.rot-btn {
    width: 32px;
    height: 32px;
    background: rgba(50, 50, 50, 0.5);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 4px; 
    color: #ccc;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 10px;
    cursor: pointer;
    backdrop-filter: blur(4px);
    transition: all 0.2s;
}

.rot-btn:hover { background: rgba(255,255,255,0.1); color: #fff; }
.rot-btn:active { background: var(--accent-color); color: #000; }
.spacer { width: 32px; visibility: hidden; }

.palette-container {
    padding: 12px;
    background: #111;
    border-top: 1px solid #333;
    display: flex;
    flex-direction: column;
    gap: 8px;
    flex-shrink: 0;
}

.palette-container label {
    font-size: 11px;
    color: #888;
    font-family: var(--font-mono);
}

.palette-grid {
    display: flex;
    gap: 8px;
    justify-content: space-between;
}

.color-dot {
    flex: 1;
    height: 36px;
    border-radius: 6px;
    cursor: pointer;
    border: 2px solid transparent;
    transition: transform 0.1s;
    min-width: 30px;
}

.color-dot:active { transform: scale(0.9); }
.color-dot.selected {
    border-color: #fff;
    box-shadow: 0 0 10px var(--accent-color);
    transform: scale(1.1);
    z-index: 2;
}

/* --- 響應式設計 (手機/平板) 最終修復版 --- */
/* --- [修正] 相機掃描功能與模態視窗樣式 --- */

.btn-scan {
    background: #2E7D32;
    color: #fff;
    border: 1px solid #4CAF50;
}
.btn-scan:hover { background: #4CAF50; }

.camera-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #000;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.camera-header {
    width: 100%;
    padding: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #111;
    border-bottom: 1px solid #333;
}

#face-indicator {
    color: #fff;
    font-size: 1.2rem;
    font-weight: bold;
    font-family: var(--font-tech);
}

.close-camera {
    background: none;
    border: none;
    color: #fff;
    font-size: 2rem;
    cursor: pointer;
    padding: 0 10px;
}

.camera-container {
    position: relative;
    width: 100%;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: #000;
}

.video-wrapper {
    position: relative;
    width: 320px;
    height: 320px;
    overflow: hidden;
    border-radius: 12px;
    box-shadow: 0 0 20px rgba(0,0,0,0.5);
}

#video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transform: scaleX(1);
}

#grid-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
}

.scan-message {
    margin-top: 20px;
    color: var(--accent-color);
    font-family: var(--font-mono);
    font-size: 14px;
    text-align: center;
    min-height: 20px;
}

#button-container {
    position: absolute;
    bottom: 30px;
    left: 0;
    width: 100%;
    display: flex;
    justify-content: center;
    gap: 20px;
    z-index: 20;
}

.confirm-btn, .retry-btn {
    padding: 10px 30px;
    border-radius: 20px;
    border: none;
    font-weight: bold;
    font-size: 16px;
    cursor: pointer;
    box-shadow: 0 4px 6px rgba(0,0,0,0.3);
}

.confirm-btn { background-color: var(--accent-color); color: #000; }
.retry-btn { background-color: #444; color: #fff; }

@media (max-width: 900px) {
    .video-wrapper {
        width: 90vw;
        height: 90vw;
        max-width: 350px;
        max-height: 350px;
    }
}

/* [修正] 美化後的懸浮相機按鈕 (科技毛玻璃風格) */
.float-scan-btn {
    display: flex;
    position: absolute;
    top: 20px;
    left: 20px;
    width: 52px;
    height: 52px;
    /* 改用圓角矩形 (Squircle) 比較有現代科技感 */
    border-radius: 16px; 
    
    /* 毛玻璃背景效果 */
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    
    /* 細緻邊框 */
    border: 1px solid rgba(255, 255, 255, 0.2);
    box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.3);
    
    color: #fff;
    font-size: 24px;
    align-items: center;
    justify-content: center;
    z-index: 100;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
}

.float-scan-btn:hover { 
    background: rgba(255, 255, 255, 0.2);
    border-color: var(--accent-color);
    color: var(--accent-color);
    box-shadow: 0 0 15px rgba(0, 229, 255, 0.3);
    transform: translateY(-2px);
}

.float-scan-btn:active { 
    transform: scale(0.95); 
}

/* 電腦版隱藏手機分頁按鈕 */
.mobile-tabs { display: none; }

/* --- 響應式設計 (手機/平板) 最終細節修飾版 --- */
@media (max-width: 900px) {
    /* 1. 外層容器：鎖定視窗 */
    .app-grid {
        display: flex;
        flex-direction: column;
        height: 100dvh;
        width: 100vw;
        max-width: 100vw;
        overflow: hidden;
    }

    /* 2. 主操作區 */
    .main-column {
        order: 1;
        flex: 1;
        display: flex;
        flex-direction: column;
        height: auto;
        overflow: hidden;
        min-height: 0;
    }

    /* 3. 公式區 */
    .panel-solution {
        flex-shrink: 0;
        /* [修正] 增加上下 padding (25px) 讓公式區高度變高，視覺更平衡，填補版面 */
        padding: 25px 12px;
        border-bottom: 1px solid #333;
        z-index: 20;
        width: 100%;
        box-sizing: border-box;
    }

    .solution-header { display: none; }

    #solution-text {
        font-size: 1.3rem;
        margin: 0;
        text-align: left;
        padding-bottom: 6px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        border-bottom: 1px solid rgba(255,255,255,0.1);
        margin-bottom: 4px;
    }

    .formula-details {
        display: flex !important;
        flex-direction: column;
        width: 100%;
        gap: 6px; /* 行距 */
    }

    /* [修正] 公式標題與內容改成「同一行」顯示 */
    .detail-row {
        display: flex;
        flex-direction: row; /* 強制橫向排列 */
        align-items: center; /* 垂直置中 */
        gap: 8px;
        font-size: 11px;
        width: 100%;
    }

    .detail-row .label { 
        min-width: auto; /* 不需要固定寬度，緊接著就好 */
        flex-shrink: 0; 
        color: #888;
    }

    .detail-row .value {
        font-family: var(--font-mono);
        min-width: 0; 
        flex: 1;
        
        /* 禁止斷行設定，保持整齊 */
        white-space: normal;
        word-break: normal;
        word-wrap: break-word;
        overflow-wrap: break-word;
        
        background: transparent; /* 移除背景讓它看起來像句子 */
        padding: 0;
        color: #ddd;
        line-height: 1.4;
    }

    /* 4. TAB 分頁 */
    .mobile-tabs {
        display: flex;
        flex-shrink: 0;
        width: 100%;
        background: #111;
        border-bottom: 1px solid #333;
    }
    
    .tab-btn {
        flex: 1;
        padding: 10px 0;
        background: none;
        border: none;
        color: #666;
        border-bottom: 2px solid transparent;
        font-size: 14px;
        transition: 0.2s;
    }
    .tab-btn.active {
        color: var(--accent-color);
        border-bottom-color: var(--accent-color);
        background: rgba(0, 229, 255, 0.05);
    }

    /* 5. 3D 工作區 */
    .panel-3d {
        flex: 1;
        padding: 0;
        display: flex;
        flex-direction: column;
        min-height: 0;
        background: #000;
        overflow: hidden;
    }
    
    .viewport-header { display: none; }

    .workspace-wrapper {
        flex: 1;
        display: flex;
        flex-direction: column;
        height: 100%;
        width: 100%;
    }

    .workspace-split {
        display: none !important;
        width: 100%;
        height: 100%;
        border: none;
        border-radius: 0;
    }

    .workspace-split.active {
        display: flex !important;
    }

    /* 填色區 */
    .right-pane { position: relative; }
    
    #canvas-wrapper {
        flex: 1;
        width: 100%;
        background: radial-gradient(circle at center, #1a1a1a 0%, #000 90%);
        overflow: hidden;
    }

    /* 調色盤 */
    .palette-container {
        padding: 8px;
        background: rgba(17,17,17,0.95);
        border-top: 1px solid rgba(255,255,255,0.1);
    }
    .palette-container label { display: none; }
    .color-dot { height: 36px; }

    /* 預覽區 */
    .left-pane { background: #050505; }
    twisty-player { transform: scale(1); }

    /* 6. 底部控制面板 */
    .panel-controls {
        order: 2;
        flex-shrink: 0;
        padding: 12px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom));
        background: var(--panel-bg);
        border-top: 1px solid var(--panel-border);
        border-right: none;
        width: 100%;
        max-height: 45vh;
        overflow-y: auto;
    }

    /* [修正] 移除 .input-group label 的隱藏，讓標題顯示出來 */
    .panel-header, .hint-text, .subtitle, .footer-note { display: none; }

    .control-section {
        flex-direction: row;
        gap: 12px;
        /* [修正] 減少下方 padding，拉近與按鈕的距離，消除空白 */
        padding-bottom: 6px; 
        margin: 0;
        border: none;
    }
    
    .input-group { flex: 1; }

    /* [新增] 手機版下拉選單的標籤樣式 */
    .input-group label {
        display: block;
        font-size: 11px;
        color: var(--accent-color); /* 用亮色標示 */
        margin-bottom: 4px;
        font-family: var(--font-tech);
    }
    
    /* 按鈕三分天下 */
    .action-buttons {
        display: grid;
        grid-template-columns: 1fr 1fr 1fr;
        gap: 8px;
        margin-top: 0;
    }

    .btn {
        height: 44px;
        font-size: 13px;
        margin: 0;
        padding: 0;
    }
    
    .rotate-controls {
        top: 10px;
        right: 10px;
        transform: scale(0.9);
    }
}

/* [修正] 隱藏所有視窗標籤，解決與相機按鈕重疊的問題，並讓畫面更乾淨 */
.pane-label { display: none !important; }