:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #faf8f3;
  --ink: #1f2430;
  --ink-2: #4b5263;
  --muted: #7a8090;
  --line: #e4e0d6;
  --accent: #c2620a;        /* สีจีวร */
  --accent-ink: #ffffff;
  --accent-soft: #fbeedd;
  --teal: #0f6e6e;
  --teal-soft: #dcefee;
  --ok: #2f7d32;  --ok-soft: #e3f2e3;
  --warn: #9a6700; --warn-soft: #fdf2d5;
  --bad: #b3261e; --bad-soft: #fbe4e2;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(30, 30, 20, .06), 0 4px 16px rgba(30, 30, 20, .05);
  --sidebar-w: 236px;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: 'IBM Plex Sans Thai', 'Sarabun', system-ui, sans-serif;
  background: var(--bg); color: var(--ink); font-size: 15px; line-height: 1.6;
}
a { color: var(--teal); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0 0 .4em; line-height: 1.3; font-weight: 600; }
h1 { font-size: 1.5rem; } h2 { font-size: 1.2rem; } h3 { font-size: 1.05rem; }
small, .muted { color: var(--muted); }
[hidden] { display: none !important; }

/* ---------- top bar & layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30; height: 56px; display: flex; align-items: center; gap: 12px;
  padding: 0 16px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); font-weight: 600; }
.brand:hover { text-decoration: none; }
.brand-mark {
  display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 8px;
  background: var(--accent); color: #fff; font-weight: 700; font-family: monospace; font-size: 14px;
}
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.who { color: var(--ink-2); font-size: .9rem; }
.menu-btn { display: none; }
.layout { display: flex; min-height: calc(100vh - 56px); }
.sidebar {
  width: var(--sidebar-w); flex: none; background: var(--surface); border-right: 1px solid var(--line);
  padding: 16px 10px; position: sticky; top: 56px; height: calc(100vh - 56px); overflow-y: auto;
}
.sidebar .nav-label { font-size: .75rem; color: var(--muted); padding: 10px 12px 4px; letter-spacing: .02em; }
.sidebar a { display: block; padding: 8px 12px; border-radius: 8px; color: var(--ink-2); }
.sidebar a:hover { background: var(--surface-2); text-decoration: none; }
.sidebar a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.main { flex: 1; min-width: 0; padding: 24px clamp(16px, 3vw, 36px) 64px; max-width: 1280px; }

/* ---------- building blocks ---------- */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head .sub { color: var(--muted); font-size: .92rem; }
.crumbs { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; font-size: .88rem; color: var(--muted); margin-bottom: 10px; }
.crumbs .trail { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; min-width: 0; }
.crumbs .trail a { color: var(--teal); }
.crumbs .trail b { color: var(--ink); font-weight: 600; max-width: 42ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.crumbs .sep { color: var(--line); color: #b9b3a6; }
.crumbs .back-btn { display: inline-flex; align-items: center; height: 30px; padding: 0 12px; border-radius: 99px; background: var(--surface);
  border: 1px solid var(--line); color: var(--ink-2); font-weight: 500; white-space: nowrap; }
.crumbs .back-btn:hover { background: var(--accent-soft); color: var(--accent); border-color: transparent; text-decoration: none; }
@media (max-width: 860px) { .crumbs .trail > a:first-child, .crumbs .trail > .sep:nth-child(2) { display: none; } }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.card + .card { margin-top: 16px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.grid { display: grid; gap: 16px; }
.grid.cols-3 { grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.grid.cols-4 { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); }
.stat { padding: 16px; }
.stat .n { font-size: 1.8rem; font-weight: 700; line-height: 1.1; }
.stat .l { color: var(--muted); font-size: .88rem; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.stack > * + * { margin-top: 12px; }

.course-card { display: block; color: var(--ink); transition: transform .12s, box-shadow .12s; position: relative; overflow: hidden; }
.course-card:hover { text-decoration: none; transform: translateY(-2px); box-shadow: 0 6px 20px rgba(30, 30, 20, .08); }
.course-card::before { content: ''; position: absolute; left: 0; top: 0; bottom: 0; width: 5px; background: var(--accent); }
.course-card.t2::before { background: var(--teal); }
.course-card .code { font-family: monospace; color: var(--muted); font-size: .85rem; }
.course-card .title { font-weight: 600; font-size: 1.05rem; margin: 2px 0 6px; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 38px; padding: 0 16px;
  border-radius: 9px; border: 1px solid transparent; background: var(--accent); color: var(--accent-ink);
  font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.btn:hover { filter: brightness(.95); text-decoration: none; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.secondary { background: var(--surface); color: var(--ink); border-color: var(--line); }
.btn.ghost { background: transparent; color: var(--ink-2); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { background: var(--surface); color: var(--bad); border-color: #f0c6c2; }
.btn.teal { background: var(--teal); }
.btn.sm { height: 30px; padding: 0 10px; font-size: .88rem; border-radius: 7px; }
.icon-btn { background: none; border: 0; font-size: 20px; cursor: pointer; color: var(--ink-2); padding: 4px 8px; }

/* ---------- forms ---------- */
label.field { display: block; }
label.field > span { display: block; font-size: .88rem; color: var(--ink-2); margin-bottom: 4px; font-weight: 500; }
input[type=text], input[type=password], input[type=number], input[type=datetime-local], input[type=url], select, textarea {
  width: 100%; font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px; min-height: 38px;
}
textarea { min-height: 90px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.check { display: flex; gap: 8px; align-items: center; font-size: .92rem; }
.form-grid { display: grid; gap: 12px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.form-grid .full { grid-column: 1 / -1; }
.hint { font-size: .82rem; color: var(--muted); }
input.score { width: 72px; text-align: center; min-height: 32px; padding: 4px; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: top; }
th { background: var(--surface-2); font-weight: 600; color: var(--ink-2); white-space: nowrap; font-size: .85rem; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: center; }
tr.inactive td { color: var(--muted); text-decoration: line-through; }
.gradebook th, .gradebook td { white-space: nowrap; text-align: center; padding: 7px 9px; }
.gradebook td:nth-child(2), .gradebook th:nth-child(2) { text-align: left; position: sticky; left: 0; background: inherit; }
.gradebook tbody tr { background: var(--surface); }
.gradebook th.grp { background: var(--accent-soft); color: var(--accent); }
.gradebook td.total { font-weight: 700; }

/* ---------- badges ---------- */
.badge { display: inline-block; padding: 1px 9px; border-radius: 99px; font-size: .78rem; font-weight: 500; background: var(--surface-2); color: var(--ink-2); border: 1px solid var(--line); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.bad { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.badge.accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.badge.teal { background: var(--teal-soft); color: var(--teal); border-color: transparent; }

/* ---------- tabs ---------- */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 18px; overflow-x: auto; }
.tabs a { padding: 10px 14px; color: var(--ink-2); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs a:hover { text-decoration: none; color: var(--ink); }
.tabs a.active { color: var(--accent); border-color: var(--accent); font-weight: 600; }

/* ---------- unit blocks ---------- */
.unit { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); margin-bottom: 14px; overflow: hidden; }
.unit > summary { list-style: none; cursor: pointer; padding: 14px 18px; display: flex; gap: 12px; align-items: center; }
.unit > summary::-webkit-details-marker { display: none; }
.unit > summary .no { flex: none; width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.unit > summary .t { font-weight: 600; }
.unit > summary .meta { font-size: .82rem; color: var(--muted); }
.unit[open] > summary { border-bottom: 1px solid var(--line); background: var(--surface-2); }
.unit .body { padding: 14px 18px; }
.essence { font-size: .9rem; color: var(--ink-2); background: var(--surface-2); border-left: 3px solid var(--teal); padding: 8px 12px; border-radius: 6px; }
.item { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px dashed var(--line); }
.item:last-child { border-bottom: 0; }
.item .ic { flex: none; width: 32px; height: 32px; border-radius: 8px; display: grid; place-items: center; font-size: 15px; background: var(--surface-2); border: 1px solid var(--line); }
.item .ic.a { background: var(--accent-soft); border-color: transparent; }
.item .ic.q { background: var(--teal-soft); border-color: transparent; }
.item .main-col { flex: 1; min-width: 0; }
.item .t { font-weight: 500; }
.item .m { font-size: .82rem; color: var(--muted); }
.section-label { font-size: .8rem; font-weight: 600; color: var(--muted); margin: 14px 0 2px; letter-spacing: .02em; }

/* ---------- material view ---------- */
.material-body { white-space: pre-wrap; word-break: break-word; }
.embed { position: relative; padding-top: 56.25%; border-radius: 10px; overflow: hidden; background: #000; margin-top: 8px; }
.embed iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }

/* ---------- quiz ---------- */
.q-card { border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px; background: var(--surface); }
.q-card + .q-card { margin-top: 12px; }
.q-card .qno { font-weight: 600; color: var(--accent); }
.choice { display: flex; gap: 10px; align-items: flex-start; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; margin-top: 8px; cursor: pointer; }
.choice:hover { background: var(--surface-2); }
.choice input { margin-top: 5px; }
.choice.right { border-color: var(--ok); background: var(--ok-soft); }
.choice.wrong { border-color: var(--bad); background: var(--bad-soft); }
.timer { position: sticky; top: 64px; z-index: 5; display: inline-block; background: var(--ink); color: #fff; border-radius: 99px; padding: 4px 14px; font-weight: 600; }
.timer.low { background: var(--bad); }
.qedit { border: 1px solid var(--line); border-radius: 10px; padding: 14px; background: var(--surface); margin-bottom: 12px; }
.qedit .choice-row { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.bar { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; min-width: 80px; }
.bar > i { display: block; height: 100%; background: var(--teal); }

/* ---------- groups ---------- */
.group-box { border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: var(--surface); }
.group-box h4 { display: flex; gap: 8px; align-items: center; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 99px; font-size: .85rem; margin: 3px; }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; background:
  radial-gradient(1200px 500px at 10% -10%, var(--accent-soft), transparent),
  radial-gradient(900px 500px at 110% 110%, var(--teal-soft), transparent), var(--bg); }
.login-card { width: 100%; max-width: 400px; padding: 30px; }
.login-card .brand-mark { width: 46px; height: 46px; font-size: 20px; border-radius: 12px; margin-bottom: 14px; }

/* ---------- modal & toast ---------- */
.modal-back { position: fixed; inset: 0; background: rgba(20, 22, 30, .45); z-index: 100; display: grid; place-items: center; padding: 16px; }
.modal { background: var(--surface); border-radius: 14px; width: 100%; max-width: 640px; max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0, 0, 0, .25); }
.modal.wide { max-width: 920px; }
.modal-head { padding: 16px 20px; border-bottom: 1px solid var(--line); display: flex; align-items: center; }
.modal-head h3 { margin: 0; flex: 1; }
.modal-body { padding: 18px 20px; overflow-y: auto; }
.modal-foot { padding: 12px 20px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; }
.toast-root { pointer-events: none; position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 200; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast { background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 10px; box-shadow: var(--shadow); font-size: .92rem; max-width: 90vw; }
.toast.err { background: var(--bad); }
.toast.ok { background: var(--ok); }

.empty { text-align: center; padding: 30px 10px; color: var(--muted); }
.loading { padding: 40px; text-align: center; color: var(--muted); }
.loading::before { content: ''; display: block; width: 26px; height: 26px; margin: 0 auto 10px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.err-box { background: var(--bad-soft); color: var(--bad); padding: 12px 14px; border-radius: 10px; }
.pre { white-space: pre-wrap; word-break: break-word; }

/* ---------- responsive ---------- */
@media (max-width: 860px) {
  .menu-btn { display: inline-block; }
  .sidebar { position: fixed; left: 0; top: 56px; z-index: 40; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  .who { display: none; }
  .form-grid { grid-template-columns: 1fr; }
  .main { padding: 18px 16px 60px; }
}
@media print {
  .topbar, .sidebar, .btn, .tabs { display: none !important; }
  .main { padding: 0; }
  body { background: #fff; }
}
.grid > .card + .card { margin-top: 0; }
@media (max-width: 480px) { #brandName { display: none; } }

/* ---------- บทเรียนย่อย (v1.1) ---------- */
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; padding: 12px 18px; }
.lesson { border: 1px solid var(--line); border-radius: 10px; margin-top: 10px; background: var(--surface); }
.lesson.on { border-left: 4px solid var(--teal); }
.lesson-head { display: flex; gap: 12px; align-items: center; padding: 10px 12px; flex-wrap: wrap; }
.lesson-head .main-col { flex: 1; min-width: 200px; }
.lesson-head .t { font-weight: 600; }
.lesson-head .m { font-size: .84rem; color: var(--muted); }
.lno { flex: none; width: 30px; height: 30px; border-radius: 8px; display: grid; place-items: center; background: var(--surface-2); border: 1px solid var(--line); font-weight: 700; color: var(--ink-2); }
.lesson.on .lno { background: var(--teal-soft); color: var(--teal); border-color: transparent; }
.lesson-tools { display: flex; gap: 2px; flex-wrap: wrap; padding: 0 8px 8px 50px; }
.lesson-items { padding: 0 14px 6px 54px; border-top: 1px dashed var(--line); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { font: inherit; font-size: .82rem; border: 0; background: var(--surface); color: var(--ink-2); padding: 4px 10px; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--teal); color: #fff; }
.plan-row { display: grid; grid-template-columns: 130px 1fr; gap: 12px; padding: 8px 0; border-bottom: 1px dashed var(--line); }
.plan-k { font-weight: 600; color: var(--ink-2); }
.plan-v { white-space: pre-wrap; }
.rubric-edit { border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: var(--surface-2); }
.rb-row { display: grid; grid-template-columns: 1.2fr 70px 2fr auto; gap: 6px; margin-top: 6px; align-items: center; }
.rb-grade { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.rb-grade label { display: flex; flex-direction: column; align-items: center; font-size: .78rem; background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px; padding: 3px 6px; max-width: 110px; text-align: center; }
.rb-grade select { min-height: 28px; padding: 2px 4px; width: 52px; }
table.track th, table.track td { text-align: center; padding: 6px 7px; white-space: nowrap; }
table.track td:nth-child(2), table.track th:nth-child(2) { text-align: left; }
table.track th.grp { background: var(--accent-soft); color: var(--accent); }
table.track .dim { opacity: .38; }
table.track tr.sum td { background: var(--surface-2); }
.cell { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 6px; font-weight: 700; font-size: .9rem; }
.cell.ok { background: var(--ok-soft); color: var(--ok); }
.cell.bad { background: var(--bad-soft); color: var(--bad); }
.cell.teal { background: var(--teal-soft); color: var(--teal); }
.cell.warn { background: var(--warn-soft); color: var(--warn); }
.cell.muted { color: var(--muted); }
.legend { font-size: .82rem; color: var(--ink-2); }
a.chip:hover { text-decoration: none; background: var(--accent-soft); }
.stretch { position: absolute; inset: 0; z-index: 0; }
.continue { position: relative; z-index: 1; display: block; margin-top: 10px; padding-top: 8px; border-top: 1px dashed var(--line); font-weight: 600; color: var(--teal); }
a.lesson-link { color: var(--ink); border-radius: 8px; }
a.lesson-link:hover { text-decoration: none; background: var(--surface-2); }
a.lesson-link.current { background: var(--teal-soft); }
.lesson-page { display: grid; gap: 14px; }
.goal { background: var(--accent-soft); border-radius: 12px; padding: 14px 18px; font-size: 1.05rem; }
.lesson-sec { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 14px 18px; }
.lesson-sec h3 { margin-bottom: 6px; }
.lesson-sec.reflect { background: var(--teal-soft); border-color: transparent; }
.material + .material { margin-top: 14px; padding-top: 12px; border-top: 1px dashed var(--line); }
.hist { border-top: 1px dashed var(--line); padding: 8px 0; }
@media (max-width: 860px) {
  .lesson-tools, .lesson-items { padding-left: 12px; }
  .rb-row { grid-template-columns: 1fr 60px; }
  .rb-row input[data-rb=desc] { grid-column: 1 / -1; }
  .plan-row { grid-template-columns: 1fr; gap: 2px; }
}

/* ---------- ปีการศึกษา / ภาคเรียน ---------- */
.badge.termyear { font-weight: 600; font-size: .82rem; padding: 2px 11px; }
.badge.termyear.t1 { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.badge.termyear.t2 { background: var(--teal-soft); color: var(--teal); border-color: transparent; }
.term-head { margin: 22px 0 10px; }
.term-head .badge.termyear { font-size: .95rem; padding: 4px 14px; }
.sidebar .nav-term { font-size: .74rem; font-weight: 600; color: var(--ink-2); padding: 8px 12px 2px 14px; }
.sidebar a.sub { padding: 6px 12px 6px 22px; font-size: .92rem; }
tr.group-row td { background: var(--surface-2); padding: 8px 12px; }
.sec-preview { margin-top: 14px; padding: 12px; border-radius: 10px; background: var(--surface-2); border: 1px dashed var(--line); }
.sec-preview:empty { display: none; }
