:root {
  --bg: #1f2120;
  --panel: #181c29;
  --text: #fbf3e4;
  --muted: #d9cfbd;
  --violet: #7c47f5;
  --cyan: #27d6c1;
  --yellow: #facc15;
  --pink: #fb7185;
  --lime: #a3e635;
  --orange: #fb923c;
  --ink: #1e1238;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  color: var(--text);
  font-family: ui-rounded, "SF Pro Rounded", "Segoe UI", system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  background:
    radial-gradient(110% 70% at 0% 0%, rgb(34 211 238 / 0.18), transparent 60%),
    radial-gradient(90% 70% at 100% 100%, rgb(124 58 237 / 0.26), transparent 60%),
    var(--bg);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.hidden {
  display: none !important;
}

/* ---------- Layout: portrait = column, landscape = side panel + board ---------- */

.app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.side {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas:
    "brand actions"
    "scores scores";
  align-items: center;
  gap: 10px 12px;
  width: min(100%, 560px);
  margin: 0 auto;
  padding: 12px 14px 4px;
}

.brand {
  grid-area: brand;
  font-size: 26px;
  font-weight: 900;
  line-height: 1;
  letter-spacing: -0.01em;
  color: var(--cyan);
}

.brand span {
  margin-left: 6px;
  color: var(--pink);
}

.scores {
  grid-area: scores;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 5px 10px 6px;
  border-radius: 16px;
  background: rgb(255 255 255 / 0.07);
  box-shadow: inset 0 0 0 1px rgb(255 255 255 / 0.06);
}

.label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.stat strong {
  font-size: 24px;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}

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

.icon-btn {
  position: relative;
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  padding: 0;
  border: 0;
  border-radius: 15px;
  background: rgb(255 255 255 / 0.1);
  color: var(--text);
  cursor: pointer;
}

.icon-btn svg {
  width: 24px;
  height: 24px;
}

.icon-btn:hover:not(:disabled) {
  background: rgb(255 255 255 / 0.17);
}

.icon-btn:disabled {
  opacity: 0.38;
  cursor: default;
}

.badge {
  position: absolute;
  top: -5px;
  right: -5px;
  min-width: 19px;
  height: 19px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--yellow);
  color: #1a1408;
  font-size: 12px;
  font-weight: 900;
  line-height: 19px;
}

.side .hint {
  display: none;
}

.foot-hint {
  margin: 0;
  padding: 0 16px 18px;
  color: var(--muted);
  font-size: 14px;
  text-align: center;
}

@media (max-height: 560px) {
  .foot-hint {
    display: none;
  }
}

@media (max-width: 370px) {
  .side {
    gap: 8px 8px;
    padding: 10px 10px 4px;
  }

  .brand {
    font-size: 21px;
  }

  .brand span {
    margin-left: 4px;
  }

  .actions {
    gap: 6px;
  }

  .icon-btn {
    width: 44px;
    height: 44px;
  }
}

.stage {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 14px 16px;
  touch-action: none;
}

@media (min-aspect-ratio: 6/5) {
  .app {
    flex-direction: row;
    justify-content: center;
  }

  .side {
    grid-template-columns: 1fr;
    grid-template-areas:
      "brand"
      "scores"
      "actions";
    align-content: center;
    justify-items: center;
    width: clamp(190px, 26vw, 300px);
    margin: 0;
    padding: 14px 4px 14px 18px;
  }

  .brand {
    font-size: clamp(24px, 4.2vmin, 36px);
    text-align: center;
  }

  .brand span {
    display: block;
    margin: 0;
  }

  .scores {
    width: 100%;
  }

  .side .hint {
    display: block;
    margin: 0;
    text-align: center;
  }

  .foot-hint {
    display: none;
  }

  .stage {
    flex: none;
    padding: 14px 18px 14px 18px;
  }
}

@media (min-aspect-ratio: 6/5) and (max-height: 420px) {
  .side {
    gap: 8px;
  }

  .side .hint {
    display: none;
  }

  .stat strong {
    font-size: 20px;
  }
}

/* ---------- Board ---------- */

.board {
  --cell: 80px;
  --gap: 10px;
  position: relative;
  width: 360px;
  height: 360px;
  flex: none;
  border-radius: calc(var(--gap) * 1.6);
  background: #1b1f33;
  box-shadow:
    inset 0 0 0 2px rgb(255 255 255 / 0.05),
    0 18px 40px rgb(0 0 0 / 0.35);
}

.cells {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--gap);
  padding: var(--gap);
}

.cells i {
  border-radius: 28%;
  background: #262b44;
  box-shadow: inset 0 3px 0 rgb(0 0 0 / 0.18);
}

.tiles {
  position: absolute;
  inset: 0;
}

.tile {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--cell);
  height: var(--cell);
  transition: transform 0.12s ease-out;
  will-change: transform;
}

.board.no-anim .tile {
  transition: none;
}

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

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

.tile,
.demo-tile {
  --s: 0.8;
  --c: #67e8f9;
  --t: var(--ink);
}

