@import url('../shared/theme.css');

:root {
  --paper: var(--game-bg);
  --paper-deep: var(--game-surface-strong);
  --ink: var(--game-text);
  --muted: var(--game-muted);
  --line: var(--game-divider);
  --leaf: var(--game-accent);
  --leaf-dark: var(--game-accent-strong);
  --sun: var(--game-warning);
  --soil: var(--game-warning);
  --empty: var(--game-surface);
  --tile-ink: #18332d;
}

* {
  box-sizing: border-box;
}

html {
  min-width: 280px;
  background: var(--paper);
}

body {
  display: grid;
  min-width: 280px;
  min-height: 100svh;
  margin: 0;
  padding: 0.75rem;
  place-items: center;
  color: var(--ink);
  background:
    radial-gradient(circle at 88% 8%, var(--game-accent-soft), transparent 12rem), var(--paper);
  font-family: var(--game-font);
}

button,
output {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.game-card {
  width: min(100%, 420px);
  padding: clamp(0.8rem, 3vw, 1.1rem);
  overflow: hidden;
  background: var(--game-surface);
  border: 1px solid var(--line);
  border-radius: var(--game-radius);
  box-shadow: var(--game-shadow);
}

.game-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.75rem;
  align-items: start;
}

.eyebrow {
  margin: 0 0 0.2rem;
  color: var(--leaf);
  font-family: var(--game-mono);
  font-size: 0.58rem;
  font-weight: 850;
  letter-spacing: 0.13em;
}

h1 {
  margin: 0;
  font-family: var(--game-font);
  font-size: clamp(1.45rem, 7vw, 1.9rem);
  font-weight: 500;
  letter-spacing: -0.035em;
}

.subtitle {
  margin: 0.25rem 0 0;
  color: var(--muted);
  font-size: 0.7rem;
}

.scoreboard {
  display: grid;
  grid-template-columns: repeat(2, minmax(54px, 1fr));
  gap: 0.35rem;
}

.scoreboard > div {
  display: grid;
  min-height: 46px;
  padding: 0.35rem 0.5rem;
  place-items: center;
  background: var(--paper-deep);
  border: 1px solid var(--line);
  border-radius: var(--game-radius);
}

.scoreboard span {
  color: var(--muted);
  font-size: 0.56rem;
  font-weight: 750;
  letter-spacing: 0.08em;
}

.scoreboard output {
  font-family: var(--game-mono);
  font-size: 0.9rem;
  font-weight: 850;
  font-variant-numeric: tabular-nums;
}

.status {
  min-height: 2.4em;
  margin: 0.7rem 0 0.55rem;
  color: var(--muted);
  font-size: 0.69rem;
  line-height: 1.35;
}

.board-shell {
  padding: 0.45rem;
  background: linear-gradient(rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0)), var(--leaf-dark);
  border-radius: var(--game-radius);
  box-shadow: inset 0 1px rgba(255, 255, 255, 0.14);
}

.board {
  --board-gap: clamp(0.3rem, 1.6vw, 0.45rem);
  display: grid;
  grid-template-rows: repeat(4, minmax(0, 1fr));
  gap: var(--board-gap);
  aspect-ratio: 1;
}

.board-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--board-gap);
}

.cell {
  position: relative;
  display: grid;
  min-width: 0;
  aspect-ratio: 1;
  place-items: stretch;
  overflow: hidden;
  background: var(--empty);
  border: 1px solid var(--line);
  border-radius: var(--game-radius);
}

.tile {
  display: grid;
  min-width: 0;
  padding: 0.2rem;
  place-content: center;
  place-items: center;
  color: var(--tile-ink);
  background: #f6ead1;
  border-radius: inherit;
  line-height: 1;
  text-align: center;
}

.tile-symbol {
  display: grid;
  height: 1.4em;
  place-items: center;
  font-family: 'Segoe UI Emoji', 'Apple Color Emoji', 'Noto Color Emoji', sans-serif;
  font-size: clamp(0.95rem, 5.5vw, 1.35rem);
  line-height: 1;
}

