/* Themes drive every color via CSS variables; .theme-<id> on <body>. */
.theme-classic {
  --bg: #faf8ef;
  --fg: #776e65;
  --muted: #8f8577;
  --panel: #bbada0;
  --cell: rgba(238, 228, 218, 0.35);
  --accent: #f67c5f;
  --button: #8f7a66;
  --button-fg: #f9f6f2;
  --border: #bbada0;
  --t-2-bg:    #eee4da; --t-2-fg:    #776e65;
  --t-4-bg:    #ede0c8; --t-4-fg:    #776e65;
  --t-8-bg:    #f2b179; --t-8-fg:    #f9f6f2;
  --t-16-bg:   #f59563; --t-16-fg:   #f9f6f2;
  --t-32-bg:   #f67c5f; --t-32-fg:   #f9f6f2;
  --t-64-bg:   #f65e3b; --t-64-fg:   #f9f6f2;
  --t-128-bg:  #edcf72; --t-128-fg:  #f9f6f2;
  --t-256-bg:  #edcc61; --t-256-fg:  #f9f6f2;
  --t-512-bg:  #edc850; --t-512-fg:  #f9f6f2;
  --t-1024-bg: #edc53f; --t-1024-fg: #f9f6f2;
  --t-2048-bg: #edc22e; --t-2048-fg: #f9f6f2;
  --t-super-bg:#3c3a32; --t-super-fg:#f9f6f2;
}

.theme-dark {
  --bg: #0f1115;
  --fg: #e6e8ec;
  --muted: #8a93a6;
  --panel: #1e222b;
  --cell: #272b35;
  --accent: #9ae6b4;
  --button: #272b35;
  --button-fg: #e6e8ec;
  --border: #272b35;
  --t-2-bg:    #2a3042; --t-2-fg:    #e6e8ec;
  --t-4-bg:    #35406a; --t-4-fg:    #e6e8ec;
  --t-8-bg:    #4453a3; --t-8-fg:    #f9f6f2;
  --t-16-bg:   #5465c7; --t-16-fg:   #f9f6f2;
  --t-32-bg:   #637de2; --t-32-fg:   #f9f6f2;
  --t-64-bg:   #51a3d0; --t-64-fg:   #f9f6f2;
  --t-128-bg:  #3fb79b; --t-128-fg:  #f9f6f2;
  --t-256-bg:  #4cc98a; --t-256-fg:  #f9f6f2;
  --t-512-bg:  #a6d95c; --t-512-fg:  #0f1115;
  --t-1024-bg: #e6d65c; --t-1024-fg: #0f1115;
  --t-2048-bg: #f7b955; --t-2048-fg: #0f1115;
  --t-super-bg:#f67c5f; --t-super-fg:#f9f6f2;
}

.theme-pastel {
  --bg: #fff6f8;
  --fg: #5b4a52;
  --muted: #a59098;
  --panel: #f6d6de;
  --cell: rgba(255, 255, 255, 0.55);
  --accent: #e57ea0;
  --button: #e57ea0;
  --button-fg: #fff;
  --border: #f6d6de;
  --t-2-bg:    #fce4ec; --t-2-fg:    #7a5a64;
  --t-4-bg:    #f8bbd0; --t-4-fg:    #7a5a64;
  --t-8-bg:    #f48fb1; --t-8-fg:    #fff;
  --t-16-bg:   #f06292; --t-16-fg:   #fff;
  --t-32-bg:   #ec407a; --t-32-fg:   #fff;
  --t-64-bg:   #d81b60; --t-64-fg:   #fff;
  --t-128-bg:  #c8e6c9; --t-128-fg:  #2e5030;
  --t-256-bg:  #a5d6a7; --t-256-fg:  #2e5030;
  --t-512-bg:  #81d4fa; --t-512-fg:  #154560;
  --t-1024-bg: #4fc3f7; --t-1024-fg: #154560;
  --t-2048-bg: #ffd54f; --t-2048-fg: #5b4a52;
  --t-super-bg:#5b4a52; --t-super-fg:#fff6f8;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans",
    "Hiragino Kaku Gothic ProN", Meiryo, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}

