:root {
  color-scheme: light;
  --page: #f4f6f2;
  --panel: #ffffff;
  --ink: #26302b;
  --muted: #647067;
  --line: #d8ded7;
  --board: #3d5148;
  --cell: #617469;
  --accent: #f35f42;
  --accent-dark: #c9412e;
  --focus: #1a8c7c;
  --shadow: 0 18px 60px rgba(38, 48, 43, 0.18);
  --radius: 8px;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
}

body {
  min-height: 100vh;
  margin: 0;
  background:
    linear-gradient(135deg, rgba(26, 140, 124, 0.12), transparent 34%),
    linear-gradient(315deg, rgba(243, 95, 66, 0.13), transparent 42%),
    var(--page);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
    "Segoe UI", sans-serif;
  letter-spacing: 0;
}

button {
  font: inherit;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.game-panel {
  width: min(100%, 640px);
  display: grid;
  gap: 18px;
  padding: 22px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.brand {
  display: flex;
  min-width: 0;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
}

h1 {
  margin: 0;
  font-size: 2.75rem;
  line-height: 1;
}

.actions {
  display: flex;
  flex: 0 0 auto;
  gap: 8px;
}

.icon-button,
.command-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  border: 0;
  border-radius: var(--radius);
  color: var(--ink);
  background: #eef2ed;
  cursor: pointer;
  transition: transform 160ms ease, background-color 160ms ease, color 160ms ease;
}

.icon-button svg,
.command-button svg {
  width: 21px;
  height: 21px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-button:hover,
.command-button:hover {
  transform: translateY(-1px);
  background: #e2ebe5;
}

.icon-button:focus-visible,
.command-button:focus-visible {
  outline: 3px solid rgba(26, 140, 124, 0.35);
  outline-offset: 2px;
}

.icon-button.primary {
  color: #fff;
  background: var(--accent);
}

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

.icon-button:disabled {
  color: #a3ada6;
  background: #edf0ec;
  cursor: default;
  transform: none;
}

.scorebar {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.score-chip {
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #f8faf7;
}

.score-chip span {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  line-height: 1.2;
  text-transform: uppercase;
}

.score-chip strong {
  display: block;
  margin-top: 3px;
  font-size: 1.55rem;
  line-height: 1;
}

.board-shell {
  display: grid;
  place-items: center;
}

.board {
  --gap: 10px;
  --pad: 10px;
  position: relative;
  width: min(100%, 560px);
  aspect-ratio: 1;
  border-radius: var(--radius);
  background: var(--board);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
  touch-action: none;
  user-select: none;
}

.grid-bg,
.tile-layer {
  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);
}

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

.tile {
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  border-radius: 6px;
  color: #ffffff;
  font-size: 2.45rem;
  font-weight: 850;
  line-height: 1;
  animation: tile-pop 140ms ease-out;
  box-shadow:
    inset 0 -2px 0 rgba(0, 0, 0, 0.14),
    0 6px 18px rgba(21, 30, 26, 0.16);
}

.tile[data-digits="3"] {
  font-size: 2rem;
}

.tile[data-digits="4"] {
  font-size: 1.65rem;
}

.tile-2 {
  color: var(--ink);
  background: #dcefe8;
}

.tile-4 {
  color: var(--ink);
  background: #eadfbf;
}

.tile-8 {
  background: #f28d4f;
}

.tile-16 {
  background: #ef6b4f;
}

.tile-32 {
  background: #e84b62;
}

.tile-64 {
  background: #b83c80;
}

.tile-128 {
  background: #22a699;
}

.tile-256 {
  background: #2489c7;
}

.tile-512 {
  background: #3f7d44;
}

.tile-1024 {
  background: #7c4dc4;
}

.tile-2048,
.tile-super {
  background: #212923;
  color: #ffd166;
}

.message {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  border-radius: var(--radius);
  background: rgba(244, 246, 242, 0.86);
  backdrop-filter: blur(4px);
}

.message[hidden] {
  display: none;
}

.message-panel {
  width: min(100%, 330px);
  display: grid;
  gap: 12px;
  padding: 18px;
  border: 1px solid rgba(38, 48, 43, 0.14);
  border-radius: var(--radius);
  background: #ffffff;
  text-align: center;
  box-shadow: 0 18px 50px rgba(38, 48, 43, 0.22);
}

.message-panel strong {
  font-size: 1.45rem;
}

.message-panel span {
  color: var(--muted);
}

.message-actions {
  display: flex;
  justify-content: center;
  gap: 8px;
}

.direction-pad {
  width: min(100%, 220px);
  display: grid;
  grid-template-columns: repeat(3, 52px);
  grid-template-rows: repeat(2, 52px);
  justify-content: center;
  gap: 8px;
  margin: 0 auto;
}

.direction-button {
  width: 52px;
  height: 52px;
  color: #fff;
  background: #3d5148;
}

.direction-button:hover {
  background: #26302b;
}

.direction-up {
  grid-column: 2;
  grid-row: 1;
}

.direction-left {
  grid-column: 1;
  grid-row: 2;
}

.direction-down {
  grid-column: 2;
  grid-row: 2;
}

.direction-right {
  grid-column: 3;
  grid-row: 2;
}

.command-button {
  min-width: 0;
  gap: 8px;
  padding: 0 12px;
  font-weight: 800;
}

.command-button:first-child {
  color: #fff;
  background: var(--focus);
}

.command-button:first-child:hover {
  background: #116d62;
}

.status {
  min-height: 1.35rem;
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
  text-align: center;
}

@keyframes tile-pop {
  from {
    transform: scale(0.82);
  }
  to {
    transform: scale(1);
  }
}

@media (max-width: 520px) {
  .app-shell {
    padding: 12px;
    align-items: start;
  }

  .game-panel {
    gap: 14px;
    padding: 14px;
  }

  h1 {
    font-size: 2.25rem;
  }

  .brand-mark {
    width: 38px;
    height: 38px;
  }

  .board {
    --gap: 7px;
    --pad: 7px;
  }

  .tile {
    font-size: 1.85rem;
  }

  .tile[data-digits="3"] {
    font-size: 1.45rem;
  }

  .tile[data-digits="4"] {
    font-size: 1.14rem;
  }

  .score-chip strong {
    font-size: 1.35rem;
  }
}

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