/* ===== UI shell: form controls, panels, buttons (invoice has its own file) ===== */

:root {
  --ring: 13 148 136;
}

html { -webkit-text-size-adjust: 100%; }

body { margin: 0; }

/* ---------- cards & panels ---------- */
.card {
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 14px;
  padding: 14px 14px 15px;
  box-shadow: 0 1px 2px rgba(15, 23, 42, .05);
}

.card-h {
  display: block;
  margin: 0 0 10px;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0;
  color: #0f766e;
  text-transform: none;
  padding-left: 9px;
  border-left: 3px solid #0d9488;
  line-height: 1.2;
}

.panel {
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 1px 2px rgba(15, 23, 42, .05);
}

.preview-wrap {
  background: #f1f5f9;
  padding: 16px;
  overflow: hidden;
}

.invoice-scale {
  transform-origin: top left;
  will-change: transform;
}

.chip {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  color: #0f766e;
  background: #f0fdfa;
  border: 1px solid #99f6e4;
  border-radius: 999px;
  padding: 2px 9px;
}

/* ---------- fields ---------- */
.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.field > span {
  font-size: 12px;
  font-weight: 600;
  color: #475569;
  line-height: 1.35;
}

.field input,
.field select,
.field textarea {
  width: 100%;
  box-sizing: border-box;
  font-family: inherit;
  font-size: 15px;
  line-height: 1.4;
  color: #0f172a;
  background: #fff;
  border: 1px solid #cbd5e1;
  border-radius: 9px;
  padding: 7px 10px;
  min-height: 38px;
  transition: border-color .12s, box-shadow .12s;
  -webkit-appearance: none;
  appearance: none;
}

.field select {
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20' fill='%2364748b'%3E%3Cpath d='M5.5 7.5 10 12l4.5-4.5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 6px center;
  background-size: 20px;
  padding-right: 28px;
}

.field textarea { min-height: 72px; resize: vertical; line-height: 1.5; }

.field input::placeholder, .field textarea::placeholder { color: #94a3b8; }

.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: #0d9488;
  box-shadow: 0 0 0 3px rgba(var(--ring), .18);
}

.field input:invalid:not(:placeholder-shown) { border-color: #f59e0b; }

.calc-line {
  margin: 9px 0 0;
  font-size: 12.5px;
  font-weight: 600;
  color: #0f766e;
  background: #f0fdfa;
  border: 1px dashed #99f6e4;
  border-radius: 8px;
  padding: 5px 9px;
  font-variant-numeric: tabular-nums;
}

.calc-line.is-bad { color: #b45309; background: #fffbeb; border-color: #fcd34d; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 600;
  line-height: 1.2;
  border-radius: 10px;
  padding: 9px 13px;
  min-height: 38px;
  border: 1px solid transparent;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color .12s, border-color .12s, box-shadow .12s, opacity .12s;
}

.btn:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(var(--ring), .35); }
.btn[disabled] { opacity: .6; cursor: progress; }

/* ปุ่มที่ยังกดไม่ได้เพราะเงื่อนไขไม่ครบ (สีจางจากกฎบน + ไม่ใช่เคอร์เซอร์ "กำลังทำงาน") */
.btn:disabled:not([data-action="download"]) { cursor: not-allowed; }

.btn-primary { background: #0f766e; color: #fff; box-shadow: 0 1px 2px rgba(15, 118, 110, .35); }
.btn-primary:hover { background: #115e59; }

.btn-ghost { background: #fff; color: #334155; border-color: #cbd5e1; }
.btn-ghost:hover { background: #f8fafc; border-color: #94a3b8; }

.btn-sm { font-size: 12.5px; min-height: 30px; padding: 5px 10px; border-radius: 8px; }

.link-btn {
  display: inline-flex;
  align-items: center;
  font-family: inherit;
  font-size: 12px;
  font-weight: 600;
  color: #0f766e;
  background: none;
  border: 0;
  min-height: 26px;
  padding: 4px 2px;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.link-btn:hover { color: #115e59; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translate(-50%, 24px);
  z-index: 60;
  max-width: min(92vw, 460px);
  background: #0f172a;
  color: #f8fafc;
  font-size: 13.5px;
  font-weight: 500;
  line-height: 1.4;
  padding: 10px 16px;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, .3);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s, transform .18s;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.bad { background: #7f1d1d; }

/* ---------- offscreen capture stage ---------- */
.capture-stage {
  position: fixed;
  left: -20000px;
  top: 0;
  z-index: -1;
  pointer-events: none;
}

@media (max-width: 640px) {
  .preview-wrap { padding: 8px; }
}

/* ---------- โหมดแก้เทมเพลต (ของเหล่านี้ไม่ติดไปในรูปที่ export) ---------- */
.inv.is-editing { outline: 2px dashed #99f6e4; outline-offset: 6px; }

.ed-tip {
  margin: 0 0 6px;
  font-family: 'Sarabun', 'Leelawadee UI', Tahoma, sans-serif;
  font-size: 11px;
  line-height: 1.45;
  color: #0f766e;
  background: #f0fdfa;
  border: 1px dashed #99f6e4;
  border-radius: 6px;
  padding: 4px 8px;
}

.inv-blk { position: relative; }
.inv-blk.is-off,
tr.is-off { opacity: .42; }
.is-dragging { opacity: .35; }

/* .ed-bar เป็น display:flex จึงทับ [hidden] ของเบราว์เซอร์ — ต้องมีกฎนี้ (แบบเดียวกับ .cal-pop) */
.ed-bar[hidden] { display: none !important; }

.ed-bar {
  display: flex;
  align-items: center;
  gap: 5px;
  margin: 8px 0 2px;
  padding: 3px 6px;
  font-family: 'Sarabun', 'Leelawadee UI', Tahoma, sans-serif;
  font-size: 11.5px;
  line-height: 1.2;
  color: #0f766e;
  background: #f0fdfa;
  border: 1px dashed #5eead4;
  border-radius: 6px;
  cursor: grab;
  user-select: none;
}

.ed-bar:active { cursor: grabbing; }
.ed-grab { font-size: 12px; letter-spacing: 1px; color: #0d9488; }
.ed-name { font-weight: 600; }
.ed-spacer { flex: 1 1 auto; }

.ed-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 4px;
  font-family: inherit;
  font-size: 11px;
  font-weight: 600;
  line-height: 1;
  color: #0f766e;
  background: #fff;
  border: 1px solid #99f6e4;
  border-radius: 5px;
  cursor: pointer;
}

.ed-btn:hover { background: #ccfbf1; }
.ed-btn:focus-visible { outline: 2px solid #0d9488; outline-offset: 1px; }
.ed-btn.wide { padding: 0 7px; min-width: 0; }

.ed-rowtools {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  margin-left: 6px;
  vertical-align: middle;
}

.item-label[contenteditable="true"],
.text-h[contenteditable="true"],
.text-body[contenteditable="true"] {
  outline: 1px dashed #5eead4;
  outline-offset: 1px;
  border-radius: 3px;
}

.ed-add td {
  border: 1px dashed #5eead4;
  background: #f0fdfa;
  padding: 5px 8px;
}

.ed-add-block {
  margin-top: 14px;
  padding: 6px;
  border: 1px dashed #5eead4;
  border-radius: 8px;
  background: #f0fdfa;
  text-align: center;
}

.inv-blk.drop-before { box-shadow: 0 -3px 0 0 #0d9488; }
.inv-blk.drop-after { box-shadow: 0 3px 0 0 #0d9488; }
tr.drop-before > td { box-shadow: inset 0 3px 0 0 #0d9488; }
tr.drop-after > td { box-shadow: inset 0 -3px 0 0 #0d9488; }

@media (max-width: 640px) {
  .ed-bar { font-size: 10.5px; }
  .ed-rowtools { gap: 2px; }
}

/* ---------- ซ่อนชิ้นส่วนฟอร์มตามเทมเพลต ----------
   .field เป็น display:flex จึงทับ [hidden] ของเบราว์เซอร์ ต้องใช้คลาสนี้เท่านั้น */
.form-off { display: none !important; }

/* ---------- การ์ดใบ้การซ่อน (การ์ดเดียว) ---------- */
.form-hint {
  padding: 10px 12px;
  background: #f0fdfa;
  border-color: #99f6e4;
  box-shadow: none;
}

.form-hint p {
  margin: 0;
  font-size: 12.5px;
  line-height: 1.45;
  color: #0f766e;
}

/* ---------- checkbox แบบบรรทัดเดียว ---------- */
.check-line { display: flex; align-items: flex-start; gap: 8px; font-size: 12.5px; color: #475569; cursor: pointer; }
.check-line input { width: 15px; height: 15px; margin: 2px 0 0; accent-color: #0d9488; flex: none; }
.check-line > span { font-weight: 500; line-height: 1.4; }

/* ช่องที่ถูกปิด (เช่น เลขห้องตอนติ๊ก "ไม่ใส่เลขห้อง") */
.field input:disabled { background: #f1f5f9; color: #94a3b8; cursor: not-allowed; }

/* ค่าที่มาจากทะเบียน (readonly) — ต้องดูเป็น "อ่านอย่างเดียว" ไม่ใช่ช่องที่พิมพ์ได้ */
.field input.is-locked { background: #f8fafc; color: #475569; border-color: #e2e8f0; cursor: not-allowed; }
.field input.is-locked:focus { box-shadow: none; border-color: #e2e8f0; }

/* ป้ายบอกว่าค่าชุดนี้แก้ที่หน้าอื่น */
.lock-tag { margin-left: 8px; padding: 1px 8px; border: 1px solid #dbe3ec; border-radius: 999px; background: #eef2f6; color: #475569; font-size: 11px; font-weight: 500; vertical-align: middle; }

/* ---------- สลับมุมมอง: หน้าหลัก / หน้าแก้ไขขั้นสูง ----------
   <main> เป็น display:grid จึงทับ [hidden] ของเบราว์เซอร์ ต้องใช้คลาสนี้เท่านั้น */
.view-off { display: none !important; }

/* ---------- รายการผู้เช่าในหน้าแก้ไขขั้นสูง ---------- */
.tenant-list { display: flex; flex-direction: column; gap: 8px; }

.tenant-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 11px;
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  background: #f8fafc;
}

.tenant-main { flex: 1 1 auto; min-width: 0; }
.tenant-name { font-size: 14px; font-weight: 700; line-height: 1.3; color: #0f172a; }
.tenant-meta { margin-top: 3px; font-size: 12px; line-height: 1.45; color: #475569; }
.tenant-acts { display: flex; flex: none; gap: 6px; }
.tenant-empty { margin: 0; font-size: 12.5px; line-height: 1.45; color: #64748b; }

/* ---------- ช่องวันที่ + ปฏิทิน พ.ศ. ---------- */
.date-row { display: flex; align-items: stretch; gap: 6px; }
.date-row input { flex: 1 1 auto; min-width: 0; background: #fff; cursor: pointer; }
.date-row .btn { flex: 0 0 auto; }
/* ปุ่มเปิดปฏิทิน: ต้องกดด้วยนิ้วได้ ดูด hit area ให้สูงเท่าช่องกรอก */
.date-row .link-btn { flex: 0 0 auto; justify-content: center; min-width: 40px; align-self: stretch; }

.cal-pop {
  grid-column: 1 / -1;
  margin-top: 4px;
  padding: 10px;
  background: #fff;
  border: 1px solid #cbd5e1;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, .12);
}

.cal-pop[hidden] { display: none; }

.cal-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }

.cal-title {
  flex: 1 1 auto;
  text-align: center;
  font-size: 14px;
  font-weight: 700;
  color: #0f172a;
}

.cal-nav {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 1px solid #cbd5e1;
  background: #fff;
  font-size: 18px;
  line-height: 1;
  color: #0f766e;
  cursor: pointer;
}

.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }

.cal-dow { text-align: center; font-size: 11px; color: #64748b; padding: 2px 0; }

.cal-pad { height: 34px; }

.cal-day {
  height: 34px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: #f8fafc;
  font-family: inherit;
  font-size: 14px;
  color: #0f172a;
  cursor: pointer;
}

.cal-day.is-today { border-color: #5eead4; }
.cal-day.is-pick { background: #0f766e; color: #fff; font-weight: 700; }

.cal-foot { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; }

.hint-line {
  margin: 6px 0 0;
  font-size: 11.5px;
  line-height: 1.45;
  color: #64748b;
}

/* ใบ้ใต้ปุ่มที่ยังกดไม่ได้ = แดงเข้ม (syncMetersGate ใส่/ถอด is-warn ให้) */
.hint-line.is-warn {
  color: #b91c1c;
  font-weight: 600;
}

/* ---------- แถบปุ่มลอยล่างจอ (มือถือเท่านั้น) ---------- */
.quick-bar {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: #fff;
  border-top: 1px solid #e2e8f0;
  box-shadow: 0 -6px 18px rgba(15, 23, 42, .08);
}

.quick-bar[hidden] { display: none !important; }

.quick-total { font-size: 13px; font-weight: 600; color: #0f172a; white-space: nowrap; }

/* ---------- มือถือ (Android เช่น Vivo Y): เรียบง่าย กดง่าย อ่านง่าย ---------- */
@media (max-width: 720px) {
  body { padding-bottom: 76px; }
  main { padding-left: 10px !important; padding-right: 10px !important; padding-top: 12px !important; }
  .card { border-radius: 12px; padding: 12px 12px 13px; }
  .card-h { font-size: 12.5px; margin-bottom: 8px; }
  .field input,
  .field select,
  .field textarea { font-size: 16px; min-height: 44px; }
  .field > span { font-size: 12.5px; }
  .btn { min-height: 44px; font-size: 14px; }
  .btn-sm { min-height: 38px; font-size: 13px; }
  .check-line input { width: 24px; height: 24px; }
  .check-line { font-size: 13.5px; line-height: 1.45; padding: 4px 0; }
  .quick-bar { display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; }
  .quick-bar .btn { flex: 1 1 auto; }
  .panel .border-b { padding-left: 12px; padding-right: 12px; }
  .cal-day, .cal-pad { height: 40px; }
}
