/* ========== Reset & Base ========== */
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: 'Segoe UI', system-ui, -apple-system, 'Helvetica Neue', Arial, sans-serif;
  background: linear-gradient(160deg, #eef2ff 0%, #faf5ff 100%);
  color: #1f2937;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
  user-select: none;
  -webkit-user-select: none;
}
input { user-select: text; -webkit-user-select: text; }

#app {
  max-width: 600px;
  margin: 0 auto;
  min-height: 100vh;
  position: relative;
  padding-bottom: env(safe-area-inset-bottom);
}

/* ========== Screens ========== */
.screen {
  display: none;
  padding: 20px 18px 32px;
  animation: fadeIn 0.3s ease;
}
.screen.active { display: block; }
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ========== Home ========== */
.home-header {
  text-align: center;
  padding: 24px 0 16px;
}
.logo {
  font-size: 64px;
  filter: drop-shadow(0 6px 12px rgba(99,102,241,0.3));
  animation: bounce 2.5s ease-in-out infinite;
}
@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}
.home-header h1 {
  font-size: 32px;
  font-weight: 800;
  background: linear-gradient(135deg, #6366f1, #a855f7);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  margin-top: 4px;
}
.subtitle {
  color: #6b7280;
  font-size: 16px;
  margin-top: 4px;
}

.stat-card {
  background: white;
  border-radius: 20px;
  padding: 18px;
  margin: 16px 0 24px;
  box-shadow: 0 4px 20px rgba(99,102,241,0.1);
}
.stat-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  text-align: center;
}
.stat-value {
  font-size: 20px;
  font-weight: 700;
  color: #4338ca;
}
.stat-label {
  font-size: 12px;
  color: #9ca3af;
  margin-top: 2px;
}
.progress-wrap { margin-top: 14px; }
.progress-bar {
  height: 10px;
  background: #f3f4f6;
  border-radius: 999px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #fbbf24, #f59e0b);
  border-radius: 999px;
  transition: width 0.5s ease;
  width: 0%;
}
.progress-text {
  font-size: 12px;
  color: #6b7280;
  margin-top: 6px;
  text-align: center;
}

.section-title {
  font-size: 18px;
  margin: 8px 4px 12px;
  color: #374151;
}

.level-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}
@media (min-width: 480px) {
  .level-grid { grid-template-columns: repeat(3, 1fr); }
}
.level-btn {
  background: white;
  border: 3px solid transparent;
  border-radius: 18px;
  padding: 18px 12px;
  cursor: pointer;
  text-align: center;
  transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
  box-shadow: 0 4px 12px rgba(0,0,0,0.06);
  font-family: inherit;
}
.level-btn:active { transform: scale(0.97); }
.level-btn.level-1 { border-color: #86efac; }
.level-btn.level-2 { border-color: #93c5fd; }
.level-btn.level-3 { border-color: #fcd34d; }
.level-btn:hover { box-shadow: 0 8px 24px rgba(0,0,0,0.1); }

.level-emoji { font-size: 36px; }
.level-title { font-size: 18px; font-weight: 700; margin-top: 4px; }
.level-desc { font-size: 13px; color: #6b7280; margin-top: 6px; line-height: 1.4; }
.level-reward { font-size: 12px; color: #f59e0b; margin-top: 8px; font-weight: 600; }

.home-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 24px;
  justify-content: center;
}

/* ========== Buttons ========== */
.btn {
  border: none;
  border-radius: 14px;
  padding: 12px 20px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: transform 0.1s ease, box-shadow 0.15s ease, background 0.15s ease;
}
.btn:active { transform: scale(0.96); }
.btn-primary {
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  color: white;
  box-shadow: 0 6px 16px rgba(99,102,241,0.35);
}
.btn-primary:hover { box-shadow: 0 8px 20px rgba(99,102,241,0.45); }
.btn-secondary {
  background: white;
  color: #4338ca;
  border: 2px solid #e0e7ff;
}
.btn-ghost {
  background: transparent;
  color: #6b7280;
}
.btn-danger {
  background: #fee2e2;
  color: #b91c1c;
  width: 100%;
}
.btn-big {
  width: 100%;
  padding: 16px;
  font-size: 18px;
  margin-top: 16px;
}

/* ========== Quiz ========== */
.quiz-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}
.icon-btn {
  background: white;
  border: none;
  border-radius: 12px;
  width: 44px;
  height: 44px;
  font-size: 22px;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  color: #4338ca;
}
.icon-btn:active { transform: scale(0.94); }
.quiz-header h2 { font-size: 22px; flex: 1; }

.hud {
  display: flex;
  gap: 8px;
  flex: 1;
  justify-content: flex-end;
  flex-wrap: wrap;
}
.hud-item {
  background: white;
  border-radius: 12px;
  padding: 8px 14px;
  font-weight: 700;
  font-size: 14px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  color: #4338ca;
}

.question-card {
  background: white;
  border-radius: 24px;
  padding: 28px 20px;
  margin-top: 12px;
  box-shadow: 0 8px 30px rgba(99,102,241,0.12);
}
.question-prompt {
  text-align: center;
  font-size: 14px;
  color: #6b7280;
  text-transform: uppercase;
  letter-spacing: 1px;
  font-weight: 600;
}
.question-text {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px 12px;
  font-size: 24px;
  font-weight: 700;
  margin: 24px 0 8px;
  color: #1f2937;
}
.q-value {
  color: #4338ca;
  font-size: 32px;
}
.q-unit {
  color: #6b7280;
}
.q-equals {
  color: #9ca3af;
  font-size: 28px;
}
.answer-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
}
#q-input {
  width: 140px;
  font-size: 28px;
  font-weight: 700;
  text-align: center;
  border: 3px solid #e0e7ff;
  border-radius: 14px;
  padding: 10px 8px;
  outline: none;
  background: #f8fafc;
  color: #4338ca;
  font-family: inherit;
  transition: border-color 0.15s ease, background 0.15s ease;
}
#q-input:focus {
  border-color: #6366f1;
  background: white;
}
#q-input.correct {
  border-color: #10b981;
  background: #d1fae5;
  color: #065f46;
}
#q-input.wrong {
  border-color: #ef4444;
  background: #fee2e2;
  color: #991b1b;
  animation: shake 0.4s ease;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-8px); }
  75% { transform: translateX(8px); }
}
.q-unit-to {
  color: #4338ca;
}

