:root {
  color-scheme: light;
  --ink: #392719;
  --muted-ink: #766f66;
  --paper: #f8f1df;
  --paper-light: #fffaf0;
  --brown: #bf8867;
  --board: #bdb5a6;
  --cell: #cbc5b8;
  --gap: clamp(8px, 2.1vw, 13px);
  --button-size: clamp(55px, 15vw, 78px);
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  overflow-x: hidden;
  color: var(--ink);
  font-family: "Arial Rounded MT Bold", "Microsoft YaHei", "PingFang SC", Arial, sans-serif;
  background:
    linear-gradient(rgba(154, 126, 85, 0.045) 1px, transparent 1px),
    linear-gradient(90deg, rgba(154, 126, 85, 0.045) 1px, transparent 1px),
    linear-gradient(145deg, transparent 0 38%, rgba(221, 202, 165, 0.3) 38% 56%, transparent 56%),
    radial-gradient(circle at 15% 12%, rgba(236, 215, 171, 0.55), transparent 32%),
    var(--paper);
  background-size: 9px 9px, 9px 9px, 100% 100%, 100% 100%, auto;
}

button {
  font: inherit;
}

button:focus-visible,
.board:focus-visible {
  outline: 3px solid #7a4bb4;
  outline-offset: 3px;
}

.game-shell {
  position: relative;
  width: min(100%, 680px);
  min-height: 100vh;
  min-height: 100svh;
  margin: 0 auto;
  padding: max(22px, env(safe-area-inset-top)) 14px max(20px, env(safe-area-inset-bottom));
}

.top-area {
  min-height: clamp(280px, 42vw, 350px);
}

.brand-score-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: clamp(10px, 2.8vw, 18px);
  min-width: 0;
}

.brand-mark {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  flex: 0 0 auto;
  width: clamp(70px, 19vw, 100px);
  aspect-ratio: 1;
  overflow: hidden;
  border-radius: 17px;
  box-shadow: 0 5px 0 rgba(112, 92, 67, 0.2);
}

.brand-mark span {
  display: grid;
  place-items: center;
  color: #725234;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(27px, 7.2vw, 40px);
  font-weight: 800;
  line-height: 1;
  text-shadow: 0 1px 0 #fff8dc;
  background: #d8d0b7;
}

.brand-mark span:nth-child(1),
.brand-mark span:nth-child(4) {
  color: #fff7df;
  background: #e88b20;
  text-shadow: 0 2px 0 #9f5717;
}

.brand h1 {
  margin: 0;
  white-space: nowrap;
  font-family: "Microsoft YaHei", "PingFang SC", sans-serif;
  font-size: clamp(26px, 7vw, 46px);
  font-weight: 900;
  letter-spacing: -0.06em;
}

.score-group {
  display: flex;
  gap: clamp(8px, 2.2vw, 16px);
}

.score-card {
  display: grid;
  grid-template-rows: auto auto;
  gap: 5px;
  min-width: clamp(100px, 24vw, 144px);
  padding: 8px 11px 10px;
  color: white;
  text-align: center;
  background: var(--brown);
  border-radius: 15px;
}

.score-label {
  font-size: clamp(16px, 4.4vw, 24px);
  font-weight: 700;
  line-height: 1.05;
}

.score-value {
  display: grid;
  place-items: center;
  min-height: 38px;
  padding: 2px 8px;
  overflow: hidden;
  color: #98664e;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(22px, 5.8vw, 34px);
  font-weight: 500;
  line-height: 1;
  background: var(--paper-light);
  border-radius: 14px;
}

.toolbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-top: 26px;
}

.tool-actions {
  display: flex;
  gap: clamp(10px, 2.5vw, 17px);
}

.square-button {
  position: relative;
  display: grid;
  place-items: center;
  width: var(--button-size);
  height: var(--button-size);
  padding: 0;
  border: 4px solid #fffdf3;
  border-radius: 14px;
  box-shadow: 0 7px 0 rgba(94, 91, 84, 0.52);
  cursor: pointer;
  transition: transform 110ms ease, filter 110ms ease;
}

.square-button:active:not(:disabled) {
  transform: translateY(4px);
  box-shadow: 0 3px 0 rgba(94, 91, 84, 0.52);
}

.square-button:disabled {
  cursor: default;
  filter: grayscale(0.62) saturate(0.45);
  opacity: 0.82;
}

.back-button {
  margin-left: 2px;
  background: #c38c6c;
  border-color: transparent;
}

