* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

:root {
  --bg: #f4f7fb;
  --panel: #ffffff;
  --ink: #1c2733;
  --sub: #5b6b7c;
  --primary: #2563eb;
  --primary-ink: #ffffff;
  --good: #16a34a;
  --bad: #dc2626;
  --accent: #f59e0b;
  --border: #dde5ee;
  --shadow: 0 6px 18px rgba(28, 39, 51, 0.08);
  --good-tint: #e7f8ee;
  --bad-tint: #fdeaea;
  --overlay-bg: rgba(244, 247, 251, 0.9);
}

:root[data-theme="dark"] {
  --bg: #10151d;
  --panel: #1a212c;
  --ink: #eef2f7;
  --sub: #93a1b0;
  --primary: #3b82f6;
  --primary-ink: #ffffff;
  --good: #22c55e;
  --bad: #f87171;
  --accent: #fbbf24;
  --border: #2a3441;
  --shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  --good-tint: rgba(34, 197, 94, 0.15);
  --bad-tint: rgba(248, 113, 113, 0.15);
  --overlay-bg: rgba(16, 21, 29, 0.92);
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: 'Segoe UI', system-ui, sans-serif;
  overflow: hidden;
  touch-action: manipulation;
}

button { font-family: inherit; cursor: pointer; border: none; color: inherit; }

#topbar {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

#crumb {
  font-size: clamp(14px, 2vw, 20px);
  font-weight: 600;
  color: var(--sub);
}

.iconBtn {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.iconBtn:active { transform: scale(0.94); }

#app {
  height: calc(100% - 64px);
  overflow: auto;
  padding: clamp(12px, 2.5vw, 32px);
}

.screen { display: none; height: 100%; flex-direction: column; }
.screen.active { display: flex; }

h1 {
  font-size: clamp(28px, 4.5vw, 48px);
  text-align: center;
  margin-bottom: 4px;
}
h1 span {
  display: block;
  font-size: clamp(16px, 2.2vw, 24px);
  color: var(--sub);
  font-weight: 400;
  margin-top: 4px;
}

h2 { font-size: clamp(22px, 3.5vw, 34px); text-align: center; margin-bottom: 8px; }

.bonusToggle {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: center;
  margin: 14px auto 22px;
  font-size: clamp(14px, 1.8vw, 18px);
  color: var(--sub);
}
.bonusToggle input { width: 22px; height: 22px; accent-color: var(--primary); }

.cardGrid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: clamp(12px, 2vw, 20px);
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

.unitCard {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: clamp(16px, 2.5vw, 26px);
  box-shadow: var(--shadow);
  text-align: left;
  min-height: 120px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: transform 0.12s ease;
}
.unitCard:active { transform: scale(0.97); }
.unitCard .unitNum { font-size: clamp(13px, 1.6vw, 15px); color: var(--primary); font-weight: 700; letter-spacing: 1px; }
.unitCard .unitTopic { font-size: clamp(18px, 2.4vw, 24px); font-weight: 700; }
.unitCard .unitCount { font-size: clamp(13px, 1.6vw, 15px); color: var(--sub); }

.bookCard {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: clamp(20px, 3vw, 32px);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 10px;
}
.bookCard:active { transform: scale(0.97); }
.bookCard .bookIcon { font-size: clamp(40px, 6vw, 60px); }
.bookCard .bookTitle { font-size: clamp(18px, 2.4vw, 24px); font-weight: 700; }
.bookCard .bookUnitCount { font-size: clamp(13px, 1.6vw, 15px); color: var(--sub); }

.changeBookBtn {
  display: none;
  margin: 0 auto 14px;
}
.changeBookBtn.show { display: block; }

.modeGrid { max-width: 980px; }
.modeCard {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: clamp(20px, 3vw, 32px);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 10px;
  min-height: 220px;
}
.modeCard:active { transform: scale(0.97); }
.modeEmoji { font-size: clamp(40px, 6vw, 64px); }
.modeName { font-size: clamp(20px, 2.6vw, 26px); font-weight: 700; }
.modeDesc { font-size: clamp(13px, 1.6vw, 16px); color: var(--sub); }

.gameHud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  margin-bottom: 14px;
  font-size: clamp(16px, 2vw, 22px);
  font-weight: 700;
  flex-shrink: 0;
}
.hudMid { flex: 1; text-align: center; color: var(--ink); font-size: clamp(15px, 2.2vw, 24px); }
.hudRight { font-size: clamp(18px, 2.4vw, 26px); }

#quizProgressBarWrap {
  width: 140px;
  height: 10px;
  background: var(--bg);
  border-radius: 6px;
  overflow: hidden;
  font-size: 0;
}
#quizProgressBar { height: 100%; background: var(--primary); width: 0%; transition: width 0.3s ease; }

