* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Noto Sans TC', sans-serif;
    background-color: #FFF5E6;
    color: #333;
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

.container {
    max-width: 500px;
    width: 100%;
    padding: 20px;
    margin: 0 auto;
}

header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 20px;
}

h1 {
    font-size: 48px;
    color: #FF6B6B;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}

.score-container {
    display: flex;
    gap: 10px;
}

.score-box {
    background-color: #FFD166;
    padding: 10px 15px;
    border-radius: 8px;
    text-align: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.score-label {
    font-size: 14px;
    color: #666;
}

.score {
    font-size: 24px;
    font-weight: bold;
    color: #333;
}

.game-container {
    background-color: #FFE8D6;
    border-radius: 12px;
    padding: 15px;
    margin-bottom: 20px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.grid-container {
    display: grid;
    grid-template-rows: repeat(4, 1fr);
    gap: 10px;
    background-color: #FFD166;
    padding: 10px;
    border-radius: 8px;
}

.grid-row {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
}

.grid-cell {
    background-color: #FFE8D6;
    aspect-ratio: 1;
    border-radius: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 24px;
    font-weight: bold;
    transition: all 0.2s ease;
}

.tile {
    position: absolute;
    width: 95px;
    height: 95px;
    background-color: #FFB4A2;
    border-radius: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 24px;
    font-weight: bold;
    color: #333;
    /* Default animation will be overridden when tiles are created */
    animation: tile-appear 0.2s ease;
    transition: all 0.2s ease;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2),
                0 2px 4px rgba(0, 0, 0, 0.1),
                inset 0 1px 0 rgba(255, 255, 255, 0.3);
}

.tile[data-value="2"] { background-color: #FFB4A2; }
.tile[data-value="4"] { background-color: #FF8C8C; }
.tile[data-value="8"] { background-color: #FF6B6B; }
.tile[data-value="16"] { background-color: #FF4D4D; }
.tile[data-value="32"] { background-color: #FF3333; }
.tile[data-value="64"] { background-color: #FF1A1A; }
.tile[data-value="128"] { background-color: #FF0000; }
.tile[data-value="256"] { background-color: #E60000; }
.tile[data-value="512"] { background-color: #CC0000; }
.tile[data-value="1024"] { background-color: #B30000; }
.tile[data-value="2048"] { background-color: #990000; }

.controls {
    display: flex;
    justify-content: center;
    gap: 20px;
    margin-bottom: 20px;
}

.btn {
    background-color: #FF6B6B;
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 8px;
    font-size: 16px;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.btn:hover {
    background-color: #FF4D4D;
    transform: translateY(-2px);
}

.btn:disabled {
    background-color: #e0e0e0 !important;
    color: #aaa !important;
    cursor: not-allowed !important;
    box-shadow: none !important;
    border: none !important;
    opacity: 1 !important;
}

.game-over {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: white;
    padding: 30px;
    border-radius: 12px;
    text-align: center;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    display: none;
    z-index: 3000;
}

.game-over h2 {
    color: #FF6B6B;
    margin-bottom: 20px;
}

.final-score {
    font-size: 24px;
    margin-bottom: 20px;
}

.high-scores {
    margin-bottom: 20px;
}

.high-scores h3 {
    color: #666;
    margin-bottom: 10px;
}

#high-scores-list {
    list-style-position: inside;
    text-align: left;
}

@media (max-width: 500px) {
    .container {
        padding: 10px;
        max-width: 100%;
    }
    
    h1 {
        font-size: 36px;
    }
    
    .score-box {
        padding: 8px 12px;
    }
    
    .score-label {
        font-size: 12px;
    }
    
    .score {
        font-size: 20px;
    }
    
    .game-container {
        padding: 10px;
    }
    
    .grid-container {
        gap: 8px;
        padding: 8px;
    }
    
    .grid-row {
        gap: 8px;
    }
    
    .tile {
        width: calc(25vw - 20px);
        height: calc(25vw - 20px);
        font-size: 18px;
    }
    
    .btn {
        padding: 8px 16px;
        font-size: 14px;
    }
    
    .game-over {
        width: 90%;
        max-width: 300px;
        padding: 20px;
    }
    
    .game-over h2 {
        font-size: 24px;
    }
    
    .final-score {
        font-size: 20px;
    }
    
    .high-scores h3 {
        font-size: 18px;
    }
}

/* 針對小螢幕手機的額外優化 */
@media (max-width: 360px) {
    .tile {
        width: calc(25vw - 15px);
        height: calc(25vw - 15px);
        font-size: 16px;
    }
    
    h1 {
        font-size: 32px;
    }
    
    .score-box {
        padding: 6px 10px;
    }
    
    .score-label {
        font-size: 11px;
    }
    
    .score {
        font-size: 18px;
    }
}

/* 針對平板設備的優化 */
@media (min-width: 501px) and (max-width: 768px) {
    .container {
        max-width: 90%;
    }
    
    .tile {
        width: calc(25vw - 30px);
        height: calc(25vw - 30px);
    }
}

@keyframes tile-appear {
  from { transform: scale(0.8); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.modal-content {
  background: #fff;
  border-radius: 12px;
  padding: 32px 24px 24px 24px;
  max-width: 400px;
  width: 90vw;
  box-shadow: 0 4px 24px rgba(0,0,0,0.15);
  text-align: center;
  position: relative;
}
.modal-content h2 {
  margin-bottom: 16px;
}
.modal-content ul {
  text-align: left;
  margin: 16px 0 24px 0;
  padding-left: 20px;
}
.close-x {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  font-size: 28px;
  color: #888;
  cursor: pointer;
  z-index: 10;
  transition: color 0.2s;
}
.close-x:hover {
  color: #FF6B6B;
}
#rules-desc {
  margin-bottom: 24px;
}

.game-over .close-x {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  font-size: 28px;
  color: #888;
  cursor: pointer;
  z-index: 10;
  transition: color 0.2s;
}
.game-over .close-x:hover {
  color: #FF6B6B;
}
.final-round {
  font-size: 20px;
  margin-bottom: 12px;
}
.save-result {
  margin-bottom: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
#player-name {
  padding: 6px 10px;
  font-size: 16px;
  border-radius: 6px;
  border: 1px solid #ccc;
  margin-bottom: 8px;
}
#save-share {
  margin-bottom: 8px;
}

.milestone-animate {
  animation: milestone-pop 0.7s cubic-bezier(.36,1.56,.64,1) both;
}
@keyframes milestone-pop {
  0% { transform: scale(1); opacity: 0.2; }
  30% { transform: scale(1.3); opacity: 1; }
  60% { transform: scale(0.95); }
  100% { transform: scale(1); opacity: 1; }
}
.milestone-message {
  position: fixed;
  top: 80px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 3000;
  font-size: 2rem;
  font-weight: bold;
  padding: 18px 36px;
  border-radius: 16px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.18);
  color: #fff;
  pointer-events: none;
  opacity: 0.98;
  transition: background 0.3s;
}
.milestone-8    { background: #fff200; color: #222; box-shadow: 0 0 24px #fff20099; }
.milestone-16   { background: #ff9800; color: #fff; box-shadow: 0 0 24px #ff980099; }
.milestone-32   { background: #ff5722; color: #fff; box-shadow: 0 0 24px #ff572299; }
.milestone-64   { background: #e91e63; color: #fff; box-shadow: 0 0 24px #e91e6399; }
.milestone-128  { background: #9c27b0; color: #fff; box-shadow: 0 0 24px #9c27b099; }
.milestone-256  { background: #3f51b5; color: #fff; box-shadow: 0 0 24px #3f51b599; }
.milestone-512  { background: #2196f3; color: #fff; box-shadow: 0 0 24px #2196f399; }
.milestone-1024 { background: #009688; color: #fff; box-shadow: 0 0 24px #00968899; }
.milestone-2048 { background: #212121; color: #fff; box-shadow: 0 0 32px #212121cc; border: 2px solid #fff; }

.tile-explode {
    animation: tile-explode 0.5s cubic-bezier(.36,1.56,.64,1) both;
    z-index: 10;
}
@keyframes tile-explode {
    0% { transform: scale(1); opacity: 1; box-shadow: 0 0 0 #fff; }
    60% { transform: scale(1.5); opacity: 0.8; box-shadow: 0 0 32px #fff8, 0 0 64px #fff4; }
    100% { transform: scale(2.2); opacity: 0; box-shadow: 0 0 0 #fff0; }
}

.milestone-shake {
    animation: milestone-shake 1s cubic-bezier(.36,1.56,.64,1) both;
    z-index: 20;
}
@keyframes milestone-shake {
    0% { transform: scale(1); }
    10% { transform: scale(1.25) rotate(-2deg); }
    20% { transform: scale(1.15) rotate(2deg); }
    30% { transform: scale(1.3) rotate(-3deg); }
    40% { transform: scale(1.2) rotate(3deg); }
    50% { transform: scale(1.35) rotate(-2deg); }
    60% { transform: scale(1.2) rotate(2deg); }
    70% { transform: scale(1.15) rotate(-1deg); }
    80% { transform: scale(1.1) rotate(1deg); }
    90% { transform: scale(1.05); }
    100% { transform: scale(1); }
}

.milestone-flash {
    animation: milestone-flash 1s linear both !important;
    background: linear-gradient(90deg, #fff200, #ff9800) !important;
}
@keyframes milestone-flash {
    0% { background: linear-gradient(90deg, #fff200, #ff9800) !important; }
    20% { background: linear-gradient(90deg, #ff9800, #ff5722) !important; }
    40% { background: linear-gradient(90deg, #ff5722, #e91e63) !important; }
    60% { background: linear-gradient(90deg, #e91e63, #3f51b5) !important; }
    80% { background: linear-gradient(90deg, #3f51b5, #00e676) !important; }
    100% { background: linear-gradient(90deg, #fff200, #ff9800) !important; }
}

.milestone-number-zoom {
    animation: milestone-number-zoom 1s cubic-bezier(.36,1.56,.64,1) both;
}
@keyframes milestone-number-zoom {
    0% { font-size: 1em; }
    20% { font-size: 1.5em; }
    50% { font-size: 2em; }
    80% { font-size: 1.5em; }
    100% { font-size: 1em; }
}

#game-title.super-milestone {
  color: #fff;
  background: linear-gradient(90deg, #ff9800, #fff200, #ff9800, #e91e63, #3f51b5, #009688, #212121);
  font-size: 3.2em;
  font-weight: 900;
  padding: 0.2em 0.6em;
  border-radius: 16px;
  box-shadow: 0 0 32px #ff0b, 0 0 64px #f80b;
  letter-spacing: 0.1em;
  animation: super-milestone-pop 1.2s cubic-bezier(.36,1.56,.64,1) both;
}
@keyframes super-milestone-pop {
  0% { transform: scale(1); box-shadow: 0 0 0 #fff200; }
  30% { transform: scale(1.2); box-shadow: 0 0 32px #fff200; }
  60% { transform: scale(0.95); }
  100% { transform: scale(1); box-shadow: 0 0 64px #f80b; }
}
#medals {
  min-height: 40px;
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin: 10px 0;
  max-width: 220px;
}
#medals .medal-icon {
  flex: 0 0 25%;
  box-sizing: border-box;
}
.medal-icon {
  width: auto;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  background: none;
  box-shadow: none;
  border: none;
  margin: 0 2px;
  position: relative;
  transition: transform 0.2s;
  padding: 0 4px;
}
.medal-label {
  color: #e53935;
  font-weight: bold;
  font-size: 18px;
  margin-left: 0.5px;
  margin-right: 2px;
  letter-spacing: 0.5px;
  vertical-align: middle;
}
.medal-icon.milestone {
  animation: medal-pop 0.7s cubic-bezier(.36,1.56,.64,1) both;
}
@keyframes medal-pop {
  0% { transform: scale(0.7); opacity: 0.2; }
  30% { transform: scale(1.2); opacity: 1; }
  60% { transform: scale(0.95); }
  100% { transform: scale(1); opacity: 1; }
}

@media (max-width: 600px) {
  header {
    gap: 8px;
  }
  #medals {
    justify-content: flex-start;
    margin: 8px 0;
    max-width: 100%;
  }
  .score-container {
    flex-direction: row !important;
    align-items: center !important;
    gap: 10px !important;
    min-width: 0 !important;
  }
}

