/* ===== Reset & Base ===== */
* { margin: 0; padding: 0; box-sizing: border-box; }
html { touch-action: manipulation; -ms-touch-action: manipulation; }

body {
  background: #fff;
  color: #333;
  font-family: 'Segoe UI', 'Noto Sans TC', sans-serif;
  overflow: hidden;
  width: 100vw;
  height: 100vh;
}

.page {
  display: none;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0; left: 0;
}
.page.active { display: flex; }

.btn {
  padding: 12px 32px;
  border: none;
  border-radius: 8px;
  font-size: 1.2rem;
  cursor: pointer;
  font-weight: bold;
  transition: transform 0.1s, opacity 0.2s;
}
.btn:hover { transform: scale(1.05); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

.btn-coin {
  background: #f0b429;
  color: #fff;
}
.btn-start {
  background: #e94560;
  color: #fff;
}
.btn-confirm {
  background: #4a7dff;
  color: #fff;
}

/* ===== Coin Page ===== */
#coin-page {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
  background: #fff;
}

.game-title {
  font-size: 3rem;
  color: #e94560;
  text-shadow: none;
}

.coin-display {
  font-size: 2.5rem;
  display: flex;
  align-items: center;
  gap: 12px;
  color: #333;
}

.coin-icon {
  animation: coin-spin 1s ease-in-out infinite;
}

@keyframes coin-spin {
  0%, 100% { transform: scaleX(1); }
  50% { transform: scaleX(0.1); }
}

.blink-text {
  font-size: 1.5rem;
  letter-spacing: 4px;
  animation: blink 1s step-end infinite;
  color: #e94560;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

.coin-buttons {
  display: flex;
  gap: 20px;
}

.hint-text {
  font-size: 0.9rem;
  color: #999;
}

.sound-reminder {
  margin-top: 12px;
  padding: 8px 16px;
  background: #fff3cd;
  color: #856404;
  border-radius: 8px;
  font-size: 0.85rem;
  animation: pulse-glow 2s ease-in-out infinite;
}
.sound-reminder.hidden { display: none; }

@keyframes pulse-glow {
  0%, 100% { opacity: 0.7; }
  50% { opacity: 1; }
}

/* Coin insert animation */
.coin-fly {
  position: fixed;
  font-size: 2rem;
  z-index: 999;
  pointer-events: none;
  animation: coin-insert 0.5s ease-in forwards;
}

@keyframes coin-insert {
  0% { transform: translateY(0) scale(1); opacity: 1; }
  100% { transform: translateY(-80px) scale(0.3); opacity: 0; }
}

/* ===== Select Page ===== */
#select-page {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 40px;
  background: #fff;
}

.select-title {
  font-size: 2.2rem;
  color: #333;
}

.song-carousel-wrapper {
  width: 100%;
  overflow: hidden;
  position: relative;
}

.song-carousel {
  display: flex;
  align-items: center;
  gap: 20px;
  transition: transform 0.3s ease;
}

.song-card {
  width: 180px;
  height: 220px;
  background: #f8f8f8;
  border: 3px solid #ddd;
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  transition: all 0.3s ease;
  opacity: 0.5;
  transform: scale(0.85);
  cursor: pointer;
  flex-shrink: 0;
}

.song-card.selected {
  border-color: #e94560;
  opacity: 1;
  transform: scale(1.1);
  box-shadow: 0 4px 20px rgba(233, 69, 96, 0.25);
}

