:root {
  --bg: #f4f5fb;
  --surface: #ffffff;
  --surface-alt: #eef0fa;
  --text: #1c1d2b;
  --text-muted: #6b6d85;
  --border: #e1e3f0;
  --primary: #5b5fef;
  --primary-hover: #4a4ddb;
  --primary-contrast: #ffffff;
  --success: #22c55e;
  --success-bg: #e8faf0;
  --danger: #ef4444;
  --danger-bg: #fdecec;
  --warning: #f59e0b;
  --mastered: #22c55e;
  --learning: #f59e0b;
  --fresh: #c8cae4;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 2px 10px rgba(30, 30, 60, 0.06);
  --shadow-lg: 0 8px 30px rgba(30, 30, 60, 0.12);
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #14141f;
  --surface: #1d1e2c;
  --surface-alt: #262838;
  --text: #eceefb;
  --text-muted: #9a9cbb;
  --border: #303350;
  --primary: #7b7fff;
  --primary-hover: #9295ff;
  --primary-contrast: #101018;
  --success: #34d399;
  --success-bg: #10301f;
  --danger: #f87171;
  --danger-bg: #3a1414;
  --warning: #fbbf24;
  --mastered: #34d399;
  --learning: #fbbf24;
  --fresh: #3a3c5c;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.45);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14141f;
    --surface: #1d1e2c;
    --surface-alt: #262838;
    --text: #eceefb;
    --text-muted: #9a9cbb;
    --border: #303350;
    --primary: #7b7fff;
    --primary-hover: #9295ff;
    --primary-contrast: #101018;
    --success: #34d399;
    --success-bg: #10301f;
    --danger: #f87171;
    --danger-bg: #3a1414;
    --warning: #fbbf24;
    --mastered: #34d399;
    --learning: #fbbf24;
    --fresh: #3a3c5c;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

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

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

button { font-family: inherit; cursor: pointer; }

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.05rem; }
.brand-icon { font-size: 1.3rem; }

.main-nav { display: flex; gap: 6px; margin-left: 8px; flex: 1; }

.nav-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.9rem;
  padding: 8px 12px;
  border-radius: 999px;
}
.nav-btn.active { background: var(--surface-alt); color: var(--primary); }

.icon-btn {
  border: none;
  background: var(--surface-alt);
  width: 38px;
  height: 38px;
  border-radius: 50%;
  font-size: 1.05rem;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
}

#app-main {
  flex: 1;
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px 48px;
}

.view { display: none; }
.view.active { display: block; }

/* Dashboard */
.stats-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 16px; }
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 8px;
  text-align: center;
  box-shadow: var(--shadow);
}
.stat-value { font-size: 1.6rem; font-weight: 800; color: var(--primary); }
.stat-label { font-size: 0.72rem; color: var(--text-muted); margin-top: 2px; }

.progress-overview { margin-bottom: 20px; }
.progress-bar-track {
  height: 14px;
  border-radius: 999px;
  background: var(--fresh);
  overflow: hidden;
  display: flex;
}
.progress-bar-fill {
  display: flex;
  height: 100%;
  width: 0%;
}
.progress-legend {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  margin-top: 8px;
  font-size: 0.75rem;
  color: var(--text-muted);
}
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; margin-right: 5px; }
.dot-mastered { background: var(--mastered); }
.dot-learning { background: var(--learning); }
.dot-fresh { background: var(--fresh); }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.panel h2 { margin: 0 0 12px; font-size: 1rem; }

.filter-groups { display: flex; flex-direction: column; gap: 10px; }
.filter-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 4px;
}
.filter-section-title {
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--text);
}
.section-toggle {
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text-muted);
  width: 26px;
  height: 26px;
  min-width: 26px;
  border-radius: 50%;
  font-size: 0.9rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.section-toggle.active {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--primary-contrast);
}
.filter-chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
.filter-chip {
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text-muted);
  padding: 6px 11px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
}
.filter-chip.active {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--primary-contrast);
}
.filter-actions { display: flex; gap: 14px; margin-top: 10px; }
.link-btn { background: none; border: none; color: var(--primary); font-size: 0.8rem; font-weight: 600; padding: 0; }

.settings-row { display: flex; flex-direction: column; gap: 14px; }
.settings-label { display: block; font-size: 0.78rem; color: var(--text-muted); margin-bottom: 6px; font-weight: 600; }
.segmented { display: flex; background: var(--surface-alt); border-radius: var(--radius-sm); padding: 3px; gap: 3px; }
.segmented button {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 8px 6px;
  border-radius: 8px;
  font-size: 0.82rem;
  font-weight: 600;
}
.segmented button.active { background: var(--surface); color: var(--primary); box-shadow: var(--shadow); }

.mode-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.mode-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 14px 6px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
}
.mode-card:active { transform: scale(0.97); }
.mode-icon { font-size: 1.6rem; }
.mode-title { font-weight: 700; font-size: 0.82rem; }
.mode-desc { font-size: 0.68rem; color: var(--text-muted); text-align: center; }

