/* ==========================================================================
   TablePilot — Design tokens (อิงจาก dataviz skill reference palette)
   ========================================================================== */
:root {
  color-scheme: light;
  --surface-0: #f5f4f1;
  --surface-1: #fcfcfb;
  --surface-2: #ffffff;
  --border: #e4e2dc;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #8a887f;

  /* categorical */
  --c-blue: #2a78d6;
  --c-orange: #eb6834;
  --c-aqua: #1baf7a;
  --c-yellow: #eda100;
  --c-magenta: #e87ba4;
  --c-green: #008300;
  --c-violet: #4a3aa7;
  --c-red: #e34948;

  /* status (fixed, never re-themed) */
  --st-good: #0ca30c;
  --st-warning: #fab219;
  --st-serious: #ec835a;
  --st-critical: #d03b3b;

  --accent: var(--c-orange);
  --accent-ink: #ffffff;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --shadow-1: 0 1px 3px rgba(11,11,11,0.08), 0 1px 2px rgba(11,11,11,0.05);
  --shadow-2: 0 8px 24px rgba(11,11,11,0.12);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: 'Prompt', 'Noto Sans Thai', 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--surface-0);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  line-height: 1.5;
  letter-spacing: 0.1px;
}
button, input, select, textarea { font-family: inherit; }
a { color: inherit; }
::selection { background: color-mix(in srgb, var(--accent) 30%, white); }

/* ---------- App shell ---------- */
#app-shell { min-height: 100vh; display: flex; flex-direction: column; }

.topbar {
  display: flex; align-items: center; gap: 18px;
  padding: 12px 22px; background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 50;
  box-shadow: 0 1px 0 rgba(11,11,11,.02);
}
.topbar .brand { display: flex; align-items: center; gap: 9px; font-weight: 800; font-size: 17px; margin-right: 6px; white-space: nowrap; letter-spacing: -0.01em; }
.topbar .brand .logo {
  width: 34px; height: 34px; border-radius: 10px; display: block;
}
.topbar nav { display: flex; gap: 2px; flex: 1; overflow-x: auto; }
.topbar nav a {
  display: flex; align-items: center; gap: 6px;
  padding: 9px 14px; border-radius: 999px;
  text-decoration: none; color: var(--text-secondary); font-weight: 600; font-size: 13.5px;
  white-space: nowrap; transition: background .12s, color .12s;
}
.topbar nav a:hover { background: var(--surface-0); }
.topbar nav a.active { background: var(--accent); color: white; }
.topbar .user-chip {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 8px 6px 12px; border-radius: 999px; background: var(--surface-0);
  font-size: 13px; white-space: nowrap;
}
.topbar .user-chip .role-badge {
  font-size: 11px; font-weight: 700; padding: 3px 8px; border-radius: 999px;
  background: var(--c-blue); color: white;
}
.topbar .logout-btn {
  border: none; background: transparent; color: var(--text-muted); cursor: pointer;
  font-size: 18px; padding: 4px 8px; border-radius: 8px;
}
.topbar .logout-btn:hover { background: var(--surface-2); color: var(--st-critical); }

