/* การพิมพ์ / บันทึกเป็น PDF จากหน้าผู้ดูแล (โหลดเฉพาะตอนพิมพ์ — media="print")
   พิมพ์เฉพาะ #print-area · แนวกระดาษตามเอกสาร (named page — ห้ามใช้ <style> ในหน้า เพราะ CSP บล็อก)
     ใบปะหน้าซอง = A4 แนวนอน 1 แผ่นต่อ 1 ซอง · ใบลงชื่อเข้าสอบ = A4 แนวตั้ง 1 แผ่นต่อ 1 วิชา/Section */
@page { size: A4 landscape; margin: 0; }
@page signin { size: A4 portrait; margin: 0; }
@page report { size: A4 landscape; margin: 0; }

html, body { background: #fff !important; color: #000 !important; }
body { display: block; min-height: 0; }
body > :not(#print-area) { display: none !important; }
.print-area { display: block !important; }

/* ---------- ใบปะหน้าซอง ----------
   ฟอนต์ Sarabun (แบบเอกสารราชการ) · โทนขาวดำ พิมพ์เครื่องขาวดำได้ชัด · หน่วย mm/pt ตามกระดาษจริง
   สี/ฟอนต์/หัวเอกสาร (.env-header, .env-info-label/value) ใช้ร่วมกับใบลงชื่อเข้าสอบ (.sig-page) */
.env-page, .sig-page, .hand-page, .rep-page {
  --ink: #000;
  --ink-2: #333;        /* ข้อความรอง */
  --ink-3: #666;        /* ป้ายกำกับ / คำอธิบาย */
  --rule: #000;
  --rule-2: #8a8a8a;    /* เส้นบรรทัดเขียน */
  --shade: #ececec;     /* พื้นหัวตาราง */
  color: var(--ink);
  font-family: "Sarabun", "Noto Sans Thai", sans-serif;   /* ห้ามใส่ TH Sarabun New: ตัวไทยเล็กกว่าตัวอังกฤษมาก */
  font-size: 14pt;
  line-height: 1.35;
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
  box-sizing: border-box;
  overflow: hidden;
  break-after: page;
  page-break-after: always;
}
.env-page:last-child, .sig-page:last-child, .hand-page:last-child, .rep-page:last-child { break-after: auto; page-break-after: auto; }
.env-page p, .sig-page p, .hand-page p, .rep-page p { margin: 0; }

.env-page {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 297mm;
  height: 210mm;
  padding: 10mm 16mm 9mm;
}
.env-page > * { flex-shrink: 0; }   /* ห้ามบีบเนื้อหา — ถ้าไม่พอให้เห็นว่าล้น */

/* หัวเอกสาร */
.env-header {
  display: flex;
  align-items: center;
  gap: 5mm;
  padding-bottom: 2.5mm;
  border-bottom: .9mm double var(--rule);
}
.env-logo { flex: none; width: auto; height: 21mm; }
.env-heading { flex: 1; min-width: 0; }
.env-title { font-size: 20pt; font-weight: 700; line-height: 1.2; letter-spacing: .005em; }
.env-term { margin-top: .8mm !important; font-size: 15pt; font-weight: 500; color: var(--ink-2); }
.env-org { font-size: 12.5pt; color: var(--ink-3); }

/* รหัสซอง (ขวาบน) */
.env-code {
  flex: none;
  padding: 2mm 6mm 2.5mm;
  border: .7mm solid var(--rule);
  border-radius: 2.5mm;
  font-size: 27pt;
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: .03em;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* ห้องสอบ / วันสอบ / เวลาสอบ */
.env-info {
  display: grid;
  grid-template-columns: 2.1fr 1.25fr .85fr;
  margin-top: 3.5mm;
  border: .35mm solid var(--rule);
}
.env-info-cell { padding: 1.2mm 4mm 1.6mm; }
.env-info-cell + .env-info-cell { border-left: .35mm solid var(--rule); }
.env-info-label { font-size: 10.5pt; color: var(--ink-3); line-height: 1.2; }
.env-info-value { font-size: 15pt; font-weight: 700; line-height: 1.25; }

/* ตารางรายวิชา — ความกว้างคอลัมน์คงที่ทุกซอง (265mm = A4 แนวนอน − ขอบซ้ายขวา) */
.env-subjects {
  width: 100%;
  table-layout: fixed;
  margin-top: 3.5mm;
  border-collapse: collapse;
  font-size: 13pt;   /* ทุกช่องขนาดเดียวกัน — ตัวหนาเฉพาะหัวตารางและแถวรวม */
}
.env-subjects .col-section { width: 24mm; }
.env-subjects .col-num { width: 27mm; }
.env-subjects .col-subject { width: auto; }   /* ที่เหลือ ≈ 133mm ชื่อยาวจะขึ้นบรรทัดใหม่ */
.env-subjects th, .env-subjects td {
  padding: 1mm 3mm;
  border: .3mm solid var(--rule);
  text-align: left;
  vertical-align: middle;
  overflow-wrap: anywhere;
}
.env-subjects th { background: var(--shade); font-weight: 700; line-height: 1.25; }
.env-subjects td { height: 8.5mm; font-weight: 400; }   /* สูงพอให้เขียนตัวเลขด้วยมือ */
.env-subjects .c { text-align: center; white-space: nowrap; }
.env-subjects .num { font-variant-numeric: tabular-nums; }
.env-subjects .total td { background: #f6f6f6; font-weight: 700; }   /* แถวรวม: ตัวหนา (ช่องว่างไม่มีข้อความ) */

/* ซองกระดาษทด: รายวิชา | Section | จำนวนกระดาษทด (ไม่มีบันทึกรายงาน/ลงชื่อกรรมการ) */
.scratch-subjects .col-count { width: 48mm; }
.scratch-rules { padding: 3mm 6mm 3.5mm; }
.scratch-rule-list { margin: 1.5mm 0 0; padding-left: 7mm; font-size: 14pt; line-height: 1.5; }
.scratch-rule-list li + li { margin-top: 1.5mm; }

/* หัวข้อส่วน */
.env-section-title { font-size: 13.5pt; font-weight: 700; }

/* บันทึกรายงานการสอบ */
.env-report { margin-top: 3.5mm; }
.env-line { height: 7.6mm; border-bottom: .25mm dotted var(--rule-2); }

/* ลงลายมือชื่อกรรมการ — อยู่ล่างสุดของหน้าเสมอ */
.env-sign-box {
  margin-top: auto;
  padding: 2mm 6mm 2.5mm;
  border: .4mm solid var(--rule);
  border-radius: 1.5mm;
}
.env-signs { display: flex; justify-content: center; gap: 12mm; margin-top: 10mm; }   /* เว้นที่ให้เซ็นเหนือเส้น */   /* กึ่งกลาง ไม่ว่ากี่คน */
.env-sign { flex: 0 0 74mm; text-align: center; }
.env-sign-line { display: flex; align-items: flex-end; gap: 2mm; font-size: 13pt; }
.env-sign-dots { flex: 1; height: 1em; border-bottom: .3mm dotted var(--rule); }
.env-sign-name { margin-top: 1.5mm !important; padding-left: 12mm; font-size: 13pt; }
.env-sign-role { padding-left: 12mm; font-size: 11pt; color: var(--ink-3); }

/* ---------- ใบลงชื่อเข้าสอบ ----------
   A4 แนวตั้ง 1 หน้าต่อ 1 วิชา/Section · หัวเอกสารใช้ .env-header ร่วมกับใบปะหน้าซอง
   ส่วนหัว + ตารางข้อมูลทำให้กะทัดรัด (≈ 38mm ไม่รวมขอบบนและ tag) — เหลือพื้นที่ให้ตารางลงชื่อนักศึกษามากที่สุด */
.sig-page {
  page: signin;   /* → @page signin (A4 แนวตั้ง) */
  display: flex;
  flex-direction: column;
  width: 210mm;
  height: 297mm;
  padding: 6mm 12mm 8mm;   /* พื้นที่เนื้อหากว้าง 186mm */
}
.sig-page > * { flex-shrink: 0; }
/* หัวเอกสารแบบกะทัดรัด ใช้ร่วมกับใบรับส่งเอกสาร (.hand-page) */
.sig-page .env-header, .hand-page .env-header, .rep-page .env-header { gap: 3.5mm; padding-bottom: 1.5mm; border-bottom-width: .7mm; }
.sig-page .env-logo, .hand-page .env-logo, .rep-page .env-logo { height: 12mm; }
.sig-page .env-title, .hand-page .env-title, .rep-page .env-title { font-size: 15pt; }
/* ภาคเรียน · มหาวิทยาลัย อยู่บรรทัดเดียวกัน รูปแบบเดียวกันทั้งบรรทัด */
.sig-page .env-term, .sig-page .env-org,
.hand-page .env-term, .hand-page .env-org,
.rep-page .env-term, .rep-page .env-org { display: inline; font-size: 11pt; font-weight: 500; color: var(--ink-2); }
.sig-page .env-org::before, .hand-page .env-org::before, .rep-page .env-org::before { content: " · "; }

/* วันสอบ | เวลาสอบ / ห้องสอบ (เต็มแถว) / วิชา | Section
   เส้นตาราง: กรอบนอก 2px ที่ตัวกรอบ (อยู่ในความกว้าง 186mm เท่าส่วนสรุป/ตารางลงชื่อ — ห้ามใช้ outline: ล้นออกนอกกรอบ)
   เส้นใน 1px = ขวา+ล่าง ของเซลล์ ยกเว้นคอลัมน์ขวาสุด/แถวล่างสุด (ชนกรอบนอกแล้ว)
   (ห้ามใช้พื้นดำ + gap แทนเส้น: เส้นบาง/หนาไม่เท่ากันตามการซูม และหายถ้าไม่พิมพ์พื้นหลัง) */
.sig-info {
  display: grid;
  grid-template-columns: 1fr 46mm;
  margin-top: 2mm;
  border: 2px solid var(--rule);
}
.sig-cell {
  padding: .5mm 3mm .7mm;
  border-right: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
  min-width: 0;
  overflow-wrap: anywhere;
}
.sig-cell.room { grid-column: 1 / -1; }
.sig-cell.time, .sig-cell.room, .sig-cell.section { border-right: 0; }   /* คอลัมน์ขวาสุด */
.sig-cell.subject, .sig-cell.section { border-bottom: 0; }                /* แถวล่างสุด */
.sig-page .env-info-value { font-size: 12.5pt; }

/* มุมขวาของหัวเอกสาร: tag รหัสซอง (บน) + เลขหน้าในซอง (ล่าง ทุกหน้า: หน้าที่/จำนวนหน้าใบลงชื่อทั้งซอง) */
.sig-head-right { flex: none; align-self: stretch; display: flex; flex-direction: column; justify-content: space-between; align-items: flex-end; }
.sig-pageno { font-size: 11pt; font-weight: 500; color: var(--ink-2); }

/* tag รหัสวิชา-แผ่นที่/จำนวนแผ่นทั้งวิชา (บรรทัดสุดท้ายของทุกหน้า ชิดขวา) — สูง 1 บรรทัด ≈ 7.4mm รวมระยะห่าง
   margin-top: auto ดันลงล่างสุดของหน้าเสมอ (หน้าที่รายชื่อไม่เต็มก็อยู่ตำแหน่งเดียวกัน) */
.sig-tags { display: flex; justify-content: flex-end; gap: 2.5mm; margin-top: auto; padding-top: 1.2mm; }
.sig-tag {
  padding: .2mm 2.5mm .4mm;
  border: .35mm solid var(--rule);
  border-radius: 1.2mm;
  font-size: 11pt;
  font-weight: 700;
  line-height: 1.3;
  letter-spacing: .02em;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* ส่วนสรุปยอดผู้เข้าสอบ (หน้าแรกของชุด) — สูงรวม ≈ 28mm
   แถวบน: มีสิทธิสอบ (พิมพ์) | เข้าสอบ | ขาดสอบ | ยุติการสอบ (ช่องว่างให้เขียน) · แถวล่าง: ลงชื่อกรรมการทุกคน */
.sig-summary { margin-top: 2mm; border: 2px solid var(--rule); }   /* กรอบนอก 2px / เส้นใน 1px เหมือนตารางลงชื่อ */
.sig-counts { display: grid; grid-template-columns: repeat(4, 1fr); border-bottom: 1px solid var(--rule); }
.sig-count { display: flex; align-items: baseline; gap: 1.5mm; padding: 1.4mm 3mm; font-size: 11.5pt; white-space: nowrap; }
.sig-count + .sig-count { border-left: 1px solid var(--rule); }
.sig-count-label { font-weight: 500; }
.sig-count-value { flex: 1; text-align: center; font-weight: 700; font-size: 13pt; }
.sig-count-blank { flex: 1; align-self: stretch; border-bottom: .25mm dotted var(--rule); }   /* ที่เขียนตัวเลข */
.sig-proctors { display: flex; justify-content: space-around; gap: 4mm; padding: 6.5mm 4mm 1.2mm; }   /* เว้นที่เซ็นเหนือเส้น */
.sig-proctor { flex: 0 1 62mm; min-width: 0; text-align: center; }
.sig-proctor-line { display: flex; align-items: flex-end; gap: 1.5mm; font-size: 11pt; }
/* ห้ามใส่ overflow: hidden ที่ข้อความไทย: ตัดสระบน/วรรณยุกต์ที่ซ้อนสูงเกินบรรทัด (เช่น ไม้โทใน "ชุมเชื้อ") */
.sig-proctor-name { margin-top: .6mm !important; font-size: 10pt; white-space: nowrap; }

/* ตารางลงชื่อ — กว้าง 186mm คงที่ทุกหน้า · 30 แถวพอดีหน้า (SIGNIN_ROWS ใน admin.js)
   หน้าแรกมีส่วนสรุป จึงกำหนดความสูงแถวจากหน้าแรก (ทุกหน้าเท่ากัน):
   297 − 6 (ขอบบน) − 38.2 (หัว+ข้อมูล) − 2 − 28.1 (สรุป) − 2 − 7 (หัวตาราง) − 7.4 (tag) − 8 (ขอบล่าง) ≈ 198mm ÷ 30 ≈ 6.5mm (วัดจริง ≈ 294mm) */
.sig-table {
  width: 100%;
  table-layout: fixed;
  margin-top: 2mm;
  border-collapse: collapse;
  font-size: 11.5pt;
  line-height: 1.25;
}
.sig-table .col-no { width: 15mm; }
.sig-table .col-id { width: 34mm; }
.sig-table .col-name { width: auto; }   /* ที่เหลือ ≈ 66mm — ชื่อยาวสุดในข้อมูล 33 ตัวอักษรยังพอดี */
.sig-table .col-group { width: 27mm; }   /* รองรับกลุ่มที่มีต่อท้าย เช่น 65088.121-A */
.sig-table .col-sign { width: 44mm; }
/* น้ำหนักเส้น: กรอบนอก + ใต้หัวตาราง = 2px (≈ .53mm) · เส้นในอื่น ๆ = 1px (≈ .26mm)
   ใช้หน่วย px: Chrome ปัดความหนาเส้นเป็นจำนวนเต็มพิกเซล (.2–.35mm กลายเป็น 1px เท่ากันหมด)
   (border-collapse ใช้เส้นที่หนากว่าเมื่อซ้อนกัน · เส้นดำล้วน ถ่ายเอกสารขาวดำได้ไม่จาง) */
.sig-table { border: 2px solid var(--rule); }
.sig-table th, .sig-table td { border: 1px solid var(--rule); padding: 0 1.5mm; vertical-align: middle; }
.sig-table thead th { border-bottom-width: 2px; }
.sig-table th { height: 7mm; background: var(--shade); font-weight: 700; text-align: center; white-space: nowrap; }
.sig-table td { height: 6.5mm; white-space: nowrap; }   /* ไม่ใส่ overflow: hidden — ตัดวรรณยุกต์ที่ซ้อนสูง (เช่น ปิ่น) · ชื่อยาวสุดยังพอดีช่อง */
/* แถวรายชื่อ 10.5pt + เลื่อนลง .4mm: ตัวที่สูงสุด (ป+ิ+่ เช่น ปิ่น) ห่างเส้นบน ≈ .5mm, ต่ำสุด (ญ+ู เช่น วรัญญู) ห่างเส้นล่าง ≈ .4mm
   (11.5pt ในแถวสูง 6.5mm ไม่พอ — วรรณยุกต์ชนเส้นบนจนมองไม่เห็น · 11pt พอดีแต่ดูแน่นช่อง) */
.sig-table tbody td { font-size: 10.5pt; padding-top: .4mm; padding-bottom: 0; }
.sig-table .c { text-align: center; }
.sig-table .num { font-variant-numeric: tabular-nums; }

/* ---------- ใบยุติการสอบ ----------
   ใช้ .sig-page / .sig-info / .sig-summary / .sig-table ร่วมกับใบลงชื่อเข้าสอบ (A4 แนวตั้ง 1 หน้าต่อห้อง)
   ไม่มีแถว วิชา | Section → ห้องสอบเป็นแถวล่างสุด · ไม่มีช่องจำนวนและ tag ล่างขวา → แถวสูงขึ้นให้เขียนง่าย (30 แถวยังพอดีหน้า) */
.room-info .sig-cell.room { border-bottom: 0; }   /* ใบยุติการสอบ */
.stop-page .sig-table .col-no { width: 15mm; }
.stop-page .sig-table .col-subj { width: 24mm; }
.stop-page .sig-table .col-sec { width: 19mm; }
.stop-page .sig-table .col-id { width: 32mm; }
.stop-page .sig-table .col-name { width: auto; }   /* ที่เหลือ ≈ 52mm */
.stop-page .sig-table .col-sign { width: 44mm; }
.stop-page .sig-table td { height: 7.2mm; }

/* ---------- ใบรับส่งเอกสาร ----------
   A4 แนวตั้ง 1 ชุดต่อ 1 อาคาร ในแต่ละวัน (ทุกช่วงสอบ) ไม่เกิน 18 แถวต่อหน้า (HAND_ROWS ใน admin.js)
   หัวเอกสารแบบเดียวกับใบลงชื่อเข้าสอบ (.sig-page) + เลขหน้ามุมขวา (.sig-pageno)
   ความสูงแถว 13mm: 18 แถวพอดีหน้า → อาคารเล็กได้วันละ 1 หน้า (ห้องที่มีกรรมการ 3 คนสูงขึ้นเล็กน้อย ยังไม่ล้น) */
.hand-page {
  page: signin;   /* → @page signin (A4 แนวตั้ง) */
  display: flex;
  flex-direction: column;
  width: 210mm;
  height: 297mm;
  padding: 9mm 10mm 7mm;   /* พื้นที่เนื้อหากว้าง 190mm */
  font-size: 11pt;
  line-height: 1.25;
}
.hand-page > * { flex-shrink: 0; }

/* อาคาร | วันสอบ (บรรทัดเดียว) — เส้นแบบเดียวกับตาราง: กรอบนอก 2px · เส้นใน 1px */
.hand-info {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: stretch;
  margin-top: 3.5mm;
  border: 2px solid var(--rule);
  font-size: 14pt;
  font-weight: 700;
  line-height: 1.3;
}
.hand-info p { display: flex; align-items: center; padding: 2mm 4.5mm; white-space: nowrap; }
.hand-info p + p { border-left: 1px solid var(--rule); }
.hand-building { font-size: 16pt; }

/* ตาราง: กรอบนอก 2px (border-collapse เลือกเส้นที่หนากว่าที่ขอบ) · เส้นใน 1px · เส้น 2px คั่นเมื่อเปลี่ยนช่วงสอบ
   หัวตาราง: พื้นเทา · ช่องลงชื่อเทาเข้มกว่า ให้เห็นว่าเป็นช่องที่ต้องเขียน */
.hand-table { width: 100%; margin-top: 3.5mm; table-layout: fixed; border-collapse: collapse; border: 2px solid var(--rule); }
.hand-table th, .hand-table td { border: 1px solid var(--rule); padding: .4mm 2mm; vertical-align: middle; }
.hand-table th { height: 9mm; padding: 0 1mm; background: var(--shade); font-size: 11pt; font-weight: 700; text-align: center; white-space: nowrap; }
.hand-table th.sign { background: #d9d9d9; }
.hand-table thead tr, .hand-table tr.slot-start { border-top: 2px solid var(--rule); }
.hand-table thead tr { border-bottom: 2px solid var(--rule); }
.hand-table td { height: 13mm; font-size: 10.5pt; }
/* ความกว้างคอลัมน์ = ข้อความยาวที่สุดในข้อมูลจริง + ระยะขอบ (เช่น "ห้องคอมพิวเตอร์ 11", ชื่อกรรมการที่มียศ) */
.hand-table .col-no { width: 15mm; }
.hand-table .col-room { width: 36mm; }
.hand-table .col-time { width: 27mm; }
.hand-table .col-proc { width: 59mm; }
.hand-table .col-sign { width: auto; }   /* ที่เหลือแบ่งเท่ากัน = 26.5mm */
.hand-table .c { text-align: center; padding-left: 1mm; padding-right: 1mm; }
.hand-table .num, .hand-table .time { font-variant-numeric: tabular-nums; }
.hand-table .room, .hand-table .time { white-space: nowrap; }
.hand-table .proc { line-height: 1.25; }   /* ชื่อยาวกว่าที่มีในข้อมูลตอนนี้ → ตัดขึ้นบรรทัดใหม่ได้ */

/* ---------- รายงานการส่งตัวนักศึกษา (เมนู จัดการข้อมูลส่งตัว) ----------
   A4 แนวนอน ขอบกระดาษ 0 (เบราว์เซอร์จึงไม่พิมพ์วันที่/ชื่อไฟล์/URL) · admin.js แบ่งหน้าเอง (REP_BODY_MM)
   ทุกหน้า: หัวรายงาน + เลขหน้า + บรรทัดตัวกรอง + หัวตาราง · ความกว้างคอลัมน์ตรงกับ REP_COLS ใน admin.js (รวม 277mm) */
.rep-page {
  page: report;   /* → @page report */
  display: flex;
  flex-direction: column;
  width: 297mm;
  height: 210mm;
  padding: 8mm 10mm;
  font-size: 9.5pt;
  line-height: 1.3;
}
.rep-page > * { flex-shrink: 0; }
.rep-filter { margin-top: 2.5mm !important; font-size: 11pt; font-weight: 600; }
.rep-table { width: 277mm; margin-top: 2.5mm; table-layout: fixed; border-collapse: collapse; border: 2px solid var(--rule); }
.rep-table th, .rep-table td { border: 1px solid var(--rule); padding: 1.1mm 1.4mm; vertical-align: middle; overflow-wrap: anywhere; }
.rep-table th { height: 7mm; background: var(--shade); font-weight: 700; text-align: center; white-space: nowrap; }
.rep-table thead tr { border-bottom: 2px solid var(--rule); }
.rep-table .c { text-align: center; }
.rep-table .nw { white-space: nowrap; }
.rep-table td { font-variant-numeric: tabular-nums; }
.rep-table .rc-0 { width: 8mm; }
.rep-table .rc-1 { width: 26mm; }
.rep-table .rc-2 { width: 17mm; }
.rep-table .rc-3 { width: 27mm; }
.rep-table .rc-4 { width: 36mm; }
.rep-table .rc-5 { width: 27mm; }
.rep-table .rc-6 { width: 26mm; }
.rep-table .rc-7 { width: 32mm; }
.rep-table .rc-8 { width: 14mm; }
.rep-table .rc-9 { width: 34mm; }
.rep-table .rc-10 { width: 30mm; }
