:root {
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --ink-1: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --hairline: rgba(11, 11, 11, 0.1);
  --accent: #7a3ce8;
  --good: #006300;
  --good-bg: rgba(0, 128, 0, 0.12);
  --critical: #d03b3b;
  --critical-bg: rgba(208, 59, 59, 0.12);
}
@media (prefers-color-scheme: dark) {
  :root {
    --page: #0d0d0d;
    --surface: #1a1a19;
    --ink-1: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --hairline: rgba(255, 255, 255, 0.1);
    --accent: #9d6bff;
    --good: #0ca30c;
    --good-bg: rgba(12, 163, 12, 0.18);
    --critical-bg: rgba(208, 59, 59, 0.22);
  }
}
* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink-1);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  line-height: 1.6;
}
.wrap { max-width: 640px; margin: 0 auto; padding: 24px 16px 64px; }
header.site { display: flex; justify-content: space-between; align-items: center; padding-bottom: 16px; }
header.site a { color: var(--ink-2); text-decoration: none; font-size: 14px; }
.brand { font-weight: 600; color: var(--ink-1) !important; font-size: 16px !important; }
.brand span { color: var(--accent); }
h1 { font-size: 26px; line-height: 1.35; margin: 8px 0; }
h2.grade { font-size: 16px; color: var(--ink-2); margin: 28px 0 4px; }
p.lead { color: var(--ink-2); margin-top: 0; }
.card {
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 12px;
  padding: 20px;
  margin: 16px 0;
}

/* attribute hidden ต้องชนะกฎ display ของคลาสอื่นเสมอ (เช่น .cta ที่เป็น display:block) */
[hidden] { display: none !important; }

/* ฟอร์ม (โปรไฟล์นักเรียน + หน้าครู) */
.field { display: block; margin: 10px 0; color: var(--ink-2); font-size: 14px; }
.field input, .field select {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 10px 12px;
  font-size: 16px;
  font-family: inherit;
  color: var(--ink-1);
  background: var(--page);
  border: 1px solid var(--hairline);
  border-radius: 8px;
}
.field input:focus, .field select:focus { outline: 2px solid var(--accent); border-color: transparent; }

/* ใบสรุปผลท้ายชุดโจทย์ */
.result-card {
  border: 1px dashed var(--accent);
  border-radius: 10px;
  padding: 14px 16px;
  margin: 16px auto 0;
  max-width: 420px;
  text-align: left;
  font-size: 15px;
}
.result-card .rc-title { font-weight: 600; color: var(--accent); margin-bottom: 4px; }
.result-card .verify { font-weight: 700; letter-spacing: 2px; color: var(--accent); }
.result-card .rc-hint { font-size: 13px; color: var(--muted); margin-top: 6px; }
.submit-status { font-size: 14px; color: var(--ink-2); }

/* รูปประกอบโจทย์ (SVG generate สด) */
svg.fig { display: block; max-width: 100%; height: auto; margin: 12px auto; font-family: inherit; }

