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

body {
  font-family: -apple-system, "PingFang TC", "Microsoft JhengHei", sans-serif;
  background: linear-gradient(180deg, #87CEEB 0%, #B0E0E6 60%, #98D98E 60%, #7CB342 100%);
  min-height: 100vh;
  color: #2c3e50;
  user-select: none;
}

#app {
  max-width: 900px;
  margin: 0 auto;
  min-height: 100vh;
  position: relative;
}

.screen {
  display: none;
  padding: 30px 20px;
  min-height: 100vh;
  flex-direction: column;
  align-items: center;
}

.screen.active { display: flex; }

h1 {
  font-size: 2.4em;
  margin-bottom: 8px;
  text-shadow: 2px 2px 0 rgba(255,255,255,0.6);
}

.subtitle {
  font-size: 1.1em;
  color: #555;
  margin-bottom: 30px;
}

/* ---------- Menu ---------- */
.setting {
  background: rgba(255,255,255,0.85);
  border-radius: 16px;
  padding: 18px 24px;
  margin: 12px 0;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.setting label {
  font-size: 1.15em;
  font-weight: 600;
  display: block;
  margin-bottom: 8px;
}

input[type="range"] {
  width: 100%;
  height: 8px;
  accent-color: #FF6B6B;
}

input[type="text"] {
  width: 100%;
  font-size: 1.1em;
  padding: 10px 14px;
  border: 2px solid #FFA726;
  border-radius: 10px;
  outline: none;
  font-family: inherit;
}
input[type="text"]:focus {
  border-color: #FF6B6B;
  box-shadow: 0 0 0 3px rgba(255,107,107,0.2);
}
.shake-input {
  animation: shake 0.4s;
  border-color: #B71C1C !important;
}

/* ---------- Menu Layout ---------- */
.menu-grid {
  display: grid;
  grid-template-columns: minmax(320px, 1fr) minmax(320px, 1fr);
  gap: 24px;
  width: 100%;
  max-width: 900px;
  align-items: start;
}
.menu-left, .menu-right { display: flex; flex-direction: column; align-items: center; }
.menu-left { gap: 12px; }
.menu-left .setting, .menu-left .big-btn { width: 100%; max-width: 420px; }

@media (max-width: 760px) {
  .menu-grid { grid-template-columns: 1fr; }
}

/* ---------- Leaderboard ---------- */
.leaderboard { width: 100%; max-width: 480px; }
.leaderboard .lb-head {
  margin-bottom: 8px;
}
.leaderboard h3 { color: #E65100; font-size: 1.1em; }

#lb-sections {
  max-height: 600px;
  overflow-y: auto;
  padding-right: 4px;
}

.lb-section { margin-bottom: 14px; }
.lb-section h4 {
  font-size: 0.95em;
  color: #C43E3E;
  background: linear-gradient(90deg, #FFE0B2, transparent);
  padding: 4px 8px;
  border-left: 4px solid #FF7A1A;
  border-radius: 4px;
  margin-bottom: 4px;
}

.lb-list {
  list-style: none;
  counter-reset: rank;
  font-family: "Courier New", monospace;
}
.lb-list li {
  display: grid;
  grid-template-columns: 28px 1fr auto auto;
  align-items: center;
  gap: 8px;
  padding: 5px 4px;
  border-bottom: 1px dashed #eee;
  counter-increment: rank;
  font-size: 0.92em;
}
.lb-list li::before {
  content: counter(rank);
  font-weight: bold;
  color: #E65100;
  text-align: center;
}
.lb-list li:nth-child(1)::before { content: "🥇"; }
.lb-list li:nth-child(2)::before { content: "🥈"; }
.lb-list li:nth-child(3)::before { content: "🥉"; }
.lb-list .lb-name { font-weight: bold; color: #2c3e50; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-list .lb-score { color: #E65100; font-weight: bold; }
.lb-list .lb-date {
  background: #FFE0B2;
  color: #5D4037;
  border-radius: 8px;
  padding: 1px 6px;
  font-size: 0.78em;
  white-space: nowrap;
}
.lb-list .empty {
  text-align: center;
  color: #888;
  font-style: italic;
  display: block;
  padding: 6px;
  font-size: 0.85em;
}
#finalRank {
  margin-top: 6px;
  color: #C43E3E;
  font-weight: bold;
}

.diff-hint {
  margin-top: 10px;
  font-size: 0.95em;
  color: #666;
  min-height: 2.4em;
}

.big-btn {
  background: linear-gradient(135deg, #FF6B6B, #EE5A52);
  color: white;
  border: none;
  font-size: 1.4em;
  font-weight: bold;
  padding: 16px 40px;
  border-radius: 50px;
  cursor: pointer;
  margin-top: 24px;
  box-shadow: 0 6px 0 #C43E3E, 0 8px 16px rgba(0,0,0,0.2);
  transition: all 0.1s;
}

.big-btn:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 #C43E3E, 0 4px 8px rgba(0,0,0,0.2);
}

.text-btn {
  background: none;
  border: none;
  color: #555;
  font-size: 1em;
  margin-top: 10px;
  cursor: pointer;
  text-decoration: underline;
}

.tip {
  margin-top: 20px;
  color: #555;
  font-size: 0.95em;
}

.rules {
  background: rgba(255,255,255,0.85);
  border-radius: 14px;
  padding: 14px 18px;
  margin-top: 14px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 3px 8px rgba(0,0,0,0.08);
  font-size: 0.95em;
  line-height: 1.7;
}
.rules-title {
  font-weight: bold;
  color: #E65100;
  margin-bottom: 4px;
}
.rules ul {
  list-style: none;
  padding-left: 0;
}
.rules code {
  background: #FFE0B2;
  color: #C43E3E;
  padding: 1px 6px;
  border-radius: 6px;
  font-family: "Courier New", monospace;
  font-weight: bold;
}

/* ---------- HUD ---------- */
#hud {
  display: flex;
  gap: 16px;
  background: rgba(255,255,255,0.9);
  padding: 12px 20px;
  border-radius: 30px;
  margin-bottom: 16px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  font-size: 1.1em;
  font-weight: bold;
  flex-wrap: wrap;
  justify-content: center;
}

.hud-item { white-space: nowrap; }

/* ---------- Track (2.5D vertical) ---------- */
#track {
  width: 100%;
  height: 380px;
  position: relative;
  overflow: hidden;
  border-radius: 16px;
  border: 3px solid rgba(255,255,255,0.6);
  background: linear-gradient(180deg,
    #2C3E50 0%,
    #5D6D7E 35%,
    #FFE4B5 49%,
    #FFE4B5 51%,
    #6B8E23 51%,
    #4a6b18 100%);
}

/* far horizon glow */
#sky {
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 50%;
  background: radial-gradient(ellipse at 50% 100%,
    rgba(255, 220, 150, 0.6) 0%,
    rgba(255, 220, 150, 0) 60%);
  pointer-events: none;
  overflow: hidden;
}

