:root {
  --bg: #f6f1e6;
  --bg-glow: #fffdf7;
  --surface: #fffdf8;
  --board: #bba78f;
  --cell: #d3c6b4;
  --ink: #3c352c;
  --ink-soft: #7b6f60;
  --muted: #a2957f;
  --card: #b39d80;
  --card-fg: #fdf9f1;
  --btn: #8a7150;
  --btn-fg: #fdf9f1;
  --btn-edge: #6d583c;
  --line: rgba(60, 53, 44, 0.12);
  --shade: rgba(60, 53, 44, 0.22);
  --panel: rgba(250, 246, 238, 0.9);
  --win-veil: rgba(232, 169, 51, 0.86);

  --t2: #efe4d3;
  --t4: #eddbba;
  --t8: #f0b57c;
  --t16: #ef9a63;
  --t32: #f07d54;
  --t64: #ef6038;
  --t128: #eccb78;
  --t256: #ecc467;
  --t512: #ebbc55;
  --t1024: #eab344;
  --t2048: #e8a933;
  --tsuper: #38332a;
  --tile-ink: #6f6354;
  --tile-light: #fdf9f1;

  --pad: 0.7rem;
  --gap: 0.7rem;
  --radius: 0.85rem;
  --slide: 170ms;
  --pop: 250ms;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --font-body: "Outfit", "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  --font-mark: "Fraunces", "Iowan Old Style", "Songti SC", Georgia, serif;
}

[data-theme="dark"] {
  --bg: #171410;
  --bg-glow: #241e16;
  --surface: #1f1b15;
  --board: #3c342a;
  --cell: #4a4237;
  --ink: #f2ebdd;
  --ink-soft: #c2b6a3;
  --muted: #928572;
  --card: #4a4237;
  --card-fg: #f2ebdd;
  --btn: #d6c5a8;
  --btn-fg: #241e16;
  --btn-edge: #a08d70;
  --line: rgba(242, 235, 221, 0.14);
  --shade: rgba(0, 0, 0, 0.5);
  --panel: rgba(31, 27, 21, 0.92);
  --win-veil: rgba(122, 88, 18, 0.86);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button:not(:disabled) {
  cursor: pointer;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.page {
  display: flex;
  justify-content: center;
  min-height: 100dvh;
  padding:
    max(1.1rem, env(safe-area-inset-top))
    max(0.9rem, env(safe-area-inset-right))
    max(1.4rem, env(safe-area-inset-bottom))
    max(0.9rem, env(safe-area-inset-left));
  background:
    radial-gradient(1100px 520px at 50% -12%, var(--bg-glow), transparent 72%),
    var(--bg);
}

.stage {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  width: min(100%, 26.5rem);
}

.hero {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 0.9rem;
}

.hero-copy {
  min-width: 0;
}

.eyebrow-row {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  margin-bottom: 0.15rem;
}

.eyebrow {
  margin: 0;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.lang {
  padding: 0.12rem 0.5rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font: inherit;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  line-height: 1.5;
}

.lang:hover {
  color: var(--ink);
  border-color: var(--btn);
}

.title {
  margin: 0;
  font-family: var(--font-mark);
  font-size: clamp(3rem, 12vw, 4.2rem);
  font-weight: 700;
  line-height: 0.9;
  letter-spacing: -0.04em;
}

.lede {
  margin: 0.5rem 0 0;
  max-width: 15.5rem;
  font-size: 0.88rem;
  line-height: 1.45;
  color: var(--ink-soft);
}

.scores {
  display: flex;
  gap: 0.45rem;
  flex-shrink: 0;
}

.score-card {
  position: relative;
  min-width: 4.5rem;
  padding: 0.4rem 0.65rem 0.45rem;
  border-radius: var(--radius);
  background: var(--card);
  color: var(--card-fg);
  text-align: center;
  overflow: hidden;
}

.score-label {
  display: block;
  font-size: 0.62rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  opacity: 0.78;
}

.score-value {
  display: block;
  font-size: 1.2rem;
  font-weight: 700;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
}

.score-gain {
  position: absolute;
  top: 0.3rem;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--card-fg);
  opacity: 0;
  pointer-events: none;
}

.score-gain.is-on {
  animation: gain-float 620ms var(--ease) forwards;
}

.board-wrap {
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.board {
  --pad: 0.7rem;
  position: relative;
  aspect-ratio: 1;
  padding: var(--pad);
  border-radius: calc(var(--radius) + var(--pad));
  background:
    linear-gradient(165deg, rgba(255, 255, 255, 0.16) 0%, var(--board) 42%, rgba(0, 0, 0, 0.14) 100%);
  box-shadow:
    inset 0 1.5px 0 rgba(255, 255, 255, 0.3),
    inset 0 -3px 0 rgba(0, 0, 0, 0.18),
    inset 0 10px 18px rgba(0, 0, 0, 0.09),
    0 8px 0 rgba(0, 0, 0, 0.16),
    0 16px 28px var(--shade);
}

.board-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--gap);
  height: 100%;
}

.board-grid span {
  border-radius: 0.55rem;
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.14), rgba(255, 255, 255, 0.09) 72%), var(--cell);
  box-shadow:
    inset 0 3px 6px rgba(0, 0, 0, 0.26),
    inset 0 -2px 0 rgba(255, 255, 255, 0.16);
}

