/* 基本的界面設置 */
html,
body {
    min-height: 100%;
    background-color: #000000;
    margin: 0;
    padding: 0;
}

#game-container {
    display: flex;
    flex-direction: column;
    gap: 18px;
    justify-content: center;
    align-items: center;
    min-height: 90vh;
    padding: 24px 16px;
    --board-size: min(430px, calc(100vw - 32px));
    --gap: clamp(6px, 2vw, 10px);
    --cell-size: calc((var(--board-size) - 3 * var(--gap)) / 4);
    box-sizing: border-box;
    background-color: #000000;
}

.game-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: var(--board-size);
}

.game-actions h1 {
    margin: 0;
    color: #ffffff;
    font: bold 36px 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

#restart-button {
    border: 0;
    border-radius: 6px;
    min-height: 48px;
    padding: 12px 24px;
    background-color: #a8e6a0;
    color: #000000;
    font: bold 18px 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    cursor: pointer;
}

#restart-button:hover {
    background-color: #5ae65e;
}

#restart-button:focus-visible {
    outline: 3px solid #ffffff;
    outline-offset: 3px;
}

.score-panel {
    display: flex;
    gap: 12px;
    width: var(--board-size);
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.score-card {
    flex: 1;
    min-width: 0;
    padding: 14px 16px;
    border-radius: 8px;
    background-color: #202020;
    color: #ffffff;
}

.score-label {
    display: block;
    margin-bottom: 6px;
    font-size: 14px;
    color: #bbbbbb;
}

.score-card strong {
    display: block;
    font-size: 28px;
    overflow-wrap: anywhere;
}

.score-note {
    width: var(--board-size);
    margin: 0;
    color: #bbbbbb;
    font: 13px/1.6 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

#grid-container {
    position: relative;
    display: grid;
    width: var(--board-size);
    grid-template-columns: repeat(4, var(--cell-size));
    grid-template-rows: repeat(4, var(--cell-size));
    gap: var(--gap);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

.grid {
    width: var(--cell-size);
    height: var(--cell-size);
    background-color: #ffffff;
    border-radius: 5px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 50px;
    font-weight: bold;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    color: #000000;
    box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1);
    user-select: none;
}

/* 背景格子固定，數字方塊在上方獨立移動。 */
.grid {
    background-color: #d3d3d3;
}

.tile {
    position: absolute;
    top: 0;
    left: 0;
    width: var(--cell-size);
    height: var(--cell-size);
    border-radius: 5px;
    display: flex;
    justify-content: center;
    align-items: center;
    font: bold clamp(26px, 10vw, 50px) 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    color: #000000;
    user-select: none;
    pointer-events: none;
}

/* 根据方块的值设置不同的背景颜色 */
.grid-0 {
    background-color: #d3d3d3;
}

.grid-2 {
    background-color: #a8e6a0;
}

.grid-4 {
    background-color: #5ae65e;
}

.grid-8 {
    background-color: #ffeb3b;
}

.grid-16 {
    background-color: #fdd835;
}

.grid-32 {
    background-color: #ff9800;
}

.grid-64 {
    background-color: #f57c00;
}

.grid-128 {
    background-color: #f44336;
}

.grid-256 {
    background-color: #e53935;
}

.grid-512 {
    background-color: #9c27b0;
}

.grid-1024 {
    background-color: #7b1fa2;
}

.grid-2048 {
    background-color: #2196f3;
}
[hidden] { display: none !important; }
.tile-small { font-size: clamp(20px, 7vw, 34px); }
.tile-tiny { font-size: clamp(14px, 5vw, 24px); }
.global-panel .score-card { border: 1px solid #41613e; box-sizing: border-box; }
.game-over {
    width: var(--board-size);
    box-sizing: border-box;
    padding: 18px;
    border: 1px solid #a8e6a0;
    border-radius: 8px;
    background: #192217;
    color: white;
    font: 15px/1.6 'Segoe UI', sans-serif;
}
.game-over h2 { margin: 0; font-size: 24px; }
.game-over p { margin: 8px 0; }
.game-over button {
    min-height: 44px;
    padding: 8px 14px;
    border: 0;
    border-radius: 6px;
    background: #a8e6a0;
    color: #000;
    font: bold 15px 'Segoe UI', sans-serif;
    cursor: pointer;
    margin: 4px 8px 0 0;
}
button:focus-visible, .game-over:focus-visible { outline: 3px solid white; outline-offset: 3px; }
@media (max-width: 360px) {
    .score-card { padding: 10px; }
    .score-label { font-size: 12px; }
    .score-card strong { font-size: 24px; }
}

/* 遊戲結束後才開啟的原生對話框，背景遊戲暫停操作。 */
dialog.game-over {
    width: min(640px, calc(100vw - 24px));
    max-width: none;
    max-height: 90vh;
    max-height: 90dvh;
    overflow-y: auto;
    overscroll-behavior: contain;
    margin: auto;
    padding: clamp(16px, 4vw, 28px);
    box-shadow: 0 20px 80px #000a;
}
dialog.game-over::backdrop { background: #000b; backdrop-filter: blur(5px); }
.result-heading h2 { font-size: 28px; }
.result-heading p { color: #ddd; font-size: 17px; }
.result-heading strong { color: #a8e6a0; font-size: 30px; margin-left: 10px; }
.game-over .score-panel { width: 100%; margin: 16px 0 20px; }
.game-over .score-card { padding: 12px; }
.game-over .score-card strong { font-size: clamp(20px, 5vw, 28px); }
.game-over label { display: block; margin-bottom: 6px; font-weight: 600; }
.name-entry { display: flex; gap: 8px; align-items: stretch; }
.name-entry input {
    flex: 1; min-width: 0; width: 100%; box-sizing: border-box;
    border: 1px solid #637260; border-radius: 6px; padding: 10px 12px;
    background: #10170f; color: white; font: 16px 'Segoe UI', sans-serif;
}
.name-entry button { flex-shrink: 0; margin: 0; }
.game-over button:disabled { opacity: .6; cursor: default; }
.game-over input:disabled { opacity: .7; }
.game-over .dialog-note { color: #bfc8bc; font-size: 12px; line-height: 1.6; margin: 8px 0; }
#submit-status { font-size: 14px; color: #d2edc9; }
.leaderboard-heading { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 20px; }
.leaderboard-heading h3 { margin: 0; font-size: 20px; }
.leaderboard-heading button { background: #344431; color: white; margin: 0; font-size: 13px; }
.leaderboard-scroll { max-height: 36vh; max-height: 36dvh; overflow-y: auto; overscroll-behavior: contain; margin: 12px 0; border-radius: 6px; }
#leaderboard-table { width: 100%; border-collapse: collapse; table-layout: fixed; font-variant-numeric: tabular-nums; }
#leaderboard-table th { position: sticky; top: 0; background: #30432b; z-index: 1; }
#leaderboard-table th, #leaderboard-table td { padding: 10px 8px; text-align: left; border-bottom: 1px solid #344431; overflow-wrap: anywhere; }
#leaderboard-table th:first-child { width: 44px; }
#leaderboard-table th:last-child { width: 36%; }
#leaderboard-table td:last-child, #leaderboard-table th:last-child { text-align: right; }
#leaderboard-table tbody tr:nth-child(even) { background: #ffffff06; }
#leaderboard-table tbody tr:nth-child(-n+3) td:first-child { color: #a8e6a0; font-weight: bold; }
.result-actions { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 8px; margin-top: 18px; }
.result-actions button { margin: 0; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
@media (max-width: 360px) {
    .name-entry { flex-direction: column; }
    #leaderboard-table th, #leaderboard-table td { padding: 8px 6px; font-size: 13px; }
}
body:has(#game-over[open]) { overflow: hidden; }
