body {
    font-family: 'Arial', sans-serif;
    min-height: 100vh;
    margin: 0;
    padding: 10px;
    background-color: #f0f8ff;
    box-sizing: border-box;
    overflow-x: hidden;
}

h1 {
    text-align: center;
    color: #0056b3;
    margin: 5px 0 15px 0;
    font-size: clamp(20px, 4vw, 28px);
}

.main-content {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    gap: 20px;
    max-width: 1200px;
    margin: 0 auto;
    min-height: calc(100vh - 80px);
}

.game-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    flex: 1;
    max-width: 800px;
}

.bottom-controls {
    display: flex;
    gap: 10px;
    margin-top: 8px;
}


.game-board {
    display: grid;
    border: 2px solid #333;
    background-color: #a1d490; /* 綠色草地背景 */
    margin: 10px 0;
}

.cell {
    box-sizing: border-box; /* 確保邊框不會增加尺寸 */
    font-size: clamp(16px, calc(var(--cell-size, 40px) * 0.7), 32px); /* 動態字體大小 */
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 20px; /* 最小尺寸確保可讀性 */
    min-height: 20px;
}

.path {
    background-color: #d3d3d3; /* 道路顏色 */
    border: 1px solid #c0c0c0;
}

.wall {
    background-color: #8b4513; /* 牆壁/障礙物顏色 */
    border: 1px solid #6a2e0f;
}

.player {
    font-size: 40px;
}

.goal {
    font-size: 40px;
}


.message-area {
    font-size: clamp(16px, 4vw, 22px);
    font-weight: bold;
    height: 28px;
    margin: 3px 0;
    color: #0056b3;
    text-align: center;
}

.control-hints {
    color: #666;
    text-align: center;
    margin: 1px 0 3px 0;
    font-size: clamp(12px, 2.5vw, 14px);
}

/* 在觸控設備上隱藏鍵盤提示 */
@media (pointer: coarse) {
    .control-hints {
        display: none;
    }
}

/* 桌面大螢幕優化 */
@media (min-width: 1200px) {
    .game-board {
        max-width: 800px; /* 限制最大寬度避免過度延伸 */
        max-height: calc(100vh - 300px);
    }
    
    .cell {
        font-size: clamp(20px, calc(var(--cell-size, 40px) * 0.75), 36px);
    }
    
    h1 {
        font-size: 42px;
        margin-bottom: 30px;
    }
    
    .message-area {
        font-size: 28px;
        margin-bottom: 10px;
    }
    
    .control-hints {
        font-size: 16px;
        margin-bottom: 20px;
    }
}

#reset-button, .back-button {
    margin-top: 5px;
    padding: 8px 16px;
    font-size: 14px;
    border-radius: 6px;
    border: 2px solid #0056b3;
    cursor: pointer;
    background-color: #fff;
    color: #0056b3;
    font-weight: bold;
    text-decoration: none;
    transition: background-color 0.3s, color 0.3s;
}

#reset-button:hover, .back-button:hover {
    background-color: #0056b3;
    color: #fff;
}





/* 改善可讀性 */
.cell {
    border-radius: 2px;
}

.path {
    background-color: #f5f5f5;
    border: 1px solid #ddd;
}

.wall {
    background-color: #228B22;
    border: 1px solid #006400;
}

/* 震動動畫效果 */
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-3px); }
    20%, 40%, 60%, 80% { transform: translateX(3px); }
}

/* 改善觸控體驗 */
.game-board {
    touch-action: none; /* 防止預設的觸控行為 */
    user-select: none; /* 防止文字選取 */
    -webkit-user-select: none;
    -webkit-touch-callout: none; /* 防止長按選單 */
}

/* 觸控回饋 */
.game-board:active {
    opacity: 0.9;
}

/* 虛擬方向控制器樣式 - 右側固定 */
.virtual-controller {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    min-width: 200px;
    position: sticky;
    top: 20px;
}

.controller-toggle {
    display: flex;
    justify-content: center;
}

.toggle-btn {
    padding: 6px 12px;
    font-size: 12px;
    background-color: #f0f0f0;
    border: 1px solid #ccc;
    border-radius: 4px;
    cursor: pointer;
    color: #666;
    transition: all 0.2s;
}

.toggle-btn:hover {
    background-color: #e0e0e0;
    color: #333;
}

.direction-pad {
    display: grid;
    grid-template-rows: 1fr 1fr 1fr;
    gap: 4px;
    align-items: center;
    justify-items: center;
    padding: 8px;
    background-color: rgba(255, 255, 255, 0.95);
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    border: 1px solid #ddd;
}

.horizontal-controls {
    display: flex;
    align-items: center;
    gap: 4px;
}

.center-space {
    width: 40px;
    height: 40px;
    background-color: rgba(0, 86, 179, 0.1);
    border-radius: 50%;
    border: 1px dashed #0056b3;
    display: flex;
    align-items: center;
    justify-content: center;
}

.center-space::after {
    content: '🚗';
    font-size: 16px;
}

.direction-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 2px solid #0056b3;
    background-color: #fff;
    color: #0056b3;
    font-size: 18px;
    font-weight: bold;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
    /* 防止雙擊縮放 */
    -webkit-tap-highlight-color: transparent;
    /* 防止觸控高亮 */
}

