@import url(fonts/clear-sans.css);

/* ── Theme tokens ── */
:root,
[data-theme="classic"] {
  --bg: #faf8ef;
  --text: #776e65;
  --text-light: #f9f6f2;
  --grid-bg: #bbada0;
  --cell-bg: rgba(238, 228, 218, 0.35);
  --score-bg: #bbada0;
  --score-label: #eee4da;
  --btn-bg: #8f7a66;
  --btn-text: #f9f6f2;
  --overlay: rgba(238, 228, 218, 0.55);
  --overlay-win: rgba(237, 194, 46, 0.55);
  --border: #d8d4d0;
  --link: #776e65;
  --panel-bg: rgba(255, 255, 255, 0.5);
  --tile-2: #eee4da;
  --tile-4: #ede0c8;
  --tile-8: #f2b179;
  --tile-16: #f59563;
  --tile-32: #f67c5f;
  --tile-64: #f65e3b;
  --tile-128: #edcf72;
  --tile-256: #edcc61;
  --tile-512: #edc850;
  --tile-1024: #edc53f;
  --tile-2048: #edc22e;
  --tile-super: #3c3a32;
  --topbar-link: #776e65;
}

[data-theme="arcadebox"] {
  --bg: #06060f;
  --text: #c8cce0;
  --text-light: #f4f6ff;
  --grid-bg: rgba(124, 92, 255, 0.22);
  --cell-bg: rgba(255, 255, 255, 0.06);
  --score-bg: rgba(124, 92, 255, 0.35);
  --score-label: #a78bfa;
  --btn-bg: linear-gradient(135deg, #7c5cff, #ff4d8d);
  --btn-text: #fff;
  --overlay: rgba(6, 6, 15, 0.72);
  --overlay-win: rgba(124, 92, 255, 0.55);
  --border: rgba(255, 255, 255, 0.1);
  --link: #a78bfa;
  --panel-bg: rgba(255, 255, 255, 0.04);
  --tile-2: #2a2840;
  --tile-4: #3d3560;
  --tile-8: #7c5cff;
  --tile-16: #9b6dff;
  --tile-32: #b44dff;
  --tile-64: #d946ef;
  --tile-128: #ec4899;
  --tile-256: #f43f5e;
  --tile-512: #fb7185;
  --tile-1024: #fbbf24;
  --tile-2048: #34d399;
  --tile-super: linear-gradient(135deg, #7c5cff, #00d4aa);
  --topbar-link: #a78bfa;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Clear Sans", "Noto Sans SC", "Helvetica Neue", Arial, sans-serif;
  font-size: 18px;
  transition: background 0.25s ease, color 0.25s ease;
}

body {
  margin: 0;
  padding: 1rem 0 2rem;
}

.page-wrapper {
  max-width: 540px;
  margin: 0 auto;
  padding: 0 1rem;
}

/* ── Top bar ── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
  gap: 0.75rem;
}

.back-link {
  color: var(--topbar-link);
  text-decoration: none;
  font-weight: 700;
  font-size: 0.9rem;
}

.back-link:hover {
  text-decoration: underline;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.lang-switch {
  display: flex;
  padding: 3px;
  border-radius: 999px;
  background: var(--panel-bg);
  border: 1px solid var(--border);
}

.lang-switch button {
  padding: 0.3rem 0.65rem;
  border: none;
  border-radius: 999px;
  background: transparent;
  color: var(--text);
  font-size: 0.75rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  opacity: 0.65;
}

.lang-switch button.active {
  background: var(--btn-bg);
  color: var(--btn-text);
  opacity: 1;
}

.theme-toggle {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--panel-bg);
  color: var(--text);
  font-size: 1rem;
  cursor: pointer;
  line-height: 1;
}

/* ── Heading & scores ── */
.heading:after {
  content: "";
  display: block;
  clear: both;
}

h1.title {
  font-size: clamp(2.5rem, 12vw, 4.5rem);
  font-weight: bold;
  margin: 0;
  float: left;
  line-height: 1;
}

.scores-container {
  float: right;
  text-align: right;
  display: flex;
  gap: 0.5rem;
}

.score-container,
.best-container {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--score-bg);
  padding: 0.5rem 1rem;
  min-width: 4.5rem;
  border-radius: 6px;
  color: var(--text-light);
}

.score-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--score-label);
  line-height: 1.2;
}

.score-value {
  font-size: 1.35rem;
  font-weight: bold;
  line-height: 1.3;
}

