/*
  สมุดบิลบนโต๊ะ
  โต๊ะและตัวหนังสือใช้น้ำเงินปากกาลูกลื่น กระดาษแต่ละสีบอกความหมาย:
  ขาว = รายการ, เหลือง = ยอดของเรา, ชมพู = ของเจ้ามือ
  แดงตรายางใช้กับเลขที่บิล ตราประทับ และยอดขาดทุนเท่านั้น
*/
:root {
  color-scheme: light;
  --desk: #1b2a94;
  --desk-deep: #131e73;
  --on-desk: #ffffff;
  --on-desk-soft: #cfd6f7;
  --ink: #18247f;
  --ink-soft: #4a5699;
  --paper: #ffffff;
  --copy-yellow: #fff1a8;
  --copy-pink: #ffd3dd;
  --rule: #c3cbee;
  --rule-strong: #18247f;
  --stamp: #c4122f;
  --marker: #ffe63d;
  --marker-blue: #bfe6ff; /* แถวที่กลับมาจาก x3 x6 แยกจากสีเหลืองที่ใช้เตือน */
  --print: "Sarabun", "Leelawadee UI", Tahoma, sans-serif;
  --hand: "Itim", "Sarabun", "Leelawadee UI", sans-serif;
  --numbering: "Courier Prime", "Courier New", monospace;
  --sheet-shadow: 0 1px 0 rgba(8, 12, 60, .25), 0 14px 28px rgba(8, 12, 60, .28);
  font-family: var(--print);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--desk);
  color: var(--on-desk);
  font-size: 16px;
  line-height: 1.5;
}

h1, h2, h3, p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: inherit; }

:focus-visible {
  outline: 3px solid var(--marker);
  outline-offset: 1px;
  box-shadow: 0 0 0 1px var(--ink);
}

/* ---------- แถบบนโต๊ะ ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  max-width: 1320px;
  margin: 0 auto;
  padding: 16px 28px 4px;
}

.brand { display: flex; align-items: baseline; gap: 12px; text-decoration: none; }
.brand h1 { font-size: 23px; font-weight: 800; letter-spacing: -.005em; }
.brand small { color: var(--on-desk-soft); font-size: 15px; font-weight: 400; }

.topbar-side { display: flex; align-items: center; gap: 20px; }
.grand { display: flex; align-items: baseline; gap: 8px; color: var(--on-desk-soft); white-space: nowrap; }
.grand strong { color: var(--on-desk); font-size: 21px; font-weight: 700; font-variant-numeric: tabular-nums; }

.nav { display: flex; gap: 18px; }
.nav a { padding: 4px 0; border-bottom: 2px solid transparent; color: var(--on-desk-soft); text-decoration: none; white-space: nowrap; }
.nav a:hover { color: var(--on-desk); }
.nav a[aria-current="page"] { border-bottom-color: var(--marker); color: var(--on-desk); font-weight: 600; }

/* ---------- กระดาษ ---------- */

.sheet {
  position: relative;
  min-width: 0;
  background: var(--paper);
  color: var(--ink);
  box-shadow: var(--sheet-shadow);
}
.sheet-pad { padding: 18px 22px 20px; }
.sheet-yellow { background: var(--copy-yellow); }
.sheet-pink { background: var(--copy-pink); }

/* รอยปรุฉีกของสมุดบิล เห็นสีโต๊ะลอดรูขึ้นมา */
.perforated { padding-top: 22px; }
.perforated::before {
  content: "";
  position: absolute;
  inset: 0 0 auto;
  height: 14px;
  background: radial-gradient(circle at 11px 7px, var(--desk) 3.5px, transparent 4px) repeat-x 0 0 / 22px 14px;
}

.sheet-title { font-size: 19px; font-weight: 700; }
.sheet-title small { margin-left: 8px; color: var(--ink-soft); font-size: 15px; font-weight: 400; }
.sheet-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 12px; margin-bottom: 10px; }

.hint { color: var(--ink-soft); font-size: 15px; }
.hint strong { color: var(--ink); font-weight: 600; }
.on-desk { color: var(--on-desk-soft); }
.on-desk strong { color: var(--on-desk); }

