:root {
  --page: #1b1c21;
  --surface: #24252c;
  --surface-raised: #2c2d35;
  --ink: #f1f1f4;
  --muted: rgba(241, 241, 244, 0.6);
  --blue: #4da3ff;
  --blue-soft: #8cc4ff;
  --amber: #f2b45c;
  --error: #ff6b6b;
  --thin-line: rgba(255, 255, 255, 0.07);
  --box-line: rgba(255, 255, 255, 0.3);
  --header-height: 58px;
  --footer-height: 24px;
  --viewport-height: 100vh;
}

@supports (height: 100svh) {
  :root {
    --viewport-height: 100svh;
  }
}

* {
  box-sizing: border-box;
}

html {
  overflow-x: hidden;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  margin: 0;
  background: radial-gradient(ellipse at 50% 20%, #23252e, var(--page) 70%) no-repeat, var(--page);
  background-size: 100% var(--viewport-height), auto;
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: var(--header-height);
  padding: 10px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  background: rgba(0, 0, 0, 0.18);
  user-select: none;
}

.header-start {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.home-link {
  position: relative;
  display: flex;
  align-items: center;
  padding-right: 14px;
  border-right: 1px solid rgba(255, 255, 255, 0.15);
  transition: opacity 0.15s;
}

.home-link:hover {
  opacity: 0.8;
}

.home-link-mark {
  display: block;
  width: 30px;
  height: 30px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: linear-gradient(135deg, #0fa3a9, #6a55ef 60%, #e0428a);
  color: #fff;
}

h1 {
  margin: 0;
  font-size: 19px;
  font-weight: 750;
  white-space: nowrap;
}

.stats {
  display: flex;
  gap: 6px;
  margin: 0;
}

.stat {
  min-width: 70px;
  padding: 5px 10px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.05);
  text-align: center;
}

.stat-label {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  opacity: 0.6;
}

.stat-value {
  margin: 0;
  font-size: 16px;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
}

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

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

.mode-picker {
  display: flex;
  gap: 2px;
  padding: 3px;
  border-radius: 11px;
  background: rgba(255, 255, 255, 0.05);
}

.mode-option {
  position: relative;
  cursor: pointer;
}

.mode-option input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.mode-option span {
  display: block;
  padding: 6px 11px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 650;
  opacity: 0.7;
  transition: background 0.15s, color 0.15s, opacity 0.15s;
}

.mode-option:hover span {
  opacity: 1;
}

.mode-option input:checked + span {
  background: var(--ink);
  color: #1d1d27;
  opacity: 1;
}

.mode-option input:focus-visible + span {
  outline: 2px solid var(--blue);
  outline-offset: 1px;
}

.new-game-button {
  display: flex;
  align-items: center;
  gap: 7px;
  height: 38px;
  padding: 0 14px;
  border: 0;
  border-radius: 10px;
  background: var(--blue);
  color: #0c1726;
  font-size: 14px;
  font-weight: 700;
  transition: background 0.15s, transform 0.1s;
}

.new-game-button:hover {
  background: #6cb4ff;
}

.new-game-button:active {
  transform: scale(0.96);
}

kbd {
  padding: 1px 5px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.12);
  font-family: inherit;
  font-size: 10px;
  font-weight: 600;
}

.ghost-button {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  opacity: 0.85;
}

.ghost-button:hover {
  background: rgba(255, 255, 255, 0.1);
  opacity: 1;
}

#table {
  position: relative;
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  min-height: calc(var(--viewport-height) - var(--header-height) - var(--footer-height));
  padding: 20px 16px;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.play-area {
  --board-size: min(540px, calc(var(--viewport-height) - var(--header-height) - var(--footer-height) - 130px), calc(100vw - 32px));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.board {
  --gap: calc(var(--board-size) * 0.03);
  --cell: calc((var(--board-size) - var(--gap) * (var(--size) + 1)) / var(--size));
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  border-radius: calc(var(--gap) * 2.4);
  background: linear-gradient(180deg, #17181d, #121317);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.06), 0 24px 60px rgba(0, 0, 0, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.04);
  touch-action: none;
  overscroll-behavior: contain;
  cursor: grab;
}

.board[data-size="5"] {
  --gap: calc(var(--board-size) * 0.024);
}

.board[data-size="3"] {
  --gap: calc(var(--board-size) * 0.036);
}

.board[data-nudge="up"] {
  animation: nudge-up 0.16s ease-out;
}

.board[data-nudge="down"] {
  animation: nudge-down 0.16s ease-out;
}

.board[data-nudge="left"] {
  animation: nudge-left 0.16s ease-out;
}

.board[data-nudge="right"] {
  animation: nudge-right 0.16s ease-out;
}

.cell-layer,
.tile-layer {
  position: absolute;
  inset: var(--gap);
}

.cell-layer {
  display: grid;
  grid-template-columns: repeat(var(--size), 1fr);
  gap: var(--gap);
}

.cell {
  border-radius: 24%;
  background: rgba(255, 255, 255, 0.035);
  box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.45);
}

.tile {
  --tile-color: #263a4f;
  --tile-ink: #b9dcf2;
  --tile-glow: transparent;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  width: var(--cell);
  height: var(--cell);
  transform: translate(calc(var(--column) * (var(--cell) + var(--gap))), calc(var(--row) * (var(--cell) + var(--gap))));
  transition: transform var(--slide-ms) cubic-bezier(0.35, 0, 0.25, 1);
}

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

.tile.is-consumed {
  z-index: 0;
}

.tile-face {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  overflow: hidden;
  border-radius: 24%;
  background: linear-gradient(160deg, color-mix(in srgb, var(--tile-color) 72%, #fff) 0%, var(--tile-color) 48%, color-mix(in srgb, var(--tile-color) 82%, #000) 100%);
  box-shadow: inset 0 calc(var(--cell) * -0.05) 0 rgba(0, 0, 0, 0.22), inset 0 1px 0 rgba(255, 255, 255, 0.25), 0 0 calc(var(--cell) * 0.35) var(--tile-glow);
  color: var(--tile-ink);
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: calc(var(--cell) * 0.42);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  line-height: 1;
}

.tile-face::before {
  content: "";
  position: absolute;
  top: 9%;
  left: 9%;
  width: 34%;
  height: 12%;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.22);
  transform: rotate(-12deg);
}

.tile-value {
  position: relative;
  padding-bottom: 4%;
}

.tile[data-digits="3"] .tile-face {
  font-size: calc(var(--cell) * 0.34);
}

.tile[data-digits="4"] .tile-face {
  font-size: calc(var(--cell) * 0.27);
}

.tile[data-digits="5"] .tile-face,
.tile[data-digits="6"] .tile-face {
  font-size: calc(var(--cell) * 0.21);
}

.tile[data-tier="2"] {
  --tile-color: #1d5560;
  --tile-ink: #cdf5f0;
}

.tile[data-tier="3"] {
  --tile-color: #13807e;
  --tile-ink: #f2fffd;
}

.tile[data-tier="4"] {
  --tile-color: #0fa3a9;
  --tile-ink: #f2fffd;
}

.tile[data-tier="5"] {
  --tile-color: #1f8fd6;
  --tile-ink: #f4faff;
}

.tile[data-tier="6"] {
  --tile-color: #3d6ff0;
  --tile-ink: #f4f6ff;
}

.tile[data-tier="7"] {
  --tile-color: #6a55ef;
  --tile-ink: #f6f3ff;
  --tile-glow: rgba(106, 85, 239, 0.35);
}

.tile[data-tier="8"] {
  --tile-color: #9146e0;
  --tile-ink: #faf3ff;
  --tile-glow: rgba(145, 70, 224, 0.4);
}

.tile[data-tier="9"] {
  --tile-color: #bb3cc8;
  --tile-ink: #fff3fd;
  --tile-glow: rgba(187, 60, 200, 0.45);
}

.tile[data-tier="10"] {
  --tile-color: #e0428a;
  --tile-ink: #fff4f8;
  --tile-glow: rgba(224, 66, 138, 0.5);
}

.tile[data-tier="11"] {
  --tile-color: #ffb02e;
  --tile-ink: #2e1700;
  --tile-glow: rgba(255, 176, 46, 0.6);
}

.tile[data-tier="12"] {
  --tile-color: #ffe07a;
  --tile-ink: #2e2200;
  --tile-glow: rgba(255, 224, 122, 0.6);
}

.tile[data-tier="13"] {
  --tile-color: #eeeaff;
  --tile-ink: #2b1d52;
  --tile-glow: rgba(190, 170, 255, 0.7);
}

.tile.is-spawned .tile-face {
  animation: tile-spawn 0.18s ease-out var(--slide-ms) backwards;
}

.tile.is-merged .tile-face {
  animation: tile-merge 0.22s ease-out var(--slide-ms) backwards;
}

@keyframes tile-spawn {
  from {
    opacity: 0;
    transform: scale(0.2) rotate(-8deg);
  }
}

@keyframes tile-merge {
  0% {
    opacity: 0;
    transform: scale(0.7);
  }
  45% {
    opacity: 1;
    transform: scale(1.16);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes nudge-up {
  40% {
    transform: translateY(-5px);
  }
}

@keyframes nudge-down {
  40% {
    transform: translateY(5px);
  }
}

@keyframes nudge-left {
  40% {
    transform: translateX(-5px);
  }
}

@keyframes nudge-right {
  40% {
    transform: translateX(5px);
  }
}

.board-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: var(--board-size);
}

.status {
  margin: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.undo-button {
  display: flex;
  flex-shrink: 0;
  align-items: center;
  gap: 7px;
  height: 36px;
  padding: 0 8px 0 12px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  background: var(--surface);
  font-size: 14px;
  font-weight: 650;
  transition: background 0.15s, opacity 0.15s;
}

.undo-button:hover:not(:disabled) {
  background: var(--surface-raised);
}

.undo-button:disabled {
  cursor: default;
  opacity: 0.45;
}

.undo-count {
  display: grid;
  place-items: center;
  min-width: 22px;
  height: 22px;
  padding: 0 5px;
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.1);
  font-size: 12px;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
}

.rules-note {
  margin: 0;
  font-size: 12px;
  text-align: center;
  opacity: 0.5;
}

.stat {
  position: relative;
}

.score-bump {
  position: absolute;
  top: 50%;
  left: 50%;
  color: #5ee0d6;
  font-size: 15px;
  font-weight: 800;
  pointer-events: none;
  white-space: nowrap;
  animation: score-rise 0.75s ease-out forwards;
}

@keyframes score-rise {
  from {
    opacity: 1;
    transform: translate(-50%, -20%);
  }
  to {
    opacity: 0;
    transform: translate(-50%, 90%);
  }
}


.result-overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(10, 11, 14, 0.6);
  backdrop-filter: blur(3px);
  animation: fade-in 0.25s ease-out;
}

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

.result-card {
  width: min(360px, 100%);
  padding: 26px 24px 22px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 18px;
  background: var(--surface-raised);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
  text-align: center;
  animation: card-rise 0.35s cubic-bezier(0.2, 0.8, 0.25, 1);
}

.result-card h2 {
  margin: 0 0 16px;
  font-size: 22px;
}

.result-stats {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin: 0 0 14px;
}

.result-stat {
  min-width: 70px;
  padding: 7px 10px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.05);
}

.result-stat dd {
  margin: 2px 0 0;
  font-size: 17px;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
}

.result-note {
  margin: 0 0 14px;
  color: var(--amber);
  font-size: 14px;
  font-weight: 600;
}

.result-actions {
  display: grid;
  gap: 8px;
}

.action {
  height: 42px;
  border: 0;
  border-radius: 11px;
  background: rgba(255, 255, 255, 0.08);
  font-size: 15px;
  font-weight: 700;
}

.action:hover {
  background: rgba(255, 255, 255, 0.13);
}

.action.primary {
  background: var(--blue);
  color: #0c1726;
}

.action.primary:hover {
  background: #6cb4ff;
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
}

@keyframes card-rise {
  from {
    opacity: 0;
    transform: translateY(14px) scale(0.97);
  }
}

.site-footer {
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 5px;
  background: rgba(0, 0, 0, 0.18);
  font-size: 10px;
  opacity: 0.7;
}

.site-footer a {
  color: inherit;
}

@media (max-width: 1100px) {
  header {
    flex-wrap: wrap;
    row-gap: 8px;
  }

  .button-label {
    display: none;
  }

  .new-game-button {
    width: 38px;
    justify-content: center;
    padding: 0;
  }
}

@media (max-width: 860px) {
  :root {
    --header-height: 104px;
  }

  .play-area {
    --board-size: min(540px, calc(100vw - 24px), calc(var(--viewport-height) - var(--header-height) - var(--footer-height) - 110px));
  }

  .rules-note {
    display: none;
  }
}

@media (max-width: 640px) {
  header {
    padding: 8px 12px;
    gap: 8px;
  }

  .header-start {
    gap: 10px;
  }

  .home-link {
    padding-right: 10px;
  }

  .brand-mark {
    display: none;
  }

  h1 {
    font-size: 17px;
  }

  .stat {
    min-width: 64px;
    padding: 4px 8px;
  }

  .stat-value {
    font-size: 15px;
  }

  .controls {
    width: 100%;
    justify-content: space-between;
    gap: 4px;
  }

  .mode-option span {
    padding: 6px 9px;
    font-size: 13px;
  }

  #table {
    padding: 14px 10px 18px;
  }

  .play-area {
    --board-size: min(calc(100vw - 20px), calc(var(--viewport-height) - var(--header-height) - var(--footer-height) - 90px));
  }

  .site-footer {
    flex-wrap: wrap;
  }
}

@media (prefers-reduced-motion: reduce) {
  .tile,
  .tile .tile-face,
  .board,
  .score-bump,
  .result-overlay,
  .result-card {
    animation: none !important;
    transition: none !important;
  }
}


.guide {
  max-width: 760px;
  margin: 0 auto;
  padding: 48px 20px 64px;
  color: rgba(241, 241, 244, 0.84);
  font-size: 16px;
  line-height: 1.65;
  user-select: text;
}

.guide h2 {
  margin: 0 0 10px;
  color: var(--ink);
  font-size: 24px;
}

.guide h3 {
  margin: 22px 0 6px;
  color: var(--ink);
  font-size: 17px;
}

.guide p,
.guide li {
  margin: 0 0 10px;
}

.guide ul,
.guide ol {
  padding-left: 22px;
}

.guide kbd {
  padding: 1px 6px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 5px;
  background: transparent;
  font: inherit;
  font-size: 13px;
}

.guide h2:not(:first-child) {
  margin-top: 40px;
}

.guide a,
.more-games a {
  color: var(--blue-soft);
}

.more-games {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 20px 64px;
  user-select: text;
}

.more-games h2 {
  margin: 0 0 12px;
  font-size: 20px;
}

.more-games ul {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 20px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.noscript-note {
  margin: 0;
  padding: 12px 20px;
  background: var(--blue);
  color: #0c1726;
  text-align: center;
}

@media (max-width: 640px) {
  .site-footer {
    flex-wrap: wrap;
    row-gap: 2px;
    column-gap: 12px;
  }

  .site-footer > [aria-hidden="true"] {
    display: none;
  }
}
