@import url('https://fonts.googleapis.com/css2?family=Manrope:wght@500;600;700;800&display=swap');

:root {
  color-scheme: dark;
  --ink: #f7f8ff;
  --muted: #99a2bd;
  --panel: rgba(19, 27, 50, 0.82);
  --line: rgba(255, 255, 255, 0.09);
  --accent: #7f8cff;
  --accent-soft: rgba(127, 140, 255, .12);
  --board-gap: clamp(8px, 2.3vw, 14px);
  --radius: 26px;
}

[data-theme="light"] {
  color-scheme: light;
  --ink: #3e4052;
  --muted: #7b7d8f;
  --panel: rgba(255, 255, 255, .72);
  --line: rgba(76, 73, 101, .13);
  --accent: #7774c9;
  --accent-soft: rgba(130, 123, 206, .14);
}

* { box-sizing: border-box; }

html { min-height: 100%; }

body {
  min-height: 100vh;
  margin: 0;
  overflow-x: hidden;
  font-family: "Manrope", system-ui, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 15% 15%, rgba(91, 102, 255, 0.22), transparent 30rem),
    radial-gradient(circle at 85% 80%, rgba(13, 218, 176, 0.13), transparent 30rem),
    #090e1d;
  transition: color .28s ease, background-color .28s ease;
}

[data-theme="light"] body {
  background:
    radial-gradient(circle at 13% 14%, rgba(202, 195, 245, .72), transparent 31rem),
    radial-gradient(circle at 88% 78%, rgba(190, 235, 221, .65), transparent 30rem),
    #f7f3ef;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0.24;
  background-image: linear-gradient(rgba(255,255,255,.025) 1px, transparent 1px), linear-gradient(90deg, rgba(255,255,255,.025) 1px, transparent 1px);
  background-size: 44px 44px;
  mask-image: linear-gradient(to bottom, black, transparent 85%);
}

[data-theme="light"] body::before {
  opacity: .38;
  background-image: linear-gradient(rgba(91,84,120,.035) 1px, transparent 1px), linear-gradient(90deg, rgba(91,84,120,.035) 1px, transparent 1px);
}

button { font: inherit; }
button:focus-visible, .board:focus-visible { outline: 3px solid var(--accent); outline-offset: 4px; }

.app-shell {
  position: relative;
  width: min(calc(100% - 32px), 620px);
  margin: 0 auto;
  padding: 30px 0 24px;
}

.topbar, .game-intro, .controls { display: flex; align-items: center; justify-content: space-between; gap: 20px; }
.header-actions { display: flex; align-items: center; gap: 10px; }

.brand { display: flex; align-items: center; gap: 14px; }
.brand-mark {
  font-size: clamp(2.15rem, 8vw, 3.25rem);
  font-weight: 800;
  letter-spacing: -0.1em;
  line-height: 1;
}
.brand-mark span { color: var(--accent); }
.brand p { margin: 0; color: var(--muted); font-size: .74rem; font-weight: 700; line-height: 1.45; text-transform: uppercase; letter-spacing: .07em; }

.scoreboard { display: flex; gap: 8px; }
.score-card {
  position: relative;
  min-width: 88px;
  padding: 9px 14px 10px;
  border: 1px solid var(--line);
  border-radius: 14px;
  text-align: center;
  background: rgba(255,255,255,.055);
  box-shadow: inset 0 1px rgba(255,255,255,.04);
  transition: background-color .28s ease, border-color .28s ease, box-shadow .28s ease;
}
.score-card span { display: block; color: var(--muted); font-size: .66rem; font-weight: 800; letter-spacing: .09em; text-transform: uppercase; }
.score-card strong { display: block; margin-top: 2px; font-size: 1.25rem; line-height: 1; }
.score-card.best { background: var(--accent-soft); }
#scoreAddition { position: absolute; left: 50%; top: 60%; color: #7cf5d2; font-style: normal; font-weight: 800; opacity: 0; pointer-events: none; }
#scoreAddition.pop { animation: score-pop .75s ease-out; }

.game-intro { margin: 32px 0 20px; align-items: flex-end; }
.game-intro h1 { margin: 0 0 6px; font-size: clamp(1.35rem, 5vw, 1.85rem); line-height: 1.15; letter-spacing: -.04em; }
.game-intro h1 span { color: var(--accent); }
.game-intro p { max-width: 390px; margin: 0; color: var(--muted); font-size: .86rem; line-height: 1.55; }

