* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    background-color: #121213;
    color: #ffffff;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    
    /* 核心修正：使用 dynamic viewport height (dvh) */
    /* 它會自動扣除 Safari/Chrome 底部彈出的網頁工具列高度 */
    height: 100vh; /* 舊瀏覽器備用 */
    height: 100dvh; 
    
    overflow: hidden;
    padding: 10px 0;
}

header {
    border-bottom: 1px solid #3a3a3c;
    width: 100%;
    text-align: center;
    padding: 10px 0;
}

h1 {
    font-size: 2.2rem;
    letter-spacing: 2px;
}

/* 遊戲看板 */
#game-container {
    flex-grow: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    max-width: 500px;
    min-height: 0; /* 允許容器在空間不足時縮小，避免擠壓鍵盤 */
}

#board {
    display: grid;
    grid-template-rows: repeat(6, 1fr);
    gap: 5px;
    padding: 10px;
    /* 加上最大高度限制，防止格子把底部的鍵盤往下推 */
    max-height: 40vh; 
    aspect-ratio: 5 / 6; /* 保持格子永遠是完美的完美比例 */
}

.board-row {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 5px;
}

.tile {
    /* 移除原本固定的 55px 寬高，改用彈性比例 */
    width: 100%;
    height: 100%;
    /* 使用 min() 確保大螢幕好看，小螢幕（如 iPhone SE）自動縮小 */
    min-width: 35px;
    min-height: 35px;
    max-width: 55px;
    max-height: 55px;
    border: 2px solid #3a3a3c;
    font-size: 1.5rem; /* 稍微縮小字體以適應響應式 */
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    text-transform: uppercase;
    user-select: none;
}

/* 字母輸入時的微幅跳動動畫 */
.tile.pop {
    animation: popIn 0.1s ease-in-out;
    border-color: #565758;
}

@keyframes popIn {
    0% { transform: scale(0.8); }
    100% { transform: scale(1); }
}

/* 三種狀態的顏色顏色 */
.correct { background-color: #538d4e; border-color: #538d4e; }
.present { background-color: #b59f3b; border-color: #b59f3b; }
.absent { background-color: #3a3a3c; border-color: #3a3a3c; }

/* 虛擬鍵盤 */
#keyboard-container {
    width: 100%;
    max-width: 500px;
    padding: 10px;
    /* 核心修正：加上 iOS 底部安全區域留白（防止被 iPhone 底部黑條擋住） */
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
    margin-bottom: 0;
}

.keyboard-row {
    display: flex;
    justify-content: center;
    margin-bottom: 8px;
    gap: 6px;
}

.key {
    background-color: #818384;
    color: white;
    border: 0;
    border-radius: 4px;
    /* 將原本固定的 58px 高度改為動態高度，避免在矮螢幕手機上爆出去 */
    height: clamp(40px, 6vh, 58px); 
    flex: 1;
    font-weight: bold;
    font-size: 1rem;
    cursor: pointer;
    user-select: none;
    text-transform: uppercase;
    display: flex;
    align-items: center;
    justify-content: center;
}

.large-key {
    flex: 1.5;
    font-size: 0.9rem;
}

/* 訊息彈窗 */
#message-container {
    position: absolute;
    top: 10%;
    background-color: #ffffff;
    color: #000000;
    padding: 12px 20px;
    border-radius: 5px;
    font-weight: bold;
    z-index: 10;
    box-shadow: 0 4px 23px rgba(0,0,0,0.5);
    transition: opacity 0.2s;
}

.hidden { display: none !important; }

/* 統計資訊模組彈窗 */
.modal {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    /* 稍微降低黑底透明度，讓後方的綠黃灰棋盤格式隱約看得到 */
    background-color: rgba(0, 0, 0, 0.5); 
    display: flex; justify-content: center; align-items: center;
    z-index: 100;
    /* 確保好操作 */
    cursor: pointer; 
}

.modal-content {
    background-color: #121213;
    border: 1px solid #3a3a3c;
    border-radius: 12px;
    padding: 30px;
    width: 90%;
    max-width: 380px;
    text-align: center;
    box-shadow: 0 4px 23px rgba(0,0,0,0.8);
    /* 避免點擊彈窗內部時，觸發關閉 */
    cursor: default; 
}

.modal-content h2 { margin-bottom: 20px; }

.stats-boxes {
    display: flex;
    justify-content: space-around;
    margin-bottom: 25px;
}

.stats-box span { font-size: 1.8rem; font-weight: bold; }
.stats-box p { font-size: 0.8rem; color: #818384; margin-top: 5px; }

#close-modal-btn {
    background-color: #538d4e;
    color: white;
    border: none;
    padding: 10px 20px;
    font-size: 1rem;
    font-weight: bold;
    border-radius: 4px;
    cursor: pointer;
}
/* 讓虛擬鍵盤在變色時有平滑的過渡效果 */
.key {
    background-color: #818384;
    color: white;
    border: 0;
    border-radius: 4px;
    height: clamp(40px, 6vh, 58px); 
    flex: 1;
    font-weight: bold;
    font-size: 1rem;
    cursor: pointer;
    user-select: none;
    text-transform: uppercase;
    display: flex;
    align-items: center;
    justify-content: center;

    /* 【核心修正】告訴 iOS：這個按鈕只允許正常點擊與滑動，禁用「雙擊放大」 */
    touch-action: manipulation;
}

/* 虛擬鍵盤猜對/猜錯的顏色樣式 (必須與 tile 的顏色一致或相近) */
.key.correct {
    background-color: #538d4e !important; /* 綠色：位置與字母皆正確 */
    color: white;
}

.key.present {
    background-color: #b59f3b !important; /* 黃色：有這個字母但位置錯了 */
    color: white;
}

.key.absent {
    background-color: #3a3a3c !important; /* 灰色：答案裡完全沒有這個字母 */
    color: #787c7e; /* 讓字體顏色變暗，代表此鍵已失效 */
}
/* 答錯或非合法單字時的震動動畫 */
.board-row.shake {
    animation: shake 0.5s linear;
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
    20%, 40%, 60%, 80% { transform: translateX(5px); }
}