:root {
  --field-width: 500px;
  --grid-spacing: 15px;
  --grid-row-cells: 4;
  --tile-size: calc((var(--field-width) - (var(--grid-spacing) * (var(--grid-row-cells) + 1))) / var(--grid-row-cells));
  --border-radius: 6px;
  /* 3px in original, but let's make it smoother */

  --color-background: #faf8ef;
  --color-form-background: #bbada0;
  --color-text: #776e65;
  --color-text-light: #f9f6f2;
  --color-button: #8f7a66;
  --color-button-hover: #9f8b77;

  /* Tile colors */
  --tile-color-empty: rgba(238, 228, 218, 0.35);
  --tile-color-2: #eee4da;
  --tile-color-4: #ede0c8;
  --tile-color-8: #f2b179;
  --tile-color-16: #f59563;
  --tile-color-32: #f67c5f;
  --tile-color-64: #f65e3b;
  --tile-color-128: #edcf72;
  --tile-color-256: #edcc61;
  --tile-color-512: #edc850;
  --tile-color-1024: #edc53f;
  --tile-color-2048: #edc22e;

  --font-family: 'Outfit', sans-serif;
  --transition-speed: 100ms;
}

body {
  margin: 0;
  padding: 0;
  background: var(--color-background);
  color: var(--color-text);
  font-family: var(--font-family);
  font-size: 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

h1.title {
  font-size: 80px;
  font-weight: 800;
  margin: 0;
  display: block;
  float: left;
}

.container {
  width: var(--field-width);
  margin: 0 auto;
}

.heading {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.scores-container {
  float: right;
  text-align: right;
  display: flex;
  gap: 5px;
}

.score-container,
.best-container {
  position: relative;
  display: inline-block;
  background: var(--color-form-background);
  padding: 15px 25px;
  font-size: 25px;
  height: 25px;
  line-height: 47px;
  font-weight: bold;
  border-radius: 3px;
  color: white;
  margin-top: 8px;
  text-align: center;
  min-width: 40px;
}

.score-container:after,
.best-container:after {
  position: absolute;
  width: 100%;
  top: 10px;
  left: 0;
  text-transform: uppercase;
  font-size: 13px;
  line-height: 13px;
  text-align: center;
  color: #eee4da;
}

.score-container:after {
  content: "Score";
}

.best-container:after {
  content: "Best";
}

.above-game {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.game-intro {
  float: left;
  line-height: 42px;
  margin-bottom: 0;
}

.restart-button {
  display: inline-block;
  background: var(--color-button);
  border-radius: 3px;
  padding: 0 20px;
  text-decoration: none;
  color: var(--color-text-light);
  height: 40px;
  line-height: 42px;
  cursor: pointer;
  display: block;
  text-align: center;
  float: right;
  font-weight: bold;
  transition: background var(--transition-speed);
}

.restart-button:hover {
  background: var(--color-button-hover);
}

.game-container {
  margin-top: 40px;
  position: relative;
  padding: var(--grid-spacing);
  cursor: default;
  -webkit-touch-callout: none;
  -ms-touch-action: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  background: var(--color-form-background);
  border-radius: var(--border-radius);
  width: var(--field-width);
  height: var(--field-width);
  box-sizing: border-box;
}

.game-message {
  display: none;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(238, 228, 218, 0.5);
  z-index: 100;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  animation: fade-in 800ms ease 1200ms;
  animation-fill-mode: both;
}

.game-message p {
  font-size: 60px;
  font-weight: bold;
  height: 60px;
  line-height: 60px;
  margin-top: 222px;
}

.game-message .lower {
  display: block;
  margin-top: 59px;
}

.game-message a {
  display: inline-block;
  background: var(--color-button);
  border-radius: 3px;
  padding: 0 20px;
  text-decoration: none;
  color: var(--color-text-light);
  height: 40px;
  line-height: 42px;
  cursor: pointer;
  margin-left: 9px;
}

.game-message.game-won {
  background: rgba(237, 194, 46, 0.5);
  color: #f9f6f2;
}

.game-message.game-won,
.game-message.game-over {
  display: flex;
}

.grid-container {
  position: absolute;
  z-index: 1;
  top: var(--grid-spacing);
  left: var(--grid-spacing);
}

.tile-container {
  position: absolute;
  z-index: 2;
  top: var(--grid-spacing);
  left: var(--grid-spacing);
}

.tile-position-1-1 {
  transform: translate(0px, 0px);
}

.tile-position-1-2 {
  transform: translate(0px, calc(var(--tile-size) + var(--grid-spacing)));
}

.tile-position-1-3 {
  transform: translate(0px, calc(2 * (var(--tile-size) + var(--grid-spacing))));
}

.tile-position-1-4 {
  transform: translate(0px, calc(3 * (var(--tile-size) + var(--grid-spacing))));
}

.tile-position-2-1 {
  transform: translate(calc(var(--tile-size) + var(--grid-spacing)), 0px);
}

.tile-position-2-2 {
  transform: translate(calc(var(--tile-size) + var(--grid-spacing)), calc(var(--tile-size) + var(--grid-spacing)));
}

.tile-position-2-3 {
  transform: translate(calc(var(--tile-size) + var(--grid-spacing)), calc(2 * (var(--tile-size) + var(--grid-spacing))));
}

.tile-position-2-4 {
  transform: translate(calc(var(--tile-size) + var(--grid-spacing)), calc(3 * (var(--tile-size) + var(--grid-spacing))));
}

.tile-position-3-1 {
  transform: translate(calc(2 * (var(--tile-size) + var(--grid-spacing))), 0px);
}

.tile-position-3-2 {
  transform: translate(calc(2 * (var(--tile-size) + var(--grid-spacing))), calc(var(--tile-size) + var(--grid-spacing)));
}

.tile-position-3-3 {
  transform: translate(calc(2 * (var(--tile-size) + var(--grid-spacing))), calc(2 * (var(--tile-size) + var(--grid-spacing))));
}

.tile-position-3-4 {
  transform: translate(calc(2 * (var(--tile-size) + var(--grid-spacing))), calc(3 * (var(--tile-size) + var(--grid-spacing))));
}

.tile-position-4-1 {
  transform: translate(calc(3 * (var(--tile-size) + var(--grid-spacing))), 0px);
}

.tile-position-4-2 {
  transform: translate(calc(3 * (var(--tile-size) + var(--grid-spacing))), calc(var(--tile-size) + var(--grid-spacing)));
}

.tile-position-4-3 {
  transform: translate(calc(3 * (var(--tile-size) + var(--grid-spacing))), calc(2 * (var(--tile-size) + var(--grid-spacing))));
}

.tile-position-4-4 {
  transform: translate(calc(3 * (var(--tile-size) + var(--grid-spacing))), calc(3 * (var(--tile-size) + var(--grid-spacing))));
}

.grid-row {
  margin-bottom: var(--grid-spacing);
}

.grid-row:last-child {
  margin-bottom: 0;
}

.grid-row:after {
  content: "";
  display: block;
  clear: both;
}

.grid-cell {
  width: var(--tile-size);
  height: var(--tile-size);
  margin-right: var(--grid-spacing);
  float: left;
  border-radius: 3px;
  background: var(--tile-color-empty);
}

.grid-cell:last-child {
  margin-right: 0;
}

.tile-container {
  position: absolute;
  z-index: 2;
}

.tile {
  width: var(--tile-size);
  height: var(--tile-size);
  line-height: var(--tile-size);
  border-radius: 3px;
  background: #eee4da;
  text-align: center;
  font-weight: bold;
  z-index: 10;
  font-size: 55px;
  position: absolute;
  transition: transform 100ms ease-in-out;
}

.tile.tile-2 {
  background: var(--tile-color-2);
  color: #776e65;
}

.tile.tile-4 {
  background: var(--tile-color-4);
  color: #776e65;
}

.tile.tile-8 {
  background: var(--tile-color-8);
  color: #f9f6f2;
}

.tile.tile-16 {
  background: var(--tile-color-16);
  color: #f9f6f2;
}

.tile.tile-32 {
  background: var(--tile-color-32);
  color: #f9f6f2;
}

.tile.tile-64 {
  background: var(--tile-color-64);
  color: #f9f6f2;
}

.tile.tile-128 {
  background: var(--tile-color-128);
  color: #f9f6f2;
  font-size: 45px;
}

.tile.tile-256 {
  background: var(--tile-color-256);
  color: #f9f6f2;
  font-size: 45px;
}

.tile.tile-512 {
  background: var(--tile-color-512);
  color: #f9f6f2;
  font-size: 45px;
}

.tile.tile-1024 {
  background: var(--tile-color-1024);
  color: #f9f6f2;
  font-size: 35px;
}

.tile.tile-2048 {
  background: var(--tile-color-2048);
  color: #f9f6f2;
  font-size: 35px;
}

.tile-new .tile-inner {
  animation: appear 200ms ease 100ms;
  animation-fill-mode: backwards;
}

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

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

  100% {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes pop {
  0% {
    transform: scale(0);
  }

  50% {
    transform: scale(1.2);
  }

  100% {
    transform: scale(1);
  }
}

@media screen and (max-width: 520px) {
  :root {
    --field-width: 280px;
    --grid-spacing: 10px;
  }

  h1.title {
    font-size: 27px;
    margin-top: 15px;
  }

  .container {
    width: 280px;
    margin: 0 auto;
  }

  .score-container,
  .best-container {
    margin-top: 0;
    padding: 15px 10px;
    min-width: 40px;
  }

  .heading {
    margin-bottom: 10px;
  }

  .game-container {
    margin-top: 10px;
  }

  .tile {
    font-size: 35px;
  }

  .tile.tile-128,
  .tile.tile-256,
  .tile.tile-512 {
    font-size: 25px;
  }

  .tile.tile-1024,
  .tile.tile-2048 {
    font-size: 15px;
  }
}