:root {
  color-scheme: light;
  --bg: #f6f7fb;
  --bg-elevated: #eceff5;
  --surface: rgba(255, 255, 255, 0.72);
  --surface-strong: rgba(255, 255, 255, 0.9);
  --surface-soft: rgba(255, 255, 255, 0.52);
  --text: #15171c;
  --muted: #626976;
  --line: rgba(17, 20, 28, 0.12);
  --shadow: 0 18px 50px rgba(22, 26, 35, 0.12);
  --shadow-soft: 0 10px 28px rgba(22, 26, 35, 0.09);
  --accent: #0f9f8e;
  --accent-strong: #08796d;
  --accent-soft: rgba(15, 159, 142, 0.14);
  --warn: #e06d45;
  --gold: #c9981e;
  --board: rgba(31, 35, 43, 0.08);
  --cell: rgba(255, 255, 255, 0.62);
  --toast: rgba(23, 25, 30, 0.92);
  --toast-text: #fff;
  --tile-text-dark: #1a1c20;
  --tile-text-light: #fff;
  --radius: 8px;
  --motion: 180ms ease-in-out;
  --tile-motion: 125ms ease-in-out;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #101113;
  --bg-elevated: #1b1a1d;
  --surface: rgba(31, 32, 35, 0.72);
  --surface-strong: rgba(38, 39, 43, 0.9);
  --surface-soft: rgba(255, 255, 255, 0.08);
  --text: #f3f4f6;
  --muted: #a6aab2;
  --line: rgba(255, 255, 255, 0.12);
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.32);
  --shadow-soft: 0 10px 30px rgba(0, 0, 0, 0.24);
  --accent: #38c7b5;
  --accent-strong: #7adfd1;
  --accent-soft: rgba(56, 199, 181, 0.14);
  --warn: #ff8b63;
  --gold: #f0c45a;
  --board: rgba(255, 255, 255, 0.08);
  --cell: rgba(255, 255, 255, 0.08);
  --toast: rgba(246, 247, 251, 0.94);
  --toast-text: #15171c;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  background: linear-gradient(135deg, var(--bg), var(--bg-elevated));
  overflow-x: hidden;
  overscroll-behavior: none;
  transition:
    background var(--motion),
    color var(--motion);
}

button {
  border: 0;
  font: inherit;
  color: inherit;
}

button:focus-visible,
.board:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 72%, transparent);
  outline-offset: 3px;
}

.app-shell {
  width: min(100%, 1040px);
  margin: 0 auto;
  padding: clamp(18px, 4vw, 34px);
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 12px;
}

.brand-block {
  display: grid;
}

.header-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
}

.metric-card span,
.modal-kicker {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.metric-card span {
  font-size: 0.66rem;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(2.65rem, 12vw, 5.2rem);
  line-height: 0.9;
  font-weight: 900;
}

.icon {
  width: 1.25rem;
  height: 1.25rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: 0 0 auto;
}

.icon-button,
.primary-button,
.secondary-button,
.power-button {
  min-height: 44px;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition:
    transform var(--motion),
    border-color var(--motion),
    background var(--motion),
    color var(--motion),
    opacity var(--motion),
    box-shadow var(--motion);
}

.icon-button {
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  box-shadow: var(--shadow-soft);
  backdrop-filter: blur(16px);
}

.icon-button:hover,
.primary-button:hover,
.secondary-button:hover,
.power-button:hover:not(:disabled) {
  transform: translateY(-1px);
}

.theme-button .moon-icon,
:root[data-theme="dark"] .theme-button .sun-icon {
  display: none;
}

:root[data-theme="dark"] .theme-button .moon-icon {
  display: block;
}

.score-strip {
  display: grid;
  grid-template-columns: 1fr 1fr 0.82fr;
  gap: 8px;
  margin-bottom: 12px;
}

.metric-card,
.control-panel {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-soft);
  backdrop-filter: blur(18px);
}

.metric-card {
  min-width: 0;
  padding: 9px 11px;
}

.metric-card strong {
  display: block;
  margin-top: 3px;
  font-size: clamp(1.05rem, 5vw, 1.45rem);
  line-height: 1;
}

.metric-card.compact strong {
  font-size: clamp(1rem, 4.6vw, 1.35rem);
}

