/* สไตล์กลาง — ใช้ร่วมกันทุกหน้า (เคาน์เตอร์ / แอดมิน / แอปสมาชิก) · mobile-first */
:root {
  --green: #0b6b3a;
  --green-dark: #084d2a;
  --green-soft: #e3f2e9;
  --bg: #f3f5f4;
  --surface: #ffffff;
  --text: #1b2420;
  --muted: #64716b;
  --border: #d5ddd9;
  --danger: #b3261e;
  --danger-soft: #fbe5e3;
  --warn: #8a5a00;
  --warn-soft: #fff1cc;
  --info: #1f5aa6;
  --info-soft: #e1ecfa;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0, 0, 0, .08), 0 1px 2px rgba(0, 0, 0, .04);
  --font: system-ui, -apple-system, "Segoe UI", "Noto Sans Thai", "Leelawadee UI", Tahoma, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
}
h1, h2, h3 { margin: 0 0 .5rem; line-height: 1.25; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .5rem; }
a { color: var(--green); }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.num { font-variant-numeric: tabular-nums; }
.right { text-align: right; }
.center { text-align: center; }
.big { font-size: 1.6rem; font-weight: 700; }
.nowrap { white-space: nowrap; }
.text-danger { color: var(--danger); }
.text-ok { color: var(--green); }

/* ---------- โครงหน้า ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; flex-wrap: wrap; align-items: center; gap: .5rem .75rem;
  padding: .6rem 16px;
  background: var(--green); color: #fff;
}
.topbar .brand { font-weight: 700; font-size: 1.1rem; margin-right: auto; }
.topbar a, .topbar .link { color: #fff; }
.topbar .btn { background: rgba(255, 255, 255, .15); color: #fff; border-color: rgba(255, 255, 255, .4); }
.container { max-width: 1200px; margin: 0 auto; padding: 16px; }
.narrow { max-width: 420px; margin: 2rem auto; }
.banner {
  padding: .5rem 16px; background: var(--warn-soft); color: var(--warn);
  font-weight: 600; text-align: center;
}
.row { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }
.stack > * + * { margin-top: .75rem; }
.mt { margin-top: 1rem; }
.mb { margin-bottom: 1rem; }

/* ---------- แท็บ ---------- */
.tabs {
  display: flex; gap: .25rem; overflow-x: auto; -webkit-overflow-scrolling: touch;
  padding: 0 16px; background: var(--surface); border-bottom: 1px solid var(--border);
  scrollbar-width: thin;
}
.tabs button {
  flex: 0 0 auto; padding: .7rem .9rem; border: 0; border-bottom: 3px solid transparent;
  background: none; font: inherit; color: var(--muted); cursor: pointer; white-space: nowrap;
}
.tabs button.active { color: var(--green); border-bottom-color: var(--green); font-weight: 600; }
.tabs .count {
  display: inline-block; min-width: 1.4em; padding: 0 .35em; margin-left: .25em;
  border-radius: 999px; background: var(--danger); color: #fff; font-size: .75rem; text-align: center;
}

/* ---------- ปุ่ม ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
  min-height: 40px; padding: .45rem .9rem;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text);
  font: inherit; font-weight: 500; text-decoration: none; cursor: pointer;
  touch-action: manipulation;
}
.btn:hover { filter: brightness(.97); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-outline-danger { color: var(--danger); border-color: var(--danger); background: var(--surface); }
.btn-sm { min-height: 32px; padding: .2rem .6rem; font-size: .875rem; }
.btn-block { display: flex; width: 100%; }

/* ---------- การ์ด / กริด ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1rem;
}
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
.grid-sm { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
.stat .label { color: var(--muted); font-size: .875rem; }
.stat .value { font-size: 1.4rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .sub { color: var(--muted); font-size: .8rem; }

/* การ์ดโต๊ะ (เคาน์เตอร์) */
.table-card { display: flex; flex-direction: column; gap: .4rem; border-top: 6px solid var(--border); }
.table-card.busy { border-top-color: var(--green); background: var(--green-soft); }
.table-card .t-name { font-size: 1.2rem; font-weight: 700; }
.table-card .t-clock { font-size: 1.8rem; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: .02em; }
.table-card .t-price { font-size: 1.3rem; font-weight: 700; color: var(--green-dark); font-variant-numeric: tabular-nums; }
.table-card .btn { margin-top: auto; }

