:root {
  --rosewater: #f5e0dc;
  --flamingo: #f2cdcd;
  --pink: #f5c2e7;
  --mauve: #cba6f7;
  --red: #f38ba8;
  --maroon: #eba0ac;
  --peach: #fab387;
  --yellow: #f9e2af;
  --green: #a6e3a1;
  --teal: #94e2d5;
  --sky: #89dceb;
  --sapphire: #74c7ec;
  --blue: #89b4fa;
  --lavender: #b4befe;
  --text: #cdd6f4;
  --subtext1: #bac2de;
  --subtext0: #a6adc8;
  --overlay2: #9399b2;
  --overlay1: #7f849c;
  --overlay0: #6c7086;
  --surface2: #585b70;
  --surface1: #45475a;
  --surface0: #313244;
  --base: #1e1e2e;
  --mantle: #181825;
  --crust: #11111b;
  --speed: 110ms;
}

* {
  box-sizing: border-box;
}

html {
  color-scheme: dark;
}

body {
  --board: min(92vw, 480px);
  --gap: calc(var(--board) * 0.03);
  --radius: calc(var(--board) * 0.02);
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--base);
  color: var(--text);
  font-family: "Clear Sans", "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

a {
  color: var(--blue);
  text-decoration-color: color-mix(in srgb, var(--blue) 40%, transparent);
}

a:hover {
  color: var(--lavender);
}

.game {
  width: var(--board);
  margin: clamp(12px, 4vh, 40px) auto 0;
}

.header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
}

.title {
  margin: 0;
  padding: 0 0 0.08em;
  font-size: clamp(2.6rem, 14vw, 4.5rem);
  line-height: 0.86;
  font-weight: 800;
  color: var(--mauve);
  letter-spacing: -0.02em;
}

.scores {
  display: flex;
  gap: 8px;
  padding-bottom: 0.1em;
}

.score-box {
  min-width: 78px;
  padding: 6px 14px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
  background: var(--surface0);
  border-radius: 8px;
}

.score-label {
  display: block;
  font-size: 0.68rem;
  line-height: 1.3;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--subtext0);
}

.score-value {
  display: block;
  font-size: 1.35rem;
  line-height: 1.25;
  font-weight: 800;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 18px 0 14px;
}

.hint {
  margin: 0;
  font-size: 0.92rem;
  line-height: 1.35;
  color: var(--subtext1);
}

.hint strong {
  color: var(--peach);
}

.hint-keys {
  font-size: 0.78rem;
  color: var(--overlay1);
}

