* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --accent: #f2b179;
  --accent-dark: #ef6c00;
  --ink: #4a3526;
  --gap: clamp(6px, 1.8vw, 10px);
}

html {
  height: 100%;
}

html, body {
  font-family: "PingFang SC", "Microsoft YaHei", -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  background: linear-gradient(160deg, #fff3e0 0%, #ffe0b2 45%, #ffccbc 100%);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}

/* Language toggle — top-right corner, matches the playful button style. */
#langToggle {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  right: max(10px, env(safe-area-inset-right));
  z-index: 60;
  display: flex;
  gap: 2px;
  padding: 3px;
  border: 3px solid var(--accent);
  border-radius: 14px;
  background: #fff;
  box-shadow: 0 4px 0 rgba(239, 108, 0, 0.25);
}

#langToggle button {
  font-family: inherit;
  font-size: clamp(11px, 2.6vw, 13px);
  font-weight: 800;
  padding: 5px 8px;
  border: none;
  border-radius: 11px;
  background: transparent;
  color: var(--accent-dark);
  cursor: pointer;
  white-space: nowrap;
}

#langToggle button.active { background: var(--accent); color: #fff; }
#langToggle button:active { transform: translateY(1px); }

.screen {
  margin: auto 0;
  width: 100%;
  max-width: 440px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.hidden { display: none !important; }

/* ---- Header: centered title (clear of the fixed corner buttons) + scores ---- */
h1 {
  font-size: clamp(40px, 11vw, 58px);
  color: #776e65;
  text-shadow: 2px 2px 0 #fff, 4px 4px 0 rgba(239, 108, 0, 0.25);
}

.scores { display: flex; gap: 10px; }

.scorebox {
  position: relative;
  min-width: 78px;
  padding: 8px 14px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.75);
  box-shadow: 0 4px 0 rgba(239, 108, 0, 0.15);
  text-align: center;
}

.scorebox .label {
  display: block;
  font-size: clamp(11px, 3vw, 13px);
  font-weight: 700;
  color: var(--accent-dark);
}

.scorebox b {
  font-size: clamp(18px, 5vw, 24px);
  font-weight: 800;
}

/* "+N" that floats up out of the score box on a merge. */
#scoreAdd {
  position: absolute;
  left: 0;
  right: 0;
  top: 6px;
  font-weight: 800;
  font-size: clamp(16px, 4.5vw, 20px);
  color: var(--accent-dark);
  opacity: 0;
  pointer-events: none;
}

#scoreAdd.float { animation: scoreFloat 0.7s ease-out; }

@keyframes scoreFloat {
  0%   { opacity: 1; transform: translateY(0); }
  100% { opacity: 0; transform: translateY(-32px); }
}

.subtitle {
  font-size: clamp(14px, 3.8vw, 18px);
  color: var(--accent-dark);
  font-weight: 600;
  text-align: center;
}

/* ---- Board ---- */
/* Classic 2048 board + tile palette from the MIT-licensed original by
   Gabriele Cirulli (github.com/gabrielecirulli/2048). */
#board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  background: #bbada0;
  border-radius: 16px;
  box-shadow: 0 8px 0 rgba(180, 120, 60, 0.35), 0 12px 24px rgba(0, 0, 0, 0.15);
  touch-action: none;
}

.grid {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--gap);
  padding: var(--gap);
}

.grid .cell {
  background: rgba(238, 228, 218, 0.35);
  border-radius: 10px;
}

#tiles {
  position: absolute;
  inset: var(--gap);
}

.tile {
  position: absolute;
  top: 0;
  left: 0;
  width: calc((100% - 3 * var(--gap)) / 4);
  height: calc((100% - 3 * var(--gap)) / 4);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  font-weight: 800;
  font-size: clamp(24px, 9.5vw, 46px);
  background: #eee4da;
  color: #776e65;
  transform: translate(calc(var(--c) * 100% + var(--c) * var(--gap)),
                       calc(var(--r) * 100% + var(--r) * var(--gap)));
  transition: transform 0.12s ease-in-out;
}

.tile.d3 { font-size: clamp(19px, 7.5vw, 38px); }
.tile.d4 { font-size: clamp(15px, 6vw, 30px); }

