/* =====================================================================
   EduNexus — มาตรฐานการพิมพ์เอกสาร (โหลดทุกหน้า ต่อจาก app.css)
   ---------------------------------------------------------------------
   1) แผ่นกระดาษ A4 แบบ WYSIWYG  (.paper-stage > .paper)
      - บนจอแสดงเป็นกระดาษ A4 ขนาดจริง (มม.) — สิ่งที่เห็น = สิ่งที่พิมพ์ออกมา 1:1
      - ระยะขอบกระดาษ = padding ของแผ่น (--pm)  ·  ตอนพิมพ์ @page margin: 0
        → เบราว์เซอร์ไม่เติมขอบซ้ำ ไม่ย่อ (shrink-to-fit) ไม่เกิดหน้าว่าง และไม่พิมพ์หัว/ท้ายกระดาษของเบราว์เซอร์
      - เนื้อหายาวเกิน 1 หน้า: ใส่ data-paginate ที่ .paper → App.paper ตัดหน้าให้บนจอเลย
        ตารางยาวตัดตามแถว (หัวตาราง thead ซ้ำทุกหน้า, tfoot อยู่หน้าสุดท้าย), กล่อง [data-paper-split] ตัดตามลูก
        องค์ประกอบ [data-paper-repeat] (เช่นหัวกระดาษย่อ) ทำซ้ำทุกหน้าต่อ · [data-page-no]/[data-page-count] = เลขหน้า
      - มือถือ/แท็บเล็ต: ย่อทั้งชุดด้วย zoom ให้พอดีจอ (เฉพาะบนจอ) — ตอนพิมพ์กลับเป็นขนาดจริงเสมอ
   2) Baseline สำหรับหน้ารายงานทั่วไปที่ไม่ได้ใช้ .paper
      - ซ่อนเมนู/ปุ่ม/ตัวกรอง, ตารางไม่ถูกตัดขวา, หัวตารางซ้ำทุกหน้า, ไม่ตัดกลางแถว
      - สีพื้นหลัง/แถบสีพิมพ์ออกตามจอ (ไม่ต้องติ๊ก "กราฟิกพื้นหลัง")
   ===================================================================== */

/* ฟอนต์เอกสารราชการ — Sarabun (SIL OFL) ใกล้เคียง TH Sarabun New */
@font-face { font-family: 'DocSarabun'; font-style: normal; font-weight: 400; font-display: block; src: url('../vendor/fonts/Sarabun-Regular.ttf') format('truetype'); }
@font-face { font-family: 'DocSarabun'; font-style: normal; font-weight: 700; font-display: block; src: url('../vendor/fonts/Sarabun-Bold.ttf') format('truetype'); }

/* ---------- เวที + แผ่นกระดาษ (บนจอ) ---------- */
.paper-stage { display: flex; flex-direction: column; align-items: center; gap: 22px; padding: 4px 0 34px; width: 100%; min-width: 0; }
.paper {
  --pm: 15mm 15mm 15mm 15mm;                 /* ระยะขอบ บน ขวา ล่าง ซ้าย (ปรับได้ต่อเอกสาร) */
  --doc-font: 'DocSarabun', 'Sarabun', 'TH Sarabun New', 'Anuphan', sans-serif;
  --ink: #000; --ink-2: #333; --rule: #000; --rule-soft: #8a8a8a; --shade: #efefef;
  position: relative; display: block; flex-shrink: 0; box-sizing: border-box;
  width: 210mm; height: 297mm; padding: var(--pm); overflow: hidden;
  background: #fff; color: var(--ink); font-family: var(--doc-font); font-size: 11pt; line-height: 1.42; text-align: left;
  border-radius: 2px; box-shadow: 0 0 0 1px rgba(15, 23, 42, .06), 0 1px 2px rgba(15, 23, 42, .06), 0 18px 40px -22px rgba(15, 23, 42, .4);
  -webkit-print-color-adjust: exact; print-color-adjust: exact;
}
.paper.landscape { width: 297mm; height: 210mm; }
.paper.ui-font { --doc-font: var(--font); }   /* เอกสารเชิงออกแบบ (เช่นบัตร) ใช้ฟอนต์ระบบ */
.paper *, .paper *::before, .paper *::after { box-sizing: border-box; }
.paper h1, .paper h2, .paper h3, .paper h4, .paper h5 { margin: 0; color: var(--ink); font-family: inherit; letter-spacing: 0; line-height: 1.3; }
.paper p { margin: 0 0 1.5mm; }
.paper img { max-width: 100%; }
.paper a { color: inherit; text-decoration: none; }
.paper .muted, .paper .muted-2 { color: var(--ink-2); }
.paper .badge { border: .5pt solid currentColor; background: none !important; padding: 0 1.4mm; border-radius: 1mm; font-size: .86em; }
.paper .c { text-align: center; } .paper .r { text-align: right; } .paper .nw { white-space: nowrap; } .paper .b { font-weight: 700; }
.paper .num { font-variant-numeric: tabular-nums; }
.paper .grow { flex: 1; min-width: 0; }
.paper .mt-1 { margin-top: 1mm; } .paper .mt-2 { margin-top: 2mm; } .paper .mt-3 { margin-top: 3mm; } .paper .mt-4 { margin-top: 4mm; } .paper .mt-6 { margin-top: 6mm; }
.paper .mb-1 { margin-bottom: 1mm; } .paper .mb-2 { margin-bottom: 2mm; } .paper .mb-3 { margin-bottom: 3mm; } .paper .mb-4 { margin-bottom: 4mm; }
.paper .paper-push { margin-top: auto; }