.undo-button {
  background: #df885e;
}

.magic-button {
  background: #bd861d;
}

.hammer-button {
  background: #d95830;
}

.icon-back,
.icon-undo {
  display: block;
  width: 42%;
  height: 42%;
  border-left: 8px solid white;
  border-bottom: 8px solid white;
  transform: rotate(45deg) translate(5%, -5%);
  border-radius: 3px;
}

.icon-undo {
  position: relative;
  width: 46%;
  height: 38%;
  border: 7px solid white;
  border-right-color: transparent;
  border-bottom-color: transparent;
  border-radius: 50%;
  transform: rotate(-22deg) translate(3px, 3px);
}

.icon-undo::after {
  content: "";
  position: absolute;
  left: -11px;
  top: -9px;
  width: 0;
  height: 0;
  border-top: 10px solid transparent;
  border-right: 15px solid white;
  border-bottom: 10px solid transparent;
  transform: rotate(18deg);
}

.icon-magic {
  position: relative;
  width: 45%;
  height: 11%;
  background: white;
  border-radius: 3px;
  transform: rotate(-45deg);
  box-shadow: 7px 0 0 rgba(255, 255, 255, 0.72);
}

.icon-magic::before,
.icon-magic::after {
  content: "";
  position: absolute;
  width: 8px;
  height: 8px;
  background: white;
  transform: rotate(45deg);
}

.icon-magic::before {
  left: -9px;
  top: -16px;
}

.icon-magic::after {
  right: 3px;
  bottom: -18px;
}

.icon-hammer {
  position: relative;
  width: 48%;
  height: 16%;
  background: white;
  border-radius: 4px;
  transform: rotate(-42deg);
}

.icon-hammer::before {
  content: "";
  position: absolute;
  left: -6px;
  top: -9px;
  width: 16px;
  height: 30px;
  background: white;
  border-radius: 4px;
}

.icon-hammer i {
  position: absolute;
  right: -20px;
  top: 3px;
  width: 25px;
  height: 7px;
  background: white;
  border-radius: 0 5px 5px 0;
}

.side-entry {
  position: absolute;
  z-index: 8;
  left: 0;
  top: clamp(265px, 44vw, 360px);
  width: clamp(52px, 13vw, 70px);
  height: clamp(78px, 20vw, 108px);
  padding: 0;
  border: 0;
  border-radius: 0 21px 21px 0;
  background: #a75ae9;
  box-shadow: 0 8px 0 rgba(95, 82, 93, 0.45), inset -3px 0 0 rgba(255, 255, 255, 0.36);
  cursor: pointer;
}

.side-dot {
  position: absolute;
  top: -14px;
  right: -8px;
  width: 31px;
  height: 31px;
  border: 3px solid white;
  border-radius: 50%;
  background: #d75d52;
}

.side-chevrons {
  display: flex;
  justify-content: center;
  gap: 1px;
  transform: translateX(-3px);
}

.side-chevrons i {
  width: 25px;
  height: 25px;
  border-top: 5px solid white;
  border-right: 5px solid white;
  transform: rotate(45deg);
}

.side-chevrons i + i {
  margin-left: -15px;
}

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  padding: var(--gap);
  overflow: hidden;
  touch-action: none;
  user-select: none;
  background: var(--board);
  box-shadow: 0 3px 0 rgba(101, 91, 73, 0.08);
}

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

.cell {
  border-radius: clamp(8px, 2vw, 13px);
  background: var(--cell);
}

.tile {
  --move-x: 0px;
  --move-y: 0px;
  display: grid;
  place-items: center;
  color: #746e65;
  font-size: clamp(36px, 10.4vw, 68px);
  font-weight: 900;
  line-height: 1;
  border-radius: clamp(8px, 2vw, 13px);
  background: #ece8df;
  will-change: transform;
}

.tile-2 {
  background: #ede9e1;
}

.tile-4 {
  background: #e9dfc2;
}

.tile-8 {
  color: #fffdf5;
  background: #e2b57a;
}

.tile-16 {
  color: #fffdf5;
  background: #e49a60;
}

.tile-32 {
  color: #fffdf5;
  background: #dc7b59;
}

.tile-64 {
  color: #fffdf5;
  background: #dc6036;
}

.tile-128 {
  color: #fffdf5;
  background: #e8d47a;
}

.tile-256 {
  color: #fffdf5;
  background: #e6da57;
}

.tile-512 {
  color: #fffdf5;
  background: #c8c64e;
}

