:root {
  --blue: #1a3e72;
  --blue-light: #2c5aa0;
  --red: #e00025;
  --gold: #f0c419;
  --green: #2ecc71;
  --bg: #f4f6fa;
  --card-bg: #ffffff;
  --text: #1c2230;
  --text-muted: #6b7585;
  --radius: 14px;
  --nav-height: 64px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Khmer", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body {
  padding-bottom: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px));
}

a { text-decoration: none; color: inherit; }
button { font-family: inherit; }

/* Header */
header.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--blue);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
}

.title-group {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

header.app-header h1 {
  font-size: 1.1rem;
  margin: 0;
  font-weight: 700;
  text-align: center;
}

.app-badge {
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--blue);
  background: var(--gold);
  padding: 2px 8px;
  border-radius: 999px;
}

.home-link {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  text-decoration: none;
  font-size: 1.1rem;
  flex-shrink: 0;
}

.home-link .label {
  display: none;
  font-size: 0.75rem;
  font-weight: 600;
}

@media (min-width: 640px) {
  .home-link {
    width: auto;
    padding: 0 12px;
  }
  .home-link .label { display: inline; }
}

.home-link:hover {
  background: rgba(255, 255, 255, 0.3);
}

#lang-toggle {
  background: var(--gold);
  color: var(--blue);
  border: none;
  border-radius: 999px;
  padding: 6px 14px;
  font-weight: 700;
  font-size: 0.85rem;
}

/* Main content */
main#app {
  max-width: 600px;
  margin: 0 auto;
  padding: 16px;
}

.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}

.page-title {
  margin: 6px 4px 14px;
}

.section-title {
  margin: 18px 4px 8px;
  font-size: 1rem;
  color: var(--text-muted);
}

/* Hero / stats */
.hero h2 { margin-top: 0; }

.stat-row {
  display: flex;
  justify-content: space-between;
  text-align: center;
  gap: 8px;
}

.stat {
  display: flex;
  flex-direction: column;
  flex: 1;
}

.stat-value {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--blue);
}

.stat-label {
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* Action cards */
.action-card {
  display: flex;
  align-items: center;
  gap: 14px;
  transition: transform 0.1s ease;
}

.action-card:active { transform: scale(0.98); }

.action-card.primary {
  background: linear-gradient(135deg, var(--blue), var(--blue-light));
  color: #fff;
}

.action-card.primary p { color: rgba(255,255,255,0.85); }

.action-card h3 { margin: 0 0 4px; font-size: 1rem; }
.action-card p { margin: 0; font-size: 0.85rem; color: var(--text-muted); }

.action-icon {
  font-size: 1.8rem;
  flex-shrink: 0;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(26,62,114,0.08);
  border-radius: 12px;
}

.action-card.primary .action-icon {
  background: rgba(255,255,255,0.15);
}

.disclaimer {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
  padding: 0 8px;
  line-height: 1.4;
}

/* Categories */
.category-card {
  display: flex;
  gap: 14px;
  align-items: center;
}

.cat-icon { font-size: 1.8rem; }
.cat-body { flex: 1; }
.cat-body h3 { margin: 0 0 4px; }
.cat-body p { margin: 0 0 6px; font-size: 0.8rem; color: var(--text-muted); }
.mastery-label { font-size: 0.75rem; color: var(--text-muted); }

.survival-toggle {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
}
.survival-toggle input { width: 20px; height: 20px; }
.survival-toggle p { margin: 2px 0 0; font-size: 0.8rem; color: var(--text-muted); }

/* Progress bar */
.progress-bar {
  height: 8px;
  background: #e3e8f0;
  border-radius: 999px;
  overflow: hidden;
  flex: 1;
}
.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--gold), var(--red));
  border-radius: 999px;
  transition: width 0.4s ease;
}

/* Race progress track */
.race-track {
  position: relative;
  height: 40px;
  margin: 6px 6px 30px;
}
.race-road {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 8px;
  transform: translateY(-50%);
  background: #e3e8f0;
  border-radius: 999px;
  overflow: hidden;
}
.race-road::after {
  content: '';
  position: absolute;
  inset: 0;
  background-image: repeating-linear-gradient(90deg, #fff 0 8px, transparent 8px 18px);
  opacity: 0.6;
}
.race-fill {
  position: absolute;
  top: 50%;
  left: 0;
  height: 8px;
  transform: translateY(-50%);
  background: linear-gradient(90deg, var(--gold), var(--red));
  border-radius: 999px;
  transition: width 0.5s ease;
}
.race-flag {
  position: absolute;
  right: -6px;
  top: 50%;
  transform: translateY(-65%);
  font-size: 18px;
}
.race-car {
  position: absolute;
  top: 50%;
  width: 38px;
  height: 38px;
  transform: translate(-50%, -62%);
  transition: left 0.5s ease;
  filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.18));
}
.race-car.celebrate {
  animation: car-celebrate 0.9s ease;
}
@keyframes car-celebrate {
  0% { transform: translate(-50%, -62%) scale(1) rotate(0); }
  25% { transform: translate(-50%, -85%) scale(1.2) rotate(-10deg); }
  50% { transform: translate(-50%, -62%) scale(1.2) rotate(8deg); }
  75% { transform: translate(-50%, -85%) scale(1.2) rotate(-6deg); }
  100% { transform: translate(-50%, -62%) scale(1) rotate(0); }
}