/* ---------- Catch game ---------- */
#catchField,
#survivalField {
  position: relative;
  flex: 1;
  background: var(--bg);
  border-radius: 20px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.bubble {
  position: absolute;
  top: 0;
  /* transform is set inline via JS every frame (compositor-only, avoids layout thrash) */
  will-change: transform;
  background: var(--primary);
  color: #fff;
  padding: 14px 22px;
  border-radius: 999px;
  font-size: clamp(16px, 2.2vw, 24px);
  font-weight: 700;
  box-shadow: 0 6px 16px rgba(37, 99, 235, 0.35);
  white-space: nowrap;
}
.bubble.wrong { background: var(--bad); box-shadow: 0 6px 16px rgba(220, 38, 38, 0.35); }
.bubble.correctFlash { background: var(--good); box-shadow: 0 6px 16px rgba(22, 163, 74, 0.4); }

.floatText {
  position: absolute;
  font-size: clamp(20px, 3vw, 34px);
  font-weight: 800;
  pointer-events: none;
  animation: floatUp 0.9s ease forwards;
  transform: translate(-50%, -50%);
}
@keyframes floatUp {
  0% { opacity: 1; transform: translate(-50%, -50%) translateY(0); }
  100% { opacity: 0; transform: translate(-50%, -50%) translateY(-60px); }
}

/* ---------- Match game ---------- */
#matchGrid {
  flex: 1;
  display: grid;
  gap: clamp(8px, 1.4vw, 14px);
  align-content: center;
  justify-content: center;
}

.matchCard {
  width: clamp(110px, 15vw, 200px);
  height: clamp(90px, 12vw, 140px);
  border-radius: 14px;
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  font-size: clamp(13px, 1.6vw, 18px);
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 8px;
  box-shadow: var(--shadow);
  line-height: 1.25;
}
.matchCard.flipped { background: var(--panel); color: var(--ink); border: 2px solid var(--primary); }
.matchCard.matched { background: var(--good-tint); color: var(--good); border: 2px solid var(--good); opacity: 0.85; }
.matchCard.shake { animation: shake 0.4s ease; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

/* ---------- Quiz game ---------- */
#quizCard {
  text-align: center;
  padding: clamp(16px, 3vw, 32px);
  flex-shrink: 0;
}
#quizEmoji { font-size: clamp(48px, 8vw, 84px); }
#quizWord { font-size: clamp(28px, 5vw, 52px); font-weight: 800; margin-top: 6px; }

#quizOptions {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: clamp(10px, 1.6vw, 18px);
  max-width: 1000px;
  margin: 0 auto;
  width: 100%;
  align-content: start;
}

.quizOption {
  background: var(--panel);
  border: 2px solid var(--border);
  border-radius: 16px;
  padding: clamp(14px, 2vw, 22px);
  font-size: clamp(15px, 1.8vw, 20px);
  font-weight: 600;
  text-align: left;
  box-shadow: var(--shadow);
}
.quizOption:active { transform: scale(0.98); }
.quizOption.correct { background: var(--good-tint); border-color: var(--good); color: var(--good); }
.quizOption.incorrect { background: var(--bad-tint); border-color: var(--bad); color: var(--bad); }

/* ---------- Study slideshow ---------- */
.studyToggleBtn {
  background: var(--primary);
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-weight: 700;
  font-size: clamp(13px, 1.6vw, 16px);
}
.studyToggleBtn:active { transform: scale(0.95); }

#studySlide {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 20px;
  gap: 10px;
  padding: 20px;
}
#studyEmoji { font-size: clamp(56px, 10vw, 110px); }
#studyWord { font-size: clamp(32px, 6vw, 64px); font-weight: 800; }
#studyMeaning { font-size: clamp(18px, 3vw, 32px); color: var(--sub); max-width: 900px; }

#studyNav {
  display: flex;
  justify-content: center;
  gap: 16px;
  padding: 16px 0;
  flex-shrink: 0;
}

#studyListWrap {
  display: none;
  flex: 1;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: clamp(10px, 2vw, 20px);
}

#studyTable { width: 100%; border-collapse: collapse; font-size: clamp(14px, 1.8vw, 19px); }
#studyTable th {
  text-align: left;
  padding: 10px 12px;
  color: var(--sub);
  border-bottom: 2px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--panel);
}
#studyTable td { padding: 12px; border-bottom: 1px solid var(--border); }
#studyTable td.wordCell { font-weight: 700; white-space: nowrap; }
#studyTable td.emojiCell { font-size: 1.4em; width: 40px; }

body.studyListMode #studySlide,
body.studyListMode #studyNav { display: none; }
body.studyListMode #studyListWrap { display: block; }