.new-game-button, .primary-button, .secondary-button {
  border: 0;
  border-radius: 13px;
  cursor: pointer;
  font-weight: 800;
  transition: transform .16s ease, background-color .16s ease;
}
.theme-toggle, .sound-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 46px;
  padding: 0 13px;
  border: 1px solid var(--line);
  border-radius: 14px;
  color: var(--ink);
  background: rgba(255,255,255,.055);
  cursor: pointer;
  font-size: .72rem;
  font-weight: 800;
  transition: transform .16s ease, color .28s ease, background-color .28s ease, border-color .28s ease;
}
.theme-toggle:hover, .sound-toggle:hover { transform: translateY(-2px); background: rgba(255,255,255,.1); }
.theme-toggle:active, .sound-toggle:active { transform: translateY(0); }
.theme-icon { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.moon-icon { display: none; fill: currentColor; stroke: none; }
[data-theme="light"] .sun-icon { display: none; }
[data-theme="light"] .moon-icon { display: block; }
[data-theme="light"] .theme-toggle, [data-theme="light"] .sound-toggle { background: rgba(255,255,255,.65); box-shadow: 0 6px 18px rgba(91,84,120,.08); }
.sound-toggle { width: 46px; padding: 0; flex: 0 0 46px; }
.sound-icon { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.sound-off-icon { display: none; }
.sound-toggle[data-sound="off"] .sound-on-icon { display: none; }
.sound-toggle[data-sound="off"] .sound-off-icon { display: block; }
.new-game-button { display: inline-flex; align-items: center; flex: 0 0 auto; gap: 8px; padding: 12px 15px; color: #0a1020; background: #f4f5ff; }
.new-game-button:hover { transform: translateY(-2px); }
.new-game-button:active { transform: translateY(0); }
.new-game-button svg { width: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
[data-theme="light"] .new-game-button { color: white; background: #686587; box-shadow: 0 8px 20px rgba(87,82,117,.16); }
[data-theme="light"] .score-card { background: rgba(255,255,255,.62); box-shadow: inset 0 1px rgba(255,255,255,.7), 0 6px 18px rgba(91,84,120,.06); }
[data-theme="light"] .score-card.best { background: rgba(226, 221, 249, .72); }

.game-area { position: relative; }
.board {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--board-gap);
  width: 100%;
  aspect-ratio: 1;
  padding: var(--board-gap);
  touch-action: none;
  user-select: none;
  border: 1px solid rgba(255,255,255,.1);
  border-radius: var(--radius);
  background: linear-gradient(145deg, rgba(29, 39, 70, .96), rgba(13, 20, 39, .96));
  box-shadow: 0 28px 70px rgba(0, 0, 0, .32), inset 0 1px rgba(255,255,255,.06);
  transition: background .28s ease, border-color .28s ease, box-shadow .28s ease;
}

[data-theme="light"] .board {
  border-color: rgba(91, 84, 120, .1);
  background: linear-gradient(145deg, rgba(216, 216, 238, .96), rgba(225, 219, 238, .96));
  box-shadow: 0 28px 60px rgba(93, 86, 119, .16), inset 0 1px rgba(255,255,255,.72);
}

.cell {
  display: grid;
  place-items: center;
  min-width: 0;
  aspect-ratio: 1;
  border-radius: clamp(12px, 3vw, 19px);
  background: rgba(255,255,255,.055);
  box-shadow: inset 0 1px 4px rgba(0,0,0,.12);
  transition: background-color .28s ease, box-shadow .28s ease;
}

[data-theme="light"] .cell { background: rgba(255,255,255,.42); box-shadow: inset 0 1px 3px rgba(92,84,119,.07); }

.tile {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  border-radius: inherit;
  color: #11172a;
  font-size: clamp(1.75rem, 8vw, 3.65rem);
  font-weight: 800;
  letter-spacing: -.07em;
  background: linear-gradient(145deg, #edf0ff, #cbd1ef);
  box-shadow: inset 0 1px rgba(255,255,255,.6), 0 9px 20px rgba(0,0,0,.18);
  animation: tile-appear .18s cubic-bezier(.2,.9,.25,1.25);
}
.tile[data-value="4"] { background: linear-gradient(145deg, #dbe3ff, #aebdff); }
.tile[data-value="8"] { background: linear-gradient(145deg, #bfc5ff, #858fff); }
.tile[data-value="16"] { color: white; background: linear-gradient(145deg, #939cff, #6974ed); }
.tile[data-value="32"] { color: white; background: linear-gradient(145deg, #b084ff, #8053e7); }
.tile[data-value="64"] { color: white; background: linear-gradient(145deg, #e077e9, #ad43bd); }
.tile[data-value="128"] { color: white; background: linear-gradient(145deg, #ff7bb3, #dd467f); font-size: clamp(1.45rem, 7vw, 3.25rem); }
.tile[data-value="256"] { color: white; background: linear-gradient(145deg, #ff9172, #e85f48); font-size: clamp(1.45rem, 7vw, 3.25rem); }
.tile[data-value="512"] { color: #241b00; background: linear-gradient(145deg, #ffd66c, #f0a928); font-size: clamp(1.45rem, 7vw, 3.25rem); }
.tile[data-value="1024"] { color: #102018; background: linear-gradient(145deg, #88f0c7, #36bd8c); font-size: clamp(1.2rem, 6vw, 2.65rem); }
.tile[data-value="2048"] { color: white; background: linear-gradient(145deg, #34dfd0, #09a39c); font-size: clamp(1.2rem, 6vw, 2.65rem); box-shadow: 0 0 32px rgba(52,223,208,.35); }
.tile.super { color: white; background: linear-gradient(145deg, #202a4b, #11172e); font-size: clamp(1rem, 5vw, 2.25rem); }

.game-message {
  position: absolute;
  z-index: 5;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  border-radius: var(--radius);
  background: rgba(7, 11, 24, .76);
  backdrop-filter: blur(11px);
  animation: message-in .24s ease-out;
}
[data-theme="light"] .game-message { background: rgba(241, 236, 240, .75); }
.game-message[hidden] { display: none; }
.message-card { width: min(100%, 370px); padding: 28px; border: 1px solid var(--line); border-radius: 24px; text-align: center; background: rgba(20, 29, 54, .95); box-shadow: 0 22px 60px rgba(0,0,0,.3); }
.message-icon { display: grid; place-items: center; width: 58px; height: 58px; margin: 0 auto 16px; border-radius: 18px; color: #11172a; font-size: 1.8rem; background: #86f1d0; box-shadow: 0 0 30px rgba(134,241,208,.18); }
.message-card .eyebrow { margin: 0 0 5px; color: #8d98ff; font-size: .72rem; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; }
.message-card h2 { margin: 0; font-size: 2rem; letter-spacing: -.05em; }
.message-card > p:not(.eyebrow) { margin: 8px 0 22px; color: var(--muted); font-size: .9rem; line-height: 1.55; }
.message-actions { display: flex; gap: 9px; }
.message-actions button { flex: 1; padding: 12px; }
.primary-button { color: #11172a; background: white; }
.secondary-button { color: white; background: rgba(255,255,255,.09); }
.primary-button:hover, .secondary-button:hover { transform: translateY(-1px); }
[data-theme="light"] .message-card { background: rgba(255, 252, 249, .96); box-shadow: 0 22px 60px rgba(91,84,120,.18); }
[data-theme="light"] .message-card .eyebrow { color: var(--accent); }
[data-theme="light"] .primary-button { color: white; background: #686587; }
[data-theme="light"] .secondary-button { color: var(--ink); background: #ebe7f5; }

.controls { margin-top: 22px; padding: 17px 19px; border: 1px solid var(--line); border-radius: 18px; background: rgba(255,255,255,.035); }
.keyboard-hint, .gesture-hint { display: flex; align-items: center; gap: 13px; }
.controls p { margin: 0; color: var(--muted); font-size: .7rem; line-height: 1.55; }
.controls strong { color: #dce0f2; font-size: .73rem; }
.key-grid { display: grid; grid-template-columns: repeat(3, 23px); gap: 3px; }
kbd { display: grid; place-items: center; height: 21px; border: 1px solid rgba(255,255,255,.12); border-radius: 5px; color: #c8cee6; font: 600 .65rem/1 inherit; background: rgba(255,255,255,.07); box-shadow: 0 2px rgba(0,0,0,.2); }
.swipe-icon { display: grid; place-items: center; width: 46px; height: 46px; border-radius: 14px; color: #87edd0; font-size: 1.45rem; background: rgba(77,226,184,.09); }

[data-theme="light"] .controls { background: rgba(255,255,255,.48); box-shadow: 0 8px 24px rgba(91,84,120,.05); }
[data-theme="light"] .controls strong { color: #535466; }
[data-theme="light"] kbd { border-color: rgba(86,82,112,.14); color: #65667a; background: rgba(255,255,255,.7); box-shadow: 0 2px rgba(91,84,120,.1); }
[data-theme="light"] .swipe-icon { color: #4d9f89; background: rgba(167,221,205,.34); }
[data-theme="light"] footer { color: #9995a5; }

footer { padding-top: 22px; color: #59627c; text-align: center; font-size: .68rem; }

@keyframes tile-appear { from { opacity: .25; transform: scale(.78); } to { opacity: 1; transform: scale(1); } }
@keyframes score-pop { 0% { opacity: 0; transform: translate(-50%,0); } 30% { opacity: 1; } 100% { opacity: 0; transform: translate(-50%,-34px); } }
@keyframes message-in { from { opacity: 0; } to { opacity: 1; } }

@media (max-width: 570px) {
  .app-shell { width: min(calc(100% - 22px), 500px); padding-top: max(18px, env(safe-area-inset-top)); }
  .brand p { display: none; }
  .score-card { min-width: 74px; padding-inline: 10px; }
  .game-intro { margin-top: 24px; }
  .game-intro p { font-size: .76rem; }
  .new-game-button { width: 44px; height: 44px; padding: 0; justify-content: center; font-size: 0; }
  .new-game-button svg { width: 20px; }
  .header-actions { gap: 7px; }
  .theme-toggle { width: 44px; min-height: 44px; padding: 0; }
  .sound-toggle { width: 44px; min-height: 44px; flex-basis: 44px; }
  .theme-toggle span { display: none; }
  .controls { justify-content: center; }
  .keyboard-hint { display: none; }
}

@media (max-width: 380px) {
  .score-card { min-width: 66px; }
  .header-actions { gap: 5px; }
  .theme-toggle { width: 40px; min-height: 40px; }
  .sound-toggle { width: 40px; min-height: 40px; flex-basis: 40px; }
  .game-intro h1 { font-size: 1.17rem; }
  .game-intro p { max-width: 240px; }
}

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