/*
 * 2048 — visual style
 *
 * Direction: GDD §13 "plan B" colour ladder (grey → blue → cyan → green →
 * yellow → orange → red → purple → pink → gold → rainbow) on the "plan A"
 * dark, glowing tech backdrop. Glow intensity grows with the tile value.
 *
 * Two themes: light (default) and dark, switched with data-game-theme on
 * <html>. Every theme-dependent colour is a token defined below; component
 * rules only use tokens. (We use our own attribute rather than data-theme so
 * a host page that sets data-theme cannot override the player's choice.)
 *
 * Sizing is driven by one variable, --board, so the whole layout scales from
 * small phones to desktop without breakpoints for the board itself.
 */

:root {
  /* ---- light theme (default) ---- */
  color-scheme: light;
  --bg: #eef1f8;
  --bg-glow-1: #dfe6ff;
  --bg-glow-2: #f6e8ff;
  --panel: #ffffff;
  --panel-border: #dce1ee;
  --board-bg: #c9d0e3;
  --board-border: #bcc4da;
  --board-shadow: 0 18px 44px rgba(52, 64, 110, 0.18);
  --cell: #dde2ef;
  --cell-shadow: inset 0 2px 4px rgba(40, 52, 96, 0.08);
  --text: #1c2240;
  --text-dim: #687096;
  --text-soft: #3d4466;
  --accent: #5b6cf0;
  --accent-2: #0b9ec2;
  --danger: #e23a54;
  --title-from: #2a3170;
  --title-glow: rgba(91, 108, 240, 0.18);
  --gold: #c98a00;
  --score-add-glow: rgba(11, 158, 194, 0.35);
  --overlay-bg: rgba(238, 241, 248, 0.78);
  --scrim: rgba(28, 34, 64, 0.35);
  --modal-shadow: 0 30px 80px rgba(28, 34, 64, 0.25);
  --tile2-bg: #ffffff;
  --tile2-fg: #4a5174;
  --tile-shadow: 0 2px 6px rgba(40, 52, 96, 0.12);
  --glow-scale: 0.55;
  --super-bg: #1c2240;
}

/* ---- dark theme ---- */
:root[data-game-theme="dark"] {
  color-scheme: dark;
  --bg: #0c0f1d;
  --bg-glow-1: #1b2150;
  --bg-glow-2: #1a1338;
  --panel: #161b33;
  --panel-border: #252c52;
  --board-bg: #161b33;
  --board-border: #252c52;
  --board-shadow: 0 20px 60px rgba(0, 0, 0, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.04);
  --cell: #20274a;
  --cell-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.25);
  --text: #eef1ff;
  --text-dim: #8c93b8;
  --text-soft: #c9cee8;
  --accent: #7c8cff;
  --accent-2: #46d2ff;
  --danger: #ff5d73;
  --title-from: #ffffff;
  --title-glow: rgba(124, 140, 255, 0.35);
  --gold: #ffcf48;
  --score-add-glow: rgba(70, 210, 255, 0.7);
  --overlay-bg: rgba(10, 12, 26, 0.72);
  --scrim: rgba(6, 8, 18, 0.7);
  --modal-shadow: 0 30px 80px rgba(0, 0, 0, 0.5);
  --tile2-bg: #3a4266;
  --tile2-fg: #dfe3f7;
  --tile-shadow: 0 0 0 transparent;
  --glow-scale: 1;
  --super-bg: #0b0d18;
}

