/* ===================================================================
   ระบบจัดทำเอกสารจัดซื้อจัดจ้าง (วิธีเฉพาะเจาะจง)
   สไตล์สำหรับหน้าจอ และการพิมพ์ A4
   =================================================================== */

/* ---------- ฟอนต์ราชการ TH Sarabun New ---------- */
@font-face {
  font-family: 'TH Sarabun New';
  src: local('TH Sarabun New'), local('TH SarabunPSK'), local('Sarabun');
}

:root {
  --gov-blue: #1b4965;
  --gov-blue-light: #2a6f97;
  --gov-accent: #e9c46a;
  --bg: #f2f4f7;
  --card: #ffffff;
  --line: #d7dde5;
  --text: #1f2933;
  --muted: #667085;
  --danger: #c1121f;
  --ok: #2a9d8f;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Sarabun', 'TH Sarabun New', 'Segoe UI', Tahoma, sans-serif;
  font-size: 16px;
}

/* ---------- โครงหน้าจอ ---------- */
.app-header {
  background: linear-gradient(135deg, var(--gov-blue), var(--gov-blue-light));
  color: #fff;
  padding: 14px 22px;
  display: flex;
  align-items: center;
  gap: 14px;
  box-shadow: 0 2px 8px rgba(0,0,0,.15);
  position: sticky;
  top: 0;
  z-index: 50;
}
.app-header .logo {
  font-size: 26px;
  background: rgba(255,255,255,.15);
  width: 46px; height: 46px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 12px;
}
.app-header h1 { font-size: 20px; margin: 0; font-weight: 700; }
.app-header .sub { font-size: 13px; opacity: .85; margin-top: 2px; }

.layout { display: flex; min-height: calc(100vh - 74px); }

