/* ปาลิกา อิเลคทริคอล — ใบเสนอราคา
   ออกแบบสำหรับมือถือก่อน: ปุ่มใหญ่กดง่าย อ่านกลางแดดได้ ใช้มือเดียวได้ */

:root {
  color-scheme: light;
  --brand: #5B5BF0;
  --brand-dark: #4B4BD8;
  --brand-soft: rgba(91, 91, 240, .12);
  --ink: #14141C;
  --ink2: #3C3C4A;
  --muted: #6B6B80;
  --faint: #9A9AAE;
  --line: #E4E4EC;
  --line2: #EFEFF4;
  --bg: #F4F4F8;
  --card: #FFFFFF;
  --ok: #14875C;
  --ok-soft: rgba(70, 192, 138, .16);
  --warn: #F5B93B;
  --warn-ink: #9A6B00;
  --warn-soft: rgba(245, 185, 59, .2);
  --danger: #C8372D;
  --r: 16px;
  --nav-h: calc(60px + env(safe-area-inset-bottom));
  --num: 'Space Grotesk', 'Noto Sans Thai', system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: 'Noto Sans Thai', system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

body { display: flex; flex-direction: column; }

.num { font-family: var(--num); font-feature-settings: 'tnum' 1; }

/* ---------- โครงหน้า ---------- */
#app {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(var(--nav-h) + 16px);
}

.view { padding: 0 16px; max-width: 640px; margin: 0 auto; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--bg);
  padding: calc(env(safe-area-inset-top) + 14px) 16px 10px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.topbar.bordered { background: var(--card); border-bottom: 1px solid var(--line); }
.topbar h1 { margin: 0; font-size: 20px; font-weight: 600; letter-spacing: -.01em; }
.topbar .sub { font-size: 12px; color: var(--muted); font-family: var(--num); }
.topbar .grow { flex: 1 1 auto; min-width: 0; }

.iconbtn {
  flex: none;
  width: 40px; height: 40px;
  display: flex; align-items: center; justify-content: center;
  border: none; background: transparent; color: var(--ink);
  border-radius: 12px; font-size: 22px; cursor: pointer;
}
.iconbtn:active { background: var(--line2); }

/* ---------- การ์ด ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: 14px 16px;
  margin-bottom: 10px;
}
.card.tap { cursor: pointer; }
.card.tap:active { background: #FAFAFD; }

.row { display: flex; align-items: center; gap: 10px; }
.row.between { justify-content: space-between; }
.row.top { align-items: flex-start; }
.grow { flex: 1 1 auto; min-width: 0; }
.stack { display: flex; flex-direction: column; }
.gap4 { gap: 4px; } .gap6 { gap: 6px; } .gap8 { gap: 8px; } .gap12 { gap: 12px; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.tiny { font-size: 12px; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.chip {
  font-size: 11px; font-weight: 600;
  padding: 4px 9px; border-radius: 10px;
  flex: none; white-space: nowrap;
}
.chip.draft { color: var(--warn-ink); background: var(--warn-soft); }
.chip.sent { color: var(--brand-dark); background: var(--brand-soft); }
.chip.approved { color: var(--ok); background: var(--ok-soft); }

/* ---------- สรุปยอด ---------- */
.stats { display: flex; gap: 10px; margin-bottom: 12px; }
.stat {
  flex: 1; background: var(--card); border: 1px solid var(--line);
  border-radius: 14px; padding: 12px 14px;
  display: flex; flex-direction: column; gap: 3px;
}
.stat b { font-family: var(--num); font-size: 21px; font-weight: 600; }
.stat span { font-size: 12px; color: var(--muted); }

.hero {
  background: var(--brand); border-radius: 20px; padding: 16px 18px;
  display: flex; align-items: center; gap: 14px; color: #fff;
  border: none; width: 100%; text-align: left; cursor: pointer;
  margin-bottom: 12px; font-family: inherit;
}
.hero:active { background: var(--brand-dark); }
.hero .ic {
  width: 48px; height: 48px; border-radius: 15px; flex: none;
  background: rgba(255,255,255,.18);
  display: flex; align-items: center; justify-content: center;
}
.hero b { font-size: 17px; font-weight: 600; display: block; }
.hero span { font-size: 13px; color: rgba(255,255,255,.88); line-height: 1.4; }

