:root {
  --purple-50: #f6f3ff;
  --purple-100: #ede7fe;
  --purple-200: #ddd2fd;
  --purple-300: #c2b0fa;
  --purple-500: #8b5cf6;
  --purple-600: #7c3aed;
  --purple-700: #6d28d9;
  --purple-900: #3b1a78;
  --bg: #fbfaff;
  --surface: #ffffff;
  --text: #1f1a2e;
  --muted: #6e6787;
  --border: #ebe6f7;
  --ok: #15803d;
  --ok-bg: #ecfdf3;
  --bad: #c02646;
  --bad-bg: #fff1f3;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(60, 30, 120, .04), 0 4px 16px rgba(60, 30, 120, .05);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: 'IBM Plex Sans Thai', system-ui, -apple-system, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; color: inherit; }
h1, h2, h3 { line-height: 1.35; margin: 0 0 .4em; font-weight: 600; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.35rem; }
h3 { font-size: 1.05rem; }
p { margin: 0 0 .8em; }
[hidden] { display: none !important; }

.container { max-width: 960px; margin: 0 auto; padding: 24px 16px 64px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.r { text-align: right; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 20px;
  background: rgba(255, 255, 255, .85);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; color: var(--purple-900); }
.brand-mark {
  display: inline-grid; place-items: center;
  width: 30px; height: 30px; border-radius: 9px;
  background: var(--purple-600); color: #fff; font-weight: 700; font-size: .95rem;
}
.brand-mark.lg { width: 52px; height: 52px; border-radius: 15px; font-size: 1.5rem; }
.topbar-right { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; justify-content: flex-end; }
.user-chip { font-size: .9rem; color: var(--muted); }
.tag { background: var(--purple-100); color: var(--purple-700); padding: 1px 8px; border-radius: 99px; font-size: .75rem; font-weight: 600; }

/* buttons & inputs */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 20px; border-radius: 10px; border: 1px solid var(--purple-600);
  background: var(--purple-600); color: #fff; font-weight: 500; cursor: pointer;
  transition: background .15s, transform .05s;
}
.btn:hover { background: var(--purple-700); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-ghost { background: #fff; color: var(--purple-700); border-color: var(--purple-200); }
.btn-ghost:hover { background: var(--purple-50); }
.btn-sm { padding: 6px 12px; font-size: .88rem; border-radius: 8px; }
.btn-block { width: 100%; }
.link { background: none; border: 0; padding: 0; color: var(--purple-700); cursor: pointer; font-weight: 500; }
.link:hover { text-decoration: underline; }

label { display: block; font-size: .9rem; font-weight: 500; margin-bottom: 14px; }
input:not([type=checkbox]) {
  display: block; width: 100%; margin-top: 6px;
  padding: 11px 14px; border: 1px solid var(--border); border-radius: 10px; background: #fff;
  outline: none; transition: border-color .15s, box-shadow .15s;
}
input:focus { border-color: var(--purple-500); box-shadow: 0 0 0 4px var(--purple-100); }
.search { max-width: 260px; margin: 0; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 24px; margin-bottom: 18px;
}
.narrow { max-width: 460px; margin: 40px auto; }
.alert { background: var(--bad-bg); color: var(--bad); padding: 10px 14px; border-radius: 10px; font-size: .9rem; margin-bottom: 14px; }
.step { color: var(--purple-600); font-size: .82rem; font-weight: 600; letter-spacing: .02em; margin-bottom: 4px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin: 8px 0 20px; flex-wrap: wrap; }
.page-head p { margin: 0; }
.head-tools { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* login */
.login-wrap { min-height: calc(100vh - 100px); display: grid; place-items: center; }
.login-card { width: 100%; max-width: 380px; text-align: center; padding: 36px 28px; }
.login-card form { text-align: left; margin-top: 20px; }
.login-logo { margin-bottom: 14px; }
.hint { margin: 16px 0 0; font-size: .8rem; color: var(--muted); }

/* mode */
.mode-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; }
.mode-card {
  text-align: left; cursor: pointer; padding: 26px; border-radius: var(--radius);
  border: 1px solid var(--border); background: #fff; box-shadow: var(--shadow);
  transition: border-color .15s, transform .15s, box-shadow .15s;
}
.mode-card:hover { border-color: var(--purple-300); transform: translateY(-2px); box-shadow: 0 8px 24px rgba(109, 40, 217, .1); }
.mode-card p { color: var(--muted); }
.mode-icon {
  width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center;
  background: var(--purple-100); color: var(--purple-700); font-weight: 700; margin-bottom: 14px;
}
.mode-meta { font-size: .82rem; color: var(--purple-700); background: var(--purple-50); padding: 3px 10px; border-radius: 99px; }

/* topics */
.book { margin-bottom: 26px; }
.book-title { color: var(--purple-900); padding-bottom: 6px; border-bottom: 1px solid var(--border); margin-bottom: 12px; }
.topic-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; }
.topic {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px; text-align: left;
  padding: 14px 16px; border-radius: 12px; border: 1px solid var(--border); background: #fff; cursor: pointer;
  transition: border-color .15s, background .15s;
}
.topic:hover { border-color: var(--purple-300); background: var(--purple-50); }
.topic-no { font-size: .78rem; color: var(--purple-600); font-weight: 600; }
.topic-name { font-weight: 500; }
.topic-count { font-size: .8rem; color: var(--muted); }

/* exam */
.exam-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; margin-bottom: 10px; }
.exam-progress-text { font-weight: 600; }
.timer { white-space: nowrap; flex: none; font-variant-numeric: tabular-nums; color: var(--purple-700); background: var(--purple-50); padding: 4px 12px; border-radius: 99px; font-size: .9rem; }
.progress { height: 6px; background: var(--purple-100); border-radius: 99px; overflow: hidden; margin-bottom: 18px; }
.progress > div { height: 100%; background: var(--purple-600); transition: width .25s; }
.q-card { padding: 28px; }
.q-meta { font-size: .8rem; color: var(--purple-600); font-weight: 500; margin-bottom: 8px; }
.q-text { font-size: 1.2rem; font-weight: 500; margin-bottom: 20px; }
.q-no { color: var(--purple-600); font-weight: 700; }
.choices { display: grid; gap: 10px; }
.choice {
  display: flex; align-items: flex-start; gap: 12px; text-align: left;
  padding: 13px 16px; border-radius: 12px; border: 1.5px solid var(--border); background: #fff; cursor: pointer;
  transition: border-color .12s, background .12s;
}
.choice:hover { border-color: var(--purple-300); background: var(--purple-50); }
.choice.selected { border-color: var(--purple-600); background: var(--purple-50); }
.choice-label {
  flex: none; width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center;
  background: var(--purple-100); color: var(--purple-700); font-weight: 600; font-size: .9rem;
}
.choice.selected .choice-label { background: var(--purple-600); color: #fff; }
.q-nav { display: flex; justify-content: space-between; margin-top: 22px; gap: 10px; }
.grid-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; font-weight: 600; font-size: .95rem; }
.q-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(38px, 1fr)); gap: 6px; }
.q-dot {
  height: 36px; border-radius: 8px; border: 1px solid var(--border); background: #fff; cursor: pointer;
  font-size: .85rem; font-variant-numeric: tabular-nums; color: var(--muted);
}
.q-dot.done { background: var(--purple-100); border-color: var(--purple-200); color: var(--purple-700); font-weight: 600; }
.q-dot.current { outline: 2px solid var(--purple-600); outline-offset: 1px; }