/* parallax clouds — 3 層不同速度，純 emoji */
.cloud {
  position: absolute;
  pointer-events: none;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.12));
  will-change: transform;
}
.cloud.c1 { top: 10%; font-size: 36px; animation: cloud-drift 26s linear infinite; }
.cloud.c2 { top: 28%; font-size: 26px; opacity: 0.85; animation: cloud-drift 38s linear infinite; animation-delay: -14s; }
.cloud.c3 { top: 4%;  font-size: 22px; opacity: 0.7;  animation: cloud-drift 52s linear infinite; animation-delay: -30s; }

@keyframes cloud-drift {
  from { transform: translateX(-80px); }
  to   { transform: translateX(105vw); }
}

/* ground = lower half */
#ground {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  height: 50%;
  overflow: hidden;
}

/* the road - trapezoid going from horizon to viewer */
#road {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  height: 100%;
  background:
    linear-gradient(180deg, #6D4C41 0%, #4E342E 100%);
  clip-path: polygon(43% 0%, 57% 0%, 100% 100%, 0% 100%);
}

/* center dashed line (perspective stripes) */
#road::before {
  content: '';
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(
    180deg,
    rgba(255, 235, 59, 0.85) 0 6%,
    transparent 6% 12%
  );
  clip-path: polygon(49% 0%, 51% 0%, 53% 100%, 47% 100%);
  animation: road-dash 0.45s linear infinite;
}