/* หัวเอกสาร: แบบกึ่งกลาง (ทางการ) */
.doc-head { text-align: center; margin-bottom: 4mm; }
.doc-head .doc-logo { display: block; width: 20mm; height: 20mm; object-fit: contain; margin: 0 auto 2mm; }
.doc-head .doc-logo.garuda { width: 15mm; height: 16.5mm; }
.doc-head .doc-school { font-size: 13pt; font-weight: 700; line-height: 1.3; }
.doc-head .doc-aff { font-size: 10.5pt; line-height: 1.3; }
.doc-head .doc-title { font-size: 14pt; font-weight: 700; line-height: 1.3; margin-top: 1.5mm; }
.doc-head .doc-sub { font-size: 11pt; line-height: 1.35; }
/* หัวเอกสาร: แบบแถว (โลโก้ซ้าย · ชื่อโรงเรียน · ชื่อเอกสารขวา) สำหรับรายงาน/ตาราง */
.doc-head.row { display: flex; align-items: center; gap: 4mm; text-align: left; padding-bottom: 2.5mm; border-bottom: 1pt solid var(--rule); }
.doc-head.row .doc-logo { margin: 0; width: 15mm; height: 15mm; flex-shrink: 0; }
.doc-head.row .doc-id { flex: 1; min-width: 0; }
.doc-head.row .doc-school { font-size: 12.5pt; }
.doc-head.row .doc-right { text-align: right; flex-shrink: 0; max-width: 55%; }
.doc-head.row .doc-title { margin-top: 0; font-size: 13pt; }
.doc-head.compact { margin-bottom: 2.5mm; }
.doc-head.compact .doc-logo { width: 11mm; height: 11mm; }
.doc-head.compact .doc-school { font-size: 11pt; }
.doc-head.compact .doc-title { font-size: 11.5pt; }

/* ช่องข้อมูลแบบเส้นประ: <span class="doc-f"><b>ชื่อ</b><span class="v">ค่า</span></span> */
.doc-fields { display: flex; flex-wrap: wrap; gap: 1.2mm 5mm; margin: 1mm 0 2.5mm; }
.doc-f { display: inline-flex; align-items: baseline; gap: 1.5mm; min-width: 0; max-width: 100%; }
.doc-f > b, .doc-f > .l { font-weight: 700; white-space: nowrap; }
.doc-f > .v { flex: 1; min-width: 16mm; border-bottom: .5pt dotted var(--ink); padding: 0 1.5mm; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-f.full { display: flex; width: 100%; }
.doc-kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: .8mm 4mm; }
.doc-kv > dt { font-weight: 700; } .doc-kv > dd { margin: 0; }