.tile-label {
  max-width: 100%;
  margin-top: 0.18rem;
  overflow: hidden;
  font-size: clamp(0.52rem, 2.4vw, 0.66rem);
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tile-number {
  margin-top: 0.15rem;
  opacity: 0.67;
  font-family: var(--game-mono);
  font-size: clamp(0.45rem, 2vw, 0.56rem);
  font-weight: 750;
  font-variant-numeric: tabular-nums;
}

.tile[data-value='4'] {
  background: #e8edcd;
}

.tile[data-value='8'] {
  background: #d2e7ba;
}

.tile[data-value='16'] {
  background: #acdaa4;
}

.tile[data-value='32'] {
  background: #83c693;
}

.tile[data-value='64'] {
  color: #102f29;
  background: #5db17e;
}

.tile[data-value='128'] {
  color: #fffdf4;
  background: #388d6c;
}

.tile[data-value='256'] {
  color: #fffdf4;
  background: #276f65;
}

.tile[data-value='512'] {
  color: #fffdf4;
  background: #365a70;
}

.tile[data-value='1024'] {
  color: #fffdf4;
  background: #5f537b;
}

.tile[data-value='2048'] {
  color: #fffdf4;
  background: #9b6246;
  box-shadow: inset 0 0 0 2px rgba(255, 234, 174, 0.45);
}

.tile[data-value='4096'] {
  color: #fffdf4;
  background: #714d3d;
  box-shadow: inset 0 0 0 2px rgba(255, 225, 143, 0.55);
}

.tile[data-large='true'] {
  color: #fffdf4;
  background: #352f4e;
}

.tile.is-new {
  animation: seed-in 170ms ease-out both;
}

.tile.is-merged {
  animation: merge-pulse 190ms ease-out both;
}

.session-controls {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(76px, 0.36fr);
  gap: 0.5rem;
  margin-top: 0.65rem;
}

.session-controls button,
.direction-pad button {
  min-height: 46px;
  border-radius: var(--game-radius);
  cursor: pointer;
  font-weight: 800;
}

.primary-control {
  color: var(--game-on-accent);
  background: var(--leaf);
  border: 1px solid var(--leaf);
}

.primary-control:hover {
  background: var(--leaf-dark);
}

.secondary-control {
  color: var(--ink);
  background: transparent;
  border: 1px solid var(--line);
}

.secondary-control:hover {
  background: var(--paper-deep);
}

.direction-pad {
  display: grid;
  grid-template-columns: repeat(4, minmax(44px, 1fr));
  gap: 0.45rem;
  margin-top: 0.5rem;
}

.direction-pad button {
  min-width: 44px;
  color: var(--ink);
  background: var(--paper-deep);
  border: 1px solid var(--line);
  font-size: 1.25rem;
}

.direction-pad button:hover:not(:disabled) {
  color: var(--game-on-accent);
  background: var(--soil);
}

.direction-pad button:disabled {
  cursor: not-allowed;
  opacity: 0.42;
}

button:focus-visible {
  outline: 3px solid var(--sun);
  outline-offset: 2px;
}

.help {
  margin: 0.55rem 0 0;
  color: var(--muted);
  font-size: 0.62rem;
  line-height: 1.45;
  text-align: center;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

html[data-embed='true'] body {
  min-height: 100%;
  padding: 0;
  background: transparent;
}

html[data-embed='true'] .game-card {
  max-width: 420px;
  border-radius: 0;
  box-shadow: none;
}

@keyframes seed-in {
  from {
    opacity: 0.35;
    transform: scale(0.72);
  }
}

@keyframes merge-pulse {
  45% {
    transform: scale(1.08);
  }
}

@media (max-width: 340px) {
  body {
    padding: 0.4rem;
  }

  .game-card {
    padding: 0.72rem;
    border-radius: var(--game-radius);
  }

  .game-header {
    grid-template-columns: 1fr;
    gap: 0.4rem;
  }

  .scoreboard {
    width: 100%;
  }

  .scoreboard {
    grid-template-columns: repeat(2, minmax(48px, 1fr));
    gap: 0.25rem;
  }

  .scoreboard > div {
    min-height: 44px;
    padding-inline: 0.3rem;
  }
}

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

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    color-scheme: dark;
    --game-surface: #212529;
    --game-accent-soft: #134e4a;
    --game-on-accent: #10201e;
    --game-shadow: 0 12px 32px rgba(0, 0, 0, 0.24);
    --paper: #1c1c1d;
    --paper-deep: #2c3237;
    --ink: #e0e0e0;
    --muted: #9e9e9e;
    --line: #424246;
    --leaf: #5eead4;
    --leaf-dark: #2dd4bf;
    --sun: #fbbf24;
    --soil: #fbbf24;
    --empty: #212529;
  }
}