.score-container .score-addition {
  position: absolute;
  right: 0.5rem;
  top: 50%;
  font-size: 1rem;
  font-weight: bold;
  color: var(--text);
  z-index: 10;
  animation: move-up 600ms ease-in both;
}

@keyframes move-up {
  0% { transform: translateY(0); opacity: 1; }
  100% { transform: translateY(-2rem); opacity: 0; }
}

/* ── Above game ── */
.above-game {
  margin-top: 1rem;
  clear: both;
}

.above-game:after {
  content: "";
  display: block;
  clear: both;
}

.game-intro {
  margin: 0 0 0.75rem;
  line-height: 1.5;
}

.action-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.restart-button,
.undo-button,
.share-button,
.game-message a {
  display: inline-block;
  background: var(--btn-bg);
  border: none;
  border-radius: 6px;
  padding: 0 1rem;
  text-decoration: none;
  color: var(--btn-text);
  height: 2.5rem;
  line-height: 2.5rem;
  font-size: 0.9rem;
  font-weight: bold;
  font-family: inherit;
  cursor: pointer;
  text-align: center;
}

.undo-button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ── Game board ── */
.game-container {
  --board-size: 4;
  --board-padding: 15px;
  --cell-gap: 15px;
  --cell-size: 106px;

  margin-top: 1.25rem;
  position: relative;
  padding: var(--board-padding);
  touch-action: none;
  user-select: none;
  background: var(--grid-bg);
  border-radius: 8px;
  width: min(calc(100vw - 2rem), 500px);
  aspect-ratio: 1;
  box-sizing: border-box;
  cursor: default;
}

.grid-container {
  position: absolute;
  inset: var(--board-padding);
  z-index: 1;
  display: flex;
  flex-direction: column;
  gap: var(--cell-gap);
}

.grid-row {
  display: flex;
  gap: var(--cell-gap);
  flex: 1;
  margin: 0;
}

.grid-row:after { display: none; }

.grid-cell {
  flex: 1;
  border-radius: 4px;
  background: var(--cell-bg);
  margin: 0;
  float: none;
  width: auto;
  height: auto;
}

.tile-container {
  position: absolute;
  inset: var(--board-padding);
  z-index: 2;
}

.tile {
  position: absolute;
  transition: transform 100ms ease-in-out;
  width: var(--cell-size);
  height: var(--cell-size);
}

.tile .tile-inner {
  width: 100%;
  height: 100%;
  border-radius: 4px;
  background: var(--tile-2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: clamp(1rem, 5vw, 2.2rem);
  color: var(--text);
}

.tile.tile-2 .tile-inner  { background: var(--tile-2); color: var(--text); }
.tile.tile-4 .tile-inner  { background: var(--tile-4); color: var(--text); }
.tile.tile-8 .tile-inner  { background: var(--tile-8); color: var(--text-light); }
.tile.tile-16 .tile-inner { background: var(--tile-16); color: var(--text-light); }
.tile.tile-32 .tile-inner { background: var(--tile-32); color: var(--text-light); }
.tile.tile-64 .tile-inner { background: var(--tile-64); color: var(--text-light); }
.tile.tile-128 .tile-inner  { background: var(--tile-128); color: var(--text-light); font-size: clamp(0.85rem, 4vw, 1.8rem); }
.tile.tile-256 .tile-inner  { background: var(--tile-256); color: var(--text-light); font-size: clamp(0.85rem, 4vw, 1.8rem); }
.tile.tile-512 .tile-inner  { background: var(--tile-512); color: var(--text-light); font-size: clamp(0.85rem, 4vw, 1.8rem); }
.tile.tile-1024 .tile-inner { background: var(--tile-1024); color: var(--text-light); font-size: clamp(0.7rem, 3.5vw, 1.4rem); }
.tile.tile-2048 .tile-inner { background: var(--tile-2048); color: var(--text-light); font-size: clamp(0.7rem, 3.5vw, 1.4rem); }
.tile.tile-super .tile-inner {
  background: var(--tile-super);
  color: var(--text-light);
  font-size: clamp(0.6rem, 3vw, 1.2rem);
}

@keyframes appear {
  0% { opacity: 0; transform: scale(0); }
  100% { opacity: 1; transform: scale(1); }
}

@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.12); }
  100% { transform: scale(1); }
}

.tile-new .tile-inner {
  animation: appear 200ms ease 100ms both;
}

.tile-merged .tile-inner {
  z-index: 20;
  animation: pop 200ms ease 100ms both;
}

