:root {
  --bg: #f3f2ef; --surface: #ffffff; --surface2: #eceae6; --ink: #1c2130; --ink2: #5b6273; --line: #dedbd5;
  --accent: #2c3b5e; --accent-ink: #ffffff; --silver: #c9cdd6;
  --ok: #1b7a4b; --okbg: #e1f2e8; --warn: #9a5b00; --warnbg: #fdecc8; --bad: #b3261e; --badbg: #fbe4e1;
  --shadow: 0 1px 2px rgba(20, 24, 40, .06), 0 6px 18px rgba(20, 24, 40, .07);
  --r: 14px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151d; --surface: #1b202b; --surface2: #252b39; --ink: #eceef4; --ink2: #9ea6b8; --line: #2d3445;
    --accent: #a9bcf2; --accent-ink: #10131c; --silver: #5b6279;
    --ok: #5fd197; --okbg: #17362a; --warn: #f2b955; --warnbg: #3b2d10; --bad: #ff8f86; --badbg: #3d1c1a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 6px 18px rgba(0, 0, 0, .35);
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.5 "Sarabun", "Noto Sans Thai", "Noto Sans Thai UI", Thonburi, "Leelawadee UI", Tahoma, system-ui, sans-serif;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}
body.noscroll { overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
b { font-weight: 700; }
.hidden { display: none !important; }
.muted { color: var(--ink2); }
.hint { color: var(--ink2); font-size: 14px; margin: 0 0 12px; }
.err { color: var(--bad); font-size: 14px; margin: 6px 0; }
.errtxt { color: var(--bad); font-size: 13px; }

/* หัว */
.top {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px; background: var(--accent); color: var(--accent-ink);
}
@media (prefers-color-scheme: dark) { .top { background: var(--surface); color: var(--ink); border-bottom: 1px solid var(--line); } }
.logo { border-radius: 9px; display: block; }
.brand { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; flex: 1; }
.brand b { font-size: 18px; letter-spacing: .2px; }
.brand span { font-size: 12.5px; opacity: .8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who { display: flex; flex-direction: column; align-items: flex-end; font-size: 12.5px; line-height: 1.25; max-width: 38%; }
.who .mail { opacity: .85; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.role { font-weight: 700; padding: 0 8px; border-radius: 99px; background: rgba(255, 255, 255, .18); }
.role.owner { background: #e8d9a8; color: #4a3a05; }

main { max-width: 980px; margin: 0 auto; padding: 14px 14px 24px; }

/* แท็บล่าง */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: flex; background: var(--surface);
  border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  flex: 1; border: 0; background: none; padding: 8px 4px 7px; display: flex; flex-direction: column; align-items: center;
  gap: 0; position: relative; color: var(--ink2); cursor: pointer; min-height: 58px;
}
.tab .ti { font-size: 20px; line-height: 1.2; }
.tab .tt { font-size: 12.5px; font-weight: 600; }
.tab.on { color: var(--accent); }
.tab.on::before { content: ""; position: absolute; top: 0; left: 24%; right: 24%; height: 3px; border-radius: 0 0 3px 3px; background: var(--accent); }
.badge { position: absolute; top: 4px; left: calc(50% + 8px); min-width: 18px; padding: 0 5px; border-radius: 99px; background: var(--bad); color: #fff; font-size: 11px; font-weight: 700; line-height: 18px; text-align: center; }
@media (prefers-color-scheme: dark) { .badge { color: #1b0b0a; } }

/* ปุ่ม */
.btn {
  border: 1px solid transparent; border-radius: 12px; padding: 10px 16px; min-height: 44px; cursor: pointer; font-weight: 700;
  background: var(--surface2); color: var(--ink); text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.sm { min-height: 36px; padding: 5px 12px; font-size: 14px; }
.btn.big { width: 100%; min-height: 52px; font-size: 17px; }
.btn.danger { background: var(--bad); color: #fff; }
.btn.danger-soft { background: var(--badbg); color: var(--bad); }
.btn.ok-soft { background: var(--okbg); color: var(--ok); }
.btn.okbtn { background: var(--ok); color: #fff; }
.btn.warnbtn { background: var(--warn); color: #fff; }
@media (prefers-color-scheme: dark) { .btn.okbtn, .btn.warnbtn, .btn.danger { color: #101010; } .btn.okbtn { background: var(--ok); } .btn.warnbtn { background: var(--warn); } .btn.danger { background: var(--bad); } }
.danger-text { color: var(--bad) !important; }
.row { display: flex; gap: 8px; align-items: center; }
.row.end { justify-content: flex-end; margin-top: 12px; }
.row.center { justify-content: center; }
.row.wrap { flex-wrap: wrap; }
.more { width: 100%; margin-top: 12px; }

/* ช่องกรอก */
input[type=text], input[type=search], input[type=number], input[type=email], select, textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); padding: 10px 12px; min-height: 46px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.label { font-size: 13.5px; font-weight: 700; color: var(--ink2); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.search { margin-bottom: 10px; }

/* สรุป */
.sum { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 12px; }
.sum.six { grid-template-columns: repeat(3, 1fr); }
.tile {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 10px 8px; text-align: center;
  display: flex; flex-direction: column; gap: 0; cursor: pointer; box-shadow: var(--shadow); min-width: 0;
}
.tile.info { cursor: default; }
.tile.money .tv { font-size: 16px; line-height: 1.7; }
.tile .tv { font-size: 20px; font-weight: 800; line-height: 1.25; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile .tl { font-size: 12.5px; color: var(--ink2); }
.tile.on { outline: 2px solid var(--accent); }
.tile.warn .tv { color: var(--warn); }
.tile.bad .tv { color: var(--bad); }
@media (min-width: 700px) { .sum.six { grid-template-columns: repeat(6, 1fr); } }

.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 12px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: 0 0 auto; border: 1px solid var(--line); background: var(--surface); border-radius: 99px; padding: 6px 14px; min-height: 38px; cursor: pointer; font-weight: 600; font-size: 14.5px; }
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* การ์ดสินค้า */
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
@media (min-width: 560px) { .grid { grid-template-columns: repeat(3, 1fr); } }
@media (min-width: 860px) { .grid { grid-template-columns: repeat(4, 1fr); } }
.card {
  text-align: left; background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 0; overflow: hidden;
  cursor: pointer; display: flex; flex-direction: column; box-shadow: var(--shadow);
}
.card:active { transform: scale(.985); }
.card.out { opacity: .72; }
.cbody { padding: 8px 10px 10px; display: flex; flex-direction: column; gap: 2px; }
.ctop { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.ccode { font-size: 17px; }
.cname { font-size: 13.5px; color: var(--ink2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-height: 20px; }
.cprice { font-size: 13.5px; font-weight: 700; }
.thumb { position: relative; background: var(--surface2); overflow: hidden; aspect-ratio: 1; width: 100%; display: block; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb.ph {
  display: flex; align-items: center; justify-content: center; font-size: 34px; font-weight: 800; color: var(--silver);
  background: linear-gradient(135deg, var(--surface2), var(--surface) 55%, var(--surface2));
}
.thumb.small { width: 48px; height: 48px; border-radius: 10px; flex: 0 0 48px; }
.thumb.small.ph { font-size: 18px; }
.thumb.tiny { width: 36px; height: 36px; border-radius: 8px; flex: 0 0 36px; }
.thumb.tiny.ph { font-size: 14px; }
.thumb.big { width: min(100%, 320px); margin: 0 auto; border-radius: 16px; }
.pill { display: inline-block; font-size: 12.5px; font-weight: 800; padding: 1px 9px; border-radius: 99px; white-space: nowrap; }
.pill.ok { background: var(--okbg); color: var(--ok); }
.pill.low { background: var(--warnbg); color: var(--warn); }
.pill.out { background: var(--badbg); color: var(--bad); }
.empty { text-align: center; color: var(--ink2); padding: 36px 16px; grid-column: 1 / -1; }

.fab {
  position: fixed; right: 16px; bottom: calc(76px + env(safe-area-inset-bottom)); z-index: 25; width: 56px; height: 56px; border-radius: 50%;
  border: 0; background: var(--accent); color: var(--accent-ink); font-size: 28px; line-height: 1; box-shadow: 0 6px 18px rgba(0, 0, 0, .28); cursor: pointer;
}

/* ตัด / รับ */
.seg { display: flex; background: var(--surface2); border-radius: 14px; padding: 4px; gap: 4px; margin-bottom: 12px; }
.segb { flex: 1; border: 0; background: none; border-radius: 11px; padding: 10px 6px; font-weight: 700; cursor: pointer; min-height: 44px; color: var(--ink2); }
.segb.on { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.segb.on.out { color: var(--bad); }
.segb.on.in { color: var(--ok); }
.segb.on.adjust { color: var(--warn); }
.codewrap { position: relative; margin-bottom: 10px; }
.codein { font-size: 20px; font-weight: 700; min-height: 54px; letter-spacing: .5px; }
.sugg { position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 15; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); overflow: hidden; }
.sg { width: 100%; display: flex; align-items: center; gap: 10px; border: 0; border-bottom: 1px solid var(--line); background: none; padding: 8px 12px; cursor: pointer; text-align: left; }
.sg:last-child { border-bottom: 0; }
.sg .muted { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.cart { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.citem { display: flex; align-items: center; gap: 10px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 8px 10px; }
.citem.err { border-color: var(--bad); background: color-mix(in srgb, var(--badbg) 55%, var(--surface)); }
.cmain { flex: 1; min-width: 0; }
.cnm { font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msub { font-size: 13px; }
.stepper { display: inline-flex; align-items: center; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--surface); }
.stepper input { width: 52px; border: 0; border-radius: 0; text-align: center; padding: 6px 0; min-height: 44px; font-weight: 800; font-size: 17px; -moz-appearance: textfield; box-shadow: none !important; }
.stepper input::-webkit-outer-spin-button, .stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.sbtn { width: 42px; min-height: 44px; border: 0; background: var(--surface2); font-size: 22px; cursor: pointer; font-weight: 700; }
.x { border: 0; background: none; color: var(--ink2); font-size: 18px; width: 36px; height: 36px; border-radius: 50%; cursor: pointer; }
.x:hover { background: var(--surface2); }
.movefoot { position: sticky; bottom: calc(66px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 8px; background: var(--bg); padding: 8px 0 4px; }

/* ประวัติ */
.dayhead { font-weight: 800; font-size: 14px; color: var(--ink2); padding: 14px 2px 6px; }
.hrow { display: flex; align-items: center; gap: 10px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 8px 10px; margin-bottom: 6px; cursor: pointer; }
.mline { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.mlist .mline { padding: 8px 0; border-bottom: 1px solid var(--line); }
.mlist .mline:last-child { border-bottom: 0; }
.mdelta { min-width: 46px; text-align: center; font-weight: 800; font-size: 17px; padding: 2px 4px; border-radius: 10px; }
.mdelta.plus { color: var(--ok); background: var(--okbg); }
.mdelta.minus { color: var(--bad); background: var(--badbg); }
.mmain { flex: 1; min-width: 0; }
.mtop { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mnote { font-size: 13.5px; }
.hrow .mline { order: 2; }

/* หน้าต่างย่อย */
.back { position: fixed; inset: 0; z-index: 100; background: rgba(10, 12, 20, .55); display: flex; align-items: flex-end; justify-content: center; }
.sheet { width: 100%; max-width: 640px; max-height: 92vh; max-height: 92dvh; background: var(--bg); border-radius: 22px 22px 0 0; display: flex; flex-direction: column; box-shadow: 0 -10px 40px rgba(0, 0, 0, .3); animation: up .2s ease-out; }
@keyframes up { from { transform: translateY(24px); opacity: .6; } to { transform: none; opacity: 1; } }
@media (min-width: 700px) { .back { align-items: center; } .sheet { border-radius: 22px; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 8px 20px; }
.sheet-head h2 { margin: 0; font-size: 19px; }
.sheet-body { overflow-y: auto; padding: 4px 20px calc(20px + env(safe-area-inset-bottom)); -webkit-overflow-scrolling: touch; }
.confirm p { margin: 4px 0 8px; font-size: 17px; }
.pd-photo { display: flex; flex-direction: column; align-items: center; gap: 8px; margin-bottom: 10px; }
.pd-code { font-size: 22px; }
.pd-qtyline { display: flex; align-items: baseline; gap: 4px; flex-wrap: wrap; margin: 2px 0 12px; }
.bigqty { font-size: 40px; font-weight: 800; line-height: 1.1; }
.pd-quick { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 12px; margin-bottom: 14px; }
.profit { font-size: 14px; font-weight: 700; margin: -2px 0 8px; }
.profit.ok { color: var(--ok); } .profit.bad { color: var(--bad); }
.pd-hist { margin-top: 18px; }
.danger-zone { margin-top: 20px; border-top: 1px solid var(--line); padding-top: 8px; }

/* เจ้าของ */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 14px; margin-bottom: 12px; box-shadow: var(--shadow); }
.panel h3 { margin: 0 0 8px; font-size: 17px; }
.warnbox { border-color: var(--warn); background: var(--warnbg); }
.warnbox p { margin: 6px 0 0; font-size: 14px; }
.kv { display: flex; flex-direction: column; margin-bottom: 8px; }
.srow { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.srow:last-child { border-bottom: 0; }
.addstaff { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.addstaff input { flex: 1 1 140px; width: auto; }
.mapgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.preview { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); margin-bottom: 10px; }
.preview table { border-collapse: collapse; width: 100%; font-size: 13.5px; }
.preview th, .preview td { padding: 5px 8px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
.preview th { background: var(--surface2); }
.impsum { margin: 8px 0; }
.done { text-align: center; padding: 10px 0; }
.bigok { width: 64px; height: 64px; margin: 0 auto 6px; border-radius: 50%; background: var(--okbg); color: var(--ok); font-size: 36px; line-height: 64px; font-weight: 800; }

/* ข้อความเต็มจอ */
.msg { max-width: 420px; margin: 18vh auto 0; padding: 0 24px; text-align: center; }
.logo.big { width: 72px; height: 72px; margin: 0 auto 12px; border-radius: 18px; }
.msg h1 { font-size: 22px; margin: 6px 0; }
.msg p { color: var(--ink2); }

#toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px); z-index: 200;
  max-width: min(92vw, 520px); background: #232837; color: #fff; padding: 11px 18px; border-radius: 14px; font-weight: 600; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; text-align: center; box-shadow: 0 8px 24px rgba(0, 0, 0, .3);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.ok { background: #176a41; }
#toast.warn { background: #8a5200; }
#toast.bad { background: #a5271f; }

@media (min-width: 900px) {
  body { padding-bottom: 24px; }
  #app { display: flex; flex-direction: column; }
  .top { order: 0; }
  .tabs { order: 1; }
  main { order: 2; width: 100%; }
  .tabs { position: sticky; top: 54px; bottom: auto; border-top: 0; border-bottom: 1px solid var(--line); justify-content: center; padding-bottom: 0; z-index: 19; }
  .tab { flex: 0 0 150px; flex-direction: row; justify-content: center; gap: 8px; min-height: 48px; }
  .tab.on::before { top: auto; bottom: 0; border-radius: 3px 3px 0 0; left: 10%; right: 10%; }
  .badge { position: static; margin-left: 2px; }
  .fab { bottom: 24px; }
  #toast { bottom: 30px; }
  .movefoot { bottom: 0; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