/* ---------- ปุ่มและช่องกรอก ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  padding: 7px 16px;
  border: 1.5px solid var(--ink);
  border-radius: 3px;
  background: var(--paper);
  color: var(--ink);
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
}
.btn:hover { background: #eceffc; }
.btn:active { transform: translateY(1px); }
.btn:disabled { border-color: var(--rule); color: var(--ink-soft); background: var(--paper); cursor: not-allowed; }
.btn-primary { background: var(--ink); color: #fff; }
.btn-primary:hover { background: var(--desk-deep); }
.btn-primary:disabled { background: var(--ink-soft); color: #fff; }
.btn-danger:not(:disabled):hover { border-color: var(--stamp); background: #fdecef; color: var(--stamp); }
.btn-sm { min-height: 36px; padding: 3px 12px; font-size: 15px; }
.btn.is-done { border-color: var(--ink); background: var(--marker); color: var(--ink); }

.icon-btn {
  flex: none;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 3px;
  background: none;
  color: var(--ink-soft);
  font-size: 21px;
  line-height: 1;
  cursor: pointer;
}
.icon-btn:hover { background: #fdecef; color: var(--stamp); }

/* ช่องกรอกแบบเส้นประบนแบบฟอร์ม */
.field { display: grid; gap: 2px; min-width: 0; }
.field > span { color: var(--ink-soft); font-size: 14px; }
.field input, .field select, .name-cell input, .settings-table input {
  width: 100%;
  height: 40px;
  padding: 4px 6px;
  border: 0;
  border-bottom: 1.5px dashed var(--ink-soft);
  border-radius: 0;
  background: transparent;
  font-family: var(--hand);
  font-size: 18px;
}
.field select { cursor: pointer; }
.field input:hover, .field select:hover, .name-cell input:hover, .settings-table input:hover { border-bottom-style: solid; }
.field input::placeholder, .name-cell input::placeholder, .settings-table input::placeholder, textarea::placeholder {
  color: #7d87bd;
  font-family: var(--print);
  font-size: 15px;
}
.grow { flex: 1; }

/* ---------- ช่องเลขแบบแบบฟอร์ม ---------- */

.digits { display: inline-flex; vertical-align: middle; border: 1.5px solid var(--ink); background: var(--paper); }
.digits span {
  display: grid;
  place-items: center;
  width: 1.45em;
  height: 1.65em;
  border-left: 1px solid var(--ink);
  color: var(--ink);
  font-family: var(--hand);
  font-size: 1.1em;
  line-height: 1;
}
.digits span:first-child { border-left: 0; }
.digits-group { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; }
.digits-lg { font-size: 25px; }

/* ---------- หน้าแยกโพย: รูปบนโต๊ะ ---------- */

/* รูปโพยกว้างเกือบครึ่งจอ เทียบกับบิลข้างขวาได้สะดวก */
.layout {
  display: grid;
  grid-template-columns: minmax(320px, 1fr) minmax(0, 1.1fr);
  gap: 26px;
  max-width: 1680px;
  margin: 0 auto;
  padding: 18px 28px 48px;
  align-items: start;
}

.source { display: grid; gap: 14px; position: sticky; top: 16px; max-height: calc(100vh - 32px); overflow-y: auto; padding: 4px; scrollbar-width: thin; }

.drop {
  position: relative;
  display: grid;
  gap: 2px;
  padding: 18px 20px;
  border: 2px dashed var(--on-desk-soft);
  border-radius: 3px;
  cursor: pointer;
}
.drop:hover, .drop.is-over, .drop:focus-within { border-color: var(--marker); background: var(--desk-deep); }
.drop input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.drop strong { font-size: 21px; font-weight: 700; }
.drop span { color: var(--on-desk-soft); font-size: 15px; }

.manual summary { width: fit-content; color: var(--on-desk-soft); cursor: pointer; }
.manual summary:hover, .manual[open] summary { color: var(--on-desk); }
.manual-body { margin-top: 8px; padding: 14px; background: var(--paper); color: var(--ink); box-shadow: var(--sheet-shadow); }
.manual textarea {
  display: block;
  width: 100%;
  margin-bottom: 10px;
  padding: 2px 4px;
  border: 0;
  background: repeating-linear-gradient(transparent 0 29px, var(--rule) 29px 30px);
  background-attachment: local;
  font-family: var(--hand);
  font-size: 18px;
  line-height: 30px;
  resize: vertical;
}

