:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --ink: #1f2430;
  --muted: #6b7280;
  --line: #e6e2d8;
  --brand: #2f6f4f;
  --brand-ink: #ffffff;
  --brand-soft: #e3f0e8;
  --accent: #e8a33d;
  --good: #2f8a4f;
  --mid: #d08a1c;
  --bad: #c2453a;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 20, 20, .04), 0 4px 16px rgba(20, 20, 20, .05);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; }
button, input, select { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 24px; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand .logo { font-size: 28px; }
.brand strong { display: block; font-size: 18px; letter-spacing: .2px; }
.brand small { color: var(--muted); }
.topbar-right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.learner-select { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; }
.learner-select select { min-width: 160px; }

.lang-toggle, .segmented { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; padding: 3px; background: var(--bg); }
.lang-toggle button, .segmented button {
  border: 0; background: transparent; padding: 6px 14px; border-radius: 999px; cursor: pointer; font-weight: 600; color: var(--muted);
}
.lang-toggle button.active, .segmented button.active { background: var(--brand); color: var(--brand-ink); }
.segmented { width: 100%; }
.segmented button { flex: 1; }

/* ---------- layout ---------- */
.layout { display: grid; grid-template-columns: 320px 1fr; gap: 20px; padding: 20px 24px; max-width: 1400px; margin: 0 auto; }
@media (max-width: 900px) { .layout { grid-template-columns: 1fr; } }

.panel, .card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.setup { padding: 18px; align-self: start; position: sticky; top: 84px; }
@media (max-width: 900px) { .setup { position: static; } }
.setup h2 { margin: 0 0 14px; font-size: 17px; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field > span { font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
select, input[type="text"], input:not([type]) {
  padding: 9px 11px; border: 1px solid var(--line); border-radius: 10px; background: #fff; width: 100%;
}
select:focus, input:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }

.subject-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.subject-grid button {
  display: flex; align-items: center; gap: 6px; text-align: left; padding: 8px 10px; border: 1px solid var(--line);
  background: #fff; border-radius: 10px; cursor: pointer; font-size: 13.5px;
}
.subject-grid button.active { border-color: var(--brand); background: var(--brand-soft); font-weight: 600; }
.subject-grid .ico { font-size: 17px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; max-height: 150px; overflow: auto; }
.chip { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 4px 10px; font-size: 13px; cursor: pointer; }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- workspace ---------- */
.workspace { min-width: 0; }
.tabs { display: flex; gap: 6px; margin-bottom: 14px; }
.tab { border: 1px solid var(--line); background: var(--surface); padding: 9px 18px; border-radius: 999px; cursor: pointer; font-weight: 600; }
.tab.active { background: var(--ink); color: #fff; border-color: var(--ink); }
.tabpane { display: none; }
.tabpane.active { display: block; }

.pane-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.context-line { color: var(--muted); font-size: 14px; }
.context-line b { color: var(--ink); }
.row { display: flex; gap: 10px; align-items: center; }
.inline { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.inline select { width: auto; }

.btn { border: 1px solid transparent; border-radius: 10px; padding: 9px 16px; cursor: pointer; font-weight: 600; }
.btn.primary { background: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.ghost:hover { background: var(--surface); }
.btn:disabled { opacity: .55; cursor: wait; }

.card { padding: 22px 26px; }
.card.empty { color: var(--muted); text-align: center; padding: 48px 26px; }

/* ---------- markdown ---------- */
.markdown { line-height: 1.65; font-size: 15.5px; }
.markdown h1, .markdown h2, .markdown h3 { line-height: 1.3; }
.markdown h1 { font-size: 24px; margin-top: 0; }
.markdown h2 { font-size: 19px; margin-top: 26px; padding-bottom: 4px; border-bottom: 1px solid var(--line); }
.markdown h3 { font-size: 16px; }
.markdown pre { background: #f3f1ea; padding: 12px 14px; border-radius: 10px; overflow-x: auto; }
.markdown code { font-family: Consolas, "Cascadia Mono", monospace; font-size: 14px; }
.markdown table { border-collapse: collapse; margin: 10px 0; }
.markdown th, .markdown td { border: 1px solid var(--line); padding: 6px 10px; }
.markdown th { background: var(--bg); }
.markdown blockquote { margin: 0; padding: 4px 14px; border-left: 4px solid var(--accent); background: #fdf7ec; border-radius: 0 8px 8px 0; }

.thinking { display: flex; align-items: center; gap: 10px; color: var(--muted); justify-content: center; padding: 30px 0; }
.dots span { display: inline-block; width: 8px; height: 8px; margin: 0 2px; border-radius: 50%; background: var(--brand); animation: bounce 1.2s infinite; }
.dots span:nth-child(2) { animation-delay: .15s; }
.dots span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 80%, 100% { transform: scale(.5); opacity: .5; } 40% { transform: scale(1); opacity: 1; } }

/* ---------- chat ---------- */
.chat { margin-top: 16px; }
.chat h3 { margin-top: 0; }
.chat-log { display: flex; flex-direction: column; gap: 10px; max-height: 420px; overflow-y: auto; margin-bottom: 12px; }
.msg { padding: 10px 14px; border-radius: 12px; max-width: 85%; line-height: 1.55; }
.msg.user { align-self: flex-end; background: var(--brand); color: #fff; }
.msg.assistant { align-self: flex-start; background: var(--bg); }
.msg.assistant p:first-child { margin-top: 0; }
.msg.assistant p:last-child { margin-bottom: 0; }
.chat-form { display: flex; gap: 8px; }
.chat-actions { margin-top: 10px; text-align: right; }

/* ---------- quiz ---------- */
.quiz-title { margin-top: 0; }
.question { border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; margin-bottom: 12px; text-align: left; }
.question .qnum { font-size: 12px; font-weight: 700; color: var(--brand); text-transform: uppercase; letter-spacing: .5px; }
.question .qprompt { margin: 6px 0 10px; font-weight: 600; }
.question .qprompt p { margin: 0; }
.option { display: flex; gap: 10px; align-items: flex-start; padding: 8px 10px; border-radius: 8px; cursor: pointer; border: 1px solid transparent; }
.option:hover { background: var(--bg); }
.option input { margin-top: 4px; }
.option.correct { background: #e6f4ea; border-color: var(--good); }
.option.wrong { background: #fbe9e7; border-color: var(--bad); }
.question.graded .option { cursor: default; }
.question .explain { margin-top: 10px; padding: 10px 12px; background: var(--bg); border-radius: 8px; font-size: 14px; }
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 700; margin-left: 8px; }
.badge.good { background: #e6f4ea; color: var(--good); }
.badge.mid { background: #fdf1dc; color: var(--mid); }
.badge.bad { background: #fbe9e7; color: var(--bad); }
.quiz-submit { text-align: right; }

.score-hero { display: flex; align-items: center; gap: 22px; padding: 18px; border-radius: 12px; background: var(--brand-soft); margin-bottom: 18px; text-align: left; flex-wrap: wrap; }
.score-ring { --p: 0; width: 96px; height: 96px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: conic-gradient(var(--ring, var(--brand)) calc(var(--p) * 1%), #fff 0); }
.score-ring b { background: var(--brand-soft); width: 74px; height: 74px; border-radius: 50%; display: grid; place-items: center; font-size: 22px; }
.score-hero .feedback { flex: 1; min-width: 240px; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.tag { font-size: 12.5px; padding: 3px 9px; border-radius: 999px; background: #fff; border: 1px solid var(--line); }

/* ---------- progress ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { padding: 16px; }
.stat .label { color: var(--muted); font-size: 13px; }
.stat .value { font-size: 28px; font-weight: 700; margin-top: 4px; }
.bars { display: flex; flex-direction: column; gap: 10px; }
.bar-row { display: grid; grid-template-columns: 170px 1fr 54px; gap: 10px; align-items: center; font-size: 14px; }
.bar-track { height: 14px; border-radius: 999px; background: var(--bg); overflow: hidden; }
.bar-fill { height: 100%; border-radius: 999px; }
.history { width: 100%; border-collapse: collapse; font-size: 14px; }
.history th, .history td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; }
.history th { color: var(--muted); font-weight: 600; font-size: 13px; }
.section-title { margin: 20px 0 10px; font-size: 16px; }
#reportOutput { margin-top: 16px; }
.trend { margin-bottom: 4px; }

/* ---------- access code screen ---------- */
.login-screen { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; background: var(--bg); padding: 20px; }
.login-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 30px 26px; width: min(360px, 100%); text-align: center; display: flex; flex-direction: column; gap: 12px; }
.login-card .logo { font-size: 44px; }
.login-card h2, .login-card p { margin: 0; }
.login-card p { color: var(--muted); }
.login-card input { text-align: center; font-size: 18px; letter-spacing: 2px; }
.login-error { color: var(--bad) !important; font-weight: 600; }

/* ---------- dialog & toast ---------- */
dialog { border: 0; border-radius: var(--radius); padding: 22px; width: min(400px, 92vw); box-shadow: 0 20px 60px rgba(0, 0, 0, .25); }
dialog::backdrop { background: rgba(20, 24, 32, .45); }
dialog h3 { margin-top: 0; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 11px 18px; border-radius: 10px; box-shadow: var(--shadow); z-index: 50; max-width: 90vw; }
.toast.error { background: var(--bad); }
