/* style.css */

/* =========================================
   1. Reset & Variables
   ========================================= */
* { box-sizing: border-box; }

:root {
    /* --- Light Mode (Default) --- */
    --bg-body: #f0f2f5;
    --bg-panel: #ffffff;
    --text-main: #333333;
    --text-sub: #666666;
    --border-color: #cccccc;
    --border-light: #eeeeee;
    --wall-color: #000000;
    --grid-line: #f0f0f0;
    --btn-bg: #ffffff;
    --btn-hover: #e9ecef;
    --btn-text: #444444;
    --primary: #28a745;
    --primary-hover: #218838;
    --danger: #dc3545;
    --active: #007bff;
    --start-bg: #4ade80;
    --goal-bg: #f87171;
    --path-bg: rgba(255, 235, 59, 0.7);
    --sidebar-width: 340px;
    --cell-size: 30px;
    --cols: 16;
    --rows: 16;
}

/* --- Dark Mode Variables (Keep Original) --- */
@media (prefers-color-scheme: dark) {
    :root {
        --bg-body: #121212;
        --bg-panel: #1e1e1e;
        --text-main: #e0e0e0;
        --text-sub: #a0a0a0;
        --border-color: #444444;
        --border-light: #333333;
        --wall-color: #ff4d4d;
        --grid-line: #2c2c2c;
        --btn-bg: #2d2d2d;
        --btn-hover: #3d3d3d;
        --btn-text: #e0e0e0;
        --primary: #1e7e34;
        --primary-hover: #155724;
        --danger: #bd2130;
        --active: #0056b3;
        --path-bg: rgba(255, 215, 0, 0.5);
    }
    :root:not([data-theme="light"]) .cell.is-start.wall-n,
    :root:not([data-theme="light"]) .cell.is-goal.wall-n { border-top-color: #FFD700 !important; }
    :root:not([data-theme="light"]) .cell.is-start.wall-e,
    :root:not([data-theme="light"]) .cell.is-goal.wall-e { border-right-color: #FFD700 !important; }
    :root:not([data-theme="light"]) .cell.is-start.wall-s,
    :root:not([data-theme="light"]) .cell.is-goal.wall-s { border-bottom-color: #FFD700 !important; }
    :root:not([data-theme="light"]) .cell.is-start.wall-w,
    :root:not([data-theme="light"]) .cell.is-goal.wall-w { border-left-color: #FFD700 !important; }
}

[data-theme="dark"] {
    --bg-body: #121212;
    --bg-panel: #1e1e1e;
    --text-main: #e0e0e0;
    --text-sub: #a0a0a0;
    --border-color: #444444;
    --border-light: #333333;
    --wall-color: #ff4d4d;
    --grid-line: #2c2c2c;
    --btn-bg: #2d2d2d;
    --btn-hover: #3d3d3d;
    --btn-text: #e0e0e0;
    --primary: #1e7e34;
    --danger: #bd2130;
    --active: #0056b3;
    --path-bg: rgba(255, 215, 0, 0.5);
}

[data-theme="light"] {
    --bg-body: #f0f2f5;
    --bg-panel: #ffffff;
    --text-main: #333333;
    --text-sub: #666666;
    --border-color: #cccccc;
    --border-light: #eeeeee;
    --wall-color: #000000;
    --grid-line: #f0f0f0;
    --btn-bg: #ffffff;
    --btn-hover: #e9ecef;
    --btn-text: #444444;
    --primary: #28a745;
    --danger: #dc3545;
    --active: #007bff;
    --path-bg: rgba(255, 235, 59, 0.7);
}

/* =========================================
   2. Base Layout (CSS Grid) - REFACTORED
   ========================================= */
body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: var(--bg-body);
    color: var(--text-main);
    margin: 0;
    width: 100%;
    height: 100dvh;   /* 使用 dvh 動態高度，適配 iOS 工具列伸縮 */
    position: fixed;  /* ★ 關鍵：強制固定位置，防止切換 App 時版面跑掉 */
    inset: 0;         /* 上下左右貼齊 */
    overflow: hidden; /* 禁止 body 捲動 */
    
    display: grid;
    grid-template-rows: max-content 1fr; /* 配合上一題改的設定 */
    grid-template-columns: var(--sidebar-width) 1fr;
    grid-template-areas: 
        "header header"
        "sidebar main";
    transition: all 0.3s ease;
}

/* 狀態：側邊欄關閉 (Desktop) */
body.sidebar-closed {
    /* 讓側邊欄欄位歸零，內容區 (1fr) 自動填滿 */
    grid-template-columns: 0 1fr;
}

/* --- Top Bar (Header) --- */
.top-bar {
    grid-area: header;
    background-color: var(--bg-panel);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    
    /* ★ 使用 relative 讓內部的絕對定位元素參考這裡 */
    position: relative; 
    
    /* 🟢 修改 padding-top，加個保險 */
    /* 意思是：取「安全區域」跟「10px」之中比較大的那個 */
    /* 這樣就算 Safari 誤判安全區域為 0，至少還有 10px 的縫隙，不會完全黏在動態島上 */
    padding-top: max(10px, env(safe-area-inset-top)); 
    
    /* 其他 padding 保持不變 */
    padding-right: max(15px, env(safe-area-inset-right));
    padding-left: max(60px, env(safe-area-inset-left));
    padding-bottom: 0;

    height: auto;
    min-height: 50px; 
    
    gap: 10px;
    z-index: 50;
    overflow: hidden;
}

/* 設定標題文字 */
.top-bar h1 {
    margin: 0;
    font-size: 1.2rem;
    margin-right: auto; /* 把右邊的東西推到底 */
    
    /* ★ 修改重點 4: 防止標題把右邊擠出去 */
    white-space: nowrap;       /* 文字不換行 */
    overflow: hidden;          /* 超出長度隱藏 */
    text-overflow: ellipsis;   /* 超出變成 ... */
    min-width: 0;              /* 關鍵：允許 Flex 子元素縮小到比內容還小 */
    flex-shrink: 1;            /* 允許標題被壓縮 */
}

/* 設定右上的下拉選單 */
.theme-select, .lang-select {
    /* ★ 修改重點 5: 禁止下拉選單被壓縮，確保它們永遠在畫面上 */
    flex-shrink: 0; 
    max-width: 120px; /* 避免選單本身太寬 */
}


/* =========================================
   Theme Toggle Switch (精準覆蓋版)
   ========================================= */
.theme-switch-wrapper {
    display: flex;
    align-items: center;
    margin-right: 15px;
}

.theme-switch {
    position: relative;
    display: inline-block;
    width: 60px;
    height: 30px;
    flex-shrink: 0;
}

.theme-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

/* 滑軌背景 */
.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #87CEEB; /* 淺色：天藍 */
    transition: .4s;
    border-radius: 34px;
    border: 1px solid var(--border-color);
    overflow: hidden; /* 關鍵：確保圓球跑出去時不會破圖 */
}

/* 圓球按鈕 (加大版) */
.slider:before {
    position: absolute;
    content: "";
    
    /* ★ 修改 1: 加大圓球，確保能完全蓋住圖示 */
    height: 26px; 
    width: 26px;
    
    /* 計算位置: (30px高度 - 26px圓球) / 2 = 2px */
    left: 2px;
    bottom: 1px; 
    
    background-color: white;
    transition: .4s;
    border-radius: 50%;
    z-index: 2; /* 確保圓球蓋在圖示上面 */
    box-shadow: 0 2px 4px rgba(0,0,0,0.25);
}

/* 深色模式狀態 */
input:checked + .slider {
    background-color: #2c3e50; /* 深色：夜空黑 */
    border-color: #555;
}

/* 圓球移動距離 */
input:checked + .slider:before {
    /* 總寬 60 - 圓球 26 - 左邊距 2 - 右邊距 2 = 30px */
    transform: translateX(30px);
}

/* --- 圖示樣式 (改為絕對定位) --- */
.icon-sun, .icon-moon {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    
    /* ★ 修改 2: 字體稍微縮小，確保不會超出圓球 */
    font-size: 14px; 
    line-height: 1;
    z-index: 1; /* 在圓球下面 */
    user-select: none;
    pointer-events: none;
}

/* ★ 修改 3: 精準定位圖示位置 */
.icon-sun {
    /* 太陽放在左邊 (淺色模式時被圓球蓋住? 不，通常設計是圓球在太陽位置代表選中太陽) */
    /* 依照你的邏輯：圓球在左 = 淺色 = 圓球要是太陽? 還是圓球旁邊顯示太陽? */
    /* 這裡設定：圖示永遠固定在左右兩邊，圓球滑過去蓋住它 */
    
    left: 7px; /* (圓球26 - 字體14)/2 + 邊距2 ≈ 7~8px */
    color: #fff;
    text-shadow: 0 0 2px #FFD700;
}

.icon-moon {
    right: 7px; /* 同上，確保居中 */
}

/* =========================================
   Toggle Switch UI (通用開關樣式)
   ========================================= */
.toggle-control {
    display: flex;
    align-items: center;
    cursor: pointer;
    margin-bottom: 8px; /* 選項之間的間距 */
    user-select: none;
    position: relative;
}

/* 隱藏原始的 checkbox */
.toggle-control input {
    opacity: 0;
    width: 0;
    height: 0;
    position: absolute;
}

/* 滑軌背景 */
.toggle-slider {
    position: relative;
    width: 44px;
    height: 24px;
    background-color: #ccc; /* 未選取時的灰色 */
    border-radius: 24px;
    transition: 0.3s;
    margin-right: 12px; /* 開關與文字的距離 */
    flex-shrink: 0;
}

/* 深色模式下的未選取背景 */
[data-theme="dark"] .toggle-slider {
    background-color: #555;
}

/* 圓形按鈕 (Knob) */
.toggle-slider::before {
    content: "";
    position: absolute;
    height: 18px;
    width: 18px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    border-radius: 50%;
    transition: 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

/* 選取狀態 (Checked) - 背景變色 */
.toggle-control input:checked + .toggle-slider {
    background-color: var(--primary); /* 使用你的主題綠色 */
}

/* 選取狀態 (Checked) - 按鈕移動 */
.toggle-control input:checked + .toggle-slider::before {
    transform: translateX(20px);
}

/* 文字標籤樣式 */
.toggle-text {
    font-size: 14px;
    color: var(--text-main);
}

/* --- Global Toggle Button --- */
.sidebar-toggle {
    /* ★ 修改重點 3: 改為絕對定位，固定在左側 */
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%); /* 讓它垂直置中 */
    
    width: 40px;
    height: 40px;
    font-size: 26px;
    border-radius: 50%;
    border: none;
    background: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--text-main);
    transition: background-color 0.2s;
    z-index: 55;
}

.sidebar-toggle:hover {
    background-color: var(--btn-hover);
}

/* --- Main View --- */
.main-view {
    grid-area: main;
    background-color: var(--bg-body);
    position: relative;
    overflow: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

/* =========================================
   3. Sidebar Styles - REFACTORED
   ========================================= */
#sidebar {
    grid-area: sidebar;
    /* 寬度由 Grid 控制，這裡只要設定 100% 填滿 Grid Cell 即可 */
    width: 100%; 
    height: 100%;
    background-color: var(--bg-panel);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    /* 移除 transition width，改由 body grid transition 控制 */
    overflow: hidden; /* 確保縮小時內容不溢出 */
    position: relative;
    z-index: 40; /* 比 Header 低一點 */
}

.sidebar-scroll-area {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding-top: 10px;
    
    /* 1. 預設給一點底部空間 */
    padding-bottom: 20px; 
    
    opacity: 1;
    transition: opacity 0.2s;
    
    /* 優化手機捲動手感 */
    -webkit-overflow-scrolling: touch; 
}

/* 當 Sidebar 被壓縮時隱藏內容 */
body.sidebar-closed #sidebar .sidebar-scroll-area {
    opacity: 0;
    pointer-events: none;
    visibility: hidden;
}

