/* ── Reset & Base ──────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg:         #f0f4f8;
  --surface:    #ffffff;
  --surface2:   #e8edf3;
  --primary:    #1565c0;
  --primary-lt: #1976d2;
  --primary-txt:#ffffff;
  --text:       #1a1a2e;
  --text2:      #4a5568;
  --text3:      #718096;
  --border:     #d1d9e0;
  --success:    #2e7d32;
  --success-bg: #e8f5e9;
  --warn:       #e65100;
  --warn-bg:    #fff3e0;
  --danger:     #c62828;
  --danger-bg:  #ffebee;
  --shadow:     0 1px 4px rgba(0,0,0,.12);
  --radius:     12px;
  --radius-sm:  8px;
  --nav-h:      64px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg:         #0d1117;
    --surface:    #161b22;
    --surface2:   #21262d;
    --primary:    #58a6ff;
    --primary-lt: #79b8ff;
    --primary-txt:#0d1117;
    --text:       #e6edf3;
    --text2:      #8b949e;
    --text3:      #6e7681;
    --border:     #30363d;
    --success:    #56d364;
    --success-bg: #1a3a1a;
    --warn:       #ffa657;
    --warn-bg:    #3a2a00;
    --danger:     #f85149;
    --danger-bg:  #3a1a1a;
    --shadow:     0 1px 4px rgba(0,0,0,.4);
  }
}

html { font-size: 18px; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
}
#app { padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); }

/* ── Screen-reader only ────────────────────────────────────────────────────── */
.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

/* ── Bottom Nav ────────────────────────────────────────────────────────────── */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 100;
}
.nav-item {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 3px;
  color: var(--text3); text-decoration: none;
  font-size: .72rem; font-weight: 500; letter-spacing: .02em;
  min-height: 44px; padding: 6px 4px;
  transition: color .15s;
}
.nav-item.active { color: var(--primary); }
.nav-item svg { width: 22px; height: 22px; fill: currentColor; }

/* ── Page layout ───────────────────────────────────────────────────────────── */
.page {
  max-width: 480px; margin: 0 auto;
  padding: 16px 16px 24px;
  display: flex; flex-direction: column; gap: 16px;
}
.page-header {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 0 4px;
}
.page-title {
  font-size: 1.3rem; font-weight: 700; flex: 1;
}
.back-btn {
  color: var(--primary); font-size: 1.4rem;
  text-decoration: none; padding: 4px 8px 4px 0;
  min-width: 44px; min-height: 44px;
  display: flex; align-items: center;
}

/* ── Card ──────────────────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.section-title {
  font-size: 1rem; font-weight: 600;
  color: var(--text); margin-bottom: 10px;
}
.section-sub { font-size: .9rem; color: var(--text2); margin-bottom: 10px; }

/* ── Buttons ───────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 12px 20px; border-radius: var(--radius-sm);
  font-size: 1rem; font-weight: 600; text-decoration: none;
  border: none; cursor: pointer; min-height: 48px;
  transition: opacity .15s, background .15s;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { opacity: .85; }
.btn-primary { background: var(--primary); color: var(--primary-txt); }
.btn-secondary {
  background: var(--surface2); color: var(--text);
  border: 1px solid var(--border);
}
.btn-ghost { background: transparent; color: var(--primary); }
.btn-full { width: 100%; }
.btn-xl { font-size: 1.1rem; padding: 16px 20px; min-height: 56px; }
.btn-sm { font-size: .85rem; padding: 8px 14px; min-height: 36px; }

/* ── Progress bar ──────────────────────────────────────────────────────────── */
.progress-bar {
  height: 6px; background: var(--surface2);
  border-radius: 3px; overflow: hidden;
}
.progress-fill {
  height: 100%; background: var(--primary);
  border-radius: 3px; transition: width .3s ease;
  min-width: 0;
}

/* ── Input / Form ──────────────────────────────────────────────────────────── */
.input {
  width: 100%; padding: 14px 16px;
  background: var(--surface2); color: var(--text);
  border: 1.5px solid var(--border); border-radius: var(--radius-sm);
  font-size: 1rem; appearance: none;
}
.input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.input-error { border-color: var(--danger); }
.error-msg { color: var(--danger); font-size: .9rem; }
.textarea {
  width: 100%; padding: 12px 16px;
  background: var(--surface2); color: var(--text);
  border: 1.5px solid var(--border); border-radius: var(--radius-sm);
  font-size: 1rem; font-family: inherit; resize: vertical;
}
.textarea:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.select-input {
  width: 100%; padding: 12px 16px;
  background: var(--surface2); color: var(--text);
  border: 1.5px solid var(--border); border-radius: var(--radius-sm);
  font-size: 1rem; appearance: none;
}

