/* ==========================================================
   base.css  ใช้ร่วมกันทุกหน้า
   - โทเคนสี / ตัวอักษร (แต่ละบทเขียนทับได้ใน chapter.css)
   - ส่วนประกอบกลาง: แถบบน ปุ่ม แล็บทดลอง แบบทดสอบ
   ========================================================== */

:root {
  --bg: #F2F4F8;
  --surface: #FFFFFF;
  --surface-2: #E6EAF2;
  --fg: #141A24;
  --muted: #556073;
  --line: #D3D9E4;
  --accent: #2F5BEA;
  --accent-ink: #FFFFFF;
  --good: #12683F;
  --good-bg: #DDF3E6;
  --bad: #A8281C;
  --bad-bg: #FBE4E1;
  --font-display: "Prompt", "Noto Sans Thai", system-ui, sans-serif;
  --font-body: "IBM Plex Sans Thai", "Noto Sans Thai", system-ui, sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, Menlo, Consolas, monospace;
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0E1218;
    --surface: #161B24;
    --surface-2: #1E2430;
    --fg: #E9EDF4;
    --muted: #98A2B5;
    --line: #2A3242;
    --accent: #7FA2FF;
    --accent-ink: #0B1020;
    --good: #6FD9A0;
    --good-bg: #12301F;
    --bad: #FF9488;
    --bad-bg: #3A1A17;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0E1218;
  --surface: #161B24;
  --surface-2: #1E2430;
  --fg: #E9EDF4;
  --muted: #98A2B5;
  --line: #2A3242;
  --accent: #7FA2FF;
  --accent-ink: #0B1020;
  --good: #6FD9A0;
  --good-bg: #12301F;
  --bad: #FF9488;
  --bad-bg: #3A1A17;
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
html { font-size: 100%; -webkit-text-size-adjust: 100%; }
html.present { font-size: 125%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-body);
  font-size: 1.0625rem;
  line-height: 1.8;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  padding-block: 0 4rem;
}
body::before {
  content: "";
  position: fixed;
  inset: 0 0 auto;
  height: env(safe-area-inset-top, 0px);
  background: var(--bg);
  z-index: 10;
}

img, svg { max-width: 100%; }
a { color: var(--accent); }
h1, h2, h3, h4 { font-family: var(--font-display); text-wrap: balance; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.sr-only {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.page { max-width: 64rem; margin: 0 auto; }

/* ---------- แถบบน ---------- */
.topbar {
  position: sticky;
  top: env(safe-area-inset-top, 0px);
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: .5rem .75rem;
  padding-block: .6rem;
  max-width: 64rem;
  margin-inline: auto;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  font-size: .95rem;
  line-height: 1.5;
}
.topbar a.back { text-decoration: none; color: var(--fg); font-weight: 500; }
.topbar a.back:hover { color: var(--accent); }
.tools { display: flex; gap: .5rem; flex-wrap: wrap; }
.read-progress {
  position: fixed;
  top: env(safe-area-inset-top, 0px);
  left: 0;
  height: 3px;
  width: 0;
  z-index: 30;
  background: var(--accent);
}

/* ---------- ปุ่ม ---------- */
.btn {
  font: 500 .9rem/1.4 var(--font-body);
  color: var(--fg);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: .4rem .85rem;
  cursor: pointer;
  transition: border-color .15s, background-color .15s;
}
.btn:hover { border-color: var(--accent); }
.btn[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-size: 1rem; padding: .6rem 1.3rem; }
.btn-primary:disabled { opacity: .45; cursor: not-allowed; }

/* ---------- เนื้อหา ---------- */
.prose p { margin: 0 0 1.1em; max-width: 42rem; }
.sec { padding-top: 3.5rem; }
.sec > h2 {
  font-size: clamp(1.45rem, 4.2vw, 2rem);
  font-weight: 600;
  line-height: 1.35;
  margin: 0 0 1rem;
  display: flex;
  gap: .75rem;
  align-items: baseline;
}
.sec > h2 .no { font: 500 1em/1 var(--font-mono); color: var(--muted); min-width: 1.4ch; }
.note {
  background: var(--surface-2);
  border-radius: 10px;
  padding: .7rem 1rem;
  font-size: .95rem;
  line-height: 1.7;
  color: var(--muted);
  margin: 1rem 0 0;
  max-width: 42rem;
}

/* ---------- แล็บทดลอง ---------- */
.lab {
  margin-top: 1.5rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  padding: clamp(1rem, 3vw, 1.75rem);
}
.lab-title { font: 600 1rem/1.5 var(--font-display); margin: 0 0 .25rem; }
.lab-hint { margin: 0 0 1.25rem; color: var(--muted); font-size: .95rem; line-height: 1.6; }
.lab-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 1.5rem 2rem;
  align-items: start;
}
@media (max-width: 720px) { .lab-grid { grid-template-columns: minmax(0, 1fr); } }
.lab-col { min-width: 0; display: grid; gap: 1rem; }

.readout { display: flex; flex-wrap: wrap; gap: .5rem 1.75rem; margin: 0; }
.kv { min-width: 0; }
.kv dt { font-size: .8rem; color: var(--muted); line-height: 1.5; }
.kv dd { margin: 0; font: 500 1.05rem/1.5 var(--font-mono); font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.kv dd.th { font-family: var(--font-body); }

.swatch {
  border-radius: 12px;
  border: 1px solid var(--line);
  min-height: 5.5rem;
  transition: background-color .08s;
}
.slider-row {
  display: grid;
  grid-template-columns: 2.6rem minmax(0, 1fr) 3.4rem;
  gap: .75rem;
  align-items: center;
}
.slider-row label { font-weight: 600; }
.slider-row output { font: 500 .95rem/1 var(--font-mono); text-align: right; font-variant-numeric: tabular-nums; }
input[type="range"] { width: 100%; margin: 0; accent-color: var(--accent); }
.range-grad {
  -webkit-appearance: none;
  appearance: none;
  height: 1.75rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--grad);
  cursor: pointer;
}
.range-grad::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 1.9rem; height: 1.9rem;
  border-radius: 50%;
  background: var(--thumb, #fff);
  border: 3px solid #fff;
  box-shadow: 0 0 0 1.5px rgba(0, 0, 0, .65), 0 2px 6px rgba(0, 0, 0, .4);
}
.range-grad::-moz-range-thumb {
  width: 1.5rem; height: 1.5rem;
  border-radius: 50%;
  background: var(--thumb, #fff);
  border: 3px solid #fff;
  box-shadow: 0 0 0 1.5px rgba(0, 0, 0, .65), 0 2px 6px rgba(0, 0, 0, .4);
}

.chips { display: flex; flex-wrap: wrap; gap: .5rem; border: 0; padding: 0; margin: 0; min-width: 0; }
.chips legend { padding: 0; font-size: .85rem; color: var(--muted); margin-bottom: .4rem; }
.chips label { cursor: pointer; position: relative; }
.chips input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.chips span {
  display: inline-block;
  padding: .35rem .85rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: .92rem;
  line-height: 1.5;
  background: var(--surface);
  transition: background-color .15s, border-color .15s;
}
.chips input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.chips input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 3px; }

.field { display: grid; gap: .3rem; }
.field label { font-size: .85rem; color: var(--muted); }
.field input[type="text"] {
  font: 500 1.05rem/1.4 var(--font-mono);
  padding: .5rem .7rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
  width: 100%;
  text-transform: uppercase;
}
.field .err { color: var(--bad); font-size: .85rem; min-height: 1.3em; margin: 0; }
input[type="color"] {
  width: 3.2rem; height: 2.6rem; padding: 2px;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); cursor: pointer;
}

