/* 2048. Loads after base.css, which supplies the tokens, reset, background and
   card chrome. */

:root {
  --duration-slide: 110ms;

  /* Board geometry — the tile transform math depends on --gap being absolute */
  --gap: 12px;
  --tile-radius: 9px;
}

/* A squarer frame than the landing page: the board wants to stay close to the
   card's width. */
.card {
  max-width: 520px;
}

.card__body {
  padding: 26px 26px 22px;
}

.card__foot {
  padding: 13px 26px;
}

/* Header: title + scores */
.head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

.title {
  position: relative;
  margin: 0;
  font-family: var(--font-mono);
  font-size: clamp(2rem, 8vw, 2.9rem);
  font-weight: 700;
  line-height: 1;
  letter-spacing: -0.03em;
  background: linear-gradient(
    100deg,
    var(--accent-cyan) 0%,
    #818cf8 45%,
    var(--accent-violet) 100%
  );
  background-size: 200% auto;
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
  animation:
    rise 700ms var(--ease-smooth) 120ms both,
    shimmer 8s linear infinite;
}

.scores {
  display: flex;
  gap: 8px;
  animation: fade-in 700ms var(--ease-smooth) 240ms both;
}

.score {
  position: relative;
  min-width: 74px;
  padding: 6px 10px 7px;
  border: 1px solid var(--border-color);
  border-radius: 10px;
  background: var(--bg-chip);
  text-align: center;
}

.score__label {
  display: block;
  font-family: var(--font-mono);
  font-size: 0.62rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--text-dim);
  line-height: 1.4;
}

.score__value {
  display: block;
  font-family: var(--font-mono);
  font-size: 1.05rem;
  font-weight: 700;
  line-height: 1.2;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

/* Floating "+4" that rises out of the score box on a merge */
.score__gain {
  position: absolute;
  left: 0;
  right: 0;
  top: 4px;
  font-family: var(--font-mono);
  font-size: 0.86rem;
  font-weight: 700;
  color: var(--accent-lime);
  opacity: 0;
  pointer-events: none;
}

.score__gain--run {
  animation: gain 700ms var(--ease-smooth);
}

/* Hint line */
.hint {
  margin: 18px 0 14px;
  font-family: var(--font-mono);
  font-size: 0.74rem;
  color: var(--text-dim);
  letter-spacing: 0.04em;
  animation: fade-in 600ms var(--ease-smooth) 320ms both;
}

.hint__sigil {
  color: var(--accent-lime);
  margin-right: 8px;
}

.hint__touch { display: none; }

/* Coarse pointers get the swipe wording instead of the keyboard one. */
@media (hover: none) and (pointer: coarse) {
  .hint__desktop { display: none; }
  .hint__touch { display: inline; }
}

/* Board */
.board {
  position: relative;
  aspect-ratio: 1;
  padding: var(--gap);
  border: 1px solid var(--border-color);
  border-radius: calc(var(--tile-radius) + var(--gap) / 2);
  background: rgba(255, 255, 255, 0.025);
  /* Swipes must not scroll the page while the finger is on the board. */
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  animation: rise 700ms var(--ease-smooth) 400ms both;
}

.board__cells {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--gap);
  height: 100%;
}

.cell {
  border-radius: var(--tile-radius);
  background: rgba(255, 255, 255, 0.035);
}

.board__tiles {
  position: absolute;
  inset: var(--gap);
}

/* Tiles */
.tile {
  position: absolute;
  top: 0;
  left: 0;
  /* 4 columns separated by 3 gaps, so one cell is (100% - 3 gaps) / 4 */
  width: calc((100% - 3 * var(--gap)) / 4);
  height: calc((100% - 3 * var(--gap)) / 4);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--tile-radius);
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 1.6rem;
  line-height: 1;
  letter-spacing: -0.04em;
  font-variant-numeric: tabular-nums;
  /* 100% here resolves against the tile's own width, i.e. one cell. */
  transform:
    translate(
      calc(var(--x) * (100% + var(--gap))),
      calc(var(--y) * (100% + var(--gap)))
    );
  transition: transform var(--duration-slide) ease-in-out;
  will-change: transform;
  z-index: 1;
}

/* Long numbers have to shrink to stay inside the tile */
.tile--d3 { font-size: 1.3rem; }
.tile--d4 { font-size: 1.05rem; }
.tile--d5 { font-size: 0.86rem; }

/* Both entry animations are held invisible by `both` for the length of the
   slide, so a new or merged tile only shows up once the movement has settled. */
.tile--new { animation: appear 180ms var(--ease-smooth) var(--duration-slide) both; }
.tile--merged { animation: merge 220ms var(--ease-smooth) var(--duration-slide) both; }

/* The two tiles consumed by a merge slide to the target under the new tile,
   then get dropped from the DOM. */
.tile--ghost { z-index: 0; }

.tile[data-value="2"] {
  background: rgba(255, 255, 255, 0.07);
  color: #8b8b96;
}

.tile[data-value="4"] {
  background: rgba(255, 255, 255, 0.12);
  color: #c4c4cc;
}

.tile[data-value="8"] {
  background: rgba(34, 211, 238, 0.16);
  color: #67e8f9;
}

.tile[data-value="16"] {
  background: rgba(34, 211, 238, 0.28);
  color: #cffafe;
}

.tile[data-value="32"] {
  background: rgba(56, 189, 248, 0.34);
  color: #e0f2fe;
}