.tiles {
  position: absolute;
  inset: var(--pad);
}

.tile {
  --r: 0;
  --c: 0;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  width: calc((100% - var(--gap) * 3) / 4);
  height: calc((100% - var(--gap) * 3) / 4);
  container-type: size;
  transform: translate(
    calc(var(--c) * (100% + var(--gap))),
    calc(var(--r) * (100% + var(--gap)))
  );
  transition: transform var(--slide) var(--ease);
}

.tile.is-ghost {
  z-index: 1;
}

.tile-face {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  border-radius: 0.55rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  color: var(--tile-ink);
  font-size: 1.4rem;
  font-size: 40cqmin;
  background:
    linear-gradient(
      160deg,
      rgba(255, 255, 255, 0.4) 0%,
      rgba(255, 255, 255, 0.08) 30%,
      transparent 54%,
      rgba(0, 0, 0, 0.07) 100%
    ),
    var(--tile-fill, var(--t2));
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.28), 0 2px 3px rgba(0, 0, 0, 0.18);
  box-shadow:
    inset 1.5px 2px 0 rgba(255, 255, 255, 0.5),
    inset -1.5px -3px 0 rgba(0, 0, 0, 0.14),
    0 1px 0 rgba(0, 0, 0, 0.18),
    0 5px 0 var(--tile-edge, rgba(0, 0, 0, 0.22)),
    0 10px 16px rgba(0, 0, 0, 0.22);
}

.tile[data-digits="3"] .tile-face {
  font-size: 33cqmin;
}

.tile[data-digits="4"] .tile-face {
  font-size: 26cqmin;
}

.tile[data-digits="5"] .tile-face {
  font-size: 21cqmin;
}

.tile[data-digits="6"] .tile-face {
  font-size: 18cqmin;
}