/* ---------- ตัวกรอง ---------- */
.filters { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 12px; scrollbar-width: none; }
.filters::-webkit-scrollbar { display: none; }
.filters button {
  flex: none; font-family: inherit; font-size: 13px; cursor: pointer;
  padding: 8px 15px; border-radius: 16px;
  border: 1px solid var(--line); background: var(--card); color: var(--ink2);
}
.filters button[aria-pressed="true"] { background: var(--ink); color: #fff; border-color: var(--ink); font-weight: 500; }

/* ---------- ฟอร์ม ---------- */
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; }

input, textarea, select, button { font-family: inherit; font-size: 16px; color: var(--ink); }

input[type="text"], input[type="tel"], input[type="email"], input[type="date"], input[type="number"], textarea, select {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 12px;
  padding: 12px 13px;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
textarea { resize: none; line-height: 1.45; min-height: 46px; }
select { background-image: linear-gradient(45deg, transparent 50%, var(--faint) 50%), linear-gradient(135deg, var(--faint) 50%, transparent 50%); background-position: calc(100% - 18px) 21px, calc(100% - 13px) 21px; background-size: 5px 5px, 5px 5px; background-repeat: no-repeat; }

.btn {
  border: none; border-radius: 14px; padding: 14px 18px;
  font-size: 16px; font-weight: 600; cursor: pointer;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--brand); color: #fff; width: 100%;
}
.btn:active { background: var(--brand-dark); }
.btn[disabled] { opacity: .55; cursor: default; }
.btn.ghost { background: var(--card); color: var(--ink2); border: 1px solid var(--line); font-weight: 500; }
.btn.ghost:active { background: var(--line2); }
.btn.dark { background: var(--ink); }
.btn.danger { background: transparent; color: var(--danger); border: 1px solid var(--line); font-weight: 500; }
.btn.sm { padding: 10px 14px; font-size: 14px; width: auto; border-radius: 11px; }

.btnrow { display: flex; gap: 10px; }
.btnrow > * { flex: 1; }

/* ---------- แถวรายการในใบเสนอราคา ---------- */
.item { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 12px 13px; margin-bottom: 9px; }
.item.priced { border-left: 3px solid var(--brand); }
.item .head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.item .no {
  font-family: var(--num); font-size: 12px; font-weight: 600;
  min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px; flex: none;
  display: flex; align-items: center; justify-content: center;
  background: var(--brand); color: #fff;
}
.item .no.blank { background: var(--line2); color: var(--faint); }
.item .mini { border: none; background: transparent; color: var(--faint); font-size: 17px; width: 36px; height: 36px; border-radius: 10px; cursor: pointer; flex: none; }
.item .mini:active { background: var(--line2); color: var(--ink); }
.item .grid { display: grid; grid-template-columns: 1fr 1fr 1.2fr; gap: 8px; margin-top: 8px; }
.item .grid input { padding: 10px 11px; font-size: 15px; }
.item .amount { text-align: right; font-family: var(--num); font-size: 15px; font-weight: 600; margin-top: 8px; }
.item .amount small { color: var(--muted); font-weight: 400; font-size: 12px; margin-right: 6px; }

/* ---------- แถบล่างสรุปยอด ---------- */
.footbar {
  position: sticky; bottom: 0; z-index: 15;
  background: var(--card); border-top: 1px solid var(--line);
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  margin: 16px -16px 0;
}
.footbar .total { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 10px; }
.footbar .total b { font-family: var(--num); font-size: 23px; font-weight: 600; }

/* ---------- แถบนำทางล่าง ---------- */
nav.tabs {
  flex: none; z-index: 30;
  background: var(--card); border-top: 1px solid var(--line);
  display: flex; justify-content: space-around;
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
}
nav.tabs a {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px;
  color: var(--faint); text-decoration: none; font-size: 11px; padding: 4px 0;
}
nav.tabs a[aria-current="page"] { color: var(--ink); font-weight: 500; }

/* ---------- แผ่นเลื่อนขึ้น (คลังราคา) ---------- */
.sheet-back {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(20,20,45,.42);
  display: flex; align-items: flex-end;
}
.sheet {
  background: var(--card); width: 100%; max-height: 82vh;
  border-radius: 22px 22px 0 0;
  padding: 14px 16px calc(16px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 10px;
  animation: up .2s ease-out;
}
@keyframes up { from { transform: translateY(18px); opacity: .6 } to { transform: none; opacity: 1 } }
.sheet .handle { width: 40px; height: 4px; border-radius: 2px; background: #D9D9E3; align-self: center; flex: none; }
.sheet h2 { margin: 0; font-size: 17px; font-weight: 600; }
.sheet .list { overflow-y: auto; flex: 1 1 auto; min-height: 0; margin: 0 -16px; padding: 0 16px; }

.pick {
  width: 100%; text-align: left; font-family: inherit; cursor: pointer;
  background: transparent; border: none; border-bottom: 1px solid var(--line2);
  padding: 13px 2px; display: flex; align-items: center; gap: 12px;
}
.pick:active { background: var(--line2); }
.pick b { font-weight: 500; font-size: 15px; }

/* ---------- อื่น ๆ ---------- */
.empty { text-align: center; padding: 48px 24px; color: var(--muted); }
.empty .big { font-size: 40px; margin-bottom: 10px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + 18px);
  transform: translateX(-50%);
  background: var(--ink); color: #fff; font-size: 14px;
  padding: 11px 18px; border-radius: 12px; z-index: 90;
  max-width: calc(100% - 32px); text-align: center;
  animation: up .18s ease-out;
}

.busy { position: fixed; inset: 0; z-index: 80; background: rgba(255,255,255,.72); display: flex; align-items: center; justify-content: center; }
.spin { width: 34px; height: 34px; border: 3px solid var(--line); border-top-color: var(--brand); border-radius: 50%; animation: sp .7s linear infinite; }
@keyframes sp { to { transform: rotate(360deg) } }

.sec { font-size: 12px; color: var(--muted); margin: 20px 2px 8px; font-weight: 500; }

@media (min-width: 700px) {
  body { background: var(--bg); }
  .footbar { border-radius: 0 0 var(--r) var(--r); }
}

/* ---------- แถวที่ AI อ่านมาแล้วยังไม่แน่ใจ ---------- */
.item.check { border-color: var(--warn); border-width: 1.5px; }
.item.check .mini { color: var(--warn-ink); }

.warnbox {
  background: var(--warn-soft); border-radius: 12px;
  padding: 10px 13px; font-size: 13px; color: var(--warn-ink);
  display: flex; flex-direction: column; gap: 5px; line-height: 1.5;
}

/* ---------- ดูตัวอย่าง PDF ในหน้าเดียวกัน ---------- */
.pdfview {
  position: fixed; inset: 0; z-index: 70;
  background: #2A2A32; display: flex; flex-direction: column;
}
.pdfview header {
  flex: none; display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 10px 8px;
  color: #fff; font-size: 13px;
}
.pdfview header .iconbtn { color: #fff; }
.pdfview header .grow { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pdfview iframe { flex: 1 1 auto; min-height: 0; width: 100%; border: 0; background: #fff; }
.pdfview .bar {
  flex: none; display: flex; gap: 10px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--card);
}
.pdfview .bar > * { flex: 1; }

/* ---------- รูปประกอบ ---------- */
.photos {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 8px;
  margin-bottom: 4px;
}
.thumb {
  position: relative; aspect-ratio: 1;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--card); padding: 0; overflow: hidden; cursor: pointer;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb.on { border-color: var(--brand); border-width: 2px; }
.thumb .tag {
  position: absolute; left: 5px; bottom: 5px;
  font-size: 10px; font-weight: 600; color: #fff;
  background: var(--brand); padding: 2px 6px; border-radius: 7px;
}
.thumb.add {
  display: flex; align-items: center; justify-content: center;
  font-size: 28px; color: var(--faint);
  border-style: dashed; border-width: 1.5px;
}
.thumb.add:active { background: var(--line2); }

/* ---------- ตรวจยอดกับกระดาษ ---------- */
.verify {
  border-radius: 12px; padding: 11px 14px; margin: -4px 0 12px;
  font-size: 13.5px; line-height: 1.5; font-weight: 500;
}
.verify.ok { background: var(--ok-soft); color: var(--ok); }
.verify.bad { background: rgba(200, 55, 45, .1); color: var(--danger); border: 1px solid rgba(200, 55, 45, .35); }
.verify span { font-weight: 400; font-size: 12px; opacity: .85; font-family: var(--num); }