.play-layout {
  --game-size: min(92vw, 500px);
  display: grid;
  justify-items: center;
  gap: 12px;
}

.board-column {
  display: grid;
  justify-items: center;
  gap: 10px;
}

.mode-hint {
  width: min(100%, 520px);
  padding: 10px 12px;
  border: 1px solid color-mix(in srgb, var(--accent) 38%, var(--line));
  border-radius: var(--radius);
  color: var(--accent-strong);
  background: var(--accent-soft);
  font-size: 0.9rem;
  font-weight: 750;
  text-align: center;
  box-shadow: var(--shadow-soft);
}

.board {
  --board-size: var(--game-size);
  --tile-gap: clamp(8px, 2.3vw, 14px);
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background:
    linear-gradient(145deg, rgba(255, 255, 255, 0.2), transparent),
    var(--board);
  box-shadow: var(--shadow);
  overflow: hidden;
  touch-action: none;
  backdrop-filter: blur(18px);
}

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

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

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

.tile-layer {
  pointer-events: none;
}

.tile {
  --x: 0px;
  --y: 0px;
  --scale: 1;
  --tile-bg: #e9edf2;
  --tile-fg: var(--tile-text-dark);
  position: absolute;
  left: 0;
  top: 0;
  display: grid;
  place-items: center;
  border-radius: var(--radius);
  color: var(--tile-fg);
  background: var(--tile-bg);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.28),
    0 9px 20px rgba(20, 24, 30, 0.14);
  font-size: clamp(1.35rem, 8vw, 2.6rem);
  font-weight: 900;
  line-height: 1;
  pointer-events: auto;
  transform: translate3d(var(--x), var(--y), 0) scale(var(--scale));
  transition:
    transform var(--tile-motion),
    opacity var(--tile-motion),
    box-shadow var(--motion),
    filter var(--motion);
  will-change: transform, opacity;
}

.tile[data-value="2"] {
  --tile-bg: #edf0f4;
}

.tile[data-value="4"] {
  --tile-bg: #dcefe8;
}

.tile[data-value="8"] {
  --tile-bg: #9bd8cf;
}

.tile[data-value="16"] {
  --tile-bg: #7ac4e0;
}

.tile[data-value="32"] {
  --tile-bg: #f4b36d;
}

.tile[data-value="64"] {
  --tile-bg: #ef805f;
  --tile-fg: var(--tile-text-light);
}

.tile[data-value="128"] {
  --tile-bg: #d8b74a;
  --tile-fg: var(--tile-text-light);
  font-size: clamp(1.15rem, 7vw, 2.2rem);
}

.tile[data-value="256"] {
  --tile-bg: #b8a6ff;
  --tile-fg: var(--tile-text-light);
  font-size: clamp(1.15rem, 7vw, 2.2rem);
}

.tile[data-value="512"] {
  --tile-bg: #56b7a8;
  --tile-fg: var(--tile-text-light);
  font-size: clamp(1.15rem, 7vw, 2.2rem);
}

.tile[data-value="1024"] {
  --tile-bg: #3d8ad8;
  --tile-fg: var(--tile-text-light);
  font-size: clamp(0.94rem, 5.6vw, 1.9rem);
}

.tile[data-value="2048"] {
  --tile-bg: #18191d;
  --tile-fg: #fff;
  font-size: clamp(0.94rem, 5.6vw, 1.9rem);
}

.tile.tile-super {
  --tile-bg: #0f9f8e;
  --tile-fg: #fff;
  font-size: clamp(0.82rem, 5vw, 1.65rem);
}

.tile.is-new {
  animation: tile-spawn 120ms ease-in-out both;
}

.tile.is-merged {
  animation: tile-merge 155ms ease-in-out both;
}

.tile.is-removing {
  --scale: 0.62;
  opacity: 0;
}

.tile.is-selected {
  box-shadow:
    0 0 0 3px color-mix(in srgb, var(--accent) 68%, transparent),
    0 0 30px color-mix(in srgb, var(--accent) 42%, transparent),
    0 12px 24px rgba(20, 24, 30, 0.18);
  filter: saturate(1.18);
}

.control-panel {
  width: var(--game-size);
  display: grid;
  padding: 10px;
}

.primary-button,
.secondary-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  width: 100%;
  border-radius: var(--radius);
  font-weight: 850;
}