:root {
  --n: 4;
  /* Board side: fits width (16px gutters) and height (leaves room for HUD). */
  --board: max(272px, min(500px, calc(100vw - 32px), calc(100svh - 250px)));
  --gap: calc(var(--board) * 0.028);
  --tile: calc((var(--board) - var(--gap) * (var(--n) + 1)) / var(--n));
  --radius: calc(var(--tile) * 0.14);
  --move-ms: 120ms;

  --font: ui-rounded, "SF Pro Rounded", "Nunito", "Segoe UI", system-ui, -apple-system,
    "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  height: 100%;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(1200px 700px at 50% -10%, var(--bg-glow-1) 0%, transparent 60%),
    radial-gradient(800px 500px at 100% 110%, var(--bg-glow-2) 0%, transparent 60%),
    var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  transition: background-color 0.25s, color 0.25s;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

button {
  font: inherit;
  color: inherit;
  touch-action: manipulation;
}

[hidden] {
  display: none !important;
}

/* ------------------------------------------------------------------ layout */

.app {
  width: calc(var(--board) + 0px);
  margin: 0 auto;
  min-height: 100svh;
  padding: max(16px, env(safe-area-inset-top)) 0 max(16px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 14px;
}

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

.title {
  margin: 0;
  font-size: clamp(44px, calc(var(--board) * 0.15), 72px);
  font-weight: 900;
  letter-spacing: -0.04em;
  line-height: 1;
  background: linear-gradient(135deg, var(--title-from) 10%, var(--accent-2) 55%, var(--accent) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 18px var(--title-glow));
}

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

.score-box {
  position: relative;
  min-width: 76px;
  padding: 7px 12px 8px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.score-label {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.score-value {
  font-size: 20px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

.score-box.bump .score-value {
  animation: score-bump 220ms ease-out;
}

.score-box.best.is-new-best {
  border-color: var(--gold);
  box-shadow: 0 0 16px color-mix(in srgb, var(--gold) 30%, transparent);
}

.score-box.best.is-new-best .score-label {
  color: var(--gold);
}

.score-add {
  position: absolute;
  left: 50%;
  top: 50%;
  font-size: 18px;
  font-weight: 900;
  color: var(--accent-2);
  text-shadow: 0 0 10px var(--score-add-glow);
  pointer-events: none;
  animation: float-up 650ms ease-out forwards;
}

.subbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.tagline {
  margin: 0;
  font-size: 14px;
  color: var(--text-dim);
}

.tagline strong {
  color: var(--text);
}

.icon-buttons {
  display: flex;
  gap: 6px;
}

.icon-btn {
  width: 38px;
  height: 38px;
  padding: 8px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: var(--panel);
  color: var(--text-dim);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}

.icon-btn:hover {
  color: var(--text);
  border-color: var(--accent);
}

.lang-btn {
  padding: 0;
  font-size: 14px;
  font-weight: 800;
  letter-spacing: 0;
}

.icon-btn svg {
  width: 100%;
  height: 100%;
  display: block;
}

.icon-btn .icon-sound-off,
.icon-btn.is-off .icon-sound-on {
  display: none;
}

.icon-btn.is-off .icon-sound-off {
  display: block;
}

/* Theme toggle shows the theme you'd switch TO. */
.icon-btn .icon-theme-light,
:root[data-game-theme="dark"] .icon-btn .icon-theme-dark {
  display: none;
}

:root[data-game-theme="dark"] .icon-btn .icon-theme-light {
  display: block;
}

/* ------------------------------------------------------------------- board */

.board {
  position: relative;
  width: var(--board);
  height: var(--board);
  border-radius: calc(var(--radius) * 1.4);
  background: var(--board-bg);
  border: 1px solid var(--board-border);
  box-shadow: var(--board-shadow);
  touch-action: none; /* we handle swipes ourselves (GDD §15) */
  outline: none;
}

.grid,
.tiles {
  position: absolute;
  inset: var(--gap);
}

.grid {
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
  gap: var(--gap);
}

.cell {
  border-radius: var(--radius);
  background: var(--cell);
  box-shadow: var(--cell-shadow);
}

/* ------------------------------------------------------------------- tiles */

.tile {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--tile);
  height: var(--tile);
  --r: 0;
  --c: 0;
  transform: translate(
    calc(var(--c) * (var(--tile) + var(--gap))),
    calc(var(--r) * (var(--tile) + var(--gap)))
  );
  transition: transform var(--move-ms) ease-in-out;
  z-index: 2;
  /* tile colour tokens, overridden per value below */
  --t-bg: var(--tile2-bg);
  --t-fg: var(--tile2-fg);
  --t-glow: 0;
  --t-glow-color: transparent;
}

.tile.tile-merging {
  z-index: 1;
}

.tile-inner {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  background: var(--t-bg);
  color: var(--t-fg);
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.03em;
  line-height: 1;
  font-size: calc(var(--tile) * 0.46);
  box-shadow:
    var(--tile-shadow),
    0 0 calc(var(--t-glow) * var(--glow-scale) * 1px) var(--t-glow-color),
    inset 0 -3px 0 rgba(0, 0, 0, 0.18),
    inset 0 1px 0 rgba(255, 255, 255, 0.18);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.tile[data-digits="3"] .tile-inner { font-size: calc(var(--tile) * 0.38); }
.tile[data-digits="4"] .tile-inner { font-size: calc(var(--tile) * 0.30); }
.tile[data-digits="5"] .tile-inner { font-size: calc(var(--tile) * 0.25); }
.tile[data-digits="6"] .tile-inner { font-size: calc(var(--tile) * 0.21); }
.tile[data-digits="7"] .tile-inner { font-size: calc(var(--tile) * 0.18); }

.tile-new {
  animation: tile-appear 180ms ease-out backwards;
}

.tile-merged {
  animation: tile-pop 180ms ease-in-out;
}

.tile:has(.tile-merged) {
  z-index: 3;
}

/* Colour ladder (GDD §13 plan B) — glow grows with value (plan A). */
.tile[data-value="2"]    { --t-bg: var(--tile2-bg); --t-fg: var(--tile2-fg); }
.tile[data-value="4"]    { --t-bg: #3567d6; --t-fg: #fff; --t-glow: 6;  --t-glow-color: rgba(53, 103, 214, 0.35); }
.tile[data-value="8"]    { --t-bg: #12a6c8; --t-fg: #fff; --t-glow: 8;  --t-glow-color: rgba(18, 166, 200, 0.4); }
.tile[data-value="16"]   { --t-bg: #1fb36e; --t-fg: #fff; --t-glow: 10; --t-glow-color: rgba(31, 179, 110, 0.42); }
.tile[data-value="32"]   { --t-bg: #f1c232; --t-fg: #2a2205; --t-glow: 12; --t-glow-color: rgba(241, 194, 50, 0.45); }
.tile[data-value="64"]   { --t-bg: #f5862a; --t-fg: #fff; --t-glow: 14; --t-glow-color: rgba(245, 134, 42, 0.5); }
.tile[data-value="128"]  { --t-bg: #e8434f; --t-fg: #fff; --t-glow: 18; --t-glow-color: rgba(232, 67, 79, 0.55); }
.tile[data-value="256"]  { --t-bg: #8f4fd8; --t-fg: #fff; --t-glow: 20; --t-glow-color: rgba(143, 79, 216, 0.6); }
.tile[data-value="512"]  { --t-bg: #e051a9; --t-fg: #fff; --t-glow: 24; --t-glow-color: rgba(224, 81, 169, 0.65); }
.tile[data-value="1024"] {
  --t-bg: linear-gradient(145deg, #ffe38a, #f2b705 55%, #d98e00);
  --t-fg: #3a2600;
  --t-glow: 28;
  --t-glow-color: rgba(242, 183, 5, 0.7);
}
.tile[data-value="2048"] .tile-inner,
.tile[data-value="super"] .tile-inner {
  background: linear-gradient(120deg, #ff5d73, #ffb347, #ffe66d, #4be3a0, #46d2ff, #7c8cff, #d070ff, #ff5d73);
  background-size: 300% 300%;
  animation: rainbow 4s linear infinite;
  color: #fff;
  text-shadow: 0 1px 6px rgba(0, 0, 0, 0.45);
  box-shadow:
    0 0 32px rgba(255, 255, 255, 0.45),
    0 0 60px rgba(124, 140, 255, 0.45),
    inset 0 -3px 0 rgba(0, 0, 0, 0.18);
}
.tile[data-value="super"] .tile-inner {
  background: var(--super-bg);
  border: calc(var(--tile) * 0.05) solid transparent;
  background-image:
    linear-gradient(var(--super-bg), var(--super-bg)),
    linear-gradient(120deg, #ff5d73, #ffb347, #ffe66d, #4be3a0, #46d2ff, #7c8cff, #d070ff, #ff5d73);
  background-origin: border-box;
  background-clip: padding-box, border-box;
  background-size: 100% 100%, 300% 300%;
}
.tile[data-value="2048"] .tile-merged,
.tile[data-value="super"] .tile-merged {
  animation: tile-pop 180ms ease-in-out, rainbow 4s linear infinite;
}

/* ------------------------------------------------- invalid-move feedback */

.board.nudge-left  { animation: nudge-x-neg 160ms ease-out; }
.board.nudge-right { animation: nudge-x-pos 160ms ease-out; }
.board.nudge-up    { animation: nudge-y-neg 160ms ease-out; }
.board.nudge-down  { animation: nudge-y-pos 160ms ease-out; }

/* --------------------------------------------------------------- overlays */

.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  border-radius: inherit;
  background: var(--overlay-bg);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  animation: fade-in 260ms ease-out;
  touch-action: manipulation;
}

.overlay-card {
  text-align: center;
  animation: rise-in 320ms cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

.overlay-title {
  margin: 0 0 8px;
  font-size: clamp(36px, calc(var(--board) * 0.12), 56px);
  font-weight: 900;
  letter-spacing: -0.02em;
}

.overlay-win .overlay-title {
  background: linear-gradient(120deg, #ffe66d, #ffb347, #ff5d73, #d070ff, #46d2ff);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 16px rgba(255, 230, 109, 0.45));
}

.overlay-over .overlay-title {
  color: var(--danger);
  text-shadow: 0 0 22px color-mix(in srgb, var(--danger) 45%, transparent);
}

.overlay-text {
  margin: 0 0 20px;
  color: var(--text-dim);
  font-size: 16px;
}

.overlay-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

/* ---------------------------------------------------------------- buttons */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 46px;
  padding: 0 20px;
  border-radius: 12px;
  border: 1px solid transparent;
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  cursor: pointer;
  transition: transform 0.1s, filter 0.15s, opacity 0.15s, border-color 0.15s;
}

.btn:active:not(:disabled) {
  transform: scale(0.97);
}

.btn:focus-visible,
.icon-btn:focus-visible {
  outline: 2px solid var(--accent-2);
  outline-offset: 2px;
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 80%, #2a2f8f));
  color: #fff;
  box-shadow: 0 6px 20px color-mix(in srgb, var(--accent) 35%, transparent);
}

.btn-primary:hover {
  filter: brightness(1.1);
}

.btn-secondary {
  background: var(--panel);
  border-color: var(--panel-border);
  color: var(--text);
}

.btn-secondary:hover:not(:disabled) {
  border-color: var(--accent);
}

.btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.btn svg {
  width: 18px;
  height: 18px;
}

.btn-block {
  width: 100%;
}

.badge {
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 11px;
  background: var(--cell);
  color: var(--accent-2);
  font-size: 13px;
  line-height: 22px;
  letter-spacing: 0;
}

.controls {
  display: grid;
  grid-template-columns: 1fr 1.4fr;
  gap: 10px;
}

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

.hint-touch,
.hint-touch-inline {
  display: none;
}

@media (hover: none) and (pointer: coarse) {
  .hint-desktop,
  .hint-desktop-inline {
    display: none;
  }
  .hint-touch,
  .hint-touch-inline {
    display: inline;
  }
}

/* ------------------------------------------------------------- help modal */

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: var(--scrim);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  animation: fade-in 200ms ease-out;
}

.modal-card {
  width: min(360px, 100%);
  padding: 24px;
  border-radius: 18px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  box-shadow: var(--modal-shadow);
  animation: rise-in 280ms cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

.modal-card h2 {
  margin: 0 0 16px;
  font-size: 22px;
  font-weight: 900;
  letter-spacing: 0.02em;
}

.help-list {
  list-style: none;
  margin: 0 0 22px;
  padding: 0;
  display: grid;
  gap: 14px;
}

.help-list li {
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 15px;
  line-height: 1.4;
  color: var(--text-soft);
}

.help-icon {
  flex: none;
  width: 52px;
  height: 52px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  font-size: 22px;
  background: var(--cell);
  color: var(--text);
}

.help-merge {
  font-size: 15px;
  background: #3567d6;
  color: #fff;
}

.help-goal {
  font-size: 14px;
  color: #fff;
  background: linear-gradient(120deg, #ff5d73, #ffb347, #4be3a0, #46d2ff, #d070ff);
}

/* --------------------------------------------------------------- keyframes */

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

/* GDD §12: 100% → 115% → 100% */
@keyframes tile-pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

@keyframes rainbow {
  0% { background-position: 0% 50%; }
  100% { background-position: 300% 50%; }
}

@keyframes float-up {
  0% { opacity: 1; transform: translate(-50%, -50%); }
  100% { opacity: 0; transform: translate(-50%, -190%); }
}

@keyframes score-bump {
  0% { transform: scale(1); }
  40% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes rise-in {
  from { opacity: 0; transform: translateY(14px) scale(0.96); }
  to { opacity: 1; transform: none; }
}

@keyframes nudge-x-neg { 40% { transform: translateX(-5px); } }
@keyframes nudge-x-pos { 40% { transform: translateX(5px); } }
@keyframes nudge-y-neg { 40% { transform: translateY(-5px); } }
@keyframes nudge-y-pos { 40% { transform: translateY(5px); } }

/* --------------------------------------------------------- small screens */

@media (max-width: 380px) {
  .score-box {
    min-width: 64px;
    padding: 6px 8px 7px;
  }
  .score-value {
    font-size: 17px;
  }
  .tagline {
    font-size: 13px;
  }
  .btn {
    font-size: 14px;
    padding: 0 14px;
  }
}

/* Very short landscape phones: let the page scroll instead of crushing UI. */
@media (max-height: 520px) and (orientation: landscape) {
  :root {
    --board: max(260px, min(500px, calc(100vw - 32px), calc(100svh - 32px)));
  }
}

@media (prefers-reduced-motion: reduce) {
  :root {
    --move-ms: 0ms;
  }
  .tile-new,
  .tile-merged,
  .overlay,
  .overlay-card,
  .modal,
  .modal-card,
  .board[class*="nudge-"] {
    animation: none !important;
  }
}