/* 內部元件樣式 (保持原樣) */
.controls-content { padding: 15px; display: flex; flex-direction: column; gap: 10px; }
.input-group { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; background: rgba(0, 0, 0, 0.03); padding: 10px; border-radius: 8px; border: 1px solid var(--border-light); width: 100%; margin-bottom: 15px; }
.file-panel { display: flex; flex-direction: column; gap: 8px; border: 1px solid var(--border-light); padding: 10px; border-radius: 8px; background-color: rgba(0,0,0,0.02); margin-bottom: 10px; animation: fadeIn 0.3s; }
.control-group { display: flex; flex-direction: column; gap: 12px; background-color: rgba(0, 0, 0, 0.02); border: 1px solid var(--border-light); border-radius: 8px; padding: 15px; margin-top: 5px; }

/* =========================================
   4. Maze Grid & Visualization (Keep Original)
   ========================================= */
.layout-wrapper {
    flex: 1;
    width: 100%;
    height: 100%;
    background-color: var(--bg-body);
    position: relative;
    overflow: hidden; /* ★ 重要：把跑出去的迷宮藏起來 */
    
    /* 讓內容預設居中 */
    display: flex;
    justify-content: center;
    align-items: center;
    
    /* 禁止瀏覽器預設觸控行為 */
    touch-action: none;
    cursor: grab; /* 預設手型 */
}