.feedback {
  text-align: center;
  min-height: 28px;
  font-weight: 600;
  margin: 12px 0 4px;
  font-size: 16px;
}
.feedback.correct { color: #10b981; }
.feedback.wrong { color: #ef4444; }

.quiz-footer {
  margin-top: 20px;
  text-align: center;
}
.counts {
  display: inline-flex;
  gap: 14px;
  background: white;
  padding: 10px 18px;
  border-radius: 999px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
  font-weight: 600;
}
.count-correct { color: #059669; }
.count-wrong { color: #dc2626; }

/* ========== Achievements ========== */
.achievements-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
  margin-top: 12px;
}
.achievement {
  background: white;
  border-radius: 16px;
  padding: 16px 12px;
  text-align: center;
  box-shadow: 0 2px 10px rgba(0,0,0,0.06);
  border: 2px solid transparent;
  transition: all 0.2s ease;
}
.achievement.locked {
  opacity: 0.45;
  filter: grayscale(0.7);
}
.achievement.unlocked {
  border-color: #fbbf24;
  background: linear-gradient(160deg, #fef3c7 0%, white 70%);
}
.achievement-icon {
  font-size: 40px;
}
.achievement-title {
  font-weight: 700;
  font-size: 14px;
  margin-top: 6px;
}
.achievement-desc {
  font-size: 12px;
  color: #6b7280;
  margin-top: 4px;
}

/* ========== Table ========== */
.table-wrap {
  background: white;
  border-radius: 20px;
  padding: 18px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.06);
}
.table-hint {
  text-align: center;
  color: #6b7280;
  font-size: 14px;
  margin-bottom: 16px;
  line-height: 1.5;
}
.unit-ladder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.ladder-unit {
  background: linear-gradient(135deg, #ede9fe, #f0f9ff);
  border-radius: 12px;
  padding: 10px 16px;
  text-align: center;
  min-width: 200px;
  border: 2px solid #c4b5fd;
}
.ladder-unit b { font-size: 18px; color: #4338ca; display: block; }
.ladder-unit span { font-size: 12px; color: #6b7280; }
.ladder-arrow {
  color: #a855f7;
  font-weight: 700;
  font-size: 14px;
}
.examples {
  margin-top: 24px;
  background: #f9fafb;
  border-radius: 12px;
  padding: 14px 18px;
}
.examples h3 { font-size: 16px; margin-bottom: 8px; }
.examples ul { list-style: none; }
.examples li {
  padding: 6px 0;
  border-bottom: 1px dashed #e5e7eb;
  font-family: 'Consolas', 'Courier New', monospace;
  font-size: 14px;
  color: #374151;
}
.examples li:last-child { border-bottom: none; }

/* ========== Settings ========== */
.settings-list {
  background: white;
  border-radius: 20px;
  padding: 18px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.06);
}
.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  font-size: 16px;
  border-bottom: 1px solid #f3f4f6;
}
.setting-row:last-of-type { border-bottom: none; }
.setting-row input[type="checkbox"] {
  width: 22px; height: 22px;
  accent-color: #6366f1;
  cursor: pointer;
}
.settings-info {
  margin-top: 18px;
  text-align: center;
  color: #9ca3af;
  font-size: 12px;
  line-height: 1.6;
}

/* ========== Confetti ========== */
#confetti {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9998;
}

/* ========== Toast ========== */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(140%);
  background: linear-gradient(135deg, #6366f1, #a855f7);
  color: white;
  padding: 14px 22px;
  border-radius: 999px;
  font-weight: 600;
  box-shadow: 0 8px 24px rgba(99,102,241,0.4);
  z-index: 9999;
  transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
  max-width: 90%;
  text-align: center;
}
.toast.show {
  transform: translateX(-50%) translateY(0);
}

/* ========== Star burst animation ========== */
.star-burst {
  position: fixed;
  font-size: 40px;
  pointer-events: none;
  z-index: 9997;
  animation: starBurst 1s ease-out forwards;
}
@keyframes starBurst {
  0% { transform: scale(0) translateY(0); opacity: 1; }
  50% { transform: scale(1.3) translateY(-30px); opacity: 1; }
  100% { transform: scale(0.8) translateY(-80px); opacity: 0; }
}