.song-card .song-emoji { font-size: 3rem; }
.song-card .song-name { font-size: 0.95rem; font-weight: bold; color: #333; text-align: center; line-height: 1.3; padding: 0 8px; }
.song-card .song-difficulty { font-size: 0.9rem; color: #f0b429; }

.select-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 0 16px;
  width: 100%;
}

.btn-nav {
  width: 48px;
  height: 48px;
  min-width: 48px;
  flex-shrink: 0;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: #e0e0e0;
  font-size: 1.4rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
.btn-nav:hover { background: #ccc; }
.btn-nav:active { background: #bbb; }

.btn-quit {
  background: none;
  border: none;
  font-size: 1.3rem;
  color: #999;
  cursor: pointer;
  padding: 4px 8px;
  margin-left: 8px;
  line-height: 1;
}
.btn-quit:hover { color: #e94560; }

/* ===== Game Page ===== */
#game-page {
  flex-direction: column;
  background: #f0f0f0;
}

.game-hud {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 24px;
  background: #fff;
  border-bottom: 2px solid #eee;
  height: 50px;
  z-index: 10;
}

.hud-left, .hud-right { font-size: 1rem; color: #888; }

.hud-center { text-align: center; }

.score-display {
  font-size: 1.4rem;
  font-weight: bold;
  color: #e94560;
}

.combo-display {
  position: fixed;
  top: 40%;
  left: 50%;
  transform: translateX(-50%);
  font-size: 2rem;
  color: #e94560;
  font-weight: bold;
  min-height: 1.4em;
  z-index: 50;
  text-shadow: 0 2px 8px rgba(233, 69, 96, 0.3);
  pointer-events: none;
}

.combo-display.combo-pop {
  animation: combo-pop 0.35s cubic-bezier(0.17, 0.89, 0.32, 1.49);
}

@keyframes combo-pop {
  0% { transform: translateX(-50%) scale(1.8); }
  50% { transform: translateX(-50%) scale(0.9); }
  100% { transform: translateX(-50%) scale(1); }
}

/* Game Area */
.game-area {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: stretch;
  gap: 0;
  position: relative;
}

.lane-section {
  display: flex;
  align-items: stretch;
  position: relative;
}

.lane-section.lane-left {
  flex-direction: row;
}

.lane-section.lane-right {
  flex-direction: row;
}

/* Character */
.character {
  width: 120px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding-bottom: 60px;
  z-index: 5;
  position: relative;
}

.char-sprite {
  width: 110px;
  height: auto;
  image-rendering: auto;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.1));
  transition: transform 0.1s;
}

.char-sprite.bounce {
  animation: char-bounce 0.4s cubic-bezier(0.17, 0.89, 0.32, 1.49);
}

@keyframes char-bounce {
  0% { transform: scale(1) translateY(0); }
  20% { transform: scale(1.15) translateY(-12px); }
  50% { transform: scale(0.95) translateY(2px); }
  100% { transform: scale(1) translateY(0); }
}

.char-label {
  font-size: 1rem;
  font-weight: bold;
  margin-top: 4px;
  color: #555;
}

/* Particle effects */
.particle {
  position: absolute;
  top: 55%;
  left: 50%;
  font-size: 1.5rem;
  pointer-events: none;
  z-index: 20;
  opacity: 0;
  transform: translate(-50%, -50%) scale(var(--size, 1));
  animation: particle-burst 0.7s ease-out forwards;
  animation-delay: var(--delay, 0ms);
}

@keyframes particle-burst {
  0% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(0.3);
  }
  20% {
    opacity: 1;
    transform: translate(
      calc(-50% + var(--dx) * 0.3),
      calc(-50% + var(--dy) * 0.3)
    ) scale(var(--size, 1));
  }
  100% {
    opacity: 0;
    transform: translate(
      calc(-50% + var(--dx)),
      calc(-50% + var(--dy) - 20px)
    ) scale(0.2);
  }
}

/* Lane */
.lane {
  width: 100px;
  position: relative;
  overflow: hidden;
  background: #fff;
}

.lane-bg {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: linear-gradient(to bottom, rgba(0,0,0,0.01), rgba(0,0,0,0.03));
  border-left: 2px solid #e0e0e0;
  border-right: 2px solid #e0e0e0;
}

.hit-line {
  position: absolute;
  bottom: 60px;
  left: 0;
  right: 0;
  height: 4px;
  background: #e94560;
  box-shadow: 0 0 8px rgba(233, 69, 96, 0.5);
  z-index: 3;
}

.key-indicator {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 1.5rem;
  font-weight: bold;
  color: #bbb;
  z-index: 3;
  width: 44px;
  height: 44px;
  border: 2px solid #ccc;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f8f8f8;
}

.key-indicator.pressed {
  color: #e94560;
  border-color: #e94560;
  background: rgba(233, 69, 96, 0.15);
  animation: key-bounce 0.15s ease-out;
}

@keyframes key-bounce {
  0% { transform: translateX(-50%) scale(1.3); }
  100% { transform: translateX(-50%) scale(1); }
}

/* Notes */
.note {
  position: absolute;
  width: 70px;
  height: 24px;
  left: 50%;
  transform: translateX(-50%);
  border-radius: 6px;
  z-index: 2;
}

.note.lane-cat {
  background: linear-gradient(135deg, #6ec6ff, #42a5f5);
  box-shadow: 0 2px 6px rgba(66, 165, 245, 0.4);
}

.note.lane-dog {
  background: linear-gradient(135deg, #ffab91, #ff7043);
  box-shadow: 0 2px 6px rgba(255, 112, 67, 0.4);
}

.note.hit {
  animation: note-hit 0.3s ease-out forwards;
}

@keyframes note-hit {
  0% { transform: translateX(-50%) scale(1); opacity: 1; }
  40% { transform: translateX(-50%) scale(1.8); opacity: 0.8; }
  100% { transform: translateX(-50%) scale(2.2); opacity: 0; }
}

/* Lane flash on hit */
.lane-flash {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 120px;
  z-index: 1;
  pointer-events: none;
  opacity: 0;
}

.lane-flash.flash-cat {
  background: linear-gradient(to top, rgba(66, 165, 245, 0.4), transparent);
  animation: lane-glow 0.3s ease-out forwards;
}

.lane-flash.flash-dog {
  background: linear-gradient(to top, rgba(255, 112, 67, 0.4), transparent);
  animation: lane-glow 0.3s ease-out forwards;
}

@keyframes lane-glow {
  0% { opacity: 1; }
  100% { opacity: 0; }
}

/* Hit line pulse */
.hit-line.pulse {
  animation: hitline-pulse 0.3s ease-out;
}

@keyframes hitline-pulse {
  0% { transform: scaleY(3); box-shadow: 0 0 20px rgba(233, 69, 96, 0.9); }
  100% { transform: scaleY(1); box-shadow: 0 0 8px rgba(233, 69, 96, 0.5); }
}

/* Screen shake */
.game-area.shake {
  animation: screen-shake 0.15s ease-out;
}

@keyframes screen-shake {
  0% { transform: translateX(0); }
  25% { transform: translateX(-3px) translateY(-2px); }
  50% { transform: translateX(3px) translateY(1px); }
  75% { transform: translateX(-2px) translateY(-1px); }
  100% { transform: translateX(0); }
}

/* Judge text */
.judge-text {
  position: absolute;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 1.6rem;
  font-weight: 900;
  z-index: 10;
  pointer-events: none;
  opacity: 0;
  text-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.judge-text.show {
  animation: judge-show 0.7s cubic-bezier(0.17, 0.89, 0.32, 1.28) forwards;
}

@keyframes judge-show {
  0% { opacity: 1; transform: translateX(-50%) translateY(10px) scale(1.8); }
  25% { opacity: 1; transform: translateX(-50%) translateY(-5px) scale(1); }
  70% { opacity: 1; transform: translateX(-50%) translateY(-15px) scale(1); }
  100% { opacity: 0; transform: translateX(-50%) translateY(-30px) scale(0.7); }
}

.judge-text.perfect { color: #f0b429; }
.judge-text.great { color: #4caf50; }
.judge-text.good { color: #42a5f5; }

/* Countdown */
.countdown-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(255,255,255,0.7);
  z-index: 100;
}

.countdown-overlay.show {
  display: flex;
}

#countdown-text {
  font-size: 6rem;
  font-weight: bold;
  color: #e94560;
  text-shadow: 0 2px 10px rgba(233, 69, 96, 0.3);
  animation: countdown-pulse 0.8s ease-out;
}

@keyframes countdown-pulse {
  0% { transform: scale(2); opacity: 0; }
  50% { opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

/* ===== Result Page ===== */
#result-page {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #fff;
}

.result-container {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.result-title { font-size: 2rem; color: #333; }

.result-song { font-size: 1.3rem; color: #888; }

.result-grade {
  font-size: 5rem;
  font-weight: bold;
  color: #e94560;
  text-shadow: none;
}

.result-score {
  font-size: 1.8rem;
  color: #f0b429;
  font-weight: bold;
}

.result-stats {
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 1.2rem;
  min-width: 250px;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  padding: 6px 16px;
  border-radius: 8px;
  background: #f5f5f5;
}

.stat-row.perfect { color: #f0b429; }
.stat-row.great { color: #4caf50; }
.stat-row.good { color: #42a5f5; }
.stat-row.max-combo { color: #e94560; }

/* Leaderboard */
.leaderboard {
  width: 100%;
  min-width: 280px;
  max-width: 350px;
  margin-top: 12px;
}
.lb-title {
  font-size: 1.1rem;
  color: #e94560;
  margin-bottom: 8px;
  text-align: center;
}
.lb-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 200px;
  overflow-y: auto;
}
.lb-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 8px;
  background: #f8f8f8;
  font-size: 0.95rem;
}
.lb-row.lb-current {
  background: #fff3e0;
  border: 2px solid #f0b429;
}
.lb-rank { width: 28px; text-align: center; font-weight: bold; }
.lb-name { flex: 1; font-weight: bold; color: #333; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-score { width: 60px; text-align: right; font-weight: bold; color: #555; }
.lb-grade { width: 28px; text-align: center; color: #e94560; font-weight: bold; margin-left: 6px; }
.lb-empty { text-align: center; color: #ccc; padding: 12px; }

/* Leaderboard name input */
.lb-name-prompt { text-align: center; padding: 12px 0; }
.lb-congrats { font-size: 1.1rem; font-weight: bold; color: #f0b429; margin-bottom: 10px; }
.lb-name-form { display: flex; gap: 8px; justify-content: center; align-items: center; }
.lb-name-input {
  padding: 8px 12px;
  font-size: 1rem;
  border: 2px solid #f0b429;
  border-radius: 8px;
  outline: none;
  width: 140px;
  text-align: center;
  font-family: inherit;
  background: #fff8e1;
}
.lb-name-input:focus { border-color: #e94560; box-shadow: 0 0 0 2px rgba(233,69,96,0.2); }
.lb-name-btn { padding: 8px 16px !important; font-size: 0.9rem !important; min-width: auto !important; }

/* Touch Controls (mobile) */
.touch-controls {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 200;
  gap: 0;
}
.touch-btn {
  flex: 1;
  padding: 28px 0;
  font-size: 1.5rem;
  font-weight: bold;
  border: none;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.touch-left {
  background: rgba(66, 165, 245, 0.2);
  color: #42a5f5;
  border-right: 2px solid #e0e0e0;
}
.touch-right {
  background: rgba(255, 112, 67, 0.2);
  color: #ff7043;
}
.touch-btn:active {
  filter: brightness(0.85);
  transform: scale(0.97);
}

/* Mobile responsive */
@media (max-width: 768px) {
  .touch-controls {
    display: flex;
  }
  .game-hud {
    padding: 4px 12px;
    height: 40px;
    font-size: 0.85rem;
  }
  .score-display { font-size: 1.1rem; }
  /* Mobile: characters repositioned by JS to #game-page */
  .character.mobile-moved {
    position: absolute !important;
    top: 50px;
    z-index: 50;
    width: auto !important;
    padding: 0 !important;
    pointer-events: none;
    display: flex !important;
    flex-direction: column;
    align-items: center;
  }
  .cat-character.mobile-moved {
    left: 4px;
  }
  .dog-character.mobile-moved {
    right: 4px;
  }
  .mobile-moved .char-sprite { width: 55px !important; }
  .mobile-moved .char-label { font-size: 0.65rem; }
  .lane-section { flex: 1; }
  .lane { flex: 1; width: auto; overflow: hidden; }
  .game-area { padding-bottom: 80px; }
  .note { width: 55px; height: 20px; }
  .key-indicator { display: none; }
  .hit-line { bottom: 60px; }
  .combo-display { font-size: 1.4rem; top: 30%; }
  .judge-text { font-size: 1.2rem; bottom: 80px; }

  /* Coin page mobile */
  .game-title { font-size: 2rem; }
  .coin-display { font-size: 1.8rem; }
  .blink-text { font-size: 1.1rem; }

  /* Select page mobile */
  .select-title { font-size: 1.6rem; }
  .song-card { width: 140px; height: 180px; }
  .song-card .song-emoji { font-size: 2.2rem; }
  .song-card .song-name { font-size: 0.85rem; }

  /* Result page mobile */
  .result-grade { font-size: 3.5rem; }
  .result-score { font-size: 1.4rem; }
  .result-container { padding: 16px; }
}

.result-buttons {
  display: flex;
  gap: 16px;
  margin-top: 16px;
}
