/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-tap-highlight-color: transparent; }
body {
  font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: linear-gradient(135deg, #0a0e27 0%, #1a1a4e 50%, #0d1137 100%);
  color: #fff;
  min-height: 100vh;
  overflow-x: hidden;
  user-select: none;
  -webkit-user-select: none;
}

/* ===== Screen Management ===== */
.screen { display: none; min-height: 100vh; padding: 16px; }
.screen.active { display: flex; flex-direction: column; }

/* ===== Menu Screen ===== */
.menu-header { text-align: center; padding: 60px 0 40px; }
.menu-header h1 { font-size: 32px; margin-bottom: 8px; text-shadow: 0 0 20px rgba(100, 149, 237, 0.5); }
.menu-header .subtitle { font-size: 18px; color: #8899cc; }

.menu-buttons { display: flex; flex-direction: column; gap: 16px; max-width: 300px; margin: 0 auto; width: 100%; }
.menu-btn {
  display: block; width: 100%; padding: 18px 24px;
  font-size: 22px; font-weight: 600;
  border: 2px solid rgba(100, 149, 237, 0.4);
  border-radius: 16px;
  background: rgba(100, 149, 237, 0.1);
  color: #fff; cursor: pointer;
  transition: all 0.2s;
  min-height: 60px;
  touch-action: manipulation;
}
.menu-btn:active { transform: scale(0.97); }
.menu-btn.primary {
  background: linear-gradient(135deg, #4a6cf7, #6366f1);
  border-color: #6366f1;
  box-shadow: 0 4px 15px rgba(99, 102, 241, 0.3);
}
.menu-btn.primary:active { box-shadow: 0 2px 8px rgba(99, 102, 241, 0.3); }

/* ===== Screen Header ===== */
.screen-header {
  display: flex; align-items: center; gap: 12px;
  padding-bottom: 16px; border-bottom: 1px solid rgba(255,255,255,0.1);
  margin-bottom: 16px;
}
.screen-header h2 { font-size: 22px; }
.back-btn {
  background: rgba(255,255,255,0.1); border: none; color: #fff;
  padding: 10px 16px; border-radius: 10px; font-size: 16px;
  cursor: pointer; min-height: 48px; min-width: 48px;
  touch-action: manipulation;
}
.back-btn:active { background: rgba(255,255,255,0.2); }

/* ===== Level Grid ===== */
.level-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 16px; padding: 8px 0;
}
.level-card {
  background: rgba(100, 149, 237, 0.1);
  border: 2px solid rgba(100, 149, 237, 0.3);
  border-radius: 16px; padding: 16px; text-align: center;
  cursor: pointer; transition: all 0.2s;
  touch-action: manipulation;
}
.level-card:active { transform: scale(0.96); }
.level-card.completed { border-color: #4ade80; background: rgba(74, 222, 128, 0.1); }
.level-card .level-num { font-size: 28px; font-weight: 700; margin-bottom: 4px; }
.level-card .level-name { font-size: 14px; color: #8899cc; margin-bottom: 8px; }
.level-card .level-progress { font-size: 13px; color: #6b7db3; }
.level-card .level-progress-bar {
  height: 6px; background: rgba(255,255,255,0.1); border-radius: 3px;
  margin-top: 6px; overflow: hidden;
}
.level-card .level-progress-fill {
  height: 100%; background: linear-gradient(90deg, #4a6cf7, #4ade80);
  border-radius: 3px; transition: width 0.3s;
}

/* ===== Game Screen ===== */
#game-screen.active { padding: 8px; gap: 8px; }
.game-top { flex: 0 0 auto; }
.game-toolbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 4px 8px 8px;
}
.progress-text { font-size: 18px; font-weight: 600; color: #8899cc; }

/* ===== Puzzle Grid ===== */
.puzzle-container {
  width: 100%; max-width: 360px; margin: 0 auto;
  aspect-ratio: 1; position: relative;
}
.puzzle-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr);
  gap: 3px; width: 100%; height: 100%;
}
.puzzle-cell {
  position: relative; perspective: 600px; cursor: default;
}
.puzzle-cell-inner {
  position: relative; width: 100%; height: 100%;
  transition: transform 0.6s ease;
  transform-style: preserve-3d;
}
.puzzle-cell.flipped .puzzle-cell-inner { transform: rotateY(180deg); }
.puzzle-cell-front, .puzzle-cell-back {
  position: absolute; width: 100%; height: 100%;
  backface-visibility: hidden; border-radius: 3px;
  display: flex; align-items: center; justify-content: center;
}
.puzzle-cell-front {
  background: rgba(100, 149, 237, 0.15);
  border: 2px solid rgba(100, 149, 237, 0.3);
  font-size: 24px; font-weight: 600; color: rgba(255,255,255,0.4);
}
.puzzle-cell-back {
  transform: rotateY(180deg);
  background-size: cover; background-position: center;
}
.puzzle-cell.no-anim .puzzle-cell-inner { transition: none; }

/* Fallback for no 3D support */
@supports not (transform-style: preserve-3d) {
  .puzzle-cell-inner { transition: opacity 0.4s; }
  .puzzle-cell.flipped .puzzle-cell-front { opacity: 0; }
  .puzzle-cell.flipped .puzzle-cell-back { opacity: 1; transform: none; }
  .puzzle-cell-back { opacity: 0; }
}

/* Puzzle complete state */
.puzzle-grid.complete .puzzle-cell-front { display: none; }
.puzzle-grid.complete .puzzle-cell { border-radius: 0; }
.puzzle-grid.complete .puzzle-cell-back { border-radius: 0; }
.puzzle-grid.complete { gap: 0; }

/* ===== Question Area ===== */
.game-bottom { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.question-area {
  flex: 1; display: flex; flex-direction: column;
  padding: 12px 8px; gap: 12px;
}
.question-text {
  font-size: 22px; font-weight: 600; text-align: center;
  line-height: 1.5; padding: 8px; min-height: 60px;
  display: flex; align-items: center; justify-content: center;
}
.question-hint {
  font-size: 15px; color: #8899cc; text-align: center;
  min-height: 20px;
}
.options-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 12px;
  padding: 8px 0;
}
.option-btn {
  padding: 16px 12px; font-size: 22px; font-weight: 500;
  background: rgba(100, 149, 237, 0.12);
  border: 2px solid rgba(100, 149, 237, 0.35);
  border-radius: 14px; color: #fff; cursor: pointer;
  transition: all 0.15s; min-height: 56px;
  touch-action: manipulation;
  display: flex; align-items: center; justify-content: center;
  text-align: center; word-break: break-all;
}
.option-btn:active { transform: scale(0.97); }
.option-btn.correct {
  background: rgba(74, 222, 128, 0.25); border-color: #4ade80; color: #4ade80;
}
.option-btn.wrong {
  background: rgba(239, 68, 68, 0.25); border-color: #ef4444; color: #ef4444;
}
.option-btn.disabled { pointer-events: none; opacity: 0.7; }

.feedback-overlay {
  position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
  font-size: 48px; font-weight: 700; z-index: 100;
  animation: feedbackPop 0.5s ease-out forwards;
  pointer-events: none;
}
.feedback-overlay.hidden { display: none; }
@keyframes feedbackPop {
  0% { transform: translate(-50%, -50%) scale(0.5); opacity: 0; }
  50% { transform: translate(-50%, -50%) scale(1.2); opacity: 1; }
  100% { transform: translate(-50%, -50%) scale(1); opacity: 0; }
}

/* ===== Complete Screen ===== */
#complete-screen.active {
  justify-content: center; align-items: center;
}
.complete-content { text-align: center; max-width: 360px; width: 100%; }
.complete-content h2 { font-size: 32px; margin: 16px 0; }
.complete-image {
  width: 100%; aspect-ratio: 1; border-radius: 16px; overflow: hidden;
  margin-bottom: 8px; background-size: cover; background-position: center;
  box-shadow: 0 0 30px rgba(100, 149, 237, 0.3);
}
.stats-panel {
  background: rgba(100, 149, 237, 0.1); border-radius: 12px;
  padding: 16px; margin: 16px 0; text-align: left;
}
.stats-panel .stat-row {
  display: flex; justify-content: space-between; padding: 6px 0;
  font-size: 16px; border-bottom: 1px solid rgba(255,255,255,0.05);
}
.stats-panel .stat-row:last-child { border-bottom: none; }
.stats-panel .stat-label { color: #8899cc; }
.stats-panel .stat-value { font-weight: 600; }
.complete-buttons { display: flex; flex-direction: column; gap: 12px; margin-top: 16px; }

/* ===== Wrong Book ===== */
.wrong-list { display: flex; flex-direction: column; gap: 12px; padding-bottom: 20px; }
.wrong-item {
  background: rgba(239, 68, 68, 0.08); border: 1px solid rgba(239, 68, 68, 0.2);
  border-radius: 12px; padding: 14px;
}
.wrong-item .wrong-q { font-size: 18px; font-weight: 600; margin-bottom: 8px; }
.wrong-item .wrong-detail { font-size: 14px; color: #8899cc; line-height: 1.6; }
.wrong-item .wrong-detail .wrong-answer { color: #ef4444; }
.wrong-item .wrong-detail .correct-answer { color: #4ade80; }
.wrong-empty { text-align: center; color: #6b7db3; font-size: 18px; padding: 60px 0; }

/* ===== Confetti ===== */
.confetti-container {
  position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  pointer-events: none; z-index: 200; overflow: hidden;
}
.confetti-piece {
  position: absolute; width: 10px; height: 10px;
  top: -10px; opacity: 0;
  animation: confettiFall 3s ease-in forwards;
}
@keyframes confettiFall {
  0% { opacity: 1; transform: translateY(0) rotate(0deg); }
  100% { opacity: 0; transform: translateY(100vh) rotate(720deg); }
}

.icp-footer { margin-top: auto; padding: 28px 0 8px; text-align: center; font-size: 12px; line-height: 1.5; }
.icp-footer a { color: #8899cc; text-decoration: none; }