.tile[data-v="4"] .tile-face { --tile-fill: var(--t4); }
.tile[data-v="8"] .tile-face { --tile-fill: var(--t8); --tile-edge: #b9762f; color: var(--tile-light); }
.tile[data-v="16"] .tile-face { --tile-fill: var(--t16); --tile-edge: #b7662c; color: var(--tile-light); }
.tile[data-v="32"] .tile-face { --tile-fill: var(--t32); --tile-edge: #b4552a; color: var(--tile-light); }
.tile[data-v="64"] .tile-face { --tile-fill: var(--t64); --tile-edge: #ad3f1c; color: var(--tile-light); }
.tile[data-v="128"] .tile-face { --tile-fill: var(--t128); --tile-edge: #b08c2c; color: var(--tile-light); }
.tile[data-v="256"] .tile-face { --tile-fill: var(--t256); --tile-edge: #ae862a; color: var(--tile-light); }
.tile[data-v="512"] .tile-face { --tile-fill: var(--t512); --tile-edge: #ab8028; color: var(--tile-light); }
.tile[data-v="1024"] .tile-face { --tile-fill: var(--t1024); --tile-edge: #a87a26; color: var(--tile-light); }
.tile[data-v="2048"] .tile-face { --tile-fill: var(--t2048); --tile-edge: #a57422; color: var(--tile-light); }
.tile[data-v="4096"] .tile-face,
.tile[data-v="8192"] .tile-face,
.tile[data-v="16384"] .tile-face,
.tile[data-v="32768"] .tile-face,
.tile[data-v="65536"] .tile-face,
.tile[data-v="131072"] .tile-face {
  --tile-fill: var(--tsuper);
  --tile-edge: #1d1913;
  color: var(--tile-light);
}

.tile[data-v="128"] .tile-face,
.tile[data-v="256"] .tile-face,
.tile[data-v="512"] .tile-face,
.tile[data-v="1024"] .tile-face,
.tile[data-v="2048"] .tile-face {
  box-shadow:
    inset 1.5px 2px 0 rgba(255, 255, 255, 0.55),
    inset -1.5px -3px 0 rgba(0, 0, 0, 0.12),
    0 1px 0 rgba(0, 0, 0, 0.16),
    0 5px 0 var(--tile-edge),
    0 10px 16px rgba(0, 0, 0, 0.2),
    0 0 20px rgba(232, 169, 51, 0.45);
}

.tile.is-ghost .tile-face {
  --tile-edge: transparent;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
  opacity: 0.9;
}

.tile-face[data-kind="new"] {
  animation: tile-in var(--pop) var(--ease) both;
  animation-delay: 90ms;
}

.tile-face[data-kind="merged"] {
  animation: tile-pop var(--pop) var(--ease) both;
  animation-delay: 90ms;
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 8;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.65rem;
  padding: 1.4rem;
  border-radius: inherit;
  text-align: center;
  background: var(--panel);
  animation: overlay-in 220ms var(--ease) both;
}

.overlay[hidden] {
  display: none;
}

.overlay[data-kind="win"] {
  background: var(--win-veil);
}

.overlay-title {
  margin: 0;
  font-family: var(--font-mark);
  font-size: clamp(1.7rem, 7cqi, 2.6rem);
  font-weight: 700;
  letter-spacing: -0.03em;
}

.overlay-title.is-large {
  font-size: clamp(2rem, 9cqi, 3rem);
}

.overlay-body {
  margin: 0;
  max-width: 17rem;
  font-size: 0.88rem;
  line-height: 1.5;
  color: var(--ink-soft);
}

.overlay-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.45rem;
  margin-top: 0.3rem;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.4rem 1rem;
  padding: 0 0.1rem;
}

.stat {
  display: inline-flex;
  align-items: baseline;
  gap: 0.3rem;
}

.stat-label {
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.stat-value {
  font-size: 0.92rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.saved {
  margin-left: auto;
  font-size: 0.72rem;
  color: var(--muted);
}

.toolbar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  text-align: center;
}

.hint {
  margin: 0;
  font-size: 0.8rem;
  line-height: 1.4;
  color: var(--ink-soft);
}

.hint-touch {
  display: none;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.5rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 0.7rem;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  box-shadow: 0 3px 0 var(--line);
  transition: transform 120ms var(--ease), box-shadow 120ms var(--ease);
}

.btn:hover:not(:disabled) {
  border-color: var(--btn);
}

.btn:active:not(:disabled) {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--line);
}

.btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.btn:focus-visible {
  outline: 2px solid var(--btn);
  outline-offset: 2px;
}

.btn-icon {
  width: 2.4rem;
  height: 2.4rem;
  padding: 0;
}

.btn-primary {
  background: var(--btn);
  border-color: var(--btn);
  color: var(--btn-fg);
  box-shadow: 0 3px 0 var(--btn-edge);
}

.btn-primary:active:not(:disabled) {
  box-shadow: 0 1px 0 var(--btn-edge);
}

.btn svg {
  width: 1.15rem;
  height: 1.15rem;
}

.dpad {
  display: none;
  grid-template-columns: repeat(3, 2.9rem);
  grid-template-rows: repeat(2, 2.9rem);
  gap: 0.4rem;
  width: max-content;
  margin: 0.1rem auto 0;
}

.btn-pad {
  padding: 0;
  border-radius: 0.8rem;
}

.btn-pad[data-dir="up"] { grid-area: 1 / 2; }
.btn-pad[data-dir="left"] { grid-area: 2 / 1; }
.btn-pad[data-dir="down"] { grid-area: 2 / 2; }
.btn-pad[data-dir="right"] { grid-area: 2 / 3; }

.back {
  text-align: center;
  font-size: 0.8rem;
}

.back a {
  color: var(--muted);
  text-decoration: none;
}

.back a:hover {
  color: var(--ink);
  text-decoration: underline;
}

.noscript {
  padding: 1rem;
  text-align: center;
}

@keyframes tile-in {
  from { transform: scale(0.2); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

@keyframes tile-pop {
  0% { transform: scale(0.35); }
  62% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

@keyframes gain-float {
  from { opacity: 1; transform: translate(-50%, 0); }
  to { opacity: 0; transform: translate(-50%, -1.1rem); }
}

@keyframes overlay-in {
  from { opacity: 0; transform: scale(0.96); }
  to { opacity: 1; transform: scale(1); }
}

@media (max-width: 639px) {
  .lede {
    display: none;
  }
  .hint-keys {
    display: none;
  }
  .hint-touch {
    display: inline;
  }
  .dpad {
    display: grid;
  }
  .saved {
    flex-basis: 100%;
    margin-left: 0;
  }
}

@media (min-width: 640px) {
  .stage {
    width: min(100%, 28rem);
    gap: 1.15rem;
    padding-top: 1rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .tile,
  .tile-face,
  .score-gain,
  .overlay,
  .btn {
    animation: none !important;
    transition: none !important;
  }
}