/* modal เพิ่มทางลัด (shortcut.js) */
.sc-title { font-size: 1rem; margin: .8rem 0 .3rem; }
.sc-steps { margin: 0; padding-left: 1.4rem; }
.sc-steps li { margin: .2rem 0; }
.sc-qr { display: block; width: min(220px, 60vw); height: auto; margin: .4rem auto; background: #fff; }
.sc-url { word-break: break-all; }
/* รายการเลือกสมาชิกตอนเปิดโต๊ะ (พิมพ์บางส่วน → เลือก) */
.pick-list { list-style: none; margin: .4rem 0 0; padding: 0; max-height: 300px; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.pick-item { display: grid; grid-template-columns: 1fr auto; gap: 0 .75rem; padding: .55rem .75rem; border-bottom: 1px solid var(--border); cursor: pointer; }
.pick-item:last-child { border-bottom: 0; }
.pick-item:hover, .pick-item.active { background: var(--green-soft); }
.pick-name { font-weight: 600; }
.pick-phone { color: var(--muted); font-variant-numeric: tabular-nums; }
.pick-sub { grid-column: 1 / -1; font-size: .8rem; color: var(--green-dark); }
.pick-new { display: block; color: var(--green-dark); font-weight: 600; }
.pick-note { padding: .55rem .75rem; color: var(--muted); font-size: .9rem; }

/* ปุ่มแสดง/ซ่อน PIN (addPinToggle) */
.pin-wrap { position: relative; display: block; }
.pin-wrap input { width: 100%; padding-right: 4.2rem; }
.pin-toggle {
  position: absolute; top: 50%; right: .35rem; transform: translateY(-50%);
  min-height: 32px; padding: .2rem .6rem; border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--green-dark); font: inherit; font-size: .85rem; cursor: pointer;
}
.pin-toggle[aria-pressed="true"] { background: var(--green-soft); }

/* QR สำหรับพิมพ์ (เมนูแอดมิน) */
.qr-tool { display: grid; gap: 16px; grid-template-columns: minmax(260px, 360px) 1fr; align-items: start; }
.qr-canvas { width: 100%; max-width: 480px; height: auto; border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); background: #fff; }
#qr-url { word-break: break-all; }
@media (max-width: 720px) { .qr-tool { grid-template-columns: 1fr; } }

/* เมนู ☰ (หน้าพนักงาน/แอดมิน) */
.menu-btn {
  position: relative; display: inline-flex; align-items: center; justify-content: center;
  width: 44px; height: 40px; border: 1px solid rgba(255, 255, 255, .4); border-radius: 8px;
  background: rgba(255, 255, 255, .15); color: #fff; cursor: pointer;
}
.burger, .burger::before, .burger::after { display: block; width: 20px; height: 2px; border-radius: 2px; background: currentColor; }
.burger { position: relative; }
.burger::before, .burger::after { content: ""; position: absolute; left: 0; }
.burger::before { top: -6px; }
.burger::after { top: 6px; }
.menu-btn .count { position: absolute; top: -6px; right: -8px; }
.count {
  display: inline-block; min-width: 1.4em; padding: 0 .35em; margin-left: .25em;
  border-radius: 999px; background: var(--danger); color: #fff; font-size: .75rem; line-height: 1.5; text-align: center;
}
.topbar .who { font-size: .9rem; opacity: .9; }
.drawer-backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, .35); }
.drawer {
  position: fixed; top: 0; bottom: 0; left: 0; z-index: 41; display: flex; flex-direction: column;
  width: min(300px, 85vw); background: var(--surface); box-shadow: 4px 0 16px rgba(0, 0, 0, .2); overflow-y: auto;
}
.drawer[hidden], .drawer-backdrop[hidden] { display: none; }
.drawer-head { display: flex; align-items: flex-start; justify-content: space-between; gap: .5rem; padding: 1rem; background: var(--green); color: #fff; }
.drawer-head .dlg-close { color: #fff; }
.menu { flex: 1; padding: .5rem 0; }
.menu-item {
  display: flex; align-items: center; justify-content: space-between; width: 100%; min-height: 46px;
  padding: .6rem 1.1rem; border: 0; border-left: 4px solid transparent; background: none;
  font: inherit; color: var(--text); text-align: left; text-decoration: none; cursor: pointer;
}
.menu-item:hover { background: var(--bg); }
.menu-item.active { border-left-color: var(--green); background: var(--green-soft); color: var(--green-dark); font-weight: 600; }
.menu-group summary { padding: .7rem 1.1rem .3rem; color: var(--muted); font-size: .85rem; font-weight: 600; cursor: pointer; }
.menu-group .menu-item { padding-left: 2rem; }
.drawer-foot { border-top: 1px solid var(--border); padding: .5rem 0; }

/* แผงโต๊ะแบบรูปโต๊ะ (หน้าเคาน์เตอร์) — --s = ตัวคูณขนาด ตั้งจาก fitTables() ให้โต๊ะน้อยขยายเต็มจอ */
.container.wide { max-width: none; }
.tbl-grid {
  --s: 1;
  display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  padding: 14px; border-radius: var(--radius); background: #bfe6ea;
}
.tbl-grid.fit { grid-template-columns: repeat(var(--cols), calc(150px * var(--s))); justify-content: center; align-content: center; }
.tbl {
  display: flex; flex-direction: column; align-items: center; gap: .25em;
  padding: .5em .4em .6em; border: 2px solid transparent; border-radius: var(--radius);
  background: rgba(255, 255, 255, .55); color: var(--text); font: inherit; font-size: calc(1rem * var(--s)); cursor: pointer;
  transition: transform .1s, box-shadow .1s;
}
.tbl:hover { transform: translateY(-2px); box-shadow: 0 4px 10px rgba(0, 0, 0, .15); }
.tbl:focus-visible { outline: 3px solid var(--info); outline-offset: 2px; }
.tbl.busy { border-color: #c62828; }
.tbl-clock {
  padding: .06em .6em; border-radius: .4em; background: #fff; border: 1px solid #9aa;
  font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: .02em;
}
.tbl.busy .tbl-clock { color: #b71c1c; }
.tbl-felt {
  position: relative; display: flex; align-items: center; justify-content: center;
  width: 4em; height: 7em; border: .375em solid #6d4424; border-radius: .375em;
  background: #2e9d3a; box-shadow: inset 0 0 .6em rgba(0, 0, 0, .35);
}
.tbl.busy .tbl-felt { background: #d32f2f; }
.tbl-pocket { position: absolute; width: .625em; height: .625em; border-radius: 50%; background: #111; }
.tbl-pocket.tl { top: -.31em; left: -.31em; }
.tbl-pocket.tr { top: -.31em; right: -.31em; }
.tbl-pocket.ml { top: calc(50% - .31em); left: -.44em; }
.tbl-pocket.mr { top: calc(50% - .31em); right: -.44em; }
.tbl-pocket.bl { bottom: -.31em; left: -.31em; }
.tbl-pocket.br { bottom: -.31em; right: -.31em; }
.tbl-num {
  max-width: 3.4em; padding: 0 .25em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  border-radius: .3em; background: #fff; color: #111; font-size: .85em; font-weight: 700;
}
.tbl-name { font-weight: 700; }
.tbl-sub { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .8em; color: var(--muted); }
.tbl-price { font-weight: 700; color: #b71c1c; font-variant-numeric: tabular-nums; }
/* แลกแต้ม: ช่องโค้ด + ภาพจากกล้อง · แอปสมาชิก: QR + โค้ดตัวใหญ่ */
.code-input { font-size: 1.4rem; letter-spacing: .15em; text-transform: uppercase; font-variant-numeric: tabular-nums; }
.scan-video { display: block; width: 100%; max-height: 320px; margin: .5rem 0; border-radius: var(--radius); background: #000; object-fit: cover; }
.redeem-qr { display: block; width: min(240px, 70vw); height: auto; margin: .5rem auto; background: #fff; }
.redeem-code { font-size: 2rem; font-weight: 700; letter-spacing: .25em; text-align: center; font-variant-numeric: tabular-nums; }
.tbl-legend { display: inline-flex; align-items: center; gap: 4px; }
.tbl-legend::before { content: ""; width: 12px; height: 12px; border-radius: 3px; background: #2e9d3a; }
.tbl-legend.busy::before { background: #d32f2f; }

/* ---------- ป้าย ---------- */
.badge {
  display: inline-block; padding: .1rem .55rem; border-radius: 999px;
  font-size: .78rem; font-weight: 600; line-height: 1.5; white-space: nowrap;
  background: #e8ebea; color: var(--muted);
}
.badge-ok { background: var(--green-soft); color: var(--green-dark); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-info { background: var(--info-soft); color: var(--info); }

/* ---------- ฟอร์ม ---------- */
label { display: block; font-weight: 500; margin-bottom: .2rem; }
.field { margin-bottom: .75rem; }
.field .hint { font-size: .8rem; color: var(--muted); }
input[type=text], input[type=tel], input[type=password], input[type=number], input[type=date], input[type=search], select, textarea {
  width: 100%; min-height: 42px; padding: .45rem .65rem;
  border: 1px solid var(--border); border-radius: 8px; background: #fff; color: var(--text);
  font: inherit; font-size: 16px; /* 16px กัน iOS/Android zoom */
}
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--green); outline-offset: 1px; }
input[type=checkbox] { width: 1.2rem; height: 1.2rem; accent-color: var(--green); vertical-align: middle; }
.check { display: flex; align-items: center; gap: .5rem; font-weight: 500; cursor: pointer; }
.inline-fields { display: flex; flex-wrap: wrap; gap: .5rem; align-items: flex-end; }
.inline-fields .field { flex: 1 1 140px; margin-bottom: 0; }
.form-error { color: var(--danger); background: var(--danger-soft); padding: .45rem .65rem; border-radius: 8px; margin: .5rem 0; }
.form-error:empty { display: none; }

/* ---------- ตาราง ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table.data { width: 100%; border-collapse: collapse; font-size: .9rem; }
.table-wrap.wide table.data { min-width: 760px; } /* ตารางคอลัมน์เยอะ: เลื่อนแนวนอนในกรอบแทนการบีบ */
table.data th, table.data td { padding: .5rem .6rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
table.data th { background: #f7f9f8; font-weight: 600; white-space: nowrap; }
table.data tr:last-child td { border-bottom: 0; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.data tfoot td { font-weight: 700; background: #f7f9f8; }
.empty { padding: 1.5rem; text-align: center; color: var(--muted); }

/* รายการ (เช่น บิลรอยืนยัน) */
.list { display: flex; flex-direction: column; gap: 12px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: .15rem .9rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.kv .total { font-size: 1.25rem; font-weight: 700; }

/* ---------- dialog ---------- */
dialog {
  width: min(480px, calc(100vw - 24px)); max-height: calc(100vh - 24px);
  padding: 0; border: 0; border-radius: 14px; box-shadow: 0 10px 40px rgba(0, 0, 0, .3);
  color: var(--text); background: var(--surface);
}
dialog.wide { width: min(900px, calc(100vw - 24px)); }
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.dlg-head { display: flex; align-items: center; gap: .5rem; padding: .8rem 1rem; border-bottom: 1px solid var(--border); }
.dlg-head h2 { margin: 0; flex: 1; }
.dlg-close { border: 0; background: none; font-size: 1.5rem; line-height: 1; cursor: pointer; color: var(--muted); padding: .2rem .4rem; }
.dlg-body { padding: 1rem; overflow-y: auto; max-height: calc(100vh - 160px); }
.dlg-actions { display: flex; flex-wrap: wrap; gap: .5rem; justify-content: flex-end; padding: .75rem 1rem; border-top: 1px solid var(--border); }

/* ---------- toast ---------- */
.toast-box { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 1000; display: flex; flex-direction: column; gap: 8px; width: min(420px, calc(100vw - 32px)); pointer-events: none; }
.toast { padding: .7rem 1rem; border-radius: 10px; background: #1b2420; color: #fff; box-shadow: var(--shadow); text-align: center; animation: toast-in .2s ease-out; }
.toast.error { background: var(--danger); }
.toast.ok { background: var(--green); }
@keyframes toast-in { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

@media (min-width: 720px) {
  h1 { font-size: 1.4rem; }
  .container { padding: 20px 24px; }
}
@media print {
  .topbar, .tabs, .btn, .toast-box { display: none !important; }
}
