:root { --gap: 10px; --size: 400px; --cell: calc((var(--size) - var(--gap) * 5) / 4); --accent: #edc22e; }
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: #1a1612; color: #f5efe6;
  font-family: system-ui, "Segoe UI", Roboto, sans-serif; touch-action: none; user-select: none; -webkit-user-select: none;
}
button { font-family: inherit; cursor: pointer; }
.hidden { display: none !important; }

#app { height: 100%; display: flex; flex-direction: column; align-items: center; padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); padding-top: calc(10px + env(safe-area-inset-top)); }
header { width: var(--size); display: flex; align-items: center; justify-content: space-between; flex: none; }
header h1 { margin: 0; font-size: 44px; color: var(--accent); letter-spacing: 1px; }
.scores { display: flex; gap: 8px; }
.stat { background: rgba(255, 255, 255, .08); border: 1px solid rgba(255, 255, 255, .12); border-radius: 10px; padding: 4px 14px; min-width: 76px; text-align: center; }
.stat .label { display: block; font-size: 10px; opacity: .7; text-transform: uppercase; letter-spacing: .5px; }
.stat b { font-size: 17px; }

.toolbar { width: var(--size); display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 6px 0 10px; flex: none; }
.hint { margin: 0; font-size: 13px; opacity: .8; line-height: 1.4; }
.hint kbd { background: rgba(255, 255, 255, .12); border: 1px solid rgba(255, 255, 255, .25); border-radius: 5px; padding: 0 5px; font-size: 11px; font-family: inherit; }
.tb-btns { display: flex; gap: 6px; flex: none; }
#btn-new, #btn-board { flex: none; background: #8f7a66; color: #fff; border: none; border-radius: 9px; padding: 9px 14px; font-weight: 700; font-size: 14px; }
#btn-new:hover, #btn-board:hover { background: #a08b76; }

#board { position: relative; width: var(--size); height: var(--size); background: #bbada0; border-radius: 12px; flex: none; touch-action: none; }
#cells, #tiles { position: absolute; inset: 0; }
.cell { position: absolute; width: var(--cell); height: var(--cell); background: rgba(238, 228, 218, .35); border-radius: 8px; }
.tile {
  position: absolute; left: 0; top: 0; width: var(--cell); height: var(--cell); border-radius: 8px;
  display: flex; align-items: center; justify-content: center; font-weight: 800;
  font-size: calc(var(--cell) * 0.5); background: #eee4da; color: #776e65;
  transition: transform .11s ease-in-out; will-change: transform;
}
.tile.d3 { font-size: calc(var(--cell) * 0.42); }
.tile.d4 { font-size: calc(var(--cell) * 0.34); }
.tile.new { animation: appear .18s ease-out; }
.tile.pop { animation: pop .2s ease-in-out; }
@keyframes appear { from { scale: 0; } to { scale: 1; } }
@keyframes pop { 0% { scale: 1; } 50% { scale: 1.18; } 100% { scale: 1; } }
.t2 { background: #eee4da; }
.t4 { background: #ede0c8; }
.t8 { background: #f2b179; color: #f9f6f2; }
.t16 { background: #f59563; color: #f9f6f2; }
.t32 { background: #f67c5f; color: #f9f6f2; }
.t64 { background: #f65e3b; color: #f9f6f2; }
.t128 { background: #edcf72; color: #f9f6f2; box-shadow: 0 0 20px rgba(243, 215, 116, .4); }
.t256 { background: #edcc61; color: #f9f6f2; box-shadow: 0 0 24px rgba(243, 215, 116, .5); }
.t512 { background: #edc850; color: #f9f6f2; box-shadow: 0 0 28px rgba(243, 215, 116, .6); }
.t1024 { background: #edc53f; color: #f9f6f2; box-shadow: 0 0 32px rgba(243, 215, 116, .7); }
.t2048 { background: #edc22e; color: #f9f6f2; box-shadow: 0 0 36px rgba(243, 215, 116, .85); }
.tbig { background: #3c3a32; color: #f9f6f2; }

#msg {
  position: absolute; inset: 0; border-radius: 12px; background: rgba(238, 228, 218, .78);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; z-index: 5; animation: fade .4s;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
#msg p { margin: 0; font-size: 34px; font-weight: 800; color: #776e65; text-align: center; padding: 0 10px; }
.msg-btns { display: flex; gap: 8px; }
#msg button { background: #8f7a66; color: #fff; border: none; border-radius: 9px; padding: 10px 18px; font-weight: 700; font-size: 15px; }
#msg-continue { background: #edc22e !important; color: #4a3b00 !important; }

/* ô bảng xếp hạng trong hộp thông báo (nền sáng) */
#msg .gnx-result { color: #776e65; background: rgba(255, 255, 255, .55); border-color: rgba(143, 122, 102, .4); margin: 0 12px; }
#msg .gnx-result b, #msg .gnx-result .gnx-open { color: #8f7a66 !important; }
#msg .gnx-result input { background: #fff; color: #4a3b00; border-color: #bbada0; }
#msg .gnx-result .gnx-edit { background: none !important; color: #776e65 !important; padding: 0 4px !important; }