.layout-wrapper.grabbing {
    cursor: grabbing; /* 抓取時的手型 */
}

/* ✅ 新增：內層容器 (負責 Grid 排版與移動) */
.maze-container {
    display: grid;
    /* 原本在 layout-wrapper 的 grid 設定搬來這裡 */
    grid-template-columns: max-content max-content;
    grid-template-rows: max-content max-content;
    justify-content: center;
    align-content: center;
    
    /* 移動的核心：Transform */
    transform-origin: center center;
    /* 如果想要絲滑一點可以加 transition，但在拖曳時建議不要加太強的延遲 */
    /* transition: transform 0.1s linear; */ 
}
.layout-wrapper.grab-mode { cursor: grab; }
.layout-wrapper.grabbing { cursor: grabbing; }
.y-axis { grid-row: 1; grid-column: 1; display: grid; grid-template-rows: repeat(var(--rows), var(--cell-size)); align-items: center; justify-items: end; padding-right: 8px; font-size: 12px; }
.x-axis-wrapper { grid-row: 2; grid-column: 2; display: grid; grid-template-columns: repeat(var(--cols), var(--cell-size)); justify-items: center; font-size: 12px; margin-top: 5px; }
.y-axis div, .x-axis-wrapper div { color: var(--text-sub); font-weight: bold; white-space: nowrap; }
/* 1. 容器設定：調整高度與外距 */
.x-axis-wrapper.rotate-mode {
    /* ★ 關鍵 1：用負的 margin 把整排數字往上拉，靠近迷宮 */
    margin-top: -7px; 
    
    /* 給予足夠高度，確保突出的長數字不會被切掉 */
    height: 40px; 
    
    /* 確保垂直對齊 */
    align-items: start; 
}