main#view { flex: 1; padding: 20px; max-width: 1400px; margin: 0 auto; width: 100%; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text-primary);
  border-radius: var(--radius-md); padding: 11px 18px; font-size: 14.5px; font-weight: 600;
  cursor: pointer; transition: transform .05s, box-shadow .12s, background .12s;
  min-height: 44px; letter-spacing: -0.005em;
}
.btn:hover { box-shadow: var(--shadow-1); }
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: color-mix(in srgb, var(--accent) 88%, black); }
.btn-blue { background: var(--c-blue); border-color: var(--c-blue); color: white; }
.btn-good { background: var(--st-good); border-color: var(--st-good); color: white; }
.btn-danger { background: var(--st-critical); border-color: var(--st-critical); color: white; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-lg { padding: 16px 22px; font-size: 17px; border-radius: var(--radius-lg); }
.btn-sm { padding: 6px 12px; font-size: 13px; min-height: 34px; border-radius: var(--radius-sm); }
.btn-block { width: 100%; }
.icon-btn { padding: 8px 10px; min-height: 36px; }

/* ---------- Cards / layout ---------- */
.card { background: var(--surface-2); border: 1px solid var(--border); border-radius: 18px; padding: 20px 22px; box-shadow: 0 1px 2px rgba(11,11,11,.04); }
.card h3 { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; }
.grid { display: grid; gap: 14px; }
/* minmax(0,1fr) แทน 1fr เฉยๆ — ปกติ 1fr มีขนาดต่ำสุดเป็น auto (min-content) โดยปริยาย ถ้าลูกในคอลัมน์มีเนื้อหากว้าง
   (เช่นตารางคอลัมน์เยอะ) คอลัมน์จะไม่ยอมหดตามพื้นที่จริง ทำให้ทั้งหน้าล้นแนวนอนได้ทั้งที่ตั้งใจให้แบ่งครึ่งเท่ากัน */
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.row { display: flex; align-items: center; gap: 10px; }
.row.between { justify-content: space-between; }
.row.wrap { flex-wrap: wrap; }
.col { display: flex; flex-direction: column; gap: 10px; }
.spacer { flex: 1; }
.muted { color: var(--text-muted); }
.secondary-text { color: var(--text-secondary); }
h1, h2, h3, h4 { margin: 0 0 4px 0; font-weight: 700; letter-spacing: -0.01em; }
h1 { font-size: 26px; font-weight: 800; } h2 { font-size: 19px; } h3 { font-size: 16px; font-weight: 600; }
.page-header { margin-bottom: 24px; }
.page-header .subtitle { color: var(--text-secondary); font-size: 14px; margin-top: 4px; font-weight: 400; }
.page-header h1 { display: flex; align-items: center; gap: 10px; }
.page-icon {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; border-radius: 12px; font-size: 20px; flex-shrink: 0;
  background: color-mix(in srgb, var(--accent) 14%, white);
}

input, select, textarea {
  font-family: inherit; font-size: 15px; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface-2);
  color: var(--text-primary); min-height: 42px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 50%, transparent); outline-offset: 1px; border-color: var(--accent); }
label { font-size: 13px; font-weight: 600; color: var(--text-secondary); display: block; margin-bottom: 4px; }
.field { display: flex; flex-direction: column; }

/* ห่อตารางที่อาจกว้างเกินการ์ด (คอลัมน์เยอะ) ด้วย overflow-x:auto กันหน้าล้นแนวนอน — ให้ตารางเลื่อนในกรอบตัวเองแทน */
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--border); }
th { color: var(--text-muted); font-weight: 600; font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; }
tbody tr:last-child td { border-bottom: none; }
tr:hover td { background: color-mix(in srgb, var(--surface-0) 60%, transparent); }

