:root {
  color-scheme: light;
  --bg: #faf8ef;
  --bg-deep: #f2ebdd;
  --ink: #4b4038;
  --ink-soft: #6f6255;
  --muted: #7d6f61;
  --line: #e4d9c9;
  --board: #bbada0;
  --cell: rgba(238, 228, 218, 0.35);
  --accent: #7c6653;
  --accent-dark: #6a5645;
  --warn-bg: #fdf0e0;
  --warn-line: #efcfa4;
  --radius: 16px;
  --pad: 12px;
  --gap: 12px;
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  font-family: "Avenir Next", "Segoe UI", system-ui, -apple-system, "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  background:
    radial-gradient(46rem 28rem at 12% -12%, #fff6e7 0%, rgba(255, 246, 231, 0) 70%),
    radial-gradient(40rem 26rem at 100% 0%, #f6ecdc 0%, rgba(246, 236, 220, 0) 65%),
    var(--bg);
  -webkit-font-smoothing: antialiased;
}

.page {
  width: min(1040px, 100%);
  margin: 0 auto;
  padding: clamp(1.25rem, 3vw, 2.75rem) clamp(1rem, 3vw, 2rem) 3rem;
}

/* ---------- 页头 ---------- */

.masthead {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: clamp(1rem, 3vw, 2.5rem);
  flex-wrap: wrap;
  padding-bottom: clamp(1.25rem, 2.5vw, 2rem);
  border-bottom: 1px solid var(--line);
}

.brand {
  min-width: 0;
}

.kicker {
  margin: 0 0 0.4rem;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.22em;
  color: var(--muted);
}

h1 {
  margin: 0;
  font-size: 4.1rem;
  font-weight: 800;
  line-height: 0.92;
  letter-spacing: -0.05em;
  color: #6f6156;
  font-variant-numeric: tabular-nums;
}

.tagline {
  margin: 0.75rem 0 0;
  max-width: 30rem;
  font-size: 0.95rem;
  line-height: 1.75;
  color: var(--ink-soft);
}

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

.hud {
  display: flex;
  align-items: stretch;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.score-card {
  position: relative;
  min-width: 5.75rem;
  padding: 0.55rem 0.85rem 0.6rem;
  border-radius: 10px;
  background: #6b5a4a;
  color: #fffaf2;
  text-align: center;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.16);
}

.score-card--best {
  background: #574839;
}

.score-label {
  display: block;
  font-size: 0.7rem;
  letter-spacing: 0.12em;
  color: #f2e9dd;
}

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

.score-gain {
  position: absolute;
  top: -0.85rem;
  left: 50%;
  transform: translate(-50%, 0.4rem);
  font-size: 0.9rem;
  font-weight: 700;
  color: #a05a1e;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease, transform 0.22s ease;
}

.score-gain.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ---------- 按钮 ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  padding: 0.68rem 1.05rem;
  border: 1px solid transparent;
  border-radius: 10px;
  font: inherit;
  font-size: 0.92rem;
  font-weight: 600;
  line-height: 1.2;
  color: inherit;
  cursor: pointer;
  transition: background-color 0.16s ease, border-color 0.16s ease,
    transform 0.12s ease, box-shadow 0.16s ease;
}

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

.btn:focus-visible,
.pad-btn:focus-visible,
.board:focus-visible {
  outline: 3px solid #d99a5b;
  outline-offset: 3px;
}

.btn-primary {
  background: var(--accent);
  color: #fffdf9;
  box-shadow: 0 2px 0 rgba(107, 88, 69, 0.35);
}

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

.btn-ghost {
  background: rgba(255, 253, 249, 0.7);
  border-color: #d8c9b6;
  color: var(--ink);
}

.btn-ghost:hover {
  background: #fffdf9;
  border-color: var(--accent);
}

.hud-actions {
  display: flex;
  align-items: stretch;
  gap: 0.6rem;
}

.hud-actions .btn {
  white-space: nowrap;
}

.btn-undo {
  padding-inline: 0.9rem;
}

.btn-ghost:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  background: rgba(255, 253, 249, 0.4);
  border-color: #e4d9c9;
  color: #8a7b6c;
  transform: none;
}

.btn-restart {
  align-self: stretch;
  min-width: 9.5rem;
}

.btn-restart.is-confirm {
  background: #9a5218;
  box-shadow: 0 0 0 3px rgba(154, 82, 24, 0.18);
}

/* ---------- 主布局 ---------- */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 18.5rem;
  gap: clamp(1.5rem, 4vw, 3rem);
  align-items: start;
  padding-top: clamp(1.5rem, 3vw, 2.25rem);
}

.play {
  min-width: 0;
  max-width: 32rem;
}

/* ---------- 棋盘 ---------- */

