/* JP Garage — สมุดงานอู่: กระดาษอุ่น หมึกดำ เขียวตามใบเสร็จเดิม */
:root {
  --ink: #15140f;
  --ink-2: #4a473f;
  --muted: #7d776a;
  --paper: #ece8de;
  --card: #fbf9f4;
  --line: #d9d3c5;
  --line-soft: #e8e3d8;
  --accent: #1e7a4c;
  --accent-deep: #145535;
  --accent-wash: #e1eee6;
  --amber: #9a5207;
  --amber-wash: #f5e9d4;
  --red: #a82a22;
  --black: #121210;
  --series-2: #d9822b;
  --display: 'Chakra Petch', 'IBM Plex Sans Thai', sans-serif;
  --body: 'IBM Plex Sans Thai', system-ui, sans-serif;
  --tabbar: 62px;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 400 15px/1.5 var(--body);
  color: var(--ink);
  background: var(--paper);
  -webkit-font-smoothing: antialiased;
}
h1, h2, p, dl, dd, ol, ul, figure { margin: 0; padding: 0; }
ol, ul { list-style: none; }
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- โครงหน้า ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--black);
  padding: calc(env(safe-area-inset-top) + 8px) 16px 8px;
  border-bottom: 3px solid var(--accent);
}
.brand { display: flex; align-items: center; gap: 10px; max-width: 560px; margin: 0 auto; color: #fff; }
.brand img { border-radius: 4px; }
.brand-name { font: 700 18px/1 var(--display); letter-spacing: .08em; }
.brand-tag { margin-left: auto; font-size: 12px; color: #b9b4a6; }

#app {
  max-width: 560px; margin: 0 auto;
  padding: 16px 16px calc(var(--tabbar) + env(safe-area-inset-bottom) + 28px);
  display: flex; flex-direction: column; gap: 14px;
}

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a {
  height: var(--tabbar);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font-size: 11px; color: var(--muted);
}
.tabbar svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.tabbar a.active { color: var(--ink); font-weight: 600; }
.tabbar .tab-new svg { background: var(--accent); stroke: #fff; border-radius: 6px; padding: 3px; width: 28px; height: 28px; }
.tabbar .tab-new.active svg { background: var(--accent-deep); }

.page-title { font: 700 22px/1.2 var(--display); letter-spacing: .01em; }
.section {
  display: flex; align-items: baseline; gap: 8px;
  font: 600 13px/1 var(--display); letter-spacing: .08em; text-transform: uppercase; color: var(--ink-2);
  margin-top: 6px;
}
.section b { color: var(--ink); }
.section-amt { margin-left: auto; font-size: 15px; letter-spacing: 0; color: var(--ink); }
.muted { color: var(--muted); font-size: 13px; }
.pad { padding: 4px 2px; }
.back { font-size: 14px; color: var(--ink-2); padding: 4px 0; display: inline-block; }
.loading, .empty { text-align: center; color: var(--muted); padding: 48px 16px; }
.empty p { color: var(--ink); font-weight: 500; }
.empty span { font-size: 13px; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* ---------- ค้นหา ---------- */
.searchbox { position: relative; }
.searchbox svg {
  position: absolute; left: 14px; top: 50%; width: 20px; height: 20px; transform: translateY(-50%);
  fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round;
}
.searchbox input {
  width: 100%; height: 52px; padding: 0 14px 0 44px;
  border: 1.5px solid var(--ink); border-radius: 6px; background: var(--card);
  font-size: 16px; box-shadow: 3px 3px 0 var(--ink);
}
.searchbox input::placeholder { color: var(--muted); }
.searchbox input:focus { outline: none; box-shadow: 3px 3px 0 var(--accent); border-color: var(--accent); }

#results { display: flex; flex-direction: column; gap: 10px; }

/* ---------- แถวรายการ ---------- */
.list { display: flex; flex-direction: column; gap: 8px; }
.row {
  display: block; padding: 11px 12px;
  background: var(--card); border: 1px solid var(--line); border-left: 4px solid var(--ink-2); border-radius: 4px;
  animation: rise .28s ease both;
}
.list > :nth-child(2) { animation-delay: 30ms; }
.list > :nth-child(3) { animation-delay: 60ms; }
.list > :nth-child(4) { animation-delay: 90ms; }
.list > :nth-child(n + 5) { animation-delay: 120ms; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
@media (prefers-reduced-motion: reduce) { .row { animation: none; } }
.row:active { background: #f3efe5; }
.row-cust { border-left-color: var(--ink); }
.tone-paid { border-left-color: var(--accent); }
.tone-due { border-left-color: var(--amber); }
.tone-void { border-left-color: var(--line); opacity: .65; }
.row-top { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.row-date { margin-left: auto; font-size: 12px; color: var(--muted); }
.row-body { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; }
.row-body > div:first-child { min-width: 0; }
.row strong { display: block; font-weight: 600; overflow-wrap: anywhere; }
.row-sub { display: block; font-size: 13px; color: var(--ink-2); overflow-wrap: anywhere; }
.row-side { text-align: right; flex: none; }
.row-meta { display: block; font-size: 12px; color: var(--muted); }
.tel { display: block; font: 500 14px/1.4 var(--display); letter-spacing: .02em; }
.docno { font: 600 13px/1 var(--display); letter-spacing: .06em; color: var(--ink-2); }
.amt { font: 600 17px/1.2 var(--display); white-space: nowrap; }
.row-link strong { margin-bottom: 2px; }

.stamp {
  display: inline-block; padding: 1px 6px 0;
  font: 600 11px/1.5 var(--display); letter-spacing: .04em;
  border: 1.5px solid currentColor; border-radius: 3px; transform: rotate(-2deg);
}
.stamp-paid { color: var(--accent-deep); }
.stamp-due { color: var(--amber); }
.stamp-quote { color: var(--ink-2); }
.stamp-void { color: var(--red); }

.plate {
  display: inline-block; padding: 1px 8px;
  font: 600 13px/1.5 var(--display); letter-spacing: .04em;
  background: #fff; border: 1.5px solid var(--ink); border-radius: 4px;
}
.odo { display: inline-flex; align-items: center; gap: 1px; }
.odo b {
  width: 15px; text-align: center;
  font: 600 13px/20px var(--display);
  color: #f1eee6; background: var(--black); border-radius: 2px;
}
.odo b:last-of-type { background: #f1eee6; color: var(--black); box-shadow: inset 0 0 0 1px var(--black); }
.odo i { font-style: normal; font-size: 11px; color: var(--muted); margin-left: 4px; }

/* ---------- การ์ด / ฟอร์ม ---------- */
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: 6px;
  padding: 14px; display: flex; flex-direction: column; gap: 12px;
}
.card-title { font: 600 13px/1 var(--display); letter-spacing: .08em; text-transform: uppercase; color: var(--ink-2); }
.card-title small { font: 400 12px var(--body); letter-spacing: 0; text-transform: none; color: var(--muted); margin-left: 6px; }

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: 12px; color: var(--ink-2); }
.field input, .field textarea, .field select {
  width: 100%; min-height: 44px; padding: 9px 11px;
  background: #fff; border: 1px solid var(--line); border-radius: 4px; font-size: 16px;
}
.field textarea { resize: vertical; line-height: 1.45; }
.field input:focus, .field textarea:focus, .field select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-wash); }
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; font-weight: 500; }
.check input { width: 22px; height: 22px; accent-color: var(--accent); }
.hint { font-size: 12px; color: var(--muted); margin-top: 2px; }
.advanced summary { font-size: 13px; color: var(--ink-2); padding: 8px 0; cursor: pointer; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 46px; padding: 0 16px; border-radius: 5px; border: 1.5px solid transparent;
  font: 600 15px/1.2 var(--body); text-align: center; background: none;
}
.btn:disabled { opacity: .5; }
.btn-block { width: 100%; }
.btn-sm { min-height: 40px; padding: 0 12px; font-size: 13px; }
.btn-primary { background: var(--accent); color: #fff; box-shadow: 0 2px 0 var(--accent-deep); }
.btn-primary:active { transform: translateY(1px); box-shadow: 0 1px 0 var(--accent-deep); }
.btn-outline { border-color: var(--ink); background: var(--card); }
.btn-ghost { color: var(--ink-2); border-color: var(--line); }
.btn-danger { color: var(--red); }

.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: #dcd6c8; border-radius: 6px; padding: 3px; gap: 3px; }
.seg > * {
  min-height: 40px; display: flex; align-items: center; justify-content: center;
  border: 0; border-radius: 4px; background: none; font-weight: 500; color: var(--ink-2);
}
.seg > [aria-checked='true'], .seg > [aria-current='true'] { background: var(--black); color: #fff; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { min-height: 40px; padding: 0 14px; border-radius: 20px; border: 1.5px solid var(--line); background: #fff; font-weight: 500; }
.chip[aria-pressed='true'] { border-color: var(--accent); background: var(--accent-wash); color: var(--accent-deep); }

.picked { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.picked strong { display: block; font-size: 17px; }
.picked span { font: 500 14px var(--display); color: var(--ink-2); }
.match {
  display: block; width: 100%; text-align: left; margin-top: 6px; padding: 9px 11px;
  background: var(--accent-wash); border: 1px solid #bfd9c9; border-radius: 4px;
}
.match strong { display: block; }
.match span { font-size: 13px; color: var(--ink-2); }

/* ---------- รายการในฟอร์ม ---------- */
.docform { display: flex; flex-direction: column; gap: 14px; }
#items { display: flex; flex-direction: column; }
.item { padding: 10px 0; border-bottom: 1px dashed var(--line); display: flex; flex-direction: column; gap: 8px; }
.item:first-child { padding-top: 0; }
.item-top { display: flex; gap: 6px; }
.item input { min-height: 44px; padding: 8px 10px; background: #fff; border: 1px solid var(--line); border-radius: 4px; font-size: 16px; min-width: 0; }
.item input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-wash); }
.i-desc { flex: 1; }
.i-del { width: 44px; flex: none; border: 0; background: none; color: var(--muted); font-size: 16px; }
.item-nums { display: grid; grid-template-columns: auto 1fr auto 56px minmax(72px, auto); align-items: center; gap: 6px; }
.i-kind { min-height: 44px; padding: 0 10px; border: 1px solid var(--line); border-radius: 4px; background: #fff; font-size: 13px; font-weight: 500; }
.i-kind[data-kind='labor'] { background: var(--amber-wash); border-color: #e2c79b; color: var(--amber); }
.i-rate, .i-qty { text-align: right; width: 100%; }
.i-total { text-align: right; font: 600 15px var(--display); }

.sumbar {
  position: sticky; bottom: calc(var(--tabbar) + env(safe-area-inset-bottom) + 8px); z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 10px 10px 14px; background: var(--black); color: #fff; border-radius: 6px;
}
.sumbar span { display: block; font-size: 11px; color: #b9b4a6; }
.sumbar strong { font: 700 21px/1.1 var(--display); }
.sumbar .btn { min-width: 108px; }

/* ---------- dialog / toast ---------- */
.sheet { width: min(480px, calc(100vw - 24px)); border: 1.5px solid var(--ink); border-radius: 8px; padding: 18px; background: var(--card); }
.sheet::backdrop { background: rgba(18, 18, 16, .5); }
.sheet form { display: flex; flex-direction: column; gap: 12px; }
.sheet h2 { font: 700 18px var(--display); }
.sheet-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 4px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar) + env(safe-area-inset-bottom) + 76px); z-index: 50;
  transform: translate(-50%, 12px); opacity: 0; pointer-events: none;
  max-width: calc(100vw - 32px); padding: 10px 14px; border-radius: 5px;
  background: var(--black); color: #fff; font-size: 14px; text-align: center;
  transition: opacity .18s, transform .18s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast[data-tone='error'] { background: var(--red); }

/* ---------- ลูกค้า ---------- */
.cust-head { display: flex; flex-direction: column; gap: 8px; }
.cust-title { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.cust-title h1 { font: 700 28px/1.15 var(--display); overflow-wrap: anywhere; }
.tel-link { font: 600 19px var(--display); letter-spacing: .03em; color: var(--accent-deep); align-self: flex-start; }
.cust-note { font-size: 14px; color: var(--ink-2); white-space: pre-line; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); border: 1px solid var(--line); border-radius: 6px; background: var(--card); }
.stats > div { padding: 10px 12px; }
.stats > div + div { border-left: 1px solid var(--line); }
.stats dt { font-size: 12px; color: var(--ink-2); }
.stats dd { font: 600 19px/1.3 var(--body); }
.stats small { font-size: 12px; font-weight: 400; color: var(--muted); }
.is-due { color: var(--amber); }

.veh-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.veh-head h2 { font: 700 18px/1.2 var(--display); }
.veh-tags { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 6px; }
.veh-tags:empty { display: none; }
.timeline { border-left: 2px solid var(--line); margin-left: 5px; display: flex; flex-direction: column; }
.timeline li { position: relative; padding: 0 0 14px 16px; }
.timeline li:last-child { padding-bottom: 2px; }
.timeline li::before {
  content: ''; position: absolute; left: -6px; top: 6px; width: 10px; height: 10px;
  border-radius: 50%; background: var(--card); border: 2px solid var(--ink);
}
.timeline a { display: block; }
.tl-top { display: flex; align-items: center; gap: 8px; }
.tl-top time { font-weight: 600; }
.tl-top .amt { margin-left: auto; font-size: 15px; }
.tl-items { font-size: 14px; color: var(--ink-2); margin: 2px 0; overflow-wrap: anywhere; }
.tl-meta { display: flex; gap: 12px; font-size: 12px; color: var(--muted); }
.tl-meta .docno { font-size: 12px; color: var(--muted); }

.doc-nav { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; }

/* ---------- รายงาน ---------- */
.seg-link > * { min-height: 38px; font-size: 14px; }
.hero { display: flex; flex-direction: column; gap: 2px; }
.hero span { font-size: 13px; color: var(--ink-2); }
.hero strong { font: 600 48px/1.1 var(--body); letter-spacing: -.01em; }

.viz { position: relative; }
.cols {
  position: relative; display: grid; grid-template-columns: repeat(var(--n), minmax(0, 1fr));
  height: 210px; padding: 20px 0 0 38px;
}
.cols-grid { position: absolute; inset: 20px 0 20px 38px; }
.cols-grid span { position: absolute; left: 0; right: 0; border-top: 1px solid var(--line-soft); }
.cols-grid span:last-child { border-top-color: #c3bdae; }
.cols-grid i {
  position: absolute; right: 100%; top: -8px; margin-right: 6px;
  font: 400 11px/16px var(--body); font-style: normal; color: var(--muted); font-variant-numeric: tabular-nums;
}
.col { position: relative; display: grid; grid-template-rows: 1fr 20px; padding: 0 1px; border: 0; background: none; min-width: 0; }
.col-bar {
  position: relative; align-self: end; justify-self: center;
  width: min(24px, 100%); background: var(--accent); border-radius: 4px 4px 0 0;
}
.col.on .col-bar, .col:hover .col-bar { background: var(--accent-deep); }
.col-bar b {
  position: absolute; bottom: 100%; left: 50%; transform: translateX(-50%); margin-bottom: 2px;
  font: 600 11px/1.2 var(--body); color: var(--ink); white-space: nowrap;
}
.col-x { align-self: end; font-size: 11px; line-height: 16px; color: var(--muted); white-space: nowrap; justify-self: center; }
.viz-tip {
  position: absolute; top: -8px; transform: translateX(-50%); pointer-events: none; white-space: nowrap;
  padding: 4px 8px; border-radius: 4px; background: var(--black); color: #fff; font-size: 12px;
}
.viz-table summary { font-size: 13px; color: var(--ink-2); padding: 8px 0 2px; cursor: pointer; }
.viz-table table { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
.viz-table th, .viz-table td { padding: 5px 4px; border-bottom: 1px solid var(--line-soft); text-align: right; font-weight: 400; }
.viz-table th { color: var(--ink-2); }
.viz-table th:first-child, .viz-table td:first-child { text-align: left; }

.split { display: flex; gap: 2px; height: 20px; }
.split span:first-child { border-radius: 4px 0 0 4px; }
.split span:last-child { border-radius: 0 4px 4px 0; }
.split span:only-child { border-radius: 4px; }
.s-part { background: var(--accent); }
.s-labor { background: var(--series-2); }
.legend { display: flex; flex-direction: column; gap: 4px; margin-top: 10px; font-size: 14px; }
.legend li { display: flex; align-items: center; gap: 8px; }
.legend i { width: 10px; height: 10px; border-radius: 2px; flex: none; }
.legend b { margin-left: auto; font-weight: 600; }
.legend small { width: 38px; text-align: right; color: var(--muted); font-size: 13px; }

.ranked { display: flex; flex-direction: column; gap: 10px; }
.ranked li > * { display: grid; grid-template-columns: 1fr auto; gap: 3px 10px; align-items: baseline; }
.r-label { font-size: 14px; overflow-wrap: anywhere; min-width: 0; }
.r-label small { color: var(--muted); font-size: 12px; margin-left: 6px; }
.r-value { font-weight: 600; font-size: 14px; }
.r-track { grid-column: 1 / -1; height: 8px; }
.r-track span { display: block; height: 100%; background: var(--accent); border-radius: 0 4px 4px 0; }

/* ---------- นำเข้า ---------- */
#review { display: flex; flex-direction: column; gap: 14px; }
.filepick { cursor: pointer; }
.dropzone { border: 2px dashed var(--muted); transition: background .15s, border-color .15s; }
.dropzone.is-over { background: var(--accent-wash); border-color: var(--accent); }
.match { margin-top: 6px; font-size: 13px; }
.match small { color: var(--muted); margin-left: 4px; }
.match-old { color: var(--accent-deep); }
.match-new { color: var(--ink-2); }
.match-unsure { color: var(--amber); }
.match-skip { color: var(--muted); }
.pick-q { width: 100%; padding: 10px 12px; border: 1.5px solid var(--line); border-radius: 5px; background: #fff; font: inherit; font-size: 16px; }
.pick-list { max-height: 50vh; overflow-y: auto; min-height: 48px; }
.pick-row { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; width: 100%; text-align: left; font: inherit; color: inherit; cursor: pointer; }
.dropzone ul { padding-left: 18px; display: flex; flex-direction: column; gap: 4px; }
details.row > summary { list-style: none; cursor: pointer; }
details.row > summary::-webkit-details-marker { display: none; }
details.row .edit { margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line); display: flex; flex-direction: column; gap: 10px; }
.warn { margin-top: 6px; font-size: 12px; color: var(--amber); }
.warn-text { color: var(--amber); }
.raw { font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
progress { width: 100%; height: 10px; accent-color: var(--accent); }
.fails { font-size: 13px; color: var(--ink-2); display: flex; flex-direction: column; gap: 4px; }

@media (max-width: 360px) {
  .grid-2 { grid-template-columns: 1fr; }
  .hero strong { font-size: 40px; }
}

@media print {
  .no-print, .topbar, .tabbar, .toast { display: none !important; }
  body { background: #fff; }
  #app { max-width: none; padding: 0; display: block; }
}