/* side rails */
#road::after {
  content: '';
  position: absolute;
  inset: 0;
  background:
    linear-gradient(90deg,
      transparent 0%, transparent 42%,
      rgba(255,255,255,0.25) 42.5%, rgba(255,255,255,0.25) 43%,
      transparent 43.5%, transparent 56.5%,
      rgba(255,255,255,0.25) 57%, rgba(255,255,255,0.25) 57.5%,
      transparent 58%, transparent 100%);
  clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 100%);
  pointer-events: none;
}

@keyframes road-dash {
  from { background-position: 0 0; }
  to   { background-position: 0 60px; }
}

/* ---------- Player (排球) ---------- */
#player {
  position: absolute;
  bottom: 6%;
  left: 50%;
  transform: translateX(-50%);
  width: 70px;
  height: 70px;
  z-index: 5;
  filter: drop-shadow(0 8px 8px rgba(0,0,0,0.5));
  animation: bob 0.35s ease-in-out infinite alternate;
}

.ball-only {
  font-size: 60px;
  line-height: 1;
  display: block;
  text-align: center;
  animation: ball-spin 1s linear infinite;
}

@keyframes ball-spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}

@keyframes bob {
  from { transform: translateX(-50%) translateY(0); }
  to   { transform: translateX(-50%) translateY(-8px); }
}

#player.hit {
  animation: hit-shake 0.6s;
}

@keyframes hit-shake {
  0%, 100% { transform: translateX(-50%) translateY(0); filter: drop-shadow(0 8px 8px rgba(0,0,0,0.5)); }
  15% { transform: translateX(-58%) translateY(4px) rotate(-15deg); filter: drop-shadow(0 8px 8px rgba(255,0,0,0.8)) brightness(1.5); }
  30% { transform: translateX(-42%) translateY(2px) rotate(12deg); filter: drop-shadow(0 8px 8px rgba(255,0,0,0.8)) brightness(1.5); }
  60% { transform: translateX(-50%) translateY(0) rotate(0); }
}

#player.success {
  animation: jump 0.5s ease-out;
}

@keyframes jump {
  0%, 100% { transform: translateX(-50%) translateY(0); }
  50% { transform: translateX(-50%) translateY(-60px) rotate(15deg); }
}

/* ---------- Obstacle (the wall, 2.5D) ---------- */
#obstacle {
  position: absolute;
  left: 50%;
  z-index: 4;
  pointer-events: none;
  transform: translate(-50%, -50%) scale(0.08);
  top: 50%;
  width: 280px;
  height: 170px;
  background:
    linear-gradient(180deg, #BCAAA4 0%, #8D6E63 35%, #5D4037 100%);
  border: 4px solid #3E2723;
  border-radius: 6px;
  box-shadow:
    inset 0 -10px 0 rgba(0,0,0,0.3),
    inset 0 6px 0 rgba(255,255,255,0.15),
    0 10px 0 #2c1810,
    0 14px 30px rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #FFF59D;
  text-shadow: 2px 2px 0 #2c1810, 0 0 6px rgba(0,0,0,0.6);
}

/* brick texture */
#obstacle::before {
  content: '';
  position: absolute;
  inset: 0;
  background:
    repeating-linear-gradient(0deg,
      transparent 0 28px,
      rgba(0,0,0,0.25) 28px 30px),
    repeating-linear-gradient(90deg,
      transparent 0 60px,
      rgba(0,0,0,0.25) 60px 62px);
  border-radius: 4px;
  pointer-events: none;
}

#obstacle.moving {
  animation: approach var(--obstacle-duration, 5.5s) linear forwards;
}

@keyframes approach {
  0% {
    top: 49%;
    transform: translate(-50%, -50%) scale(0.06);
    opacity: 0;
    filter: blur(2px);
  }
  10% {
    opacity: 1;
    filter: blur(1px);
  }
  60% {
    filter: blur(0);
  }
  100% {
    top: 100%;
    transform: translate(-50%, -90%) scale(1.6);
    opacity: 1;
    filter: blur(0);
  }
}

