/* ===================================================================
   ระบบการเงิน–บัญชีโรงเรียน — โครงหน้าตาเดียวกับระบบพัสดุ
   (สีหลักเขียวเงิน แยกจากระบบพัสดุที่เป็นน้ำเงิน จะได้ไม่สับสนว่าอยู่ระบบไหน)
   =================================================================== */
@font-face { font-family: 'TH Sarabun New'; src: local('TH Sarabun New'), local('TH SarabunPSK'), local('Sarabun'); }

:root {
  --main: #0f5e4a; --main-light: #178a6c; --main-soft: #e3f2ec;
  --acc: #3b4aa8; --acc-soft: #e6e9f8;          /* บัญชี */
  --fin: #0f6b55; --fin-soft: #dff1ea;          /* การเงิน */
  --bg: #f1f4f3; --card: #ffffff; --line: #d6dfdb; --text: #1b2623; --muted: #5d6b67;
  --danger: #b42318; --danger-soft: #fdecea; --warn: #8a5a00; --warn-soft: #fff6df; --ok: #1a7f5a;
  --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; }
[hidden] { display: none !important; }

/* ---------- โครงหน้าจอ ---------- */
.app-header { background: linear-gradient(135deg, var(--main), var(--main-light)); color: #fff; padding: 12px 20px;
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap; box-shadow: 0 2px 8px rgba(0,0,0,.15); position: sticky; top: 0; z-index: 50; }
.app-header .logo { font-size: 24px; background: rgba(255,255,255,.16); width: 44px; height: 44px; display: flex; align-items: center; justify-content: center; border-radius: 12px; flex: none; }
.app-header h1 { font-size: 19px; margin: 0; font-weight: 700; }
.app-header .sub { font-size: 13px; opacity: .88; }
.hdr-right { margin-left: auto; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; font-size: 14px; }
.hdr-right .btn-ghost { background: rgba(255,255,255,.16); color: #fff; }
.fy-pick select { width: auto; padding: 4px 8px; margin-left: 4px; }
.role-chip { display: inline-block; background: rgba(255,255,255,.2); border-radius: 999px; padding: 1px 8px; font-size: 12px; margin-left: 4px; }
.mode-badge { background: #ffd166; color: #4a3500; border-radius: 6px; padding: 2px 8px; font-weight: 700; font-size: 13px; }
.menu-btn { display: none; background: rgba(255,255,255,.16); color: #fff; border: 0; border-radius: 8px; font-size: 20px; width: 40px; height: 40px; }

.layout { display: flex; min-height: calc(100vh - 68px); }
.sidebar { width: 260px; background: var(--card); border-right: 1px solid var(--line); padding: 12px; flex-shrink: 0; }
body:not(.signed-in) .sidebar { display: none; }
.nav-btn { display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px 12px; border: none; background: transparent; color: var(--text);
  font-size: 14.5px; font-family: inherit; text-align: left; border-radius: 8px; cursor: pointer; margin-bottom: 2px; }
.nav-btn .lb { flex: 1; }
.nav-btn:hover { background: #eef4f1; }
.nav-btn.active { background: var(--main); color: #fff; font-weight: 600; }
.nav-btn.primary:not(.active) { background: var(--main-soft); color: var(--main); font-weight: 600; }
.nav-btn .ic { font-size: 17px; width: 22px; text-align: center; }
.nav-sep { font-size: 12px; font-weight: 700; color: var(--muted); letter-spacing: .04em; margin: 14px 12px 4px; }
.nav-sep:empty { border-top: 1px solid var(--line); margin: 10px 4px; }
.nbadge { background: var(--danger); color: #fff; border-radius: 999px; font-size: 12px; padding: 0 7px; font-weight: 700; }
.mine-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); flex: none; }
.nav-btn.active .mine-dot { background: #ffd166; }

.content { flex: 1; padding: 22px 26px 60px; max-width: 1180px; min-width: 0; }

/* ---------- การ์ด / ฟอร์ม ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 16px; }
.card h2 { margin: 0 0 8px; font-size: 18px; color: var(--main); }
.hint { color: var(--muted); font-size: 14px; margin: 6px 0; }
.muted { color: var(--muted); } .small { font-size: 13px; } .nw { white-space: nowrap; }
.danger-text { color: var(--danger); }
.page-title { font-size: 22px; margin: 0 0 4px; color: var(--main); font-weight: 700; }
.page-desc { color: var(--muted); margin: 0 0 16px; font-size: 15px; max-width: 75ch; }
.sec-title { font-size: 16px; color: var(--muted); margin: 18px 0 8px; }
.grid { display: grid; gap: 12px; }
.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; margin-bottom: 10px; min-width: 0; }
.field label { font-size: 14px; font-weight: 600; color: #34403c; }
.field .desc { display: block; 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; padding: 0; accent-color: var(--main-light); }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--main-light); box-shadow: 0 0 0 3px rgba(23,138,108,.15); }
.money-in { text-align: right; font-variant-numeric: tabular-nums; }
fieldset.plain { border: 0; padding: 0; margin: 0; min-width: 0; }
.chk { display: flex; gap: 8px; align-items: center; font-size: 14.5px; cursor: pointer; padding: 8px 0; }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg label { border: 1px solid var(--line); border-radius: 999px; padding: 6px 12px; cursor: pointer; font-size: 14px; background: #fff; }
.seg label.on { background: var(--main); color: #fff; border-color: var(--main); }
.seg input { display: none; }
.auto-no { display: flex; align-items: center; gap: 4px; }
.auto-no span { color: var(--muted); font-weight: 600; white-space: nowrap; }
.auto-no input { background: #f7faf9; }

/* ---------- ปุ่ม ---------- */
.btn { font-family: inherit; font-size: 15px; padding: 9px 16px; border: none; border-radius: 8px; cursor: pointer; font-weight: 600; display: inline-flex; align-items: center; gap: 6px; }
.btn-primary { background: var(--main); color: #fff; } .btn-primary:hover { background: var(--main-light); }
.btn-ghost { background: #eaf0ee; color: var(--text); } .btn-ghost:hover { background: #dfe8e4; }
.btn-danger { background: var(--danger-soft); color: var(--danger); }
.btn-sm { padding: 5px 10px; font-size: 13.5px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn:focus-visible, .nav-btn:focus-visible, .kind-tile:focus-visible { outline: 2px solid var(--warn); outline-offset: 2px; }
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; align-items: center; }
.toolbar select, .toolbar input { width: auto; }
.spacer { flex: 1; }
.badge { display: inline-block; padding: 2px 9px; border-radius: 20px; font-size: 12px; font-weight: 600; background: var(--main-soft); color: var(--main); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }

/* ---------- ตาราง ---------- */
.tbl-scroll { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 14.5px; }
table.data th, table.data td { border-bottom: 1px solid var(--line); padding: 8px 10px; text-align: left; vertical-align: top; }
table.data th { background: #f2f6f4; font-weight: 600; color: #34403c; font-size: 13.5px; }
table.data tr.void td { color: #999; text-decoration: line-through; }
table.data.lines td { padding: 5px 6px; vertical-align: middle; }
.text-right { text-align: right !important; font-variant-numeric: tabular-nums; } .text-center { text-align: center !important; }

/* ---------- หน้าแรก ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; display: grid; gap: 2px; cursor: default; }
.kpi[onclick] { cursor: pointer; } .kpi span { color: var(--muted); font-size: 14px; } .kpi b { font-size: 24px; font-variant-numeric: tabular-nums; } .kpi small { color: var(--muted); }
.kpi.warn { border-color: #f0d98c; background: var(--warn-soft); } .kpi.bad { border-color: #f3b4b4; background: var(--danger-soft); }
.todo { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.todo li { display: flex; gap: 10px; align-items: center; padding: 8px 10px; border-radius: 8px; background: #f7faf9; }
.todo li.mine { background: var(--warn-soft); }
.todo .tx { flex: 1; }
.who { display: inline-block; font-size: 12px; font-weight: 700; padding: 1px 8px; border-radius: 999px; white-space: nowrap; }
.who.f { background: var(--fin-soft); color: var(--fin); } .who.a { background: var(--acc-soft); color: var(--acc); }

/* ---------- บันทึกรายการ ---------- */
.kind-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 10px; }
.kind-tile { display: flex; gap: 12px; align-items: flex-start; text-align: left; padding: 14px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--card); cursor: pointer; font-family: inherit; font-size: 15px; color: var(--text); }
.kind-tile:hover { border-color: var(--main-light); box-shadow: 0 2px 10px rgba(0,0,0,.06); }
.kind-tile.mine { border-left: 4px solid var(--warn); }
.kind-tile .ic { font-size: 26px; } .kind-tile b { display: block; font-size: 16px; } .kind-tile small { color: var(--muted); font-size: 13.5px; line-height: 1.45; display: block; margin-top: 2px; }
.entry-wrap { display: grid; grid-template-columns: minmax(0, 1fr) 330px; gap: 16px; align-items: start; }
@media (max-width: 1000px) { .entry-wrap { grid-template-columns: 1fr; } }
.preview { position: sticky; top: 84px; }
.preview h3 { margin: 0 0 10px; font-size: 15.5px; color: var(--main); }
.goes { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.goes li { display: flex; gap: 8px; align-items: flex-start; }
.goes li b { display: block; font-size: 14px; } .goes li small { color: var(--muted); font-size: 13px; }
.errs { margin-top: 12px; background: var(--danger-soft); color: var(--danger); border-radius: 8px; padding: 8px 10px; font-size: 13.5px; }
.ok-line { margin-top: 12px; color: var(--ok); font-weight: 600; font-size: 14px; }
.money-box { background: #f7faf9; border: 1px solid var(--line); border-radius: 8px; padding: 10px 14px; margin: 4px 0 12px; display: grid; gap: 3px; font-size: 14.5px; }
.money-box div { display: flex; justify-content: space-between; gap: 10px; } .money-box b { font-variant-numeric: tabular-nums; }
.money-box .net { font-size: 17px; border-top: 1px dashed var(--line); padding-top: 4px; color: var(--main); }
.money-box .baht { justify-content: flex-end; color: var(--muted); font-size: 13px; }
.money-box .warn-line { color: var(--warn); font-size: 13.5px; }
.pill-info { background: var(--warn-soft); border: 1px solid #f0d98c; color: #6b4f00; padding: 9px 13px; border-radius: 8px; font-size: 14px; margin: 4px 0 12px; }
.pill-info.danger { background: var(--danger-soft); border-color: #f3b4b4; color: var(--danger); }

/* ---------- งานจากพัสดุ ---------- */
.job-item { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; margin-bottom: 8px; background: #fff; flex-wrap: wrap; }
.job-icon { width: 40px; height: 40px; border-radius: 10px; background: var(--main-soft); display: flex; align-items: center; justify-content: center; font-size: 20px; flex-shrink: 0; }
.job-main { flex: 1; min-width: 220px; } .job-name { font-weight: 600; } .job-meta { color: var(--muted); font-size: 13.5px; margin-top: 2px; }
.job-amount { font-weight: 700; color: var(--main); text-align: right; } .job-amount small { display: block; color: var(--muted); font-weight: 400; font-size: 12.5px; }
.empty { text-align: center; color: var(--muted); padding: 34px 20px; border: 2px dashed var(--line); border-radius: 12px; }
.empty .big { font-size: 38px; margin-bottom: 8px; }

/* ---------- ล็อกอิน ---------- */
.login { max-width: 420px; margin: 40px auto; text-align: center; }
.login .grid { text-align: left; } .login-ic { font-size: 42px; }

/* ---------- ตั้งค่า ---------- */
.save-bar { position: sticky; bottom: 0; background: var(--bg); padding: 10px 0; display: flex; gap: 10px; z-index: 5; }

/* ---------- เอกสารพิมพ์ ---------- */
.doc-toolbar { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; margin-bottom: 16px;
  display: flex; gap: 10px; flex-wrap: wrap; align-items: center; position: sticky; top: 76px; z-index: 20; }
.doc-toolbar select, .doc-toolbar input { width: auto; max-width: 100%; }
.doc-scroll { overflow-x: auto; padding-bottom: 10px; }
.doc-stack { display: flex; flex-direction: column; align-items: center; gap: 20px; }
.doc-stack[contenteditable="true"]:focus { outline: none; }
.doc-page { background: #fff; width: 210mm; min-height: 297mm; padding: 15mm 20mm 15mm 25mm; 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; flex: none; }
.doc-page.flow { min-height: 0; }
.doc-page.landscape { width: 297mm; padding: 10mm 10mm 10mm 12mm; font-size: 13.5pt; line-height: 1.3; }
.doc-page.compact { font-size: 15pt; line-height: 1.22; padding: 10mm 20mm 10mm 25mm; }
.doc-page .garuda img { height: 15mm; } .doc-page .garuda { margin-bottom: 0; }
.memo-head { font-weight: bold; font-size: 20pt; text-align: center; margin: -10mm 0 2mm; letter-spacing: 1px; }
.memo-line { display: flex; gap: 6px; margin-bottom: 1mm; } .memo-line .lbl { font-weight: bold; white-space: nowrap; } .memo-line .val { flex: 1; }
.doc-hr { border: none; border-top: 1px solid #000; margin: 1.5mm 0 2.5mm; }
.doc-body p { margin: 0 0 1.6mm; text-align: justify; } .doc-body p.indent { text-indent: 25mm; } .doc-body p.indent-sm { text-indent: 12mm; }
.doc-title-center { text-align: center; font-weight: bold; font-size: 20pt; margin: 0 0 6mm; }
.doc-page table.gov { width: 100%; border-collapse: collapse; font-size: 14.5pt; margin: 2mm 0; }
.doc-page table.gov th, .doc-page table.gov td { border: 1px solid #000; padding: 2px 5px; vertical-align: top; }
.doc-page table.gov th { text-align: center; font-weight: bold; vertical-align: middle; }
.sign-area { margin-top: 5mm; display: flex; justify-content: flex-end; }
.sign-box { text-align: center; min-width: 70mm; } .sign-box .line { margin-bottom: .5mm; }
.sign-grid { display: flex; justify-content: space-around; flex-wrap: wrap; gap: 6mm; margin-top: 8mm; }
.reg-title { text-align: center; font-weight: bold; font-size: 17pt; }
.reg-sub { text-align: center; margin-bottom: 2mm; }
.doc-page table.gov.reg { table-layout: fixed; font-size: 12.5pt; }
.doc-page table.gov.reg th { font-size: 12pt; line-height: 1.15; padding: 2px 3px; }
.doc-page table.gov.reg td { padding: 1px 4px; line-height: 1.25; overflow-wrap: anywhere; }
.doc-page table.gov.reg td.text-right { white-space: nowrap; }
.doc-page table.gov.reg tr.sum td { font-weight: bold; background: #f3f3f3; }
.doc-page table.gov.reg tr.grp td { background: #f7f7f7; }
.doc-page table.gov.reg tr.void td { color: #888; }
.doc-page small { font-size: 11pt; }
.reg-note { font-size: 12pt; margin: 2mm 0 0; }
.bank-in { width: 30mm; padding: 2px 4px; font-size: 12pt; text-align: right; font-family: inherit; }

/* ---------- แจ้งเตือน / ยืนยัน ---------- */
.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; max-width: 90vw; }
.toast.show { opacity: 1; bottom: 34px; } .toast.ok { background: var(--ok); } .toast.err { background: var(--danger); }
#confirmBox { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 900; display: grid; place-items: center; padding: 16px; }
.confirm-card { background: #fff; border-radius: 12px; padding: 18px 20px; max-width: 440px; box-shadow: 0 10px 40px rgba(0,0,0,.25); }

/* ---------- มือถือ ---------- */
@media (max-width: 860px) {
  .menu-btn { display: block; }
  .sidebar { position: fixed; top: 68px; bottom: 0; left: 0; z-index: 60; transform: translateX(-100%); transition: .2s; overflow-y: auto; box-shadow: 4px 0 20px rgba(0,0,0,.15); }
  body.nav-open .sidebar { transform: none; }
  .content { padding: 16px 16px 60px; }
  .doc-toolbar { top: 68px; }
}

/* ---------- พิมพ์ ---------- */
@media print {
  @page { size: A4; margin: 0; }
  @page land { size: A4 landscape; margin: 0; }
  body { background: #fff; }
  .app-header, .sidebar, .doc-toolbar, .no-print, .toast, #confirmBox { display: none !important; }
  .content { padding: 0; max-width: none; } .layout { display: block; }
  .doc-scroll { overflow: visible; } .doc-stack { display: block; }
  .doc-page { box-shadow: none; margin: 0; page-break-after: always; }
  .doc-page:last-child { page-break-after: auto; }
  .doc-page.landscape { page: land; }
  .bank-in { border: 0; }
  .doc-page table.gov thead { display: table-header-group; }
  .doc-page table.gov tr { page-break-inside: avoid; }
}

/* ประเภทย่อยในหน้าตั้งค่า */
tr.sub-fund td { background: #f6faf7; }
.sub-name { display: flex; align-items: center; gap: 6px; padding-left: 14px; }
.sub-name span { color: var(--muted, #6b7280); font-size: 13px; white-space: nowrap; }
.sub-name input { flex: 1; min-width: 140px; }

/* ช่องเลือกประเภทเงิน + ปุ่มเพิ่มประเภทย่อย */
.fund-pick { display: flex; gap: 6px; align-items: center; }
.fund-pick select { flex: 1; min-width: 0; }
.fund-pick .btn { white-space: nowrap; }

/* ยกยอดต้นปี: บรรทัดที่แยกบัญชี */
tr.split-line td { background: #f6faf7; }
tr.split-line td:first-child { padding-left: 18px; font-size: 13px; }