/* 2. 數字設定：旋轉並微調位置 */
.x-axis-wrapper.rotate-mode div {
    transform-origin: center center;
    
    /* ★ 關鍵修正：改成負數，或者直接歸零 */
    /* translateX(-15px) 代表「往下」移動 (因為旋轉後 X 軸朝上) */
    transform: rotate(-90deg) translateX(-15px);

    width: var(--cell-size);
    text-align: center;
    font-size: 10px;
    line-height: var(--cell-size);
    position: relative;
}
#maze-grid { grid-row: 1; grid-column: 2; display: grid; grid-template-columns: repeat(var(--cols), var(--cell-size)); grid-template-rows: repeat(var(--rows), var(--cell-size)); background-color: var(--bg-panel); user-select: none; -webkit-user-select: none; position: relative; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
#maze-grid.editing-wall .cell { cursor: crosshair; }
.cell { width: var(--cell-size); height: var(--cell-size); border: 1px solid var(--grid-line); position: relative; cursor: pointer; transition: var(--cell-transition, background-color 0.15s ease-out); }
.cell:hover { background-image: linear-gradient(rgba(0,0,0,0.1), rgba(0,0,0,0.1)); }
.cell.searching, .cell.path-node { transition: background-color 0.2s ease-in; }
.wall-n { border-top: 2px solid var(--wall-color); }
.wall-e { border-right: 2px solid var(--wall-color); }
.wall-s { border-bottom: 2px solid var(--wall-color); }
.wall-w { border-left: 2px solid var(--wall-color); }
.is-start { background-color: var(--start-bg) !important; }
.is-goal { background-color: var(--goal-bg) !important; }
.is-start::after { content: 'S'; color: white; font-weight: bold; font-size: 12px; position:absolute; inset:0; display:flex; justify-content:center; align-items:center;}
.is-goal::after { content: 'G'; color: white; font-weight: bold; font-size: 12px; position:absolute; inset:0; display:flex; justify-content:center; align-items:center;}
/* ★★★ 修改如下：增加平滑轉場 ★★★ */
.cell.searching { 
    background-color: rgba(33, 150, 243, 0.5) !important; /* 淺藍色 */
    
    /* 增加 transform 讓格子有微微彈出的感覺，看起來更活潑 */
    transform: scale(0.95); 
    border-radius: 4px;
    
    /* 讓顏色變化有 0.3秒 的漸變，消除閃爍感 */
    transition: background-color 0.3s ease-out, transform 0.2s ease-out; 
}

