:root {
  color-scheme: light dark;
  --bg: #f3f3f3;
  --panel: #ffffff;
  --panel-soft: rgba(255, 255, 255, 0.78);
  --text: #050505;
  --muted: #747781;
  --line: #dedede;
  --shadow: 0 22px 55px rgba(20, 24, 32, 0.12);
  --x: #ff406d;
  --danger: #ff4d5f;
  --success: #26ad61;
  --blue: #4a93ff;
  --tile: #ffffff;
}

body.dark {
  --bg: #13151b;
  --panel: #1c2029;
  --panel-soft: rgba(28, 32, 41, 0.86);
  --text: #f6f7fb;
  --muted: #a0a6b5;
  --line: #323846;
  --shadow: 0 22px 55px rgba(0, 0, 0, 0.34);
  --tile: #202530;
}

@media (prefers-color-scheme: dark) {
  body:not(.light) {
    --bg: #13151b;
    --panel: #1c2029;
    --panel-soft: rgba(28, 32, 41, 0.86);
    --text: #f6f7fb;
    --muted: #a0a6b5;
    --line: #323846;
    --shadow: 0 22px 55px rgba(0, 0, 0, 0.34);
    --tile: #202530;
  }
}

* { box-sizing: border-box; }

body {
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
  background:
    radial-gradient(circle at top, rgba(255, 64, 109, 0.1), transparent 34rem),
    var(--bg);
}

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

.app-shell {
  width: min(100% - 28px, 1120px);
  min-height: 100vh;
  margin: 0 auto;
  padding: 20px 0 22px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.brand {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-bottom: 22px;
}

.brand-icon {
  width: clamp(3rem, 10vw, 4rem);
  height: clamp(3rem, 10vw, 4rem);
  display: grid;
  place-items: center;
  color: var(--x);
  filter: drop-shadow(0 8px 10px rgba(255, 64, 109, 0.25));
}

.brand-icon svg {
  width: 100%;
  height: 100%;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.1;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.brand h1 {
  margin: 0;
  font-size: clamp(2.6rem, 8vw, 3.55rem);
  line-height: 1;
  font-weight: 900;
  letter-spacing: 0;
}

.game-card {
  width: 100%;
  display: grid;
  grid-template-columns: 330px minmax(0, 1fr);
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: var(--panel-soft);
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px);
}

.side-panel, .board-panel {
  padding: clamp(18px, 3vw, 28px);
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 18px;
  min-width: 0;
  border-right: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 84%, transparent);
}