/* Quiz */
.quiz-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.exit-btn {
  background: none;
  border: 1px solid #d0d6e2;
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.8rem;
  color: var(--text-muted);
}
.timer {
  font-weight: 700;
  color: var(--red);
}
.q-counter {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin: 8px 4px;
}
.hearts {
  font-size: 1.2rem;
  margin: 4px 4px 8px;
}

.question-card h3 { margin-top: 0; line-height: 1.4; }

.sign-image {
  display: block;
  width: 110px;
  height: 110px;
  margin: 0 auto 14px;
}

.km-pending {
  font-size: 0.75rem;
  color: var(--gold);
  background: #fff7e0;
  padding: 6px 10px;
  border-radius: 8px;
}

.options {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 14px 0;
}

.option-btn {
  text-align: left;
  padding: 14px;
  border-radius: 10px;
  border: 2px solid #e3e8f0;
  background: #fff;
  font-size: 0.95rem;
  cursor: pointer;
  transition: all 0.15s ease;
}
.option-btn:active { transform: scale(0.99); }
.option-btn:disabled { cursor: default; }

.option-btn.correct {
  border-color: var(--green);
  background: #eafaf0;
  font-weight: 700;
}
.option-btn.incorrect {
  border-color: var(--red);
  background: #fdecee;
}

.feedback {
  border-radius: 10px;
  padding: 12px 14px;
  margin: 10px 0;
  animation: pop 0.2s ease;
}
.feedback p { margin: 6px 0 0; font-size: 0.9rem; }
.feedback-correct { background: #eafaf0; color: #1d7a45; }
.feedback-incorrect { background: #fdecee; color: #b4233a; }

@keyframes pop {
  from { transform: scale(0.95); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

/* Buttons */
.btn[hidden] {
  display: none;
}
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 14px;
  border-radius: 10px;
  border: none;
  background: #e3e8f0;
  color: var(--text);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  margin-top: 4px;
}
.btn.primary { background: var(--blue); color: #fff; }
.btn.danger { background: #fdecee; color: var(--red); margin-top: 18px; }

/* Results */
.results-card { text-align: center; }
.results-card.pass { border-top: 6px solid var(--green); }
.results-card.fail { border-top: 6px solid var(--gold); }

.score-circle {
  font-size: 2.4rem;
  font-weight: 800;
  color: var(--blue);
  margin: 8px 0;
}
.score-detail { color: var(--text-muted); margin: 0; }
.result-status { font-weight: 700; font-size: 1.1rem; }
.xp-earned { color: var(--gold); font-weight: 700; }

.share-cta {
  margin-bottom: 14px;
}
.share-main {
  background: linear-gradient(135deg, var(--blue), var(--blue-light));
  color: #fff;
  font-size: 1.05rem;
  box-shadow: 0 4px 14px rgba(26, 62, 114, 0.3);
}
.share-row {
  display: none;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 12px;
  padding: 12px;
  background: var(--card-bg);
  border-radius: 10px;
}
.share-row.open { display: flex; }
.share-row-label {
  font-weight: 600;
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-right: 4px;
}
.share-row .share-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: #e3e8f0;
  border: none;
  color: var(--blue);
  cursor: pointer;
  text-decoration: none;
  flex: none;
  margin: 0;
  padding: 0;
  transition: background 0.15s ease, color 0.15s ease;
}
.share-row .share-btn:hover {
  background: var(--blue);
  color: #fff;
}
.share-row .share-btn.copied {
  background: var(--green);
  color: #fff;
}

.results-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 14px;
}

.review-item.correct { border-left: 4px solid var(--green); }
.review-item.incorrect { border-left: 4px solid var(--red); }
.review-item .exp { color: var(--text-muted); font-size: 0.85rem; }

/* Badges */
.badges-earned { text-align: center; border: 2px solid var(--gold); }
.badge-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}
.badge {
  background: var(--card-bg);
  border-radius: 10px;
  padding: 10px;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}
.badge.locked { opacity: 0.5; }
.badge-icon { font-size: 1.5rem; }
.badge p { margin: 2px 0 0; font-size: 0.75rem; color: var(--text-muted); }

/* Flashcards */
.flash-done { text-align: center; }

/* Progress page */
.level-name {
  text-align: center;
  font-weight: 700;
  color: var(--blue);
  margin: 4px 0;
}
.cat-progress-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  font-size: 0.85rem;
}
.cat-progress-row span:first-child { flex: 0 0 130px; }
.cat-progress-row span:last-child { flex: 0 0 36px; text-align: right; color: var(--text-muted); }

/* Bottom nav */
nav#bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: var(--nav-height);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: #fff;
  border-top: 1px solid #e3e8f0;
  display: flex;
  z-index: 10;
}
nav#bottom-nav a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 0.7rem;
  color: var(--text-muted);
}
nav#bottom-nav a .icon { font-size: 1.3rem; }
nav#bottom-nav a.active { color: var(--blue); font-weight: 700; }

/* Confetti */
.confetti {
  position: fixed;
  top: -10px;
  width: 8px;
  height: 14px;
  z-index: 999;
  animation: fall linear forwards;
}
@keyframes fall {
  to { transform: translateY(105vh) rotate(360deg); opacity: 0.6; }
}

@media (min-width: 640px) {
  main#app { padding: 24px; }
}
