/* ---------------------------------- */
/* 1. General Styles
/* ---------------------------------- */

html,
body {
  font-size: 18px;
}

body {
  margin: 80px 0;
  padding: 0;
  background: #faf8ef;
  color: #776e65;
  font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
}

p {
  margin-top: 0;
  margin-bottom: 10px;
  line-height: 1.65;
}

a {
  color: #776e65;
  font-weight: bold;
  text-decoration: underline;
  cursor: pointer;
}

hr {
  border: none;
  border-bottom: 1px solid #d8d4d0;
  margin-top: 20px;
  margin-bottom: 30px;
}

/* ---------------------------------- */
/* 2. Layout & Containers
/* ---------------------------------- */

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

.heading::after {
  content: "";
  display: block;
  clear: both;
}

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

.scores-container {
  float: right;
  text-align: right;
}

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

.game-intro {
  margin: 0;
  font-size: 12px;
}

.game-explanation {
  margin-top: 50px;
}

/* ---------------------------------- */
/* 3. UI Components (Scores & Buttons)
/* ---------------------------------- */

.score-container,
.best-container,
.ai-container,
#undo-button {
  position: relative;
  display: inline-block;
  background: #bbada0;
  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;
}

.score-container::after,
.best-container::after,
.ai-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: "分数";
}

.best-container::after {
  content: "最高分";
}

.ai-container::after {
  content: "AI";
}

.score-container .score-addition,
.best-container .score-addition {
  position: absolute;
  right: 30px;
  font-size: 25px;
  line-height: 25px;
  font-weight: bold;
  color: rgba(119, 110, 101, 0.9);
  z-index: 100;
  animation: move-up 300ms ease-in;
  animation-fill-mode: both;
}

#undo-button.active {
  font-size: 32px;
  line-height: 27px;
  display: inline-block;
  margin-left: 120px;
  margin-top: 15px;
  width: 210px;
}

#undo-button.inactive {
  display: none;
}

.restart-button,
.set-board-button {
  display: inline-block;
  background: #8f7a66;
  border-radius: 3px;
  padding: 12px 20px;
  text-decoration: none;
  color: #f9f6f2;
  font-weight: bold;
  text-align: center;
  cursor: pointer;
  border: none;
}

.set-board-button {
  background: #b8860b;
}


/* ---------------------------------- */
/* 4. Game Board & Grid
/* ---------------------------------- */

.game-container {
  margin-top: 40px;
  position: relative;
  padding: 15px;
  background: #bbada0;
  border-radius: 6px;
  width: 500px;
  height: 500px;
  box-sizing: border-box;
  cursor: default;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  touch-action: none;
}

.grid-container {
  position: absolute;
  z-index: 1;
}

.grid-row {
  margin-bottom: 15px;
}

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

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

.grid-cell {
  width: 106.25px;
  height: 106.25px;
  margin-right: 15px;
  float: left;
  border-radius: 3px;
  background: rgba(238, 228, 218, 0.35);
}

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

/* ---------------------------------- */
/* 5. Game Messages (Win/Lose Overlay)
/* ---------------------------------- */

.game-message {
  display: none;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(238, 228, 218, 0.5);
  z-index: 100;
  text-align: center;
  animation: fade-in 300ms ease 500ms;
  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: #8f7a66;
  border-radius: 3px;
  padding: 0 20px;
  text-decoration: none;
  color: #f9f6f2;
  height: 40px;
  line-height: 42px;
  margin-left: 9px;
}

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

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

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

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

/* ---------------------------------- */
/* 6. Tiles
/* ---------------------------------- */

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

.tile,
.tile .tile-inner {
  width: 107px;
  height: 107px;
  line-height: 107px;
}

.tile {
  position: absolute;
  transition: transform 100ms ease-in-out;
}

.tile .tile-inner {
  border-radius: 3px;
  background: #eee4da;
  text-align: center;
  font-weight: bold;
  z-index: 10;
  font-size: 55px;
}

/* Tile Positioning */
.tile.tile-position-1-1 { transform: translate(0px, 0px); }
.tile.tile-position-1-2 { transform: translate(0px, 121px); }
.tile.tile-position-1-3 { transform: translate(0px, 242px); }
.tile.tile-position-1-4 { transform: translate(0px, 363px); }
.tile.tile-position-2-1 { transform: translate(121px, 0px); }
.tile.tile-position-2-2 { transform: translate(121px, 121px); }
.tile.tile-position-2-3 { transform: translate(121px, 242px); }
.tile.tile-position-2-4 { transform: translate(121px, 363px); }
.tile.tile-position-3-1 { transform: translate(242px, 0px); }
.tile.tile-position-3-2 { transform: translate(242px, 121px); }
.tile.tile-position-3-3 { transform: translate(242px, 242px); }
.tile.tile-position-3-4 { transform: translate(242px, 363px); }
.tile.tile-position-4-1 { transform: translate(363px, 0px); }
.tile.tile-position-4-2 { transform: translate(363px, 121px); }
.tile.tile-position-4-3 { transform: translate(363px, 242px); }
.tile.tile-position-4-4 { transform: translate(363px, 363px); }