.blob {
  position: absolute;
  inset: 0;
  margin: auto;
  width: calc(var(--s) * 100%);
  height: calc(var(--s) * 100%);
  font-size: calc(var(--cell) * var(--s));
  border-radius: 47% 53% 46% 54% / 55% 50% 50% 45%;
  background:
    radial-gradient(circle at 19% 42%, rgb(255 255 255 / 0.5) 0 6%, transparent 7%),
    radial-gradient(120% 100% at 30% 15%, rgb(255 255 255 / 0.3), transparent 55%),
    var(--c);
  box-shadow:
    inset 0 -0.07em 0 rgb(0 0 0 / 0.16),
    0 0.05em 0 rgb(0 0 0 / 0.28);
  color: var(--t);
}

.blob .eyes {
  position: absolute;
  top: 19%;
  left: 50%;
  display: flex;
  gap: 0.2em;
  transform: translateX(-50%);
}

.blob .eyes i {
  width: 0.095em;
  height: 0.125em;
  border-radius: 50%;
  background:
    radial-gradient(circle at 35% 30%, #fff 0 22%, transparent 26%),
    #1b1035;
}

.blob .mouth {
  position: absolute;
  top: 31%;
  left: 50%;
  width: 0.13em;
  height: 0.065em;
  transform: translateX(-50%);
  border-radius: 0 0 0.1em 0.1em;
  background: #1b1035;
}

.blob::before,
.blob::after {
  content: "";
  position: absolute;
  top: 29%;
  width: 0.12em;
  height: 0.065em;
  border-radius: 50%;
  background: rgb(255 80 140 / 0.42);
}

.blob::before {
  left: 17%;
}

.blob::after {
  right: 17%;
}

.blob .num {
  position: absolute;
  left: 0;
  right: 0;
  top: 43%;
  font-size: 0.4em;
  font-weight: 900;
  line-height: 1;
  text-align: center;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}

.tile[data-d="3"] .num,
.demo-tile[data-d="3"] .num {
  top: 46%;
  font-size: 0.33em;
}

.tile[data-d="4"] .num,
.demo-tile[data-d="4"] .num {
  top: 48%;
  font-size: 0.26em;
}

.tile[data-d="5"] .num,
.tile[data-d="6"] .num {
  top: 50%;
  font-size: 0.2em;
}

/* Tier colors + size bump (2 … 2048 and beyond). */
.v1 { --c: #67e8f9; --s: 0.78; }
.v2 { --c: #a3e635; --s: 0.8; }
.v3 { --c: #facc15; --s: 0.82; }
.v4 { --c: #fb923c; --s: 0.84; }
.v5 { --c: #fb7185; --s: 0.86; }
.v6 { --c: #f43f5e; --s: 0.88; --t: #fff; }
.v7 { --c: #e879f9; --s: 0.9; }
.v8 { --c: #a78bfa; --s: 0.92; }
.v9 { --c: #7c47f5; --s: 0.94; --t: #fff; }
.v10 { --c: #3b82f6; --s: 0.97; --t: #fff; }
.v11 { --c: #fbbf24; --s: 1; }
.v12 { --c: #22c55e; --s: 1; --t: #fff; }

.v6 .eyes i,
.v9 .eyes i,
.v10 .eyes i,
.v12 .eyes i {
  background:
    radial-gradient(circle at 35% 30%, #fff 0 22%, transparent 26%),
    #12082a;
}

.v11 .blob {
  box-shadow:
    inset 0 -0.07em 0 rgb(0 0 0 / 0.16),
    0 0 0.25em rgb(251 191 36 / 0.75);
}

.v12 .blob {
  background:
    radial-gradient(circle at 19% 42%, rgb(255 255 255 / 0.5) 0 6%, transparent 7%),
    linear-gradient(135deg, #27d6c1, #a3e635 35%, #facc15 55%, #fb7185 75%, #7c47f5);
}

/* Animations */
.tile.is-new .blob {
  animation: appear 0.2s 0.06s cubic-bezier(0.3, 1.6, 0.5, 1) backwards;
}

.tile.is-merged .blob {
  animation: squish 0.26s ease-out;
}

.tile.is-undo .blob {
  animation: fade 0.18s ease-out;
}

/* Jelly inertia: stretch along the slide, squash against the stop (anchored at the leading
   edge), wobble back. */
.tile.slide-right .blob,
.tile.slide-left .blob {
  animation: jelly-x 0.34s ease-out;
}

.tile.slide-up .blob,
.tile.slide-down .blob {
  animation: jelly-y 0.34s ease-out;
}

.tile.slide-right .blob {
  transform-origin: 100% 50%;
}

.tile.slide-left .blob {
  transform-origin: 0% 50%;
}

.tile.slide-down .blob {
  transform-origin: 50% 100%;
}

.tile.slide-up .blob {
  transform-origin: 50% 0%;
}

@keyframes jelly-x {
  25% {
    transform: scale(1.12, 0.9);
  }
  42% {
    transform: scale(0.84, 1.13);
  }
  64% {
    transform: scale(1.06, 0.96);
  }
  82% {
    transform: scale(0.98, 1.02);
  }
}

@keyframes jelly-y {
  25% {
    transform: scale(0.9, 1.12);
  }
  42% {
    transform: scale(1.13, 0.84);
  }
  64% {
    transform: scale(0.96, 1.06);
  }
  82% {
    transform: scale(1.02, 0.98);
  }
}

@keyframes appear {
  from {
    transform: scale(0);
  }
}

@keyframes squish {
  0% {
    transform: scale(0.9);
  }
  40% {
    transform: scale(1.22, 1.08);
  }
  70% {
    transform: scale(0.96, 1.08);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes fade {
  from {
    opacity: 0;
    transform: scale(0.92);
  }
}

.board.bump-left {
  animation: bump-left 0.18s ease;
}
.board.bump-right {
  animation: bump-right 0.18s ease;
}
.board.bump-up {
  animation: bump-up 0.18s ease;
}
.board.bump-down {
  animation: bump-down 0.18s ease;
}

@keyframes bump-left {
  50% {
    transform: translateX(-6px);
  }
}
@keyframes bump-right {
  50% {
    transform: translateX(6px);
  }
}
@keyframes bump-up {
  50% {
    transform: translateY(-6px);
  }
}
@keyframes bump-down {
  50% {
    transform: translateY(6px);
  }
}

/* ---------- Overlays ---------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  padding: 16px;
  overflow-y: auto;
  background: rgb(31 33 32 / 0.8);
  -webkit-backdrop-filter: blur(5px);
  backdrop-filter: blur(5px);
}

.panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  width: min(440px, 100%);
  margin: auto;
  padding: clamp(18px, 4vmin, 30px);
  border-radius: 26px;
  text-align: center;
  background: linear-gradient(180deg, #1f2436, var(--panel));
  box-shadow:
    inset 0 0 0 1px rgb(255 255 255 / 0.08),
    0 24px 60px rgb(0 0 0 / 0.45);
}

.panel h1,
.panel h2 {
  margin: 0;
  font-size: clamp(28px, 7vmin, 46px);
  line-height: 1.05;
}

.panel h1 {
  background: linear-gradient(90deg, var(--cyan), var(--lime) 40%, var(--yellow) 60%, var(--pink));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.panel p {
  margin: 0;
}

.goal {
  color: #d5dae4;
  font-size: 17px;
  line-height: 1.35;
}

.hint {
  color: var(--muted);
  font-size: 14px;
}

.best-line {
  color: var(--muted);
  font-size: 15px;
}

.best-line strong {
  color: var(--text);
}

.new-best {
  display: inline-block;
  margin-left: 6px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--yellow);
  color: #1a1408;
  font-size: 12px;
  font-weight: 800;
}

.demo {
  --cell: 64px;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: 4px;
  height: 72px;
}

.demo-one {
  --cell: 84px;
  height: 84px;
}

.demo-tile {
  position: relative;
  width: var(--cell);
  height: var(--cell);
}

.demo .demo-tile:nth-child(1) {
  transform: rotate(-8deg);
}

.demo .demo-tile:nth-child(3) {
  transform: rotate(7deg);
}

.demo-one .demo-tile {
  animation: float 2.4s ease-in-out infinite;
}

@keyframes float {
  50% {
    transform: translateY(-5px) rotate(-3deg);
  }
}

.summary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  width: 100%;
}

.summary > div {
  display: flex;
  flex-direction: column;
  padding: 8px 4px;
  border-radius: 14px;
  background: rgb(255 255 255 / 0.06);
}

.summary strong {
  font-size: 26px;
  font-variant-numeric: tabular-nums;
}

.row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
}

.btn {
  min-width: 160px;
  min-height: 52px;
  padding: 10px 26px;
  border: 0;
  border-radius: 999px;
  background: #ee5e24;
  color: #15130f;
  font: inherit;
  font-size: 20px;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 5px 0 #b8400f;
  transition:
    transform 0.08s ease,
    box-shadow 0.08s ease,
    filter 0.15s ease;
}

.btn:hover {
  filter: brightness(1.1);
}

.btn:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #b8400f;
}

.btn-alt {
  background: #2b3147;
  box-shadow: 0 5px 0 #161a27;
}

.btn-alt:active {
  box-shadow: 0 2px 0 #161a27;
}

.btn:focus,
.icon-btn:focus {
  outline: none;
}

.btn:focus-visible,
.icon-btn:focus-visible {
  outline: 3px solid var(--cyan);
  outline-offset: 3px;
}

@media (max-height: 460px) {
  .panel {
    gap: 8px;
    padding: 14px 22px;
  }

  .demo {
    --cell: 48px;
    height: 52px;
  }

  .demo-one {
    --cell: 56px;
    height: 56px;
  }

  .goal {
    font-size: 15px;
  }

  .summary strong {
    font-size: 22px;
  }
}

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

  .tile.is-new .blob,
  .tile.is-merged .blob,
  .tile.is-undo .blob,
  .tile.slide-left .blob,
  .tile.slide-right .blob,
  .tile.slide-up .blob,
  .tile.slide-down .blob,
  .demo-one .demo-tile,
  .board.bump-left,
  .board.bump-right,
  .board.bump-up,
  .board.bump-down {
    animation: none;
  }
}