/* ตารางทางการ: เส้นดำบาง หัวตารางเทาอ่อน */
.doc-table { width: 100%; border-collapse: collapse; font-size: 10pt; line-height: 1.3; table-layout: auto; }
.doc-table th, .doc-table td { border: .6pt solid var(--rule); padding: 1.1mm 1.6mm; vertical-align: top; color: var(--ink); }
.doc-table thead th { font-weight: 700; text-align: center; vertical-align: middle; background: var(--shade); }
.doc-table tfoot td, .doc-table tfoot th { font-weight: 700; background: #f7f7f7; }
.doc-table.sm { font-size: 9pt; } .doc-table.sm th, .doc-table.sm td { padding: .8mm 1.2mm; }
.doc-table.xs { font-size: 8pt; } .doc-table.xs th, .doc-table.xs td { padding: .5mm 1mm; }
.doc-table.fixed { table-layout: fixed; }
.doc-table.mid td { vertical-align: middle; }
.doc-table td.c, .doc-table th.c { text-align: center; } .doc-table td.r, .doc-table th.r { text-align: right; }
.doc-table tr { break-inside: avoid; }
.doc-table .ell { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-section, .paper .doc-section { margin: 3.5mm 0 1.5mm; font-weight: 700; font-size: 11.5pt; }
.doc-note { font-size: 9pt; color: var(--ink-2); line-height: 1.35; }
.doc-box { border: .6pt solid var(--rule); padding: 2mm 3mm; }

/* ลายมือชื่อ: <div class="doc-signs"> <div class="doc-sign"> ... </div> </div> */
.doc-signs { display: flex; justify-content: space-around; align-items: flex-start; gap: 4mm; margin-top: 8mm; break-inside: avoid; }
.doc-signs.end { justify-content: flex-end; }
.doc-signs.between { justify-content: space-between; }
.doc-sign { flex: 0 1 62mm; min-width: 0; text-align: center; font-size: 10.5pt; line-height: 1.45; }
.doc-sign .ln { display: flex; align-items: flex-end; justify-content: center; gap: 1.5mm; height: 9mm; white-space: nowrap; }
.doc-sign .ln i { display: inline-block; width: 44mm; border-bottom: .5pt dotted var(--ink); font-style: normal; }
.doc-sign .nm { white-space: nowrap; }
.doc-sign .ps { white-space: nowrap; }

/* ท้ายกระดาษ (อยู่ในระยะขอบล่าง): เลขหน้า / วันที่พิมพ์ */
.paper-foot { position: absolute; left: max(8mm, var(--pm-l, 15mm)); right: max(8mm, var(--pm-r, 15mm)); bottom: 6mm; display: flex; justify-content: space-between; gap: 6mm; font-size: 8pt; line-height: 1.2; color: #555; white-space: nowrap; }
.paper.single .paper-foot [data-page-multi] { display: none; }
.paper:not(.paper-cont-page) > [data-paper-cont-only] { display: none; }  /* หัวกระดาษย่อเฉพาะหน้าต่อ: doc_head(..., ['repeat' => 'cont']) */
.paper-cont-mark { font-size: 9pt; color: var(--ink-2); text-align: right; margin-bottom: 1mm; }

/* ตัดหน้า (JS): ตาราง/กล่องที่ต่อจากหน้าก่อน */
.paper .paper-cont > caption, .paper .paper-cont .paper-cont-hide { display: none; }

/* มือถือ/แท็บเล็ต: ระยะห่างระหว่างแผ่นเล็กลง (zoom ตั้งโดย App.paper.fit) */
@media screen and (max-width: 1023px) {
  .paper-stage { gap: 12px; padding-bottom: 20px; }
}

/* =====================================================================
   พิมพ์
   ===================================================================== */
@media print {
  @page paper-p { size: A4 portrait; margin: 0; }
  @page paper-l { size: A4 landscape; margin: 0; }

  html, body { background: #fff !important; }
  *, *::before, *::after { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; animation: none !important; transition: none !important; }
  body { min-height: 0 !important; padding: 0 !important; }
  .app, .app-main { display: block !important; margin: 0 !important; padding: 0 !important; min-height: 0 !important; width: auto !important; max-width: none !important; overflow: visible !important; }
  .page { margin: 0 !important; padding: 0 !important; max-width: none !important; width: auto !important; }
  .sidebar, .topbar, .tabbar, .app-footer, .page-actions, .no-print, .filters, .pager, #toasts, .app-bg, .sb-backdrop, .modal, .modal-backdrop, .dropdown-menu, .fab, .tabs, .segmented, .page-crumbs, .ph-title-ico, [data-tip]::after, [data-tip]::before { display: none !important; }
  body:has(.print-head) .page-head { display: none !important; }
  .page-head { margin-bottom: 4mm !important; }

  /* ตาราง: ไม่ถูกตัดขวา · หัวซ้ำทุกหน้า · ไม่ตัดกลางแถว */
  .table-wrap, .scroll-x, .table-scroll { overflow: visible !important; max-height: none !important; }
  .table-wrap > .table, .table { min-width: 0 !important; }
  thead { display: table-header-group; }
  tfoot { display: table-footer-group; }
  tr, img, svg, canvas, .stat, .kpi, .doc-signs, .print-keep { break-inside: avoid; }
  h1, h2, h3, h4, .card-head, .card-title, .doc-section { break-after: avoid; }
  .table th, .acad-gb th, .acad-gb td, .acad-matrix th, .acad-matrix td, .cr-matrix th, .cr-matrix td, .asg-matrix th, .asg-matrix td { position: static !important; }
  .card { box-shadow: none !important; }
  a { color: inherit !important; text-decoration: none !important; }

  /* กราฟ/ภาพไม่ล้นกระดาษ · sparkline วาดเส้นเต็ม */
  .apexcharts-canvas, .apexcharts-svg, canvas, svg, img { max-width: 100% !important; }
  [data-spark] path { stroke-dashoffset: 0 !important; stroke-dasharray: none !important; }

  /* ---------- โหมดกระดาษ: พิมพ์เฉพาะแผ่น A4 ขนาดจริง ---------- */
  body.paper-mode .page :not(.paper-stage):not(:has(.paper-stage)):not(.paper-stage *) { display: none !important; }
  body.paper-mode .page :has(.paper-stage) { display: block !important; margin: 0 !important; padding: 0 !important; border: 0 !important; box-shadow: none !important; background: none !important; max-width: none !important; width: auto !important; overflow: visible !important; }
  .paper-stage { display: block !important; zoom: 1 !important; transform: none !important; padding: 0 !important; margin: 0 !important; gap: 0 !important; width: auto !important; }
  .paper { margin: 0 !important; box-shadow: none !important; border-radius: 0 !important; page: paper-p; break-after: page; break-inside: avoid; }
  .paper.landscape { page: paper-l; }
  .paper.paper-last, .paper-stage:last-of-type > .paper:last-child { break-after: auto; }
  .paper-ghost { border: 0 !important; }
}

/* คำแนะนำเหนือเอกสาร (บนจอเท่านั้น) */
.paper-hint { display: flex; align-items: center; justify-content: center; gap: 8px; max-width: 210mm; margin: 0 auto 12px; font-size: 12.5px; color: var(--muted); text-align: center; }
.paper-hint i { font-size: 16px; color: var(--brand); flex-shrink: 0; }

/* tfoot แบบข้อความท้ายตาราง (หมายเหตุ/ช่องลงนามใน tfoot) — ไม่หนา ไม่มีพื้นเทา ไม่มีเส้น */
.doc-table tfoot.plain td, .doc-table tfoot.plain th { font-weight: 400; background: none; border: 0; padding-left: 0; padding-right: 0; }

/* หัวเอกสารแบบแถว: ไม่ให้เหลือคำเดียวค้างบรรทัด */
.doc-head.row .doc-aff, .doc-head.row .doc-right > div { text-wrap: balance; }

/* Baseline: ตารางรายการทั่วไป (.table) เมื่อกด Ctrl+P บนหน้าที่ไม่ใช่เอกสาร — กระชับให้พอดีกระดาษ ไม่ถูกย่อทั้งหน้า */
@media print {
  body:not(.paper-mode) .table { font-size: 9pt; }
  body:not(.paper-mode) .table th, body:not(.paper-mode) .table td { padding: 1.2mm 1.6mm; }
  body:not(.paper-mode) .table th { white-space: normal; font-size: 8pt; }
  body:not(.paper-mode) .table td.actions, body:not(.paper-mode) .table th.actions { display: none; }
  body:not(.paper-mode) .cell-user .meta b { white-space: normal; }
  body:not(.paper-mode) .table .avatar { width: 6mm; height: 6mm; font-size: 6pt; }
  body:not(.paper-mode) .badge, body:not(.paper-mode) .status-pill { white-space: normal; height: auto; min-width: 0; line-height: 1.25; }
}

/* การ์ดที่สูงกว่า 1 หน้าไม่ถูกดันไปหน้าใหม่จนเหลือหน้าว่าง/ถูกตัด — กันตัดเฉพาะการ์ดสถิติเล็ก */
@media print {
  .card { break-inside: auto; }
  .stat, .kpi, .card.print-keep, .stat-card { break-inside: avoid; }
}