/* Tile Number Specific Styles */
.tile.tile-2 .tile-inner { background: #eee4da; }
.tile.tile-4 .tile-inner { background: #ede0c8; }
.tile.tile-8 .tile-inner { color: #f9f6f2; background: #f2b179; }
.tile.tile-16 .tile-inner { color: #f9f6f2; background: #f59563; }
.tile.tile-32 .tile-inner { color: #f9f6f2; background: #f67c5f; }
.tile.tile-64 .tile-inner { color: #f9f6f2; background: #f65e3b; }

.tile.tile-128 .tile-inner,
.tile.tile-256 .tile-inner,
.tile.tile-512 .tile-inner {
  color: #f9f6f2;
  font-size: 45px;
}

.tile.tile-128 .tile-inner { background: #edcf72; box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.2381), inset 0 0 0 1px rgba(255, 255, 255, 0.14286); }
.tile.tile-256 .tile-inner { background: #edcc61; box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.31746), inset 0 0 0 1px rgba(255, 255, 255, 0.19048); }
.tile.tile-512 .tile-inner { background: #edc850; box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.39683), inset 0 0 0 1px rgba(255, 255, 255, 0.2381); }

.tile.tile-1024 .tile-inner,
.tile.tile-2048 .tile-inner {
  color: #f9f6f2;
  font-size: 35px;
}

.tile.tile-1024 .tile-inner { background: #edc53f; box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.47619), inset 0 0 0 1px rgba(255, 255, 255, 0.28571); }
.tile.tile-2048 .tile-inner { background: #edc22e; box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.55556), inset 0 0 0 1px rgba(255, 255, 255, 0.33333); }

.tile.tile-4096 .tile-inner { color: #f9f6f2; background: #77a136; font-size: 18px; }
.tile.tile-8192 .tile-inner { color: #f9f6f2; background: #2db388; font-size: 18px; }
.tile.tile-16384 .tile-inner { color: #f9f6f2; background: #2d83b3; font-size: 16px; }

.tile.tile-super .tile-inner {
  color: #f9f6f2;
  background: #3c3a32;
  font-size: 30px;
}

/* ---------------------------------- */
/* 7. Animations & Keyframes
/* ---------------------------------- */

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

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

@keyframes move-up {
  0%   { top: 25px; opacity: 1; }
  100% { top: -50px; opacity: 0; }
}

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

@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); }
}


/* ---------------------------------- */
/* 8. Modal Styles
/* ---------------------------------- */
.modal {
  position: fixed;
  z-index: 1000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgba(0, 0, 0, 0.4);
}

.modal-content {
  background-color: #faf8ef;
  margin: 5% auto;
  padding: 0;
  border: none;
  border-radius: 6px;
  width: 80%;
  max-width: 600px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.modal-header {
  padding: 20px;
  background-color: #8f7a66;
  color: #f9f6f2;
  border-radius: 6px 6px 0 0;
  position: relative;
}

.modal-header h2 {
  margin: 0;
  font-size: 24px;
}

.close {
  position: absolute;
  top: 15px;
  right: 20px;
  color: #f9f6f2;
  font-size: 28px;
  font-weight: bold;
  cursor: pointer;
}

.close:hover {
  opacity: 0.7;
}

.modal-body {
  padding: 20px;
}

.modal-body p {
  color: #776e65;
  font-weight: bold;
  margin-bottom: 10px;
}

.modal-buttons {
  margin-top: 20px;
  text-align: right;
}

.modal-buttons button {
  background: #8f7a66;
  color: #f9f6f2;
  border: none;
  border-radius: 3px;
  padding: 10px 20px;
  margin-left: 10px;
  cursor: pointer;
  font-weight: bold;
  font-size: 14px;
}

.modal-buttons button:hover {
  background: #9f8a76;
}

#resetToDefault { background: #b8860b !important; }
#resetToDefault:hover { background: #c8960b !important; }
#cancelSetBoard { background: #999 !important; }
#cancelSetBoard:hover { background: #aaa !important; }


/* ---------------------------------- */
/* 9. Grid Editor (in Modal)
/* ---------------------------------- */

#gameStateInput {
  width: 100%;
  padding: 10px;
  border: 2px solid #bbada0;
  border-radius: 3px;
  font-family: monospace;
  font-size: 12px;
  color: #776e65;
  background-color: #fbf8ef;
  resize: vertical;
  box-sizing: border-box;
}

#gameStateInput:focus {
  outline: none;
  border-color: #8f7a66;
}

.grid-editor {
  margin: 20px 0;
  display: inline-block;
  background: #bbada0;
  border-radius: 6px;
  padding: 15px;
}

.grid-editor-row {
  display: flex;
  margin-bottom: 10px;
}

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

.grid-cell-input {
  width: 60px;
  height: 60px;
  margin-right: 10px;
  border: none;
  border-radius: 3px;
  background: #eee4da;
  text-align: center;
  font-weight: bold;
  font-size: 18px;
  color: #776e65;
  outline: none;
}

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

.grid-cell-input:focus {
  background: #edcf72;
  color: #f9f6f2;
}

/* Hide number input spinners */
.grid-cell-input[type="number"] {
  -moz-appearance: textfield;
}
.grid-cell-input::-webkit-outer-spin-button,
.grid-cell-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.quick-actions {
  margin: 20px 0;
  text-align: center;
}

.quick-actions p {
  margin-bottom: 10px;
}

.quick-actions button {
  background: #8f7a66;
  color: #f9f6f2;
  border: none;
  border-radius: 3px;
  padding: 8px 16px;
  margin: 0 5px;
  cursor: pointer;
  font-weight: bold;
  font-size: 12px;
}

.quick-actions button:hover {
  background: #9f8a76;
}

#setDefaultGrid { background: #b8860b !important; }
#setDefaultGrid:hover { background: #c8960b !important; }
#setRandomGrid { background: #2db388 !important; }
#setRandomGrid:hover { background: #3dc398 !important; }


/* ---------------------------------- */
/* 10. Media Queries (Responsive)
/* ---------------------------------- */

@media screen and (max-width: 480px) {
  #undo-button.active {
    margin-left: 78px;
    margin-top: 10px;
    width: 76px;
    height: 20px;
    line-height: 23px;
    font-size: 25px;
  }
}

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

  body {
    margin: 20px 0;
    padding: 0 20px;
  }

  .container {
    width: 280px;
  }

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

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

  .above-game {
    flex-direction: column;
  }

  .game-intro {
    width: 100%;
    text-align: center;
  }
  
  .restart-button {
      margin-top: 10px;
  }

  .game-container {
    margin-top: 17px;
    width: 280px;
    height: 280px;
    padding: 10px;
  }

  .grid-row {
    margin-bottom: 10px;
  }

  .grid-cell {
    width: 57.5px;
    height: 57.5px;
    margin-right: 10px;
  }
  
  .tile,
  .tile .tile-inner {
    width: 58px;
    height: 58px;
    line-height: 58px;
  }

  /* Tile positions for small screen */
  .tile.tile-position-1-1 { transform: translate(0px, 0px); }
  .tile.tile-position-1-2 { transform: translate(0px, 67px); }
  .tile.tile-position-1-3 { transform: translate(0px, 135px); }
  .tile.tile-position-1-4 { transform: translate(0px, 202px); }
  .tile.tile-position-2-1 { transform: translate(67px, 0px); }
  .tile.tile-position-2-2 { transform: translate(67px, 67px); }
  .tile.tile-position-2-3 { transform: translate(67px, 135px); }
  .tile.tile-position-2-4 { transform: translate(67px, 202px); }
  .tile.tile-position-3-1 { transform: translate(135px, 0px); }
  .tile.tile-position-3-2 { transform: translate(135px, 67px); }
  .tile.tile-position-3-3 { transform: translate(135px, 135px); }
  .tile.tile-position-3-4 { transform: translate(135px, 202px); }
  .tile.tile-position-4-1 { transform: translate(202px, 0px); }
  .tile.tile-position-4-2 { transform: translate(202px, 67px); }
  .tile.tile-position-4-3 { transform: translate(202px, 135px); }
  .tile.tile-position-4-4 { transform: translate(202px, 202px); }
  
  .tile .tile-inner {
    font-size: 35px;
  }

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

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

  .game-message p {
    font-size: 30px !important;
    height: 30px !important;
    line-height: 30px !important;
    margin-top: 90px !important;
  }

  .game-message .lower {
    margin-top: 30px !important;
  }
}