/* ---------- Team Race ---------- */
#screen-race { position: relative; gap: 10px; flex-direction: row; }

.raceLane {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: clamp(10px, 1.6vw, 18px);
  min-height: 0;
  min-width: 0;
}
.raceLane.team1 { border-left: 5px solid #2563eb; }
.raceLane.team2 { border-left: 5px solid #dc2626; }

.raceDivider {
  display: flex;
  align-items: center;
  justify-content: center;
  writing-mode: vertical-rl;
  font-weight: 800;
  color: var(--sub);
  font-size: clamp(12px, 1.6vw, 16px);
  letter-spacing: 2px;
  flex-shrink: 0;
  width: clamp(20px, 2.5vw, 32px);
}

.laneHeader {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: clamp(14px, 1.8vw, 20px);
}

.laneTimer {
  font-variant-numeric: tabular-nums;
  color: var(--sub);
  flex-shrink: 0;
}
.laneTimer.urgent { color: var(--bad); }

.timerBarWrap {
  height: 14px;
  border-radius: 999px;
  background: var(--bg);
  overflow: hidden;
  flex-shrink: 0;
  border: 2px solid var(--border);
  padding: 2px;
}
.timerBarFill {
  height: 100%;
  width: 100%;
  border-radius: 999px;
  background: #16a34a;
  transition: width 0.25s linear, background-color 0.4s ease;
}

.track {
  position: relative;
  height: clamp(34px, 5vw, 52px);
  flex-shrink: 0;
}
.trackLine {
  position: absolute;
  left: 0; right: 0; top: 50%;
  height: 4px;
  background: repeating-linear-gradient(90deg, var(--border) 0 16px, transparent 16px 32px);
  transform: translateY(-50%);
}
.car {
  position: absolute;
  top: 50%;
  left: 0%;
  transform: translate(-10%, -50%);
  font-size: clamp(26px, 4vw, 40px);
  transition: left 0.6s cubic-bezier(.34,1.56,.64,1);
}
.flag {
  position: absolute;
  right: -4px;
  top: 50%;
  transform: translateY(-50%);
  font-size: clamp(22px, 3.4vw, 34px);
}

.raceMeaning {
  text-align: center;
  font-size: clamp(13px, 1.7vw, 18px);
  color: var(--sub);
  min-height: 1.4em;
}

.slotsRow {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
}
.raceSlot {
  min-width: clamp(24px, 3.2vw, 36px);
  height: clamp(28px, 3.6vw, 40px);
  border-bottom: 3px solid var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(16px, 2.2vw, 24px);
  font-weight: 800;
  text-transform: uppercase;
}
.raceSlot.fixed { border-bottom-color: transparent; color: var(--sub); }
.raceSlot.space { min-width: 12px; border-bottom-color: transparent; }

.letterBank {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}
.letterTile {
  width: clamp(34px, 4.6vw, 52px);
  height: clamp(34px, 4.6vw, 52px);
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
  font-size: clamp(16px, 2.2vw, 24px);
  text-transform: uppercase;
  box-shadow: var(--shadow);
}
.letterTile:active { transform: scale(0.92); }
.letterTile.used { visibility: hidden; }
.letterTile.shake { animation: shake 0.35s ease; }

.raceCountdown {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: clamp(64px, 14vw, 160px);
  font-weight: 900;
  color: var(--primary);
  background: var(--overlay-bg);
  border-radius: 20px;
  z-index: 40;
}
.raceCountdown.show { display: flex; }

/* ---------- Results ---------- */
#screen-results { align-items: center; justify-content: center; }
#resultsBox {
  background: var(--panel);
  border-radius: 22px;
  box-shadow: var(--shadow);
  padding: clamp(24px, 4vw, 44px);
  text-align: center;
  max-width: 560px;
  width: 100%;
}
#resultsScore { font-size: clamp(18px, 2.4vw, 26px); color: var(--sub); margin: 10px 0 18px; }
#resultsReview {
  text-align: left;
  max-height: 220px;
  overflow-y: auto;
  margin-bottom: 20px;
  font-size: clamp(13px, 1.6vw, 16px);
}
#resultsReview .reviewRow { padding: 8px 0; border-bottom: 1px solid var(--border); }
#resultsReview .reviewRow b { color: var(--primary); }

.resultsBtns { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }

.bigBtn {
  padding: 16px 28px;
  border-radius: 999px;
  font-size: clamp(15px, 1.8vw, 19px);
  font-weight: 700;
  background: var(--primary);
  color: var(--primary-ink);
  box-shadow: var(--shadow);
}
.bigBtn.secondary { background: var(--bg); color: var(--ink); border: 1px solid var(--border); }
.bigBtn:active { transform: scale(0.96); }
.bigBtn:disabled { opacity: 0.35; cursor: default; }
.bigBtn:disabled:active { transform: none; }

