/* ============ 主题变量 ============ */
:root,
[data-theme="light"] {
  --bg: #f6f1e7;
  --bg-1: #fdfaf3;
  --bg-2: #efe5d4;
  --surface: #ffffff;
  --board: #bbada0;
  --cell: #cdc1b4;
  --cell-blocked: #a5958a;
  --text: #5b524b;
  --text-soft: #93887e;
  --text-light: #f9f6f2;
  --accent: #8f7a66;
  --accent-strong: #7a6652;
  --line: rgba(120, 100, 80, .14);
  --seg-bg: rgba(120, 100, 80, .09);
  --shadow: 0 20px 44px rgba(90, 70, 50, .18);
  --tile-shadow: 0 2px 7px rgba(80, 60, 40, .2);
  --overlay: rgba(250, 248, 239, .82);
  --t2: #eee4da; --t2c: #776e65;
  --t4: #ede0c8; --t4c: #776e65;
  --dim: #8d8279;
}
[data-theme="dark"] {
  --bg: #111419;
  --bg-1: #181d24;
  --bg-2: #0b0e12;
  --surface: #1b2027;
  --board: #272c34;
  --cell: #31373f;
  --cell-blocked: #1c2127;
  --text: #e8ebf0;
  --text-soft: #97a0ac;
  --text-light: #f9f6f2;
  --accent: #7f8ea6;
  --accent-strong: #93a2ba;
  --line: rgba(255, 255, 255, .1);
  --seg-bg: rgba(255, 255, 255, .07);
  --shadow: 0 20px 44px rgba(0, 0, 0, .55);
  --tile-shadow: 0 2px 8px rgba(0, 0, 0, .45);
  --overlay: rgba(17, 20, 25, .84);
  --t2: #3a4048; --t2c: #d3dae3;
  --t4: #444b55; --t4c: #dfe6ee;
  --dim: #8b94a1;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
}

body {
  background: radial-gradient(120% 80% at 50% 0%, var(--bg-1) 0%, var(--bg) 45%, var(--bg-2) 100%);
  color: var(--text);
  font-family: "SF Pro Display", -apple-system, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  transition: background-color .3s ease, color .3s ease;
}

.app {
  height: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) 16px max(10px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ============ 顶部 ============ */
.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.brand { display: flex; flex-direction: column; gap: 4px; }

h1 {
  font-size: clamp(30px, 8vw, 42px);
  font-weight: 800;
  letter-spacing: -1.5px;
  line-height: 1;
  color: var(--text);
}

.chip {
  align-self: flex-start;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .2px;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--seg-bg);
  color: var(--text-soft);
}

.scores { display: flex; gap: 8px; }

.score-box {
  position: relative;
  min-width: 74px;
  padding: 8px 14px;
  border-radius: 14px;
  background: var(--board);
  color: var(--text-light);
  text-align: center;
  box-shadow: var(--tile-shadow);
}
.score-box .label {
  display: block;
  font-size: 10px;
  letter-spacing: 1.5px;
  opacity: .78;
}
.score-box .value {
  display: block;
  margin-top: 2px;
  font-size: 21px;
  font-weight: 800;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}
.score-box.bump .value { animation: bump 340ms cubic-bezier(.2, .9, .3, 1); }