/* 深色模式也要改 */
[data-theme="dark"] .cell.searching { 
    background-color: #01579b !important; 
    transition: background-color 0.3s ease-out, transform 0.2s ease-out; 
}
.cell.current-head { background-color: #ff9800 !important; border: 2px solid #fff; z-index: 30; }
/* 修改這一段 */
.cell.backtracking { 
    /* 1. 保持原樣大小 */
    transform: none !important; 
    border-radius: 0 !important;
    
    /* ★ 關鍵修正：刪除原本這裡的 border 設定 */
    /* 不要寫 border: ... !important，這樣才能保留牆壁樣式 (.wall-n 等) */
    
    /* 2. 背景色：淡紅色 */
    background-color: #ffcdd2 !important; 
    
    /* 3. 轉場效果 */
    transition: background-color 0.2s;
    
    /* 確保裡面可以定位叉叉 */
    position: relative;
    z-index: 0; /* 確保不會蓋住邊框 */
}

/* 4. 利用偽元素加一個「叉叉」 */
.cell.backtracking::after {
    content: "✕"; 
    position: absolute;
    inset: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    
    color: #c62828;  /* 深紅色 */
    font-size: 1.2em; 
    font-weight: bold;
    opacity: 0.6;   
    
    animation: fadeIn 0.2s ease-out;
}

/* --- 深色模式微調 --- */
[data-theme="dark"] .cell.backtracking { 
    background-color: #4a1818 !important; /* 深暗紅底 */
}
/* 注意：這裡也不要加 border 設定，讓它自動顯示亮紅色的牆壁 */

[data-theme="dark"] .cell.backtracking::after {
    color: #ff5252; 
    opacity: 0.8;
}

.cell-weight { position: absolute; inset: 0; display: flex; justify-content: center; align-items: center; font-size: 10px; color: var(--text-sub); z-index: 20 !important; pointer-events: none; font-weight: bold; text-shadow: 1px 1px 0 rgba(255,255,255,0.8); display: none; }
[data-theme="dark"] .cell-weight { color: #fff; text-shadow: 1px 1px 0 rgba(0,0,0,0.8); }
#maze-grid.show-weights .cell-weight { display: flex; }
.maze-svg-layer { position: absolute; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; z-index: 10; }
.path-line { fill: none; stroke-width: 2px; vector-effect: non-scaling-stroke; stroke-linecap: round; stroke-linejoin: round; opacity: 0.9; filter: drop-shadow(0px 1px 1px rgba(0,0,0,0.3)); z-index: 10; }
.path-dashed { fill: none; stroke-width: 1.5px; vector-effect: non-scaling-stroke; stroke-dasharray: 4, 4; stroke-linecap: round; stroke-linejoin: round; opacity: 0.7; z-index: 9; }

/* --- Info/Status Container --- */
.info-container {
    position: fixed; 
    bottom: 20px; 
    right: 20px;
    background-color: var(--bg-panel); 
    padding: 8px 15px;
    border-radius: 8px; 
    border: 1px solid var(--border-color);
    box-shadow: 0 4px 15px rgba(0,0,0,0.15);
    z-index: 60;
    
    /* 預設完全不透明 */
    opacity: 1; 
    transition: opacity 0.3s ease;
    pointer-events: auto; /* 預設可以點擊選取文字 */
}

/* ❌ 刪除原本的 .info-container:hover {...} */

/* ✅ 新增：由 JS 控制的重疊狀態 */
.info-container.is-overlapping {
    /* 當重疊時，變半透明 */
    opacity: 0.2; 
    
    /* 讓滑鼠點擊穿透（這樣才能點到底下的迷宮格子） */
    pointer-events: none; 
}

.status-text { font-size: 0.9em; color: var(--text-main); }

/* =========================================
   5. UI Components (Keep Original)
   ========================================= */
input[type="number"], select { background: var(--bg-panel); color: var(--text-main); border: 1px solid var(--border-color); padding: 5px; border-radius: 4px; }
.theme-select, .lang-select { padding: 5px; border-radius: 4px; border: 1px solid var(--border-color); background: var(--bg-panel); color: var(--text-main); }
.slider-container { width: 100%; display: flex; align-items: center; }
.slider-container input { width: 100%; }
button { padding: 8px 12px; cursor: pointer; border: 1px solid var(--border-color); border-radius: 6px; background-color: var(--btn-bg); color: var(--btn-text); transition: all 0.2s; font-size: 14px; white-space: nowrap; flex: 1 1 auto; display: inline-flex; justify-content: center; align-items: center; gap: 5px; }
button:hover { background-color: var(--btn-hover); }
button.active { background-color: var(--active); color: white; border-color: var(--active); }
button.primary { background-color: var(--primary); color: white; border: none; }
button.primary:hover { background-color: var(--primary-hover); }
button.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
button.danger:hover { background-color: var(--danger); color: white; }
.btn-row { display: flex; flex-direction: row; justify-content: space-between; gap: 8px; width: 100%; }
.btn-row button { flex: 1; }
.file-btn { width: 100% !important; text-align: center; margin-bottom: 8px; padding: 10px 15px; display: flex; justify-content: center; align-items: center; box-shadow: 0 1px 2px rgba(0,0,0,0.05); }
@keyframes fadeIn { from { opacity: 0; transform: translateY(-5px); } to { opacity: 1; transform: translateY(0); } }


/* =========================================
   Language Selector (加大美化版)
   ========================================= */
.lang-select {
    /* 1. 尺寸放大 */
    height: 40px;           /* 增加高度 (原本約 20-30px) */
    font-size: 16px;        /* 字體變大 (原本預設通常是 13px) */
    font-weight: 500;       /* 字體稍微加粗 */
    padding: 0 12px;        /* 增加左右留白 */
    
    /* 2. 寬度調整 */
    min-width: 140px;       /* 設定最小寬度，確保文字不折行 */
    max-width: none;        /* ★ 重要：移除原本 120px 的限制 */
    flex-shrink: 0;         /* 防止被擠壓 */
    
    /* 3. 外觀美化 */
    border: 1px solid var(--border-color);
    border-radius: 8px;     /* 圓角更圓潤 */
    background-color: var(--bg-panel);
    color: var(--text-main);
    cursor: pointer;
    outline: none;          /* 移除點擊時的藍框 */
    
    /* 4. 添加陰影與轉場效果 */
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
    transition: all 0.2s ease;
    
    /* 讓文字垂直置中 */
    display: flex;
    align-items: center;
}

/* 滑鼠懸停效果 */
.lang-select:hover {
    border-color: var(--primary);       /* 邊框變綠色 */
    background-color: var(--bg-body);   /* 背景稍微變深 */
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

/* 點擊/聚焦效果 */
.lang-select:focus {
    border-color: var(--primary);
    /* 加一圈淡綠色的光暈，提升操作感 */
    box-shadow: 0 0 0 3px rgba(40, 167, 69, 0.2); 
}

/* =========================================
   Sidebar Selects (側邊欄下拉選單美化)
   ========================================= */
#algo-select, 
#map-select, 
#size-preset {
    /* 1. 尺寸放大 */
    height: 38px;           /* 高度增加，好點擊 */
    font-size: 14px;        /* 字體適中 */
    padding: 0 10px;        /* 內距 */
    
    /* 2. 外觀美化 */
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background-color: var(--bg-panel);
    color: var(--text-main);
    cursor: pointer;
    outline: none;
    
    /* 3. 排版 */
    flex: 1;                /* 自動填滿剩餘空間 */
    min-width: 0;           /* 防止被撐開 */
    
    /* 4. 陰影與轉場 */
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
    transition: all 0.2s ease;
}

/* 滑鼠懸停 */
#algo-select:hover, 
#map-select:hover, 
#size-preset:hover {
    border-color: var(--primary);
    background-color: var(--bg-body);
}

/* 點擊聚焦 */
#algo-select:focus, 
#map-select:focus, 
#size-preset:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(40, 167, 69, 0.15); /* 淡綠色光暈 */
}