/* result */
.score-card { display: flex; gap: 32px; align-items: center; flex-wrap: wrap; padding: 30px; }
.score-ring {
  --p: 0; width: 170px; height: 170px; border-radius: 50%; flex: none;
  display: grid; place-items: center;
  background: conic-gradient(var(--purple-600) calc(var(--p) * 1%), var(--purple-100) 0);
}
.score-ring > div { width: 136px; height: 136px; border-radius: 50%; background: #fff; display: grid; place-content: center; text-align: center; }
.score-num { font-size: 2.2rem; font-weight: 700; color: var(--purple-900); line-height: 1.1; }
.score-num span { font-size: 1rem; color: var(--muted); font-weight: 500; }
.score-pct { color: var(--purple-600); font-weight: 600; }
.score-info { flex: 1; min-width: 240px; }
.score-info h2 { margin-bottom: 2px; }
.grade { display: inline-block; background: var(--purple-50); color: var(--purple-700); padding: 2px 12px; border-radius: 99px; font-weight: 600; font-size: .88rem; margin-bottom: 14px; }
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px 20px; margin: 0 0 18px; }
.facts dt { font-size: .78rem; color: var(--muted); }
.facts dd { margin: 0; font-weight: 500; }
.ok-t { color: var(--ok); font-weight: 600; }
.bad-t { color: var(--bad); font-weight: 600; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; }

.bars { display: grid; gap: 8px; margin-top: 8px; }
.bar-row { display: grid; grid-template-columns: minmax(120px, 280px) 1fr 52px; gap: 12px; align-items: center; font-size: .88rem; }
.bar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar { height: 8px; background: var(--purple-100); border-radius: 99px; overflow: hidden; }
.bar > div { height: 100%; background: var(--purple-500); border-radius: 99px; }
.bar-val { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }

.review-tabs { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.review-tabs h3 { margin: 0; }
.seg { display: inline-flex; background: var(--purple-50); border-radius: 10px; padding: 3px; }
.seg-btn { border: 0; background: none; padding: 6px 14px; border-radius: 8px; cursor: pointer; color: var(--muted); font-size: .88rem; }
.seg-btn.active { background: #fff; color: var(--purple-700); font-weight: 600; box-shadow: 0 1px 3px rgba(60, 30, 120, .1); }
.perfect { text-align: center; padding: 20px; color: var(--purple-700); font-weight: 500; }
.review { border-top: 1px solid var(--border); padding: 16px 0; }
.review:first-child { border-top: 0; }
.review-head { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; flex-wrap: wrap; }
.review-q { font-weight: 500; margin-bottom: 8px; }
.review-choices { display: grid; gap: 4px; font-size: .92rem; }
.rc { padding: 6px 12px; border-radius: 8px; color: var(--muted); }
.rc.correct { background: var(--ok-bg); color: var(--ok); font-weight: 500; }
.rc.chosen-wrong { background: var(--bad-bg); color: var(--bad); text-decoration: line-through; text-decoration-thickness: 1px; }
.rc-note { float: right; font-size: .75rem; font-weight: 600; text-decoration: none; display: inline-block; }
.review-answer { margin-top: 8px; font-size: .9rem; color: var(--text); background: var(--purple-50); padding: 8px 12px; border-radius: 8px; border-left: 3px solid var(--purple-500); }
.badge { font-size: .75rem; font-weight: 600; padding: 2px 10px; border-radius: 99px; }
.badge-ok { background: var(--ok-bg); color: var(--ok); }
.badge-bad { background: var(--bad-bg); color: var(--bad); }

/* table */
.table-card { padding: 0; overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th { text-align: left; font-weight: 500; color: var(--muted); font-size: .8rem; padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--purple-50); }
.table th.r { text-align: right; }
.table td { padding: 12px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.pill { font-size: .75rem; padding: 1px 8px; border-radius: 99px; font-weight: 600; }
.pill-ok { background: var(--ok-bg); color: var(--ok); }
.pill-bad { background: var(--bad-bg); color: var(--bad); }
.empty { text-align: center; padding: 40px; }

/* admin summary */
select {
  padding: 8px 12px; border: 1px solid var(--border); border-radius: 10px; background: #fff;
  font: inherit; font-size: .9rem; color: var(--text); outline: none;
}
select:focus { border-color: var(--purple-500); box-shadow: 0 0 0 4px var(--purple-100); }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 18px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px 18px; }
.kpi-label { font-size: .82rem; color: var(--muted); }
.kpi-value { font-size: 1.8rem; font-weight: 700; color: var(--purple-900); line-height: 1.3; font-variant-numeric: tabular-nums; }
.kpi-sub { font-size: .78rem; color: var(--muted); }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 10px; }
.card-head h3 { margin: 0; }
.table-card .card-head { padding: 18px 16px 4px; }
.num { font-variant-numeric: tabular-nums; }
.bar-cell { display: flex; align-items: center; gap: 8px; min-width: 140px; }
.mini-bar { flex: 1; height: 8px; background: var(--purple-100); border-radius: 99px; overflow: hidden; }
.mini-bar > div { height: 100%; background: var(--purple-500); border-radius: 99px; }
.bar-row:hover .bar > div { background: var(--purple-700); }
.bar-row .bar-val { white-space: nowrap; }
.bars .bar-row { grid-template-columns: minmax(120px, 300px) 1fr 110px; }

/* loading */
.center-block { display: grid; place-items: center; gap: 12px; padding: 80px 0; }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--purple-100); border-top-color: var(--purple-600); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 640px) {
  .container { padding: 16px 16px 48px; }
  .topbar { padding: 10px 16px; }
  .topbar-right { gap: 10px; }
  .user-chip { display: none; }
  .card, .q-card { padding: 18px; }
  .q-text { font-size: 1.08rem; }
  .score-card { flex-direction: column; align-items: stretch; text-align: center; }
  .score-ring { margin: 0 auto; }
  .score-info { min-width: 0; }
  .facts { text-align: left; grid-template-columns: 1fr 1fr; }
  .facts dd { overflow-wrap: anywhere; }
  .actions { justify-content: center; }
  .bar-row { grid-template-columns: 1fr 52px; }
  .bar-row .bar { grid-column: 1 / -1; grid-row: 2; }
  .bars .bar-row { grid-template-columns: 1fr auto; }
  .kpis { grid-template-columns: 1fr 1fr; }
  .kpi-value { font-size: 1.4rem; }
  .filters { width: 100%; }
  .filters select { flex: 1; }
  .search { max-width: none; }
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table tr { padding: 12px 16px; border-bottom: 1px solid var(--border); }
  .table tr:last-child { border-bottom: 0; }
  .table td, .table td.r { padding: 2px 0; border: 0; text-align: left; }
  .table td:last-child { margin-top: 8px; }
}
