:root {
  --bg: #f3f5f9; --panel: #ffffff; --ink: #1f2937; --muted: #6b7280; --line: #e5e7eb;
  --brand: #1e3a8a; --brand-2: #2563eb; --brand-soft: #dbeafe; --accent: #b45309;
  --ok: #15803d; --ok-soft: #dcfce7; --warn: #b45309; --warn-soft: #fef3c7; --bad: #b91c1c; --bad-soft: #fee2e2;
  --radius: 10px; --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .1);
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body { font-family: 'Sarabun', 'TH Sarabun New', 'Tahoma', system-ui, sans-serif; font-size: 16px; color: var(--ink); background: var(--bg); line-height: 1.5; }
a { color: var(--brand-2); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0 0 .5rem; line-height: 1.3; font-weight: 600; }
h1 { font-size: 1.5rem; } h2 { font-size: 1.25rem; } h3 { font-size: 1.05rem; }
p { margin: 0 0 .75rem; }
.boot { padding: 3rem; text-align: center; color: var(--muted); }
.muted { color: var(--muted); } .small { font-size: .875rem; } .tiny { font-size: .8rem; }
.right { text-align: right; } .center { text-align: center; } .nowrap { white-space: nowrap; }
.mt { margin-top: 1rem; } .mb { margin-bottom: 1rem; } .mt-s { margin-top: .5rem; } .mb-s { margin-bottom: .5rem; }
.flex { display: flex; gap: .75rem; align-items: center; flex-wrap: wrap; } .flex.between { justify-content: space-between; } .grow { flex: 1; }
.grid { display: grid; gap: 1rem; } .grid-2 { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); } .grid-3 { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); } .grid-4 { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }

/* layout */
.shell { display: grid; grid-template-columns: 250px 1fr; min-height: 100vh; }
.sidebar { background: var(--brand); color: #e0e7ff; padding: 1rem .75rem; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { display: flex; gap: .6rem; align-items: center; padding: .25rem .5rem 1rem; color: #fff; }
.brand .logo { width: 40px; height: 40px; border-radius: 10px; background: #fff; display: grid; place-items: center; color: var(--brand); font-weight: 700; font-size: 1.1rem; overflow: hidden; }
.brand .logo img { width: 100%; height: 100%; object-fit: contain; }
.brand b { display: block; font-size: 1rem; line-height: 1.2; } .brand span { font-size: .75rem; opacity: .85; }
.nav a { display: flex; gap: .6rem; align-items: center; padding: .5rem .7rem; border-radius: 8px; color: #e0e7ff; font-size: .95rem; }
.nav a:hover { background: rgba(255, 255, 255, .1); text-decoration: none; }
.nav a.active { background: #fff; color: var(--brand); font-weight: 600; }
.nav .sec { font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; opacity: .65; padding: .9rem .7rem .3rem; }
.nav .badge { margin-left: auto; }
.main { min-width: 0; }
.topbar { display: flex; align-items: center; gap: .75rem; padding: .6rem 1.25rem; background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.topbar .menu-btn { display: none; }
.topbar .search { flex: 1; max-width: 520px; }
.content { padding: 1.25rem; max-width: 1400px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; margin-bottom: 1rem; flex-wrap: wrap; }
.page-head h1 { font-size: 1.4rem; }
/* ----- แท็บเล็ต (iPad แนวตั้ง 768–1100px): แถบเมนูแคบลง ----- */
@media (max-width: 1100px) and (min-width: 901px) {
  .shell { grid-template-columns: 200px 1fr; }
  .sidebar { padding: .75rem .5rem; }
  .nav a { font-size: .9rem; padding: .45rem .55rem; }
  .content { padding: 1rem; }
}
/* ----- มือถือ / iPad แนวตั้งขนาดเล็ก (≤ 900px) ----- */
@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: fixed; top: 0; bottom: 0; left: -290px; width: 280px; z-index: 40; transition: left .2s; height: auto; padding-bottom: calc(1rem + env(safe-area-inset-bottom)); }
  .sidebar.open { left: 0; box-shadow: 0 0 0 100vw rgba(0, 0, 0, .35); }
  .topbar { padding: .5rem .75rem; padding-top: calc(.5rem + env(safe-area-inset-top)); }
  .topbar .menu-btn { display: inline-flex; }
  .topbar .search { display: none; }
  .topbar .user-link { display: none; }
  .content { padding: .8rem .8rem calc(4.8rem + env(safe-area-inset-bottom)); }
  .page-head h1 { font-size: 1.2rem; }
  .card { padding: .85rem .9rem; border-radius: 12px; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .grid-4 { grid-template-columns: 1fr 1fr; }
  .stat .n { font-size: 1.4rem; }
  .modal-bg { padding: 0; align-items: flex-end; }
  .modal, .modal.wide { width: 100%; max-height: 96vh; border-radius: 16px 16px 0 0; padding: 1rem 1rem calc(1rem + env(safe-area-inset-bottom)); }
  .notif-panel { right: .5rem; left: .5rem; width: auto; }
  .kv { grid-template-columns: 1fr; gap: 0; } .kv dt { margin-top: .4rem; font-size: .8rem; }
  .tabs button { padding: .55rem .7rem; font-size: .9rem; }
  .actions .btn { flex: 1 1 auto; justify-content: center; }
  .page-head .actions .btn { flex: 1 1 45%; }
  /* ตารางเอกสาร → การ์ด */
  .doc-table { display: none; }
  .doc-cards { display: block; }
  /* หน้ารายละเอียด: คอลัมน์เดียว */
  .detail-grid { grid-template-columns: 1fr !important; }
  .bottom-nav { display: flex; }
  .sigpad { height: 150px; }
}
@media (min-width: 901px) { .doc-cards { display: none; } .bottom-nav { display: none; } }

/* การ์ดเอกสาร (มือถือ) */
.doc-cards .dc { display: block; background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: .7rem .85rem; margin-bottom: .6rem; color: inherit; text-decoration: none; }
.doc-cards .dc:active { background: #f1f5f9; }
.doc-cards .dc .no { font-size: .78rem; color: var(--muted); display: flex; justify-content: space-between; gap: .5rem; }
.doc-cards .dc .sub { font-weight: 500; margin: .15rem 0; line-height: 1.35; }
.doc-cards .dc .meta { font-size: .8rem; color: var(--muted); display: flex; flex-wrap: wrap; gap: .3rem .6rem; align-items: center; }

/* แถบเมนูล่าง (มือถือ) */
.bottom-nav { position: fixed; left: 0; right: 0; bottom: 0; z-index: 35; background: #fff; border-top: 1px solid var(--line); justify-content: space-around; padding: .3rem 0 calc(.3rem + env(safe-area-inset-bottom)); box-shadow: 0 -2px 10px rgba(0, 0, 0, .06); }
.bottom-nav a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: .1rem; font-size: .68rem; color: var(--muted); text-decoration: none; padding: .2rem 0; position: relative; min-height: 44px; justify-content: center; }
.bottom-nav a .ic { font-size: 1.3rem; line-height: 1; }
.bottom-nav a.active { color: var(--brand); font-weight: 600; }
.bottom-nav a.primary .ic { background: var(--brand-2); color: #fff; width: 42px; height: 42px; border-radius: 50%; display: grid; place-items: center; margin-top: -14px; box-shadow: 0 4px 10px rgba(37, 99, 235, .4); }
.bottom-nav .badge { position: absolute; top: 0; right: 18%; }

/* สัมผัส: ปุ่ม/ช่องกรอกใหญ่พอสำหรับนิ้ว */
@media (hover: none) and (pointer: coarse) {
  .btn { min-height: 44px; }
  .btn.sm { min-height: 38px; }
  input[type=text], input[type=password], input[type=email], input[type=number], input[type=date], input[type=datetime-local], input[type=time], input[type=search], select, textarea { min-height: 44px; font-size: 16px; }
  .tabs button { min-height: 44px; }
  .list li { padding: .75rem 0; }
}

/* cards & panels */
.card { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem 1.15rem; }
.card + .card { margin-top: 1rem; }
.card h2, .card h3 { margin-bottom: .75rem; }
.stat { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: .9rem 1rem; }
.stat .n { font-size: 1.7rem; font-weight: 700; color: var(--brand); line-height: 1.1; }
.stat .l { color: var(--muted); font-size: .85rem; }
.stat.red .n { color: var(--bad); } .stat.green .n { color: var(--ok); } .stat.amber .n { color: var(--warn); }
.callout { border-left: 4px solid var(--brand-2); background: var(--brand-soft); padding: .7rem .9rem; border-radius: 6px; font-size: .92rem; }
.callout.warn { border-color: var(--warn); background: var(--warn-soft); }
.callout.bad { border-color: var(--bad); background: var(--bad-soft); }
.callout.ok { border-color: var(--ok); background: var(--ok-soft); }

/* forms */
label.f { display: block; margin-bottom: .75rem; }
label.f > span { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .2rem; }
label.f > span .req { color: var(--bad); }
input[type=text], input[type=password], input[type=email], input[type=number], input[type=date], input[type=datetime-local], input[type=time], input[type=search], select, textarea {
  width: 100%; padding: .5rem .65rem; border: 1px solid #cbd5e1; border-radius: 8px; font: inherit; background: #fff; color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand-2); }
textarea { min-height: 96px; resize: vertical; }
.check { display: flex; gap: .4rem; align-items: center; font-size: .95rem; }
.row { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0 1rem; }
.btn { display: inline-flex; align-items: center; gap: .4rem; padding: .5rem .9rem; border-radius: 8px; border: 1px solid transparent; font: inherit; font-weight: 500; cursor: pointer; background: var(--brand-2); color: #fff; text-decoration: none; white-space: nowrap; }
.btn:hover { filter: brightness(1.08); text-decoration: none; }
.btn:disabled { opacity: .55; cursor: default; }
.btn.secondary { background: #fff; color: var(--ink); border-color: #cbd5e1; }
.btn.ghost { background: transparent; color: var(--brand-2); }
.btn.ok { background: var(--ok); } .btn.warn { background: var(--accent); } .btn.bad { background: var(--bad); }
.btn.sm { padding: .3rem .6rem; font-size: .85rem; }
.btn.lg { padding: .7rem 1.2rem; font-size: 1.05rem; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }

/* tables */
.table-wrap { overflow-x: auto; }
table.t { width: 100%; border-collapse: collapse; font-size: .93rem; }
table.t th, table.t td { padding: .5rem .6rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
table.t th { background: #f8fafc; font-weight: 600; color: #334155; white-space: nowrap; }
table.t tr:hover td { background: #f8fafc; }
table.t tr.clickable { cursor: pointer; }
table.t td.num { text-align: right; }

/* badges */
.badge { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .75rem; font-weight: 600; background: #e2e8f0; color: #334155; white-space: nowrap; }
.badge.blue { background: var(--brand-soft); color: var(--brand); }
.badge.green { background: var(--ok-soft); color: var(--ok); }
.badge.amber { background: var(--warn-soft); color: var(--warn); }
.badge.orange { background: #ffedd5; color: #c2410c; }
.badge.red { background: var(--bad-soft); color: var(--bad); }
.badge.gray { background: #f1f5f9; color: #64748b; }
.badge.purple { background: #ede9fe; color: #6d28d9; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--ok); }
.dot.red { background: var(--bad); } .dot.amber { background: var(--warn); } .dot.gray { background: #cbd5e1; }

/* lists */
.list { list-style: none; margin: 0; padding: 0; }
.list li { padding: .6rem 0; border-bottom: 1px solid var(--line); display: flex; gap: .75rem; align-items: flex-start; }
.list li:last-child { border-bottom: 0; }
.list .t1 { font-weight: 500; } .list .t2 { color: var(--muted); font-size: .85rem; }
.timeline { list-style: none; margin: 0; padding: 0 0 0 1.4rem; border-left: 2px solid var(--line); }
.timeline li { position: relative; padding: 0 0 1rem .75rem; }
.timeline li::before { content: ''; position: absolute; left: -1.85rem; top: .35rem; width: 12px; height: 12px; border-radius: 50%; background: #cbd5e1; border: 2px solid #fff; box-shadow: 0 0 0 1px #cbd5e1; }
.timeline li.done::before { background: var(--ok); box-shadow: 0 0 0 1px var(--ok); }
.timeline li.current::before { background: var(--brand-2); box-shadow: 0 0 0 4px var(--brand-soft); }
.timeline li.returned::before, .timeline li.rejected::before { background: var(--bad); box-shadow: 0 0 0 1px var(--bad); }
.timeline li.skipped { opacity: .5; }

/* tabs */
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--line); margin-bottom: 1rem; overflow-x: auto; }
.tabs button { background: none; border: 0; border-bottom: 2px solid transparent; padding: .6rem .9rem; font: inherit; cursor: pointer; color: var(--muted); white-space: nowrap; }
.tabs button.active { color: var(--brand); border-bottom-color: var(--brand); font-weight: 600; }

/* modal / toast */
.modal-bg { position: fixed; top: 0; right: 0; bottom: 0; left: 0; inset: 0; background: rgba(15, 23, 42, .5); z-index: 50; display: grid; place-items: center; padding: 1rem; }
.modal { background: #fff; border-radius: 14px; width: min(720px, 100%); max-height: 92vh; overflow: auto; padding: 1.25rem; box-shadow: 0 20px 60px rgba(0, 0, 0, .3); }
.modal.wide { width: min(1000px, 100%); }
.modal h2 { margin-bottom: .75rem; }
#toast-area { position: fixed; right: 1rem; bottom: 1rem; z-index: 60; display: flex; flex-direction: column; gap: .5rem; }
.toast { background: #111827; color: #fff; padding: .6rem .9rem; border-radius: 8px; box-shadow: var(--shadow); max-width: 360px; animation: fade .2s; }
.toast.bad { background: var(--bad); } .toast.ok { background: var(--ok); }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } }

/* signature pad */
.sigpad { border: 1px dashed #94a3b8; border-radius: 8px; background: #fff; touch-action: none; width: 100%; height: 180px; display: block; }
.sig-img { max-height: 70px; max-width: 220px; }

/* notifications */
.bell { position: relative; background: none; border: 0; cursor: pointer; font-size: 1.25rem; padding: .3rem .5rem; }
.bell .cnt { position: absolute; top: -2px; right: -2px; background: var(--bad); color: #fff; border-radius: 999px; font-size: .68rem; padding: 0 .35rem; font-weight: 700; }
.notif-panel { position: absolute; right: 1rem; top: 3.2rem; width: min(420px, 95vw); background: #fff; border-radius: 12px; box-shadow: 0 10px 40px rgba(0, 0, 0, .2); z-index: 30; max-height: 70vh; overflow: auto; }
.notif-panel .item { padding: .6rem .9rem; border-bottom: 1px solid var(--line); cursor: pointer; }
.notif-panel .item.unread { background: #eff6ff; }

/* kv */
.kv { display: grid; grid-template-columns: max-content 1fr; gap: .25rem 1rem; font-size: .95rem; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; }
.pre { white-space: pre-wrap; font-family: inherit; }
.doc-body { white-space: pre-wrap; line-height: 1.8; }

/* login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; background: linear-gradient(160deg, #1e3a8a, #1d4ed8 60%, #0ea5e9); padding: 1rem; }
.login { background: #fff; border-radius: 16px; padding: 2rem; width: min(420px, 100%); box-shadow: 0 20px 60px rgba(0, 0, 0, .3); }
.login .brand { color: var(--ink); padding: 0 0 1rem; }
.login .brand .logo { background: var(--brand); color: #fff; }

/* ----- print: official forms ----- */
/* กระดาษตามคู่มือการพิมพ์หนังสือราชการ: ขอบซ้าย 3 ซม. ขวา 2 ซม. บน 2.5 ซม. ล่าง 2 ซม.
   ฟอนต์ TH SarabunIT๙/TH Sarabun New 16 พอยท์ ระยะบรรทัด 1 เท่า */
.paper { background: #fff; color: #000; font-family: 'TH SarabunIT๙', 'TH Sarabun New', 'Sarabun', serif; font-size: 16pt; line-height: 1.25; width: 210mm; min-height: 297mm; padding: 25mm 20mm 20mm 30mm; margin: 0 auto; box-shadow: var(--shadow); position: relative; }
.paper.landscape { width: 297mm; min-height: 210mm; padding: 15mm 20mm; }
/* ตราครุฑ: หนังสือภายนอก/คำสั่ง/ประกาศ สูง 3 ซม. กึ่งกลาง ชิดขอบบน; บันทึกข้อความ สูง 1.5 ซม. มุมบนซ้าย (ข้อ 71, 75, 76) */
.paper .garuda { text-align: center; height: 3cm; display: flex; align-items: flex-start; justify-content: center; margin-bottom: .2cm; }
.paper .garuda img { height: 3cm; }
.paper .garuda .ph { width: 3cm; height: 3cm; border: 1px dashed #999; display: grid; place-items: center; font-size: 10pt; color: #666; }
.paper .garuda.small, .paper .garuda.small img, .paper .garuda.small .ph { height: 1.5cm; width: auto; }
.paper .garuda.small .ph { width: 1.5cm; font-size: 7pt; }
/* บันทึกข้อความ: หัวเรื่อง 24 พอยท์ ตัวหนา ระยะบรรทัด 35 พอยท์; คำว่า ส่วนราชการ ที่ วันที่ เรื่อง 20 พอยท์ ตัวหนา; ใช้จุดไข่ปลาแสดงเส้นบรรทัด */
.paper .memo-title { font-size: 24pt; font-weight: 700; line-height: 35pt; text-align: left; display: inline-block; vertical-align: middle; margin-left: 1cm; }
.paper .hdr { display: grid; grid-template-columns: max-content 1fr; gap: 0 .3cm; align-items: baseline; }
.paper .hdr b { font-weight: 700; }
.paper .hdr.memo b { font-size: 20pt; }
.paper .dotted { border-bottom: 1px dotted #000; min-height: 1.2em; }
/* ย่อหน้า 2.5 ซม.; หัวข้อ เรื่อง/เรียน/อ้างถึง/สิ่งที่ส่งมาด้วย เว้นก่อนหน้า 6 พอยท์ */
.paper .line6 { margin-top: 6pt; }
.paper .line12 { margin-top: 12pt; }
.paper .body { text-indent: 2.5cm; text-align: justify; margin-top: 6pt; white-space: pre-wrap; }
.paper .body p { margin: 0; text-indent: 2.5cm; }
/* คำลงท้ายและลายมือชื่อ: กึ่งกลางแนวตราครุฑ (ครึ่งขวาของหน้า) เว้น 3 บรรทัดสำหรับลงชื่อ */
.paper .sign-block { margin-top: 12pt; margin-left: 50%; text-align: center; }
.paper .sign-block .space { height: 3.2em; display: flex; align-items: center; justify-content: center; }
.paper .sign-block img { max-height: 2cm; }
.paper .footer-unit { margin-top: 1.2cm; font-size: 16pt; line-height: 1.2; }
/* ชั้นความเร็ว: ตัวอักษรสีแดง ขนาดไม่เล็กกว่า 32 พอยท์ (ข้อ 28) */
.paper .urgency { position: absolute; top: 10mm; left: 30mm; color: #d00; font-size: 32pt; font-weight: 700; line-height: 1; }
/* ตรารับหนังสือ 2.5 × 5 ซม. มุมบนขวา (ข้อ 37, 78) */
.paper .stamp-box { position: absolute; top: 10mm; right: 12mm; border: 1.5px solid #000; padding: 3px 6px; font-size: 12pt; line-height: 1.3; width: 5cm; min-height: 2.5cm; }
.paper .stamp-box b { display: block; text-align: center; font-size: 13pt; }
/* ตรากำหนดเก็บหนังสือ (ข้อ 55.1, 73): "ห้ามทำลาย" หมึกแดง / "เก็บถึง พ.ศ. ...." หมึกน้ำเงิน ไม่เล็กกว่า 24 พอยท์ มุมล่างขวาแผ่นแรก */
.paper .keep-stamp { position: absolute; right: 15mm; bottom: 15mm; border: 2px solid; padding: 2px 10px; font-size: 24pt; font-weight: 700; transform: rotate(-4deg); }
.paper .keep-stamp.red { color: #c00; border-color: #c00; } .paper .keep-stamp.blue { color: #1a3fbf; border-color: #1a3fbf; }
/* สำเนาคู่ฉบับ: ผู้ร่าง/พิมพ์/ตรวจ ลงชื่อย่อท้ายขอบล่างขวา (ข้อ 30) */
.paper .copy-note { position: absolute; left: 30mm; bottom: 8mm; font-size: 11pt; color: #333; }
.paper .order-title { text-align: center; font-size: 18pt; font-weight: 700; line-height: 1.3; }
.paper .order-rule { text-align: center; letter-spacing: 2px; }
.paper table.reg { width: 100%; border-collapse: collapse; font-size: 13pt; }
.paper table.reg th, .paper table.reg td { border: 1px solid #000; padding: 2px 5px; vertical-align: top; }
.paper table.reg th { text-align: center; font-weight: 700; }
.paper .reg-title { text-align: center; font-size: 20pt; font-weight: 700; margin-bottom: .2cm; }
.paper .eSig { font-size: 11pt; color: #333; border-top: 1px solid #999; margin-top: 1cm; padding-top: .2cm; }
.print-only { display: none; }
@media screen and (max-width: 900px) {
  .paper, .paper.landscape { width: 100%; min-height: auto; padding: 8mm; font-size: 13pt; }
  .paper .memo-title { font-size: 22pt; }
  .paper .stamp-box { position: static; width: auto; margin-bottom: .5cm; }
}
@media print {
  body { background: #fff; }
  .sidebar, .topbar, .no-print, #toast-area, .modal-bg, .bottom-nav, .notif-panel { display: none !important; }
  .shell { display: block; }
  .content { padding: 0; max-width: none; }
  .paper { box-shadow: none; margin: 0; width: auto; min-height: auto; padding: 15mm 15mm 15mm 25mm; page-break-after: always; }
  .paper.landscape { padding: 10mm 15mm; }
  .print-only { display: block; }
  @page { size: A4; margin: 0; }
  .paper.landscape ~ * { }
}
.print-landscape { }