.direction-btn:hover {
    background-color: #0056b3;
    color: #fff;
    transform: scale(1.05);
    box-shadow: 0 4px 8px rgba(0, 86, 179, 0.3);
}

.direction-btn:active {
    transform: scale(0.95);
    background-color: #003d82;
    box-shadow: 0 2px 4px rgba(0, 86, 179, 0.5);
}

.arrow {
    font-size: 20px;
    line-height: 1;
}

/* 確保按鈕之間有足夠的間距避免誤觸 */
.direction-btn + .direction-btn {
    margin-left: 4px;
}

/* 隱藏狀態 */
.direction-pad.hidden {
    display: none;
}

/* 響應式調整 - 平板/iPad */
@media (max-width: 1024px) {
    .main-content {
        flex-direction: column;
        align-items: center;
        gap: 5px; /* 減少間距 */
        justify-content: flex-start;
    }
    
    .virtual-controller {
        position: relative;
        top: auto;
        width: 100%;
        max-width: 300px;
        margin-top: 5px; /* 減少上邊距 */
    }
    
    .direction-pad {
        padding: 8px; /* 減少內邊距 */
        gap: 5px;
    }
    
    .direction-btn {
        width: 45px; /* 稍微縮小 */
        height: 45px;
        font-size: 20px;
    }
    
    .center-space {
        width: 45px;
        height: 45px;
    }
    
    .center-space::after {
        font-size: 18px;
    }
    
    .horizontal-controls {
        gap: 5px;
    }
    
    .arrow {
        font-size: 22px;
    }
}

/* iPad Portrait 特別優化 */
@media (min-width: 768px) and (max-width: 1024px) and (orientation: portrait) {
    body {
        padding: 5px;
        height: 100vh;
        overflow-y: auto; /* 允許垂直滾動以顯示所有內容 */
        overflow-x: hidden;
    }
    
    h1 {
        margin: 2px 0 3px 0; /* 最小標題邊距 */
        font-size: clamp(20px, 2.8vw, 22px);
        flex-shrink: 0; /* 防止標題被壓縮 */
    }
    
    .main-content {
        gap: 0; /* 完全無間距 */
        min-height: calc(100vh - 20px);
        justify-content: flex-start; /* 從頂部開始排列 */
        height: auto; /* 改為 auto 讓內容自適應 */
    }
    
    .game-area {
        margin-bottom: 0;
        flex-shrink: 0; /* 防止縮小 */
    }
    
    .message-area {
        margin: 1px 0;
        height: 20px;
        font-size: clamp(13px, 2.3vw, 15px);
        flex-shrink: 0; /* 防止被壓縮 */
    }
    
    .control-hints {
        margin: 0 0 1px 0;
        font-size: clamp(9px, 1.6vw, 10px);
        flex-shrink: 0; /* 防止被壓縮 */
    }
    
    .game-board {
        margin: 3px 0 0 0; /* 極小上邊距，下邊距為0 */
    }
    
    .virtual-controller {
        margin-top: -5px; /* 負邊距，讓控制器往上移 */
        flex-shrink: 0; /* 防止縮小 */
    }
    
    .bottom-controls {
        margin-top: 3px;
    }
    
    .toggle-btn {
        padding: 3px 6px;
        font-size: 10px;
    }
    
    /* 進一步縮小控制器 */
    .direction-pad {
        padding: 6px;
        gap: 4px;
    }
    
    .direction-btn {
        width: 42px;
        height: 42px;
        font-size: 18px;
    }
    
    .center-space {
        width: 42px;
        height: 42px;
    }
    
    .center-space::after {
        font-size: 16px;
    }
    
    .arrow {
        font-size: 20px;
    }
}

/* 響應式調整 - 手機 */
@media (max-width: 767px) {
    body {
        padding: 5px;
    }
    
    .main-content {
        gap: 10px;
    }
    
    .virtual-controller {
        gap: 5px;
        max-width: 250px;
    }
    
    .direction-pad {
        padding: 6px;
        gap: 3px;
    }
    
    .direction-btn {
        width: 35px;
        height: 35px;
        font-size: 16px;
        border-width: 1px;
    }
    
    .center-space {
        width: 35px;
        height: 35px;
        border-width: 1px;
    }
    
    .center-space::after {
        font-size: 14px;
    }
    
    .horizontal-controls {
        gap: 3px;
    }
    
    .arrow {
        font-size: 18px;
    }
}

/* 大螢幕優化 */
@media (min-width: 1025px) {
    .main-content {
        gap: 30px;
    }
    
    .virtual-controller {
        min-width: 220px;
    }
    
    .direction-pad {
        padding: 12px;
        gap: 8px;
    }
    
    .direction-btn {
        width: 55px;
        height: 55px;
        font-size: 24px;
    }
    
    .center-space {
        width: 55px;
        height: 55px;
    }
    
    .center-space::after {
        font-size: 22px;
    }
    
    .horizontal-controls {
        gap: 8px;
    }
    
    .arrow {
        font-size: 26px;
    }
}
