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

:root {
  --pink: #FF6B9D;
  --pink-light: #FFB8D0;
  --pink-bg: #FFF0F5;
  --purple: #B388FF;
  --purple-light: #E1BEE7;
  --purple-dark: #7C4DFF;
  --yellow: #FFD54F;
  --green: #81C784;
  --green-light: #C8E6C9;
  --red: #EF5350;
  --red-light: #FFCDD2;
  --blue: #64B5F6;
  --white: #FFFFFF;
  --shadow: 0 4px 15px rgba(255, 107, 157, 0.25);
  --shadow-lg: 0 8px 30px rgba(255, 107, 157, 0.3);
  --radius: 20px;
  --radius-lg: 28px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans SC", sans-serif;
}

html, body {
  width: 100%;
  min-height: 100vh;
  font-family: var(--font);
  background: linear-gradient(135deg, #FFF0F5 0%, #F3E5F5 50%, #E8EAF6 100%);
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

body {
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.screen {
  display: none;
  width: 100%;
  min-height: 100vh;
  padding: 16px;
  animation: fadeIn 0.3s ease;
}

.screen.active {
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* ===== Animations ===== */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes bounce {
  0%, 100% { transform: scale(1); }
  30% { transform: scale(1.15); }
  50% { transform: scale(0.95); }
  70% { transform: scale(1.05); }
}

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

@keyframes pop {
  0% { transform: scale(0.5); opacity: 0; }
  70% { transform: scale(1.1); }
  100% { transform: scale(1); opacity: 1; }
}

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

@keyframes sparkle {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.6; transform: scale(1.3); }
}

@keyframes slideLeft {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

@keyframes slideRight {
  from { transform: translateX(-100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

@keyframes correctFlash {
  0% { background-color: transparent; }
  50% { background-color: rgba(129, 199, 132, 0.3); }
  100% { background-color: transparent; }
}

@keyframes wrongFlash {
  0% { background-color: transparent; }
  50% { background-color: rgba(239, 83, 80, 0.2); }
  100% { background-color: transparent; }
}

@keyframes starBurst {
  0% { transform: scale(0) rotate(0deg); opacity: 1; }
  100% { transform: scale(1.5) rotate(180deg); opacity: 0; }
}

.animate-bounce { animation: bounce 0.5s ease; }
.animate-shake { animation: shake 0.5s ease; }
.animate-pop { animation: pop 0.4s ease; }
.animate-float { animation: float 2s ease-in-out infinite; }

/* ===== Common Components ===== */
.back-btn {
  position: fixed;
  top: 16px;
  left: 16px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--white);
  border: none;
  font-size: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow);
  cursor: pointer;
  z-index: 100;
  transition: transform 0.2s;
}

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

.page-title {
  font-size: 28px;
  font-weight: 800;
  background: linear-gradient(135deg, var(--pink), var(--purple-dark));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-align: center;
  margin-bottom: 4px;
}

.page-subtitle {
  font-size: 14px;
  color: #B388FF;
  text-align: center;
  margin-bottom: 20px;
}

/* ===== Home Screen ===== */
#home {
  padding-top: 40px;
}

.home-title {
  font-size: 34px;
  font-weight: 900;
  background: linear-gradient(135deg, var(--pink), var(--purple-dark), var(--pink));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 6px;
  animation: float 3s ease-in-out infinite;
}

.home-subtitle {
  font-size: 15px;
  color: var(--purple);
  margin-bottom: 28px;
  letter-spacing: 2px;
}

.home-deco {
  font-size: 24px;
  margin-bottom: 16px;
  animation: sparkle 2s ease-in-out infinite;
}

.unit-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  width: 100%;
  max-width: 400px;
  padding-bottom: 30px;
}

.unit-card {
  background: var(--white);
  border-radius: var(--radius-lg);
  padding: 20px 12px;
  text-align: center;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: all 0.2s;
  border: 3px solid transparent;
  position: relative;
  overflow: hidden;
}

.unit-card::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,107,157,0.05) 0%, transparent 70%);
  pointer-events: none;
}

.unit-card:active {
  transform: scale(0.95);
  border-color: var(--pink-light);
}

.unit-card .unit-emoji {
  font-size: 40px;
  display: block;
  margin-bottom: 8px;
}