/* =========================================
   6. Responsive Design (Mobile) - REFACTORED
   ========================================= */
@media (max-width: 768px) {
    body {
        /* 手機版：永遠保持單欄，不管選單有沒有開 */
        grid-template-columns: 1fr;
        grid-template-rows: max-content 1fr;
        grid-template-areas: 
            "header"
            "main";
    }

    /* ★ 關鍵修復：覆蓋掉桌機版的設定 */
    /* 桌機版說關閉時要變成 0 1fr，但手機版我們強迫它保持 1fr */
    body.sidebar-closed {
        grid-template-columns: 1fr;
    }

    .top-bar {
        padding-left: 60px; /* 留位置給左上角的浮動按鈕 */
        justify-content: flex-start; /* 確保標題靠左 */
    }

    /* Mobile: Sidebar 變成浮動抽屜 */
    #sidebar {
        position: fixed;
        top: 50px;
        bottom: 0;
        left: 0;
        width: var(--sidebar-width);
        
        /* 3. ★ 確保容器本身不捲動，只讓內部的 scroll-area 捲動 */
        display: flex;
        flex-direction: column;
        overflow: hidden; 
        
        /* 其他原本的樣式... */
        background-color: var(--bg-panel);
        border-right: 1px solid var(--border-color);
        box-shadow: 2px 0 10px rgba(0,0,0,0.2);
        z-index: 100;
        transform: translateX(0);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

    /* Mobile: 當 Sidebar 關閉時，移出畫面 */
    body.sidebar-closed #sidebar {
        transform: translateX(-100%);
        width: var(--sidebar-width); /* 保持寬度，只是移出去了 */
    }

    .sidebar-scroll-area {
        /* 2. ★ 關鍵修正：手機版底部加厚 */
        /* 這樣可以確保最後一個按鈕能被「捲動」到手指點得到的地方，不會被瀏覽器底下的 bar 擋住 */
        padding-bottom: 100px; 
    }

    /* Mobile: 內容淡出 */
    body.sidebar-closed .sidebar-scroll-area {
        opacity: 0; 
    }

    /* 手機版按鈕：浮在左上角 (Absolute/Fixed) */
    .sidebar-toggle {
        position: fixed;
        top: 5px;
        left: 10px;
        z-index: 101;
        margin: 0;
        background-color: transparent;
        
        /* ★ 關鍵修正：取消電腦版的垂直置中偏移 */
        transform: none; 
    }
    /* ★ 新增：當側邊欄打開時，在主畫面上蓋一層半透明黑布 
       解釋：body:not(.sidebar-closed) 代表「側邊欄開啟時」
    */
    body:not(.sidebar-closed) .main-view::after {
        content: "";
        position: fixed; /* 強制覆蓋全螢幕 */
        top: 0; left: 0; right: 0; bottom: 0;
        
        background-color: rgba(0, 0, 0, 0.4); /* 40% 透明度的黑色 */
        backdrop-filter: blur(2px); /* (選用) 讓背景稍微模糊，質感更好 */
        
        z-index: 99; /* 層級設定：
                        Sidebar 是 100
                        Toggle按鈕 是 101
                        所以這個遮罩 99 會剛好蓋住迷宮，但在選單下面 */
        
        /* 讓這層遮罩可以被點擊 */
        pointer-events: auto; 
        
        /* 增加淡入動畫 */
        animation: fadeInMask 0.3s ease;
    }
}

