:root {
  --purple: #7c3aed;
  --purple-dark: #5b21b6;
  --blue: #4f6bff;
  --bg: #f5f6fb;
  --card: #ffffff;
  --text: #1f2333;
  --muted: #6b7280;
  --green: #22c55e;
  --green-bg: #dcfce7;
  --green-text: #15803d;
  --blue2: #3b82f6;
  --blue-bg: #dbeafe;
  --blue-text: #1d4ed8;
  --orange: #f97316;
  --orange-bg: #ffedd5;
  --orange-text: #c2410c;
  --pink: #ec4899;
  --pink-bg: #fce7f3;
  --pink-text: #be185d;
  --violet: #a855f7;
  --violet-bg: #f3e8ff;
  --violet-text: #7e22ce;
  --indigo: #8b5cf6;
  --indigo-bg: #ede9fe;
  --indigo-text: #6d28d9;
  --red: #ef4444;
  --red-bg: #fee2e2;
  --radius: 18px;
  --shadow: 0 6px 20px rgba(31, 35, 51, 0.08);
}

* { box-sizing: border-box; }

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

#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  position: relative;
  padding-bottom: 78px;
}

.screen { padding: 20px 20px 8px; animation: fadein .18s ease; }
@keyframes fadein { from { opacity: 0; transform: translateY(6px);} to { opacity: 1; transform: translateY(0);} }

/* Header bars */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 4px 0 12px;
}
.topbar h1 { font-size: 22px; margin: 0; }
.topbar .subtitle { font-size: 13px; color: var(--muted); margin-top: 2px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: none;
  background: var(--card); box-shadow: var(--shadow);
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; cursor: pointer; color: var(--text);
}
.back-row { display: flex; align-items: center; gap: 12px; padding: 4px 0 16px; }
.back-row h2 { font-size: 19px; margin: 0; }

/* Cards */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }

.reward-card { margin-bottom: 18px; }
.reward-card h3 { margin: 0 0 2px; font-size: 16px; }
.reward-card .hint { font-size: 12px; color: var(--muted); margin-bottom: 12px; }