.board {
  position: relative;
  aspect-ratio: 1 / 1;
  padding: var(--pad);
  border-radius: var(--radius);
  background: var(--board);
  box-shadow: 0 14px 30px -18px rgba(91, 74, 58, 0.55),
    inset 0 1px 0 rgba(255, 255, 255, 0.18);
  container-type: inline-size;
  touch-action: manipulation;
}

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

.cell {
  border-radius: 9px;
  background: var(--cell);
}

.tiles {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.tile {
  position: absolute;
  top: var(--pad);
  left: var(--pad);
  width: calc((100% - var(--pad) * 2 - var(--gap) * 3) / 4);
  height: calc((100% - var(--pad) * 2 - var(--gap) * 3) / 4);
  transform: translate(
    calc(var(--col) * (100% + var(--gap))),
    calc(var(--row) * (100% + var(--gap)))
  );
  transition: transform 110ms ease-in-out;
  will-change: transform;
  z-index: 2;
}

.tile-face {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  border-radius: 9px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  font-size: clamp(1.4rem, 9.2cqw, 2.9rem);
  letter-spacing: -0.02em;
}

.tile[data-digits="3"] .tile-face {
  font-size: clamp(1.15rem, 7.4cqw, 2.35rem);
}

.tile[data-digits="4"] .tile-face {
  font-size: clamp(0.95rem, 6.1cqw, 1.95rem);
}

.tile[data-digits="5"] .tile-face {
  font-size: clamp(0.8rem, 5.1cqw, 1.6rem);
}

.tile[data-value="2"] .tile-face {
  background: #eee4da;
  color: #746a60;
}

.tile[data-value="4"] .tile-face {
  background: #ede0c8;
  color: #746a60;
}

.tile[data-value="8"] .tile-face {
  background: #f2b179;
  color: #4a321c;
}

.tile[data-value="16"] .tile-face {
  background: #f59563;
  color: #4a321c;
}

.tile[data-value="32"] .tile-face {
  background: #f67c5f;
  color: #fffdf9;
}

.tile[data-value="64"] .tile-face {
  background: #f65e3b;
  color: #fffdf9;
}

.tile[data-value="128"] .tile-face {
  background: #edcf72;
  color: #5b4526;
}

.tile[data-value="256"] .tile-face {
  background: #edcc61;
  color: #5b4526;
}

.tile[data-value="512"] .tile-face {
  background: #edc850;
  color: #5b4526;
}

.tile[data-value="1024"] .tile-face {
  background: #edc53f;
  color: #5b4526;
}

.tile[data-value="2048"] .tile-face {
  background: #edc22e;
  color: #5b4526;
  box-shadow: 0 0 0 2px rgba(255, 246, 219, 0.7),
    0 0 22px rgba(237, 194, 46, 0.7);
}

.tile[data-value="super"] .tile-face {
  background: #4a4038;
  color: #fdf7ec;
}

.tile.is-new .tile-face {
  animation: face-in 150ms ease-out;
}

.tile.is-merged .tile-face {
  animation: face-pop 190ms ease-out;
}

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

@keyframes face-pop {
  0% {
    transform: scale(0.88);
  }
  55% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
  }
}

/* ---------- 结算浮层 ---------- */

.board-overlay {
  position: absolute;
  inset: 0;
  z-index: 6;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  padding: 1.5rem;
  border-radius: var(--radius);
  text-align: center;
  background: rgba(250, 248, 239, 0.86);
  backdrop-filter: blur(5px);
  animation: overlay-in 180ms ease-out;
}

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

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

.overlay-kicker {
  margin: 0;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.2em;
  color: var(--muted);
}

.overlay-title {
  margin: 0;
  font-size: clamp(1.5rem, 5.4vw, 2.1rem);
  font-weight: 800;
  line-height: 1.25;
  letter-spacing: -0.01em;
  color: var(--ink);
}

.overlay-text {
  margin: 0;
  max-width: 24ch;
  font-size: 0.92rem;
  line-height: 1.7;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}

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

/* ---------- 提示与方向键 ---------- */

.hint {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  margin: 1rem 0 1.15rem;
  font-size: 0.86rem;
  line-height: 1.6;
  color: var(--ink-soft);
}

.hint svg {
  flex: none;
  margin-top: 0.12rem;
  color: var(--muted);
}

.pad {
  display: grid;
  grid-template-columns: repeat(3, 3rem);
  grid-template-rows: repeat(3, 3rem);
  gap: 0.5rem;
  justify-content: start;
}

.pad-btn {
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fffdf9;
  color: var(--ink);
  cursor: pointer;
  box-shadow: 0 2px 0 rgba(187, 173, 160, 0.4);
  transition: background-color 0.14s ease, border-color 0.14s ease,
    transform 0.1s ease;
}

