:root {
  color-scheme: light;
  --ink: #112044;
  --ink-soft: #53617c;
  --line: rgba(255, 255, 255, 0.62);
  --glass: rgba(255, 255, 255, 0.42);
  --glass-strong: rgba(255, 255, 255, 0.68);
  --board: rgba(104, 115, 145, 0.23);
  --cell: rgba(255, 255, 255, 0.26);
  --accent: #2457f5;
  --accent-2: #7a5cff;
  --shadow: 0 18px 55px rgba(42, 57, 91, 0.16), inset 0 1px 0 rgba(255, 255, 255, 0.76);
  --radius-xl: 34px;
  --radius-lg: 25px;
  --gap: clamp(8px, 1.7vw, 14px);
  /* The tile layer already sits inside the board padding. Four columns have
     three internal gaps, exactly like the CSS grid behind the tiles. */
  --tile-size: calc((100% - var(--gap) * 3) / 4);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }

html { min-height: 100%; scroll-behavior: smooth; }

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  overflow-x: hidden;
  color: var(--ink);
  background:
    radial-gradient(circle at 50% 5%, rgba(255,255,255,.98) 0 18%, transparent 48%),
    linear-gradient(145deg, #edf5ff 0%, #cbdcff 34%, #e5dbff 68%, #d3efff 100%);
  transition: color .35s ease, background .35s ease;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(115deg, rgba(255,255,255,.58), transparent 38% 66%, rgba(255,255,255,.3));
  z-index: -2;
}

button, a { -webkit-tap-highlight-color: transparent; }
button { font: inherit; color: inherit; }

