* {
    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: flex-start; /* 【修正】改為靠上對齊，讓捲動行為更自然 */
    width: 100%;
    max-width: 500px;
    min-height: 0;           /* 允許在小螢幕上彈性縮小 */
    overflow-y: auto;        /* 【核心】當 10 列格子超過範圍時，此容器會出現捲動條 */
    padding: 10px 0;
    
    /* 隱藏行動端瀏覽器的原生捲動條，讓畫面更乾淨 */
    -webkit-overflow-scrolling: touch;}
#game-container::-webkit-scrollbar {
    width: 4px;
    }
#game-container::-webkit-scrollbar-thumb {
    background: #3a3a3c;
    border-radius: 2px;
    }

#board {
    display: grid;
    /* 【核心】將原本的 6 變更為動態，或者直接寫死 10 */
    grid-template-rows: repeat(10, 1fr); 
    gap: 5px;
    padding: 10px;
    width: 90%;
    max-width: 350px; /* 稍微縮小看板最大寬度，為 10 列的高窄比例騰出空間 */
    /* 限制看板最高只能佔據除去 header/footer 後剩餘可用高度的 100% */
    max-height: 100%;
}

.board-row {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 5px;
}

.tile {
    /* 調整格子寬高上限，10 列如果每列都 55px 在普通手機一定塞不下 */
    /* 改用 clamp 讓它在矮螢幕上能自動縮得更小（最小 30px） */
    width: 100%;
    aspect-ratio: 1 / 1; /* 確保永遠是正方形 */
    min-width: 30px;
    max-width: 48px;     /* 從 55px 略微調降至 48px，更適合 10 列的視覺 */
    border: 2px solid #3a3a3c;
    font-size: 1.4rem;   /* 字體微調，避免格子縮小時字體溢出 */
    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); }
}