:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f0f3fa;
  --text: #1c2434;
  --text-muted: #5b6678;
  --border: #e1e6f0;
  --primary: #3b6cf6;
  --primary-dark: #2a55d4;
  --primary-soft: #e7eeff;
  --correct: #1f9d5a;
  --correct-soft: #e3f6ec;
  --wrong: #d6453d;
  --wrong-soft: #fbe6e5;
  --ai: #8b5cf6;
  --ai-dark: #7c3aed;
  --ai-soft: #f0e9ff;
  --shadow: 0 1px 2px rgba(20, 30, 60, .06), 0 8px 24px rgba(20, 30, 60, .07);
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f131c;
    --surface: #181f2e;
    --surface-2: #1f2838;
    --text: #e7ecf5;
    --text-muted: #9aa6bd;
    --border: #2a3346;
    --primary: #5b85ff;
    --primary-dark: #4870f0;
    --primary-soft: #1e2740;
    --correct: #34c97b;
    --correct-soft: #14301f;
    --wrong: #ff6b62;
    --wrong-soft: #341a18;
    --ai: #a78bfa;
    --ai-dark: #8b5cf6;
    --ai-soft: #241b3d;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 10px 30px rgba(0,0,0,.35);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: 820px;
  margin: 0 auto;
  padding: 28px 20px 80px;
}

.loading { text-align: center; color: var(--text-muted); padding: 80px 0; }

/* ---------- Header ---------- */
.app-header { margin-bottom: 24px; padding-right: 96px; }
.app-header h1 { font-size: 1.7rem; margin: 0 0 4px; letter-spacing: -.02em; }
.app-header p { margin: 0; color: var(--text-muted); }

.crumb {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: none;
  color: var(--primary);
  font-size: .95rem;
  cursor: pointer;
  padding: 6px 0;
  margin-bottom: 14px;
  font-weight: 600;
}
.crumb:hover { text-decoration: underline; }

/* ---------- Course cards ---------- */
.course-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 16px;
}
.course-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  transition: transform .12s ease, box-shadow .12s ease, border-color .12s ease;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.course-card:hover { transform: translateY(-3px); border-color: var(--primary); }
.course-card h3 { margin: 0; font-size: 1.15rem; letter-spacing: -.01em; }
.course-card .meta { color: var(--text-muted); font-size: .9rem; }
.course-card .best {
  font-size: .8rem;
  font-weight: 600;
  color: var(--correct);
  background: var(--correct-soft);
  align-self: flex-start;
  padding: 3px 9px;
  border-radius: 999px;
}
.course-card .start-hint { color: var(--primary); font-weight: 600; font-size: .9rem; margin-top: auto; }

.resume-card {
  display: flex;
  flex-direction: column;
  gap: 3px;
  width: 100%;
  text-align: left;
  background: var(--primary-soft);
  border: 1px solid var(--primary);
  border-radius: var(--radius);
  padding: 14px 18px;
  margin-bottom: 18px;
  cursor: pointer;
  font: inherit;
  color: var(--text);
}
.resume-card:hover { box-shadow: var(--shadow); }
.resume-label { font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--primary-dark); }
.resume-course { font-size: 1.1rem; font-weight: 700; }
.resume-meta { font-size: .85rem; color: var(--text-muted); }

/* In-progress badge on a course card */
.course-card .best.inprog { color: var(--primary-dark); background: var(--primary-soft); }

/* Continue / Reset block on the setup screen */
.resume-field { text-align: center; }
.resume-field .finish-link { display: inline-block; margin-top: 10px; }
.or-divider {
  text-align: center;
  color: var(--text-muted);
  font-size: .75rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  margin-top: 16px;
}

/* ---------- Setup panel ---------- */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: var(--shadow);
  margin-bottom: 18px;
}
.panel h2 { margin: 0 0 4px; font-size: 1.35rem; }
.panel .sub { color: var(--text-muted); margin: 0 0 18px; }

.field { margin-bottom: 20px; }
.field > label.field-label {
  display: block;
  font-weight: 600;
  margin-bottom: 8px;
  font-size: .95rem;
}
.choice-row { display: flex; flex-wrap: wrap; gap: 10px; }

.choice {
  border: 1.5px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 16px;
  cursor: pointer;
  font-size: .95rem;
  font-weight: 500;
  transition: all .12s ease;
}
.choice:hover:not(:disabled) { border-color: var(--primary); }
.choice[aria-pressed="true"] {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary-dark);
  font-weight: 700;
}
.choice:disabled { opacity: .4; cursor: not-allowed; }

.choice.mode-choice { flex: 1 1 200px; text-align: left; padding: 14px 16px; }
.choice.mode-choice .mode-title { display: block; font-weight: 700; }
.choice.mode-choice .mode-desc { display: block; font-size: .82rem; color: var(--text-muted); font-weight: 400; margin-top: 2px; }
.choice.mode-choice[aria-pressed="true"] .mode-desc { color: var(--primary-dark); }

