@font-face {
  font-family: 'TH SarabunPSK';
  src: url('/fonts/THSarabunPSK.ttf') format('truetype');
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: 'TH SarabunPSK';
  src: url('/fonts/THSarabunPSK-Bold.ttf') format('truetype');
  font-weight: 700;
  font-display: swap;
}

:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --text: #1d2433;
  --muted: #5d6779;
  --border: #d9dee7;
  --primary: #1f4e8c;
  --primary-hover: #183f72;
  --primary-soft: #e7eef8;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --success: #1a7f37;
  --success-soft: #e6f4ea;
  --warn: #9a6700;
  --warn-soft: #fff4d6;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
}

* { box-sizing: border-box; }
html { font-size: 20px; }
body {
  margin: 0;
  font-family: 'TH SarabunPSK', 'Sarabun', Tahoma, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.35;
}
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; }
h1 { font-size: 1.8rem; }
h2 { font-size: 1.45rem; }
h3 { font-size: 1.2rem; }
a { color: var(--primary); }

/* ----- ปุ่ม / ฟอร์ม ----- */
button, .btn {
  font: inherit;
  font-size: 1rem;
  border: 1px solid var(--primary);
  background: var(--primary);
  color: #fff;
  border-radius: var(--radius);
  padding: .3rem .9rem;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
  white-space: nowrap;
}
button:hover, .btn:hover { background: var(--primary-hover); }
button:disabled { opacity: .55; cursor: not-allowed; }
button.secondary, .btn.secondary { background: var(--surface); color: var(--primary); }
button.secondary:hover, .btn.secondary:hover { background: var(--primary-soft); }
.btn.small { font-size: .9rem; padding: .1rem .6rem; }
button.danger { background: var(--surface); color: var(--danger); border-color: var(--danger); }
button.danger:hover { background: var(--danger-soft); }
button.small { font-size: .9rem; padding: .1rem .6rem; }

label { display: block; font-weight: 700; margin-bottom: .15rem; }
input, select {
  font: inherit;
  font-size: 1.05rem;
  width: 100%;
  padding: .3rem .6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  color: var(--text);
}
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary); }
.field { margin-bottom: .8rem; }
.hint { color: var(--muted); font-size: .9rem; }
.row { display: flex; gap: .75rem; flex-wrap: wrap; align-items: flex-end; }
.row > .field { flex: 1 1 180px; margin-bottom: 0; }

.msg { padding: .45rem .8rem; border-radius: var(--radius); margin: .6rem 0; }
.msg.error { background: var(--danger-soft); color: var(--danger); }
.msg.ok { background: var(--success-soft); color: var(--success); }
.msg.warn { background: var(--warn-soft); color: var(--warn); }
.hidden { display: none !important; }

