:root {
  --bg: #f4f5fb;
  --bg-elevated: #ffffff;
  --bg-soft: #eceeff;
  --text: #191a2b;
  --text-muted: #62657d;
  --border: #e1e3f2;
  --accent: #5b5bf0;
  --accent-2: #ff7a59;
  --accent-soft: #eceafd;
  --correct: #1ea672;
  --correct-soft: #e3f7ee;
  --wrong: #e0455a;
  --wrong-soft: #fdecee;
  --shadow: 0 10px 30px -12px rgba(30, 30, 70, 0.18);
  --radius-lg: 22px;
  --radius-md: 16px;
  --radius-sm: 10px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #121220;
  --bg-elevated: #1b1c2e;
  --bg-soft: #23243a;
  --text: #f1f1fb;
  --text-muted: #9a9cc0;
  --border: #2c2d47;
  --accent: #8686ff;
  --accent-2: #ff9a7a;
  --accent-soft: #26234f;
  --correct: #38d495;
  --correct-soft: #163a2c;
  --wrong: #ff6b7f;
  --wrong-soft: #3a1c22;
  --shadow: 0 10px 30px -12px rgba(0, 0, 0, 0.55);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121220;
    --bg-elevated: #1b1c2e;
    --bg-soft: #23243a;
    --text: #f1f1fb;
    --text-muted: #9a9cc0;
    --border: #2c2d47;
    --accent: #8686ff;
    --accent-2: #ff9a7a;
    --accent-soft: #26234f;
    --correct: #38d495;
    --correct-soft: #163a2c;
    --wrong: #ff6b7f;
    --wrong-soft: #3a1c22;
    --shadow: 0 10px 30px -12px rgba(0, 0, 0, 0.55);
    color-scheme: dark;
  }
}

[hidden] { display: none !important; }

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Segoe UI", "Inter", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
  transition: background 0.25s ease, color 0.25s ease;
}