.slips { display: grid; gap: 4px; }
.slips:empty { display: none; }
.slip-chip { display: flex; align-items: center; background: var(--desk-deep); }
.slip-chip.is-active { background: var(--paper); color: var(--ink); }
.slip-chip .icon-btn { color: var(--on-desk-soft); }
.slip-chip.is-active .icon-btn { color: var(--ink-soft); }
.slip-select { flex: 1; min-width: 0; display: flex; justify-content: space-between; gap: 10px; padding: 8px 4px 8px 12px; border: 0; background: none; cursor: pointer; text-align: left; }
.slip-select span:first-child { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.slip-select span:last-child { flex: none; font-variant-numeric: tabular-nums; font-weight: 600; }

/* รูปโพยแปะเทปไว้บนโต๊ะ */
.preview { position: relative; margin: 14px 0 0; padding: 9px; background: var(--paper); box-shadow: var(--sheet-shadow); }
.preview::before {
  display: none; /* เทปบังปุ่มซูม จึงไม่แสดง */
  content: "";
  position: absolute;
  z-index: 1;
  top: -13px;
  left: 50%;
  width: 104px;
  height: 27px;
  background: rgba(255, 241, 168, .78);
  box-shadow: 0 1px 2px rgba(8, 12, 60, .2);
  transform: translateX(-50%) rotate(1.4deg);
}

/* ---------- ตัวดูรูปโพย (ซูม ลาก หมุน เต็มจอ) ---------- */
.viewer { display: grid; gap: 6px; color: var(--ink); }
.viewer-tools { display: flex; flex-wrap: wrap; gap: 6px; }
.viewer-btn { min-width: 40px; min-height: 36px; padding: 2px 10px; border: 1.5px solid var(--ink); background: var(--paper); color: var(--ink); font: inherit; font-size: 17px; font-weight: 700; cursor: pointer; }
.viewer-btn:hover:not(:disabled) { background: var(--ink); color: #fff; }
.viewer-btn:disabled { border-color: var(--rule); color: var(--rule); cursor: default; }
.viewer-fit { min-width: 66px; font-size: 15px; }
.viewer-stage { max-height: min(74vh, 1100px); overflow: auto; background: #eef0f8; cursor: grab; scrollbar-width: thin; touch-action: pan-x pan-y; }
.viewer-stage.is-dragging { cursor: grabbing; }
.viewer-stage img { display: block; width: 100%; max-width: none; user-select: none; }
.viewer:not(.is-zoomed) .viewer-stage { cursor: zoom-in; }
.viewer-hint { margin: 0; color: var(--ink-soft); font-size: 13px; }
.viewer.is-full { position: fixed; z-index: 1000; inset: 0; grid-template-rows: auto 1fr auto; padding: 10px; background: rgba(8, 12, 60, .94); color: #fff; }
.viewer.is-full .viewer-stage { position: relative; max-height: none; min-height: 0; background: #1c2350; }
.viewer.is-full:not(.is-zoomed) .viewer-stage img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.viewer.is-full .viewer-btn { border-color: #fff; }
.viewer.is-full .viewer-hint { color: #c3cbee; }
body.viewer-open { overflow: hidden; }

/* หน้าบิลหลังบ้าน: มีรูปโพยวางข้างบิล */
.bill-layout { display: grid; gap: 18px; min-width: 0; }
.bill-layout.has-photo { grid-template-columns: minmax(0, 1fr) minmax(0, 1.1fr); align-items: start; }
.bill-photo { position: sticky; top: 12px; display: grid; gap: 8px; padding: 14px; }
.bill-photo .rows-title { margin: 0; }
.bill-photo .viewer-stage { max-height: min(78vh, 1100px); }
.admin:has(#view-bill:not([hidden]) .bill-layout.has-photo) { max-width: 1680px; }
@media (max-width: 900px) {
  .bill-layout.has-photo { grid-template-columns: minmax(0, 1fr); }
  .bill-photo { position: static; }
  .bill-photo .viewer-stage { max-height: 60vh; }
}

/* ปากกาเน้นข้อความไล่บนรูประหว่างอ่าน */
.preview.is-reading::after {
  content: "";
  position: absolute;
  inset: 9px 9px auto;
  height: 40px;
  background: var(--marker);
  mix-blend-mode: multiply;
  animation: highlight 2.4s ease-in-out infinite alternate;
}
@keyframes highlight { to { transform: translateY(min(48vh, 340px)); } }

/* ---------- บิล ---------- */

.bill { padding: 22px 26px 0; }
.bill-head { position: relative; display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 6px 16px; padding-bottom: 12px; border-bottom: 3px double var(--rule-strong); }
.bill-title { font-size: 28px; font-weight: 800; line-height: 1.15; letter-spacing: -.01em; }
.bill-source { display: block; color: var(--ink-soft); font-size: 15px; overflow-wrap: anywhere; }
.bill-no { color: var(--stamp); font-family: var(--numbering); font-size: 21px; font-weight: 700; letter-spacing: .05em; white-space: nowrap; }
.bill-no span { margin-right: 6px; color: var(--ink-soft); font-family: var(--print); font-size: 15px; font-weight: 400; letter-spacing: 0; }

.bill-fields { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); gap: 10px 24px; padding: 12px 0 14px; }
.draw-picker { display: flex; align-items: flex-end; gap: 8px; min-width: 0; }
.draw-picker .field { flex: 1; }

.draw-form {
  display: grid;
  grid-template-columns: minmax(150px, 1fr) minmax(170px, 1.4fr) auto auto;
  gap: 10px;
  align-items: end;
  margin-bottom: 14px;
  padding: 12px 14px;
  background: var(--copy-yellow);
}

.alerts { display: grid; gap: 6px; margin-bottom: 12px; }
.alerts:empty { display: none; }
.alert { width: fit-content; max-width: 100%; padding: 2px 8px; background: var(--marker); color: var(--ink); -webkit-box-decoration-break: clone; box-decoration-break: clone; }

.rows-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 0 8px; border-bottom: 2px solid var(--rule-strong); }
.rows-title { font-size: 17px; font-weight: 700; }
.rows-title small { margin-left: 6px; color: var(--ink-soft); font-size: 15px; font-weight: 400; }

/* บรรทัดในบิล */
.row {
  display: grid;
  grid-template-columns: 104px minmax(0, 1fr) minmax(0, 1fr) minmax(0, 1.1fr) 80px 36px;
  gap: 2px 14px;
  align-items: center;
  min-height: 50px;
  padding: 3px 0;
  border-bottom: 1px solid var(--rule);
}
.row.is-reversal { background: linear-gradient(transparent 6%, var(--marker-blue) 6%, var(--marker-blue) 94%, transparent 94%); }
.row.is-flagged { background: linear-gradient(transparent 6%, var(--marker) 6%, var(--marker) 94%, transparent 94%); }
.reversal-tag { padding: 0 8px; border: 1.5px solid var(--ink); border-radius: 3px; background: var(--paper); font-size: 14px; font-weight: 600; white-space: nowrap; }
.row input {
  width: 100%;
  min-width: 0;
  height: 40px;
  padding: 2px 4px;
  border: 0;
  border-radius: 0;
  background: transparent;
  font-family: var(--hand);
  font-size: 22px;
  text-align: right;
}
.row input:hover { background: rgba(24, 36, 127, .05); }
.row .num { font-size: 25px; letter-spacing: .12em; text-align: left; }
.amount { display: flex; align-items: center; justify-content: flex-end; gap: 8px; min-width: 0; }
.amount span { flex: none; color: var(--ink-soft); font-size: 15px; }
.amount input { flex: 0 1 4.4em; border-bottom: 1.5px dashed var(--rule); }
.amount.is-off { visibility: hidden; }
.row-total { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; text-align: right; }
.row-issues { grid-column: 1 / -1; display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; padding-bottom: 4px; font-size: 15px; }
.row-issues:empty { display: none; }
.check-btn { min-height: 30px; padding: 0 10px; border: 1.5px solid var(--ink); border-radius: 3px; background: var(--paper); font-weight: 600; cursor: pointer; }
.check-btn:hover { background: #eceffc; }

.bill-add { padding: 10px 0 2px; }

/* ท้ายบิลค้างอยู่ระหว่างเลื่อนดูรายการ */
.bill-foot {
  position: sticky;
  bottom: 0;
  z-index: 5;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 4px 20px;
  align-items: center;
  margin: 10px -26px 0;
  padding: 10px 26px 14px;
  background: var(--paper);
  box-shadow: 0 -6px 12px -8px rgba(8, 12, 60, .3);
}
.bill-total { display: flex; align-items: baseline; gap: 12px; }
.bill-total span { font-weight: 600; }
.bill-total strong { padding: 0 4px; border-bottom: 4px double var(--ink); font-size: 34px; font-weight: 800; line-height: 1.15; font-variant-numeric: tabular-nums; }
.bill-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
.save-message { grid-column: 1 / -1; font-size: 15px; }
.save-message:empty { display: none; }
.save-message.is-error { justify-self: start; padding: 0 6px; background: var(--marker); }

/* ตรายาง */
.stamp {
  position: absolute;
  z-index: 3;
  top: 2px;
  right: 190px;
  padding: 4px 14px 5px;
  border: 3px solid var(--stamp);
  border-radius: 4px;
  color: var(--stamp);
  font-size: 22px;
  font-weight: 800;
  line-height: 1.1;
  text-align: center;
  white-space: nowrap;
  opacity: .84;
  pointer-events: none;
  transform: rotate(-7deg);
}
.stamp.animate { animation: stamp-down .24s cubic-bezier(.3, 1.6, .5, 1) both; }
@keyframes stamp-down {
  from { opacity: 0; transform: rotate(-11deg) scale(2.1); }
  to { opacity: .84; transform: rotate(-7deg) scale(1); }
}

/* บิลเปล่าตอนยังไม่มีโพย */
.blank-note { padding: 16px 0 6px; font-size: 18px; }
.blank-note strong { display: block; font-size: 21px; font-weight: 700; }
.blank-note span { color: var(--ink-soft); font-size: 16px; }
.blank-lines { margin-bottom: 8px; }
.blank-line { display: flex; align-items: center; gap: 18px; min-height: 50px; border-bottom: 1px solid var(--rule); }
.blank-line b { color: var(--ink-soft); font-size: 14px; font-weight: 400; }
.blank-line i { margin-right: 10px; font-family: var(--hand); font-size: 22px; font-style: normal; }
.blank-line .blank-total { margin-left: auto; font-family: var(--print); font-size: 18px; font-weight: 700; }
.blank-line:nth-child(n+2) { min-height: 42px; }

.status { padding: 20px 24px; background: var(--paper); color: var(--ink); box-shadow: var(--sheet-shadow); font-size: 18px; }
.status.is-error { background: var(--copy-pink); }
.status .btn { margin-top: 12px; }
.spinner { display: inline-block; width: 15px; height: 15px; margin-right: 10px; border: 2.5px solid var(--rule); border-top-color: var(--ink); border-radius: 50%; vertical-align: -1px; animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.result-stack { display: grid; grid-template-columns: minmax(0, 1fr); gap: 22px; }

/* แยกตามประเภท */
.types { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 22px 28px; }
.type-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding-bottom: 5px; border-bottom: 2px solid var(--rule-strong); }
.type-chip { font-weight: 700; white-space: nowrap; }
.type-chip small { margin-left: 3px; color: var(--ink-soft); font-size: 14px; font-weight: 400; }
.type-head .btn { min-height: 30px; padding: 0 10px; font-size: 14px; }
.type-card table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.type-card th, .type-card td { padding: 4px 2px; border-bottom: 1px solid var(--rule); text-align: right; }
.type-card th { color: var(--ink-soft); font-size: 14px; font-weight: 400; }
.type-card th:first-child, .type-card td:first-child { text-align: left; }
.type-card tbody td:first-child { font-family: var(--hand); font-size: 18px; letter-spacing: .06em; }
.type-card tfoot td { border-bottom: 0; font-weight: 700; }

/* ---------- หน้าหลังบ้าน ---------- */

.admin { max-width: 1180px; margin: 0 auto; padding: 14px 28px 56px; }
.view { display: grid; grid-template-columns: minmax(0, 1fr); gap: 18px; min-width: 0; }
.stack { display: grid; grid-template-columns: minmax(0, 1fr); gap: 18px; min-width: 0; }

.tabs { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 20px; border-bottom: 2px solid var(--on-desk-soft); }
.tab { margin-bottom: -2px; padding: 8px 18px; border-bottom: 2px solid transparent; color: var(--on-desk-soft); text-decoration: none; }
.tab:hover { color: var(--on-desk); }
.tab.is-active { border-bottom-color: var(--paper); background: var(--paper); color: var(--ink); font-weight: 700; }
.sub-tabs { margin-bottom: 0; }

/* ลอยไว้กลางจอด้านบน ให้เห็นผลการบันทึกได้แม้เลื่อนลงไปท้ายบิล */
.banner {
  position: fixed;
  top: 16px;
  left: 50%;
  z-index: 50;
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 10px 18px;
  transform: translateX(-50%);
  border: 2px solid var(--ink);
  background: var(--marker);
  color: var(--ink);
  font-weight: 600;
  box-shadow: 0 8px 20px -6px rgba(8, 12, 60, .45);
  cursor: pointer;
}
.banner.is-error { background: var(--copy-pink); color: var(--stamp); }

.filters { display: flex; flex-wrap: wrap; gap: 12px 18px; align-items: flex-end; }
.filters .field { flex: 1 1 170px; }
.filters .btn { flex: none; }

.page-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; }
.page-head h2 { font-size: 27px; font-weight: 800; line-height: 1.2; }
.back-link { justify-self: start; color: var(--on-desk-soft); text-decoration: underline; text-underline-offset: 3px; }
.back-link:hover { color: var(--on-desk); }

/* เล่มบิลของแต่ละงวด ยิ่งมีบิลเยอะเล่มยิ่งหนา */
.books { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 26px 24px; }
.book {
  display: grid;
  gap: 12px;
  padding: 26px 20px 16px;
  background: var(--paper);
  color: var(--ink);
  text-decoration: none;
  box-shadow: var(--sheet-shadow);
}
.book:hover h3 { text-decoration: underline; text-underline-offset: 4px; }
.book.thick-1 { box-shadow: 4px 4px 0 #dfe4fa, var(--sheet-shadow); }
.book.thick-2 { box-shadow: 4px 4px 0 #dfe4fa, 8px 8px 0 #fff1a8, var(--sheet-shadow); }
.book.thick-3 { box-shadow: 4px 4px 0 #dfe4fa, 8px 8px 0 #fff1a8, 12px 12px 0 #ffd3dd, var(--sheet-shadow); }
.book { position: relative; }
.book::before {
  content: "";
  position: absolute;
  inset: 0 0 auto;
  height: 14px;
  background: radial-gradient(circle at 11px 7px, var(--desk) 3.5px, transparent 4px) repeat-x 0 0 / 22px 14px;
}
.book h3 { font-size: 22px; font-weight: 800; line-height: 1.25; }
.book h3 small { display: block; font-family: var(--hand); font-size: 18px; font-weight: 400; }
.book-lines { margin: 0; }
.book-lines div { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0; border-top: 1px solid var(--rule); }
.book-lines dt { color: var(--ink-soft); }
.book-lines dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ยอดของเรา: คิดเลขเป็นบรรทัดลงมา ปิดท้ายด้วยเส้นคู่ */
.summary-top { display: grid; grid-template-columns: minmax(0, 1.05fr) minmax(0, 1fr); gap: 22px; align-items: stretch; }
.sum-line { display: flex; align-items: baseline; gap: 8px; padding: 5px 0; font-size: 18px; }
.sum-line i { flex: 1; min-width: 12px; border-bottom: 2px dotted rgba(24, 36, 127, .4); }
.sum-line b { font-size: 21px; font-weight: 700; font-variant-numeric: tabular-nums; }
.sum-total { align-items: flex-end; margin-top: 8px; padding-top: 10px; border-top: 2px solid var(--ink); }
.sum-total span { font-size: 19px; font-weight: 700; }
.sum-total i { border: 0; }
.sum-total b { padding: 0 4px; border-bottom: 5px double var(--ink); font-size: 44px; font-weight: 800; line-height: 1.1; }
.sum-total.is-loss b { border-bottom-color: var(--stamp); color: var(--stamp); }
.sum-tag { margin-top: 8px; font-size: 15px; font-weight: 600; text-align: right; }
.sum-tag.is-loss { color: var(--stamp); }
.sum-sub { margin-bottom: 14px; padding-top: 6px; border-top: 1px solid var(--ink); }
.sum-sub span, .sum-sub b { font-weight: 800; }
.sum-sub i { border: 0; }
.sum-extra { margin-top: 16px; padding-top: 10px; border-top: 1px dashed var(--ink); }
.sum-extra .sum-line.is-loss b { color: var(--stamp); }

.notes { display: grid; align-content: stretch; }
.note { display: grid; gap: 5px; padding: 13px 0; border-top: 1px solid var(--rule); }
.note:first-child { padding-top: 0; border-top: 0; }
.note:last-child { padding-bottom: 0; }
.note-label { font-weight: 700; }
.note-value { font-size: 26px; font-weight: 800; line-height: 1.2; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.note-value.is-loss { color: var(--stamp); }
.note-sub { color: var(--ink-soft); font-size: 15px; }
.note-result { color: var(--ink-soft); }

.two-col { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 22px; align-items: start; }

.max-loss-field { max-width: 340px; margin-bottom: 8px; }
.max-loss-field input { font-size: 22px; text-align: right; }

/* กล่องเทียบวิธีส่ง */
.compare-table { max-height: 300px; overflow-y: auto; margin-top: 10px; }

/* กล่องตัดส่งด้านบน */
.cut-sheet { display: grid; align-content: start; min-width: 0; }
.cut-table { max-height: 250px; overflow-y: auto; margin-top: 10px; background: var(--copy-pink); }
.cut-compare { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px 18px; margin-top: 14px; padding-top: 12px; border-top: 2px solid var(--rule-strong); }
.cut-note { display: grid; align-content: start; gap: 5px; min-width: 0; }
.cut-note .note-value { font-size: 22px; }
.cut-sheet .payout-breakdown { border-top-color: rgba(24, 36, 127, .35); }
.cut-sheet .note-sub, .cut-sheet .note-result { color: var(--ink); }

/* ใครถูกอะไร และแบ่งจ่ายกันอย่างไร */
.hit-line { display: flex; justify-content: space-between; gap: 14px; font-size: 15px; }
.hit-line b { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.payout-breakdown { display: grid; gap: 1px; margin-top: 2px; padding-top: 5px; border-top: 1px dashed var(--rule); }
.payout-breakdown .hit-line:last-child { font-weight: 700; }
.hits-cell { min-width: 220px; }
.muted-label { color: var(--ink-soft); font-size: 14px; }
.data-table td.is-loss { color: var(--stamp); }
.table-note { margin-top: 8px; font-size: 14px; }

.plan-status { margin-bottom: 8px; font-size: 15px; font-weight: 600; }
.sheet-pink .sum-line { padding: 2px 0; font-size: 16px; }
.sheet-pink .sum-line b { font-size: 17px; }
.sheet-pink .table-wrap { margin-top: 10px; }
.sheet-pink .sum-line i { border-bottom-color: rgba(24, 36, 127, .35); }

/* เลขที่ยังไม่ซื้อ */
.unbought-switch { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.unbought-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(3.4em, 1fr));
  gap: 2px 6px;
  max-height: 300px;
  overflow-y: auto;
  padding: 4px 2px;
  border-top: 2px solid var(--rule-strong);
  font-family: var(--hand);
  font-size: 18px;
  scrollbar-width: thin;
}
.unbought-grid span { padding: 1px 0; border-bottom: 1px solid var(--rule); text-align: center; }
.unbought-grid:empty { display: none; }

/* ตารางบนกระดาษ */
.table-wrap { overflow-x: auto; scrollbar-width: thin; }
.scroll-y { max-height: 470px; overflow-y: auto; }
.data-table { width: 100%; border-collapse: collapse; }
.data-table th, .data-table td { padding: 8px 10px; border-bottom: 1px solid var(--rule); text-align: left; }
.sheet-pink .data-table th, .sheet-pink .data-table td { border-bottom-color: rgba(24, 36, 127, .25); }
.data-table thead th { position: sticky; top: 0; z-index: 1; border-bottom: 2px solid var(--rule-strong); background: var(--paper); color: var(--ink-soft); font-size: 14px; font-weight: 400; white-space: nowrap; }
.sheet-pink .data-table thead th { background: var(--copy-pink); }
.data-table .num-cell { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
/* หัวตารางสองแถว: แถวบนบอกกลุ่มคอลัมน์ จึงไม่ตรึงหัวตาราง กันสองแถวซ้อนกัน */
.data-table.grouped-head thead th { position: static; }
.data-table.grouped-head .group-row th { border-bottom: 0; padding-bottom: 0; }
.data-table.grouped-head .group-row th.group-th { text-align: center; color: var(--ink); font-weight: 700; border-bottom: 2px solid var(--rule); }
.data-table.grouped-head .group-row th.group-strong { color: var(--stamp); border-bottom-color: var(--stamp); }
.data-table.grouped-head .group-first { border-left: 1px solid var(--rule); }
.data-table.grouped-head tbody td:first-child { white-space: nowrap; }
.data-table td.strong { font-weight: 700; }
.data-table .action-cell { text-align: right; white-space: nowrap; }
.data-table .action-cell .btn { min-height: 36px; padding: 2px 12px; font-size: 15px; }
.data-table .action-cell .btn + .btn { margin-left: 6px; }
.numbers-cell { font-family: var(--hand); font-size: 17px; }
.data-table td a:not(.btn) { text-underline-offset: 3px; white-space: nowrap; }
.data-table > tfoot td { border-top: 2px solid var(--rule-strong); border-bottom: 0; font-weight: 700; }
.data-table tr.is-idle td { color: var(--ink-soft); }

/* เมนูลูกค้า: เลือกงวด และรายละเอียด % แยกประเภท */
.customer-head { align-items: end; }
.customer-draw { min-width: min(320px, 100%); }
.detail-row > td { padding: 4px 10px 14px 28px; background: var(--copy-yellow); }
.percent-breakdown { width: min(640px, 100%); border-collapse: collapse; }
.percent-breakdown th, .percent-breakdown td { padding: 5px 10px; border-bottom: 1px dashed rgba(24, 36, 127, .3); text-align: left; }
.percent-breakdown thead th { color: var(--ink-soft); font-size: 14px; font-weight: 400; white-space: nowrap; }
.percent-breakdown tfoot td { border-top: 2px solid var(--rule-strong); border-bottom: 0; font-weight: 700; }
.hide-paid .paid-col { display: none; }
.paid-check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; white-space: nowrap; font-size: 14px; color: var(--ink-soft); }
.paid-check input { width: 22px; height: 22px; margin: 0; accent-color: #1d7a3a; cursor: pointer; }
.paid-check input:disabled { cursor: not-allowed; }
.data-table tr.is-paid > td { background: #e3f4e6; }
.data-table tr.is-paid .paid-check { color: #1d6b34; font-weight: 600; }
.paid-sum { font-size: 14px; white-space: nowrap; }
#view-customers .name-cell { min-width: 150px; }
#view-customers .action-cell .btn { padding: 2px 10px; }
.name-cell .tag { display: inline-block; margin-top: 4px; padding: 0 8px; border: 1px solid var(--stamp); color: var(--stamp); font-size: 13px; font-weight: 600; }
.name-cell { min-width: 190px; }
.name-cell input { height: 36px; }
.empty-note { padding: 26px 12px; color: var(--ink-soft); text-align: center; }

.bar-cell { min-width: 140px; }
.bar-cell b { display: block; font-weight: 700; }
.bar { display: block; height: 4px; margin: 4px 0 0 auto; border-radius: 0 2px 2px 0; background: var(--ink); }

/* % ลูกค้าและตั้งค่า */
.rate-groups { display: grid; grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); gap: 16px 28px; }
.rate-group { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 16px; margin: 0; padding: 0; border: 0; }
.rate-group legend { width: 100%; margin-bottom: 4px; padding: 0 0 3px; border-bottom: 2px solid var(--rule-strong); font-weight: 700; }
.rate-group input { text-align: right; }
.settings-table input { min-width: 104px; height: 36px; text-align: right; }
.settings-table .group-start td { border-top: 2px solid var(--rule-strong); }
.rate-unset { display: block; color: var(--stamp); font-size: 14px; font-weight: 600; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }

@media (prefers-reduced-motion: reduce) {
  .preview.is-reading::after { animation: none; opacity: .6; }
  .stamp.animate { animation: none; }
  .spinner { animation-duration: 3s; }
}

/* ---------- จอเล็ก ---------- */

@media (max-width: 860px) {
  .topbar { padding: 12px 16px 2px; }
  .layout { grid-template-columns: minmax(0, 1fr); gap: 18px; padding: 12px 16px 32px; }
  .source { position: static; max-height: none; overflow: visible; padding: 0; }
  .viewer-stage { max-height: 60vh; }
  .admin { padding: 10px 16px 40px; }
  .summary-top { grid-template-columns: minmax(0, 1fr); }
  .bill { padding: 20px 16px 0; }
  .bill-foot { position: static; margin: 10px -16px 0; padding: 10px 16px 14px; box-shadow: none; }
  .bill-fields { grid-template-columns: minmax(0, 1fr); }
  .draw-form { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .stamp { top: auto; bottom: 8px; right: 0; font-size: 19px; }
  .sheet-pad { padding: 16px; }
}

@media (max-width: 560px) {
  .brand small, .grand span { display: none; }
  .brand h1 { font-size: 20px; }
  .topbar-side { gap: 12px; }
  .nav { gap: 12px; }
  .row { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 0 12px; padding: 6px 0; }
  .row .num { grid-column: 1; }
  .row .row-total { grid-column: 2; grid-row: 1; padding-right: 42px; }
  .row .icon-btn { grid-column: 2; grid-row: 1; justify-self: end; }
  .amount { gap: 6px; }
  .amount span { min-width: 0; }
  .amount.is-off { display: none; }
  .bill-title { font-size: 25px; }
  .bill-foot { grid-template-columns: minmax(0, 1fr); }
  .bill-actions { justify-content: stretch; }
  .bill-actions .btn { flex: 1; }
  .draw-form .btn { width: 100%; }
  .two-col { grid-template-columns: minmax(0, 1fr); }
  .sum-total b { font-size: 36px; }
  .books { grid-template-columns: minmax(0, 1fr); }
  .page-head h2 { font-size: 23px; }
}

/* ส่งตามยอดที่กำหนด */
.amount-form { display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: flex-end; margin-bottom: 8px; }
.amount-form .field { flex: 1 1 160px; }
.amount-form .btn { flex: none; }
.amount-body { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 8px 28px; align-items: start; }
.amount-body .compare-table { margin-top: 0; max-height: 340px; }
.head-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.amount-types { margin-top: 18px; padding-top: 12px; border-top: 2px solid var(--rule-strong); }
.amount-types .sub-title { margin: 0 0 8px; font-size: 16px; }
.type-copy { display: grid; gap: 6px; margin-top: 10px; }
.type-copy > span { color: var(--ink-soft); font-size: 14px; }
.type-copy-buttons { display: flex; flex-wrap: wrap; gap: 8px; }
.dealer-pay .cash-now { display: block; margin-top: 4px; padding-top: 4px; border-top: 1px dashed var(--rule-strong); font-weight: 400; }
.dealer-pay .cash-now b { color: var(--stamp); }
.dealer-pay .extra-pay { display: block; margin-top: 4px; padding-top: 4px; border-top: 1px dashed var(--rule-strong); color: var(--ink-soft); font-weight: 400; }

/* กล่องคัดลอกเอง เมื่อเบราว์เซอร์ไม่ยอมให้คัดลอกอัตโนมัติ */
.copy-box { width: min(560px, calc(100vw - 32px)); border: 2px solid var(--rule-strong); padding: 16px; display: grid; gap: 10px; }
.copy-box[open] { display: grid; }
.copy-box::backdrop { background: rgb(0 0 0 / 0.35); }
.copy-box p { margin: 0; }
.copy-box-text { width: 100%; min-height: 220px; font: 16px/1.5 "Courier Prime", monospace; }
.copy-box .btn { justify-self: end; }

/* บรรทัดในบิลมี 3 ช่องราคา จอแคบให้ช่องราคาขึ้นบรรทัดใหม่ */
@media (max-width: 760px) {
  .row { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 0 12px; padding: 6px 0; }
  .row .num { grid-column: 1; }
  .row .row-total { grid-column: 2; grid-row: 1; padding-right: 42px; }
  .row .icon-btn { grid-column: 2; grid-row: 1; justify-self: end; }
  .amount.is-off { display: none; }
}