/* ----- การ์ด / ตาราง ----- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 1.1rem 1.3rem;
  margin-bottom: 1rem;
}
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; }
table.data th, table.data td { border-bottom: 1px solid var(--border); padding: .35rem .5rem; text-align: left; vertical-align: middle; }
table.data th { background: var(--primary-soft); font-weight: 700; }
.badge { display: inline-block; padding: 0 .55rem; border-radius: 99px; font-size: .9rem; }
.badge.green { background: var(--success-soft); color: var(--success); }
.badge.gray { background: #eceff3; color: var(--muted); }
.badge.blue { background: var(--primary-soft); color: var(--primary); }
.badge.yellow { background: var(--warn-soft); color: var(--warn); }
.actions { display: flex; gap: .35rem; flex-wrap: wrap; }

.logo-box { width: 110px; height: 110px; border: 1px dashed var(--border); border-radius: var(--radius); display: grid; place-items: center; background: #fff; overflow: hidden; }
.logo-box img { max-width: 100%; max-height: 100%; }
.tsel { display: flex; flex-direction: column; gap: .25rem; align-items: flex-start; }
.tsel select { min-width: 200px; }
.manual-order tr[draggable] { cursor: grab; }
.manual-order tr.dragging { opacity: .4; }
.manual-order tr.drop td { border-top: 2px solid var(--primary, #2563eb); }
.manual-order tr.moved td { background: #fff7d6; }
.manual-order .grip { color: #999; margin-right: .3rem; }
.sig-preview { display: flex; flex-wrap: wrap; gap: .8rem; }
.sig-preview .sp { flex: 1 1 200px; border: 1px dashed var(--border); border-radius: var(--radius); padding: .6rem; text-align: center; background: #fff; line-height: 1.6; }
.toolbar { display: flex; gap: .6rem; flex-wrap: wrap; align-items: flex-end; margin-bottom: .8rem; }
.toolbar .field { margin: 0; }
.toolbar select, .toolbar input { width: auto; min-width: 140px; }
tr.err td { background: var(--danger-soft); }
tr.muted td { color: var(--muted); }
.num { text-align: right; }
.stat { display: inline-block; margin-right: 1.2rem; }
.stat b { font-size: 1.3rem; }
dialog { border: 1px solid var(--border); border-radius: 12px; padding: 1.1rem 1.3rem; max-width: 560px; width: calc(100% - 32px); }
dialog::backdrop { background: rgba(16,24,40,.35); }

/* ----- ตารางกรอกคะแนน ----- */
table.grid td { padding: .15rem .4rem; }
table.grid td.name { white-space: nowrap; }
table.grid th { position: sticky; top: 0; z-index: 1; text-align: center; line-height: 1.1; }
table.grid input.cell { width: 72px; text-align: center; padding: .1rem .3rem; font-size: 1.1rem; }
table.grid input.cell.bad { background: var(--danger-soft); border-color: var(--danger); color: var(--danger); }
table.grid td.ro { color: var(--muted); }
table.grid td.g { font-weight: 700; color: var(--text); }
table.grid tbody tr:hover td { background: #f7f9fc; }
.statusbar { display: flex; justify-content: space-between; gap: 1rem; flex-wrap: wrap; position: sticky; top: 0; background: var(--surface); padding: .2rem 0; z-index: 2; }
.ok-text { color: var(--success); }
.err-text { color: var(--danger); font-weight: 700; }
.seg { display: flex; gap: .2rem; flex-wrap: wrap; }
.seg label { display: inline-flex; align-items: center; font-weight: 400; border: 1px solid var(--border); border-radius: var(--radius); padding: .2rem .7rem; cursor: pointer; margin: 0; }
.seg input { width: auto; margin-right: .3rem; }
.seg label:has(input:checked) { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); font-weight: 700; }
.bar { height: 8px; background: #eceff3; border-radius: 99px; overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--success); }

/* ----- ปุ่มเลือก 0 / ร / มส ----- */
.pick { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.pick button { background: #fff; color: var(--text); border: 0; border-right: 1px solid var(--border); border-radius: 0; padding: .1rem .75rem; min-width: 46px; }
.pick button:last-child { border-right: 0; }
.pick button:hover { background: var(--bg); }
.pick button.on { background: var(--danger); color: #fff; font-weight: 700; }
.pick button.on[data-k=""] { background: #eceff3; color: var(--muted); }
tr.flagged td { background: #fff8f7; }

/* ----- ตาราง ปพ.5 (หลายคอลัมน์ ตรึงเลขที่/ชื่อ) ----- */
.pp5wrap { max-height: 72vh; overflow: auto; }
table.pp5 input.cell { width: 52px; font-size: 1.05rem; }
table.pp5 input.cell.miss { background: var(--warn-soft); color: var(--warn); font-weight: 700; }
table.pp5 th { font-size: .9rem; white-space: nowrap; }
table.pp5 th.midcol, table.pp5 td.midcol { background: #fdf6e3; }
table.pp5 th.calc { background: #e9f3ec; }
table.pp5 .sticky0 { position: sticky; left: 0; background: var(--surface); z-index: 2; }
table.pp5 .sticky1 { position: sticky; left: 46px; background: var(--surface); z-index: 2; box-shadow: 1px 0 0 var(--border); }
table.pp5 thead th.sticky0, table.pp5 thead th.sticky1 { z-index: 3; background: var(--primary-soft); }
table.pp5 select { width: auto; padding: .05rem .2rem; }

/* ----- หน้ากลาง (ล็อกอิน / ตั้งค่า / ค้นผล) ----- */
.center-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.center-box { width: 100%; max-width: 420px; }
.brand { text-align: center; margin-bottom: 1rem; }
.brand .school { color: var(--muted); }

/* ----- เค้าโครงแอป ----- */
.topbar {
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: .4rem 1rem;
  flex-wrap: wrap;
}
.topbar .title { font-weight: 700; font-size: 1.25rem; margin-right: auto; }
.topbar select { width: auto; padding: .1rem .5rem; }
.topbar .who { font-size: .95rem; }
.topbar button { background: transparent; border-color: rgba(255,255,255,.6); }
.topbar button:hover { background: rgba(255,255,255,.15); }

.layout { display: flex; min-height: calc(100vh - 52px); }
nav.side {
  width: 220px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: .75rem .5rem;
}
nav.side a {
  display: block;
  padding: .35rem .75rem;
  border-radius: var(--radius);
  color: var(--text);
  text-decoration: none;
}
nav.side a:hover { background: var(--bg); }
nav.side a.active { background: var(--primary-soft); color: var(--primary); font-weight: 700; }
nav.side .group { color: var(--muted); font-size: .85rem; padding: .6rem .75rem .1rem; }
nav.side a.soon { color: var(--muted); }
main.content { flex: 1; padding: 1.2rem; min-width: 0; }

@media (max-width: 760px) {
  .layout { flex-direction: column; }
  nav.side { width: auto; border-right: 0; border-bottom: 1px solid var(--border); display: flex; flex-wrap: wrap; gap: .25rem; }
  nav.side .group { display: none; }
  main.content { padding: 16px; }
}

/* ===== งานของฉัน / แท็บของวิชา (โครงสร้างใหม่) ===== */
.worklist { display: grid; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); gap: .6rem; margin-top: .6rem; }
.workitem { display: block; border: 1px solid var(--border); border-radius: var(--radius); padding: .55rem .7rem; color: var(--text); text-decoration: none; background: var(--surface); }
.workitem:hover { border-color: var(--primary); box-shadow: var(--shadow); }
.workitem.done { border-left: 5px solid var(--success); }
.workitem:not(.done) { border-left: 5px solid #e3b341; }
.wi-title { line-height: 1.25; }
.wi-chips { display: flex; flex-wrap: wrap; gap: .3rem; margin-top: .35rem; }
.worktabs { display: flex; flex-wrap: wrap; gap: .25rem; border-bottom: 2px solid var(--border); margin: .6rem 0 .8rem; }
.worktabs a { padding: .35rem .7rem; border: 1px solid var(--border); border-bottom: none; border-radius: var(--radius) var(--radius) 0 0;
  background: #eef1f5; color: var(--text); text-decoration: none; margin-bottom: -2px; white-space: nowrap; }
.worktabs a.on { background: var(--surface); border-color: var(--primary); border-bottom: 2px solid var(--surface); color: var(--primary); font-weight: 700; }
.worktabs a.is-soon { color: var(--muted); }
.wt-ok { color: var(--success); font-weight: 700; }
.wt-warn { background: var(--danger); color: #fff; border-radius: 9px; padding: 0 .4rem; font-size: .8rem; }
.soon-dot { font-size: .7rem; color: var(--muted); border: 1px dashed var(--muted); border-radius: 6px; padding: 0 .25rem; }
.termsw { display: flex; gap: .4rem; flex-wrap: wrap; }
.days { display: flex; flex-wrap: wrap; gap: .5rem; margin: .4rem 0; }
.day { display: flex; flex-direction: column; gap: .2rem; border: 1px solid var(--border); border-radius: var(--radius); padding: .4rem .6rem; min-width: 120px; font-weight: 400; }
.day.on { border-color: var(--primary); background: var(--primary-soft); }
.day input[type=number] { width: 60px; display: inline-block; }
.hrgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: .6rem; margin-top: .6rem; }
.hrbox { border: 1px solid var(--border); border-radius: var(--radius); padding: .5rem .7rem; }
.hrbox h3 { margin: 0 0 .3rem; font-size: 1.05rem; }
.hrbox p { margin: .2rem 0 .4rem; }
.pgroup { font-weight: 700; color: var(--primary); margin: .6rem 0 .1rem; border-bottom: 1px solid var(--border); }
.pgroup:first-child { margin-top: 0; }
details.special { display: inline-block; }
details.special summary { cursor: pointer; color: var(--muted); font-size: .9rem; }
.holed { padding: .4rem 0; }

/* ===== เวลาเรียน ===== */
.brushes { display: flex; flex-wrap: wrap; gap: .35rem; margin-top: .5rem; }
.brush { border: 1px solid var(--border); border-radius: var(--radius); padding: .2rem .6rem; font-weight: 400; cursor: pointer; }
.brush:has(input:checked) { border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary-soft); font-weight: 700; }
.brush.b-ข { background: var(--danger-soft); } .brush.b-ป { background: var(--warn-soft); } .brush.b-ล { background: var(--primary-soft); } .brush.b-- { background: #eceef2; }
.attwrap { overflow: auto; max-height: 72vh; border: 1px solid var(--border); border-radius: var(--radius); }
table.att { border-collapse: separate; border-spacing: 0; font-size: .85rem; }
table.att th, table.att td { border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); padding: .1rem .2rem; text-align: center; white-space: nowrap; background: var(--surface); }
table.att thead th { position: sticky; z-index: 2; background: #eef1f5; font-weight: 700; }
table.att thead th { top: 0; }
table.att .s1 { position: sticky; left: 0; z-index: 3; min-width: 2.4rem; }
table.att .s2 { position: sticky; left: 2.4rem; z-index: 3; min-width: 11rem; text-align: left; border-right: 2px solid var(--border); }
table.att thead .s1, table.att thead .s2 { z-index: 4; }
table.att td.am { width: 1.45rem; min-width: 1.45rem; cursor: pointer; user-select: none; touch-action: none; font-weight: 700; }
table.att td.am:empty::before { content: '/'; color: #c3cad6; font-weight: 400; }
table.att td.am:hover { outline: 2px solid var(--primary); outline-offset: -2px; }
table.att td.mข { background: var(--danger-soft); color: var(--danger); }
table.att td.mป { background: var(--warn-soft); color: var(--warn); }
table.att td.mล { background: var(--primary-soft); color: var(--primary); }
table.att td.mx { background: #e3e6eb; color: var(--muted); }
table.att th.wk { font-weight: 400; color: var(--muted); }
table.att th.dw, table.att th.pd { font-weight: 400; }
table.att td.sm { min-width: 2.3rem; background: #fafbfc; }
table.att td.pct.low { color: #fff; background: var(--danger); font-weight: 700; }
.attmanage summary { cursor: pointer; }

/* ===== ประเมิน 3 ด้านรายข้อ ===== */
.badge.red { background: var(--danger-soft); color: var(--danger); }
table.asg th.it { min-width: 4.6rem; max-width: 6.5rem; vertical-align: bottom; white-space: normal; font-weight: 400; line-height: 1.1; }
table.asg .it-no { font-weight: 700; }
table.asg .it-name { font-size: .78rem; color: var(--muted); }
table.asg input.cell { width: 3rem; text-align: center; }
table.asg td.nm { white-space: nowrap; }
table.asg th.grp { font-size: .85rem; }
/* สถานะนักเรียน: ข / – ที่ระบบใส่ให้ (ขาดประจำ / ย้ายออก) แสดงแบบลาย เพื่อแยกจากที่ครูบันทึกเอง */
table.att td.am.auto { font-weight: 400; font-style: italic; background-image: repeating-linear-gradient(135deg, transparent 0 4px, rgba(0,0,0,.05) 4px 6px); }
table.att tr.moved td.s2 { color: #7a8494; }
table.att tr.moved td.am.mx.auto { cursor: not-allowed; }
