:root {
  --bg: #f1ece2;
  --surface: #fffbf3;
  --ink: #202b24;
  --muted: #73786c;
  --line: rgba(32, 43, 36, 0.14);
  --forest: #243128;
  --forest-deep: #1b251e;
  --accent: #e96745;
  --accent-deep: #cd4f31;
  --gold: #e8b64c;
  --board: #243128;
  --cell: rgba(255, 255, 255, 0.055);
  --pad: clamp(10px, 2.3vw, 14px);
  --gap: clamp(8px, 1.9vw, 12px);
  --radius: clamp(10px, 2.4vw, 14px);
  --font-sans: "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif;
  --font-display: "Iowan Old Style", "Baskerville", "Times New Roman", "Songti SC", Georgia, serif;
}

* {
  box-sizing: border-box;
}

html {
  color-scheme: light;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100svh;
  color: var(--ink);
  background: var(--bg);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

button {
  font-family: inherit;
}

::selection {
  color: var(--surface);
  background: var(--accent);
}

.ambient {
  position: fixed;
  z-index: -1;
  width: 70vw;
  height: 70vw;
  max-width: 980px;
  max-height: 980px;
  border-radius: 50%;
  pointer-events: none;
  filter: blur(10px);
}

.ambient--warm {
  top: -42vw;
  right: -25vw;
  background: radial-gradient(circle, rgba(233, 103, 69, 0.18), rgba(233, 103, 69, 0) 66%);
}

.ambient--cool {
  bottom: -50vw;
  left: -30vw;
  background: radial-gradient(circle, rgba(74, 105, 79, 0.16), rgba(74, 105, 79, 0) 66%);
}

.side-note {
  position: fixed;
  top: 50%;
  right: 26px;
  transform: translateY(-50%);
  writing-mode: vertical-rl;
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.32em;
  color: rgba(32, 43, 36, 0.34);
  user-select: none;
  pointer-events: none;
}

.shell {
  width: min(100% - 36px, 520px);
  margin: 0 auto;
  padding: clamp(26px, 5.5vh, 62px) 0 34px;
  position: relative;
}

/* ---------- Masthead ---------- */

.masthead {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 22px;
}

.brand {
  min-width: 0;
}

.eyebrow {
  margin: 0 0 12px;
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.22em;
  color: var(--muted);
  white-space: nowrap;
}

h1 {
  position: relative;
  display: inline-block;
  margin: 0;
  color: var(--forest);
  font-family: var(--font-display);
  font-size: clamp(68px, 13vw, 94px);
  font-weight: 600;
  line-height: 0.76;
  letter-spacing: -0.075em;
  font-variant-numeric: lining-nums tabular-nums;
}

.brand-dot {
  position: absolute;
  top: -0.08em;
  right: -0.2em;
  width: 0.12em;
  height: 0.12em;
  border-radius: 50%;
  background: var(--accent);
}

.tagline {
  margin: 20px 0 0;
  font-size: 12.5px;
  line-height: 1.7;
  letter-spacing: 0.02em;
  color: var(--muted);
}

.scores {
  display: flex;
  flex-shrink: 0;
  gap: 8px;
  padding-top: 3px;
}

.score-card {
  position: relative;
  min-width: 82px;
  padding: 10px 13px 9px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: rgba(255, 251, 243, 0.74);
  box-shadow: 0 6px 20px rgba(48, 41, 29, 0.045);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.score-card--current {
  border-color: var(--forest);
  color: var(--surface);
  background: var(--forest);
  box-shadow: 0 10px 24px rgba(28, 39, 31, 0.18);
}

.score-label {
  display: block;
  margin-bottom: 4px;
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.14em;
  color: var(--muted);
}

.score-card--current .score-label {
  color: rgba(255, 251, 243, 0.6);
}

.score-card strong {
  display: block;
  font-family: var(--font-display);
  font-size: 23px;
  font-weight: 600;
  line-height: 1;
  letter-spacing: -0.025em;
  font-variant-numeric: tabular-nums;
}

.score-card--best strong {
  color: var(--accent-deep);
}

.score-card--best.is-popping strong {
  animation: best-pop 420ms cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

.score-gain {
  position: absolute;
  top: 7px;
  right: 10px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: #ffd18a;
  opacity: 0;
  pointer-events: none;
}

.score-gain.is-visible {
  animation: score-float 760ms ease-out;
}

/* ---------- Controls ---------- */

.controlbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-top: clamp(26px, 4.5vh, 38px);
}

.actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn {
  appearance: none;
  border: 0;
  border-radius: 999px;
  padding: 10px 18px;
  font-size: 12.5px;
  font-weight: 650;
  letter-spacing: 0.02em;
  line-height: 1.2;
  cursor: pointer;
  transition: transform 160ms ease, background-color 160ms ease, border-color 160ms ease,
    box-shadow 160ms ease, opacity 160ms ease;
  -webkit-tap-highlight-color: transparent;
}

.btn--primary {
  color: #fffaf2;
  background: var(--accent);
  box-shadow: 0 7px 18px rgba(205, 79, 49, 0.22);
}

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

.btn--ghost {
  color: var(--ink);
  border: 1px solid var(--line);
  background: transparent;
}

.btn--ghost:hover:not(:disabled) {
  border-color: rgba(32, 43, 36, 0.3);
  background: rgba(255, 251, 243, 0.55);
}

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

.btn:disabled {
  cursor: not-allowed;
  opacity: 0.32;
}

.hint {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  font-size: 10px;
  letter-spacing: 0.04em;
  color: var(--muted);
  white-space: nowrap;
}

.hint-keys {
  font-size: 11px;
  letter-spacing: 0.05em;
  color: var(--ink);
}

.hint-touch {
  display: none;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

/* ---------- Board ---------- */

.game {
  margin-top: 16px;
}

.board-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 4px;
  margin-bottom: 9px;
  font-size: 9px;
  font-weight: 650;
  letter-spacing: 0.14em;
  color: var(--muted);
  text-transform: uppercase;
}

.board-target {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--ink);
}

.board-target i {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--accent);
}

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  padding: var(--pad);
  border: 1px solid rgba(255, 255, 255, 0.07);
  border-radius: clamp(18px, 4.5vw, 24px);
  background:
    radial-gradient(120% 100% at 0% 0%, rgba(255, 255, 255, 0.07), transparent 44%),
    linear-gradient(145deg, #2a392f 0%, #202c24 58%, #1b251e 100%);
  box-shadow:
    0 38px 64px -30px rgba(24, 35, 27, 0.65),
    0 12px 28px rgba(24, 35, 27, 0.14),
    inset 0 1px 0 rgba(255, 255, 255, 0.12);
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.board::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.035);
}

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

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