@keyframes bump {
  0% { transform: scale(1); }
  38% { transform: scale(1.3); color: #fff3c9; }
  100% { transform: scale(1); }
}

/* ============ 按钮 ============ */
.toolbar { display: flex; gap: 8px; }

.btn {
  flex: 1;
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  background: var(--seg-bg);
  border: none;
  border-radius: 12px;
  padding: 12px 16px;
  cursor: pointer;
  transition: transform .08s ease, background .18s ease, opacity .18s ease;
}
.btn:active:not(:disabled) { transform: scale(.975); }
.btn:disabled { opacity: .38; cursor: default; }
.btn.primary {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 6px 16px rgba(120, 95, 70, .28);
}
.btn.primary:active:not(:disabled) { background: var(--accent-strong); }
.btn.ghost { background: transparent; border: 1.5px solid var(--line); }
.btn.square { flex: none; width: 48px; padding: 12px 0; font-size: 19px; }
.btn.installed { background: var(--accent); color: #fff; }

/* ============ 棋盘 ============ */
.board-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding-top: 2px;
  gap: 12px;
}

.board {
  position: relative;
  background: var(--board);
  border-radius: 16px;
  touch-action: none;
  overflow: hidden;
  box-shadow: var(--shadow);
  transition: background-color .3s ease;
}

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

.cell-bg {
  position: absolute;
  top: 0; left: 0;
  background: var(--cell);
  border-radius: 8px;
  transition: background-color .3s ease;
}
.cell-bg.blocked {
  background:
    repeating-linear-gradient(45deg, rgba(0, 0, 0, .09) 0 7px, transparent 7px 14px),
    var(--cell-blocked);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .12), inset 0 3px 8px rgba(0, 0, 0, .2);
}

.tile {
  position: absolute;
  top: 0; left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  font-weight: 800;
  font-size: var(--fs);
  line-height: 1;
  background: var(--t2);
  color: var(--t2c);
  transition: transform 110ms ease-in-out, background-color .18s ease;
  z-index: 2;
  box-shadow: var(--tile-shadow);
}

.tile.d3 { font-size: calc(var(--fs) * .78); }
.tile.d4 { font-size: calc(var(--fs) * .58); }
.tile.d5 { font-size: calc(var(--fs) * .46); }

.tile.appear { animation: appear 150ms ease-out; }
@keyframes appear { from { opacity: 0; } to { opacity: 1; } }

.tile.pop { animation: pop 180ms ease-out; }
@keyframes pop {
  0% { transform-origin: center; }
  45% { filter: brightness(1.16); }
  100% { }
}

