:root {
  --page: #fff7ef;
  --ink: #614b43;
  --muted: #9a7f74;
  --peach: #ef9f86;
  --peach-dark: #dc7f68;
  --board: #cfae9e;
  --cell: rgba(255, 244, 235, 0.56);
  --gap: clamp(8px, 2.2vw, 14px);
  --radius: 22px;
  --shadow: 0 16px 44px rgba(113, 77, 61, 0.16);
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  display: grid;
  place-items: center;
  padding: 24px 16px 36px;
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 10%, rgba(255, 207, 187, .55), transparent 28%),
    radial-gradient(circle at 88% 90%, rgba(244, 210, 169, .42), transparent 30%),
    var(--page);
  font-family: ui-rounded, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  overscroll-behavior: none;
}

button { font: inherit; }

.game-shell { width: min(94vw, 560px); }

.game-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 18px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--peach-dark);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .16em;
}

h1 { margin: 0; font-size: clamp(36px, 10vw, 58px); line-height: .98; letter-spacing: -.05em; }
.subtitle { margin: 9px 0 0; color: var(--muted); font-size: 14px; }

.scores { display: flex; gap: 8px; flex-shrink: 0; }
.score-card {
  min-width: 72px;
  padding: 9px 10px 8px;
  border: 1px solid rgba(126, 89, 73, .11);
  border-radius: 15px;
  text-align: center;
  background: rgba(255,255,255,.7);
  box-shadow: 0 8px 22px rgba(113,77,61,.1);
}
.score-card span { display: block; color: var(--muted); font-size: 11px; font-weight: 700; }
.score-card strong { display: block; margin-top: 2px; font-size: 20px; font-variant-numeric: tabular-nums; }
.score-card.best { background: #fff0e8; }

.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-bottom: 12px; }
.toolbar p, .tip { margin: 0; color: var(--muted); font-size: 13px; }

.button {
  border: 0;
  border-radius: 13px;
  padding: 10px 16px;
  color: #fff;
  font-weight: 800;
  cursor: pointer;
  background: linear-gradient(135deg, var(--peach), var(--peach-dark));
  box-shadow: 0 7px 16px rgba(220,127,104,.26);
  transition: transform .16s ease, filter .16s ease;
}
.button:hover { filter: brightness(1.04); transform: translateY(-1px); }
.button:active { transform: translateY(1px) scale(.98); }
.button.secondary { color: var(--ink); background: #f4e7df; box-shadow: none; }

.game-board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  padding: var(--gap);
  overflow: hidden;
  border: 1px solid rgba(105,72,59,.1);
  border-radius: var(--radius);
  outline: none;
  background: var(--board);
  box-shadow: var(--shadow);
  touch-action: none;
  user-select: none;
}
.game-board:focus-visible { box-shadow: var(--shadow), 0 0 0 4px rgba(239,159,134,.3); }
.grid-background { display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--gap); width: 100%; height: 100%; }
.grid-cell { border-radius: 15px; background: var(--cell); box-shadow: inset 0 1px 1px rgba(255,255,255,.28); }
.tile-layer, .score-layer { position: absolute; inset: var(--gap); pointer-events: none; }

.tile {
  position: absolute;
  width: calc((100% - 3 * var(--gap)) / 4);
  height: calc((100% - 3 * var(--gap)) / 4);
  transform: translate(var(--x), var(--y));
  transition: transform 145ms cubic-bezier(.2,.76,.3,1);
  will-change: transform;
}
.tile-inner {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  border: 3px solid rgba(255,255,255,.7);
  border-radius: 15px;
  background: #f8e9df;
  box-shadow: 0 5px 12px rgba(82,55,44,.18);
}
.tile img { width: 100%; height: 100%; display: block; object-fit: cover; object-position: center; }
.tile-number {
  position: absolute;
  inset: 0;
  display: none;
  place-items: center;
  color: #765448;
  font-size: clamp(24px, 8vw, 46px);
  font-weight: 900;
  background: linear-gradient(145deg, #fff7ee, #f5c9b8);
}
.tile-inner.image-failed .tile-number { display: grid; }
.tile.new .tile-inner { animation: appear 190ms cubic-bezier(.2,1.35,.5,1) both; }
.tile.merged .tile-inner { animation: merge 250ms cubic-bezier(.2,1.4,.45,1) both; z-index: 2; }

.score-pop {
  position: absolute;
  width: calc((100% - 3 * var(--gap)) / 4);
  height: calc((100% - 3 * var(--gap)) / 4);
  display: grid;
  place-items: center;
  transform: translate(var(--x), var(--y));
  color: #fff;
  font-size: clamp(20px, 7vw, 34px);
  font-weight: 900;
  text-shadow: 0 2px 8px rgba(84,45,33,.65);
  animation: score-pop 700ms ease-out forwards;
  z-index: 5;
}

.tip { padding: 12px 5px 0; text-align: center; line-height: 1.55; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(76,54,47,.42);
  backdrop-filter: blur(7px);
}
.modal[hidden] { display: none; }
.modal-card {
  width: min(90vw, 390px);
  padding: 30px;
  border: 1px solid rgba(255,255,255,.8);
  border-radius: 26px;
  text-align: center;
  background: #fffaf6;
  box-shadow: 0 24px 70px rgba(63,39,30,.28);
  animation: modal-in 230ms ease-out both;
}
.modal-icon { font-size: 48px; }
.modal h2 { margin: 8px 0; font-size: 30px; }
.modal p { margin: 0 0 22px; color: var(--muted); line-height: 1.6; }
.modal-actions { display: flex; justify-content: center; gap: 10px; }

@keyframes appear { from { opacity: 0; transform: scale(.35); } to { opacity: 1; transform: scale(1); } }
@keyframes merge { 0% { transform: scale(1); } 50% { transform: scale(1.16); } 100% { transform: scale(1); } }
@keyframes score-pop { 0% { opacity: 0; transform: translate(var(--x), var(--y)) translateY(8px) scale(.75); } 25% { opacity: 1; } 100% { opacity: 0; transform: translate(var(--x), var(--y)) translateY(-42px) scale(1.08); } }
@keyframes modal-in { from { opacity: 0; transform: translateY(12px) scale(.96); } to { opacity: 1; transform: none; } }

@media (max-width: 520px) {
  body { align-items: start; padding-top: 20px; }
  .game-header { align-items: flex-start; }
  .subtitle { max-width: 210px; }
  .scores { flex-direction: column; }
  .score-card { min-width: 78px; padding: 6px 9px; }
  .score-card span, .score-card strong { display: inline; }
  .score-card strong { margin-left: 5px; font-size: 17px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