.grid {
  pointer-events: none;
}

.cell {
  border-radius: var(--radius);
  background: var(--cell);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.025);
}

.tiles {
  pointer-events: none;
}

.tile {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  container-type: size;
  will-change: transform;
}

.tile.is-sliding {
  transition: transform 130ms cubic-bezier(0.2, 0.75, 0.3, 1);
}

.tile-face {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border-radius: var(--radius);
  background-image: linear-gradient(155deg, rgba(255, 255, 255, 0.25), rgba(255, 255, 255, 0) 48%);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.38),
    inset 0 -1px 0 rgba(0, 0, 0, 0.04),
    0 3px 9px rgba(0, 0, 0, 0.16);
}

.tile-value {
  font-size: 41cqw;
  font-weight: 750;
  line-height: 1;
  letter-spacing: -0.055em;
  font-variant-numeric: tabular-nums;
  transform: translateY(-1px);
}

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

.tile[data-digits="4"] .tile-value {
  font-size: 26.5cqw;
}

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

.tile[data-digits="6"] .tile-value {
  font-size: 17.5cqw;
}

.tile[data-digits="7"] .tile-value {
  font-size: 15cqw;
}

.tile[data-digits="8"] .tile-value {
  font-size: 13cqw;
}

/* Tile palette */

.tile.v2 .tile-face {
  color: #463a2c;
  background-color: #f8f2e6;
}

.tile.v4 .tile-face {
  color: #463a2c;
  background-color: #efe2c9;
}

