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

:root {
  --mode-color: #7c4dff;
  --bg-start: #e8f4fd;
  --bg-mid: #f5e6ff;
  --bg-end: #fff5e6;
}

html, body {
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

body {
  font-family: -apple-system, 'SF Pro Rounded', 'Rounded Mplus 1c', system-ui, sans-serif;
  background: linear-gradient(135deg, var(--bg-start), var(--bg-mid), var(--bg-end));
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

body[data-mode="identify"] { --mode-color: #7c4dff; }
body[data-mode="first-letter"] { --mode-color: #00c853; }
body[data-mode="find"] { --mode-color: #ff6d00; }
body[data-mode="trace"] { --mode-color: #e040fb; }

/* ── Mode Bar ── */
.mode-bar {
  display: flex;
  justify-content: center;
  gap: clamp(8px, 2vw, 16px);
  padding: clamp(6px, 1.5vw, 12px);
  background: rgba(255,255,255,0.7);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.mode-btn, .home-btn {
  font-size: clamp(24px, 5vw, 36px);
  background: none;
  border: 3px solid transparent;
  border-radius: 12px;
  padding: 4px 10px;
  cursor: pointer;
  transition: all 0.2s;
  text-decoration: none;
}

.mode-btn.active {
  border-color: var(--mode-color);
  background: rgba(124, 77, 255, 0.1);
}

.mode-btn:not(.sound-btn):hover { transform: scale(1.1); }

/* ── Game Content Wrapper ── */
.game-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 0;
  gap: clamp(16px, 4vw, 32px);
  padding: clamp(8px, 2vw, 16px);
}

/* ── Problem Area ── */
.problem-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(12px, 3vw, 24px);
}

/* ── Identify Mode: Listen prompt ── */
.listen-prompt {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(12px, 3vw, 20px);
}

.listen-emoji {
  font-size: clamp(40px, 10vw, 60px);
  animation: float 2s ease-in-out infinite;
}

.listen-hint {
  font-size: clamp(32px, 7vw, 52px);
  font-weight: 700;
  color: var(--mode-color);
  opacity: 0.35;
  letter-spacing: 2px;
}

/* ── Large Play Button (shared) ── */
.play-btn-large {
  font-size: clamp(24px, 5vw, 36px);
  background: var(--mode-color);
  color: white;
  border: none;
  border-radius: 50%;
  width: clamp(44px, 9vw, 60px);
  height: clamp(44px, 9vw, 60px);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s;
  box-shadow: 0 6px 20px rgba(0,0,0,0.2);
}

.play-btn-large:active { transform: scale(0.9); }

.play-btn-large.pulse {
  animation: pulse 0.5s ease;
}

/* ── First Letter Mode ── */
.word-image {
  font-size: clamp(100px, 25vw, 180px);
  animation: popIn 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
  line-height: 1;
}

.prompt-row {
  display: flex;
  align-items: center;
  gap: clamp(14px, 3vw, 24px);
}

.prompt-text {
  font-size: clamp(56px, 12vw, 80px);
  font-weight: 700;
  color: var(--mode-color);
  opacity: 0.5;
}

/* ── Find Mode ── */
.find-prompt {
  display: flex;
  align-items: center;
  gap: clamp(12px, 3vw, 20px);
}

.find-target {
  display: flex;
  align-items: center;
  gap: clamp(4px, 1vw, 8px);
}

.find-target-letter {
  font-size: clamp(52px, 12vw, 80px);
  font-weight: 700;
  color: var(--mode-color);
  background: white;
  border: 4px solid var(--mode-color);
  border-radius: 16px;
  padding: 4px 18px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.find-count {
  font-size: clamp(32px, 7vw, 52px);
  font-weight: 700;
  color: #666;
}

/* Letter grid for find mode */
.letter-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: clamp(10px, 2.5vw, 16px);
  max-width: 600px;
  width: 100%;
  padding: 0 8px;
}

.grid-letter {
  font-size: clamp(40px, 11vw, 64px);
  font-weight: 700;
  background: white;
  border: 3px solid #e0e0e0;
  border-radius: 16px;
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s;
  animation: popIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

.grid-letter:active { transform: scale(0.95); }

.grid-letter.found {
  background: #c8e6c9;
  border-color: #66bb6a;
  color: #2e7d32;
  transform: scale(1.08);
  box-shadow: 0 2px 12px rgba(76, 175, 80, 0.3);
}

.grid-letter.wrong-tap {
  animation: shake 0.4s;
  border-color: #ef5350;
}

/* ── Answer Area ── */
.answer-area {
  display: flex;
  justify-content: center;
  gap: clamp(10px, 3vw, 20px);
  padding: 0 clamp(12px, 3vw, 24px);
  flex-wrap: wrap;
}

.answer-btn {
  font-size: clamp(52px, 14vw, 80px);
  font-weight: 700;
  min-width: clamp(100px, 22vw, 150px);
  padding: clamp(16px, 4vw, 28px) clamp(20px, 5vw, 36px);
  border: none;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.15s;
  box-shadow: 0 4px 14px rgba(0,0,0,0.12);
}

.answer-btn:active { transform: scale(0.95); }

.answer-btn:nth-child(1) { background: linear-gradient(135deg, #b2f5ea, #81e6d9); color: #234e52; }
.answer-btn:nth-child(2) { background: linear-gradient(135deg, #fed7aa, #fbd38d); color: #652b19; }
.answer-btn:nth-child(3) { background: linear-gradient(135deg, #d6bcfa, #b794f4); color: #322659; }
.answer-btn:nth-child(4) { background: linear-gradient(135deg, #fbb6ce, #f687b3); color: #521b41; }

.answer-btn.correct { animation: correctPop 0.3s; }
.answer-btn.wrong { animation: shake 0.4s; opacity: 0.4; pointer-events: none; }
.answer-btn.disabled { pointer-events: none; opacity: 0.5; }

/* ── Progress Bar ── */
.progress-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(4px, 1vw, 8px);
  padding: clamp(6px, 1.5vw, 12px);
  background: rgba(255,255,255,0.5);
}

.stars {
  display: flex;
  gap: clamp(2px, 0.5vw, 4px);
}

.star {
  font-size: clamp(20px, 4vw, 32px);
  opacity: 0.2;
  transition: all 0.3s;
}

.star.earned {
  opacity: 1;
  animation: starEarn 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.progress-counter {
  font-size: clamp(14px, 3vw, 20px);
  color: #666;
  font-weight: 600;
  margin-right: 8px;
}

/* ── Streak Indicator ── */
.streak-indicator {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: clamp(48px, 12vw, 72px);
  font-weight: 900;
  z-index: 50;
  animation: streakPop 1s ease-out forwards;
  pointer-events: none;
  text-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

@keyframes streakPop {
  0% { transform: translate(-50%, -50%) scale(0); opacity: 0; }
  20% { transform: translate(-50%, -50%) scale(1.3); opacity: 1; }
  70% { transform: translate(-50%, -50%) scale(1.1); opacity: 1; }
  100% { transform: translate(-50%, -50%) scale(0.8); opacity: 0; }
}

/* ── Celebration ── */
.celebration {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 100;
}

.confetti {
  position: absolute;
  top: -20px;
  width: 12px;
  height: 12px;
  border-radius: 2px;
  animation: confettiFall var(--duration, 2s) linear forwards;
}

.big-star {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 100px;
  animation: bigStarPop 2s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

/* ── Animations ── */
@keyframes popIn {
  0% { transform: scale(0); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

@keyframes correctPop {
  0% { transform: scale(1); }
  50% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

@keyframes starEarn {
  0% { transform: scale(0) rotate(-180deg); }
  80% { transform: scale(1.2) rotate(10deg); }
  100% { transform: scale(1) rotate(0); }
}

@keyframes confettiFall {
  0% { transform: translateY(0) rotate(0) translateX(0); opacity: 1; }
  100% { transform: translateY(100vh) rotate(720deg) translateX(var(--drift, 40px)); opacity: 0; }
}

@keyframes bigStarPop {
  0% { transform: translate(-50%, -50%) scale(0); opacity: 0; }
  50% { transform: translate(-50%, -50%) scale(1.3); opacity: 1; }
  70% { transform: translate(-50%, -50%) scale(0.95); }
  100% { transform: translate(-50%, -50%) scale(1); opacity: 0; }
}

@keyframes bounce {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.2); }
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

/* ── Landscape ── */
@media (orientation: landscape) and (max-height: 500px) {
  .listen-emoji { font-size: clamp(36px, 8vw, 56px); }
  .listen-hint { font-size: clamp(18px, 4vw, 28px); }
  .word-image { font-size: clamp(50px, 12vw, 90px); }
  .answer-btn { font-size: clamp(28px, 5vw, 40px); min-width: clamp(60px, 12vw, 90px); padding: 8px 14px; }
  .grid-letter { font-size: clamp(22px, 4vw, 32px); }
  .play-btn-large { width: 52px; height: 52px; font-size: 28px; }
  .find-target-letter { font-size: clamp(32px, 6vw, 48px); }
}

/* ── Trace Mode ── */
.trace-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(8px, 2vw, 16px);
  width: 100%;
}

.trace-letter-name {
  font-size: clamp(24px, 6vw, 36px);
  font-weight: 700;
  color: var(--mode-color);
}

.trace-canvas-wrap {
  position: relative;
  width: clamp(260px, 70vw, 380px);
  height: clamp(260px, 70vw, 380px);
  background: white;
  border-radius: 24px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
  border: 4px solid var(--mode-color);
  overflow: hidden;
}

.trace-canvas-wrap canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

#traceFill {
  pointer-events: none;
}

#traceCanvas {
  touch-action: none;
  cursor: crosshair;
}

.trace-buttons {
  display: flex;
  gap: clamp(12px, 3vw, 20px);
}

.trace-btn {
  font-size: clamp(28px, 7vw, 44px);
  background: white;
  border: 3px solid #ddd;
  border-radius: 50%;
  width: clamp(56px, 14vw, 72px);
  height: clamp(56px, 14vw, 72px);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s, border-color 0.15s;
}

.trace-btn:active { transform: scale(0.9); }

.trace-btn.check-btn {
  background: var(--mode-color);
  border-color: var(--mode-color);
}

.trace-btn.clear-btn:hover { border-color: #ff6b6b; }

.trace-result {
  font-size: clamp(48px, 12vw, 72px);
  animation: correctPop 0.5s ease;
}

.stroke-guide-dot {
  position: absolute;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #4CAF50;
  box-shadow: 0 0 8px rgba(76, 175, 80, 0.6);
  pointer-events: none;
  z-index: 10;
  display: none;
  transform: translate(-50%, -50%);
}

.stroke-guide-dot.pulse {
  animation: guidePulse 0.5s ease-in-out;
}

@keyframes guidePulse {
  0%, 100% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-50%, -50%) scale(1.5); }
}
