/* ── Yotathai Operate — Design System ─────────────────────────────
   แม่แบบดีไซน์มาตรฐานตระกูลแอปโยธาไทย: ใช้ง่ายก่อนสวย, มือถือหน้างานอ่านชัด */

:root {
  --blue: #1a56db;
  --blue-dark: #1240a8;
  --blue-soft: #e8effc;
  --rain: #0e7490;
  --rain-soft: #e0f2f7;
  --bg: #f4f6fa;
  --surface: #ffffff;
  --text: #1b2430;
  --text-dim: #5b6779;
  --line: #dde3ec;
  --accent-row: #f8fafd;
  --shadow: 0 1px 3px rgba(16, 30, 54, .08), 0 4px 14px rgba(16, 30, 54, .06);
  --radius: 12px;
  --ok: #067647;
}
:root[data-theme="dark"] {
  --blue: #6b96f5;
  --blue-dark: #8db0ff;
  --blue-soft: #1d2a45;
  --rain: #4cc4e0;
  --rain-soft: #12303a;
  --bg: #0f1523;
  --surface: #182136;
  --text: #e8edf6;
  --text-dim: #94a1b8;
  --line: #2a3650;
  --accent-row: #1c2740;
  --shadow: 0 1px 3px rgba(0, 0, 0, .4);
  --ok: #4ade80;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: "Sarabun", "Noto Sans Thai", -apple-system, "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.55;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--blue); }

/* ── Topbar ── */
.topbar { background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.topbar-inner { max-width: 1280px; margin: 0 auto; padding: .7rem 1rem; display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.brand { display: flex; align-items: center; gap: .7rem; }
/* โลโก้ช้างเป็นสีน้ำเงินเข้มพื้นโปร่งใส กลืนกับพื้นหลัง Dark Mode ที่เป็นโทนน้ำเงินเข้มเช่นกัน
   ใส่แผ่นขาวรองหลังเสมอ (ไม่ว่าธีมไหน) ให้ตัดกันชัดเจนแทนการทำไฟล์โลโก้แยกสองสี */
.brand-logo { width: 40px; height: 40px; object-fit: contain; flex-shrink: 0; background: #fff; border-radius: 9px; padding: 4px; box-sizing: border-box; }
.brand h1 { font-size: 1.12rem; margin: 0; line-height: 1.25; }
.brand p { font-size: .78rem; margin: 0; color: var(--text-dim); }
.icon-btn { background: none; border: 1px solid var(--line); border-radius: 10px; font-size: 1.1rem; padding: .35rem .6rem; cursor: pointer; }

/* ── Layout ── */
.layout { max-width: 1280px; margin: 0 auto; padding: 1rem; display: grid; grid-template-columns: 320px 1fr; gap: 1rem; align-items: start; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.1rem; position: sticky; top: 4.4rem; display: flex; flex-direction: column; gap: 1.1rem; }
.content { min-width: 0; }

.panel-step { display: flex; flex-direction: column; gap: .45rem; }
.step-label { font-weight: 700; font-size: .95rem; }
.input-lg { width: 100%; font-size: 1.05rem; font-family: inherit; padding: .65rem .75rem; border: 1.5px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text); }
.input-lg:focus { outline: 2px solid var(--blue); border-color: var(--blue); }
.price-row { display: flex; gap: .5rem; }
.price-row input { flex: 1; min-width: 0; }

.map-toggle-btn { align-self: flex-start; background: none; border: none; color: var(--blue); font-family: inherit; font-size: .85rem; font-weight: 700; cursor: pointer; padding: .2rem 0; margin-top: -.15rem; }
:root[data-theme="dark"] .map-toggle-btn { color: var(--blue-dark); }

/* ── หน้าต่างลอย: แผนที่เลือกจังหวัด ── */
.modal-backdrop { position: fixed; inset: 0; background: rgba(10, 16, 28, .55); display: flex; align-items: center; justify-content: center; z-index: 100; padding: 1rem; backdrop-filter: blur(2px); }
.modal-backdrop[hidden] { display: none; }
.map-modal { background: var(--surface); border-radius: var(--radius); box-shadow: 0 10px 40px rgba(0,0,0,.35); width: min(480px, 100%); max-height: 90vh; display: flex; flex-direction: column; overflow: hidden; }
.map-modal-head { display: flex; align-items: center; justify-content: space-between; padding: .8rem 1rem; border-bottom: 1px solid var(--line); font-weight: 700; }
.map-modal .hint { padding: 0 1rem .9rem; text-align: center; }
.map-legend { display: flex; flex-wrap: wrap; gap: .5rem .9rem; justify-content: center; padding: 0 1rem 1rem; font-size: .78rem; color: var(--text-dim); }
.legend-item { display: inline-flex; align-items: center; gap: .35rem; }
.legend-item i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
#map-container { width: 100%; height: 460px; display: flex; align-items: center; justify-content: center; padding: .5rem 1rem; }
#map-container svg { width: 100%; height: 100%; }
/* สีของ .province มาจาก inline style (ตามภาค/ฝนชุก) เสมอ — hover/active ใช้ filter ปรับความสว่างแทน
   เพื่อไม่ต้องแย่ง specificity กับสี inline ที่ต่างกันในแต่ละจังหวัด */
.province { stroke: var(--surface); stroke-width: .5; cursor: pointer; transition: filter .15s; }
.province:hover { filter: brightness(1.25) saturate(1.15); }
.province.active { filter: brightness(1.15); stroke: #1b2430; stroke-width: 1.6; }
@media (max-width: 560px) { #map-container { height: 60vh; } }
.hint { font-size: .78rem; color: var(--text-dim); margin: 0; }
.hint.ok { color: var(--ok); }

.btn-primary, .btn-secondary { font-family: inherit; font-size: .95rem; font-weight: 700; border-radius: 10px; padding: .6rem 1rem; cursor: pointer; border: none; white-space: nowrap; }
.btn-primary { background: var(--blue); color: #fff; }
.btn-primary:hover { background: var(--blue-dark); }
.btn-secondary { background: var(--blue-soft); color: var(--blue); }
:root[data-theme="dark"] .btn-secondary { color: var(--blue-dark); }
.btn-danger { background: #dc2626; color: #fff; }
.btn-danger:hover { background: #b91c1c; }

.mode-toggle { display: flex; border: 1.5px solid var(--line); border-radius: 10px; overflow: hidden; }
.mode-toggle button { flex: 1; font-family: inherit; font-size: 1rem; font-weight: 700; padding: .55rem; border: none; background: var(--bg); color: var(--text-dim); cursor: pointer; }
.mode-toggle button.active[data-mode="normal"] { background: var(--blue); color: #fff; }
.mode-toggle button.active[data-mode="rain"] { background: var(--rain); color: #fff; }

.panel-note { font-size: .78rem; color: var(--text-dim); border-top: 1px dashed var(--line); padding-top: .8rem; }
.panel-note a { display: inline-block; margin-top: .2rem; }

/* ── Status bar ── */
.status-bar { background: linear-gradient(100deg, var(--blue), var(--blue-dark)); color: #fff; border-radius: var(--radius); padding: .8rem 1.1rem; margin-bottom: .9rem; box-shadow: var(--shadow); display: flex; flex-wrap: wrap; gap: .3rem 1.4rem; align-items: baseline; }
.status-bar[hidden] { display: none; }
.status-bar.rain { background: linear-gradient(100deg, #0e7490, #155e75); }
.status-bar strong { font-size: 1.15rem; }
.status-bar .sub { font-size: .85rem; opacity: .92; }

/* ── Toolbar / Tabs ── */
.toolbar { display: flex; flex-wrap: wrap; gap: .6rem; justify-content: space-between; align-items: center; margin-bottom: .9rem; }
.tabs { display: flex; gap: .4rem; }
.tabs button { font-family: inherit; font-size: .95rem; font-weight: 700; padding: .55rem .95rem; border-radius: 10px; border: 1.5px solid var(--line); background: var(--surface); color: var(--text-dim); cursor: pointer; }
.tabs button.active { background: var(--blue-soft); border-color: var(--blue); color: var(--blue); }
:root[data-theme="dark"] .tabs button.active { color: var(--blue-dark); }
.badge { background: var(--blue); color: #fff; border-radius: 99px; font-size: .75rem; padding: 0 .45rem; margin-left: .2rem; }
.toolbar-right { display: flex; gap: .5rem; flex: 1; justify-content: flex-end; min-width: 0; }
.search-box { font-family: inherit; font-size: .95rem; padding: .55rem .8rem; border: 1.5px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--text); width: min(260px, 100%); }

/* ── Empty state ── */
.empty-state { text-align: center; padding: 4rem 1rem; color: var(--text-dim); }
.empty-icon { font-size: 3rem; }
.empty-state h2 { color: var(--text); font-size: 1.2rem; margin: .6rem 0 .3rem; }
.empty-state p { margin: 0; }

/* ── ตาราง ── */
.cat-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: .9rem; overflow: hidden; }
.cat-head { display: flex; align-items: baseline; gap: .6rem; padding: .7rem 1rem; background: var(--blue-soft); font-weight: 800; }
.cat-head .cat-no { color: var(--blue); }
:root[data-theme="dark"] .cat-head .cat-no { color: var(--blue-dark); }
.table-wrap { overflow-x: auto; }
table.rates { width: 100%; border-collapse: collapse; font-size: .95rem; min-width: 620px; }
table.rates th { font-size: .8rem; color: var(--text-dim); font-weight: 700; text-align: right; padding: .5rem .8rem; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.rates th:first-child { text-align: left; }
table.rates td { padding: .5rem .8rem; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
table.rates td:first-child { text-align: left; white-space: normal; min-width: 220px; }
table.rates tr:last-child td { border-bottom: none; }
table.rates tr:nth-child(even) td { background: var(--accent-row); }
td.unit { color: var(--text-dim); font-size: .85rem; text-align: center; }
td.total-active, th.total-active { font-weight: 800; color: var(--blue); background: var(--blue-soft) !important; }
.rain-mode td.total-active, .rain-mode th.total-active { color: var(--rain); background: var(--rain-soft) !important; }
:root[data-theme="dark"] td.total-active, :root[data-theme="dark"] th.total-active { color: var(--blue-dark); }
:root[data-theme="dark"] .rain-mode td.total-active, :root[data-theme="dark"] .rain-mode th.total-active { color: var(--rain); }
td.dim { color: var(--text-dim); }
.note-row td { color: var(--text-dim); font-style: italic; }
.add-btn { font-family: inherit; border: 1px solid var(--line); background: var(--surface); color: var(--blue); border-radius: 8px; padding: .15rem .55rem; font-size: .85rem; font-weight: 700; cursor: pointer; }
.add-btn:hover { background: var(--blue-soft); }
:root[data-theme="dark"] .add-btn { color: var(--blue-dark); }

/* ── BOQ ── */
.boq-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.boq-head { padding: .8rem 1rem; font-weight: 800; background: var(--blue-soft); display: flex; flex-wrap: wrap; gap: .6rem; align-items: center; justify-content: space-between; }
.boq-head-actions { display: flex; gap: .5rem; flex-wrap: wrap; }
.btn-sm { padding: .4rem .7rem; font-size: .82rem; }
table.boq { width: 100%; border-collapse: collapse; font-size: .95rem; }
table.boq th { font-size: .8rem; color: var(--text-dim); text-align: right; padding: .5rem .8rem; border-bottom: 1px solid var(--line); }
table.boq th:first-child { text-align: left; }
table.boq td { padding: .45rem .8rem; border-bottom: 1px solid var(--line); text-align: right; font-variant-numeric: tabular-nums; }
table.boq td:first-child { text-align: left; }
table.boq input.qty { width: 110px; font-family: inherit; font-size: .95rem; padding: .3rem .5rem; text-align: right; border: 1.5px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--text); }
.boq-total-row td { font-weight: 800; font-size: 1.05rem; border-top: 2px solid var(--blue); }
.del-btn { background: none; border: none; cursor: pointer; font-size: 1rem; color: var(--text-dim); }
.del-btn:hover { color: #dc2626; }
.boq-empty { padding: 2.2rem 1rem; text-align: center; color: var(--text-dim); }

/* ── Footer ── */
.footer { max-width: 1280px; margin: 1.5rem auto 0; padding: 1rem; border-top: 1px solid var(--line); color: var(--text-dim); font-size: .82rem; text-align: center; }
.footer p { margin: .2rem 0; }

/* ── Responsive ── */
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .panel { position: static; }
  .toolbar-right { justify-content: stretch; }
  .search-box { flex: 1; }
  body { font-size: 15px; }
}

/* ── Print: เอกสาร A4 แนบราคากลาง ── */
.print-only { display: none; }
@media print {
  @page { size: A4 portrait; margin: 12mm; }
  body { background: #fff; color: #000; font-size: 10.5pt; }
  /* กันโฆษณา/สคริปต์บุคคลที่สาม (Google Auto Ads ฯลฯ) ติดไปกับเอกสารพิมพ์เด็ดขาด
     ตรวจ DOM จริงบน production แล้วพบว่า Google แทรกโฆษณาไว้ 2 ระดับ:
       (1) ลูกของ <body>  — DIV.google-auto-placed, INS.adsbygoogle, IFRAME
       (2) ลูกของ <html> โดยตรง — INS.adsbygoogle (แถบล่าง), DIV#google-anno-sa (fixed), IFRAME#google_esf
     กฎเดิมคุมแค่ระดับ (1) โฆษณาระดับ (2) จึงหลุดไปพิมพ์ท้ายเอกสาร → ต้องคุมทั้งสองระดับ */
  html > *:not(body) { display: none !important; }
  body > *:not(#print-sheet) { display: none !important; }
  ins.adsbygoogle, .google-auto-placed, .adsbygoogle,
  [id^="aswift"], [id^="google-anno"], [id^="google_ads"], [class*="google-anno"] { display: none !important; }
  #print-sheet { display: block !important; }
  .no-print, .footer, .topbar { display: none !important; }
  .layout { display: block; padding: 0; max-width: none; }
  .content > * { display: none !important; }
  .print-only { display: block !important; }
  .print-header { display: flex; justify-content: space-between; align-items: center; border-bottom: 2.5px solid #1a56db; padding-bottom: 3mm; margin-bottom: 4mm; }
  .print-header .print-brand { display: flex; align-items: center; gap: 2.5mm; font-weight: 800; font-size: 13pt; letter-spacing: .5px; }
  .print-header .print-brand img { width: 11mm; height: 11mm; object-fit: contain; }
  .print-title { text-align: right; }
  .print-title h1 { font-size: 13pt; margin: 0; }
  .print-title p { font-size: 10pt; margin: .5mm 0 0; color: #444; }
  .print-meta { width: 100%; border-collapse: collapse; margin-bottom: 4mm; font-size: 10pt; }
  .print-meta td { padding: 1.4mm 2.5mm; border: .5pt solid #ccc; }
  .print-meta td:first-child { font-weight: 700; width: 38%; background: #f2f5fb; }
  /* เส้นขอบขวาสุดของตารางเคยบางจนแทบมองไม่เห็น (วัดได้: หนา 1px เข้ม 211 เทียบซ้าย 2px เข้ม 136)
     เพราะตารางกว้างเต็ม 186mm ชนขอบพื้นที่พิมพ์พอดี ครึ่งนอกของเส้น (border-collapse จะวางเส้น
     คร่อมขอบกล่อง) จึงถูก clip ออกไป → เว้นที่ .6mm ให้เส้นวาดได้เต็ม + กรอบนอกหนา/เข้มขึ้น */
  #print-sheet { padding-right: .6mm; }
  table.print-rates { width: 100%; border-collapse: collapse; font-size: 9pt; border: 1.2pt solid #444; }
  table.print-rates th, table.print-rates td { border: .6pt solid #888; padding: .75mm 1.6mm; text-align: right; }
  table.print-rates th { background: #e8effc; text-align: center; color: #1240a8; }
  table.print-rates td:first-child, table.print-rates th:first-child { text-align: left; }
  table.print-rates .p-cat td { font-weight: 800; background: #f2f5fb; text-align: left; color: #1240a8; }
  /* 1 หมวด = 1 บล็อก (หัวข้อ + ตาราง) ห่อรวมกัน break-inside:avoid — กันหัวข้อค้างหน้าหนึ่ง
     แต่เนื้อหาลอยไปอีกหน้า (ถ้าที่เหลือในหน้าปัจจุบันไม่พอทั้งบล็อก ย้ายไปหน้าใหม่ทั้งก้อน) */
  .print-cat-block { break-inside: avoid; page-break-inside: avoid; margin-bottom: 3mm; }
  .print-cat-title { font-weight: 800; color: #1240a8; background: #f2f5fb; border: .6pt solid #888; border-bottom: none; padding: 1mm 2mm; }
  .print-closing { break-inside: avoid; page-break-inside: avoid; }
  /* บล็อกลงนามแบบเอกสารราชการ — จัดด้วย grid 3 คอลัมน์ให้เส้นประทุกบรรทัดตรงแนวเดียวกัน
     (เดิมใช้ text-align:right ทำให้แต่ละบรรทัดยาวไม่เท่ากันแล้วเหลื่อมกัน) */
  .print-sign {
    display: grid; grid-template-columns: auto 62mm auto; column-gap: 2mm; row-gap: 3.4mm;
    width: max-content; margin: 8mm 0 0 auto; font-size: 10pt; line-height: 1.5; break-inside: avoid;
  }
  .print-sign .ps-l { grid-area: 1 / 1; align-self: end; }
  .print-sign .ps-r { grid-area: 1 / 3; align-self: end; }
  .print-sign .ps-line { grid-area: 1 / 2; border-bottom: .5pt dotted #333; }
  .print-sign .ps-name { grid-area: 2 / 2; text-align: center; white-space: nowrap; }
  .print-sign .ps-date { grid-area: 3 / 2; text-align: center; white-space: nowrap; }
  .print-sign .ps-blank { display: inline-block; width: 46mm; border-bottom: .5pt dotted #333; margin: 0 1mm; }
  .print-sign .ps-d { display: inline-block; width: 13mm; border-bottom: .5pt dotted #333; margin: 0 1.2mm; }
  .print-foot { margin-top: 4mm; font-size: 8.5pt; color: #555; border-top: .5pt solid #ccc; padding-top: 2mm; break-inside: avoid; }
  .cat-card, tr { break-inside: avoid; }

  /* ── กันหน้าเปล่าแถมท้ายเอกสาร ──────────────────────────────
     อาการ: จำนวนหน้าเกินจริง 1 หน้า และหน้าสุดท้ายว่างเปล่า (คุณช่างถึกเจอ 5 ส.ค. 2569)
     สาเหตุ: element ที่เบราว์เซอร์/ส่วนขยาย/โฆษณาแทรกเข้ามา ยังกินพื้นที่ต่อท้ายเอกสาร
     (วัดในเครื่องพัฒนาแล้ว body.scrollHeight = ความสูงเอกสารพอดี ไม่มีส่วนเกิน จึงเป็น
     ปัจจัยเฉพาะเครื่องผู้ใช้ที่วัดจากระยะไกลไม่ได้) → อุดแบบครอบจักรวาลไม่ให้เกิดได้เลย */
  html, body {
    margin: 0 !important; padding: 0 !important;
    height: auto !important; min-height: 0 !important; max-height: none !important;
    overflow: hidden !important;          /* ตัดส่วนเกินที่จะกลายเป็นหน้าใหม่ */
  }
  /* กล่องลอย (fixed/sticky) เป็นต้นเหตุคลาสสิกของหน้าเปล่าใน Chrome — ปลดทั้งหมดตอนพิมพ์ */
  body *:not(#print-sheet):not(#print-sheet *) { position: static !important; }
  /* ห้ามมี margin ค้างท้ายเอกสาร (ดันขึ้นหน้าใหม่ได้) */
  #print-sheet, .print-closing, .print-foot { margin-bottom: 0 !important; padding-bottom: 0 !important; }
  #print-sheet > *:last-child { margin-bottom: 0 !important; }
  #print-sheet::after { content: none !important; }
}