table.reward-table { width: 100%; border-collapse: collapse; }
table.reward-table th { text-align: left; font-size: 11px; color: var(--muted); font-weight: 600; padding-bottom: 6px; }
table.reward-table td { padding: 10px 6px; font-size: 14px; border-top: 1px solid #f0f0f5; }
.time-note { margin-top: 12px; font-size: 12px; color: var(--muted); font-weight: 600; }
.reward-badge {
  display: inline-block; padding: 4px 10px; border-radius: 999px; font-weight: 700; font-size: 13px;
}

.btn-primary {
  width: 100%; padding: 15px; border: none; border-radius: 14px;
  background: linear-gradient(135deg, var(--purple), var(--blue));
  color: #fff; font-size: 16px; font-weight: 600; cursor: pointer;
  box-shadow: 0 8px 18px rgba(124,58,237,0.28);
}
.btn-primary:disabled { opacity: .5; cursor: not-allowed; box-shadow: none; }
.btn-secondary {
  width: 100%; padding: 14px; border-radius: 14px; border: 1.5px solid #e2e2ee;
  background: #fff; color: var(--text); font-size: 15px; font-weight: 600; cursor: pointer;
}

/* Bottom nav */
.bottom-nav {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: 0; width: 100%; max-width: 480px; background: #fff;
  display: flex; justify-content: space-around; padding: 10px 0 max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid #eee; z-index: 20;
}
.nav-item { display: flex; flex-direction: column; align-items: center; gap: 3px; color: var(--muted); background: none; border: none; font-size: 11px; cursor: pointer; }
.nav-item .nav-icon { font-size: 20px; }
.nav-item.active { color: var(--purple); font-weight: 700; }

/* Challenge confirm screen */
.hero-banner {
  background: linear-gradient(135deg, #fde3c1, #fbd0d0);
  border-radius: 22px; padding: 26px 20px; text-align: center; margin: 6px 0 22px;
}
.hero-banner .amount { font-size: 34px; font-weight: 800; color: #b45309; }
.hero-banner .amount .rm { font-size: 20px; vertical-align: super; }
.hero-banner .tag { font-size: 15px; font-weight: 700; color: #92400e; margin-top: 2px; }
.stat-list { display: flex; flex-direction: column; gap: 14px; margin-bottom: 22px; }
.stat-item { display: flex; align-items: center; gap: 14px; background: var(--card); border-radius: 14px; padding: 14px; box-shadow: var(--shadow); }
.stat-item .emoji { font-size: 22px; }
.stat-item .label { font-size: 13px; color: var(--muted); }
.stat-item .value { font-size: 15px; font-weight: 700; }

/* Subject grid */
.subject-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 6px; }
.subject-card {
  background: var(--card); border-radius: 16px; box-shadow: var(--shadow);
  padding: 20px 10px; text-align: center; cursor: pointer; border: 2px solid transparent;
}
.subject-card .emoji { font-size: 30px; margin-bottom: 8px; }
.subject-card .name { font-size: 14px; font-weight: 600; }
.subject-card.selected { border-color: var(--purple); }

/* Form / topic select */
.pill-row { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 20px; }
.pill {
  padding: 10px 16px; border-radius: 12px; background: var(--card); box-shadow: var(--shadow);
  font-size: 14px; font-weight: 600; cursor: pointer; border: 2px solid transparent; color: var(--text);
}
.pill.active { background: var(--purple); color: #fff; }
.section-label { font-size: 13px; font-weight: 700; color: var(--muted); margin: 4px 0 10px; text-transform: uppercase; letter-spacing: .03em; }
.topic-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 24px; }
.topic-row {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--card); border-radius: 14px; padding: 14px 16px; box-shadow: var(--shadow);
  cursor: pointer; border: 2px solid transparent; font-size: 14px; font-weight: 600;
}
.topic-row.selected { border-color: var(--purple); color: var(--purple); }
.topic-row .chev { color: var(--muted); }
.topic-row.locked { cursor: not-allowed; opacity: .55; }
.attempts-badge { font-size: 10px; font-weight: 700; color: var(--muted); background: #eceaf6; padding: 3px 8px; border-radius: 999px; white-space: nowrap; }
.attempts-badge.locked { color: var(--red); background: var(--red-bg); }
[data-theme="dark"] .attempts-badge { background: #2c2843; }
.attempts-note { text-align: center; font-size: 12px; color: var(--muted); font-weight: 600; margin: -8px 0 18px; }

/* Quiz screen */
.quiz-topbar { display: flex; justify-content: space-between; align-items: center; padding: 4px 0 14px; }
.quiz-topbar .x-btn { font-size: 22px; background: none; border: none; cursor: pointer; color: var(--muted); }
.quiz-topbar .title { font-weight: 700; font-size: 15px; }
.quiz-timer-badge {
  display: flex; align-items: center; gap: 3px; background: #eceaf6; color: var(--purple);
  font-weight: 700; font-size: 13px; padding: 6px 12px; border-radius: 999px; transition: background .2s, color .2s;
}
.quiz-timer-badge.urgent { background: var(--red-bg); color: var(--red); animation: pulse-timer 1s infinite; }
@keyframes pulse-timer { 0%, 100% { opacity: 1; } 50% { opacity: .55; } }
.progress-info { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.progress-info .qcount { font-weight: 700; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.progress-info .remaining { font-size: 12px; color: var(--muted); }
.diff-badge {
  font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em;
  padding: 2px 8px; border-radius: 999px;
}
.diff-badge.diff-easy { background: var(--green-bg); color: var(--green-text); }
.diff-badge.diff-medium { background: var(--orange-bg); color: var(--orange-text); }
.diff-badge.diff-hard { background: var(--red-bg); color: var(--red); }
.progress-bar { height: 8px; background: #eceaf6; border-radius: 6px; overflow: hidden; margin-bottom: 22px; }
.progress-bar .fill { height: 100%; background: linear-gradient(90deg, var(--purple), var(--blue)); border-radius: 6px; transition: width .25s ease; }

.question-card { background: var(--card); border-radius: 18px; box-shadow: var(--shadow); padding: 22px 18px; margin-bottom: 20px; font-size: 17px; font-weight: 600; line-height: 1.5; min-height: 90px; }
.option-list { display: flex; flex-direction: column; gap: 12px; margin-bottom: 24px; }
.option-row {
  display: flex; align-items: center; gap: 12px; background: var(--card); border-radius: 14px;
  padding: 14px 16px; box-shadow: var(--shadow); cursor: pointer; border: 2px solid transparent; font-size: 15px;
}
.option-row .letter {
  width: 30px; height: 30px; border-radius: 50%; background: #eceaf6; color: var(--purple);
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 13px; flex-shrink: 0;
}
.option-row.chosen { border-color: var(--purple); background: #f4f2ff; }
.option-row.chosen .letter { background: var(--purple); color: #fff; }
.option-row.correct { border-color: var(--green); background: var(--green-bg); }
.option-row.correct .letter { background: var(--green); color: #fff; }
.option-row.wrong { border-color: var(--red); background: var(--red-bg); }
.option-row.wrong .letter { background: var(--red); color: #fff; }
.option-row.disabled { pointer-events: none; }

/* Feedback screens */
.feedback-screen { text-align: center; padding-top: 40px; }
.feedback-icon { width: 88px; height: 88px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 44px; margin: 0 auto 18px; }
.feedback-icon.ok { background: var(--green-bg); color: var(--green); }
.feedback-icon.bad { background: var(--red-bg); color: var(--red); }
.feedback-title { font-size: 24px; font-weight: 800; margin-bottom: 8px; }
.feedback-title.ok { color: #15803d; }
.feedback-title.bad { color: #b91c1c; }
.feedback-sub { background: var(--green-bg); color: var(--green-text); border-radius: 12px; padding: 10px; font-size: 14px; font-weight: 600; margin: 0 auto 26px; max-width: 260px; }
.explain-card { background: var(--card); border-radius: 14px; box-shadow: var(--shadow); padding: 16px; text-align: left; margin-bottom: 26px; }
.explain-card h4 { margin: 0 0 8px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.explain-card p { margin: 0; font-size: 14px; line-height: 1.5; white-space: pre-line; }

/* Result screens */
.result-screen { text-align: center; padding-top: 30px; }
.confetti { font-size: 44px; margin-bottom: 6px; }
.trophy-badge { width: 100px; height: 100px; border-radius: 50%; background: linear-gradient(135deg,#fde68a,#fbbf24); display:flex; align-items:center; justify-content:center; font-size: 48px; margin: 0 auto 18px; box-shadow: 0 10px 24px rgba(251,191,36,.4); }
.result-title { font-size: 24px; font-weight: 800; margin-bottom: 4px; }
.result-sub { font-size: 14px; color: var(--muted); margin-bottom: 24px; }
.result-stats { background: var(--card); border-radius: 16px; box-shadow: var(--shadow); padding: 18px; margin-bottom: 20px; text-align: left; }
.result-stats .row { display: flex; justify-content: space-between; padding: 8px 0; font-size: 14px; }
.result-stats .row b { font-size: 16px; }
.result-stats .row.pass b { color: var(--green-text); }
.result-stats .row.fail b { color: #b91c1c; }
.reward-banner { background: var(--green-bg); color: var(--green-text); border-radius: 14px; padding: 14px; font-weight: 700; margin-bottom: 22px; display: flex; align-items: center; justify-content: center; gap: 8px; }
.sad-icon { width: 88px; height: 88px; border-radius: 50%; background: var(--red-bg); color: var(--red); display:flex; align-items:center; justify-content:center; font-size: 42px; margin: 0 auto 18px; }
.fail-banner { background: var(--red-bg); color: #b91c1c; border-radius: 14px; padding: 14px; font-weight: 600; margin-bottom: 22px; font-size: 14px; }

/* Wallet */
.wallet-hero { background: linear-gradient(135deg, var(--purple), var(--blue)); border-radius: 20px; padding: 24px 20px; color: #fff; margin-bottom: 20px; display: flex; align-items: center; gap: 16px; }
.wallet-hero .wicon { width: 52px; height: 52px; border-radius: 14px; background: rgba(255,255,255,.2); display:flex; align-items:center; justify-content:center; font-size: 26px; }
.wallet-hero .wlabel { font-size: 13px; opacity: .85; }
.wallet-hero .wvalue { font-size: 28px; font-weight: 800; }
.wallet-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 22px; }
.wallet-tile { background: var(--card); border-radius: 14px; box-shadow: var(--shadow); padding: 14px; }
.wallet-tile .wt-label { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.wallet-tile .wt-value { font-size: 20px; font-weight: 800; }
.history-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 20px; }
.history-item { background: var(--card); border-radius: 14px; box-shadow: var(--shadow); padding: 12px 14px; display: flex; justify-content: space-between; align-items: center; font-size: 13px; }
.history-item .h-main { font-weight: 700; font-size: 14px; }
.history-item .h-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.history-item .h-score.pass { color: var(--green-text); font-weight: 700; }
.history-item .h-score.fail { color: #b91c1c; font-weight: 700; }
.empty-state { text-align: center; color: var(--muted); font-size: 14px; padding: 40px 10px; }

/* Stats */
.stats-bar-row { margin-bottom: 14px; }
.stats-bar-row .sb-top { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 6px; }
.stats-bar-row .sb-top .sname { font-weight: 700; }
.stats-bar-track { height: 10px; background: #eceaf6; border-radius: 6px; overflow: hidden; }
.stats-bar-fill { height: 100%; background: linear-gradient(90deg, var(--purple), var(--blue)); border-radius: 6px; }

/* Settings */
.settings-item { display: flex; align-items: center; justify-content: space-between; background: var(--card); border-radius: 14px; box-shadow: var(--shadow); padding: 14px 16px; margin-bottom: 12px; font-size: 14px; font-weight: 600; }
.lang-toggle { display: flex; background: #eceaf6; border-radius: 999px; padding: 3px; gap: 2px; }
.lang-btn { border: none; background: none; padding: 6px 12px; border-radius: 999px; font-size: 12px; font-weight: 700; color: var(--muted); cursor: pointer; }
.lang-btn.active { background: var(--purple); color: #fff; }
[data-theme="dark"] .lang-toggle { background: #2c2843; }
.switch { position: relative; width: 46px; height: 26px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider-toggle { position: absolute; cursor: pointer; inset: 0; background: #d9d9e6; border-radius: 999px; transition: .2s; }
.slider-toggle:before { content: ""; position: absolute; height: 20px; width: 20px; left: 3px; top: 3px; background: white; border-radius: 50%; transition: .2s; }
.switch input:checked + .slider-toggle { background: var(--purple); }
.switch input:checked + .slider-toggle:before { transform: translateX(20px); }
.about-text { font-size: 12px; color: var(--muted); line-height: 1.6; margin-top: 18px; padding: 0 4px; }
.danger-btn { color: #b91c1c; }

/* Dark mode */
[data-theme="dark"] {
  --bg: #14121f; --card: #1f1c30; --text: #f1f0f8; --muted: #9a94b3;
}
[data-theme="dark"] table.reward-table td { border-top-color: #2c2843; }
[data-theme="dark"] .bottom-nav { background: #1f1c30; border-top-color: #2c2843; }
[data-theme="dark"] .icon-btn { background: #1f1c30; }
[data-theme="dark"] .btn-secondary { background: #1f1c30; border-color: #2c2843; color: var(--text); }
[data-theme="dark"] .stats-bar-track, [data-theme="dark"] .progress-bar { background: #2c2843; }
[data-theme="dark"] .pill { background: #1f1c30; }
[data-theme="dark"] .quiz-timer-badge { background: #2c2843; }

.toast {
  position: fixed; bottom: 92px; left: 50%; transform: translateX(-50%);
  background: #1f2333; color: #fff; padding: 10px 18px; border-radius: 999px; font-size: 13px;
  box-shadow: 0 8px 20px rgba(0,0,0,.25); z-index: 50; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
  max-width: 90%; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }
