:root {
  --bg: #F3F5F8;
  --surface: #FFFFFF;
  --surface-2: #F8FAFC;
  --ink: #14202E;
  --muted: #5B6B7D;
  --line: #DCE2E9;
  --brand: #1F4E79;
  --brand-strong: #173B5C;
  --on-brand: #FFFFFF;
  --input: #FFFBEA;
  --input-line: #E5D48A;
  --good: #176B41;  --good-bg: #E2F3E9;
  --bad: #B3261E;   --bad-bg: #FBE4E1;
  --warn: #8A5A00;  --warn-bg: #FFF1CC;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 32, 46, .06), 0 4px 14px rgba(20, 32, 46, .05);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0E151E;
    --surface: #17212D;
    --surface-2: #1C2835;
    --ink: #E7EDF4;
    --muted: #9BABBD;
    --line: #2B3948;
    --brand: #1F4E79;
    --brand-strong: #2A679E;
    --input: #26271C;
    --input-line: #5A5530;
    --good: #7AD3A2;  --good-bg: #153426;
    --bad: #FF9A8F;   --bad-bg: #40201D;
    --warn: #F2C56B;  --warn-bg: #3A2E12;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Sukhumvit Set", "Thonburi", "Leelawadee UI", "Noto Sans Thai", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
}
button, input, textarea { font: inherit; color: inherit; }
a { color: inherit; text-decoration: none; }
[hidden] { display: none !important; }