/* ── Game message overlay ── */
.game-container .game-message {
  display: none;
  position: absolute;
  inset: 0;
  background: var(--overlay);
  z-index: 100;
  text-align: center;
  border-radius: 8px;
  animation: fade-in 800ms ease 400ms both;
}

@keyframes fade-in {
  0% { opacity: 0; }
  100% { opacity: 1; }
}

.game-container .game-message p {
  font-size: clamp(1.8rem, 8vw, 3rem);
  font-weight: bold;
  margin: 35% 0 0;
  color: var(--text-light);
}

.game-container .game-message .lower {
  margin-top: 1.5rem;
  display: flex;
  justify-content: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.game-container .game-message a.keep-playing-button {
  display: none;
}

.game-container .game-message.game-won {
  background: var(--overlay-win);
}

.game-container .game-message.game-won a.keep-playing-button {
  display: inline-block;
}

.game-container .game-message.game-won,
.game-container .game-message.game-over {
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* ── D-pad ── */
.dpad {
  display: none;
  grid-template-columns: repeat(3, 52px);
  grid-template-rows: repeat(3, 52px);
  gap: 6px;
  justify-content: center;
  margin: 1.25rem auto 0;
  touch-action: manipulation;
}

.dpad-btn {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel-bg);
  color: var(--text);
  font-size: 1.1rem;
  cursor: pointer;
  font-family: inherit;
}

.dpad-up    { grid-column: 2; grid-row: 1; }
.dpad-left  { grid-column: 1; grid-row: 2; }
.dpad-right { grid-column: 3; grid-row: 2; }
.dpad-down  { grid-column: 2; grid-row: 3; }

.dpad-btn:active {
  background: var(--score-bg);
  transform: scale(0.95);
}

.blitz-timer.hidden {
  display: none;
}

.game-container.touch-play-active,
.dpad.touch-play-active {
  touch-action: none;
}

.achievement-toast {
  position: fixed;
  left: 50%;
  bottom: 1.5rem;
  transform: translateX(-50%);
  padding: 0.65rem 1.1rem;
  border-radius: 999px;
  background: var(--score-bg);
  color: var(--text-light);
  font-size: 0.85rem;
  font-weight: 700;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  z-index: 100;
  pointer-events: none;
}

.achievement-toast.hidden {
  display: none;
}

.achievement-list {
  list-style: none;
  margin: 0.75rem 0 0;
  padding: 0;
  display: grid;
  gap: 0.4rem;
}

.achievement-list li {
  padding: 0.4rem 0.55rem;
  border-radius: 6px;
  font-size: 0.9rem;
}

.achievement-list li.unlocked {
  color: var(--text);
  background: rgba(52, 211, 153, 0.15);
}

.achievement-list li.locked {
  opacity: 0.45;
}

/* ── Panels ── */
.settings-panel,
.stats-panel,
.achievements-panel {
  margin-top: 1rem;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-bg);
}

.settings-panel summary,
.stats-panel summary,
.achievements-panel summary {
  cursor: pointer;
  font-weight: bold;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 0.75rem;
}

.setting-row select {
  padding: 0.35rem 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.35rem 1rem;
  margin: 0.75rem 0 0;
}

.stats-grid dt {
  margin: 0;
  opacity: 0.8;
}

.stats-grid dd {
  margin: 0;
  font-weight: bold;
  text-align: right;
}

/* ── Footer hints ── */
.hint,
.keyboard-hint,
.attribution {
  font-size: 0.85rem;
  line-height: 1.55;
  margin-top: 1rem;
  opacity: 0.85;
}

.keyboard-hint {
  font-family: monospace;
  font-size: 0.78rem;
}

.attribution {
  font-size: 0.75rem;
  opacity: 0.6;
  margin-top: 1.5rem;
}

a {
  color: var(--link);
  font-weight: bold;
}

.container {
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
}

/* ── Responsive ── */
@media screen and (max-width: 520px) {
  html, body { font-size: 15px; }

  .game-container {
    --board-padding: 10px;
    --cell-gap: 10px;
  }

  .scores-container { gap: 0.35rem; }

  .score-container,
  .best-container {
    padding: 0.4rem 0.65rem;
    min-width: 3.5rem;
  }

  .action-buttons {
    width: 100%;
  }

  .restart-button,
  .undo-button,
  .share-button {
    flex: 1;
    min-width: 0;
    padding: 0 0.5rem;
    font-size: 0.8rem;
  }
}

@media (hover: none), (max-width: 560px) {
  .dpad { display: grid; }
  .keyboard-hint { display: none; }
}

@media (min-width: 561px) {
  .keyboard-hint { display: block; }
}

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