#obstacle.hidden { display: none; }

#question {
  font-family: "Courier New", monospace;
  font-weight: bold;
  text-align: center;
  line-height: 1.2;
  padding: 8px;
  position: relative;
  z-index: 1;
}

#question.horizontal { font-size: 2.2em; }
#question.vertical {
  font-size: 1.9em;
  white-space: pre;
  text-align: right;
}
#question.vertical .vline {
  border-bottom: 4px solid #FFF59D;
  padding-bottom: 2px;
  display: inline-block;
}

/* ---------- Hit flash ---------- */
#hit-flash {
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at 50% 80%, rgba(255,80,80,0.7) 0%, rgba(255,0,0,0) 70%);
  z-index: 10;
  pointer-events: none;
  animation: hit-flash-anim 0.5s ease-out;
}

#hit-flash.hidden { display: none; }

@keyframes hit-flash-anim {
  from { opacity: 1; }
  to   { opacity: 0; }
}

/* ---------- Answers ---------- */
#answers {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  width: 100%;
  max-width: 500px;
  margin-top: 20px;
}

.answer-btn {
  background: white;
  border: 3px solid #FFA726;
  border-radius: 14px;
  padding: 18px;
  font-size: 1.6em;
  font-weight: bold;
  cursor: pointer;
  box-shadow: 0 4px 0 #E65100;
  transition: all 0.1s;
  font-family: "Courier New", monospace;
}

.answer-btn:hover { background: #FFF8E1; }

.answer-btn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 #E65100;
}

.answer-btn.correct {
  background: #66BB6A;
  border-color: #2E7D32;
  color: white;
  box-shadow: 0 4px 0 #1B5E20;
}

.answer-btn.wrong {
  background: #EF5350;
  border-color: #B71C1C;
  color: white;
  box-shadow: 0 4px 0 #7F0000;
  animation: shake 0.3s;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

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

/* ---------- Game Over ---------- */
.result {
  background: rgba(255,255,255,0.9);
  border-radius: 16px;
  padding: 24px 36px;
  font-size: 1.3em;
  line-height: 2;
  margin: 20px 0;
  text-align: center;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.result span {
  font-weight: bold;
  color: #E65100;
  font-size: 1.4em;
}

#mistakes-section {
  background: rgba(255,255,255,0.9);
  border-radius: 16px;
  padding: 18px 24px;
  margin: 10px 0;
  width: 100%;
  max-width: 500px;
  max-height: 200px;
  overflow-y: auto;
}

#mistakes-section h3 {
  margin-bottom: 8px;
  color: #C62828;
}

#mistakes-list {
  list-style: none;
  font-family: "Courier New", monospace;
  font-size: 1.1em;
}

#mistakes-list li {
  padding: 6px 0;
  border-bottom: 1px dashed #ddd;
}

#mistakes-list li:last-child { border-bottom: none; }

#mistakes-list .right-answer {
  color: #2E7D32;
  font-weight: bold;
}

#mistakes-list .empty {
  color: #888;
  font-style: italic;
  text-align: center;
}

@media (max-width: 600px) {
  h1 { font-size: 1.8em; }
  #track { height: 320px; }
  #player { width: 56px; height: 56px; }
  .ball-only { font-size: 48px; }
  #obstacle { width: 220px; height: 140px; }
  #question.horizontal { font-size: 1.7em; }
  #question.vertical { font-size: 1.5em; }
  .answer-btn { font-size: 1.3em; padding: 14px; }
}

/* ---------- Pause button (HUD) ---------- */
#pauseBtn {
  background: rgba(255,255,255,0.95);
  border: 2px solid #FFA726;
  border-radius: 50%;
  width: 38px;
  height: 38px;
  font-size: 1em;
  font-weight: bold;
  cursor: pointer;
  box-shadow: 0 3px 0 #E65100;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  line-height: 1;
}
#pauseBtn:active { transform: translateY(2px); box-shadow: 0 1px 0 #E65100; }