.buttons {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.btn {
  font: inherit;
  font-weight: 700;
  font-size: 0.88rem;
  padding: 9px 14px;
  color: var(--text);
  background: var(--surface1);
  border: 0;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, transform 0.05s, opacity 0.15s;
}

.btn:hover {
  background: var(--surface2);
}

.btn:active {
  transform: translateY(1px);
}

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

.btn:focus-visible,
.board:focus-visible,
.link-btn:focus-visible,
.focus-exit:focus-visible {
  outline: 2px solid var(--lavender);
  outline-offset: 2px;
}

.btn-primary {
  background: var(--mauve);
  color: var(--crust);
}

.btn-primary:hover {
  background: var(--lavender);
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 9px 11px;
  color: var(--subtext0);
}

.btn-icon:hover {
  color: var(--text);
}

.board {
  position: relative;
  width: var(--board);
  height: var(--board);
  padding: var(--gap);
  background: var(--crust);
  border-radius: calc(var(--radius) * 1.6);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.grid,
.tiles {
  position: absolute;
  inset: var(--gap);
}

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

.cell {
  background: var(--surface0);
  border-radius: var(--radius);
  opacity: 0.7;
}

.tile {
  --r: 0;
  --c: 0;
  position: absolute;
  top: 0;
  left: 0;
  width: calc((100% - 3 * var(--gap)) / 4);
  height: calc((100% - 3 * var(--gap)) / 4);
  transform: translate(calc(var(--c) * (100% + var(--gap))), calc(var(--r) * (100% + var(--gap))));
  transition: transform var(--speed) ease-in-out;
  will-change: transform;
}

.tile-inner {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  font-weight: 800;
  font-size: calc(var(--board) * 0.095);
  line-height: 1;
  font-variant-numeric: tabular-nums;
  color: var(--crust);
  background: var(--t-bg, var(--overlay0));
  box-shadow: 0 0 calc(var(--board) * 0.04) var(--t-glow, transparent);
}

.tile.new .tile-inner {
  animation: appear 180ms ease 60ms backwards;
}

.tile.merged .tile-inner {
  animation: pop 180ms ease 60ms;
}

.tile.gone {
  z-index: 0;
}

.tile.gone .tile-inner {
  opacity: 0.6;
}

@keyframes appear {
  from {
    transform: scale(0);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes pop {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.18);
  }
  100% {
    transform: scale(1);
  }
}

.v2 {
  --t-bg: var(--surface1);
}

.v4 {
  --t-bg: var(--surface2);
}

.v2 .tile-inner,
.v4 .tile-inner {
  color: var(--text);
}

.v8 {
  --t-bg: var(--peach);
}

.v16 {
  --t-bg: var(--maroon);
}

.v32 {
  --t-bg: var(--red);
}

.v64 {
  --t-bg: var(--pink);
}

.v128 {
  --t-bg: var(--yellow);
  --t-glow: color-mix(in srgb, var(--yellow) 35%, transparent);
}

.v256 {
  --t-bg: var(--green);
  --t-glow: color-mix(in srgb, var(--green) 40%, transparent);
}

.v512 {
  --t-bg: var(--teal);
  --t-glow: color-mix(in srgb, var(--teal) 45%, transparent);
}

.v1024 {
  --t-bg: var(--sapphire);
  --t-glow: color-mix(in srgb, var(--sapphire) 50%, transparent);
}

.v2048 {
  --t-bg: var(--mauve);
  --t-glow: color-mix(in srgb, var(--mauve) 70%, transparent);
}

.vbig {
  --t-bg: var(--lavender);
  --t-glow: color-mix(in srgb, var(--lavender) 70%, transparent);
}

.v128 .tile-inner,
.v256 .tile-inner,
.v512 .tile-inner {
  font-size: calc(var(--board) * 0.08);
}

.v1024 .tile-inner,
.v2048 .tile-inner {
  font-size: calc(var(--board) * 0.066);
}

.vbig .tile-inner {
  font-size: calc(var(--board) * 0.052);
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  background: color-mix(in srgb, var(--base) 82%, transparent);
  backdrop-filter: blur(2px);
  border-radius: calc(var(--radius) * 1.6);
  animation: fade 300ms ease;
}

.overlay.hidden {
  display: none;
}

.overlay-title {
  margin: 0;
  padding: 0 12px;
  font-size: clamp(1.7rem, 8vw, 2.6rem);
  font-weight: 800;
  text-align: center;
  color: var(--text);
}

.overlay.win .overlay-title {
  color: var(--yellow);
}

.overlay.lose .overlay-title {
  color: var(--red);
}

.overlay-buttons {
  display: flex;
  gap: 10px;
}

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

.data-row {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-top: 12px;
}

.data-spacer {
  flex: 1;
}

.link-btn {
  font: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 2px 0;
  color: var(--overlay1);
  background: none;
  border: 0;
  cursor: pointer;
  text-decoration: underline;
  text-decoration-color: transparent;
  transition: color 0.15s, text-decoration-color 0.15s;
}

.link-btn:hover {
  color: var(--lavender);
  text-decoration-color: currentColor;
}

#lang-btn {
  font-size: 0.78rem;
  color: var(--subtext0);
  padding: 2px 8px;
  border: 1px solid var(--surface1);
  border-radius: 6px;
  text-decoration: none;
}

#lang-btn:hover {
  border-color: var(--overlay0);
}

.privacy {
  margin: 12px 0 0;
  font-size: 0.78rem;
  line-height: 1.45;
  color: var(--overlay1);
}

.footer {
  width: var(--board);
  margin: auto 0 0;
  padding: 22px 0 calc(18px + env(safe-area-inset-bottom));
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--subtext0);
}

.footer p {
  margin: 0 0 5px;
}

.footer-small {
  font-size: 0.75rem;
  color: var(--overlay1);
}

.footer strong {
  color: var(--text);
}

.focus-exit {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  right: max(10px, env(safe-area-inset-right));
  z-index: 30;
  display: none;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  padding: 0;
  color: var(--overlay1);
  background: var(--surface0);
  border: 0;
  border-radius: 10px;
  cursor: pointer;
  opacity: 0.55;
  transition: opacity 0.15s, color 0.15s;
}

.focus-exit:hover,
.focus-exit:focus-visible {
  opacity: 1;
  color: var(--text);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  z-index: 40;
  transform: translate(-50%, 12px);
  padding: 10px 18px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
  background: var(--surface1);
  border-radius: 10px;
  box-shadow: 0 8px 24px var(--crust);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  max-width: min(90vw, 420px);
  text-align: center;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

body.focus-mode {
  --board: min(94vw, 76vh, 760px);
  justify-content: center;
}

body.focus-mode .game {
  width: var(--board);
  margin: 0 auto;
}

body.focus-mode .header,
body.focus-mode .toolbar,
body.focus-mode .data-row,
body.focus-mode .privacy,
body.focus-mode .footer {
  display: none;
}

body.focus-mode .focus-exit {
  display: flex;
}

@media (max-width: 420px) {
  .toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .buttons .btn {
    flex: 1;
  }

  .btn-icon {
    flex: 0 0 auto;
  }

  .data-row {
    gap: 10px;
  }
}

@media (prefers-reduced-motion: reduce) {
  :root {
    --speed: 1ms;
  }

  .tile.new .tile-inner,
  .tile.merged .tile-inner,
  .overlay {
    animation: none;
  }

  .toast {
    transition: opacity 0.2s;
  }
}