.pad-btn:hover {
  background: #fff8ec;
  border-color: #d3bfa7;
}

.pad-btn:active {
  transform: translateY(1px);
  box-shadow: none;
}

.pad-up {
  grid-area: 1 / 2;
}

.pad-left {
  grid-area: 2 / 1;
}

.pad-center {
  grid-area: 2 / 2;
  display: grid;
  place-items: center;
  border: 1px dashed rgba(143, 122, 102, 0.35);
  border-radius: 12px;
  font-size: 0.72rem;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.pad-down {
  grid-area: 3 / 2;
}

.pad-right {
  grid-area: 2 / 3;
}

/* ---------- 侧栏 ---------- */

.side {
  display: grid;
  gap: 1.35rem;
  padding: 1.35rem 1.25rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 253, 248, 0.72);
}

.panel h2 {
  margin: 0 0 0.7rem;
  font-size: 0.86rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  color: #8a7a6b;
}

.rules {
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: rule;
  display: grid;
  gap: 0.7rem;
}

.rules li {
  counter-increment: rule;
  position: relative;
  padding-left: 1.9rem;
  font-size: 0.88rem;
  line-height: 1.65;
  color: var(--ink-soft);
}

.rules li::before {
  content: counter(rule);
  position: absolute;
  top: 0.1rem;
  left: 0;
  display: grid;
  place-items: center;
  width: 1.35rem;
  height: 1.35rem;
  border-radius: 50%;
  background: var(--bg-deep);
  color: #8a7663;
  font-size: 0.72rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.6rem;
  margin: 0;
}

.stats div {
  padding: 0.55rem 0.35rem;
  border-radius: 10px;
  background: var(--bg-deep);
  text-align: center;
}

.stats dt {
  font-size: 0.7rem;
  color: var(--muted);
}

.stats dd {
  margin: 0.2rem 0 0;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.note {
  margin: 0;
  padding: 0.65rem 0.8rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #f5efe3;
  font-size: 0.82rem;
  line-height: 1.6;
  color: var(--ink-soft);
}

.note--warn {
  border-color: var(--warn-line);
  background: var(--warn-bg);
  color: #8a5a25;
}

.note[hidden] {
  display: none;
}

.side-foot {
  margin: 0;
  border-top: 1px solid var(--line);
  padding-top: 0.9rem;
  font-size: 0.78rem;
  line-height: 1.65;
  color: var(--muted);
}

/* ---------- 响应式 ---------- */

@media (max-width: 900px) {
  h1 {
    font-size: 3.6rem;
  }

  .layout {
    grid-template-columns: minmax(0, 1fr);
    gap: 1.5rem;
  }

  .play {
    max-width: 34rem;
    width: 100%;
    justify-self: center;
  }

  .side {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 1.25rem 1.75rem;
    max-width: 34rem;
    width: 100%;
    justify-self: center;
  }

  .side-foot {
    grid-column: 1 / -1;
  }
}

@media (max-width: 600px) {
  :root {
    --pad: 9px;
    --gap: 9px;
  }

  h1 {
    font-size: 2.9rem;
  }

  .page {
    padding-top: 1rem;
    padding-bottom: 2rem;
  }

  .masthead {
    align-items: flex-start;
    gap: 1rem;
    padding-bottom: 1.1rem;
  }

  .tagline {
    margin-top: 0.5rem;
    font-size: 0.88rem;
    line-height: 1.6;
  }

  .kicker {
    margin-bottom: 0.3rem;
    font-size: 0.68rem;
  }

  .hud {
    width: 100%;
  }

  .score-card {
    flex: 1 1 6rem;
    min-width: 0;
  }

  .score-value {
    font-size: 1.2rem;
  }

  .hud-actions {
    flex: 1 1 100%;
  }

  .hud-actions .btn {
    flex: 1 1 0;
    min-width: 0;
    padding-inline: 0.55rem;
    font-size: 0.86rem;
  }

  .btn-restart {
    min-width: 0;
  }

  .side {
    grid-template-columns: minmax(0, 1fr);
    padding: 1.1rem 1rem;
  }

  .pad {
    grid-template-columns: repeat(3, 2.85rem);
    grid-template-rows: repeat(3, 2.85rem);
    justify-content: center;
  }

  .hint {
    font-size: 0.82rem;
  }
}

@media (max-width: 380px) {
  :root {
    --pad: 8px;
    --gap: 8px;
  }

  .tagline {
    font-size: 0.9rem;
  }

  .overlay-text {
    font-size: 0.86rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .tile {
    transition: none;
  }

  .tile.is-new .tile-face,
  .tile.is-merged .tile-face,
  .board-overlay {
    animation: none;
  }

  .score-gain {
    transition: none;
  }
}

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