/* ---------- Pause overlay (蓋在 #track 上) ---------- */
#pause-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  color: white;
  font-size: 2em;
  font-weight: bold;
  text-shadow: 2px 2px 0 rgba(0,0,0,0.5);
  border-radius: 12px;
}
#pause-overlay.hidden { display: none; }
#pause-overlay button {
  font-size: 0.55em;
  padding: 10px 28px;
  background: #FFA726;
  border: none;
  color: white;
  border-radius: 12px;
  font-weight: bold;
  cursor: pointer;
  box-shadow: 0 4px 0 #E65100;
}
#pause-overlay button:active { transform: translateY(2px); box-shadow: 0 1px 0 #E65100; }

/* paused: freeze CSS animations */
#game.paused .cloud,
#game.paused #road,
#game.paused #road::before,
#game.paused #obstacle.moving,
#game.paused #player,
#game.paused .ball-only {
  animation-play-state: paused !important;
}

/* ---------- Obstacle themes by difficulty ---------- */
/* 難度 1-2：磚牆 (預設樣式，不覆寫) */

/* 難度 3-4：冰塊 */
#obstacle[data-difficulty="3"],
#obstacle[data-difficulty="4"] {
  background: linear-gradient(180deg,
    #E1F5FE 0%, #81D4FA 35%, #4FC3F7 70%, #29B6F6 100%);
  border-color: #01579B;
  box-shadow:
    inset 0 -10px 0 rgba(0,30,80,0.3),
    inset 0 6px 0 rgba(255,255,255,0.6),
    0 10px 0 #01579B,
    0 0 24px rgba(100,200,255,0.5),
    0 14px 30px rgba(0,80,160,0.4);
  color: #0D47A1;
  text-shadow: 1px 1px 0 white, 0 0 4px rgba(255,255,255,0.9);
}
#obstacle[data-difficulty="3"]::before,
#obstacle[data-difficulty="4"]::before {
  background:
    linear-gradient(135deg,
      rgba(255,255,255,0.55) 0%,
      rgba(255,255,255,0.05) 35%,
      transparent 60%,
      rgba(255,255,255,0.25) 100%),
    repeating-linear-gradient(45deg,
      transparent 0 22px,
      rgba(255,255,255,0.2) 22px 24px);
}

/* 難度 5-6：岩漿 */
#obstacle[data-difficulty="5"],
#obstacle[data-difficulty="6"] {
  background: linear-gradient(180deg,
    #FFEB3B 0%, #FF9800 25%, #E64A19 55%, #B71C1C 85%, #4E342E 100%);
  border-color: #2c1810;
  box-shadow:
    inset 0 -10px 0 rgba(0,0,0,0.4),
    inset 0 6px 0 rgba(255,255,200,0.6),
    0 10px 0 #2c1810,
    0 0 32px rgba(255,100,0,0.75),
    0 14px 30px rgba(255,0,0,0.5);
  color: #FFF59D;
  text-shadow: 2px 2px 0 #4E342E, 0 0 8px rgba(255,200,0,0.8);
}
#obstacle[data-difficulty="5"]::before,
#obstacle[data-difficulty="6"]::before {
  background:
    radial-gradient(ellipse at 30% 80%, rgba(255,255,150,0.55) 0%, transparent 45%),
    radial-gradient(ellipse at 70% 30%, rgba(255,180,0,0.55) 0%, transparent 45%),
    repeating-linear-gradient(0deg,
      transparent 0 38px,
      rgba(0,0,0,0.18) 38px 40px);
}
#obstacle[data-difficulty="5"]::after,
#obstacle[data-difficulty="6"]::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 4px;
  background: radial-gradient(ellipse at 50% 50%,
    rgba(255,230,80,0.35) 0%,
    transparent 65%);
  animation: lava-pulse 0.55s ease-in-out infinite alternate;
  pointer-events: none;
}

@keyframes lava-pulse {
  from { opacity: 0.5; filter: brightness(1); }
  to   { opacity: 1;   filter: brightness(1.25); }
}