.unit-card .unit-label {
  font-size: 16px;
  font-weight: 700;
  color: var(--purple-dark);
}

.unit-card .unit-count {
  font-size: 12px;
  color: var(--pink);
  margin-top: 4px;
}

/* ===== Mode Selection ===== */
#mode-select {
  padding-top: 70px;
}

.mode-unit-title {
  font-size: 20px;
  color: var(--purple-dark);
  font-weight: 700;
  margin-bottom: 4px;
}

.mode-word-count {
  font-size: 14px;
  color: var(--pink);
  margin-bottom: 30px;
}

.mode-buttons {
  display: flex;
  flex-direction: column;
  gap: 18px;
  width: 100%;
  max-width: 320px;
}

.mode-btn {
  padding: 28px 20px;
  border-radius: var(--radius-lg);
  border: none;
  font-size: 22px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: var(--shadow-lg);
  position: relative;
  overflow: hidden;
}

.mode-btn:active {
  transform: scale(0.96);
}

.mode-btn.flashcard {
  background: linear-gradient(135deg, #FFB8D0, #FF6B9D);
  color: var(--white);
}

.mode-btn.spelling {
  background: linear-gradient(135deg, #E1BEE7, #B388FF);
  color: var(--white);
}

.mode-btn .mode-desc {
  display: block;
  font-size: 13px;
  font-weight: 400;
  margin-top: 6px;
  opacity: 0.9;
}

/* ===== Flashcard Screen ===== */
#flashcard {
  padding-top: 60px;
  justify-content: flex-start;
}

.flash-progress {
  font-size: 16px;
  font-weight: 700;
  color: var(--purple-dark);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.flash-progress .shuffle-btn {
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  padding: 4px;
  transition: transform 0.3s;
}

.flash-progress .shuffle-btn:active {
  transform: rotate(180deg);
}

.flash-card-container {
  width: 100%;
  max-width: 380px;
  position: relative;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  min-height: 0;
}

.flash-image-wrap {
  width: 100%;
  aspect-ratio: 1 / 1;
  max-height: 55vh;
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--white);
  box-shadow: var(--shadow-lg);
  cursor: pointer;
  transition: transform 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.flash-image-wrap img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #fafafa;
}

.flash-image-wrap .tap-hint {
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(255,107,157,0.85);
  color: white;
  font-size: 13px;
  font-weight: 600;
  padding: 6px 16px;
  border-radius: 20px;
  pointer-events: none;
  transition: opacity 0.3s;
}

.flash-word-area {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-top: 12px;
}

.flash-word {
  font-size: 36px;
  font-weight: 800;
  color: var(--purple-dark);
  opacity: 0;
  transition: opacity 0.4s;
  text-align: center;
}

.flash-word.visible {
  opacity: 1;
  animation: pop 0.4s ease;
}

.speak-btn {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--pink-light), var(--pink));
  border: none;
  font-size: 24px;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  opacity: 0;
  transition: opacity 0.3s, transform 0.2s;
}

.speak-btn.visible {
  opacity: 1;
}

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

.flash-nav {
  display: flex;
  gap: 20px;
  margin-top: 10px;
  padding-bottom: 20px;
}

.flash-nav button {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: none;
  font-size: 26px;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.flash-nav button:active {
  transform: scale(0.9);
}

.flash-nav .prev-btn {
  background: linear-gradient(135deg, #E1BEE7, var(--purple));
  color: white;
}

.flash-nav .next-btn {
  background: linear-gradient(135deg, var(--pink-light), var(--pink));
  color: white;
}

/* ===== Spelling Screen ===== */
#spelling {
  padding-top: 60px;
  justify-content: flex-start;
}

.spell-progress-bar {
  width: 100%;
  max-width: 380px;
  height: 12px;
  background: var(--pink-light);
  border-radius: 6px;
  overflow: hidden;
  margin-bottom: 8px;
}

.spell-progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--pink), var(--purple));
  border-radius: 6px;
  transition: width 0.4s ease;
}

.spell-progress-text {
  font-size: 14px;
  color: var(--purple-dark);
  font-weight: 600;
  margin-bottom: 12px;
}

.spell-image-wrap {
  width: 100%;
  max-width: 340px;
  aspect-ratio: 1 / 1;
  max-height: 42vh;
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--white);
  box-shadow: var(--shadow-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.spell-image-wrap img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #fafafa;
}

