:root {
  --brand: #2f5bea;
  --brand-soft: color-mix(in srgb, var(--brand) 10%, #fff);
  --brand-line: color-mix(in srgb, var(--brand) 28%, #fff);
  --brand-ink: color-mix(in srgb, var(--brand) 82%, #000);
  --bg: #f3f5f9;
  --card: #fff;
  --ink: #131a2b;
  --muted: #5e6779;
  --faint: #8a93a5;
  --line: #e2e6ee;
  --ok: #138a52;
  --ok-soft: #e5f6ed;
  --warn: #b5650d;
  --warn-soft: #fdf1e2;
  --bad: #cc3434;
  --bad-soft: #fdecec;
  --radius: 18px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 6px 22px rgba(16, 24, 40, .06);
  --font: 'Noto Sans Thai', 'Leelawadee UI', 'Segoe UI', Tahoma, sans-serif;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font-family: var(--font); background: var(--bg); color: var(--ink); font-size: 16px; line-height: 1.6; }
a { color: var(--brand); }
img { max-width: 100%; }
h1, h2, h3, h4 { line-height: 1.3; margin: 0 0 .4em; }
h1 { font-size: 1.75rem; }
h2 { font-size: 1.35rem; }
h3 { font-size: 1.1rem; }
p { margin: 0 0 .8em; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.small { font-size: .875rem; }
.tiny { font-size: .78rem; }
.nowrap { white-space: nowrap; }
.hidden { display: none !important; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack > * + * { margin-top: 12px; }
.grow { flex: 1 1 auto; min-width: 0; }
.mono { font-family: ui-monospace, Consolas, monospace; letter-spacing: .02em; }

/* buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font: inherit; font-weight: 600; font-size: .95rem; line-height: 1.2;
  padding: 11px 18px; border-radius: 12px; border: 1px solid transparent;
  background: var(--brand); color: #fff; cursor: pointer; text-decoration: none;
  transition: filter .15s, background .15s, box-shadow .15s; white-space: nowrap;
}
.btn:hover { filter: brightness(1.06); }
.btn:focus-visible, .input:focus-visible, .chip:focus-visible { outline: 3px solid var(--brand-line); outline-offset: 2px; }
.btn:disabled { opacity: .55; cursor: not-allowed; filter: none; }
.btn.ghost { background: #fff; color: var(--ink); border-color: var(--line); }
.btn.ghost:hover { background: #f7f8fb; filter: none; }
.btn.soft { background: var(--brand-soft); color: var(--brand-ink); }
.btn.danger { background: #fff; color: var(--bad); border-color: #f2c4c4; }
.btn.danger:hover { background: var(--bad-soft); filter: none; }
.btn.ok { background: var(--ok); }
.btn.sm { padding: 7px 12px; font-size: .85rem; border-radius: 10px; }
.btn.xs { padding: 4px 9px; font-size: .8rem; border-radius: 8px; font-weight: 500; }
.btn.block { width: 100%; }
.btn.lg { padding: 15px 22px; font-size: 1.05rem; border-radius: 14px; }
.icon-btn { background: none; border: 0; padding: 6px; border-radius: 8px; cursor: pointer; color: var(--muted); font: inherit; }
.icon-btn:hover { background: #eef0f5; color: var(--ink); }

/* forms */
.field { display: block; }
.field > span { display: flex; justify-content: space-between; gap: 8px; font-weight: 600; font-size: .92rem; margin-bottom: 6px; }
.field > span small { font-weight: 400; color: var(--faint); }
.input, select.input, textarea.input {
  width: 100%; font: inherit; font-size: 1rem; color: var(--ink);
  padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; background: #fff;
}
.input:focus { border-color: var(--brand); outline: none; box-shadow: 0 0 0 3px var(--brand-soft); }
textarea.input { min-height: 96px; resize: vertical; line-height: 1.55; }
.input.lg { padding: 15px 18px; border-radius: 14px; font-size: 1.05rem; }
.check { display: flex; gap: 10px; align-items: flex-start; cursor: pointer; font-size: .95rem; }
.check input { width: 18px; height: 18px; margin-top: 3px; accent-color: var(--brand); flex: none; }
.form-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.err { color: var(--bad); font-size: .9rem; min-height: 1.2em; }

/* cards */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid #edf0f5; }
.card.pad { padding: 22px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.card-head h3 { margin: 0; }
.card-body { padding: 18px 20px; }

.badge { display: inline-flex; align-items: center; gap: 4px; font-size: .78rem; font-weight: 600; padding: 3px 9px; border-radius: 999px; background: #eef0f5; color: var(--muted); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.brand { background: var(--brand-soft); color: var(--brand-ink); }

.bar { height: 8px; border-radius: 99px; background: #e9ecf3; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--brand); border-radius: 99px; transition: width .4s; }
.bar.ok > i { background: var(--ok); }

.alert { padding: 12px 16px; border-radius: 12px; font-size: .93rem; background: var(--brand-soft); color: var(--brand-ink); }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.bad { background: var(--bad-soft); color: var(--bad); }
.alert.ok { background: var(--ok-soft); color: var(--ok); }

/* brand mark */
.logo-mark { width: 44px; height: 44px; border-radius: 50%; background: var(--brand); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 1.2rem; flex: none; overflow: hidden; }
.logo-mark img { width: 100%; height: 100%; object-fit: cover; }
.logo-mark.sm { width: 34px; height: 34px; font-size: 1rem; }

/* top bar */
.topbar { position: sticky; top: 0; z-index: 20; background: rgba(255, 255, 255, .92); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.topbar-in { max-width: 1240px; margin: 0 auto; padding: 10px 20px; display: flex; align-items: center; gap: 12px; }
.brand-link { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--ink); font-weight: 700; min-width: 0; }
.brand-link span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wrap { max-width: 1240px; margin: 0 auto; padding: 24px 20px 60px; }

/* tables */
.table-wrap { overflow-x: auto; }
table.table { width: 100%; border-collapse: collapse; font-size: .93rem; }
.table th { text-align: left; font-weight: 600; color: var(--muted); font-size: .82rem; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; background: #fafbfd; }
.table td { padding: 11px 12px; border-bottom: 1px solid #eef1f5; vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table tr.click { cursor: pointer; }
.table tr.click:hover td { background: #f8f9fc; }

/* modal */
.modal-back { position: fixed; inset: 0; background: rgba(15, 20, 35, .45); z-index: 100; display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; overflow-y: auto; }
.modal { background: #fff; border-radius: 20px; width: 100%; max-width: 560px; box-shadow: 0 20px 60px rgba(0, 0, 0, .25); }
.modal.wide { max-width: 860px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 18px 22px; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0; }
.modal-body { padding: 20px 22px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 22px 20px; flex-wrap: wrap; }

/* toast */
#toasts { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 300; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast { background: #1d2333; color: #fff; padding: 11px 18px; border-radius: 12px; font-size: .93rem; box-shadow: 0 8px 24px rgba(0, 0, 0, .2); animation: pop .2s ease-out; max-width: 90vw; }
.toast.bad { background: var(--bad); }
.toast.ok { background: var(--ok); }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } }

.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty .big { font-size: 2.4rem; margin-bottom: 6px; }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 3px solid #dfe3ec; border-top-color: var(--brand); animation: spin .8s linear infinite; margin: 60px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ============ login ============ */
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 560px; padding: 44px 56px; }
.login-card .brand-link { font-size: 1.15rem; margin-bottom: 28px; }
.login-card .logo-mark { width: 46px; height: 46px; }
.eyebrow { color: var(--brand); font-weight: 600; font-size: .92rem; margin-bottom: 2px; }
.login-card h1 { font-size: 2rem; margin-bottom: 4px; }
.login-school { color: var(--brand); font-weight: 600; font-size: 1.05rem; margin-bottom: 10px; }
.login-note { color: var(--muted); font-size: .93rem; margin-bottom: 24px; }
.login-foot { margin-top: 24px; padding-top: 18px; border-top: 1px solid var(--line); color: var(--muted); font-size: .88rem; text-align: center; }
.pin-input { letter-spacing: .35em; font-size: 1.15rem; }
@media (max-width: 560px) { .login-card { padding: 30px 22px; } .login-card h1 { font-size: 1.6rem; } }

/* ============ student: courses ============ */
.hello { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 22px; }
.hello h1 { margin: 0; }
.course-grid { display: grid; gap: 20px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.course-card { display: flex; flex-direction: column; overflow: hidden; text-decoration: none; color: inherit; transition: transform .15s, box-shadow .15s; }
.course-card:hover { transform: translateY(-2px); box-shadow: 0 10px 30px rgba(16, 24, 40, .1); }
.course-card.off { opacity: .7; }
.cover { aspect-ratio: 16 / 9; background: linear-gradient(135deg, var(--brand), color-mix(in srgb, var(--brand) 55%, #7b2ff7)); position: relative; display: grid; place-items: center; color: #fff; overflow: hidden; }
.cover img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.cover .cover-letter { font-size: 2.6rem; font-weight: 700; opacity: .9; }
.cover .badge { position: absolute; top: 12px; left: 12px; }
.course-card .body { padding: 16px 18px 18px; display: flex; flex-direction: column; gap: 8px; flex: 1; }
.course-card h3 { margin: 0; font-size: 1.08rem; }
.course-card .meta { display: flex; justify-content: space-between; font-size: .85rem; color: var(--muted); }
.course-card .actions { margin-top: auto; padding-top: 6px; }

/* ============ student: course player ============ */
.crumbs { display: flex; gap: 8px; align-items: center; font-size: .9rem; color: var(--muted); margin-bottom: 14px; flex-wrap: wrap; }
.crumbs a { color: var(--muted); text-decoration: none; }
.crumbs a:hover { color: var(--brand); }
.learn { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 22px; align-items: start; }
.player { aspect-ratio: 16 / 9; background: #0d1018; border-radius: var(--radius); overflow: hidden; position: relative; }
.player iframe, .player > div { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }
.no-video { border-radius: var(--radius); background: linear-gradient(135deg, var(--brand), color-mix(in srgb, var(--brand) 60%, #000)); color: #fff; display: flex; align-items: center; gap: 16px; padding: 24px 26px; }
.no-video .ico { font-size: 2.2rem; }
.lesson-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin: 18px 0 6px; flex-wrap: wrap; }
.lesson-head h1 { font-size: 1.45rem; margin: 0; }
.lesson-body { white-space: pre-wrap; overflow-wrap: anywhere; }
.block-title { font-weight: 700; margin: 0 0 10px; display: flex; gap: 8px; align-items: center; }
.file-list { display: flex; flex-direction: column; gap: 8px; }
.file-item { display: flex; align-items: center; gap: 12px; padding: 11px 14px; border: 1px solid var(--line); border-radius: 12px; text-decoration: none; color: var(--ink); background: #fff; }
.file-item:hover { border-color: var(--brand-line); background: var(--brand-soft); }
.file-item .ico { width: 36px; height: 36px; border-radius: 10px; background: var(--brand-soft); display: grid; place-items: center; flex: none; }
.lesson-nav { display: flex; justify-content: space-between; gap: 10px; margin-top: 22px; flex-wrap: wrap; }
.lesson-nav .btn { flex: 1 1 160px; }

.outline { position: sticky; top: 76px; max-height: calc(100vh - 96px); display: flex; flex-direction: column; overflow: hidden; }
.outline-head { padding: 18px 18px 14px; border-bottom: 1px solid var(--line); }
.outline-list { overflow-y: auto; padding: 6px 0 10px; }
.sec-title { font-size: .82rem; font-weight: 700; color: var(--muted); padding: 12px 18px 6px; text-transform: none; }
.lesson-link { display: flex; gap: 10px; align-items: flex-start; padding: 9px 18px; text-decoration: none; color: var(--ink); font-size: .93rem; border-left: 3px solid transparent; }
.lesson-link:hover { background: #f6f7fb; }
.lesson-link.active { background: var(--brand-soft); border-left-color: var(--brand); font-weight: 600; }
.lesson-link.locked { color: var(--faint); }
.lesson-link .dot { width: 22px; height: 22px; border-radius: 50%; border: 2px solid #cfd5e1; flex: none; display: grid; place-items: center; font-size: .72rem; margin-top: 1px; background: #fff; }
.lesson-link.done .dot { background: var(--ok); border-color: var(--ok); color: #fff; }
.lesson-link .t { flex: 1; min-width: 0; }
.lesson-link .sub { display: block; font-size: .78rem; color: var(--faint); font-weight: 400; }
.done-banner { display: flex; gap: 14px; align-items: center; padding: 16px 18px; border-radius: 14px; background: var(--ok-soft); color: #0d5c37; margin-bottom: 18px; flex-wrap: wrap; }
.done-banner .ico { font-size: 1.8rem; }

/* quiz */
.quiz-q { padding: 16px 0; border-top: 1px solid var(--line); }
.quiz-q:first-of-type { border-top: 0; padding-top: 4px; }
.quiz-q .q { font-weight: 600; margin-bottom: 10px; white-space: pre-wrap; }
.choice { display: flex; gap: 10px; align-items: flex-start; padding: 10px 14px; border: 1px solid var(--line); border-radius: 12px; margin-bottom: 8px; cursor: pointer; background: #fff; }
.choice:hover { border-color: var(--brand-line); }
.choice input { accent-color: var(--brand); width: 18px; height: 18px; margin-top: 3px; flex: none; }
.choice.right { border-color: #9ad7b6; background: var(--ok-soft); }
.choice.wrong { border-color: #f0b4b4; background: var(--bad-soft); }
.quiz-result { padding: 14px 16px; border-radius: 12px; margin-bottom: 12px; font-weight: 600; }
.quiz-result.pass { background: var(--ok-soft); color: var(--ok); }
.quiz-result.fail { background: var(--bad-soft); color: var(--bad); }

@media (max-width: 980px) {
  .learn { grid-template-columns: 1fr; }
  .outline { position: static; max-height: none; }
  .outline-list { max-height: 420px; }
}
@media (max-width: 480px) { .acct-name { display: none; } }
@media (max-width: 600px) {
  .wrap { padding: 16px 14px 50px; }
  .topbar-in { padding: 8px 14px; }
  .player { border-radius: 12px; margin: 0 -14px; border-radius: 0; }
  .card.pad { padding: 18px; }
  h1 { font-size: 1.45rem; }
}

/* ============ admin ============ */
.admin-shell { display: grid; grid-template-columns: 236px minmax(0, 1fr); min-height: 100vh; }
.side { background: #fff; border-right: 1px solid var(--line); padding: 18px 12px; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 4px; }
.side .brand-link { padding: 4px 8px 18px; }
.nav-link { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 10px; color: var(--ink); text-decoration: none; font-weight: 500; font-size: .95rem; }
.nav-link:hover { background: #f3f5f9; }
.nav-link.active { background: var(--brand-soft); color: var(--brand-ink); font-weight: 700; }
.nav-link .ni { width: 22px; text-align: center; }
.side-foot { margin-top: auto; display: flex; flex-direction: column; gap: 4px; }
.admin-main { padding: 26px 30px 70px; max-width: 1180px; width: 100%; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 14px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 1.6rem; }
.stats { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); margin-bottom: 22px; }
.stat { padding: 18px 20px; }
.stat .n { font-size: 1.9rem; font-weight: 700; line-height: 1.1; }
.stat .l { color: var(--muted); font-size: .88rem; }
.two-col { display: grid; gap: 20px; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }
.tabs { display: flex; gap: 4px; box-shadow: inset 0 -1px 0 var(--line); margin-bottom: 20px; overflow-x: auto; overflow-y: hidden; scrollbar-width: none; }
.tab { padding: 10px 16px; border: 0; background: none; font: inherit; font-weight: 600; color: var(--muted); cursor: pointer; border-bottom: 3px solid transparent; white-space: nowrap; }
.tab.active { color: var(--brand-ink); border-bottom-color: var(--brand); }
.sec-card { margin-bottom: 16px; }
.sec-card .card-head { background: #fafbfd; border-radius: var(--radius) var(--radius) 0 0; }
.lesson-row { display: flex; align-items: center; gap: 12px; padding: 11px 20px; border-bottom: 1px solid #eef1f5; }
.lesson-row:last-child { border-bottom: 0; }
.lesson-row .ico { width: 34px; height: 34px; border-radius: 10px; background: var(--brand-soft); display: grid; place-items: center; flex: none; }
.lesson-row .t { flex: 1; min-width: 0; }
.lesson-row .t b { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mini-actions { display: flex; gap: 2px; flex: none; }
.course-row { display: flex; gap: 16px; align-items: center; padding: 14px 18px; border-bottom: 1px solid #eef1f5; }
.course-row:last-child { border-bottom: 0; }
.course-row .thumb { width: 112px; aspect-ratio: 16/9; border-radius: 10px; flex: none; }
.course-row .thumb .cover-letter { font-size: 1.4rem; }
.yt-preview { display: flex; gap: 12px; align-items: center; margin-top: 8px; }
.yt-preview img { width: 140px; border-radius: 8px; }
.q-card { border: 1px solid var(--line); border-radius: 14px; padding: 14px; margin-bottom: 12px; background: #fcfcfe; }
.q-choice { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
.q-choice input[type=radio] { width: 18px; height: 18px; accent-color: var(--ok); flex: none; }
.cred-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }
.cred { border: 1.5px dashed #c7cedb; border-radius: 14px; padding: 14px 16px; background: #fff; break-inside: avoid; }
.cred .school { font-weight: 700; font-size: .9rem; color: var(--brand-ink); }
.cred .nm { font-weight: 600; margin: 4px 0 8px; }
.cred .kv { display: flex; justify-content: space-between; font-size: .95rem; }
.cred .kv b { font-family: ui-monospace, Consolas, monospace; font-size: 1.05rem; letter-spacing: .04em; }
.cred .url { font-size: .72rem; color: var(--muted); margin-top: 8px; word-break: break-all; }
.color-row { display: flex; gap: 10px; align-items: center; }
.color-row input[type=color] { width: 52px; height: 44px; border: 1px solid var(--line); border-radius: 10px; padding: 3px; background: #fff; cursor: pointer; }
.logo-prev { width: 64px; height: 64px; }
.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; }
.switch input { width: 42px; height: 24px; appearance: none; background: #d3d8e2; border-radius: 99px; position: relative; cursor: pointer; transition: background .2s; flex: none; }
.switch input::after { content: ''; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform .2s; box-shadow: 0 1px 3px rgba(0, 0, 0, .2); }
.switch input:checked { background: var(--brand); }
.switch input:checked::after { transform: translateX(18px); }
.mobile-nav { display: none; }

@media (max-width: 900px) {
  .admin-shell { grid-template-columns: 1fr; }
  .side { display: none; }
  .mobile-nav { display: flex; position: sticky; top: 0; z-index: 30; background: #fff; border-bottom: 1px solid var(--line); overflow-x: auto; gap: 2px; padding: 8px 10px; }
  .mobile-nav .nav-link { padding: 8px 12px; font-size: .88rem; white-space: nowrap; }
  .admin-main { padding: 18px 14px 60px; }
  .course-row { flex-wrap: wrap; }
  .course-row .thumb { width: 96px; }
}

/* print: บัตรเข้าเรียน */
@media print {
  body.printing > *:not(.modal-back) { display: none !important; }
  body.printing .modal-back { position: static; background: none; padding: 0; overflow: visible; }
  body.printing .modal { box-shadow: none; max-width: none; border-radius: 0; }
  body.printing .modal-head, body.printing .modal-foot, body.printing .no-print { display: none !important; }
  body.printing .modal-body { padding: 0; }
  body.printing .cred-grid { grid-template-columns: repeat(3, 1fr); }
}