.app-shell {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ---------- Topbar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 12px) 20px 12px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 34px; height: 34px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 13px;
  letter-spacing: 0.5px;
}
.brand-name { font-weight: 700; font-size: 17px; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.icon-btn {
  width: 38px; height: 38px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.status-pill {
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--bg-soft);
  color: var(--text-muted);
}

/* ---------- Main / Views ---------- */
.app-main {
  flex: 1;
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  padding: 20px 18px calc(env(safe-area-inset-bottom) + 32px);
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.view { display: flex; flex-direction: column; gap: 18px; animation: fadeIn 0.25s ease; }
.view[hidden] { display: none; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

.hero h1 { margin: 4px 0 4px; font-size: 26px; letter-spacing: -0.02em; }
.hero p { margin: 0; color: var(--text-muted); font-size: 15px; }

.panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 18px;
  box-shadow: var(--shadow);
}
.panel-head { display: flex; align-items: center; justify-content: space-between; }
.panel h2 { margin: 0 0 12px; font-size: 15px; font-weight: 700; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }
.panel-head h2 { margin-bottom: 0; }
.link-btn {
  border: none; background: none; color: var(--accent);
  font-weight: 600; font-size: 13px; cursor: pointer; padding: 4px;
}

/* ---------- Deck list ---------- */
.deck-list { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.deck-item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  cursor: pointer;
  background: var(--bg);
  transition: border-color 0.15s ease, background 0.15s ease;
}
.deck-item.checked { border-color: var(--accent); background: var(--accent-soft); }
.deck-item input { accent-color: var(--accent); width: 18px; height: 18px; flex-shrink: 0; }
.deck-item .deck-name { font-weight: 600; flex: 1; }
.deck-item .deck-count { font-size: 12px; color: var(--text-muted); background: var(--bg-soft); padding: 2px 8px; border-radius: 999px; }
.deck-summary { margin: 12px 2px 0; font-size: 13px; color: var(--text-muted); }

/* ---------- Mode grid ---------- */
.mode-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.mode-card {
  display: flex; flex-direction: column; align-items: flex-start; gap: 6px;
  padding: 16px;
  border-radius: var(--radius-md);
  border: 1.5px solid var(--border);
  background: var(--bg);
  cursor: pointer;
  text-align: left;
  color: var(--text);
  transition: border-color 0.15s ease, transform 0.1s ease, background 0.15s ease;
}
.mode-card:active { transform: scale(0.98); }
.mode-card.active { border-color: var(--accent); background: var(--accent-soft); }
.mode-icon { font-size: 22px; }
.mode-title { font-weight: 700; font-size: 15px; }
.mode-desc { font-size: 12.5px; color: var(--text-muted); line-height: 1.4; }

/* ---------- Direction toggle ---------- */
.direction-toggle { display: flex; gap: 8px; flex-wrap: wrap; }
.dir-btn {
  flex: 1;
  min-width: 120px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
}
.dir-btn.active { border-color: var(--accent); background: var(--accent); color: #fff; }

/* ---------- Buttons ---------- */
.primary-btn {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  border: none;
  padding: 15px 20px;
  border-radius: var(--radius-md);
  font-weight: 700;
  font-size: 16px;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: opacity 0.15s ease, transform 0.1s ease;
}
.primary-btn:active { transform: scale(0.98); }
.primary-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.primary-btn.wide { width: 100%; }
.ghost-btn {
  background: transparent;
  border: 1.5px solid var(--border);
  color: var(--text);
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  cursor: pointer;
}

/* ---------- Session bar ---------- */
.session-bar { display: flex; align-items: center; gap: 12px; }
.progress-track { flex: 1; height: 6px; background: var(--bg-soft); border-radius: 999px; overflow: hidden; }
.progress-fill { height: 100%; width: 0%; background: linear-gradient(90deg, var(--accent), var(--accent-2)); transition: width 0.25s ease; }
.counter { font-size: 13px; color: var(--text-muted); font-weight: 600; white-space: nowrap; }

.score-row { display: flex; gap: 10px; }
.score-pill { font-weight: 700; font-size: 13px; padding: 6px 12px; border-radius: 999px; }
.score-pill.correct { background: var(--correct-soft); color: var(--correct); }
.score-pill.wrong { background: var(--wrong-soft); color: var(--wrong); }

/* ---------- Card stage ---------- */
.card-stage { display: flex; flex-direction: column; align-items: center; gap: 10px; flex: 1; justify-content: center; padding: 10px 0; }
.tap-hint { font-size: 12.5px; color: var(--text-muted); margin: 0; }

.flashcard {
  width: 100%;
  max-width: 420px;
  aspect-ratio: 4 / 3;
  perspective: 1400px;
  cursor: pointer;
}
.flashcard-inner {
  position: relative;
  width: 100%; height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.5s cubic-bezier(.4,.2,.2,1);
}
.flashcard.flipped .flashcard-inner { transform: rotateY(180deg); }
.flashcard-face {
  position: absolute; inset: 0;
  backface-visibility: hidden;
  border-radius: var(--radius-lg);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px;
  padding: 24px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  text-align: center;
}
.flashcard-front { background: linear-gradient(160deg, var(--accent-soft), var(--bg-elevated)); }
.flashcard-back { background: linear-gradient(160deg, var(--bg-elevated), var(--accent-soft)); transform: rotateY(180deg); }

.face-label { font-size: 12px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--text-muted); }
.face-word { font-size: 30px; font-weight: 800; letter-spacing: -0.01em; word-break: break-word; }
.face-alt { font-size: 13px; color: var(--text-muted); font-style: italic; }

.speak-btn {
  border: none;
  background: var(--bg-soft);
  width: 42px; height: 42px;
  border-radius: 50%;
  font-size: 18px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: transform 0.1s ease, background 0.15s ease;
}
.speak-btn:active { transform: scale(0.9); }
.speak-btn.speaking { background: var(--accent); }

/* ---------- Controls ---------- */
.control-row { display: flex; align-items: center; justify-content: center; gap: 14px; }
.round-btn {
  width: 52px; height: 52px;
  border-radius: 50%;
  border: 1.5px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.round-btn.primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; border: none; }
.round-btn.shuffle { font-size: 16px; }
.round-btn:active { transform: scale(0.94); }

.grade-row { gap: 12px; }
.grade-btn {
  flex: 1;
  max-width: 220px;
  padding: 15px 10px;
  border-radius: var(--radius-md);
  border: none;
  font-weight: 700;
  font-size: 15px;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.grade-btn.correct { background: var(--correct); color: #fff; }
.grade-btn.wrong { background: var(--wrong); color: #fff; }
.grade-btn:active { transform: scale(0.97); }

/* ---------- Quiz card ---------- */
.quiz-card {
  width: 100%;
  max-width: 420px;
  min-height: 220px;
  background: linear-gradient(160deg, var(--accent-soft), var(--bg-elevated));
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 28px 24px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px;
  text-align: center;
}
.quiz-prompt-row { display: flex; align-items: center; gap: 12px; }
.quiz-answer { display: flex; flex-direction: column; align-items: center; gap: 8px; width: 100%; margin-top: 6px; }
.quiz-divider { width: 60%; height: 1px; background: var(--border); margin: 6px 0 10px; }

/* ---------- Summary ---------- */
.summary-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 28px 22px;
  text-align: center;
  display: flex; flex-direction: column; gap: 18px;
  margin-top: 20px;
}
.summary-card h2 { margin: 0; }
.summary-stats { display: flex; justify-content: center; gap: 24px; }
.summary-stats .stat { display: flex; flex-direction: column; gap: 4px; }
.summary-stats .stat-num { font-size: 30px; font-weight: 800; }
.summary-stats .stat-label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; }
.summary-card .control-row { justify-content: center; }

@media (max-width: 380px) {
  .mode-grid { grid-template-columns: 1fr; }
  .face-word { font-size: 24px; }
}