.spell-controls {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 16px;
  width: 100%;
  max-width: 340px;
}

.spell-input {
  flex: 1;
  height: 56px;
  border-radius: 28px;
  border: 3px solid var(--pink-light);
  padding: 0 20px;
  font-size: 22px;
  font-family: var(--font);
  font-weight: 600;
  text-align: center;
  outline: none;
  transition: border-color 0.3s;
  background: var(--white);
  color: #333;
}

.spell-input:focus {
  border-color: var(--purple);
}

.spell-input.correct {
  border-color: var(--green);
  background: var(--green-light);
}

.spell-input.wrong {
  border-color: var(--red);
  background: var(--red-light);
}

.spell-speak-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--pink-light), var(--pink));
  border: none;
  font-size: 26px;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.2s;
  flex-shrink: 0;
}

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

.spell-submit-btn {
  width: 100%;
  max-width: 340px;
  height: 52px;
  border-radius: 26px;
  border: none;
  background: linear-gradient(135deg, var(--purple), var(--purple-dark));
  color: white;
  font-size: 20px;
  font-weight: 700;
  cursor: pointer;
  margin-top: 12px;
  box-shadow: var(--shadow);
  transition: all 0.2s;
  font-family: var(--font);
}

.spell-submit-btn:active {
  transform: scale(0.96);
}

.spell-feedback {
  font-size: 28px;
  font-weight: 800;
  margin-top: 14px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.spell-feedback.correct-feedback {
  color: var(--green);
  animation: bounce 0.5s ease;
}

.spell-feedback.wrong-feedback {
  color: var(--red);
  animation: shake 0.5s ease;
}

.spell-score {
  font-size: 14px;
  color: var(--purple);
  margin-top: 4px;
}

/* ===== Results Screen ===== */
#results {
  padding-top: 60px;
  justify-content: center;
}

.results-emoji {
  font-size: 80px;
  animation: bounce 0.8s ease;
  margin-bottom: 16px;
}

.results-title {
  font-size: 24px;
  font-weight: 800;
  color: var(--purple-dark);
  margin-bottom: 8px;
}

.results-accuracy {
  font-size: 40px;
  font-weight: 900;
  background: linear-gradient(135deg, var(--pink), var(--purple-dark));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 8px;
}

.results-detail {
  font-size: 16px;
  color: var(--purple);
  margin-bottom: 24px;
}

.results-message {
  font-size: 18px;
  color: var(--pink);
  font-weight: 600;
  margin-bottom: 32px;
  text-align: center;
  line-height: 1.6;
}

.wrong-words-section {
  width: 100%;
  max-width: 340px;
  margin-bottom: 20px;
}

.wrong-words-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--red);
  margin-bottom: 10px;
  text-align: center;
}

.wrong-words-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.wrong-word-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--white);
  border: 2px solid var(--red-light);
  border-radius: 16px;
  padding: 8px 14px;
  font-size: 15px;
  font-weight: 600;
  color: var(--red);
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 2px 8px rgba(239, 83, 80, 0.15);
}

.wrong-word-chip:active {
  transform: scale(0.95);
  background: var(--red-light);
}

.wrong-word-chip .chip-speaker {
  font-size: 14px;
}

.results-buttons {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 100%;
  max-width: 300px;
}

.result-btn {
  padding: 18px;
  border-radius: var(--radius);
  border: none;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: all 0.2s;
  font-family: var(--font);
}

.result-btn:active {
  transform: scale(0.96);
}

.result-btn.retry {
  background: linear-gradient(135deg, var(--pink-light), var(--pink));
  color: white;
}

.result-btn.home-btn {
  background: var(--white);
  color: var(--purple-dark);
  border: 2px solid var(--purple-light);
}

/* ===== Star decoration ===== */
.stars {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: -1;
  overflow: hidden;
}

.star {
  position: absolute;
  font-size: 16px;
  opacity: 0.15;
  animation: float 4s ease-in-out infinite;
}

/* ===== Landscape hint ===== */
@media (orientation: landscape) and (max-height: 500px) {
  .flash-image-wrap {
    max-height: 40vh;
    aspect-ratio: auto;
    height: 40vh;
  }
  .spell-image-wrap {
    max-height: 35vh;
    aspect-ratio: auto;
    height: 35vh;
  }
}