:root {
  --navy: #1B2A4A;
  --gold: #C9A227;
  --bg: #f4f5f8;
  --card: #ffffff;
  --text: #1c2333;
  --muted: #6b7385;
  --line: #dfe3ea;
  --ok: #1e7b4b;
  --warn: #b26a00;
  --err: #b3261e;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: 'Sarabun', 'TH Sarabun New', Tahoma, sans-serif;
  font-size: 17px; line-height: 1.55;
  background: var(--bg); color: var(--text);
  min-height: 100vh; display: flex; flex-direction: column;
}
.topbar {
  background: var(--navy); color: #fff;
  padding: .7rem 1.2rem; display: flex; flex-wrap: wrap;
  align-items: center; justify-content: space-between; gap: .6rem;
  border-bottom: 3px solid var(--gold);
}
.brand { color: #fff; text-decoration: none; font-weight: 700; font-size: 1.2rem; display: flex; align-items: center; gap: .6rem; line-height: 1.2; }
.brand span { display: flex; flex-direction: column; }
.brand img { height: 44px; width: auto; max-width: 70px; object-fit: contain; background: #fff; border-radius: 8px; padding: 2px; }
.brand small { font-weight: 400; font-size: .8rem; color: #ffffffbb; }
.userbox { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; }
.userbox .who { font-weight: 600; color: #fff; text-decoration: none; }
.userbox a.who:hover { text-decoration: underline; }
.badge { background: var(--gold); color: var(--navy); border-radius: 999px; padding: .05rem .7rem; font-size: .8rem; font-weight: 700; }
.container { flex: 1; width: 100%; max-width: 1000px; margin: 1.2rem auto; padding: 0 1rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 1.2rem 1.4rem; margin-bottom: 1rem; }
.card h2 { margin: 0 0 .5rem; color: var(--navy); font-size: 1.25rem; }
.card h3 { margin: 0 0 .3rem; color: var(--navy); font-size: 1.05rem; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 1rem; }
.stat { text-align: center; }
.stat .num { font-size: 2rem; font-weight: 700; color: var(--navy); }
.stat .label { color: var(--muted); }
.muted { color: var(--muted); }
.soon { opacity: .6; }
.tag { display: inline-block; font-size: .75rem; background: #eef0f5; color: var(--muted); border-radius: 6px; padding: 0 .5rem; margin-left: .4rem; }
.form-row { margin-bottom: .9rem; }
label { display: block; font-weight: 600; margin-bottom: .25rem; }
input[type=text], input[type=password], input[type=search] {
  width: 100%; padding: .55rem .7rem; border: 1px solid #c3c9d6; border-radius: 8px;
  font: inherit; background: #fff;
}
input:focus { outline: 2px solid var(--gold); border-color: var(--gold); }
.btn { font: inherit; font-weight: 600; cursor: pointer; border: 0; border-radius: 8px; padding: .55rem 1.2rem; background: var(--navy); color: #fff; }
.btn:hover { filter: brightness(1.18); }
.btn-ghost { background: transparent; color: #fff; border: 1px solid #ffffff66; padding: .25rem .8rem; }
.btn-ghost:hover { background: #ffffff22; }
.inline { display: inline; margin: 0; }
.alert { padding: .7rem 1rem; border-radius: 8px; margin-bottom: 1rem; border: 1px solid; }
.alert-error { background: #fdecea; border-color: #f3b8b3; color: var(--err); }
.alert-ok { background: #e8f5ee; border-color: #a9d9bf; color: var(--ok); }
.alert-warn { background: #fff4e0; border-color: #f0cf94; color: var(--warn); }
.narrow { max-width: 460px; margin: 2rem auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: .45rem .6rem; border-bottom: 1px solid var(--line); text-align: left; }
th { color: var(--navy); }
.footer { text-align: center; color: var(--muted); font-size: .85rem; padding: 1rem; }
code { background: #eef0f5; padding: 0 .3rem; border-radius: 4px; }

/* ---------- เมนู ---------- */
.mainnav { background: #fff; border-bottom: 1px solid var(--line); display: flex; flex-wrap: wrap; gap: .2rem; padding: .3rem 1rem; justify-content: center; }
.mainnav a { color: var(--navy); text-decoration: none; padding: .35rem .8rem; border-radius: 8px; font-weight: 600; font-size: .95rem; }
.mainnav a:hover { background: #eef0f5; }
.mainnav a.active { background: var(--navy); color: #fff; }
.container.wide { max-width: 1250px; }

/* ---------- ฟอร์ม ---------- */
select, input[type=number], input[type=date], input[type=time], input[type=file], textarea {
  padding: .45rem .6rem; border: 1px solid #c3c9d6; border-radius: 8px; font: inherit; background: #fff; max-width: 100%;
}
textarea { width: 100%; min-height: 6rem; }
select:focus, textarea:focus { outline: 2px solid var(--gold); border-color: var(--gold); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: .8rem 1rem; align-items: end; }
.form-grid .form-row { margin: 0; }
.form-grid input[type=text], .form-grid input[type=password], .form-grid select, .form-grid input[type=date], .form-grid input[type=number] { width: 100%; }
.toolbar { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin-bottom: 1rem; }
.toolbar .spacer { flex: 1; }
.toolbar form { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin: 0; }
.checks { display: flex; flex-wrap: wrap; gap: .3rem 1.2rem; }
.checks label { font-weight: 400; display: inline-flex; gap: .35rem; align-items: center; margin: 0; }
label.plain { font-weight: 400; display: inline-flex; gap: .35rem; align-items: center; }

/* ---------- ปุ่ม ---------- */
a.btn { display: inline-block; text-decoration: none; }
.btn-sm { padding: .25rem .7rem; font-size: .9rem; }
.btn-outline { background: #fff; color: var(--navy); border: 1px solid var(--navy); }
.btn-outline:hover { background: #eef0f5; filter: none; }
.btn-gold { background: var(--gold); color: var(--navy); }
.btn-ok { background: var(--ok); }
.btn-danger { background: var(--err); }
.btn[disabled] { opacity: .45; cursor: not-allowed; }
.actions { display: flex; flex-wrap: wrap; gap: .3rem; align-items: center; }
.actions form { margin: 0; }

/* ---------- ตาราง ---------- */
.table-wrap { overflow-x: auto; }
table.list th { background: #f6f7fa; font-size: .92rem; white-space: nowrap; }
table.list td { vertical-align: top; }
table.list tr:hover td { background: #fafbfd; }
table.list .num { text-align: right; white-space: nowrap; }
table.list .c { text-align: center; }
tr.inactive td { color: var(--muted); text-decoration: line-through; }
tr.warn td { background: #fff8ec; }

/* ---------- ป้ายสถานะ ---------- */
.pill { display: inline-block; border-radius: 999px; padding: 0 .65rem; font-size: .82rem; font-weight: 700; white-space: nowrap; }
.pill-draft  { background: #eef0f5; color: var(--muted); }
.pill-open   { background: #e8f5ee; color: var(--ok); }
.pill-closed { background: #fdecea; color: var(--err); }
.pill-ok     { background: #e8f5ee; color: var(--ok); }
.pill-warn   { background: #fff4e0; color: var(--warn); }
.pill-new    { background: #e7effd; color: #1d4ed8; }
.pill-old    { background: #fdecea; color: var(--err); }
.vlist { margin: 0; padding-left: 1.1rem; font-size: .93rem; }
.vlist li { margin: .1rem 0; }
.vlist .act { color: var(--muted); font-size: .85rem; }
.vlist .noact { color: var(--warn); font-size: .85rem; }
.okmark { color: var(--ok); font-weight: 600; }

/* ---------- การ์ดห้อง/รอบ ---------- */
.room-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: .8rem; }
.room-card { border: 1px solid var(--line); border-radius: 10px; padding: .8rem 1rem; background: #fff; }
.room-card h3 { margin: 0 0 .3rem; }
.room-card.nott { border-color: #f0cf94; background: #fff8ec; }
a.room-card { display: block; text-decoration: none; color: var(--navy); }
a.room-card:hover { border-color: var(--gold); }
/* ---------- เลือกระดับ / รอบ / ห้อง (หน้าตรวจระเบียบ) ---------- */
.picker .toolbar { margin-bottom: .6rem; }
.pick-row { display: flex; align-items: flex-start; gap: .8rem; padding: .6rem 0; border-top: 1px solid var(--line); }
.pick-label { flex: none; width: 5.5rem; padding-top: .3rem; color: var(--muted); font-size: .92rem; }
.seg { display: inline-flex; flex-wrap: wrap; background: #eef0f5; border-radius: 10px; padding: 3px; gap: 2px; }
.seg a { position: relative; padding: .3rem 1rem; border-radius: 8px; color: var(--navy); text-decoration: none; font-weight: 600; }
.seg a:hover { background: #fff8; }
.seg a.on { background: var(--navy); color: #fff; box-shadow: 0 1px 3px #0002; }
.seg .dot { position: absolute; top: 5px; right: 5px; width: 7px; height: 7px; border-radius: 50%; background: var(--ok); }
.chips { display: flex; flex-wrap: wrap; gap: .4rem; }
.chip { display: inline-flex; align-items: center; gap: .45rem; padding: .3rem .8rem; border: 1px solid var(--line); border-radius: 999px;
        color: var(--navy); text-decoration: none; font-size: .95rem; background: #fff; }
.chip:hover { border-color: var(--gold); }
.chip.on { border-color: var(--navy); background: var(--navy); color: #fff; }
.chip .st { font-size: .75rem; font-weight: 700; border-radius: 999px; padding: 0 .5rem; }
/* ปุ่มเลือกรอบ: ชื่อช่วงตรวจ + วันที่ + ความคืบหน้า */
.round-chip { flex-direction: column; align-items: stretch; gap: .1rem; border-radius: 10px; padding: .45rem .85rem .5rem; min-width: 11rem; }
.rc-title { font-weight: 700; display: flex; align-items: center; gap: .35rem; }
.rc-title small { font-weight: 400; opacity: .7; }
.rc-sub { font-size: .82rem; color: var(--muted); }
.rc-bar { display: block; height: 4px; background: #eef0f5; border-radius: 999px; overflow: hidden; margin-top: .2rem; }
.rc-bar span { display: block; height: 100%; background: var(--ok); }
.round-chip.on .rc-sub { color: #ffffffcc; }
.round-chip.on .rc-bar { background: #ffffff33; }
.round-chip.on .rc-bar span { background: var(--gold); }
.round-chip.is-closed:not(.on) { background: #fafbfc; }
.round-chip.on .st-closed { background: #ffffff26; color: #fff; }
.round-chip.term-chip:not(.on) { border-style: dashed; border-color: var(--gold); background: #fffbef; }
.term-box { display: grid; grid-template-columns: repeat(auto-fit, minmax(17rem, 1fr)); gap: .8rem; }
.term-box > div { border: 1px solid #e3e6ee; border-radius: 10px; padding: .7rem .9rem; }
.term-box h3 { margin: 0 0 .3rem; display: flex; align-items: center; gap: .5rem; }
.term-box p { margin: .2rem 0; font-size: .92rem; }
.term-box .actions { margin-top: .5rem; }
.st-open { background: #e8f5ee; color: var(--ok); }
.st-closed { background: #eef0f5; color: var(--muted); }
.progress { height: 6px; background: #eef0f5; border-radius: 999px; overflow: hidden; margin-bottom: 1rem; }
.progress span { display: block; height: 100%; background: var(--ok); border-radius: 999px; }
.room-tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .6rem; }
.room-tile { display: flex; flex-direction: column; gap: .1rem; padding: .7rem .9rem; border: 1px solid var(--line); border-left: 4px solid var(--warn);
             border-radius: 10px; text-decoration: none; color: var(--text); background: #fff; transition: border-color .15s, box-shadow .15s; }
.room-tile:hover { border-color: var(--gold); box-shadow: 0 2px 8px #1b2a4a14; }
.room-tile.is-done { border-left-color: var(--ok); background: #f6fbf8; }
.rt-name { font-size: 1.2rem; font-weight: 700; color: var(--navy); }
.rt-state { font-size: .85rem; font-weight: 600; color: var(--warn); }
.is-done .rt-state { color: var(--ok); }
.room-tile.is-empty { border-left-color: var(--line); background: #fafbfc; }
.is-empty .rt-state, .is-empty .rt-name { color: var(--muted); }
.rt-count { font-size: .82rem; color: var(--muted); }
.pick-row .search-box { display: flex; gap: .4rem; flex: 1; max-width: 26rem; }
.pick-row form, form.pick-row { margin: 0; }
.level-warn .seg .dot { background: var(--warn); }
/* มอบหมายครู */
.assign-table td { vertical-align: middle; }
.assign-table .room { white-space: nowrap; color: var(--navy); }
.assign-table .tchip { margin: .1rem .25rem .1rem 0; }
.no-teacher { color: var(--warn); font-weight: 600; font-size: .92rem; }
.tchip { display: inline-flex; align-items: center; gap: .2rem; margin: 0; background: #eef0f5; border-radius: 999px; padding: .1rem .2rem .1rem .7rem; font-size: .92rem; }
.tchip button { border: 0; background: transparent; cursor: pointer; color: var(--muted); border-radius: 50%; width: 1.6rem; height: 1.6rem; font-size: .8rem; }
.tchip button:hover { background: #fdecea; color: var(--err); }
.add-teacher { display: flex; gap: .3rem; margin: 0; }
.add-teacher select { min-width: 0; flex: 1; padding: .25rem .5rem; }

/* ---------- SweetAlert2 ให้เข้ากับธีม ---------- */
.swal2-popup { font-family: 'Sarabun', Tahoma, sans-serif !important; border-radius: 14px !important; }
.swal2-title { color: var(--navy) !important; font-size: 1.3rem !important; }
.swal2-html-container { white-space: pre-line; font-size: 1rem !important; color: var(--text) !important; }
.swal2-styled { font-weight: 600 !important; border-radius: 8px !important; padding: .55rem 1.3rem !important; }
.swal2-toast .swal2-html-container { font-size: .95rem !important; margin: .2rem .6rem !important; }

/* คะแนนความประพฤติ */
.pts-minus { color: #b42318; font-weight: 600; }
.pts-plus { color: var(--ok); font-weight: 600; }
.pts-zero { color: var(--ok); }
.stat .num.pts-minus { color: #b42318; }
.stat .num.pts-plus, .stat .num.pts-zero { color: var(--ok); }
.score-grid { margin-top: .8rem; }
.score-grid .stat { border: 1px solid var(--line); border-radius: 10px; padding: .6rem; }
.pts-tag { font-size: .8rem; color: #b42318; white-space: nowrap; }
.mob-only { display: none; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: .2rem .8rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 600; }

/* ---------- หน้าต่างบันทึกความผิด ---------- */
dialog.modal { border: 0; border-radius: 14px; padding: 0; width: min(760px, 96vw); max-height: 92vh; box-shadow: 0 20px 60px #0004; }
dialog.modal::backdrop { background: #1b2a4a88; }
.modal-head { background: var(--navy); color: #fff; padding: .8rem 1.2rem; border-bottom: 3px solid var(--gold); }
.modal-head h3 { margin: 0; font-size: 1.1rem; }
.modal-body { padding: 1rem 1.2rem; overflow-y: auto; max-height: calc(92vh - 130px); }
.modal-foot { padding: .8rem 1.2rem; border-top: 1px solid var(--line); display: flex; gap: .5rem; justify-content: flex-end; align-items: center; }
.vitem { border: 1px solid var(--line); border-radius: 10px; padding: .5rem .8rem; margin-bottom: .5rem; }
.vitem.on { border-color: var(--gold); background: #fffbef; }
.vitem > label { font-weight: 600; display: flex; gap: .5rem; align-items: center; margin: 0; cursor: pointer; }
.vitem .detail { display: none; margin-top: .5rem; gap: .5rem; grid-template-columns: 1fr 120px; }
.vitem.on .detail { display: grid; }
.vitem .detail .full { grid-column: 1 / -1; }
.vitem .detail select, .vitem .detail input { width: 100%; }

/* ---------- ตั้งค่าระบบ / ท้ายหน้า ---------- */
.logo-preview { min-height: 90px; display: flex; align-items: center; justify-content: center; background: #f6f7fa; border-radius: 10px; margin-bottom: .8rem; padding: .6rem; }
.logo-preview img { max-height: 120px; max-width: 100%; }
input[type=color] { width: 4rem; height: 2.4rem; padding: .15rem; border: 1px solid #c3c9d6; border-radius: 8px; background: #fff; }
.footer a { color: var(--muted); }
.footer a.on { color: var(--navy); font-weight: 700; text-decoration: none; }
.announce { border-left: 5px solid var(--gold); background: #fffbef; white-space: pre-line; }
.login-logo { display: block; max-height: 110px; max-width: 60%; margin: 0 auto .6rem; }
.view-auto .view-toggle { display: none; }

/* ---------- แท็บเล็ต / มือถือ (มุมมองปรับตามจอ) ---------- */
@media (max-width: 1024px) {
  .view-auto .view-toggle { display: inline-block; }
}
@media (pointer: coarse) {
  .btn { min-height: 40px; }
  .btn-sm { min-height: 36px; }
  input[type=checkbox] { width: 20px; height: 20px; }
}
@media (max-width: 760px) {
  body { font-size: 16px; }
  .topbar { padding: .5rem .8rem; }
  .brand { font-size: 1.05rem; }
  .brand img { height: 36px; }
  .userbox { width: 100%; gap: .35rem; }
  .userbox .badge { font-size: .72rem; }
  .userbox form.inline { margin-left: auto; }
  .view-toggle, .userbox .btn-ghost { padding: .2rem .6rem; font-size: .85rem; }
  /* เมนูแถวเดียว เลื่อนซ้าย-ขวา */
  .mainnav { flex-wrap: nowrap; overflow-x: auto; justify-content: flex-start; padding: .35rem .6rem; scrollbar-width: none; }
  .mainnav::-webkit-scrollbar { display: none; }
  .mainnav a { white-space: nowrap; flex: none; }
  .container { margin: .8rem auto; padding: 0 .6rem; }
  .card { padding: .9rem; border-radius: 10px; }
  .narrow { margin: 1rem auto; }
  .toolbar h2 { font-size: 1.1rem; }
  .vitem .detail { grid-template-columns: 1fr; }
  .pick-row { flex-direction: column; gap: .35rem; }
  .pick-label { width: auto; padding: 0; }
  .room-tiles { grid-template-columns: repeat(2, 1fr); }
  .pick-row .search-box { max-width: none; width: 100%; }
  /* มอบหมายครู → การ์ดทีละห้อง */
  table.assign-table tr:first-child { display: none; }
  table.assign-table tr { display: grid; grid-template-columns: 1fr auto; gap: .35rem .6rem; padding: .7rem .2rem; border-bottom: 1px solid var(--line); }
  table.assign-table td { border: 0; padding: 0; }
  table.assign-table td.teachers, table.assign-table td:last-child { grid-column: 1 / -1; }
  /* หน้าต่างบันทึกความผิดเต็มจอ */
  dialog.modal { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; }
  dialog.modal form { display: flex; flex-direction: column; height: 100%; }
  .modal-body { flex: 1; max-height: none; }
  .modal-foot { padding-bottom: max(.8rem, env(safe-area-inset-bottom)); }
  /* ตารางนักเรียนในหน้าตรวจ → การ์ดทีละคน */
  table.stu-table tr:first-child { display: none; }
  table.stu-table tr { display: grid; grid-template-columns: auto auto 1fr; gap: .1rem .6rem; padding: .6rem .2rem; border-bottom: 1px solid var(--line); }
  table.stu-table td { border: 0; padding: 0; }
  table.stu-table td.vcell, table.stu-table td.actions { grid-column: 1 / -1; padding-left: 2rem; }
  table.stu-table td.num { text-align: left; font-weight: 600; }
  table.stu-table td.num:not(:empty)::before { content: 'เลขที่ '; font-weight: 400; color: var(--muted); }
  /* ตารางคะแนนความประพฤติ: แถวละการ์ด ชื่อเต็มบรรทัด คะแนน 3 ช่องเรียงกัน */
  table.conduct-table tr:first-child { display: none; }
  table.conduct-table tr { display: grid; grid-template-columns: repeat(3, 1fr); gap: .2rem .6rem; padding: .6rem .2rem; border-bottom: 1px solid var(--line); }
  table.conduct-table td { border: 0; padding: 0; text-align: left; }
  table.conduct-table td.who, table.conduct-table td.actions { grid-column: 1 / -1; }
  table.conduct-table td.who-no { display: none; }
  table.conduct-table .mob-only { display: inline; }
  table.conduct-table td[data-label]::before { content: attr(data-label) ' '; color: var(--muted); font-size: .85rem; }
}

/* หน้าเข้าสู่ระบบ: ปุ่มสำหรับนักเรียน (ใบบันทึกความดี) */
.or-line { display: flex; align-items: center; gap: .7rem; margin: 1.3rem 0 .8rem; color: #7a8399; font-size: .85rem; }
.or-line::before, .or-line::after { content: ""; flex: 1; height: 1px; background: #e3e6ee; }
.student-cta { display: flex; align-items: center; gap: .8rem; padding: .75rem .9rem; border-radius: 12px; text-decoration: none;
  color: var(--navy); background: linear-gradient(135deg, #fffaf0, #fdf3d8); border: 1px solid color-mix(in srgb, var(--gold) 55%, #fff);
  transition: transform .15s, box-shadow .15s, border-color .15s; }
.student-cta:hover, .student-cta:focus-visible { transform: translateY(-1px); box-shadow: 0 6px 16px #c9a22733; border-color: var(--gold); outline: none; }
.student-cta .cta-icon { flex: none; width: 2.6rem; height: 2.6rem; border-radius: 10px; display: grid; place-items: center;
  background: var(--gold); font-size: 1.35rem; box-shadow: inset 0 -2px 0 #0001; }
.student-cta .cta-text { flex: 1; display: flex; flex-direction: column; line-height: 1.3; min-width: 0; }
.student-cta .cta-text b { font-size: 1rem; }
.student-cta .cta-text small { color: #6b5a2a; font-size: .82rem; }
.student-cta .cta-arrow { flex: none; font-size: 1.6rem; line-height: 1; color: var(--gold); font-weight: 700; transition: transform .15s; }
.student-cta:hover .cta-arrow { transform: translateX(3px); }