.v2 { background: var(--t2); color: var(--t2c); }
.v4 { background: var(--t4); color: var(--t4c); }
.v8 { background: #f2b179; color: #fff; }
.v16 { background: #f59563; color: #fff; }
.v32 { background: #f67c5f; color: #fff; }
.v64 { background: #f65e3b; color: #fff; }
.v128 { background: #edcf72; color: #fff; }
.v256 { background: #edcc61; color: #fff; }
.v512 { background: #edc850; color: #fff; }
.v1024 { background: #edc53f; color: #fff; }
.v2048 { background: #edc22e; color: #fff; box-shadow: 0 0 22px rgba(237, 194, 46, .55); }
.v4096 { background: #3f8ce0; color: #fff; }
.v8192 { background: #6a5ae0; color: #fff; }
.v16384 { background: #b04ad1; color: #fff; }
.v32768, .v65536 { background: #2b2f36; color: #fff; }

.statusbar {
  display: flex;
  justify-content: space-between;
  width: 100%;
  max-width: 480px;
  font-size: 12px;
  color: var(--text-soft);
  font-variant-numeric: tabular-nums;
  padding: 0 2px;
}

/* ============ 覆盖层 ============ */
.overlay {
  position: absolute;
  inset: 0;
  background: var(--overlay);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  border-radius: 16px;
  animation: fade .22s ease-out;
}
.overlay[hidden] { display: none; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.overlay-inner { text-align: center; padding: 18px; }
.overlay-title {
  font-size: clamp(24px, 7vw, 34px);
  font-weight: 800;
  letter-spacing: -.5px;
  margin-bottom: 6px;
}
.overlay-title.win { color: #e8b52c; }
.overlay-sub { font-size: 13.5px; color: var(--text-soft); margin-bottom: 18px; line-height: 1.7; }
.overlay-actions { display: flex; gap: 10px; justify-content: center; }
.overlay-actions .btn { flex: none; min-width: 120px; padding: 12px 24px; }

/* ============ 设置面板 ============ */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, .42);
  backdrop-filter: blur(3px);
  animation: fade .2s ease-out;
}
.sheet-backdrop[hidden] { display: none; }

.sheet {
  width: 100%;
  max-width: 480px;
  max-height: 86vh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  padding: 16px 20px calc(22px + env(safe-area-inset-bottom));
  box-shadow: 0 -12px 40px rgba(0, 0, 0, .25);
  animation: slide-up .26s cubic-bezier(.2, .9, .3, 1);
}
@keyframes slide-up { from { transform: translateY(28px); opacity: .6; } to { transform: translateY(0); opacity: 1; } }

@media (min-width: 720px) {
  .board-wrap { justify-content: center; }
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: 22px; max-height: 80vh; }
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 17px;
  font-weight: 800;
  margin-bottom: 6px;
}

.icon-btn {
  width: 34px; height: 34px;
  border: none;
  border-radius: 50%;
  background: var(--seg-bg);
  color: var(--text-soft);
  font-size: 15px;
  cursor: pointer;
}

.group { padding: 14px 0; border-bottom: 1px solid var(--line); }
.group:last-child { border-bottom: none; }
.group.row { display: flex; align-items: center; justify-content: space-between; gap: 16px; }

.group-title { font-size: 13px; font-weight: 700; color: var(--text-soft); margin-bottom: 10px; }
.muted { font-weight: 500; opacity: .7; font-size: 11px; }
.group-tip { font-size: 12px; color: var(--text-soft); line-height: 1.5; margin-top: 8px; }

.seg {
  display: flex;
  gap: 6px;
  padding: 4px;
  border-radius: 12px;
  background: var(--seg-bg);
}
.seg button {
  flex: 1;
  padding: 10px 6px;
  border: none;
  border-radius: 9px;
  background: transparent;
  color: var(--text-soft);
  font: inherit;
  font-size: 13.5px;
  font-weight: 700;
  cursor: pointer;
  transition: background .16s ease, color .16s ease;
}
.seg button.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 2px 8px rgba(0, 0, 0, .1);
}
[data-theme="dark"] .seg button.active { background: #2b313a; }

.switch { position: relative; display: inline-block; width: 50px; height: 30px; flex: none; }
.group.row .btn { flex: none; padding: 10px 20px; font-size: 14px; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: var(--seg-bg);
  cursor: pointer;
  transition: background .2s ease;
}
.switch span::after {
  content: "";
  position: absolute;
  top: 3px; left: 3px;
  width: 24px; height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, .25);
  transition: transform .2s cubic-bezier(.2, .9, .3, 1);
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(20px); }

.leaderboard { list-style: none; display: flex; flex-direction: column; gap: 6px; }
.leaderboard li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-radius: 10px;
  background: var(--seg-bg);
  font-size: 13px;
}
.leaderboard .rank { width: 18px; font-weight: 800; color: var(--text-soft); }
.leaderboard .sc { font-weight: 800; font-variant-numeric: tabular-nums; }
.leaderboard .meta { margin-left: auto; font-size: 11px; color: var(--text-soft); }
.leaderboard .empty { justify-content: center; color: var(--text-soft); font-size: 12px; }

.link-btn {
  margin-top: 10px;
  border: none;
  background: none;
  color: var(--text-soft);
  font: inherit;
  font-size: 12px;
  text-decoration: underline;
  cursor: pointer;
  padding: 4px 0;
}

.version {
  text-align: center;
  font-size: 11px;
  color: var(--text-soft);
  opacity: .62;
  padding: 16px 0 4px;
  letter-spacing: .3px;
}
.version b { font-weight: 700; opacity: .85; }

/* ============ 小屏 ============ */
@media (max-width: 380px) {
  .score-box { min-width: 64px; padding: 7px 10px; }
  .score-box .value { font-size: 18px; }
  .btn { font-size: 14px; padding: 11px 12px; }
}