/* ── Login ─────────────────────────────────────────────────────────────────── */
.login-wrap {
  min-height: 100dvh; display: flex;
  align-items: center; justify-content: center; padding: 24px;
}
.login-card {
  width: 100%; max-width: 360px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 32px 24px;
  box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 16px;
}
.login-logo { font-size: 2.5rem; text-align: center; }
.login-title { font-size: 1.6rem; font-weight: 800; text-align: center; }
.login-sub { color: var(--text2); text-align: center; margin-top: -8px; }
.login-form { display: flex; flex-direction: column; gap: 12px; }

/* ── Home ──────────────────────────────────────────────────────────────────── */
.streak-badge {
  font-size: .9rem; font-weight: 600;
  background: var(--warn-bg); color: var(--warn);
  padding: 4px 10px; border-radius: 20px;
  white-space: nowrap;
}
.goal-card { display: flex; flex-direction: column; gap: 8px; }
.goal-header { display: flex; justify-content: space-between; align-items: center; }
.goal-label { font-weight: 600; }
.goal-count { color: var(--text2); font-size: .9rem; }
.goal-done { color: var(--success); font-size: .9rem; font-weight: 600; }
.cta-section { display: flex; flex-direction: column; gap: 10px; }
.stat-row { display: flex; justify-content: space-between; align-items: center; }
.stat-label { font-weight: 500; color: var(--text2); }
.stat-value { font-weight: 700; font-size: 1.1rem; }
.text-success { color: var(--success); }
.text-warn { color: var(--warn); }

/* Category grid on home */
.cat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.cat-tile {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 12px;
  text-decoration: none; color: var(--text);
  display: flex; flex-direction: column; gap: 6px;
  transition: border-color .15s;
}
.cat-tile:active { border-color: var(--primary); }
.cat-name { font-size: .85rem; font-weight: 600; line-height: 1.3; }
.cat-due { font-size: .8rem; color: var(--text3); }
.cat-bar { height: 4px; background: var(--surface2); border-radius: 2px; overflow: hidden; }
.cat-fill { height: 100%; border-radius: 2px; transition: width .3s; }