header {
  padding: 24px clamp(16px, 4vw, 32px) 12px;
  max-width: 560px;
  margin: 0 auto;
}

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

header h1 {
  margin: 0;
  font-size: clamp(32px, 8vw, 48px);
  font-weight: 800;
  letter-spacing: -0.02em;
}

.scores {
  display: flex;
  gap: 8px;
}
.score-box {
  min-width: 72px;
  padding: 8px 12px;
  background: var(--panel);
  border-radius: 6px;
  text-align: center;
  color: var(--button-fg);
  display: flex;
  flex-direction: column;
}
.score-box span {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  opacity: 0.85;
}
.score-box strong {
  font-size: 18px;
  font-family: "SF Mono", Menlo, Consolas, monospace;
}

#subtitle {
  margin: 8px 0 16px;
  color: var(--muted);
  font-size: 14px;
}

.top-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
}

button {
  padding: 10px 16px;
  background: var(--button);
  color: var(--button-fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
}
button:hover { filter: brightness(1.05); }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--button-fg);
}

.group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
label {
  font-size: 10px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
}
select {
  padding: 8px 10px;
  background: var(--panel);
  color: var(--button-fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
}

main {
  padding: 8px clamp(16px, 4vw, 32px) 32px;
  max-width: 560px;
  margin: 0 auto;
}

.board-wrap {
  position: relative;
  margin-top: 8px;
}

.board {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: clamp(8px, 2vw, 14px);
  padding: clamp(8px, 2vw, 14px);
  background: var(--panel);
  border-radius: 10px;
  aspect-ratio: 1 / 1;
  touch-action: none;
  user-select: none;
}

.cell {
  background: var(--cell);
  border-radius: 6px;
  position: relative;
}

.tile {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  font-weight: 800;
  font-size: clamp(20px, 6vw, 38px);
  font-family: "SF Mono", Menlo, Consolas, monospace;
  animation: pop 140ms ease-out;
}

@keyframes pop {
  from { transform: scale(0.85); }
  to { transform: scale(1); }
}

.tile-2    { background: var(--t-2-bg);    color: var(--t-2-fg); }
.tile-4    { background: var(--t-4-bg);    color: var(--t-4-fg); }
.tile-8    { background: var(--t-8-bg);    color: var(--t-8-fg); }
.tile-16   { background: var(--t-16-bg);   color: var(--t-16-fg); }
.tile-32   { background: var(--t-32-bg);   color: var(--t-32-fg); }
.tile-64   { background: var(--t-64-bg);   color: var(--t-64-fg); }
.tile-128  { background: var(--t-128-bg);  color: var(--t-128-fg); font-size: clamp(16px, 5vw, 32px); }
.tile-256  { background: var(--t-256-bg);  color: var(--t-256-fg); font-size: clamp(16px, 5vw, 32px); }
.tile-512  { background: var(--t-512-bg);  color: var(--t-512-fg); font-size: clamp(16px, 5vw, 32px); }
.tile-1024 { background: var(--t-1024-bg); color: var(--t-1024-fg); font-size: clamp(14px, 4.4vw, 28px); }
.tile-2048 { background: var(--t-2048-bg); color: var(--t-2048-fg); font-size: clamp(14px, 4.4vw, 28px); }
.tile-super{ background: var(--t-super-bg);color: var(--t-super-fg);font-size: clamp(14px, 4.4vw, 28px); }

.overlay[hidden] { display: none; }
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(238, 228, 218, 0.72);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  border-radius: 10px;
  backdrop-filter: blur(2px);
}
.theme-dark .overlay { background: rgba(15, 17, 21, 0.75); }
.theme-pastel .overlay { background: rgba(255, 246, 248, 0.78); }
.overlay p {
  margin: 0;
  font-size: clamp(24px, 6vw, 36px);
  font-weight: 800;
  color: var(--fg);
}

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

footer {
  padding: 20px 32px;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}