.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; font-weight: 600; padding: 4px 10px; border-radius: 999px; }
.badge-good { background: color-mix(in srgb, var(--st-good) 15%, white); color: color-mix(in srgb, var(--st-good) 70%, black); }
.badge-warning { background: color-mix(in srgb, var(--st-warning) 22%, white); color: #8a5a00; }
.badge-critical { background: color-mix(in srgb, var(--st-critical) 15%, white); color: var(--st-critical); }
.badge-neutral { background: var(--surface-0); color: var(--text-secondary); }
.badge-blue { background: color-mix(in srgb, var(--c-blue) 15%, white); color: var(--c-blue); }

.toast-stack { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 999; }
.toast { background: var(--text-primary); color: white; padding: 12px 18px; border-radius: var(--radius-md); box-shadow: var(--shadow-2); font-size: 14px; font-weight: 600; animation: toast-in .15s ease-out; }
.toast.error { background: var(--st-critical); }
.toast.success { background: var(--st-good); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

.modal-backdrop { position: fixed; inset: 0; background: rgba(11,11,11,0.45); display: flex; align-items: center; justify-content: center; z-index: 200; padding: 16px; }
.modal { background: var(--surface-2); border-radius: var(--radius-lg); padding: 22px; width: 100%; max-width: 480px; box-shadow: var(--shadow-2); max-height: 90vh; overflow: auto; }

.empty-state { text-align: center; padding: 40px 20px; color: var(--text-muted); }
.empty-state .emoji { font-size: 40px; display: block; margin-bottom: 10px; }

/* ---------- Login ---------- */
.login-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: radial-gradient(circle at 20% 15%, #ff8a54 0%, transparent 45%), linear-gradient(160deg, var(--accent) 0%, #a83d18 100%);
  padding: 20px;
}
.login-card { background: var(--surface-2); border-radius: 22px; padding: 40px 36px; width: 100%; max-width: 380px; box-shadow: 0 24px 60px rgba(0,0,0,.28); }
.login-card .logo {
  display: block; width: 64px; height: 64px; border-radius: 18px; margin: 0 auto 16px;
}
.login-card h1 { text-align: center; font-size: 22px; font-weight: 800; }
.login-card .subtitle { text-align: center; color: var(--text-muted); margin-bottom: 24px; font-size: 13.5px; }
.quick-accounts { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-top: 16px; }
.quick-accounts button { font-size: 12px; padding: 8px; }

/* ---------- Tables page (floor plan) ---------- */
.zone-block { margin-bottom: 26px; }
.zone-block-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.zone-block-head h3 { margin: 0; }
.legend { display: flex; gap: 16px; margin-bottom: 16px; flex-wrap: wrap; }
.legend .dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; margin-right: 6px; }

/* ---------- ผังโต๊ะแบบลากวางได้ (โต๊ะกลม + เก้าอี้) ---------- */
.floor-canvas {
  position: relative; width: 100%; min-height: 320px; border-radius: 18px;
  background: var(--surface-2); border: 1px solid var(--border);
}
.floor-canvas.edit-mode {
  background-image: radial-gradient(circle, var(--border) 1.3px, transparent 1.3px);
  background-size: 26px 26px; border-style: dashed; border-color: color-mix(in srgb, var(--accent) 40%, var(--border));
}
.table-shape { position: absolute; touch-action: none; user-select: none; }
.table-shape.edit-mode { cursor: grab; }
.table-shape.edit-mode.dragging { cursor: grabbing; z-index: 60; filter: drop-shadow(0 8px 14px rgba(0,0,0,.25)); }
.table-shape .table-top {
  position: absolute; border-radius: 50%; display: flex; flex-direction: column; align-items: center; justify-content: center;
  box-shadow: 0 4px 10px rgba(0,0,0,.18), inset 0 -4px 8px rgba(0,0,0,.10), inset 0 3px 6px rgba(255,255,255,.25);
  border: 3px solid rgba(255,255,255,.55); transition: transform .1s;
}
.table-shape:not(.edit-mode) .table-top:hover { transform: translate(-50%,-50%) scale(1.06); }
.table-shape .tt-name { font-size: 13px; font-weight: 800; line-height: 1.1; }
.table-shape .tt-seats { font-size: 8.5px; opacity: .9; font-weight: 600; margin-top: 1px; }
.table-shape .tt-total { font-size: 9.5px; margin-top: 3px; background: rgba(255,255,255,.3); padding: 1px 6px; border-radius: 8px; font-weight: 700; }
.table-del-btn {
  position: absolute; top: -4px; right: -4px; width: 22px; height: 22px; border-radius: 50%;
  border: 2px solid white; background: var(--st-critical); color: white; font-size: 14px; line-height: 1;
  cursor: pointer; display: flex; align-items: center; justify-content: center; box-shadow: 0 2px 6px rgba(0,0,0,.25); z-index: 5;
}
.table-action-btn {
  position: absolute; top: -6px; left: -6px; width: 24px; height: 24px; border-radius: 50%;
  border: 2px solid white; background: var(--surface-2); font-size: 12px; line-height: 1;
  cursor: pointer; display: flex; align-items: center; justify-content: center; box-shadow: 0 2px 6px rgba(0,0,0,.2); z-index: 5;
}
.floor-toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.floor-hint {
  font-size: 12.5px; color: var(--text-secondary); background: color-mix(in srgb, var(--accent) 10%, white);
  padding: 8px 14px; border-radius: 10px; margin-bottom: 14px;
}

/* ---------- POS screen ---------- */
.pos-layout { display: grid; grid-template-columns: 1fr 380px; gap: 16px; align-items: start; }
@media (max-width: 900px) { .pos-layout { grid-template-columns: 1fr; } }
.category-tabs { display: flex; gap: 6px; overflow-x: auto; margin-bottom: 14px; padding-bottom: 4px; }
.category-tabs button { flex-shrink: 0; padding: 9px 16px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-2); font-weight: 600; font-size: 13px; cursor: pointer; }
.category-tabs button.active { background: var(--accent); color: white; border-color: var(--accent); }
.menu-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.menu-tile {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 0; cursor: pointer; text-align: left; min-height: 108px; display: flex; flex-direction: column;
  overflow: hidden; transition: box-shadow .1s, transform .05s;
}
.menu-tile:hover { box-shadow: var(--shadow-1); transform: translateY(-1px); }
.menu-tile:active { transform: scale(0.97); }
.menu-tile .m-photo { width: 100%; height: 84px; object-fit: cover; display: block; background: var(--surface-0); }
.menu-tile .m-body { padding: 12px 14px 14px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.menu-tile.has-photo .m-body { padding: 10px 12px 12px; }
.menu-tile .m-emoji { font-size: 26px; }
.menu-tile .m-name { font-weight: 700; font-size: 14px; line-height: 1.3; }
.menu-tile .m-price { color: var(--accent); font-weight: 800; font-size: 14px; margin-top: auto; }

.cart-panel { position: sticky; top: 70px; }
.cart-items { max-height: 40vh; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; margin: 12px 0; }
.cart-item { display: flex; gap: 10px; align-items: flex-start; padding-bottom: 10px; border-bottom: 1px dashed var(--border); }
.cart-item .ci-name { font-weight: 700; font-size: 14px; }
.cart-item .ci-note { font-size: 12px; color: var(--text-muted); }
.qty-stepper { display: flex; align-items: center; gap: 8px; }
.qty-stepper button { width: 28px; height: 28px; border-radius: 50%; border: 1px solid var(--border); background: var(--surface-0); cursor: pointer; font-weight: 700; }
.cart-totals { border-top: 2px solid var(--border); padding-top: 12px; margin-top: 8px; }
.cart-totals .line { display: flex; justify-content: space-between; padding: 3px 0; font-size: 14px; }
.cart-totals .grand { font-size: 20px; font-weight: 800; padding-top: 6px; }
.item-status-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }

/* ---------- Kitchen ---------- */
.kds-board { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }

.qr-tables-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 14px; }
.qr-table-card { text-align: center; }
.qr-table-card canvas { width: 100%; max-width: 160px; height: auto; border: 1px solid var(--border); border-radius: var(--radius-sm); background: #fff; padding: 6px; }
.kds-ticket { background: var(--surface-2); border-radius: var(--radius-lg); box-shadow: var(--shadow-1); overflow: hidden; border-top: 6px solid var(--st-warning); }
.kds-ticket.urgent { border-top-color: var(--st-critical); }
.kds-ticket .kt-head { padding: 12px 14px; display: flex; justify-content: space-between; align-items: center; background: var(--surface-0); }
.kds-ticket .kt-timer { font-weight: 800; font-variant-numeric: tabular-nums; }
.kds-ticket .kt-body { padding: 12px 14px; display: flex; flex-direction: column; gap: 10px; }
.kds-line { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px; border-radius: 10px; background: var(--surface-0); }
.kds-line .qty-badge { background: var(--text-primary); color: white; border-radius: 6px; padding: 2px 8px; font-weight: 800; font-size: 13px; }
.kds-line.done { opacity: .4; text-decoration: line-through; }
.kds-ticket .kt-foot { padding: 10px 14px; }

/* ---------- Stat tiles (infographic-style) ---------- */
.stat-tile {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 18px 20px; display: flex; align-items: flex-start; gap: 14px; transition: box-shadow .15s, transform .1s;
}
.stat-tile:hover { box-shadow: var(--shadow-1); transform: translateY(-1px); }
.stat-tile .st-icon {
  width: 44px; height: 44px; border-radius: 12px; display: flex; align-items: center; justify-content: center;
  font-size: 21px; flex-shrink: 0;
}
.stat-tile .st-body { min-width: 0; }
.stat-tile .st-label { font-size: 12.5px; color: var(--text-secondary); font-weight: 600; }
.stat-tile .st-value { font-size: 25px; font-weight: 800; margin-top: 2px; letter-spacing: -0.01em; }
.stat-tile .st-delta { font-size: 12px; margin-top: 4px; font-weight: 700; }
.stat-tile .st-delta.up { color: var(--st-good); }
.stat-tile .st-delta.down { color: var(--st-critical); }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.tabs button { padding: 10px 16px; border: none; background: none; cursor: pointer; font-weight: 700; font-size: 14px; color: var(--text-muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }

.stock-bar-track { width: 100%; height: 8px; background: var(--surface-0); border-radius: 4px; overflow: hidden; }
.stock-bar-fill { height: 100%; border-radius: 4px; }

.customer-tier { font-weight: 800; }
.tier-Gold { color: #b8860b; }
.tier-Silver { color: #6b7280; }
.tier-Member { color: var(--text-muted); }

@media (max-width: 720px) {
  .topbar { padding: 8px 12px; }
  .topbar .brand span.brand-text { display: none; }
  main#view { padding: 12px; }
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr 1fr; }

  /* จอแคบ: เมนูนำทาง (nav) แข่งพื้นที่กับปุ่ม/user-chip ที่ไม่ยอมหด จน nav ถูกบีบจนความกว้างเกือบเป็นศูนย์และ
     มองไม่เห็นเลย (ลิงก์ยังอยู่ใน DOM แต่กดไม่ได้) — แก้โดยให้ topbar ตัดขึ้นบรรทัดใหม่ ดัน nav ไปเป็นแถวของตัวเอง
     เต็มความกว้าง เลื่อนซ้าย-ขวาได้ แทนที่จะแย่งพื้นที่กับปุ่มอื่นบนบรรทัดเดียวกัน */
  .topbar { flex-wrap: wrap; row-gap: 8px; }
  .topbar nav { flex: 1 1 100%; order: 5; }
}

/* ---------- PIN login (พนักงานหน้างาน) ---------- */
.pin-staff-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 12px; margin-top: 4px; }
.pin-staff-card {
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 16px 10px; border-radius: var(--radius-lg); border: 1px solid var(--border);
  background: var(--surface-2); cursor: pointer; transition: box-shadow .15s, transform .1s, border-color .15s;
}
.pin-staff-card:hover { box-shadow: var(--shadow-1); transform: translateY(-1px); border-color: var(--accent); }
.pin-staff-name { font-weight: 700; font-size: 13.5px; text-align: center; }
.pin-staff-role { font-size: 11.5px; text-align: center; }
.pin-avatar {
  width: 34px; height: 34px; border-radius: 50%; background: var(--accent); color: #fff;
  display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 15px; flex-shrink: 0;
}
.pin-avatar-lg { width: 56px; height: 56px; font-size: 22px; }
.pin-pad-wrap { display: flex; flex-direction: column; gap: 14px; align-items: stretch; }
.pin-staff-chip { display: flex; align-items: center; gap: 10px; justify-content: center; padding: 6px 0; }
.pin-dots { display: flex; justify-content: center; gap: 10px; margin: 4px 0; }
.pin-dot { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--border); background: transparent; transition: background .1s, border-color .1s; }
.pin-dot.filled { background: var(--accent); border-color: var(--accent); }
.pin-keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; max-width: 280px; margin: 0 auto; width: 100%; }
.pin-key {
  padding: 16px 0; border-radius: var(--radius-lg); border: 1px solid var(--border); background: var(--surface-2);
  font-size: 20px; font-weight: 700; cursor: pointer; transition: background .1s;
}
.pin-key:hover { background: var(--surface-0); }
.pin-key-action { font-size: 17px; color: var(--text-muted); }