/* ---------- Hard Mode card ---------- */
.hardModeCard {
  background: linear-gradient(135deg, #7c2d12, #b91c1c);
  border: 1px solid #7c2d12;
  color: #fff;
}
.hardModeCard .unitNum { color: #fed7aa; }
.hardModeCard .unitTopic { color: #fff; }
.hardModeCard .unitCount { color: #fecaca; }

/* ---------- Rankings ---------- */
#screen-rankings { align-items: center; }
#rankingsBox {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 22px;
  box-shadow: var(--shadow);
  padding: clamp(20px, 3.5vw, 36px);
  max-width: 1100px;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.rankingsColumns {
  flex: 1;
  display: flex;
  gap: clamp(12px, 2vw, 24px);
  min-height: 0;
}

.rankingsCol {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--bg);
  border-radius: 16px;
  padding: clamp(10px, 1.6vw, 16px);
}
.rankingsCol h3 {
  text-align: center;
  font-size: clamp(14px, 1.8vw, 18px);
  flex-shrink: 0;
}

.rankingsList {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  min-height: 80px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.rankingsList .rankRow {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--panel);
  font-family: 'Consolas', 'Courier New', monospace;
  font-size: clamp(12px, 1.4vw, 15px);
}
.rankingsList .rankRow:nth-child(1) { background: linear-gradient(90deg, #fef3c7, transparent 80%); }
:root[data-theme="dark"] .rankingsList .rankRow:nth-child(1) { background: linear-gradient(90deg, rgba(251,191,36,0.18), transparent 80%); }
.rankingsList .rankPos { font-weight: 800; width: 2.2em; color: var(--sub); }
.rankingsList .rankName { flex: 1; font-weight: 700; }
.rankingsList .rankScore { font-weight: 800; color: var(--primary); }
.rankingsEmpty { text-align: center; color: var(--sub); padding: 30px 0; }

/* ---------- Save score ---------- */
.saveScoreRow {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}
.saveScoreRow.show { display: flex; }
.newRecordLabel {
  font-weight: 800;
  color: var(--accent);
  font-size: clamp(15px, 2vw, 19px);
  animation: recordPulse 1s ease-in-out infinite;
}
@keyframes recordPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}
.saveScoreInputRow {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}
.saveScoreRow input {
  padding: 14px 16px;
  border-radius: 12px;
  border: 2px solid var(--border);
  background: var(--bg);
  color: var(--ink);
  font-size: 28px;
  font-weight: 800;
  font-family: 'Consolas', 'Courier New', monospace;
  letter-spacing: 8px;
  text-align: center;
  text-transform: uppercase;
  width: 140px;
}
.saveScoreConfirm {
  text-align: center;
  color: var(--good);
  font-weight: 700;
  min-height: 1.2em;
  margin-bottom: 10px;
}
.saveScoreConfirm.error { color: var(--bad); }

/* ---------- Settings ---------- */
#screen-settings { align-items: center; }
#settingsBox {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 22px;
  box-shadow: var(--shadow);
  padding: clamp(20px, 3.5vw, 40px);
  max-width: 560px;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.settingsGroup h3 {
  font-size: clamp(14px, 1.8vw, 18px);
  color: var(--sub);
  margin-bottom: 10px;
  font-weight: 700;
}

.pillRow {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.pillBtn {
  padding: 12px 22px;
  border-radius: 999px;
  background: var(--bg);
  color: var(--ink);
  border: 2px solid var(--border);
  font-weight: 700;
  font-size: clamp(14px, 1.7vw, 17px);
}
.pillBtn:active { transform: scale(0.96); }
.pillBtn.active {
  background: var(--primary);
  color: var(--primary-ink);
  border-color: var(--primary);
}

.customTimerRow {
  display: none;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
}
.customTimerRow.show { display: flex; }
.customTimerRow input {
  width: 100px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 2px solid var(--border);
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  font-weight: 700;
}
.customTimerRow span { color: var(--sub); font-size: 14px; }

#credit {
  position: fixed;
  right: 14px;
  bottom: 10px;
  font-size: clamp(11px, 1.3vw, 14px);
  color: var(--sub);
  opacity: 0.65;
  pointer-events: none;
  z-index: 50;
  font-weight: 600;
  letter-spacing: 0.2px;
}

@media (max-width: 640px) {
  #topbar { height: 56px; }
  #app { height: calc(100% - 56px); }
}
.rankingsSource { text-align: center; opacity: .75; margin: -6px 0 12px; font-size: .95em; }