/* Heat levels */
.heat-0 .cat-fill { background: var(--surface2); }
.heat-1 .cat-fill { background: var(--danger); }
.heat-2 .cat-fill { background: var(--warn); }
.heat-3 .cat-fill { background: #fdd835; }
.heat-4 .cat-fill { background: var(--success); }

.footer-links {
  display: flex; justify-content: space-between; align-items: center;
  padding: 4px 0;
}
.link-muted { color: var(--text3); font-size: .9rem; text-decoration: none; }
.footer-stat { color: var(--text3); font-size: .9rem; }
.counter {
  font-size: .9rem; font-weight: 600; color: var(--text2);
  white-space: nowrap;
}

/* ── Study / Flashcard ─────────────────────────────────────────────────────── */
.cat-select-row { margin-bottom: -4px; }
.card-area { perspective: 1200px; }
.flashcard {
  width: 100%; min-height: 260px;
  cursor: pointer; user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.card-inner {
  position: relative; width: 100%; min-height: 260px;
  transform-style: preserve-3d;
  transition: transform .45s cubic-bezier(.4,0,.2,1);
}
.card-inner.flipped { transform: rotateY(180deg); }
.card-face {
  position: absolute; top: 0; left: 0; right: 0;
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px;
  min-height: 260px;
  display: flex; flex-direction: column; gap: 12px;
  box-shadow: var(--shadow);
}
.card-back { transform: rotateY(180deg); }
.card-category {
  font-size: .78rem; font-weight: 700; letter-spacing: .06em;
  text-transform: uppercase; color: var(--primary); opacity: .9;
}
.card-text { font-size: 1.05rem; line-height: 1.55; flex: 1; }
.card-mnemonic {
  font-size: .9rem; color: var(--text2);
  background: var(--surface2); border-radius: var(--radius-sm);
  padding: 8px 12px; margin-top: 4px;
}
.flip-hint {
  font-size: .85rem; color: var(--text3);
  text-align: center; margin-top: auto; padding-top: 12px;
}

.rating-row {
  display: grid; grid-template-columns: 1fr 1fr 1fr 1fr;
  gap: 8px;
}
.btn-rate {
  padding: 14px 4px; border: none; border-radius: var(--radius-sm);
  font-size: .9rem; font-weight: 700; cursor: pointer;
  min-height: 52px;
  -webkit-tap-highlight-color: transparent;
  transition: opacity .15s;
}
.btn-rate:active { opacity: .8; }
.btn-again  { background: var(--danger-bg);  color: var(--danger); }
.btn-hard   { background: var(--warn-bg);    color: var(--warn); }
.btn-good   { background: var(--success-bg); color: var(--success); }
.btn-easy   { background: #e3f2fd; color: #1565c0; }
@media (prefers-color-scheme: dark) {
  .btn-easy { background: #0d2a4a; color: #58a6ff; }
}

.empty-state {
  text-align: center; padding: 40px 16px;
  display: flex; flex-direction: column; gap: 14px; align-items: center;
}
.empty-icon { font-size: 3rem; }
.empty-state h2 { font-size: 1.3rem; }
.empty-state p { color: var(--text2); }

/* ── Quiz Setup ────────────────────────────────────────────────────────────── */
.quiz-setup-form { display: flex; flex-direction: column; gap: 16px; }
.form-section-title { font-size: .95rem; font-weight: 600; margin-bottom: 12px; }
.form-hint { font-weight: 400; color: var(--text3); font-size: .85rem; }
.count-options { display: flex; gap: 10px; }
.count-option { flex: 1; }
.count-option input { display: none; }
.count-option span {
  display: flex; align-items: center; justify-content: center;
  height: 48px; border-radius: var(--radius-sm);
  border: 2px solid var(--border);
  font-size: 1rem; font-weight: 600; cursor: pointer;
  background: var(--surface2); color: var(--text);
  transition: border-color .15s, background .15s;
}
.count-option input:checked + span {
  border-color: var(--primary); background: var(--primary);
  color: var(--primary-txt);
}
.cat-checks { display: flex; flex-direction: column; gap: 4px; }
.cat-check-label {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 4px; cursor: pointer;
  min-height: 44px;
}
.cat-checkbox { width: 20px; height: 20px; accent-color: var(--primary); flex-shrink: 0; }
.cat-locked { opacity: .45; }
.cat-lock-hint { font-size: .75rem; color: var(--text3); margin-left: auto; }
.toggle-label {
  display: flex; align-items: center; gap: 12px; cursor: pointer;
  min-height: 44px;
}
.toggle-text { flex: 1; font-weight: 500; }
.toggle-label input { display: none; }
.toggle-switch {
  width: 48px; height: 28px; border-radius: 14px;
  background: var(--border); position: relative;
  transition: background .2s; flex-shrink: 0;
}
.toggle-switch::after {
  content: ''; position: absolute;
  top: 3px; left: 3px; width: 22px; height: 22px;
  border-radius: 50%; background: white;
  transition: transform .2s; box-shadow: 0 1px 3px rgba(0,0,0,.3);
}
.toggle-label input:checked + .toggle-switch { background: var(--primary); }
.toggle-label input:checked + .toggle-switch::after { transform: translateX(20px); }

/* ── Quiz Question ─────────────────────────────────────────────────────────── */
.quiz-progress { margin-top: -8px; }
.quiz-q-card { margin-top: 4px; }
.quiz-category {
  font-size: .78rem; font-weight: 700; letter-spacing: .06em;
  text-transform: uppercase; color: var(--primary); margin-bottom: 8px;
}
.quiz-question { font-size: 1.05rem; line-height: 1.55; }
.mcq-options { display: flex; flex-direction: column; gap: 10px; margin-top: 4px; }
.mcq-option {
  width: 100%; text-align: left; padding: 14px 16px;
  background: var(--surface); border: 1.5px solid var(--border);
  border-radius: var(--radius-sm); font-size: 1rem;
  color: var(--text); cursor: pointer; min-height: 52px;
  transition: border-color .15s, background .15s;
  -webkit-tap-highlight-color: transparent;
}
.mcq-option:active:not(:disabled) { border-color: var(--primary); }
.mcq-option:disabled { cursor: default; }
.option-correct { border-color: var(--success) !important; background: var(--success-bg) !important; color: var(--success) !important; font-weight: 600; }
.option-wrong   { border-color: var(--danger)  !important; background: var(--danger-bg)  !important; color: var(--danger)  !important; }
.free-response { display: flex; flex-direction: column; gap: 10px; margin-top: 4px; }
.timer {
  font-size: .9rem; font-weight: 700; color: var(--text2);
  background: var(--surface2); padding: 4px 10px;
  border-radius: 20px; white-space: nowrap;
}
.timer-warn { color: var(--danger); background: var(--danger-bg); }
.feedback-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
  display: flex; flex-direction: column; gap: 10px;
  margin-top: 4px;
}
.feedback-result { font-size: 1.1rem; font-weight: 700; }
.result-correct { color: var(--success); }
.result-wrong   { color: var(--danger); }
.feedback-answer { font-size: .95rem; font-weight: 600; color: var(--text); }
.feedback-explain { font-size: .9rem; color: var(--text2); line-height: 1.5; }

/* ── Quiz Results ──────────────────────────────────────────────────────────── */
.score-card {
  text-align: center; padding: 28px 16px;
  display: flex; flex-direction: column; gap: 6px; align-items: center;
}
.score-pass { border-color: var(--success); background: var(--success-bg); }
.score-fail { border-color: var(--danger);  background: var(--danger-bg); }
.score-pct { font-size: 3rem; font-weight: 800; }
.score-pass .score-pct { color: var(--success); }
.score-fail .score-pct { color: var(--danger); }
.score-fraction { font-size: 1.1rem; font-weight: 600; color: var(--text2); }
.score-label { font-size: .9rem; color: var(--text3); }
.weak-cats .weak-cat-list { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 4px; }
.review-item {
  display: flex; gap: 12px; padding: 14px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm); margin-bottom: 8px;
}
.review-correct { border-left: 3px solid var(--success); }
.review-wrong   { border-left: 3px solid var(--danger); }
.review-indicator { font-size: 1.1rem; font-weight: 700; padding-top: 1px; flex-shrink: 0; }
.review-correct .review-indicator { color: var(--success); }
.review-wrong   .review-indicator { color: var(--danger); }
.review-content { display: flex; flex-direction: column; gap: 5px; flex: 1; }
.review-cat { font-size: .75rem; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--primary); }
.review-q { font-size: .9rem; font-weight: 500; line-height: 1.45; }
.review-user-ans { font-size: .85rem; color: var(--danger); }
.review-correct-ans { font-size: .85rem; color: var(--success); }
.review-explain { font-size: .85rem; color: var(--text2); line-height: 1.45; }
.result-actions { display: flex; flex-direction: column; gap: 10px; padding-top: 8px; }

/* ── Stats ─────────────────────────────────────────────────────────────────── */
.stats-overview { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat-tile {
  text-align: center; padding: 16px 8px;
  display: flex; flex-direction: column; gap: 4px; align-items: center;
}
.stat-num { font-size: 1.8rem; font-weight: 800; color: var(--primary); }
.stat-lbl { font-size: .82rem; color: var(--text3); font-weight: 500; }

.activity-grid {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  gap: 5px; margin-bottom: 8px;
}
.activity-cell {
  aspect-ratio: 1; border-radius: 3px;
  background: var(--surface2);
}
.activity-cell.level-0 { background: var(--surface2); }
.activity-cell.level-1 { background: #90caf9; }
.activity-cell.level-2 { background: #42a5f5; }
.activity-cell.level-3 { background: #1565c0; }
@media (prefers-color-scheme: dark) {
  .activity-cell.level-1 { background: #1a3a5c; }
  .activity-cell.level-2 { background: #1d4f8a; }
  .activity-cell.level-3 { background: #58a6ff; }
}
.activity-legend {
  display: flex; align-items: center; gap: 5px;
  font-size: .78rem; color: var(--text3);
}
.activity-legend .activity-cell {
  width: 16px; height: 16px; flex-shrink: 0;
}

.cat-stat-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.cat-stat-row:last-child { border-bottom: none; }
.cat-stat-name {
  width: 130px; font-size: .82rem; font-weight: 500;
  color: var(--text); text-decoration: none; flex-shrink: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.cat-stat-name:hover { color: var(--primary); }
.cat-stat-bar-wrap { flex: 1; }
.cat-stat-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; flex-shrink: 0; }
.cat-stat-pct { font-size: .82rem; font-weight: 600; color: var(--text2); width: 36px; text-align: right; }
.cat-stat-due { font-size: .72rem; color: var(--warn); font-weight: 600; }

.quiz-hist-list { display: flex; flex-direction: column; gap: 6px; }
.quiz-hist-row {
  display: flex; gap: 12px; align-items: center;
  padding: 10px 14px; border-radius: var(--radius-sm);
  background: var(--surface2);
}
.hist-pass .hist-pct { color: var(--success); font-weight: 700; }
.hist-fail .hist-pct { color: var(--danger);  font-weight: 700; }
.hist-date { font-size: .85rem; color: var(--text3); flex: 1; }
.hist-score { font-size: .9rem; font-weight: 500; }
.hist-pct { width: 44px; text-align: right; font-size: .9rem; }