/* โหมดบอส */
.boss-row { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.boss { font-size: 34px; line-height: 1; }
.boss.hit { animation: bosshit 0.4s; }
@keyframes bosshit {
  25% { transform: rotate(-14deg) scale(0.85); filter: grayscale(0.6) brightness(1.6); }
  60% { transform: rotate(8deg) scale(1.05); }
}
.hpbar { flex: 1; height: 12px; border-radius: 6px; background: var(--hairline); overflow: hidden; }
.hp { height: 100%; width: 100%; background: linear-gradient(90deg, #d03b3b, #f5b301); border-radius: 6px; transition: width 0.35s; }
.hearts { font-size: 13px; letter-spacing: 1px; white-space: nowrap; }
.mute { background: none; border: none; cursor: pointer; font-size: 17px; padding: 0; }
.streak { color: #f5b301; font-weight: 700; }

/* จอสรุปผล */
.sum-boss { font-size: 52px; line-height: 1; margin-top: 4px; }
.stars-big { font-size: 42px; letter-spacing: 8px; color: #f5b301; margin: 4px 0; }
.stars-big span { display: inline-block; animation: starpop 0.5s backwards; }
.stars-big span.dim { opacity: 0.3; }
@keyframes starpop {
  from { transform: scale(0) rotate(-30deg); }
  60% { transform: scale(1.35) rotate(8deg); }
  to { transform: scale(1) rotate(0); }
}
#summary { position: relative; overflow: hidden; }
.confetti {
  position: absolute;
  top: -12px;
  width: 8px;
  height: 13px;
  border-radius: 2px;
  animation: confetti-fall 2.8s ease-in forwards;
}
@keyframes confetti-fall {
  to { transform: translateY(420px) rotate(720deg); opacity: 0; }
}

/* หน้าแรก — แผนที่ด่าน */
.stage-map { position: relative; padding: 4px 0 12px; }
.stage-map::before {
  content: '';
  position: absolute;
  left: 50%;
  top: 0;
  bottom: 0;
  border-left: 3px dashed var(--hairline);
}
a.stage {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  width: 82%;
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 14px;
  padding: 12px 14px;
  margin: 14px 0;
  color: var(--ink-1);
  text-decoration: none;
}
.stage-map a.stage:nth-child(even) { margin-left: auto; }
a.stage:hover { border-color: var(--accent); }
a.stage.current { border-color: var(--accent); animation: herepulse 1.8s infinite; }
@keyframes herepulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(157, 107, 255, 0.45); }
  55% { box-shadow: 0 0 0 8px rgba(157, 107, 255, 0); }
}
.stage-icon { font-size: 26px; line-height: 1; }
.stage-body { display: flex; flex-direction: column; min-width: 0; }
.stage-body small { color: var(--ink-2); font-size: 12.5px; }
.stage-body .stat { color: var(--accent); font-size: 12px; }
.stage .stars { margin-left: auto; color: #f5b301; font-size: 15px; letter-spacing: 2px; white-space: nowrap; }
.stage .here { font-size: 11px; color: var(--accent); font-weight: 700; }

/* ตารางผลคะแนน (หน้าครู) */
table.results { width: 100%; border-collapse: collapse; font-size: 14px; margin: 10px 0; }
table.results th, table.results td {
  text-align: left;
  padding: 6px 8px;
  border-bottom: 1px solid var(--hairline);
}
table.results th { color: var(--ink-2); font-weight: 600; }
.code-big { font-size: 34px; font-weight: 700; letter-spacing: 4px; color: var(--accent); }
.notice { font-size: 14px; color: var(--ink-2); background: var(--page); border: 1px solid var(--hairline); border-radius: 8px; padding: 10px 12px; }
.danger { color: var(--critical); }

/* หน้าแรก — รายการบท */
.topics a {
  display: block;
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 12px;
  padding: 14px 16px;
  margin: 10px 0;
  color: var(--ink-1);
  text-decoration: none;
}
.topics a small { display: block; color: var(--ink-2); }
.topics a .stat { display: block; font-size: 12px; color: var(--accent); margin-top: 2px; }

/* หน้าเล่นโจทย์ */
.quiz-head { display: flex; justify-content: space-between; font-size: 14px; color: var(--ink-2); }
#question { font-size: 18px; margin: 12px 0 16px; }
button.choice {
  display: block;
  width: 100%;
  text-align: left;
  font-size: 17px;
  font-family: inherit;
  color: var(--ink-1);
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 10px;
  padding: 12px 14px;
  margin: 8px 0;
  cursor: pointer;
}
button.choice:hover:not(:disabled) { border-color: var(--accent); }
button.choice:disabled { cursor: default; opacity: 0.85; }
button.choice.correct { background: var(--good-bg); border-color: var(--good); opacity: 1; }
button.choice.incorrect { background: var(--critical-bg); border-color: var(--critical); opacity: 1; }
#solution {
  margin-top: 14px;
  padding: 14px 16px;
  border-radius: 10px;
  background: var(--page);
  border: 1px dashed var(--hairline);
  font-size: 15px;
}
.cta {
  display: block;
  width: 100%;
  margin: 20px 0 0;
  padding: 14px 16px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-family: inherit;
  font-weight: 500;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
}
.cta.secondary { background: transparent; color: var(--accent); border: 1px solid var(--accent); }

/* สรุปผล */
#summary { text-align: center; }
#final-score { font-size: 52px; font-weight: 700; margin: 8px 0; }
#final-msg { color: var(--ink-2); }

footer.site {
  margin-top: 48px;
  padding-top: 16px;
  border-top: 1px solid var(--hairline);
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}
footer.site a { color: var(--muted); }