.tile[data-value="64"] {
  background: rgba(129, 140, 248, 0.38);
  color: #e0e7ff;
}

.tile[data-value="128"] {
  background: rgba(139, 92, 246, 0.42);
  color: #ede9fe;
  box-shadow: 0 0 18px -4px rgba(139, 92, 246, 0.55);
}

.tile[data-value="256"] {
  background: rgba(168, 85, 247, 0.5);
  color: #f5f3ff;
  box-shadow: 0 0 22px -4px rgba(168, 85, 247, 0.6);
}

.tile[data-value="512"] {
  background: rgba(217, 70, 239, 0.54);
  color: #fdf4ff;
  box-shadow: 0 0 26px -4px rgba(217, 70, 239, 0.65);
}

.tile[data-value="1024"] {
  background: rgba(244, 63, 94, 0.54);
  color: #fff1f2;
  box-shadow: 0 0 30px -4px rgba(244, 63, 94, 0.7);
}

.tile[data-value="2048"] {
  background: linear-gradient(135deg, var(--accent-cyan), var(--accent-violet));
  color: #ffffff;
  box-shadow: 0 0 34px -4px rgba(34, 211, 238, 0.75);
}

/* Anything past 2048 shares one loud style */
.tile--super {
  background: linear-gradient(135deg, var(--accent-lime), var(--accent-cyan));
  color: #04120a;
  box-shadow: 0 0 34px -4px rgba(74, 222, 128, 0.75);
}

/* Overlay */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-radius: calc(var(--tile-radius) + var(--gap) / 2);
  background: rgba(5, 5, 7, 0.82);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  text-align: center;
  animation: fade-in 320ms var(--ease-smooth) both;
}

.overlay[hidden] { display: none; }

.overlay__title {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 1.7rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  background: linear-gradient(100deg, var(--accent-cyan), var(--accent-violet));
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
}

.overlay__text {
  margin: 0 0 12px;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.overlay__actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

/* Buttons */
.controls {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}

.btn {
  flex: 1;
  padding: 9px 14px;
  border: 1px solid var(--border-color);
  border-radius: 10px;
  background: var(--bg-chip);
  color: var(--text-secondary);
  font-family: var(--font-mono);
  font-size: 0.78rem;
  letter-spacing: 0.06em;
  cursor: pointer;
  transition:
    color var(--duration-short) var(--ease-smooth),
    border-color var(--duration-short) var(--ease-smooth),
    background var(--duration-short) var(--ease-smooth);
}

.btn:hover:not(:disabled),
.btn:focus-visible {
  color: var(--accent-cyan);
  border-color: var(--border-hover);
  outline: none;
}

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

.btn--primary {
  color: var(--text-primary);
  border-color: rgba(34, 211, 238, 0.25);
  background: rgba(34, 211, 238, 0.1);
}

.btn[aria-pressed="true"] {
  color: var(--accent-lime);
  border-color: rgba(74, 222, 128, 0.35);
  background: rgba(74, 222, 128, 0.1);
}

/* Square glyph-only button, so it does not eat the row on a narrow phone. */
.btn--icon {
  flex: none;
  min-width: 46px;
  padding: 9px 0;
  font-size: 0.95rem;
}

.btn--icon[aria-pressed="false"] {
  color: var(--text-dim);
  text-decoration: line-through;
}

.overlay .btn {
  flex: none;
  min-width: 116px;
}

/* Animation Keyframes */
/* Scale-only keyframes: the tile's translate lives in a CSS variable that the
   animation must not clobber, so both use the same translate() expression. */
@keyframes appear {
  from {
    opacity: 0;
    transform:
      translate(
        calc(var(--x) * (100% + var(--gap))),
        calc(var(--y) * (100% + var(--gap)))
      )
      scale(0.3);
  }
  to {
    opacity: 1;
    transform:
      translate(
        calc(var(--x) * (100% + var(--gap))),
        calc(var(--y) * (100% + var(--gap)))
      )
      scale(1);
  }
}

@keyframes merge {
  0% {
    opacity: 0;
    transform:
      translate(
        calc(var(--x) * (100% + var(--gap))),
        calc(var(--y) * (100% + var(--gap)))
      )
      scale(0.6);
  }
  55% {
    opacity: 1;
    transform:
      translate(
        calc(var(--x) * (100% + var(--gap))),
        calc(var(--y) * (100% + var(--gap)))
      )
      scale(1.18);
  }
  100% {
    opacity: 1;
    transform:
      translate(
        calc(var(--x) * (100% + var(--gap))),
        calc(var(--y) * (100% + var(--gap)))
      )
      scale(1);
  }
}

@keyframes gain {
  0% { opacity: 0; transform: translateY(0); }
  25% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-26px); }
}

/* Responsive Design */
@media (max-width: 560px) {
  :root {
    --gap: 9px;
    --tile-radius: 8px;
  }

  .stage { padding: 14px; }

  .card__body { padding: 20px 16px 18px; }

  .card__foot { padding: 12px 16px; }

  .tile { font-size: 1.35rem; }
  .tile--d3 { font-size: 1.1rem; }
  .tile--d4 { font-size: 0.88rem; }
  .tile--d5 { font-size: 0.72rem; }
}

/* Accessibility - Respect user's motion preferences */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    /* The entry animations lean on their delay to stay hidden during the
       slide; with no slide left to wait for, the delay has to go too. */
    animation-delay: 0ms !important;
  }
}
