/* ระบบสต๊อกร้าน — ธีมหลัก */
:root {
  --bg: #f4f6f4;
  --surface: #ffffff;
  --ink: #1b2420;
  --muted: #68756e;
  --line: #dde4df;
  --brand: #1a7a4c;
  --brand-dark: #136038;
  --brand-soft: #e3f2ea;
  --danger: #c23b3b;
  --warn: #b97f14;
  --ok: #1a7a4c;
  --radius: 16px;
  --shadow: 0 3px 14px rgba(20, 40, 30, .09);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: 'Noto Sans Thai', 'Sukhumvit Set', system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 16.5px;
  line-height: 1.6;
}
img { max-width: 100%; }
[hidden] { display: none !important; }
h1, h2, h3 { text-wrap: balance; margin: 0 0 8px; }
h3 { font-size: 16px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.error { color: var(--danger); background: #fdeeee; border-radius: 8px; padding: 8px 12px; font-size: 13.5px; margin: 8px 0; }
.empty { text-align: center; color: var(--muted); padding: 32px 16px; }
.empty.small { padding: 12px; font-size: 13px; }

/* ---------- ปุ่ม ---------- */
.btn {
  font: inherit; font-weight: 700; cursor: pointer;
  border: 1.5px solid var(--line); background: var(--surface); color: var(--ink);
  border-radius: 14px; padding: 12px 18px; min-height: 48px;
  transition: filter .12s, transform .05s;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { background: #fdeeee; border-color: #f1c7c7; color: var(--danger); }
.btn.ghost { background: transparent; }
.btn.small { padding: 8px 14px; font-size: 14.5px; min-height: 40px; }
.btn.big { padding: 15px 20px; font-size: 18px; width: 100%; border-radius: 16px; }
.btn.left { margin-right: auto; }

/* ---------- ฟอร์ม ---------- */
input, select, textarea {
  font: inherit; color: var(--ink); font-size: 16px;
  border: 1.5px solid var(--line); border-radius: 13px;
  padding: 12px 14px; background: #fff; width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--brand); outline-offset: 1px; border-color: var(--brand); }
label { display: block; font-weight: 600; font-size: 13.5px; margin: 10px 0 0; }
label input, label select { margin-top: 4px; font-weight: 400; }
.form-row { display: flex; gap: 10px; }
.form-row label { flex: 1; }

/* ---------- ล็อกอิน ---------- */
.login-wrap { min-height: 100svh; display: grid; place-items: center; padding: 20px 16px; background: linear-gradient(160deg, var(--brand-soft), var(--bg) 60%); }
.login-card { background: var(--surface); border-radius: 18px; box-shadow: var(--shadow); padding: 30px 26px; width: 100%; max-width: 380px; }
.login-card h1 { font-size: 22px; }
.login-logo { font-size: 40px; }
.login-card .btn { margin-top: 18px; }

/* ---------- โครงหลัก ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: var(--brand); color: #fff; padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
}
.shop-name { font-weight: 800; font-size: 17px; }
.top-user { display: flex; align-items: center; gap: 8px; font-size: 13.5px; }
.avatar-btn { width: 34px; height: 34px; border-radius: 50%; border: 2px solid rgba(255,255,255,.6); padding: 0; overflow: hidden; background: rgba(255,255,255,.2); cursor: pointer; display: grid; place-items: center; font-size: 16px; color: #fff; flex: none; }
.avatar-btn img { width: 100%; height: 100%; object-fit: cover; }
.user-row .u-avatar { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; background: var(--brand-soft); display: grid; place-items: center; flex: none; }
.topbar .btn.ghost { color: #fff; border-color: rgba(255,255,255,.35); }

.tabs {
  display: flex; gap: 4px; overflow-x: auto;
  background: var(--surface); border-bottom: 1px solid var(--line);
  padding: 6px 10px; position: sticky; top: calc(48px + env(safe-area-inset-top, 0px)); z-index: 19;
}
.tab {
  font: inherit; font-weight: 700; white-space: nowrap; font-size: 15.5px;
  border: 0; background: transparent; color: var(--muted);
  padding: 11px 16px; border-radius: 12px; cursor: pointer; min-height: 46px;
}
.tab.active { background: var(--brand-soft); color: var(--brand-dark); }

main { padding: 16px; padding-bottom: calc(24px + env(safe-area-inset-bottom, 0px)); max-width: 1200px; margin: 0 auto; }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar .search { flex: 1; min-width: 140px; }
.toolbar select { width: auto; }
.toolbar .btn { white-space: nowrap; }
.btn.icon { padding: 9px 12px; font-size: 16px; }
.btn.icon.listening { background: #fdeeee; border-color: var(--danger); animation: pulse 1.2s infinite; }

.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }

/* ---------- แคตตาลอกสินค้า ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.product-card {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: hidden; cursor: pointer; border: 1px solid transparent;
  display: flex; flex-direction: column;
}
.product-card:hover { border-color: var(--brand); }
.product-img { aspect-ratio: 1; background: var(--brand-soft); display: grid; place-items: center; font-size: 34px; overflow: hidden; position: relative; }
.product-img img { width: 100%; height: 100%; object-fit: cover; }
.product-img .qty-badge {
  position: absolute; top: 6px; right: 6px;
  background: rgba(0,0,0,.6); color: #fff; font-size: 12px; font-weight: 700;
  border-radius: 99px; padding: 2px 9px;
}
.product-img .qty-badge.low { background: var(--danger); }
.product-body { padding: 8px 10px 10px; }
.product-name { font-weight: 600; font-size: 13.5px; line-height: 1.35; overflow-wrap: anywhere; }
.product-price { color: var(--brand-dark); font-weight: 800; font-size: 15px; font-variant-numeric: tabular-nums; }
.product-cat { font-size: 11.5px; color: var(--muted); }
.product-meta { font-size: 10.5px; color: var(--muted); margin-top: 3px; }
.product-card.out { opacity: .45; }

/* ---------- POS ---------- */
.pos-layout { display: grid; grid-template-columns: 1fr 330px; gap: 14px; align-items: start; }
.grid.pos { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }
.pos-cart { position: sticky; top: calc(104px + env(safe-area-inset-top, 0px)); }
.cart-items { display: flex; flex-direction: column; gap: 8px; margin: 8px 0; max-height: 300px; overflow-y: auto; }
.cart-item { display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); border-radius: 10px; padding: 6px 8px; }
.cart-item img, .cart-item .noimg { width: 38px; height: 38px; border-radius: 8px; object-fit: cover; background: var(--brand-soft); display: grid; place-items: center; flex: none; }
.cart-item .ci-name { flex: 1; font-size: 13px; font-weight: 600; line-height: 1.3; overflow-wrap: anywhere; }
.cart-item .ci-price { font-size: 11.5px; color: var(--muted); font-weight: 400; }
.qty-ctrl { display: flex; align-items: center; gap: 2px; }
.qty-ctrl button { width: 26px; height: 26px; border-radius: 8px; border: 1px solid var(--line); background: #fff; font-size: 15px; font-weight: 700; cursor: pointer; line-height: 1; }
.qty-ctrl span { min-width: 26px; text-align: center; font-weight: 700; font-variant-numeric: tabular-nums; }
.cart-rows { border-top: 1px dashed var(--line); margin-top: 8px; padding-top: 8px; display: flex; flex-direction: column; gap: 6px; }
.cart-rows .row { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.cart-rows .row input { width: 110px; text-align: right; padding: 6px 10px; }
.cart-rows .row.total { font-size: 18px; color: var(--brand-dark); }
.cart-rows .row.total strong { font-size: 22px; font-variant-numeric: tabular-nums; }
.pos-cart > input { margin: 10px 0 4px; }
.pay-choice { display: flex; gap: 14px; margin: 8px 0 12px; font-weight: 600; font-size: 14px; }
.pay-choice label { display: flex; align-items: center; gap: 6px; margin: 0; }
.pay-choice input { width: auto; }

/* ---------- บิล ---------- */
.bill-list { display: flex; flex-direction: column; gap: 10px; }
.bill-row {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 12px 14px; cursor: pointer; display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  border-left: 4px solid var(--line);
}
.bill-row.unpaid { border-left-color: var(--warn); }
.bill-row.paid { border-left-color: var(--ok); }
.bill-row.cancelled { border-left-color: var(--danger); opacity: .65; }
.bill-no { font-weight: 800; }
.bill-sub { font-size: 12px; color: var(--muted); width: 100%; }
.bill-total { margin-left: auto; font-weight: 800; font-size: 17px; color: var(--brand-dark); font-variant-numeric: tabular-nums; }
.chip { font-size: 11.5px; font-weight: 700; border-radius: 99px; padding: 2px 10px; }
.chip.unpaid { background: #fdf3df; color: var(--warn); }
.chip.paid { background: var(--brand-soft); color: var(--ok); }
.chip.cancelled { background: #fdeeee; color: var(--danger); }
.chip.slip { background: #e8effc; color: #2c5aa0; }

/* ---------- สรุปยอด ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 14px; }
.tile { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; }
.tile .t-label { font-size: 12.5px; color: var(--muted); font-weight: 600; }
.tile .t-value { font-size: 24px; font-weight: 800; color: var(--brand-dark); font-variant-numeric: tabular-nums; }
.tile.warn .t-value { color: var(--warn); }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.bar-row { display: grid; grid-template-columns: 90px 1fr 90px; align-items: center; gap: 8px; font-size: 13px; margin: 5px 0; }
.bar-row .bar { height: 14px; border-radius: 7px; background: var(--brand); min-width: 2px; }
.bar-row .val { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.low-row { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px dashed var(--line); font-size: 13.5px; }
.low-row strong { color: var(--danger); }

/* ---------- หลังบ้าน ---------- */
.admin-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.admin-grid .wide { grid-column: 1 / -1; }
.live-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: #bbb; vertical-align: middle; }
.live-dot.on { background: #e2342c; animation: pulse 1.6s infinite; }
@keyframes pulse { 50% { opacity: .35; } }
.log-feed { max-height: 320px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.log-item { border-left: 3px solid var(--brand); background: var(--bg); border-radius: 0 8px 8px 0; padding: 6px 10px; font-size: 13px; }
.log-item .l-head { font-weight: 700; }
.log-item .l-time { color: var(--muted); font-size: 11.5px; }
.log-item.new { animation: flash 1.2s; }
@keyframes flash { 0% { background: #fff4cf; } }
.user-row { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px dashed var(--line); font-size: 13.5px; flex-wrap: wrap; }
.user-row .u-name { font-weight: 700; }
.user-row .u-sub { color: var(--muted); font-size: 12px; width: 100%; }
.user-row .btn { margin-left: auto; }
.user-row.inactive { opacity: .5; }
#userList { margin-bottom: 12px; }
.log-table-wrap { overflow-x: auto; }
.log-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.log-table th, .log-table td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.log-table th { background: var(--bg); font-size: 12px; position: sticky; top: 0; }
.log-table td:first-child { white-space: nowrap; font-variant-numeric: tabular-nums; color: var(--muted); }

/* ---------- โมดัล ---------- */
.modal { border: 0; border-radius: 16px; box-shadow: 0 10px 40px rgba(0,0,0,.25); padding: 20px; width: min(430px, calc(100vw - 32px)); }
.modal.wide { width: min(760px, calc(100vw - 32px)); }
.modal::backdrop { background: rgba(10, 25, 18, .45); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.modal-meta { margin-top: 10px; }
.photo-box {
  aspect-ratio: 4 / 3; border: 2px dashed var(--line); border-radius: 12px;
  display: grid; place-items: center; cursor: pointer; overflow: hidden;
  background: var(--bg); color: var(--muted); font-size: 14px; text-align: center; position: relative;
}
.photo-box img { width: 100%; height: 100%; object-fit: cover; position: absolute; inset: 0; }
.restock-prod { display: flex; align-items: center; gap: 12px; margin: 10px 0 4px; }
.restock-prod img, .restock-prod span#restockNoImg { width: 58px; height: 58px; border-radius: 10px; object-fit: cover; background: var(--brand-soft); display: grid; place-items: center; font-size: 26px; flex: none; }
.restock-name { font-weight: 700; }
.stepper { display: flex; gap: 10px; align-items: center; margin-top: 6px; }
.stepper button { width: 52px; height: 52px; border-radius: 12px; border: 1px solid var(--line); background: var(--brand-soft); color: var(--brand-dark); font-size: 26px; font-weight: 800; cursor: pointer; line-height: 1; }
.stepper button:active { transform: scale(.95); }
.stepper input { flex: 1; text-align: center; font-size: 22px; font-weight: 800; padding: 10px; font-variant-numeric: tabular-nums; min-width: 0; }
.stepper.sm { margin-top: 4px; gap: 6px; }
.stepper.sm button { width: 40px; height: 40px; font-size: 20px; border-radius: 10px; flex: none; }
.stepper.sm input { font-size: 16px; font-weight: 600; padding: 8px; }
.product-body .restock { width: 100%; margin-top: 7px; background: var(--brand-soft); border-color: var(--brand-soft); color: var(--brand-dark); }

.photo-box.member { aspect-ratio: 1; max-width: 150px; margin: 8px auto 0; border-radius: 50%; }
.consent-box { background: var(--brand-soft); border-radius: 10px; padding: 10px 12px; margin-top: 12px; }
.consent-label { display: flex; gap: 10px; align-items: flex-start; font-weight: 400; font-size: 13px; margin: 0; line-height: 1.5; }
.consent-label input { width: auto; margin-top: 3px; flex: none; }
.cust-row { display: flex; align-items: center; gap: 10px; padding: 7px 0; border-bottom: 1px dashed var(--line); font-size: 13.5px; flex-wrap: wrap; }
.cust-row img.c-photo, .cust-row .c-ph { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; background: var(--brand-soft); display: grid; place-items: center; flex: none; }
.cust-row .c-sub { color: var(--muted); font-size: 12px; width: 100%; }
#customerList { margin-bottom: 12px; max-height: 300px; overflow-y: auto; }

.perm-row { display: flex; gap: 16px; margin-top: 12px; }
.perm-row label, .active-row { display: flex; align-items: center; gap: 6px; font-weight: 600; font-size: 13.5px; }
.perm-row input, .active-row input { width: auto; }
.active-row { margin-top: 12px; }

/* ---------- โมดัลบิล ---------- */
.bill-modal-body { display: grid; grid-template-columns: 1fr 240px; gap: 16px; }
.bill-canvas-wrap { background: var(--bg); border-radius: 12px; padding: 10px; display: grid; place-items: start center; max-height: 70vh; overflow-y: auto; }
#billCanvas, #billImage { width: 100%; max-width: 380px; height: auto; border-radius: 8px; box-shadow: var(--shadow); }
.center { text-align: center; }
.bill-canvas-wrap { grid-auto-rows: min-content; }
.bill-actions { display: flex; flex-direction: column; gap: 8px; }
.bill-actions .btn { width: 100%; }
.slip-img { width: 100%; border-radius: 10px; border: 1px solid var(--line); }
#billStatusLine { font-weight: 700; font-size: 14px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  background: var(--ink); color: #fff; border-radius: 99px; padding: 10px 20px;
  font-size: 14px; font-weight: 600; z-index: 99; box-shadow: var(--shadow);
  max-width: calc(100vw - 40px);
}
.toast.err { background: var(--danger); }

/* ---------- จอเล็ก ---------- */
@media (max-width: 820px) {
  .pos-layout { grid-template-columns: 1fr; }
  .pos-cart { position: static; }
  .two-col, .admin-grid { grid-template-columns: 1fr; }
  .bill-modal-body { grid-template-columns: 1fr; }
  .tabs { top: calc(44px + env(safe-area-inset-top, 0px)); }
}