.tile-1024 {
  color: #fffdf5;
  background: #b0bd49;
}

.tile-2048 {
  color: #fffdf5;
  background: #96ad43;
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.2), 0 0 18px rgba(224, 194, 73, 0.58);
}

.tile-super {
  color: #fffdf5;
  background: #736f3d;
}

.tile-small-number {
  font-size: clamp(27px, 7.8vw, 51px);
  letter-spacing: -0.06em;
}

.tile-tiny-number {
  font-size: clamp(23px, 6.5vw, 43px);
}

.tile-moving {
  z-index: 5;
  transform: translate(var(--move-x), var(--move-y));
  transition: transform 135ms cubic-bezier(0.24, 0.78, 0.35, 1);
}

.tile-born {
  animation: tile-born 170ms cubic-bezier(0.22, 0.9, 0.36, 1.2) both;
}

.tile-merged {
  z-index: 6;
  animation: tile-merged 190ms cubic-bezier(0.22, 0.9, 0.36, 1.25) both;
}

.score-bump {
  animation: score-bump 180ms ease-out;
}

.game-over[hidden],
.toast[hidden] {
  display: none;
}

.game-over {
  position: absolute;
  z-index: 20;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(246, 236, 213, 0.78);
  backdrop-filter: blur(2px);
  animation: overlay-in 180ms ease-out both;
}

.game-over-card {
  width: min(86%, 360px);
  padding: 26px;
  text-align: center;
  background: #fff8e9;
  border: 4px solid #c18a68;
  border-radius: 24px;
  box-shadow: 0 12px 0 rgba(92, 73, 52, 0.17);
  animation: dialog-in 230ms cubic-bezier(0.22, 0.9, 0.36, 1.12) both;
}

.game-over-card h2 {
  margin: 3px 0 10px;
  font-size: clamp(30px, 8vw, 44px);
}

.game-over-kicker {
  margin: 0;
  color: #b76f49;
  font-weight: 800;
}

.game-over-card p {
  font-size: 19px;
}

.game-over-card button {
  padding: 11px 25px;
  color: white;
  font-size: 18px;
  font-weight: 800;
  border: 0;
  border-radius: 13px;
  background: #d87549;
  box-shadow: 0 5px 0 #9f4e31;
  cursor: pointer;
}

.game-tip {
  margin: 13px 0 0;
  color: #8b7962;
  text-align: center;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.08em;
}

.toast {
  position: fixed;
  z-index: 30;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  max-width: min(88vw, 420px);
  padding: 10px 17px;
  color: white;
  text-align: center;
  background: rgba(65, 46, 32, 0.9);
  border-radius: 999px;
  transform: translateX(-50%);
}

.toast-show {
  animation: toast-in 190ms ease-out both;
}

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

@keyframes tile-merged {
  0% {
    transform: scale(0.86);
  }
  58% {
    transform: scale(1.11);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes score-bump {
  50% {
    color: #c86539;
    transform: scale(1.08);
  }
}

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

@keyframes dialog-in {
  from {
    opacity: 0;
    transform: translateY(14px) scale(0.94);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translate(-50%, 8px);
  }
  to {
    opacity: 1;
    transform: translate(-50%, 0);
  }
}

.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;
}

@media (max-width: 520px) {
  .game-shell {
    padding-right: 8px;
    padding-left: 8px;
  }

  .top-area {
    min-height: 282px;
  }

  .brand-score-row {
    gap: 7px;
  }

  .score-card {
    min-width: 86px;
    padding-right: 7px;
    padding-left: 7px;
  }

  .toolbar {
    margin-top: 25px;
  }
}

@media (max-width: 390px) {
  .brand h1 {
    font-size: 25px;
  }

  .brand-mark {
    width: 64px;
  }

  .score-card {
    min-width: 76px;
  }

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

  .score-value {
    min-height: 31px;
    font-size: 20px;
  }
}

@media (max-width: 340px) {
  .brand-score-row {
    gap: 4px;
  }

  .brand {
    gap: 6px;
  }

  .brand-mark {
    width: 56px;
  }

  .brand h1 {
    font-size: 22px;
  }

  .score-group {
    gap: 5px;
  }

  .score-card {
    min-width: 65px;
    padding: 6px 5px 8px;
  }

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

  .score-value {
    min-height: 27px;
    padding-right: 4px;
    padding-left: 4px;
    font-size: 18px;
  }

  .tool-actions {
    gap: 8px;
  }
}

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