.tile.v2    { background: #eee4da; color: #776e65; }
.tile.v4    { background: #ede0c8; color: #776e65; }
.tile.v8    { background: #f2b179; color: #f9f6f2; }
.tile.v16   { background: #f59563; color: #f9f6f2; }
.tile.v32   { background: #f67c5f; color: #f9f6f2; }
.tile.v64   { background: #f65e3b; color: #f9f6f2; }
.tile.v128  { background: #edcf72; color: #f9f6f2; box-shadow: 0 0 18px 4px rgba(243, 215, 116, 0.45); }
.tile.v256  { background: #edcc61; color: #f9f6f2; box-shadow: 0 0 20px 5px rgba(243, 215, 116, 0.5); }
.tile.v512  { background: #edc850; color: #f9f6f2; box-shadow: 0 0 22px 6px rgba(243, 215, 116, 0.55); }
.tile.v1024 { background: #edc53f; color: #f9f6f2; box-shadow: 0 0 24px 7px rgba(243, 215, 116, 0.6); }
.tile.v2048 { background: #edc22e; color: #f9f6f2; box-shadow: 0 0 26px 8px rgba(243, 215, 116, 0.65); }
.tile.vsuper { background: #3c3a32; color: #f9f6f2; }

/* New tile pops in, merged tile pops on top — both wait for the slide (~120ms). */
.tile.new { animation: tilePop 0.18s ease 0.12s backwards; }

.tile.merged { z-index: 2; animation: tileMerge 0.2s ease 0.1s backwards; }

@keyframes tilePop {
  0%   { transform: translate(calc(var(--c) * 100% + var(--c) * var(--gap)),
                              calc(var(--r) * 100% + var(--r) * var(--gap))) scale(0); }
  100% { transform: translate(calc(var(--c) * 100% + var(--c) * var(--gap)),
                              calc(var(--r) * 100% + var(--r) * var(--gap))) scale(1); }
}

@keyframes tileMerge {
  0%   { transform: translate(calc(var(--c) * 100% + var(--c) * var(--gap)),
                              calc(var(--r) * 100% + var(--r) * var(--gap))) scale(0); }
  60%  { transform: translate(calc(var(--c) * 100% + var(--c) * var(--gap)),
                              calc(var(--r) * 100% + var(--r) * var(--gap))) scale(1.18); }
  100% { transform: translate(calc(var(--c) * 100% + var(--c) * var(--gap)),
                              calc(var(--r) * 100% + var(--r) * var(--gap))) scale(1); }
}

/* ---- Win / game-over overlay ---- */
#overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 16px;
  text-align: center;
  border-radius: 16px;
  background: rgba(255, 248, 235, 0.82);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
  animation: overlayIn 0.35s ease 0.45s backwards;
}

@keyframes overlayIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

#overlay h2 {
  font-size: clamp(24px, 7vw, 34px);
  text-shadow: 2px 2px 0 #fff;
}

#overlay p {
  font-size: clamp(15px, 4.2vw, 19px);
  font-weight: 600;
  color: var(--accent-dark);
}

.overlay-btns {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin-top: 6px;
}

.big-btn {
  font-size: clamp(17px, 4.8vw, 22px);
  font-weight: 800;
  color: #fff;
  background: linear-gradient(180deg, #ffa94d, var(--accent-dark));
  border: none;
  border-radius: 16px;
  padding: 12px 26px;
  cursor: pointer;
  box-shadow: 0 5px 0 #c75b00;
}

.big-btn:active { transform: translateY(3px); box-shadow: 0 2px 0 #c75b00; }

/* ---- Controls ---- */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
}

.ctrl-btn {
  font-size: clamp(15px, 4vw, 18px);
  font-weight: 700;
  padding: 12px 16px;
  border: none;
  border-radius: 14px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  box-shadow: 0 4px 0 rgba(180, 120, 60, 0.3);
}

.ctrl-btn:active { transform: translateY(2px); box-shadow: 0 2px 0 rgba(180, 120, 60, 0.3); }

.ctrl-btn.disabled { opacity: 0.45; pointer-events: none; }

.howto {
  font-size: clamp(13px, 3.5vw, 15px);
  color: rgba(74, 53, 38, 0.75);
  text-align: center;
  line-height: 1.5;
  max-width: 400px;
}