.tile.v8 .tile-face {
  color: #fffaf1;
  background-color: #f4b76b;
}

.tile.v16 .tile-face {
  color: #fffaf1;
  background-color: #f2a158;
}

.tile.v32 .tile-face {
  color: #fffaf1;
  background-color: #ef854f;
}

.tile.v64 .tile-face {
  color: #fffaf1;
  background-color: #e96943;
}

.tile.v128 .tile-face {
  color: #fffaf1;
  background-color: #dd5437;
}

.tile.v256 .tile-face {
  color: #fffaf1;
  background-color: #ca4230;
}

.tile.v512 .tile-face {
  color: #fffaf1;
  background-color: #b1342a;
}

.tile.v1024 .tile-face {
  color: #fff7ea;
  background-color: #932825;
}

.tile.v2048 .tile-face {
  color: #3b2a0c;
  background-color: #e9b33b;
  background-image:
    linear-gradient(145deg, rgba(255, 249, 217, 0.75), rgba(255, 255, 255, 0) 52%),
    linear-gradient(135deg, #f6d477 0%, #e5a92e 62%, #d3971f 100%);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.7),
    0 0 28px rgba(238, 186, 77, 0.26),
    0 4px 12px rgba(0, 0, 0, 0.16);
}

.tile.v-super .tile-face {
  color: #f4ddc8;
  background-color: #5e1b1a;
  background-image: linear-gradient(155deg, rgba(255, 255, 255, 0.12), transparent 50%);
}

.tile.v8 .tile-value,
.tile.v16 .tile-value,
.tile.v32 .tile-value,
.tile.v64 .tile-value,
.tile.v128 .tile-value,
.tile.v256 .tile-value,
.tile.v512 .tile-value,
.tile.v1024 .tile-value {
  text-shadow: 0 1px 2px rgba(82, 29, 12, 0.16);
}

/* Motion */

.tile.is-new .tile-face {
  animation: tile-appear 200ms cubic-bezier(0.2, 0.9, 0.3, 1.18) 100ms both;
}

.tile.is-merged {
  z-index: 3;
}

.tile.is-merged .tile-face {
  animation: tile-merge 220ms cubic-bezier(0.2, 0.9, 0.3, 1.22) 80ms both;
}

.tile.ghost {
  position: fixed !important;
  z-index: 10;
  margin: 0;
  pointer-events: none;
  will-change: transform, opacity;
}

.tile.ghost .tile-face {
  box-shadow: none;
}

@keyframes tile-appear {
  0% {
    opacity: 0;
    transform: scale(0.35);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes tile-merge {
  0% {
    opacity: 0;
    transform: scale(0.5);
  }
  68% {
    opacity: 1;
    transform: scale(1.09);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes score-float {
  0% {
    opacity: 0;
    transform: translateY(7px);
  }
  18% {
    opacity: 1;
  }
  62% {
    opacity: 1;
    transform: translateY(-6px);
  }
  100% {
    opacity: 0;
    transform: translateY(-14px);
  }
}

@keyframes best-pop {
  0% {
    transform: scale(1);
  }
  45% {
    transform: scale(1.12);
  }
  100% {
    transform: scale(1);
  }
}

/* ---------- Overlay ---------- */

.board-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  border-radius: inherit;
  background: rgba(16, 24, 18, 0.7);
  backdrop-filter: blur(7px);
  -webkit-backdrop-filter: blur(7px);
  animation: overlay-in 220ms ease-out both;
}

.board-overlay[hidden] {
  display: none;
}

.overlay-card {
  width: 100%;
  max-width: 326px;
  padding: 27px 24px 24px;
  border: 1px solid rgba(255, 255, 255, 0.55);
  border-radius: 19px;
  background: var(--surface);
  box-shadow: 0 30px 70px rgba(0, 0, 0, 0.34);
  text-align: center;
  animation: card-in 320ms cubic-bezier(0.2, 0.9, 0.3, 1.08) both;
}

.overlay-card:focus {
  outline: none;
}

.overlay-kicker {
  margin: 0 0 10px;
  font-size: 8.5px;
  font-weight: 750;
  letter-spacing: 0.24em;
  color: var(--accent-deep);
}

.overlay-title {
  margin: 0;
  color: var(--forest);
  font-family: var(--font-display);
  font-size: clamp(28px, 8vw, 35px);
  font-weight: 600;
  line-height: 1.1;
  letter-spacing: -0.035em;
}

.overlay-text {
  margin: 11px 0 0;
  font-size: 12px;
  line-height: 1.65;
  color: var(--muted);
}

.overlay-text strong {
  color: var(--ink);
  font-weight: 700;
}

.overlay-actions {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-top: 21px;
}

.overlay-actions .btn {
  padding: 10px 17px;
}

.overlay-actions [hidden] {
  display: none;
}

@keyframes overlay-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes card-in {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* ---------- Footer ---------- */

.footnote {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  margin-top: 26px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.footnote-label {
  flex-shrink: 0;
  padding-top: 2px;
  font-size: 9px;
  font-weight: 750;
  letter-spacing: 0.18em;
  color: var(--ink);
}

.footnote p {
  margin: 0;
  max-width: 390px;
  font-size: 10.5px;
  line-height: 1.75;
  letter-spacing: 0.015em;
  color: var(--muted);
}

/* ---------- Utilities ---------- */

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- Responsive ---------- */

@media (max-width: 560px) {
  .shell {
    width: min(100% - 28px, 520px);
    padding-top: 22px;
    padding-bottom: 24px;
  }

  .masthead {
    gap: 12px;
  }

  .eyebrow {
    margin-bottom: 10px;
    font-size: 8px;
    letter-spacing: 0.18em;
  }

  h1 {
    font-size: clamp(60px, 20vw, 82px);
  }

  .tagline {
    margin-top: 17px;
    font-size: 11.5px;
    line-height: 1.65;
  }

  .scores {
    gap: 6px;
    padding-top: 0;
  }

  .score-card {
    min-width: 70px;
    padding: 9px 10px 8px;
    border-radius: 11px;
  }

  .score-card strong {
    font-size: 20px;
  }

  .score-label {
    font-size: 8px;
  }

  .score-gain {
    top: 5px;
    right: 7px;
    font-size: 9px;
  }

  .controlbar {
    margin-top: 24px;
  }

  .btn {
    padding: 9px 16px;
    font-size: 12px;
  }

  .hint {
    font-size: 9px;
  }

  .hint-keys {
    display: none;
  }

  .game {
    margin-top: 14px;
  }

  .board-meta {
    font-size: 8px;
    margin-bottom: 7px;
  }

  .footnote {
    margin-top: 21px;
    gap: 12px;
  }

  .footnote p {
    font-size: 10px;
  }

  .side-note {
    display: none;
  }
}

@media (max-width: 390px) {
  .shell {
    width: calc(100% - 22px);
  }

  .tagline {
    max-width: 170px;
  }

  .score-card {
    min-width: 64px;
    padding: 8px 9px;
  }

  .score-card strong {
    font-size: 18px;
  }

  .overlay-card {
    padding: 23px 18px 20px;
  }

  .overlay-actions {
    flex-wrap: wrap;
  }
}

@media (max-width: 340px) {
  .hint {
    display: none;
  }

  .controlbar {
    justify-content: flex-start;
  }
}

@media (hover: none) and (pointer: coarse) {
  .hint-keys,
  .hint-desktop {
    display: none;
  }

  .hint-touch {
    display: inline;
  }
}

@media (max-height: 760px) and (min-width: 561px) {
  .shell {
    padding-top: 24px;
    padding-bottom: 22px;
  }

  .eyebrow {
    margin-bottom: 8px;
  }

  h1 {
    font-size: 72px;
  }

  .tagline {
    margin-top: 14px;
  }

  .controlbar {
    margin-top: 22px;
  }

  .game {
    margin-top: 13px;
  }

  .footnote {
    margin-top: 19px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-delay: 0ms !important;
    transition-duration: 0.01ms !important;
    transition-delay: 0ms !important;
  }
}

@media (hover: none) {
  .btn--primary:hover {
    background: var(--accent);
  }

  .btn--ghost:hover:not(:disabled) {
    border-color: var(--line);
    background: transparent;
  }
}