.sidebar {
  width: 240px;
  background: var(--card);
  border-right: 1px solid var(--line);
  padding: 12px;
  flex-shrink: 0;
}
.nav-btn {
  display: flex; align-items: center; gap: 10px;
  width: 100%;
  padding: 11px 13px;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
  text-align: left;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 4px;
  transition: .12s;
}
.nav-btn:hover { background: #eef2f7; }
.nav-btn.active { background: var(--gov-blue); color: #fff; font-weight: 600; }
.nav-btn .ic { font-size: 18px; width: 22px; text-align: center; }

.content { flex: 1; padding: 22px 28px; max-width: 1100px; }

/* ---------- การ์ด / ฟอร์ม ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px 22px;
  margin-bottom: 18px;
}
.card h2 { margin: 0 0 4px; font-size: 19px; color: var(--gov-blue); }
.card .hint { color: var(--muted); font-size: 14px; margin: 0 0 16px; }

.page-title { font-size: 22px; margin: 0 0 4px; color: var(--gov-blue); font-weight: 700; }
.page-desc { color: var(--muted); margin: 0 0 18px; font-size: 15px; }

.grid { display: grid; gap: 14px; }
.grid-2 { grid-template-columns: 1fr 1fr; }
.grid-3 { grid-template-columns: 1fr 1fr 1fr; }
@media (max-width: 780px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } }

.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 14px; font-weight: 600; color: #344054; }
.field .desc { font-size: 12.5px; color: var(--muted); font-weight: 400; }
input, select, textarea {
  font-family: inherit;
  font-size: 15px;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  width: 100%;
}
/* ช่องติ๊ก/ตัวเลือกวงกลม ไม่ต้องยืดเต็มความกว้างเหมือนช่องกรอกข้อความ */
input[type="checkbox"], input[type="radio"] {
  width: auto;
  flex: none;
  padding: 0;
  accent-color: var(--gov-blue-light);
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--gov-blue-light);
  box-shadow: 0 0 0 3px rgba(42,111,151,.15);
}
textarea { resize: vertical; min-height: 70px; }

/* ---------- ปุ่ม ---------- */
.btn {
  font-family: inherit;
  font-size: 15px;
  padding: 10px 18px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  transition: .12s;
}
.btn-primary { background: var(--gov-blue); color: #fff; }
.btn-primary:hover { background: var(--gov-blue-light); }
.btn-ok { background: var(--ok); color: #fff; }
.btn-ghost { background: #eef2f7; color: var(--text); }
.btn-ghost:hover { background: #e2e8f0; }
.btn-danger { background: #fdecec; color: var(--danger); }
.btn-danger:hover { background: #fbd9d9; }
.btn-sm { padding: 6px 12px; font-size: 13.5px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; align-items: center; }
.spacer { flex: 1; }

/* ---------- ตาราง ---------- */
table.data { width: 100%; border-collapse: collapse; font-size: 14.5px; }
table.data th, table.data td {
  border: 1px solid var(--line);
  padding: 8px 10px;
  text-align: left;
}
table.data th { background: #f0f4f8; font-weight: 600; color: #344054; }
table.data tr:hover td { background: #fafbfc; }
.text-right { text-align: right !important; }
.text-center { text-align: center !important; }

/* ---------- รายการงาน ---------- */
.job-item {
  display: flex; align-items: center; gap: 14px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  margin-bottom: 10px;
  background: #fff;
  cursor: pointer;
  transition: .12s;
}
.job-item:hover { border-color: var(--gov-blue-light); box-shadow: 0 2px 10px rgba(0,0,0,.06); }
.job-item .job-icon {
  width: 42px; height: 42px; border-radius: 10px;
  background: #eef4f9; color: var(--gov-blue);
  display: flex; align-items: center; justify-content: center; font-size: 20px;
  flex-shrink: 0;
}
.job-item .job-main { flex: 1; }
.job-item .job-name { font-weight: 600; font-size: 16px; }
.job-item .job-meta { color: var(--muted); font-size: 13.5px; margin-top: 2px; }
.job-item .job-amount { font-weight: 700; color: var(--gov-blue); font-size: 16px; }

.badge {
  display: inline-block; padding: 2px 9px; border-radius: 20px;
  font-size: 12px; font-weight: 600; background: #eef4f9; color: var(--gov-blue-light);
}

.empty {
  text-align: center; color: var(--muted); padding: 40px 20px;
  border: 2px dashed var(--line); border-radius: 12px;
}
.empty .big { font-size: 40px; margin-bottom: 10px; }

/* ---------- แถวรายการวัสดุ (แก้ไข) ---------- */
.item-row { display: grid; grid-template-columns: 2.4fr 2.2fr .8fr .9fr 1.1fr 40px; gap: 8px; margin-bottom: 8px; align-items: center; }
.item-row .rm { color: var(--danger); cursor: pointer; text-align: center; font-size: 20px; background: none; border: none; }
.item-head { font-size: 13px; font-weight: 600; color: var(--muted); }

/* ---------- เอกสารสำหรับพิมพ์ ---------- */
.doc-toolbar {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 16px;
  margin-bottom: 18px;
  display: flex; gap: 10px; flex-wrap: wrap; align-items: center;
  position: sticky; top: 84px; z-index: 20;
}
.doc-select { min-width: 320px; }

.doc-stack { display: flex; flex-direction: column; align-items: center; gap: 22px; }

.doc-page {
  background: #fff;
  width: 210mm;
  min-height: 297mm;
  padding: 15mm 20mm 15mm 25mm; /* ระยะขอบหนังสือราชการ: ซ้าย 3ซม บน/ล่าง 2.5, ขวา 2 */
  box-shadow: 0 3px 18px rgba(0,0,0,.14);
  font-family: 'TH Sarabun New', 'TH SarabunPSK', 'Sarabun', sans-serif;
  font-size: 16pt;
  line-height: 1.35;
  color: #000;
  position: relative;
}

.keep-digits::before { content: attr(data-t); }

/* กระดาษแนวนอน — ทะเบียนคุมทรัพย์สิน */
@page landscape { size: A4 landscape; margin: 0; }
.doc-page.landscape {
  width: 297mm; min-height: 210mm; padding: 9mm 10mm 8mm 12mm;
  font-size: 13.5pt; line-height: 1.3; page: landscape;
}
.doc-page.landscape .fill { display: inline-block; border-bottom: 1px dotted #000; padding: 0 1mm; line-height: 1.15; }
.doc-page.landscape .asset-checks { display: flex; gap: 8mm; flex-wrap: wrap; }
.doc-page.landscape .asset-checks .lbl { width: 24mm; }
.doc-page table.gov.asset-card { font-size: 12.5pt; margin: 2mm 0 0; table-layout: fixed; }
.doc-page table.gov.asset-card th { font-size: 12pt; line-height: 1.1; padding: 2px 3px; vertical-align: middle; }
.doc-page table.gov.asset-card td { padding: 1px 4px; line-height: 1.2; height: 6.4mm; }
.doc-page table.gov.asset-card .asset-remark { vertical-align: top; text-align: center; color: #c00; padding-top: 2mm; }

/* แบบกระชับ ให้พอดี ๑ หน้า (บันทึกข้อความ) */
.doc-page.compact { font-size: 15pt; line-height: 1.18; padding: 10mm 20mm 10mm 25mm; }
.doc-page.compact .memo-head { font-size: 19pt; margin-bottom: 1.5mm; }
.doc-page.compact .doc-hr { margin: 1.5mm 0 2.5mm; }
.doc-page.compact .memo-line { margin-bottom: 0.8mm; }
.doc-page.compact .doc-body p { margin-bottom: 1.5mm; }
.doc-page.compact .sign-area { margin-top: 4mm; }
.doc-page.compact .sign-grid { margin-top: 5mm; gap: 6mm; }
.doc-page.compact .garuda { margin-bottom: 0; }
.doc-page.compact .garuda img { height: 15mm; }

/* องค์ประกอบหนังสือราชการ — ตราครุฑชิดซ้ายบน */
.doc-page .garuda { text-align: left; margin-bottom: 1mm; }
.doc-page .garuda img { height: 18mm; width: auto; display: inline-block; }
.doc-page .garuda.center { text-align: center; margin-bottom: 2mm; }   /* ครุฑกลางหน้า — ประกาศ/คำสั่ง */
.doc-page .garuda.center img { height: 30mm; }
.doc-title-center { text-align: center; font-weight: bold; font-size: 20pt; margin: 0 0 6mm; }
.memo-head { font-weight: bold; font-size: 22pt; text-align: center; margin: 0 0 3mm; letter-spacing: 1px; }
.memo-line { display: flex; gap: 6px; margin-bottom: 1.5mm; }
.memo-line .lbl { font-weight: bold; white-space: nowrap; }
.memo-line .val { flex: 1; }
.doc-hr { border: none; border-top: 1px solid #000; margin: 2mm 0 4mm; }

.doc-body { text-indent: 0; }
.doc-body p { margin: 0 0 3mm; text-align: justify; text-justify: inter-word; }
.doc-body p.indent { text-indent: 25mm; }
.doc-body p.indent-sm { text-indent: 12mm; }

.doc-page table.gov { width: 100%; border-collapse: collapse; font-size: 15pt; margin: 3mm 0; }
.doc-page table.gov th, .doc-page table.gov td { border: 1px solid #000; padding: 3px 6px; }
.doc-page table.gov th { text-align: center; font-weight: bold; }

/* บล็อกลายเซ็น */
.sign-area { margin-top: 8mm; display: flex; justify-content: flex-end; }
.sign-area.center { justify-content: center; }
.sign-area.between { justify-content: space-between; }
.sign-box { text-align: center; min-width: 70mm; }
.sign-box .line { margin-bottom: 1mm; }
.sign-box .dots { letter-spacing: 1px; }
.sign-grid { display: flex; justify-content: space-around; flex-wrap: wrap; gap: 10mm; margin-top: 10mm; }

.approve-box {
  border: 1px solid #000; padding: 4mm 6mm; margin-top: 6mm; width: 80mm;
}

.page-num { position: absolute; top: 8mm; right: 20mm; font-size: 15pt; }
.doc-footer { position: absolute; left: 0; right: 0; bottom: 10mm; text-align: center; font-size: 15pt; font-style: italic; }

/* ---------- ตั้งค่าเลือกเอกสารพิมพ์หลายฉบับ ---------- */
.chk-list { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 18px; margin: 6px 0 0; }
.chk-list label { display: flex; gap: 8px; align-items: center; font-size: 14px; font-weight: 500; cursor: pointer; }
.chk-list input { width: auto; }

/* ---------- แจ้งเตือน ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #1f2933; color: #fff; padding: 12px 22px; border-radius: 10px;
  font-size: 15px; box-shadow: 0 6px 24px rgba(0,0,0,.25); z-index: 999;
  opacity: 0; transition: .25s; pointer-events: none;
}
.toast.show { opacity: 1; bottom: 34px; }
.toast.ok { background: var(--ok); }
.toast.err { background: var(--danger); }

.pill-info {
  background: #fff8e6; border: 1px solid #f0d98c; color: #7a5c00;
  padding: 10px 14px; border-radius: 8px; font-size: 14px; margin-bottom: 16px;
}

/* ---------- การพิมพ์ ---------- */
@media print {
  @page { size: A4; margin: 0; }
  body { background: #fff; }
  .app-header, .sidebar, .doc-toolbar, .no-print { display: none !important; }
  .toast { display: none !important; }   /* ไม่ให้ข้อความแจ้งเตือนติดไปกับกระดาษที่พิมพ์ */
  .content { padding: 0; max-width: none; }
  .layout { display: block; }
  .doc-stack { gap: 0; display: block; }
  .doc-page {
    box-shadow: none;
    width: 210mm;
    min-height: 297mm;
    page-break-after: always;
    margin: 0;
  }
  .doc-page:last-child { page-break-after: auto; }
  .doc-page.landscape { width: 297mm; min-height: 210mm; }
}

/* ---------- ทะเบียนคุมการจัดซื้อ / จัดจ้าง (A4 แนวนอน) ---------- */
.doc-page table.gov.reg-table { font-size: 12.5pt; margin: 1mm 0 0; table-layout: fixed; }
.doc-page table.gov.reg-table th { font-size: 12.5pt; line-height: 1.15; padding: 2px 3px; vertical-align: middle; font-weight: normal; }
.doc-page table.gov.reg-table td { padding: 1px 4px; line-height: 1.2; height: 6.4mm; overflow-wrap: anywhere; font-size: 12pt; }

/* ---------- เอกสารหมายเลข (ตรวจสอบพัสดุประจำปี) — แนวนอน ๑๕–๑๖ คอลัมน์ ---------- */
.doc-page table.gov.au-table { font-size: 12pt; margin: 2mm 0 0; table-layout: fixed; }
.doc-page table.gov.au-table th { font-size: 11pt; line-height: 1.1; padding: 1px 2px; vertical-align: middle; }
.doc-page table.gov.au-table td { padding: 1px 3px; line-height: 1.2; height: 6.4mm; overflow-wrap: anywhere; }
.doc-page table.gov.au-table td.text-right { white-space: nowrap; font-size: 10.5pt; }
.doc-page table.gov.au-table .keep-digits { font-size: 10pt; white-space: nowrap; }
.doc-page table.gov.au-table td.au-date { white-space: nowrap; font-size: 10.5pt; }

/* คำที่ห้ามตัดกลางคำตอนขึ้นบรรทัดใหม่ (ดู protectThaiWords ใน util.js) */
.nw { white-space: nowrap; }

/* ---------- แก้ไขเอกสารก่อนพิมพ์ (docedit.js) ---------- */
.doc-wrap { display: flex; flex-direction: column; align-items: center; gap: 22px; }
.doc-edit-badge { background: #fff8e1; border: 1px solid #f0d98c; color: #7a5c00; border-radius: 8px; padding: 6px 12px; font-size: 13px; display: flex; gap: 10px; align-items: center; }
.doc-page[contenteditable="true"] { outline: 2px dashed #e0b84a; outline-offset: 4px; cursor: text; }
@media print {
  .doc-wrap { display: block; }
  .doc-wrap > .doc-page:last-child { page-break-after: always; }
  .doc-stack > .doc-wrap:last-child > .doc-page:last-child { page-break-after: auto; }
  .doc-page[contenteditable="true"] { outline: none; }
}

/* ---------- ตัวเลือกคำที่เคยพิมพ์ (autocomplete.js) ---------- */
.ac-menu { position: absolute; z-index: 1000; background: #fff; border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,.16); max-height: 300px; overflow-y: auto; font-size: 14px; }
.ac-head { padding: 6px 10px; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--line); }
.ac-item { padding: 8px 10px; cursor: pointer; line-height: 1.4; border-bottom: 1px solid #f2f4f6; }
.ac-item:last-child { border-bottom: 0; }
.ac-item:hover, .ac-item.active { background: #eef6ff; }
.ac-item b { color: var(--gov-blue, #1d4f86); }

/* ---------- ใบสั่งจ้าง (แบบโรงเรียน) ---------- */
.doc-page.po-hire { line-height: 1.45; }
.doc-page.po-hire p { margin: 0; }
.po-head { display: grid; grid-template-columns: 55% 45%; gap: 4mm; }
.po-head p { margin: 0 0 2.5mm; }
.doc-page table.gov.po-table { table-layout: fixed; }
.doc-page table.gov.po-table th { text-align: left; vertical-align: top; line-height: 1.25; }
.doc-page table.gov.po-table td { vertical-align: top; height: 9mm; }
.po-cond { display: grid; grid-template-columns: 9mm 1fr; margin: 0 0 2mm; }
.doc-page.po-hire p.po-para { margin: 0 0 1mm; text-align: justify; }
.po-signs { margin: 5mm 0 0 45mm; text-align: center; line-height: 1.7; }
/* ใบสั่งจ้างยาวเกิน ๒ หน้า → กระชับทีละขั้น (hireDoc08po เลือกให้อัตโนมัติ) */
.doc-page.po-hire.po-l1 { line-height: 1.3; }
.po-l1 .po-head p { margin-bottom: 1.2mm; }
.po-l1 .po-cond { margin-bottom: 1mm; }
.po-l1 .po-signs { line-height: 1.45; margin-top: 3mm; }
.doc-page.po-l1 table.gov.po-table td { height: 7.5mm; }
.doc-page.po-hire.po-l2 { font-size: 15pt; }
.doc-page.po-l2 table.gov.po-table { font-size: 14pt; }
.po-l2 .garuda.center img { height: 24mm; }
.doc-page.po-hire.po-l3 { font-size: 14pt; line-height: 1.22; }
.doc-page.po-l3 table.gov.po-table { font-size: 13pt; }
.po-l3 .garuda.center img { height: 20mm; }

/* ---------- บัญชีวัสดุ แบบ พ.ด. ๔ ---------- */
.doc-page.landscape.pd4 { font-size: 15pt; line-height: 1.35; padding: 8mm 12mm 8mm 12mm; position: relative; }
.pd4 .pd4-pno { text-align: right; font-size: 13pt; }
.pd4 .pd4-head { position: relative; text-align: center; margin-top: -2mm; }
.pd4 .pd4-title { font-size: 22pt; font-weight: bold; }
.pd4 .pd4-form { position: absolute; right: 0; top: 1mm; border: 1px solid #000; padding: 0 3mm; font-size: 14pt; }
.pd4 .pd4-org { text-align: right; }
.pd4 .pd4-line { display: flex; align-items: baseline; gap: 2mm; margin-top: 1mm; }
.pd4 .pd4-line > span { white-space: nowrap; }
.pd4 .pd4-line > span.pd4-spec { white-space: normal; flex: 1; min-width: 0; }
.pd4 .pd4-wrapfill { border-bottom: 1px dotted #000; padding: 0 1mm; line-height: 1.5; }
.pd4 .pd4-line > span.pd4-right { width: 62mm; flex: 0 0 62mm; display: flex; align-items: baseline; gap: 1mm; align-self: flex-start; }
.pd4 .pd4-rfill { flex: 1; border-bottom: 1px dotted #000; height: 1em; }
.pd4 table.pd4-table { table-layout: fixed; margin-top: 2mm; width: 100%; }
.pd4 table.pd4-table th { font-size: 15pt; line-height: 1.15; padding: 1mm 1mm; vertical-align: middle; }
.pd4 table.pd4-table td { font-size: 15pt; line-height: 1.2; height: 7.4mm; padding: 0 1.5mm; vertical-align: top; overflow-wrap: anywhere; }

/* ---------- ส่งเบิกจ่าย (การเงิน) — finlink.js ---------- */
.fin-st { font-size: 13.5px; }
.fin-st.ok { color: #1a7f5a; font-weight: 600; }
.fin-st.wait { color: #8a5a00; }
.fin-st.muted { color: var(--muted); }

/* ---------- แถบสถานะคลาวด์ (cloudsync.js) ---------- */
.cloud-bar { display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; padding: 8px 18px;
  background: #f3f7fb; border-bottom: 1px solid var(--line); font-size: 14px; }
.cloud-bar .cb-st.ok { color: #1a6b3c; }
.cloud-bar .cb-st.wait { color: #8a6d00; }
.cloud-bar .cb-st.warn { color: #a05a00; font-weight: 600; }
.cloud-bar .cb-st.err { color: #a01722; font-weight: 600; }
.cloud-bar .cb-legacy { background: #fff6df; border: 1px solid #f0d58a; border-radius: 8px; padding: 4px 8px; }
.cloud-bar .cb-user { color: #555; }
body.cloud-locked .sidebar { display: none; }
@media print { .cloud-bar { display: none !important; } }