/* ---------- แถบบน ---------- */
.bar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 4px;
  padding: calc(env(safe-area-inset-top) + 8px) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  background: var(--brand); color: var(--on-brand);
}
.bar h1 { flex: 1; margin: 0 4px; font-size: 18px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  display: inline-grid; place-items: center; width: 44px; height: 44px; flex: none;
  border: 0; border-radius: 12px; background: transparent; color: inherit; cursor: pointer;
}
.icon-btn:active { background: rgba(255, 255, 255, .16); }
.sync {
  display: inline-flex; align-items: center; gap: 7px; height: 34px; padding: 0 12px; flex: none;
  border: 1px solid rgba(255, 255, 255, .3); border-radius: 999px; background: rgba(255, 255, 255, .1);
  color: inherit; font-size: 13px; cursor: pointer; white-space: nowrap;
}
.sync .dot { width: 8px; height: 8px; border-radius: 50%; background: #B8C4D1; }
.sync[data-state="idle"] .dot { background: #6FE0A0; }
.sync[data-state="syncing"] .dot { background: #FFD666; animation: pulse 1s infinite; }
.sync[data-state="pending"] .dot { background: #FFD666; }
.sync[data-state="error"] .dot { background: #FF8A80; }
@keyframes pulse { 50% { opacity: .3; } }

main { max-width: 760px; margin: 0 auto; padding: 14px max(14px, env(safe-area-inset-right)) 120px max(14px, env(safe-area-inset-left)); outline: none; }

/* ---------- ชิ้นส่วนร่วม ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; margin-bottom: 12px; }
.card h2 { margin: 0 0 2px; font-size: 15px; font-weight: 700; }
.card .hint { margin: 0 0 10px; font-size: 13px; color: var(--muted); }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.grid .wide { grid-column: 1 / -1; }
@media (min-width: 560px) { .grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); } }

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: 13px; color: var(--muted); line-height: 1.3; }
.inp { position: relative; display: flex; align-items: center; }
.inp input, .field textarea {
  width: 100%; min-width: 0; height: 46px; padding: 0 12px;
  border: 1px solid var(--input-line); border-radius: 10px; background: var(--input);
  font-size: 16px; font-variant-numeric: tabular-nums; outline: none;
}
.field textarea { height: auto; min-height: 70px; padding: 10px 12px; resize: vertical; }
.inp input:focus, .field textarea:focus { border-color: var(--brand-strong); box-shadow: 0 0 0 3px rgba(42, 103, 158, .25); }
.inp input.text { font-variant-numeric: normal; }
.inp .unit { position: absolute; right: 12px; font-size: 13px; color: var(--muted); pointer-events: none; }
.inp.has-unit input { padding-right: 52px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 46px; padding: 0 16px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--surface); cursor: pointer; font-weight: 600; white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--brand-strong); border-color: var(--brand-strong); color: #fff; }
.btn.danger { color: var(--bad); }
.btn.small { min-height: 34px; padding: 0 10px; font-size: 13px; border-radius: 9px; }
.btn[disabled] { opacity: .5; cursor: default; }
.row-btns { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }

.chip { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; background: var(--surface-2); color: var(--muted); border: 1px solid var(--line); }
.chip.good { background: var(--good-bg); color: var(--good); border-color: transparent; }
.chip.bad { background: var(--bad-bg); color: var(--bad); border-color: transparent; }
.chip.warn { background: var(--warn-bg); color: var(--warn); border-color: transparent; }

/* ---------- หน้ารายการ ---------- */
.search { margin-bottom: 12px; }
.search input { background: var(--surface); border-color: var(--line); }
.summary { display: flex; gap: 8px; flex-wrap: wrap; margin: 0 2px 10px; font-size: 13px; color: var(--muted); }
.job {
  display: flex; align-items: center; gap: 12px; padding: 13px 14px; margin-bottom: 10px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
}
.job:active { background: var(--surface-2); }
.job-main { flex: 1; min-width: 0; }
.job-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.job-meta { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.job-side { text-align: right; flex: none; display: flex; flex-direction: column; align-items: flex-end; gap: 3px; }
.job-price { font-weight: 700; font-variant-numeric: tabular-nums; }
.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty strong { display: block; color: var(--ink); font-size: 17px; margin-bottom: 4px; }
.fab {
  position: fixed; right: max(16px, env(safe-area-inset-right)); bottom: calc(18px + env(safe-area-inset-bottom)); z-index: 15;
  height: 54px; padding: 0 20px; border-radius: 999px; border: 0; background: var(--brand-strong); color: #fff;
  font-weight: 700; box-shadow: 0 6px 18px rgba(20, 32, 46, .3); cursor: pointer; display: inline-flex; align-items: center; gap: 8px;
}

/* ---------- หน้างาน ---------- */
.lines { margin: 0; padding: 0; list-style: none; }
.lines li { display: flex; justify-content: space-between; gap: 12px; padding: 6px 0; border-bottom: 1px dashed var(--line); font-size: 15px; }
.lines li:last-child, .lines li:has(+ .total) { border-bottom: 0; }
.lines li span:first-child { color: var(--muted); }
.lines li span:last-child { font-variant-numeric: tabular-nums; white-space: nowrap; }
.lines li.total { border-top: 2px solid var(--line); border-bottom: 0; margin-top: 4px; padding-top: 9px; font-weight: 700; font-size: 16px; }
.lines li.total span:first-child { color: var(--ink); }

.price-opt { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px dashed var(--line); }
.price-opt:last-child { border-bottom: 0; padding-bottom: 0; }
.price-opt .what { flex: 1; min-width: 0; }
.price-opt .what b { display: block; font-size: 15px; }
.price-opt .what small { color: var(--muted); font-size: 12.5px; line-height: 1.35; display: block; }
.price-opt .amt { font-weight: 700; font-size: 17px; font-variant-numeric: tabular-nums; white-space: nowrap; }

.verdict { border-radius: 12px; padding: 12px 14px; margin-top: 12px; background: var(--surface-2); border: 1px solid var(--line); }
.verdict.good { background: var(--good-bg); border-color: transparent; }
.verdict.bad { background: var(--bad-bg); border-color: transparent; }
.verdict.warn { background: var(--warn-bg); border-color: transparent; }
.verdict .head { font-weight: 700; }
.verdict.good .head { color: var(--good); } .verdict.bad .head { color: var(--bad); } .verdict.warn .head { color: var(--warn); }
.verdict .big { font-size: 24px; font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1.25; }
.verdict .sub { display: flex; flex-wrap: wrap; gap: 4px 16px; font-size: 13.5px; margin-top: 4px; font-variant-numeric: tabular-nums; }

.actions {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
  background: var(--surface); border-top: 1px solid var(--line);
  padding: 10px max(14px, env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
}
.actions .inner { max-width: 732px; margin: 0 auto; display: flex; align-items: center; gap: 8px; }
.actions .mini { flex: 1; min-width: 0; font-size: 13px; color: var(--muted); line-height: 1.3; }
.actions .mini b { display: block; color: var(--ink); font-size: 15px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- ตั้งค่า ---------- */
.note { font-size: 13px; color: var(--muted); margin: 10px 0 0; }
.msg { font-size: 14px; margin: 10px 0 0; padding: 9px 12px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--line); }
.msg.good { background: var(--good-bg); color: var(--good); border-color: transparent; }
.msg.bad { background: var(--bad-bg); color: var(--bad); border-color: transparent; }

#toast {
  position: fixed; left: 50%; bottom: calc(92px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px); z-index: 30;
  max-width: min(92vw, 420px); padding: 10px 16px; border-radius: 12px; background: #14202E; color: #fff; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* ---------- แถบเตือนและส่วนขั้นสูง ---------- */
.notice { display: block; padding: 12px 14px; margin-bottom: 12px; border-radius: var(--radius); background: var(--warn-bg); color: var(--warn); }
.notice b { display: block; }
.notice span { font-size: 13px; }
details.adv { border-top: 1px dashed var(--line); padding-top: 8px; }
details.adv summary { cursor: pointer; font-size: 13px; color: var(--muted); padding: 4px 0; }
details.adv .field { margin-top: 8px; }