.primary-button {
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-strong));
  box-shadow: 0 12px 28px color-mix(in srgb, var(--accent) 24%, transparent);
}

.header-new-game {
  width: 48px;
  min-height: 48px;
  padding: 0;
}

.secondary-button {
  border: 1px solid var(--line);
  background: var(--surface-soft);
}

.power-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.power-button {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 74px;
  padding: 8px 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-strong);
  text-align: center;
}

.power-button.is-active {
  border-color: color-mix(in srgb, var(--accent) 58%, var(--line));
  background: var(--accent-soft);
}

.power-button:disabled {
  cursor: not-allowed;
  opacity: 0.46;
  transform: none;
}

.power-icon {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: var(--radius);
  color: var(--accent-strong);
  background: var(--accent-soft);
}

.power-copy {
  min-width: 0;
  display: grid;
  gap: 2px;
}

.power-name {
  font-weight: 850;
}

.power-note {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 650;
}

.charge-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.charge-dot {
  width: 7px;
  height: 7px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--muted) 32%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--muted) 20%, transparent);
}

.charge-dot.is-filled {
  background: var(--accent);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 16%, transparent);
}

.toast-host {
  position: fixed;
  z-index: 40;
  right: max(14px, env(safe-area-inset-right));
  bottom: max(14px, env(safe-area-inset-bottom));
  width: min(360px, calc(100vw - 28px));
  display: grid;
  gap: 10px;
  pointer-events: none;
}

.toast {
  padding: 12px 14px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: var(--radius);
  color: var(--toast-text);
  background: var(--toast);
  box-shadow: var(--shadow);
  font-weight: 760;
  transform: translateY(12px);
  opacity: 0;
  animation: toast-in 260ms ease-in-out forwards;
  backdrop-filter: blur(18px);
}

.toast.is-leaving {
  animation: toast-out 220ms ease-in-out forwards;
}

.modal-backdrop {
  position: fixed;
  z-index: 35;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(12, 13, 16, 0.52);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--motion);
  backdrop-filter: blur(10px);
}

.modal-backdrop.is-visible {
  opacity: 1;
  pointer-events: auto;
}

.modal-card {
  width: min(410px, 100%);
  display: grid;
  gap: 12px;
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-strong);
  box-shadow: var(--shadow);
  text-align: center;
  transform: translateY(12px) scale(0.98);
  transition: transform var(--motion);
}

.modal-backdrop.is-visible .modal-card {
  transform: translateY(0) scale(1);
}

.modal-card h2 {
  font-size: clamp(2rem, 10vw, 3.2rem);
  line-height: 1;
}

.modal-card p {
  color: var(--muted);
  font-weight: 650;
}

.modal-score {
  font-size: clamp(2.1rem, 12vw, 4rem);
  line-height: 1;
}

.modal-actions {
  display: grid;
  gap: 10px;
  margin-top: 4px;
}

.fx-canvas {
  position: fixed;
  z-index: 45;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

@keyframes tile-spawn {
  from {
    opacity: 0;
    transform: translate3d(var(--x), var(--y), 0) scale(0.52);
  }

  to {
    opacity: 1;
    transform: translate3d(var(--x), var(--y), 0) scale(1);
  }
}

@keyframes tile-merge {
  0%,
  100% {
    transform: translate3d(var(--x), var(--y), 0) scale(1);
  }

  52% {
    transform: translate3d(var(--x), var(--y), 0) scale(1.12);
  }
}

@keyframes toast-in {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes toast-out {
  to {
    opacity: 0;
    transform: translateY(10px);
  }
}

@media (min-width: 760px) {
  .app-shell {
    padding-top: 38px;
  }

  .score-strip {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (min-width: 760px) and (max-height: 760px) {
  .play-layout {
    --game-size: min(92vw, 380px);
  }
}

@media (max-width: 420px) {
  .app-shell {
    padding-inline: 14px;
  }

  .header-actions {
    gap: 8px;
  }

  .header-new-game {
    width: 44px;
    min-height: 44px;
  }

  .score-strip {
    gap: 7px;
  }

  .metric-card {
    padding: 8px 9px;
  }

  .control-panel {
    padding: 9px;
  }

  .power-button {
    min-height: 64px;
    gap: 6px;
  }

  .power-icon {
    width: 34px;
    height: 34px;
  }
}

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