/* ---------- แบบทดสอบ ---------- */
.quiz-intro { color: var(--muted); max-width: 42rem; margin: 0 0 1.25rem; }
.q {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 1rem 1.1rem 1.1rem;
  margin: 0 0 1rem;
  min-width: 0;
}
.q legend { font-weight: 600; padding: 0 .35rem; margin-left: -.35rem; line-height: 1.6; max-width: 100%; }
.choice {
  display: flex;
  gap: .7rem;
  align-items: flex-start;
  padding: .6rem .8rem;
  margin-top: .5rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
  line-height: 1.6;
  transition: border-color .15s, background-color .15s;
}
.choice:hover { border-color: var(--accent); }
.choice input { margin: .35rem 0 0; accent-color: var(--accent); flex: none; }
.choice.is-picked { border-color: var(--accent); }
.choice.is-correct { background: var(--good-bg); border-color: var(--good); }
.choice.is-wrong { background: var(--bad-bg); border-color: var(--bad); }
.q.locked .choice { cursor: default; }
.q .verdict { margin: .8rem 0 0; font-size: .95rem; line-height: 1.7; }
.q .verdict b { font-family: var(--font-display); }
.q .verdict.ok b { color: var(--good); }
.q .verdict.no b { color: var(--bad); }
.quiz-bar { display: flex; align-items: center; flex-wrap: wrap; gap: .75rem 1.25rem; margin-top: 1.25rem; }
.quiz-status { color: var(--muted); font-size: .95rem; }
.result {
  margin-top: 1.25rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  padding: 1.25rem 1.4rem;
}
.result .score { font: 700 clamp(2rem, 8vw, 3rem)/1.1 var(--font-display); font-variant-numeric: tabular-nums; margin: 0; }
.result p { margin: .4rem 0 1rem; }

/* ---------- เลื่อนไปบทก่อนหน้า/ถัดไป ---------- */
.chapter-nav { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; margin-top: 4rem; }
@media (max-width: 560px) { .chapter-nav { grid-template-columns: minmax(0, 1fr); } }
.chapter-nav a {
  display: block;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: .9rem 1.1rem;
  text-decoration: none;
  color: var(--fg);
  line-height: 1.5;
}
.chapter-nav a:hover { border-color: var(--accent); }
.chapter-nav small { display: block; color: var(--muted); }
.chapter-nav .next { text-align: right; grid-column: 2; }
@media (max-width: 560px) { .chapter-nav .next { grid-column: 1; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; scroll-behavior: auto !important; }
}

.foot-ch { margin-top: 4rem; color: var(--muted); font-size: .9rem; }

/* ---------- แถบผู้ใช้ / โหมดสาธิต (ใช้ทุกหน้า) ---------- */
.user-chip { display: inline-flex; align-items: center; gap: .5rem; text-decoration: none; max-width: 14rem; }
.user-chip .chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.avatar-s {
  width: 1.5rem; height: 1.5rem; border-radius: 50%; flex: none; overflow: hidden;
  display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink);
  font: 600 .8rem/1 var(--font-display);
}
.avatar-s img { width: 100%; height: 100%; object-fit: cover; display: block; }
.demo-banner {
  max-width: 64rem; margin: .6rem auto 0;
  padding: .5rem .9rem; border-radius: 8px;
  background: var(--surface-2); color: var(--muted);
  font-size: .85rem; line-height: 1.6;
}
.save-note { margin: .4rem 0 .9rem; font-size: .95rem; color: var(--muted); }

/* ให้แอตทริบิวต์ hidden ชนะ display ของคลาสอื่นเสมอ + ลิงก์ที่หน้าตาเป็นปุ่ม */
[hidden] { display: none !important; }
a.btn { text-decoration: none; }
a.btn:not(.btn-primary) { color: var(--fg); }