.kicker {
  margin: 0 0 6px;
  color: var(--x);
  font-size: 0.76rem;
  font-weight: 900;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

h2 {
  margin: 0 0 14px;
  font-size: 1.45rem;
  line-height: 1.1;
}

.status-text {
  min-height: 64px;
  margin: 0;
  padding: 13px 14px;
  color: var(--muted);
  line-height: 1.35;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
}

.status-text[data-tone="success"] { color: var(--success); }
.status-text[data-tone="danger"] { color: var(--danger); }

.toolbar {
  display: grid;
  gap: 10px;
}

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

.tool-button,
.back-home-btn {
  width: 100%;
  min-width: 0;
  min-height: 46px;
  border: 1px solid var(--line);
  border-radius: 11px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  padding: 0 12px;
  color: var(--text);
  font-weight: 850;
  background: var(--panel);
  box-shadow: 0 8px 22px rgba(15, 18, 25, 0.08);
  cursor: pointer;
  text-decoration: none;
}

.tool-icon {
  width: 18px;
  height: 18px;
  flex: 0 0 18px;
  fill: none;
  stroke: color-mix(in srgb, var(--text) 72%, transparent);
  stroke-width: 2.1;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.primary-action {
  color: #fff;
  border-color: rgba(255, 64, 109, 0.35);
  background: linear-gradient(135deg, var(--x), #ff6d8b);
}

.primary-action .tool-icon { stroke: #fff; }

.stats {
  display: grid;
  gap: 9px;
  margin-top: auto;
}

.stats div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 48px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: var(--panel);
}

.stats span {
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 700;
}

.hint-text {
  margin: 0;
  color: var(--muted);
  text-align: center;
  line-height: 1.35;
}

.leaderboard-panel {
  display: grid;
  gap: 10px;
  padding: 13px;
  border: 1px solid var(--line);
  border-radius: 15px;
  background: var(--panel);
}

.leaderboard-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.leaderboard-head h3 {
  margin: 0;
  font-size: 1rem;
}

.leaderboard-head span {
  color: var(--x);
  font-size: 0.82rem;
  font-weight: 900;
}

.player-name-wrap {
  display: grid;
  gap: 6px;
}

.player-name-wrap span {
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 800;
}

.player-name-wrap input {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 0 12px;
  color: var(--text);
  background: color-mix(in srgb, var(--panel) 88%, var(--bg));
  outline: none;
}

.player-name-wrap input:focus {
  border-color: rgba(255, 64, 109, 0.8);
  box-shadow: 0 0 0 4px rgba(255, 64, 109, 0.12);
}

.leaderboard-list {
  display: grid;
  gap: 7px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.leaderboard-list li {
  display: grid;
  grid-template-columns: 28px 1fr auto;
  align-items: center;
  gap: 8px;
  min-height: 34px;
  padding: 0 10px;
  border-radius: 10px;
  background: color-mix(in srgb, var(--panel) 84%, var(--bg));
}

.leaderboard-list .rank {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 999px;
  color: #fff;
  background: var(--x);
  font-size: 0.78rem;
  font-weight: 900;
}

.leaderboard-list strong,
.leaderboard-list span:last-child {
  font-size: 0.9rem;
}

.leaderboard-empty {
  grid-template-columns: 1fr !important;
  color: var(--muted);
  text-align: center;
}

.leaderboard-status {
  min-height: 18px;
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
  text-align: center;
}

.board-panel {
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 16px;
}

.board-topline {
  width: min(100%, 540px);
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 0.92rem;
}

.board-2048 {
  width: min(100%, 540px);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  padding: 12px;
  border: 4px solid color-mix(in srgb, var(--text) 42%, var(--line));
  border-radius: 20px;
  background: color-mix(in srgb, var(--panel) 72%, var(--bg));
  box-shadow: 0 16px 34px rgba(15, 18, 25, 0.13);
  touch-action: none;
}

.tile {
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--tile);
  color: var(--text);
  font-size: clamp(1.5rem, 6vw, 2.8rem);
  font-weight: 900;
  transition: transform 120ms ease;
}

.tile.merged { transform: scale(1.04); }
.tile-empty { color: transparent; background: color-mix(in srgb, var(--panel) 64%, var(--bg)); }
.tile-2 { background: #fff4f6; color: #22252d; }
.tile-4 { background: #ffe7ed; color: #22252d; }
.tile-8 { background: #b9d6ff; color: #0f172a; }
.tile-16 { background: #86b9ff; color: #0f172a; }
.tile-32 { background: #ff8aa5; color: #fff; }
.tile-64 { background: #ff5f85; color: #fff; }
.tile-128 { background: #46d18b; color: #fff; }
.tile-256 { background: #26ad61; color: #fff; }
.tile-512 { background: #7b61ff; color: #fff; }
.tile-1024 { background: #5b4bd8; color: #fff; font-size: clamp(1.1rem, 4.5vw, 2.25rem); }
.tile-2048 { background: #ff406d; color: #fff; font-size: clamp(1.1rem, 4.5vw, 2.25rem); }
.tile-super { background: #111827; color: #fff; font-size: clamp(1rem, 4vw, 2rem); }

.mobile-controls {
  width: min(100%, 310px);
  display: grid;
  justify-items: center;
  gap: 8px;
}

.pad-row {
  display: grid;
  grid-template-columns: repeat(3, 54px);
  gap: 8px;
}

.pad-button {
  width: 54px;
  height: 48px;
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--text);
  background: var(--panel);
  box-shadow: 0 8px 20px rgba(15, 18, 25, 0.08);
  cursor: pointer;
  display: grid;
  place-items: center;
}

.pad-button svg {
  width: 23px;
  height: 23px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.footer {
  padding: 18px 0 0;
  color: var(--muted);
  text-align: center;
}

@media (max-width: 840px) {
  .app-shell { width: min(100% - 20px, 540px); }
  .game-card { grid-template-columns: 1fr; }
  .side-panel {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .board-topline {
    flex-direction: column;
    text-align: center;
  }
}

@media (max-width: 520px) {
  .tool-row { grid-template-columns: 1fr; }
  .board-2048 {
    gap: 8px;
    padding: 8px;
    border-radius: 16px;
  }
}