/* ---------- Buttons ---------- */
.btn {
  border: none;
  border-radius: 10px;
  padding: 12px 22px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .12s ease, transform .08s ease, opacity .12s ease;
  font-family: inherit;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-primary:disabled { background: var(--border); color: var(--text-muted); cursor: not-allowed; }
.btn-ghost { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-ghost:hover { border-color: var(--primary); color: var(--primary); }
.btn-block { width: 100%; }

/* ---------- Quiz ---------- */
.quiz-top { margin-bottom: 18px; }
.quiz-top .row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: .9rem;
  color: var(--text-muted);
  margin-bottom: 8px;
}
.quiz-top .row .course-name { font-weight: 600; color: var(--text); }
.progress { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--primary); transition: width .25s ease; }

.q-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
}
.topic-chip {
  display: inline-block;
  font-size: .72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--primary-dark);
  background: var(--primary-soft);
  padding: 4px 10px;
  border-radius: 999px;
  margin-bottom: 12px;
}
.q-text { font-size: 1.2rem; font-weight: 600; margin: 0 0 18px; letter-spacing: -.01em; }

.options { display: flex; flex-direction: column; gap: 10px; }
.option {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  text-align: left;
  width: 100%;
  background: var(--surface-2);
  border: 1.5px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  cursor: pointer;
  font-size: 1rem;
  color: var(--text);
  font-family: inherit;
  transition: border-color .12s ease, background .12s ease;
}
.option:hover:not(:disabled) { border-color: var(--primary); }
.option:disabled { cursor: default; }
.option .key {
  flex: 0 0 auto;
  width: 26px;
  height: 26px;
  border-radius: 7px;
  background: var(--surface);
  border: 1px solid var(--border);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: .85rem;
  color: var(--text-muted);
}
.option .opt-text { flex: 1; }
.option.selected { border-color: var(--primary); background: var(--primary-soft); }
.option.selected .key { background: var(--primary); color: #fff; border-color: var(--primary); }
.option.correct { border-color: var(--correct); background: var(--correct-soft); }
.option.correct .key { background: var(--correct); color: #fff; border-color: var(--correct); }
.option.wrong { border-color: var(--wrong); background: var(--wrong-soft); }
.option.wrong .key { background: var(--wrong); color: #fff; border-color: var(--wrong); }
.option .mark { margin-left: auto; font-weight: 700; }
.option.correct .mark { color: var(--correct); }
.option.wrong .mark { color: var(--wrong); }

.explain {
  margin-top: 16px;
  padding: 14px 16px;
  border-radius: 12px;
  background: var(--surface-2);
  border-left: 4px solid var(--primary);
  font-size: .95rem;
}
.explain strong { display: block; margin-bottom: 4px; }

.q-nav {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-top: 18px;
}
.q-nav .spacer { flex: 1; }
.finish-link {
  background: none; border: none; color: var(--text-muted);
  cursor: pointer; font-size: .9rem; text-decoration: underline; font-family: inherit;
}
.finish-link:hover { color: var(--primary); }

/* ---------- Results ---------- */
.score-hero {
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px 24px;
  box-shadow: var(--shadow);
  margin-bottom: 18px;
}
.score-ring {
  --pct: 0;
  width: 140px; height: 140px;
  border-radius: 50%;
  margin: 0 auto 16px;
  display: grid;
  place-items: center;
  background:
    radial-gradient(closest-side, var(--surface) 79%, transparent 80% 100%),
    conic-gradient(var(--primary) calc(var(--pct) * 1%), var(--surface-2) 0);
}
.score-ring .pct { font-size: 2rem; font-weight: 800; }
.score-hero h2 { margin: 0 0 4px; font-size: 1.5rem; }
.score-hero .detail { color: var(--text-muted); margin: 0; }

.topic-breakdown { margin: 0 0 18px; }
.topic-breakdown .row {
  display: flex; align-items: center; gap: 12px;
  padding: 9px 0; border-bottom: 1px solid var(--border); font-size: .92rem;
}
.topic-breakdown .row:last-child { border-bottom: none; }
.topic-breakdown .t-name { flex: 1; }
.topic-breakdown .t-bar { flex: 0 0 90px; height: 7px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.topic-breakdown .t-bar > span { display: block; height: 100%; background: var(--correct); }
.topic-breakdown .t-score { flex: 0 0 50px; text-align: right; font-weight: 600; color: var(--text-muted); }

.review-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
  margin-bottom: 12px;
}
.review-item.ok { border-left-color: var(--correct); }
.review-item.bad { border-left-color: var(--wrong); }
.review-item .rq { font-weight: 600; margin: 0 0 8px; }
.review-item .ra { font-size: .92rem; margin: 3px 0; }
.review-item .ra .lbl { color: var(--text-muted); }
.review-item .ra.your.bad-text { color: var(--wrong); }
.review-item .ra.correct-text { color: var(--correct); }
.review-item .rexp { font-size: .9rem; color: var(--text-muted); margin: 8px 0 0; }

.actions { display: flex; flex-wrap: wrap; gap: 12px; margin: 8px 0 26px; }
.actions .btn { flex: 1 1 180px; }

.section-title { font-size: 1.05rem; font-weight: 700; margin: 26px 0 10px; }

.empty { text-align: center; color: var(--text-muted); padding: 40px 0; }

/* ---------- AI add-on ---------- */
.ai-gear {
  position: fixed;
  top: 14px;
  right: 16px;
  z-index: 50;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: .85rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.ai-gear:hover { border-color: var(--ai); color: var(--ai-dark); }
.ai-gear.needs-key { background: var(--ai-soft); color: var(--ai-dark); border-color: var(--ai); }

.ai-panel { margin-top: 16px; border-top: 1px dashed var(--border); padding-top: 14px; }
.ai-bar { display: flex; flex-wrap: wrap; gap: 10px; }
.ai-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--ai-soft);
  color: var(--ai-dark);
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 8px 14px;
  font-size: .9rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: all .12s ease;
}
.ai-btn:hover:not(:disabled) { border-color: var(--ai); }
.ai-btn.active { background: var(--ai); color: #fff; }
.ai-btn:disabled { opacity: .5; cursor: progress; }

.ai-output:not(:empty) { margin-top: 14px; }
.ai-msg {
  background: var(--ai-soft);
  border-left: 4px solid var(--ai);
  border-radius: 12px;
  padding: 14px 16px;
  font-size: .94rem;
}
.ai-msg.ai-error { background: var(--wrong-soft); border-left-color: var(--wrong); color: var(--wrong); }
.ai-msg.ai-rtl { direction: rtl; text-align: right; border-left: none; border-right: 4px solid var(--ai); }
.ai-msg.ai-rtl .ai-line { font-size: 1.02rem; line-height: 1.7; }
.ai-msg.ai-rtl.ai-error { border-right-color: var(--wrong); }
.ai-foot { display: flex; align-items: center; gap: 14px; margin-top: 12px; padding-top: 9px; border-top: 1px solid var(--border); }
.ai-saved { font-size: .8rem; font-weight: 700; color: var(--correct); }
.ai-regen { background: none; border: none; color: var(--ai-dark); font-size: .82rem; font-weight: 600; cursor: pointer; font-family: inherit; padding: 0; }
.ai-regen:hover { text-decoration: underline; }
.ai-chat-host:not(:empty) { margin-top: 12px; }
.ai-text .ai-line { min-height: 1px; }
.ai-text .ai-line + .ai-line { margin-top: 2px; }

.ai-loading { color: var(--ai-dark); animation: ai-pulse 1.1s ease-in-out infinite; }
@keyframes ai-pulse { 0%,100% { opacity: .4; } 50% { opacity: 1; } }

.ai-chat {
  margin-top: 4px;
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  background: var(--surface-2);
}
.ai-thread { max-height: 320px; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.ai-bubble {
  max-width: 88%;
  padding: 9px 13px;
  border-radius: 14px;
  font-size: .92rem;
  white-space: normal;
  word-wrap: break-word;
}
.ai-bubble.user { align-self: flex-end; background: var(--primary); color: #fff; border-bottom-right-radius: 4px; }
.ai-bubble.model { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.ai-bubble.ai-error { background: var(--wrong-soft); color: var(--wrong); border-color: var(--wrong); }

.ai-chat-form { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--border); background: var(--surface); }
.ai-input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: .92rem;
  font-family: inherit;
  background: var(--surface-2);
  color: var(--text);
}
.ai-input:focus { outline: none; border-color: var(--ai); }
.ai-send {
  background: var(--ai); color: #fff; border: none; border-radius: 10px;
  padding: 10px 16px; font-weight: 600; font-family: inherit; cursor: pointer;
}
.ai-send:disabled { opacity: .5; cursor: progress; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(8, 12, 22, .55);
  display: grid; place-items: center;
  padding: 20px;
}
body.modal-open { overflow: hidden; }
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: 100%; max-width: 460px;
  padding: 24px;
}
.modal h2 { margin: 0 0 6px; font-size: 1.25rem; }
.modal .modal-sub { margin: 0 0 12px; color: var(--text-muted); font-size: .9rem; }
.modal .modal-link { display: inline-block; margin-bottom: 16px; color: var(--ai-dark); font-weight: 600; font-size: .9rem; }
.modal .field-label { display: block; font-weight: 600; font-size: .9rem; margin: 12px 0 6px; }
.modal-input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 13px;
  font-size: .95rem;
  font-family: inherit;
  background: var(--surface-2);
  color: var(--text);
}
.modal-input:focus { outline: none; border-color: var(--ai); }
.modal-hint { font-size: .82rem; color: var(--text-muted); margin: 8px 0 0; }
.modal-actions { display: flex; gap: 10px; margin-top: 20px; }
.modal-actions .btn { flex: 1; }

@media (max-width: 540px) {
  #app { padding: 18px 14px 70px; }
  .q-text { font-size: 1.1rem; }
  .actions .btn { flex-basis: 100%; }
  .app-header { padding-right: 0; }
  .ai-gear { top: 10px; right: 10px; padding: 7px 11px; }
}
