/* Layout: แถบบนบาง ๆ + พื้นที่ทำงานสองคอลัมน์ (รายการสินค้า | ตะกร้าใบเบิก) ยุบเป็นคอลัมน์เดียว + แถบล่างบนมือถือ */
:root {
  --bg: #f4f6f2;
  --surface: #ffffff;
  --surface-2: #eef2ec;
  --ink: #18221d;
  --muted: #5b6a61;
  --line: #dbe2da;
  --basil: #1d6a4d;
  --basil-ink: #ffffff;
  --basil-soft: #e1efe7;
  --tomato: #cf432a;
  --tomato-soft: #fbe6e1;
  --ok: #2d7a48;
  --ok-soft: #e2f1e6;
  --warn: #9a6512;
  --warn-soft: #f8edd8;
  --bad: #b3372b;
  --shadow: 0 1px 2px rgb(24 34 29 / 6%), 0 8px 24px rgb(24 34 29 / 6%);

  --f-display: "Mitr", "IBM Plex Sans Thai", "Noto Sans Thai", system-ui, sans-serif;
  --f-body: "IBM Plex Sans Thai", "Noto Sans Thai", "Leelawadee UI", system-ui, sans-serif;
  --f-mono: "IBM Plex Mono", ui-monospace, "SFMono-Regular", Menlo, monospace;

  --r: 10px;
  --gutter-l: max(16px, env(safe-area-inset-left, 0px));
  --gutter-r: max(16px, env(safe-area-inset-right, 0px));
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111613; --surface: #19201c; --surface-2: #212a25; --ink: #e4ebe6; --muted: #98a79e;
    --line: #2d3832; --basil: #4cb185; --basil-ink: #0c1a13; --basil-soft: #1d3329; --tomato: #f0694f;
    --tomato-soft: #3a211b; --ok: #5cc07f; --ok-soft: #1c3324; --warn: #e0a64a; --warn-soft: #362a16;
    --bad: #f07a6c; --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 8px 24px rgb(0 0 0 / 25%);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111613; --surface: #19201c; --surface-2: #212a25; --ink: #e4ebe6; --muted: #98a79e;
  --line: #2d3832; --basil: #4cb185; --basil-ink: #0c1a13; --basil-soft: #1d3329; --tomato: #f0694f;
  --tomato-soft: #3a211b; --ok: #5cc07f; --ok-soft: #1c3324; --warn: #e0a64a; --warn-soft: #362a16;
  --bad: #f07a6c; --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 8px 24px rgb(0 0 0 / 25%);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { min-height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.55 var(--f-body);
  -webkit-font-smoothing: antialiased;
}
body::before {
  content: ""; position: fixed; inset: 0 0 auto; height: env(safe-area-inset-top, 0px);
  z-index: 30; background: var(--surface);
}
h1, h2, h3 { font-family: var(--f-display); font-weight: 500; line-height: 1.25; margin: 0; text-wrap: balance; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }
p { margin: 0; }
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--basil); outline-offset: 2px; }
.mono { font-family: var(--f-mono); font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; }
.label {
  font-size: .72rem; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); font-weight: 600;
}

/* ---------- ปุ่ม / ช่องกรอก ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 40px; padding: 0 16px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; font-weight: 500; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--basil); border-color: var(--basil); color: var(--basil-ink); }
.btn-primary:hover { background: var(--basil); filter: brightness(1.08); }
.btn-cta { background: var(--tomato); border-color: var(--tomato); color: #fff; }
.btn-cta:hover { background: var(--tomato); filter: brightness(1.06); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-sm { min-height: 32px; padding: 0 10px; font-size: .9rem; }
.btn-block { width: 100%; }

.field { display: grid; gap: 6px; }
.field > span { font-size: .9rem; font-weight: 500; }
.field small { color: var(--muted); font-size: .82rem; }
.input {
  width: 100%; min-height: 42px; padding: 8px 12px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface);
}
.input:focus { border-color: var(--basil); outline: none; box-shadow: 0 0 0 3px var(--basil-soft); }
.input.code { font-family: var(--f-mono); text-transform: uppercase; letter-spacing: .12em; }
.err { color: var(--bad); font-size: .9rem; }

/* ---------- ป้ายรหัสร้าน: [51|SUM] ---------- */
.shopcode {
  display: inline-flex; align-items: stretch; border-radius: 6px; overflow: hidden;
  font-family: var(--f-mono); font-size: .82rem; font-weight: 600; border: 1px solid var(--basil);
}
.shopcode b { background: var(--basil); color: var(--basil-ink); padding: 1px 7px; font-weight: 600; }
.shopcode i { font-style: normal; padding: 1px 7px; color: var(--basil); letter-spacing: .08em; }

.pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px; border-radius: 99px;
  font-size: .8rem; font-weight: 600; white-space: nowrap;
}
.pill::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.pill-ok { background: var(--ok-soft); color: var(--ok); }
.pill-wait { background: var(--warn-soft); color: var(--warn); }
.pill-off { background: var(--surface-2); color: var(--muted); }

/* ---------- แถบตัวอย่าง (โหมดเดโม) ---------- */
.demo-bar {
  background: var(--warn-soft); color: var(--warn); font-size: .85rem;
  padding-block: 6px; padding-left: var(--gutter-l); padding-right: var(--gutter-r);
  display: flex; gap: 6px 16px; flex-wrap: wrap; justify-content: center;
}
.demo-bar b { font-family: var(--f-mono); }

/* ---------- ล็อกอิน ---------- */
.login {
  min-height: calc(100vh - 40px); display: grid; place-items: center;
  padding-block: 32px; padding-left: var(--gutter-l); padding-right: var(--gutter-r);
}
.login-card {
  width: min(880px, 100%); display: grid; grid-template-columns: 1fr 1fr; background: var(--surface);
  border-radius: 16px; overflow: hidden; box-shadow: var(--shadow); border: 1px solid var(--line);
}
.login-brand {
  background: var(--basil); color: var(--basil-ink); padding: 40px 36px; display: flex; flex-direction: column;
  justify-content: space-between; gap: 32px; position: relative; overflow: hidden;
}
.login-brand h1 { font-size: 2rem; }
.login-brand p { opacity: .85; max-width: 30ch; }
.login-brand .slip {
  font-family: var(--f-mono); font-size: .8rem; background: rgb(255 255 255 / 12%); border-radius: 8px;
  padding: 12px 14px; display: grid; gap: 4px; max-width: 280px;
}
.login-brand .slip div { display: flex; justify-content: space-between; gap: 12px; }
.login-form { padding: 36px; display: grid; gap: 18px; align-content: start; }
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 10px; }
.tabs button {
  flex: 1; border: 0; background: transparent; padding: 8px 10px; border-radius: 7px; cursor: pointer; font-weight: 500;
  color: var(--muted);
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
@media (max-width: 720px) {
  .login-card { grid-template-columns: 1fr; }
  .login-brand { padding: 28px 24px; }
  .login-brand .slip { display: none; }
  .login-form { padding: 24px; }
}

/* ---------- โครงแอป ---------- */
.topbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 20; background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.topbar-in {
  max-width: 1280px; margin: 0 auto; display: flex; align-items: center; gap: 12px 20px; flex-wrap: wrap;
  padding-block: 10px; padding-left: var(--gutter-l); padding-right: var(--gutter-r);
}
.brand { font-family: var(--f-display); font-size: 1.15rem; color: var(--basil); display: flex; align-items: center; gap: 8px; }
.brand svg { width: 26px; height: 26px; }
.who { display: flex; align-items: center; gap: 10px; min-width: 0; }
.who .name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav { display: flex; gap: 4px; margin-left: auto; flex-wrap: wrap; }
.nav a {
  text-decoration: none; color: var(--muted); padding: 7px 12px; border-radius: 8px; font-weight: 500;
}
.nav a:hover { background: var(--surface-2); color: var(--ink); }
.nav a[aria-current="page"] { background: var(--basil-soft); color: var(--basil); }

.page {
  max-width: 1280px; margin: 0 auto;
  padding-block: 24px 96px; padding-left: var(--gutter-l); padding-right: var(--gutter-r);
  display: grid; gap: 20px;
}
.page-head { display: flex; align-items: end; justify-content: space-between; gap: 12px 24px; flex-wrap: wrap; }
.page-head p { color: var(--muted); }

/* ---------- หน้าเบิกของ ---------- */
.order-layout { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 20px; align-items: start; }
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.toolbar .input { flex: 1 1 240px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line); background: var(--surface); padding: 5px 12px; border-radius: 99px;
  cursor: pointer; font-size: .88rem; color: var(--muted);
}
.chip[aria-pressed="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.chip .n { font-family: var(--f-mono); font-size: .78rem; opacity: .7; margin-left: 4px; }

.items { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); overflow: hidden; }
.cat-head {
  padding: 8px 16px; background: var(--surface-2); display: flex; justify-content: space-between; gap: 12px;
  border-top: 1px solid var(--line);
}
.cat-head:first-child { border-top: 0; }
.cat-head .split { color: var(--tomato); font-size: .78rem; font-weight: 600; }
.row {
  display: grid; grid-template-columns: 80px minmax(0, 1fr) auto; gap: 4px 14px; align-items: center;
  padding: 10px 16px; border-top: 1px solid var(--line);
}
.cat-head + .row { border-top: 0; }
.row.has-qty { background: var(--basil-soft); }
.row .code { font-family: var(--f-mono); font-size: .82rem; color: var(--muted); }
.row .nm { font-weight: 500; min-width: 0; }
.row .meta { font-size: .82rem; color: var(--muted); display: flex; gap: 10px; flex-wrap: wrap; }
.tag { font-size: .72rem; padding: 0 6px; border-radius: 4px; background: var(--warn-soft); color: var(--warn); font-weight: 600; }
.stepper { display: inline-flex; align-items: center; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.stepper button {
  width: 36px; height: 36px; border: 0; background: transparent; cursor: pointer; font-size: 1.1rem; color: var(--basil);
}
.stepper button:hover { background: var(--surface-2); }
.stepper input {
  width: 64px; height: 36px; border: 0; border-inline: 1px solid var(--line); text-align: center;
  font-family: var(--f-mono); background: transparent;
}
.stepper input:focus { outline: none; background: var(--basil-soft); }
.stepper .u { padding: 0 10px 0 6px; font-size: .82rem; color: var(--muted); min-width: 44px; }
.empty { padding: 40px 16px; text-align: center; color: var(--muted); }

/* ตะกร้าใบเบิก — กระดาษใบเบิก */
.cart {
  position: sticky; top: calc(env(safe-area-inset-top, 0px) + 76px); background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--r); box-shadow: var(--shadow); display: grid;
}
.cart-head { padding: 16px 18px 12px; border-bottom: 1px dashed var(--line); display: grid; gap: 10px; }
.cart-head .docline { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.cart-body { max-height: 46vh; overflow: auto; padding: 6px 0; }
.cart-line { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 8px; padding: 6px 18px; align-items: center; }
.cart-line .q { font-family: var(--f-mono); }
.cart-line button { border: 0; background: transparent; color: var(--muted); cursor: pointer; padding: 4px; border-radius: 6px; }
.cart-line button:hover { color: var(--bad); background: var(--tomato-soft); }
.cart-group { padding: 8px 18px 2px; font-size: .78rem; font-weight: 600; color: var(--tomato); }
.cart-foot { padding: 14px 18px 18px; border-top: 1px dashed var(--line); display: grid; gap: 12px; }
.cart-sum { display: flex; justify-content: space-between; }

.mobile-bar { display: none; }
@media (max-width: 960px) {
  .order-layout { grid-template-columns: 1fr; }
  .cart { display: none; }
  .cart.open {
    display: grid; position: fixed; inset: auto 0 0 0; top: auto; z-index: 40; border-radius: 16px 16px 0 0;
    max-height: 85vh; padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  .mobile-bar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 25; background: var(--surface);
    border-top: 1px solid var(--line); gap: 12px; align-items: center; justify-content: space-between;
    padding-block: 10px calc(10px + env(safe-area-inset-bottom, 0px));
    padding-left: var(--gutter-l); padding-right: var(--gutter-r);
  }
  .row { grid-template-columns: minmax(0, 1fr) auto; }
  .row .code { grid-column: 1 / -1; }
}
.count-badge {
  display: inline-grid; place-items: center; min-width: 22px; height: 22px; padding: 0 6px; border-radius: 99px;
  background: var(--tomato); color: #fff; font-family: var(--f-mono); font-size: .78rem;
}

/* ---------- ตาราง ---------- */
.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 16px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { font-size: .78rem; letter-spacing: .06em; color: var(--muted); font-weight: 600; background: var(--surface-2); }
tr:last-child td { border-bottom: 0; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover td { background: var(--surface-2); }
td.wrap { white-space: normal; min-width: 200px; }
.r { text-align: right; }

.stats { display: flex; gap: 10px 28px; flex-wrap: wrap; }
.stat > b { font-family: var(--f-display); font-weight: 500; font-size: 1.4rem; display: block; }
.stat > span { color: var(--muted); font-size: .85rem; }

/* ---------- หน้าต่าง / แจ้งเตือน ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 50; background: rgb(10 16 13 / 45%); display: grid; place-items: center;
  padding-left: var(--gutter-l); padding-right: var(--gutter-r); padding-block: 24px;
}
.modal {
  width: min(560px, 100%); max-height: calc(100vh - 48px); overflow: auto; background: var(--surface);
  border-radius: 14px; box-shadow: var(--shadow); border: 1px solid var(--line);
}
.modal.wide { width: min(880px, 100%); }
.modal.shake { animation: shake .4s ease; }
@keyframes shake {
  20%, 60% { transform: translateX(-8px); }
  40%, 80% { transform: translateX(8px); }
}
@media (prefers-reduced-motion: reduce) { .modal.shake { animation: none; outline: 2px solid var(--bad); } }
.modal-head { padding: 18px 20px 12px; display: flex; justify-content: space-between; gap: 12px; align-items: start; }
.modal-body { padding: 4px 20px 16px; display: grid; gap: 14px; }
.modal-foot { padding: 14px 20px 18px; display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; border-top: 1px solid var(--line); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 520px) { .grid2 { grid-template-columns: 1fr; } }
.switch { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.switch input { width: 18px; height: 18px; accent-color: var(--basil); }
.preview-code {
  display: flex; align-items: center; gap: 10px; padding: 12px; border-radius: 8px; background: var(--surface-2); flex-wrap: wrap;
}
.result-doc { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px; border-radius: 8px; border: 1px solid var(--line); }
.result-doc .docno { font-family: var(--f-mono); font-size: 1.2rem; font-weight: 600; }
.result-doc.bad { border-color: var(--bad); }

.toasts {
  position: fixed; z-index: 60; left: 50%; transform: translateX(-50%);
  top: calc(env(safe-area-inset-top, 0px) + 12px); display: grid; gap: 8px; width: min(420px, calc(100% - 32px));
}
.toast { padding: 10px 14px; border-radius: 10px; background: var(--ink); color: var(--bg); box-shadow: var(--shadow); font-size: .92rem; }
.toast.bad { background: var(--bad); color: #fff; }
.spinner { width: 18px; height: 18px; border-radius: 50%; border: 2px solid currentColor; border-right-color: transparent; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation: none; } }

/* ---------- ตั้งรายการสินค้าที่ร้านเบิกได้ ---------- */
.pick-filters .input { flex: 1 1 160px; width: auto; }
.pick-filters #pick-search { flex: 2 1 240px; }
.pick-list { border: 1px solid var(--line); border-radius: var(--r); max-height: 52vh; overflow: auto; }
.pick-head {
  position: sticky; top: 0; z-index: 1; display: flex; align-items: center; gap: 10px; padding: 7px 14px;
  background: var(--surface-2); border-top: 1px solid var(--line); cursor: pointer;
}
.pick-head:first-child { border-top: 0; }
.pick-head .n { margin-left: auto; font-family: var(--f-mono); font-size: .8rem; }
.pick-row {
  display: grid; grid-template-columns: 18px 72px minmax(0, 1fr) auto; gap: 10px; align-items: center;
  padding: 7px 14px; border-top: 1px solid var(--line); cursor: pointer;
}
.pick-row.on { background: var(--basil-soft); }
.pick-row .nm { min-width: 0; }
.pick-head input, .pick-row input { width: 16px; height: 16px; accent-color: var(--basil); margin: 0; }
.tag-branch { margin-left: 8px; background: var(--surface-2); color: var(--muted); }
.import-box { display: grid; gap: 8px; padding: 12px 14px; border-radius: 8px; background: var(--warn-soft); }
@media (max-width: 520px) {
  .pick-row { grid-template-columns: 18px minmax(0, 1fr) auto; }
  .pick-row > .mono { display: none; }
}