.ambient { position: fixed; inset: 0; overflow: hidden; pointer-events: none; z-index: -1; }
.orb { position: absolute; border-radius: 50%; filter: blur(3px); opacity: .72; animation: float 14s ease-in-out infinite alternate; }
.orb--one { width: 38vw; height: 38vw; left: -12vw; top: 16vh; background: radial-gradient(circle at 65% 35%, #fff 0 5%, rgba(63,115,255,.44) 31%, rgba(81,202,255,.1) 69%, transparent 70%); }
.orb--two { width: 46vw; height: 46vw; right: -15vw; top: -14vw; background: radial-gradient(circle at 35% 65%, rgba(255,255,255,.9), rgba(181,90,255,.32) 34%, transparent 68%); animation-delay: -5s; }
.orb--three { width: 34vw; height: 34vw; right: 12vw; bottom: -24vw; background: radial-gradient(circle at 50% 30%, rgba(255,255,255,.9), rgba(32,198,170,.3) 35%, transparent 70%); animation-delay: -9s; }
.noise { position: absolute; inset: 0; opacity: .03; background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 180 180' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.7'/%3E%3C/svg%3E"); }

.glass {
  position: relative;
  background: linear-gradient(135deg, rgba(255,255,255,.66), var(--glass) 42%, rgba(255,255,255,.25));
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  -webkit-backdrop-filter: blur(24px) saturate(145%);
  backdrop-filter: blur(24px) saturate(145%);
}

.glass::before {
  content: "";
  position: absolute;
  inset: 1px;
  border-radius: inherit;
  pointer-events: none;
  background: linear-gradient(120deg, rgba(255,255,255,.58), transparent 28% 68%, rgba(129,160,255,.13));
  opacity: .9;
}

.glass > * { position: relative; z-index: 1; }

.topbar {
  position: sticky;
  z-index: 20;
  top: 18px;
  width: min(1120px, calc(100% - 32px));
  height: 64px;
  margin: 18px auto 0;
  padding: 9px 10px 9px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-radius: 24px;
}

.brand { display: flex; align-items: center; gap: 11px; color: var(--ink); font-size: 15px; font-weight: 650; letter-spacing: -.02em; text-decoration: none; }
.brand strong { font-weight: 850; }
.brand__mark { width: 27px; height: 27px; display: grid; grid-template-columns: 1fr 1fr; gap: 3px; transform: rotate(-3deg); }
.brand__mark i { border-radius: 5px; background: linear-gradient(135deg, #4878ff, #765cff); box-shadow: inset 0 1px 1px rgba(255,255,255,.7), 0 3px 8px rgba(55,85,213,.2); }
.brand__mark i:nth-child(2) { opacity: .72; }
.brand__mark i:nth-child(3) { opacity: .55; }
.brand__mark i:nth-child(4) { opacity: .88; }
.topbar__actions { display: flex; gap: 4px; }

.icon-button {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 16px;
  background: transparent;
  cursor: pointer;
  transition: transform .2s ease, background .2s ease;
}
.icon-button:hover { background: rgba(255,255,255,.45); transform: translateY(-1px); }
.icon-button:active { transform: scale(.94); }
.icon { width: 21px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon--sun { display: none; }
body.dark .icon--moon { display: none; }
body.dark .icon--sun { display: block; }

.page-shell { width: min(1060px, calc(100% - 40px)); margin: 0 auto; padding: clamp(62px, 9vh, 110px) 0 54px; }
.intro { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: end; gap: 40px; margin-bottom: 36px; }
.eyebrow { margin: 0 0 16px; display: flex; align-items: center; gap: 9px; color: var(--accent); font-size: 12px; font-weight: 750; letter-spacing: .16em; }
.eyebrow span { width: 22px; height: 1.5px; background: currentColor; border-radius: 2px; }
h1 { margin: 0; font-size: clamp(44px, 6.5vw, 72px); line-height: .98; letter-spacing: -.065em; font-weight: 810; }
h1 em { color: var(--accent); font-style: normal; }
.intro__copy > p:last-child { margin: 19px 0 0; color: var(--ink-soft); font-size: 15px; }

.score-cluster { display: flex; gap: 12px; }
.score-card { width: 132px; min-height: 89px; padding: 16px 18px; border-radius: 22px; overflow: hidden; }
.score-card span { display: block; color: var(--ink-soft); font-size: 11px; font-weight: 650; letter-spacing: .08em; }
.score-card strong { display: block; margin-top: 8px; font-size: 30px; line-height: 1; letter-spacing: -.04em; }
.score-card b { position: absolute; right: 17px; bottom: 19px; color: var(--accent); font-size: 12px; opacity: 0; transform: translateY(5px); }
.score-card b.pop { animation: score-pop .7s ease both; }

.game-layout { display: grid; grid-template-columns: minmax(0, 665px) minmax(220px, 1fr); gap: clamp(30px, 5vw, 72px); align-items: start; }
.game-column { min-width: 0; }
.game-toolbar { min-height: 62px; margin-bottom: 13px; padding: 8px 9px 8px 18px; display: flex; align-items: center; justify-content: space-between; border-radius: 22px; box-shadow: 0 11px 35px rgba(42,57,91,.11), inset 0 1px rgba(255,255,255,.7); }
.status-pill { display: flex; align-items: center; gap: 9px; color: var(--ink-soft); font-size: 12px; font-weight: 650; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #39c893; box-shadow: 0 0 0 5px rgba(57,200,147,.11); }

.action-button {
  min-height: 45px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 16px;
  border: 1px solid rgba(255,255,255,.7);
  border-radius: 15px;
  color: var(--ink);
  background: rgba(255,255,255,.45);
  box-shadow: inset 0 1px rgba(255,255,255,.72), 0 6px 16px rgba(38,55,92,.08);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  transition: transform .18s ease, background .18s ease, box-shadow .18s ease;
}
.action-button svg { width: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.action-button:hover { transform: translateY(-2px); background: rgba(255,255,255,.72); box-shadow: inset 0 1px #fff, 0 9px 20px rgba(38,55,92,.12); }
.action-button:active { transform: translateY(0) scale(.97); }
.action-button--primary { color: white; background: linear-gradient(135deg, #4775ff, #5e4bed); border-color: rgba(255,255,255,.35); box-shadow: inset 0 1px rgba(255,255,255,.45), 0 8px 20px rgba(56,75,220,.25); }
.action-button--primary:hover { background: linear-gradient(135deg, #5c86ff, #705cff); }

.board-wrap { position: relative; }
.board {
  position: relative;
  aspect-ratio: 1;
  width: 100%;
  padding: var(--gap);
  border: 1px solid rgba(255,255,255,.58);
  border-radius: var(--radius-xl);
  outline: none;
  overflow: hidden;
  background: linear-gradient(145deg, rgba(113,127,159,.34), rgba(109,112,158,.21));
  box-shadow: 0 26px 60px rgba(48,59,105,.19), inset 0 1px 1px rgba(255,255,255,.73), inset 0 -1px rgba(79,89,121,.1);
  -webkit-backdrop-filter: blur(22px) saturate(120%);
  backdrop-filter: blur(22px) saturate(120%);
  touch-action: none;
}
.board:focus-visible { box-shadow: 0 0 0 4px rgba(36,87,245,.2), 0 26px 60px rgba(48,59,105,.19); }
.grid-background { position: absolute; inset: var(--gap); display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--gap); }
.grid-background { grid-template-rows: repeat(4, 1fr); }
.grid-background span { border-radius: clamp(14px, 2.5vw, 22px); background: var(--cell); box-shadow: inset 0 1px rgba(255,255,255,.42); }
.tile-layer { position: absolute; inset: var(--gap); }

.tile {
  position: absolute;
  left: calc(var(--col) * (var(--tile-size) + var(--gap)));
  top: calc(var(--row) * (var(--tile-size) + var(--gap)));
  width: var(--tile-size);
  height: var(--tile-size);
  display: grid;
  place-items: center;
  border-radius: clamp(14px, 2.5vw, 22px);
  color: #202b52;
  background: linear-gradient(145deg, rgba(255,255,255,.88), rgba(232,238,255,.58));
  border: 1px solid rgba(255,255,255,.75);
  box-shadow: inset 0 1px rgba(255,255,255,.9), 0 9px 20px rgba(46,58,100,.13);
  font-size: clamp(28px, 7.4vw, 56px);
  font-weight: 780;
  letter-spacing: -.06em;
  transform: translate3d(0,0,0);
  transition: left .13s ease-out, top .13s ease-out;
  will-change: left, top, transform;
}
.tile--new { animation: tile-appear .2s cubic-bezier(.2,.8,.2,1) both; }
.tile--merged { animation: tile-merge .28s cubic-bezier(.2,.9,.25,1.25) both; }
.tile[data-value="4"] { background: linear-gradient(145deg, #eaf5ff, #cddfff); color: #2457a4; }
.tile[data-value="8"] { background: linear-gradient(145deg, #e4e9ff, #b6c4ff); color: #4148b8; }
.tile[data-value="16"] { background: linear-gradient(145deg, #eee7ff, #cbb8ff); color: #6340bf; }
.tile[data-value="32"] { background: linear-gradient(145deg, #ffe5f4, #fbb6df); color: #a43873; }
.tile[data-value="64"] { background: linear-gradient(145deg, #ffddd9, #ffacae); color: #9b343e; }
.tile[data-value="128"] { background: linear-gradient(145deg, #fff3c9, #ffd46c); color: #805911; font-size: clamp(24px, 6.2vw, 48px); }
.tile[data-value="256"] { background: linear-gradient(145deg, #ddfff0, #8fe4bf); color: #187a5a; font-size: clamp(24px, 6.2vw, 48px); }
.tile[data-value="512"] { background: linear-gradient(145deg, #d8f9ff, #7cd9ed); color: #146b80; font-size: clamp(24px, 6.2vw, 48px); }
.tile[data-value="1024"] { background: linear-gradient(145deg, #dbe7ff, #81a7ff); color: #234fae; font-size: clamp(20px, 5.2vw, 40px); }
.tile[data-value="2048"] { color: white; background: linear-gradient(145deg, #8678ff, #315ff4); border-color: rgba(255,255,255,.56); box-shadow: inset 0 1px rgba(255,255,255,.72), 0 0 34px rgba(75,90,255,.43), 0 10px 25px rgba(40,60,180,.28); font-size: clamp(20px, 5.2vw, 40px); }
.tile--super { color: white; background: linear-gradient(145deg, #3d4b83, #162448); font-size: clamp(18px, 4.3vw, 34px); }

.game-message { position: absolute; z-index: 5; inset: 0; display: grid; place-items: center; padding: 24px; background: rgba(233,241,255,.34); -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px); opacity: 0; visibility: hidden; transition: opacity .3s ease, visibility .3s; }
.game-message.show { opacity: 1; visibility: visible; }
.game-message__panel { width: min(390px, 100%); padding: 34px; border-radius: 27px; text-align: center; transform: translateY(12px) scale(.97); transition: transform .35s cubic-bezier(.2,.85,.2,1); }
.game-message.show .game-message__panel { transform: none; }
.message-kicker { display: block; color: var(--accent); font-size: 11px; font-weight: 800; letter-spacing: .15em; }
.game-message h2 { margin: 10px 0; font-size: clamp(28px, 6vw, 40px); letter-spacing: -.04em; }
.game-message p { margin: 0; color: var(--ink-soft); font-size: 13px; line-height: 1.65; }
.message-actions { margin-top: 24px; display: flex; justify-content: center; gap: 9px; }
.swipe-hint { display: flex; justify-content: center; align-items: center; gap: 8px; margin: 15px 0 0; color: var(--ink-soft); font-size: 11px; }
.swipe-hint span { font-size: 16px; }

.side-panel { padding-top: 78px; }
.tip-card { display: grid; grid-template-columns: 43px 1fr; gap: 13px; padding: 16px; border-radius: 20px; }
.tip-card__icon { width: 43px; height: 43px; display: grid; place-items: center; border-radius: 14px; color: #7256d7; background: rgba(255,255,255,.55); box-shadow: inset 0 1px white; }
.tip-card__icon svg { width: 21px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.tip-card span, .section-label { color: var(--ink-soft); font-size: 10px; font-weight: 750; letter-spacing: .13em; }
.tip-card p { margin: 6px 0 0; font-size: 12px; line-height: 1.6; }
.how-to { padding: 30px 7px 26px; }
.how-to ol { margin: 20px 0 0; padding: 0; list-style: none; }
.how-to li { display: grid; grid-template-columns: 31px 1fr; gap: 11px; align-items: start; padding: 13px 0; border-bottom: 1px solid rgba(74,88,122,.1); }
.how-to li:last-child { border-bottom: 0; }
.how-to li > b { color: var(--accent); font-size: 10px; font-weight: 750; padding-top: 3px; }
.how-to p { margin: 0; }
.how-to strong { display: block; font-size: 12px; }
.how-to p span { display: block; margin-top: 4px; color: var(--ink-soft); font-size: 10px; line-height: 1.5; }
.progress-card { padding: 17px; border-radius: 19px; }
.progress-card__header { display: flex; justify-content: space-between; color: var(--ink-soft); font-size: 10px; }
.progress-card__header strong { color: var(--ink); font-weight: 750; }
.progress-track { height: 5px; margin-top: 13px; overflow: hidden; border-radius: 10px; background: rgba(71,85,117,.12); }
.progress-track i { display: block; width: 0; height: 100%; border-radius: inherit; background: linear-gradient(90deg, var(--accent), var(--accent-2)); box-shadow: 0 0 12px rgba(68,86,246,.4); transition: width .4s ease; }

footer { width: min(1060px, calc(100% - 40px)); margin: 0 auto; padding: 24px 0 32px; display: flex; justify-content: space-between; border-top: 1px solid rgba(70,86,118,.11); color: var(--ink-soft); font-size: 10px; letter-spacing: .08em; }

.confirm-dialog { width: min(400px, calc(100% - 40px)); padding: 32px; border-radius: 28px; color: var(--ink); text-align: center; }
.confirm-dialog::backdrop { background: rgba(30,42,73,.24); -webkit-backdrop-filter: blur(7px); backdrop-filter: blur(7px); }
.confirm-dialog[open] { animation: dialog-in .25s cubic-bezier(.2,.85,.2,1); }
.dialog-icon { width: 48px; height: 48px; margin: 0 auto 16px; display: grid; place-items: center; border-radius: 16px; color: var(--accent); background: rgba(255,255,255,.6); }
.dialog-icon svg { width: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.confirm-dialog h2 { margin: 0; font-size: 24px; letter-spacing: -.03em; }
.confirm-dialog p { margin: 10px 0 24px; color: var(--ink-soft); font-size: 12px; }
.dialog-actions { display: flex; justify-content: center; gap: 8px; }

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

body.dark {
  color-scheme: dark;
  --ink: #f3f6ff;
  --ink-soft: #aeb9d0;
  --line: rgba(255,255,255,.18);
  --glass: rgba(25,31,52,.42);
  --glass-strong: rgba(34,41,67,.66);
  --board: rgba(13,18,34,.4);
  --cell: rgba(255,255,255,.075);
  --shadow: 0 22px 60px rgba(3,7,22,.34), inset 0 1px 0 rgba(255,255,255,.16);
  background: radial-gradient(circle at 50% 0%, #283657 0 12%, transparent 44%), linear-gradient(145deg, #101726 0%, #18233b 40%, #251f42 72%, #102c38 100%);
}
body.dark::before { background: linear-gradient(115deg, rgba(91,135,255,.1), transparent 40% 67%, rgba(150,93,255,.08)); }
body.dark .glass { background: linear-gradient(135deg, rgba(75,87,122,.37), var(--glass) 44%, rgba(255,255,255,.06)); }
body.dark .glass::before { background: linear-gradient(120deg, rgba(255,255,255,.17), transparent 30% 70%, rgba(121,150,255,.09)); }
body.dark .action-button, body.dark .tip-card__icon { background: rgba(255,255,255,.09); }
body.dark .action-button:hover { background: rgba(255,255,255,.15); }
body.dark .action-button--primary { background: linear-gradient(135deg, #4775ff, #5e4bed); }
body.dark .board { background: linear-gradient(145deg, rgba(33,42,67,.72), rgba(21,25,46,.63)); }
body.dark .game-message { background: rgba(14,21,38,.43); }

@keyframes float { to { transform: translate3d(5vw, -3vh, 0) scale(1.08); } }
@keyframes tile-appear { from { opacity: 0; transform: scale(.48); } to { opacity: 1; transform: scale(1); } }
@keyframes tile-merge { 0% { transform: scale(1); } 48% { transform: scale(1.15); } 100% { transform: scale(1); } }
@keyframes score-pop { 0% { opacity: 0; transform: translateY(6px); } 35% { opacity: 1; transform: translateY(-3px); } 100% { opacity: 0; transform: translateY(-14px); } }
@keyframes dialog-in { from { opacity: 0; transform: translateY(10px) scale(.96); } }

@media (max-width: 760px) {
  .topbar { top: 10px; margin-top: 10px; width: calc(100% - 20px); }
  .page-shell { width: min(560px, calc(100% - 24px)); padding-top: 30px; }
  .intro { grid-template-columns: 1fr; gap: 18px; align-items: start; margin-bottom: 18px; }
  .eyebrow { margin-bottom: 10px; }
  .intro__copy > p:last-child { margin-top: 12px; }
  .score-cluster { width: 100%; }
  .score-card { flex: 1; }
  .game-layout { grid-template-columns: 1fr; gap: 24px; }
  .side-panel { padding-top: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
  .tip-card { grid-column: 1 / -1; }
  .how-to { padding: 20px 8px; }
  .progress-card { align-self: end; margin-bottom: 20px; }
  footer { width: calc(100% - 32px); }
}

@media (max-width: 480px) {
  .page-shell { padding-top: 24px; }
  h1 { font-size: clamp(39px, 11.2vw, 50px); line-height: .94; }
  .intro { gap: 14px; margin-bottom: 12px; }
  .intro__copy > p:last-child { margin-top: 10px; font-size: 12px; }
  .score-card { min-height: 68px; padding: 11px 15px; border-radius: 19px; }
  .score-card strong { margin-top: 5px; font-size: 24px; }
  .game-toolbar { min-height: 54px; margin-bottom: 9px; padding: 6px 7px 6px 14px; }
  .game-toolbar .action-button { min-height: 40px; }
  .board { border-radius: 25px; }
  .side-panel { grid-template-columns: 1fr; }
  .tip-card { grid-column: auto; }
  .progress-card { margin: 0; }
  .message-actions { flex-direction: column; }
  footer { flex-direction: column; gap: 7px; align-items: center; }
}

/* Short phones need the playable board, not the introduction, above the fold.
   Keep the title and score context, then size the square from the live viewport. */
@media (max-width: 760px) and (max-height: 760px) {
  .topbar {
    top: 6px;
    height: 52px;
    margin-top: 6px;
    padding: 6px 7px 6px 15px;
    border-radius: 20px;
  }
  .brand { gap: 9px; }
  .brand__mark { width: 24px; height: 24px; }
  .icon-button { width: 38px; height: 38px; border-radius: 13px; }
  .page-shell { padding-top: 10px; }
  .intro { gap: 8px; margin-bottom: 8px; }
  .eyebrow,
  .intro__copy > p:last-child { display: none; }
  h1 {
    font-size: clamp(28px, 8.7vw, 34px);
    line-height: 1;
    white-space: nowrap;
  }
  .score-cluster { gap: 8px; }
  .score-card {
    min-height: 54px;
    padding: 7px 12px;
    border-radius: 16px;
  }
  .score-card span { font-size: 9px; }
  .score-card strong { margin-top: 3px; font-size: 20px; }
  .game-toolbar {
    min-height: 48px;
    margin-bottom: 6px;
    padding: 5px 6px 5px 12px;
    border-radius: 18px;
  }
  .game-toolbar .action-button {
    min-height: 36px;
    padding-inline: 13px;
    border-radius: 13px;
  }
  .board {
    width: min(100%, calc(100dvh - 250px));
    margin-inline: auto;
    border-radius: 22px;
  }
  .swipe-hint { margin-top: 8px; }
}

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