.text-btn {
  display: block;
  margin: 0 auto;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.8rem;
  text-decoration: underline;
  padding: 8px;
}

.primary-btn {
  background: var(--primary);
  color: var(--primary-contrast);
  border: none;
  padding: 13px 20px;
  border-radius: var(--radius-sm);
  font-weight: 700;
  font-size: 0.95rem;
  width: 100%;
}
.primary-btn:active { background: var(--primary-hover); }

.secondary-btn {
  background: var(--surface-alt);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 13px 20px;
  border-radius: var(--radius-sm);
  font-weight: 700;
  font-size: 0.95rem;
  width: 100%;
}

/* Session */
.session-header { display: flex; align-items: center; gap: 10px; margin-bottom: 20px; }
.session-progress-track {
  flex: 1;
  height: 8px;
  border-radius: 999px;
  background: var(--surface-alt);
  overflow: hidden;
}
.session-progress-fill { height: 100%; background: var(--primary); width: 0%; transition: width 0.25s ease; }
.session-count { font-size: 0.78rem; color: var(--text-muted); white-space: nowrap; min-width: 50px; text-align: right; }

.session-content { display: flex; flex-direction: column; align-items: stretch; gap: 16px; }

.category-tag {
  align-self: center;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--primary);
  background: var(--surface-alt);
  padding: 4px 10px;
  border-radius: 999px;
}

/* Flashcard */
.flashcard {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  min-height: 220px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 28px 20px;
  cursor: pointer;
  position: relative;
}
.flashcard-word { font-size: 1.55rem; font-weight: 800; line-height: 1.3; }
.flashcard-translation { font-size: 1.15rem; color: var(--primary); margin-top: 10px; font-weight: 700; }
.flashcard-example { font-size: 0.85rem; color: var(--text-muted); margin-top: 14px; font-style: italic; }
.flashcard-note { font-size: 0.78rem; color: var(--text-muted); margin-top: 10px; }
.flashcard-hint { position: absolute; bottom: 10px; right: 14px; font-size: 0.68rem; color: var(--text-muted); }

.flip-actions { display: flex; gap: 10px; }
.flip-actions button {
  flex: 1;
  padding: 14px;
  border-radius: var(--radius-sm);
  border: none;
  font-weight: 700;
  font-size: 0.95rem;
}
.btn-again { background: var(--danger-bg); color: var(--danger); }
.btn-knew { background: var(--success-bg); color: var(--success); }

.tap-hint { text-align: center; color: var(--text-muted); font-size: 0.82rem; }

/* Multiple choice */
.mc-prompt {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 26px 18px;
  text-align: center;
}
.mc-prompt-word { font-size: 1.5rem; font-weight: 800; }
.mc-options { display: flex; flex-direction: column; gap: 10px; }
.mc-option {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  text-align: left;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}
.mc-option.correct { background: var(--success-bg); border-color: var(--success); color: var(--success); }
.mc-option.incorrect { background: var(--danger-bg); border-color: var(--danger); color: var(--danger); }
.mc-option[disabled] { opacity: 0.9; }

/* Typing */
.typing-prompt {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 26px 18px;
  text-align: center;
}
.typing-prompt-word { font-size: 1.5rem; font-weight: 800; }
.typing-input {
  width: 100%;
  padding: 14px 16px;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
}
.typing-input.correct { border-color: var(--success); background: var(--success-bg); }
.typing-input.incorrect { border-color: var(--danger); background: var(--danger-bg); }
.typing-feedback { font-size: 0.85rem; color: var(--text-muted); text-align: center; min-height: 1.2em; }
.typing-feedback.correct { color: var(--success); font-weight: 700; }
.typing-feedback.incorrect { color: var(--danger); font-weight: 700; }

/* Weak words */
.hint { color: var(--text-muted); font-size: 0.85rem; }
.weak-list { display: flex; flex-direction: column; gap: 8px; margin-top: 16px; }
.weak-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: var(--shadow);
}
.weak-item-word { font-weight: 700; }
.weak-item-nl { font-size: 0.8rem; color: var(--text-muted); }
.weak-item-box { font-size: 0.7rem; color: var(--text-muted); background: var(--surface-alt); padding: 3px 8px; border-radius: 999px; }
.empty-state { text-align: center; color: var(--text-muted); padding: 30px 10px; font-size: 0.9rem; }

/* Summary */
#view-summary { display: none; }
#view-summary.active { display: flex; align-items: center; justify-content: center; min-height: 60vh; }
.summary-card { text-align: center; max-width: 320px; margin: 0 auto; }
.summary-emoji { font-size: 3rem; }
.summary-score { color: var(--text-muted); margin-bottom: 20px; }
.summary-actions { display: flex; flex-direction: column; gap: 10px; }

@media (max-width: 420px) {
  .mode-grid { grid-template-columns: repeat(3, 1fr); }
  .mode-desc { display: none; }
  .brand-name { display: none; }
}
