@font-face { font-family: Kanit; src: url(/fonts/Kanit-Regular.ttf); font-weight: 400; font-display: swap; }
@font-face { font-family: Kanit; src: url(/fonts/Kanit-Medium.ttf); font-weight: 500; font-display: swap; }
@font-face { font-family: Kanit; src: url(/fonts/Kanit-SemiBold.ttf); font-weight: 600; font-display: swap; }

:root {
  --ink: #23271f;
  --muted: #666d5f;
  --line: #e2dcc9;
  --bg: #f7f3e6;
  --card: #fffdf7;
  --green: #1f5a3a;
  --green-dark: #143d27;
  --gold: #e0a526;
  --gold-soft: #f8ecc9;
  --pre: #a9a690;
  --good: #1f7a4a;
  --good-bg: #dff1e5;
  --warn: #8a5a00;
  --warn-bg: #fbeccb;
  --bad: #a62b1f;
  --bad-bg: #f9dfda;
  --info: #1e5a7e;
  --info-bg: #dcecf5;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 400 16px/1.6 Kanit, "Noto Sans Thai", Tahoma, sans-serif; }
h1, h2, h3, h4 { margin: 0 0 8px; font-weight: 600; line-height: 1.3; }
h1 { font-size: 26px; } h2 { font-size: 21px; } h3 { font-size: 18px; } h4 { font-size: 16px; margin-top: 16px; }
p { margin: 0 0 10px; }
a { color: var(--green); }
img { max-width: 100%; }
[hidden] { display: none !important; }
.muted { color: var(--muted); } .small { font-size: 14px; } .block { display: block; }
.error { color: var(--bad); }
.grow { flex: 1; min-width: 0; }

.btn { display: inline-flex; align-items: center; justify-content: center; min-height: 44px; padding: 8px 18px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); color: var(--ink); font: 500 16px Kanit, sans-serif; text-decoration: none; cursor: pointer; }
.btn:hover:not(:disabled) { border-color: var(--green); }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--green-dark); }
.btn.danger { color: var(--bad); }
.btn.small { min-height: 36px; padding: 4px 12px; font-size: 14px; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.linkish { background: none; border: 0; padding: 4px; color: inherit; font: inherit; text-decoration: underline; cursor: pointer; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, summary:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

label { display: block; margin-bottom: 12px; font-weight: 500; }
input, select, textarea { display: block; width: 100%; margin-top: 4px; padding: 10px 12px; border: 1px solid #cfc8b2; border-radius: 8px; background: #fff; color: var(--ink); font: 400 16px/1.5 Kanit, sans-serif; }
input[type=radio], input[type=checkbox] { display: inline-block; width: 20px; height: 20px; margin: 0; flex: none; accent-color: var(--green); }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 16px; background: var(--green-dark); color: #fff; }
.brand { display: flex; flex-direction: column; color: inherit; text-decoration: none; line-height: 1.25; }
.brand-mark { color: var(--gold); font-weight: 600; font-size: 18px; }
.brand-sub { font-size: 13px; opacity: .85; }
.topbar-right { display: flex; align-items: center; gap: 12px; font-size: 14px; }
.who { max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.page { max-width: 760px; margin: 0 auto; padding: 20px 16px 48px; }
.wide-page { max-width: 1120px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 18px; margin-bottom: 16px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.row-between.wrap, .wrap { flex-wrap: wrap; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }

.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 14px; font-weight: 500; white-space: nowrap; }
.badge.idle { background: #ece8da; color: #4f5347; }
.badge.info { background: var(--info-bg); color: var(--info); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }
.badge.good { background: var(--good-bg); color: var(--good); }

.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; gap: 28px; padding: 24px 16px; background: var(--green-dark); }
.login-card { width: 100%; max-width: 380px; margin: 0; }
.login-art { height: 380px; width: auto; }
@media (max-width: 720px) { .login { flex-direction: column; gap: 0; justify-content: flex-start; } .login-art { height: 170px; margin-bottom: 12px; } }

.course-list { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); margin-top: 12px; }
.course-card { display: block; color: inherit; text-decoration: none; margin: 0; }
.course-card:hover { border-color: var(--green); }
.course-card h2 { margin-top: 10px; }
.course-title { margin-bottom: 14px; }

.stepper { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; list-style: none; margin: 0 0 18px; padding: 0; font-size: 13px; text-align: center; color: var(--muted); }
.stepper li { position: relative; }
.stepper li::before { content: ""; position: absolute; top: 14px; left: -50%; width: 100%; height: 2px; background: var(--line); }
.stepper li:first-child::before { display: none; }
.stepper li.done::before, .stepper li.active::before { background: var(--green); }
.stepper .dot { position: relative; z-index: 1; display: flex; align-items: center; justify-content: center; width: 30px; height: 30px; margin: 0 auto 4px; border-radius: 50%; background: #ece8da; font-weight: 600; }
.stepper .dot svg { width: 16px; height: 16px; }
.stepper .done .dot { background: var(--green); color: #fff; }
.stepper .active { color: var(--ink); font-weight: 500; }
.stepper .active .dot { background: var(--gold); color: var(--ink); }

.intro { display: flex; gap: 18px; align-items: flex-end; }
.intro-art { height: 240px; width: auto; flex: none; }
.facts { margin: 0 0 14px; padding-left: 20px; }
@media (max-width: 560px) { .intro-art { height: 150px; } .intro { align-items: flex-start; flex-direction: column; } }

.player video { display: block; width: 100%; aspect-ratio: 16 / 9; background: #000; border-radius: 12px; }
.player-info { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-top: 12px; }
.speed { display: flex; flex: none; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.speed button { min-width: 44px; min-height: 36px; border: 0; background: var(--card); font: 500 14px Kanit, sans-serif; color: var(--ink); cursor: pointer; }
.speed button.on { background: var(--green); color: #fff; }
#after-video { margin-top: 10px; }

.section-title { margin-top: 26px; }
.lesson-list { display: grid; gap: 8px; }
.lesson { display: flex; gap: 12px; align-items: center; width: 100%; padding: 8px; border: 1px solid var(--line); border-radius: 10px; background: var(--card); color: inherit; font: inherit; text-align: left; cursor: pointer; }
.lesson img, .lesson .thumb { flex: none; width: 104px; height: 58px; border-radius: 6px; object-fit: cover; background: #ddd8c5; }
.lesson-body { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.lesson-title { font-weight: 500; }
.lesson.current { border-color: var(--green); box-shadow: inset 0 0 0 1px var(--green); }
.lesson:disabled { opacity: .55; cursor: default; }
.mini-track { display: block; height: 5px; margin-top: 4px; border-radius: 3px; background: #ece8da; overflow: hidden; }
.mini-track span { display: block; height: 100%; width: 0; background: var(--green); }
.mini-track.big { height: 8px; margin: 0 0 16px; }

.score-pair { display: flex; align-items: center; flex-wrap: wrap; gap: 14px 18px; margin: 14px 0; }
.score-pair strong { display: block; font-size: 30px; font-weight: 600; line-height: 1.2; }
.score-label { font-size: 14px; color: var(--muted); }
.arrow svg { display: block; width: 26px; height: 26px; color: var(--muted); }
.score-total { margin-left: auto; padding-left: 18px; border-left: 2px solid var(--gold); }

.bars { display: grid; gap: 8px; }
.bar-row { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(70px, 1fr) auto; gap: 10px; align-items: center; font-size: 15px; }
.bar-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { display: block; height: 12px; background: #ece8da; border-radius: 4px; }
.bar-fill { display: block; height: 100%; width: 0; min-width: 2px; background: var(--green); border-radius: 0 4px 4px 0; }
.bar-fill.pre { background: var(--pre); }
.bar-val { min-width: 48px; text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.bar-val em { font-style: normal; color: var(--warn); font-size: 13px; }
.legend { display: flex; gap: 16px; font-size: 14px; color: var(--muted); margin-bottom: 8px; }
.swatch { display: inline-block; width: 12px; height: 12px; margin-right: 6px; border-radius: 3px; background: var(--green); vertical-align: -1px; }
.swatch.pre { background: var(--pre); }
@media (max-width: 560px) { .bar-row { grid-template-columns: 1fr auto; } .bar-track { grid-column: 1 / -1; grid-row: 2; } }

.exam { min-height: 100vh; display: flex; flex-direction: column; }
.exam-head { position: sticky; top: 0; z-index: 5; display: flex; justify-content: space-between; align-items: center; padding: 8px 16px; background: var(--green-dark); color: #fff; }
.exam-head strong { display: block; font-weight: 500; }
.exam-head .muted { color: #cfd9cf; }
.timer { padding: 2px 12px; border-radius: 8px; background: rgba(255, 255, 255, .14); font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
.timer.low { background: var(--bad); }
.exam-main { width: 100%; max-width: 680px; margin: 0 auto; padding: 16px; }
.q-no { color: var(--muted); margin-bottom: 4px; }
.q-text { font-size: 21px; margin-bottom: 16px; white-space: pre-line; }
.essay { font-weight: 400; }
.essay-item { margin: 10px 0 14px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; }
.essay-q { font-weight: 500; white-space: pre-line; }
.essay-a { padding: 8px 10px; background: #f3efe0; border-radius: 6px; white-space: pre-wrap; overflow-wrap: anywhere; }
.inline-score { margin: 0; max-width: 160px; }
.nowrap { white-space: nowrap; }
.choices { display: grid; gap: 10px; }
.choice { display: flex; gap: 12px; align-items: center; width: 100%; min-height: 56px; padding: 10px 14px; border: 1.5px solid var(--line); border-radius: 10px; background: var(--card); color: inherit; font: 400 16px/1.45 Kanit, sans-serif; text-align: left; cursor: pointer; }
.choice:hover { border-color: var(--green); }
.choice .letter { flex: none; display: flex; align-items: center; justify-content: center; width: 30px; height: 30px; border-radius: 50%; background: #ece8da; font-weight: 500; }
.choice.on { border-color: var(--green); background: #e9f3ea; }
.choice.on .letter { background: var(--green); color: #fff; }
.exam-nav { display: flex; justify-content: space-between; gap: 10px; margin: 20px 0 14px; }
.exam-nav .btn { flex: 1; }
.exam-map summary { cursor: pointer; color: var(--muted); padding: 6px 0; }
#map { display: grid; grid-template-columns: repeat(auto-fill, minmax(42px, 1fr)); gap: 6px; }
#map button { min-height: 42px; border: 1px solid var(--line); border-radius: 6px; background: var(--card); font: 500 15px Kanit, sans-serif; color: var(--ink); cursor: pointer; }
#map button.on { background: #e9f3ea; border-color: var(--green); }
#map button.here { outline: 2px solid var(--gold); }

.toast { position: fixed; left: 50%; bottom: 20px; z-index: 50; max-width: calc(100vw - 32px); transform: translateX(-50%); padding: 10px 18px; border-radius: 8px; background: var(--green-dark); color: #fff; }
.toast.bad { background: var(--bad); }

.count { display: inline-block; min-width: 20px; margin-left: 6px; padding: 0 6px; border-radius: 999px; background: var(--gold); color: var(--ink); font-size: 13px; font-weight: 600; text-align: center; }
#chat { position: fixed; right: 16px; bottom: 16px; z-index: 40; }
body.in-exam #chat { display: none; }
.chat-open { display: inline-flex; align-items: center; gap: 8px; min-height: 48px; padding: 8px 16px; border: 0; border-radius: 999px; background: var(--green-dark); color: #fff; font: 500 15px Kanit, sans-serif; box-shadow: 0 3px 10px rgba(0, 0, 0, .25); cursor: pointer; }
.chat-open svg { width: 20px; height: 20px; }
.chat-panel { display: flex; flex-direction: column; width: min(360px, calc(100vw - 32px)); height: min(500px, calc(100vh - 32px)); border: 1px solid var(--line); border-radius: 12px; background: var(--card); box-shadow: 0 6px 22px rgba(0, 0, 0, .25); overflow: hidden; }
.chat-panel[hidden] { display: none; }
.chat-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; background: var(--green-dark); color: #fff; }
.chat-log { flex: 1; display: flex; flex-direction: column; gap: 8px; padding: 12px; overflow-y: auto; }
.chat-log.room { height: 52vh; min-height: 260px; margin: 10px 0; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }
.chat-empty { margin: auto; text-align: center; }
.chat-form { display: flex; gap: 8px; align-items: flex-end; padding: 10px; border-top: 1px solid var(--line); }
.chat-room .chat-form { padding: 0; border: 0; }
.chat-form textarea { margin: 0; resize: none; }
.bubble { max-width: 85%; padding: 6px 10px; border-radius: 10px; background: #ece8da; }
.bubble p { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble.mine { align-self: flex-end; background: #dcefe0; }
.bubble.theirs { align-self: flex-start; }
.bubble-meta { font-size: 12px; color: var(--muted); }
.thread-list { display: grid; gap: 8px; margin-top: 10px; }
.thread { display: block; margin: 0; color: inherit; text-decoration: none; }
.thread:hover { border-color: var(--green); }
.thread.unread { border-left: 4px solid var(--gold); }
.thread-last { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }

/* ผู้ดูแล */
.tabs { display: flex; gap: 4px; padding: 0 16px; background: var(--green-dark); overflow-x: auto; }
.tabs a { padding: 8px 16px; border-radius: 8px 8px 0 0; color: #dfe7df; text-decoration: none; white-space: nowrap; }
.tabs a.on { background: var(--bg); color: var(--ink); font-weight: 500; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; margin: 14px 0; }
.tile { padding: 10px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--card); }
.tile span { display: block; font-size: 14px; color: var(--muted); }
.tile strong { font-size: 28px; font-weight: 600; line-height: 1.25; font-variant-numeric: tabular-nums; }
.grid-2 { display: grid; gap: 16px; grid-template-columns: repeat(2, minmax(0, 1fr)); margin-bottom: 16px; }
@media (max-width: 820px) { .grid-2 { grid-template-columns: minmax(0, 1fr); } }
.grid-2 .card { margin: 0; min-width: 0; }
.inline { margin: 0; } .inline select { min-width: 260px; max-width: 100%; }
.hard { margin: 0; padding-left: 20px; }
.hard li { margin-bottom: 8px; }
.hard li > span { display: block; }
.hard b { font-weight: 500; font-size: 14px; color: var(--warn); }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 15px; }
th { text-align: left; font-weight: 500; color: var(--muted); font-size: 14px; white-space: nowrap; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.form-row { display: flex; gap: 12px; align-items: flex-end; }
.form-row label { margin: 0; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 0 14px; }
.form-grid h3, .form-grid .span, .form-grid .btn { grid-column: 1 / -1; }
.form-grid .btn { justify-self: start; }
.copy { display: flex; gap: 8px; margin-top: 4px; }
.copy input { margin: 0; }
.admin-list { display: grid; gap: 6px; margin: 10px 0; }
.admin-item { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; }
.admin-item b { font-weight: 500; }
.upload { margin-top: 16px; padding-top: 12px; border-top: 1px solid var(--line); }
.import { margin: 10px 0; }
.import summary, details.card > summary { list-style: none; width: fit-content; }
.import summary::-webkit-details-marker, details.card > summary::-webkit-details-marker { display: none; }
.import form, details.card form { margin-top: 12px; }
.q-item { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
.q-item summary { display: flex; gap: 12px; cursor: pointer; }
.q-form { margin: 10px 0; }
.q-choice { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
.q-choice input:not([type=radio]) { margin: 0; }
.check { display: flex; gap: 8px; align-items: center; font-weight: 400; margin-top: 10px; }
.back { display: inline-block; margin-bottom: 10px; }
.pin { margin: 10px 0 0; padding: 8px 12px; background: var(--gold-soft); border-radius: 8px; font-weight: 500; }
.eval { margin-top: 12px; padding-top: 4px; border-top: 1px solid var(--line); }

@media print {
  body { background: #fff; }
  .topbar, .tabs, .no-print, .toast { display: none !important; }
  .card { border: 0; padding: 0; break-inside: avoid; }
  textarea, input { border: 0; padding: 0; }
  .bar-fill { print-color-adjust: exact; -webkit-print-color-adjust: exact; }
  .bar-track { print-color-adjust: exact; -webkit-print-color-adjust: exact; }
}