/* 定義淡入動畫 (如果你的 CSS 裡還沒定義過) */
@keyframes fadeInMask {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* =========================================
   GitHub Button (Desktop Only)
   ========================================= */

/* 1. 預設狀態 (手機)：隱藏 */
.desktop-only {
    display: none;
}

/* 2. 電腦版樣式 (螢幕寬度大於 768px 時顯示) */
@media (min-width: 768px) {
    .desktop-only {
        display: flex; /* 改為顯示 */
        align-items: center;
        text-decoration: none; /* 去掉底線 */
        color: var(--text-color, #333); /* 跟隨主題文字顏色 */
        margin-left: 10px;
        padding: 5px 10px;
        border: 1px solid var(--border-color, #ccc);
        border-radius: 5px;
        transition: background-color 0.2s, color 0.2s;
        font-size: 0.9rem;
        background-color: var(--bg-card, #fff);
    }

    /* 滑鼠懸停效果 */
    .desktop-only:hover {
        background-color: var(--primary, #007bff);
        color: white;
        border-color: var(--primary, #007bff);
    }
    
    /* 深色模式適配 (如果你有定義 .dark-theme) */
    body.dark-theme .desktop-only {
        color: #ddd;
        border-color: #555;
        background-color: #333;
    }
    
    body.dark-theme .desktop-only:hover {
        background-color: var(--primary, #007bff);
        color: white;
    }
}