/* ==== ระบบดีไซน์ มังกรยางกูซ่า ==== */
:root {
  --bg: #f7f6f4;
  --surface: #ffffff;
  --surface-2: #f1efec;
  --border: #e4e1db;
  --text: #1c1a17;
  --text-muted: #6b6459;
  --brand: #ea580c;
  --brand-strong: #c2410c;
  --brand-soft: #ffedd5;
  --gold: #b8860b;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --ok: #16a34a;
  --ok-soft: #f0fdf4;
  --warn: #d97706;
  --warn-soft: #fffbeb;
  --shadow: 0 1px 2px rgba(20,16,10,0.06), 0 4px 16px rgba(20,16,10,0.06);
  --radius: 12px;
  --sidebar-w: 248px;
}

:root[data-theme="dark"] {
  --bg: #15140f;
  --surface: #1f1d18;
  --surface-2: #262319;
  --border: #35311f;
  --text: #f3f1ec;
  --text-muted: #a89f8d;
  --brand: #fb923c;
  --brand-strong: #f97316;
  --brand-soft: #3a2410;
  --gold: #d4a72c;
  --danger: #f87171;
  --danger-soft: #2b1414;
  --ok: #4ade80;
  --ok-soft: #10241a;
  --warn: #fbbf24;
  --warn-soft: #2a2010;
  --shadow: 0 1px 2px rgba(0,0,0,0.4), 0 8px 24px rgba(0,0,0,0.35);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: 'Prompt', 'Noto Sans Thai', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
button { font-family: inherit; }
input, select, textarea { font-family: inherit; }

/* ---- App shell ---- */
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}
.sidebar-brand { display: flex; align-items: center; gap: 12px; padding: 20px 18px; border-bottom: 1px solid var(--border); }
.brand-logo { font-size: 28px; }
.brand-name { font-weight: 700; font-size: 16px; }
.brand-sub { font-size: 12px; color: var(--text-muted); }
.sidebar-nav { flex: 1; overflow-y: auto; padding: 10px; display: flex; flex-direction: column; gap: 2px; }
.nav-link {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 12px; border-radius: 10px;
  color: var(--text); font-size: 14.5px; font-weight: 500;
  min-height: 44px;
}
.nav-link:hover { background: var(--surface-2); }
.nav-link.active { background: var(--brand-soft); color: var(--brand-strong); font-weight: 600; }
.nav-icon { width: 20px; height: 20px; flex-shrink: 0; }
.sidebar-footer { padding: 12px; border-top: 1px solid var(--border); }
.theme-toggle {
  width: 100%; display: flex; align-items: center; justify-content: center; gap: 8px;
  padding: 10px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2);
  color: var(--text); cursor: pointer; min-height: 44px; font-size: 14px;
}
:root[data-theme="light"] .theme-icon-dark, :root:not([data-theme="dark"]) .theme-icon-dark { display: none; }
:root[data-theme="dark"] .theme-icon-light { display: none; }

.main-col { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 14px 20px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.topbar-title { font-weight: 600; font-size: 17px; flex: 1; }
.topbar-user { display: flex; align-items: center; gap: 10px; }
.user-name { font-size: 14px; font-weight: 500; }
.menu-btn { display: none; }
.icon-btn {
  width: 40px; height: 40px; display: inline-flex; align-items: center; justify-content: center;
  border-radius: 10px; border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer;
}
.content { padding: 20px; padding-bottom: 90px; max-width: 1400px; width: 100%; margin: 0 auto; }

/* ---- Drawer (mobile) ---- */
.drawer {
  position: fixed; top: 0; left: 0; height: 100vh; width: 280px; max-width: 85vw;
  background: var(--surface); z-index: 60; transform: translateX(-100%);
  transition: transform 0.22s ease; display: flex; flex-direction: column; box-shadow: var(--shadow);
}
.drawer.open { transform: translateX(0); }
.drawer-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 55; opacity: 0; pointer-events: none; transition: opacity 0.2s;
}
.drawer-backdrop.open { opacity: 1; pointer-events: auto; }
.drawer-header { display: flex; align-items: center; justify-content: space-between; padding: 16px; border-bottom: 1px solid var(--border); }
.drawer .sidebar-nav { padding: 10px; }
.drawer-footer, .drawer { gap: 8px; }
.drawer .theme-toggle, .drawer .btn { margin: 6px 12px; width: calc(100% - 24px); }

/* ---- Bottom nav (mobile) ---- */
.bottom-nav {
  display: none; position: fixed; bottom: 0; left: 0; right: 0; z-index: 40;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  justify-content: space-around;
}
.bottom-link {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  font-size: 11px; color: var(--text-muted); padding: 6px 8px; border-radius: 10px; border: none; background: none; min-width: 56px;
}
.bottom-link.active { color: var(--brand-strong); }
.bottom-icon { width: 22px; height: 22px; }

@media (max-width: 900px) {
  .sidebar { display: none; }
  .menu-btn { display: inline-flex; }
  .bottom-nav { display: flex; }
  .content { padding: 14px; padding-bottom: 100px; }
  .topbar { padding: 12px 14px; }
  .user-name { display: none; }
}

/* ---- Buttons ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 18px; border-radius: 10px; font-size: 14.5px; font-weight: 600;
  border: 1px solid transparent; cursor: pointer; transition: filter 0.15s, opacity 0.15s;
}
.btn:disabled { opacity: 0.6; cursor: not-allowed; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover:not(:disabled) { filter: brightness(0.94); }
.btn-outline { background: transparent; border-color: var(--border); color: var(--text); }
.btn-outline:hover:not(:disabled) { background: var(--surface-2); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { filter: brightness(0.94); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-sm { min-height: 36px; padding: 0 12px; font-size: 13px; }
.w-full { width: 100%; }

/* ---- Forms ---- */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field-label { font-size: 13.5px; font-weight: 600; color: var(--text-muted); }
.field-hint { font-size: 12px; color: var(--text-muted); }
input[type="text"], input[type="email"], input[type="password"], input[type="number"], input[type="date"], input[type="tel"], input[type="search"], select, textarea {
  min-height: 44px; padding: 0 14px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: 15px; width: 100%;
}
textarea { min-height: 88px; padding: 10px 14px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: 1px; }
input:disabled, select:disabled { opacity: 0.6; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0 16px; }
.form-error {
  background: var(--danger-soft); color: var(--danger); border: 1px solid var(--danger);
  border-radius: 10px; padding: 10px 14px; font-size: 14px; margin-bottom: 12px;
}
.form-success {
  background: var(--ok-soft); color: var(--ok); border: 1px solid var(--ok);
  border-radius: 10px; padding: 10px 14px; font-size: 14px; margin-bottom: 12px;
}

/* ---- Card / table ---- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); margin-bottom: 16px; }
.card-title { font-weight: 600; font-size: 16px; margin: 0 0 12px; }
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--border); border-radius: var(--radius); }
table { border-collapse: collapse; width: 100%; min-width: 640px; background: var(--surface); }
th, td { text-align: left; padding: 11px 14px; border-bottom: 1px solid var(--border); font-size: 14px; white-space: nowrap; }
th { color: var(--text-muted); font-weight: 600; font-size: 13px; background: var(--surface-2); position: sticky; top: 0; }
tbody tr:hover { background: var(--surface-2); }
td.wrap, th.wrap { white-space: normal; }

.badge { display: inline-flex; align-items: center; padding: 3px 10px; border-radius: 999px; font-size: 12.5px; font-weight: 600; }
.badge-role { background: var(--brand-soft); color: var(--brand-strong); }
.badge-ok { background: var(--ok-soft); color: var(--ok); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-muted { background: var(--surface-2); color: var(--text-muted); }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; margin-bottom: 16px; }
.stat-card { position: relative; overflow: hidden; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 16px 16px; box-shadow: var(--shadow); }
.stat-card::before { content: ""; position: absolute; top: 0; left: 0; right: 0; height: 3px; background: var(--border); }
.stat-label { font-size: 13px; color: var(--text-muted); margin-bottom: 6px; }
.stat-value { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-sub { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

/* โทนสีการ์ดสรุปยอด — สื่อความหมายของตัวเลข ไม่ใช่แค่ตกแต่ง (ยอดขาย=สีหลักร้าน, กำไร/สินค้าใกล้หมด-หมด=สถานะ, ต้นทุน/มูลค่าสต๊อก=โทนทอง) */
.stat-card--brand::before { background: var(--brand); }
.stat-card--brand .stat-value { color: var(--brand); }
.stat-card--ok::before { background: var(--ok); }
.stat-card--ok .stat-value { color: var(--ok); }
.stat-card--warn::before { background: var(--warn); }
.stat-card--warn .stat-value { color: var(--warn); }
.stat-card--danger::before { background: var(--danger); }
.stat-card--danger .stat-value { color: var(--danger); }
.stat-card--gold::before { background: var(--gold); }
.stat-card--gold .stat-value { color: var(--gold); }
.stat-card--neutral::before { background: var(--border); }

/* ---- States ---- */
.spinner {
  width: 18px; height: 18px; border-radius: 50%; border: 2.5px solid rgba(255,255,255,0.4); border-top-color: #fff;
  animation: spin 0.7s linear infinite; display: inline-block;
}
.spinner[hidden] { display: none; }
.btn-outline .spinner, .btn-ghost .spinner { border-color: rgba(0,0,0,0.15); border-top-color: var(--text); }
:root[data-theme="dark"] .btn-outline .spinner, :root[data-theme="dark"] .btn-ghost .spinner { border-color: rgba(255,255,255,0.2); border-top-color: var(--text); }
@keyframes spin { to { transform: rotate(360deg); } }
.empty-state { text-align: center; padding: 48px 20px; color: var(--text-muted); }
.empty-state-icon { font-size: 40px; margin-bottom: 10px; }
.loading-row { text-align: center; padding: 30px; color: var(--text-muted); }

.toast-root { position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; align-items: center; width: 92%; max-width: 420px; }
@media (min-width: 901px) { .toast-root { bottom: 24px; } }
.toast {
  padding: 12px 18px; border-radius: 10px; font-size: 14px; box-shadow: var(--shadow); width: 100%; text-align: center;
  animation: toast-in 0.18s ease;
}
.toast-success { background: var(--ok); color: #fff; }
.toast-error { background: var(--danger); color: #fff; }
.toast-info { background: var(--text); color: var(--bg); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ---- Login ---- */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: linear-gradient(160deg, var(--bg), var(--surface-2)); padding: 20px; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 36px 28px; width: 100%; max-width: 380px; box-shadow: var(--shadow); text-align: center; }
.login-logo { font-size: 44px; margin-bottom: 8px; }
.login-title { margin: 0 0 4px; font-size: 22px; }
.login-sub { color: var(--text-muted); font-size: 13.5px; margin: 0 0 24px; }
.login-form { text-align: left; }

/* ---- Utility ---- */
.flex { display: flex; }
.flex-wrap { flex-wrap: wrap; }
.gap-8 { gap: 8px; }
.gap-12 { gap: 12px; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.mt-0 { margin-top: 0; }
.text-muted { color: var(--text-muted); }
.compare-bar-track { margin-top: 4px; height: 5px; border-radius: 3px; background: var(--border, #e5e5e5); overflow: hidden; min-width: 90px; }
.compare-bar-fill { height: 100%; background: var(--brand); border-radius: 3px; }

/* ช่องเลือกหลายรายการแบบติ๊กเช็คบ็อกซ์ในกล่อง dropdown (ใช้ในตัวกรองแดชบอร์ด) */
.msf { position: relative; min-width: 200px; }
.msf-btn {
  min-height: 44px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: 14.5px; width: 100%;
  display: flex; align-items: center; justify-content: space-between; gap: 8px; cursor: pointer; text-align: left;
}
.msf-btn:hover { background: var(--surface-2); }
.msf-btn[aria-expanded="true"] { outline: 2px solid var(--brand); outline-offset: 1px; }
.msf-btn-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msf-caret { color: var(--text-muted); font-size: 11px; flex: none; }
.msf-panel {
  position: absolute; top: calc(100% + 6px); left: 0; z-index: 40;
  width: max(100%, 260px); background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow); padding: 10px;
}
.msf-search { margin-bottom: 8px; min-height: 38px; }
.msf-actions { display: flex; gap: 14px; margin-bottom: 6px; padding: 0 2px; }
.msf-link { background: none; border: none; padding: 2px 0; font-size: 12.5px; font-weight: 600; color: var(--brand); cursor: pointer; }
.msf-link:hover { text-decoration: underline; }
.msf-list { max-height: 260px; overflow-y: auto; }
.msf-option { display: flex; align-items: center; gap: 10px; padding: 8px 6px; border-radius: 8px; cursor: pointer; font-size: 14px; }
.msf-option:hover { background: var(--surface-2); }
.msf-option input[type="checkbox"] { width: 17px; height: 17px; flex: none; accent-color: var(--brand); }
.msf-empty { padding: 14px 6px; color: var(--text-muted); font-size: 13.5px; text-align: center; }
.text-danger { color: var(--danger); }
.text-right { text-align: right; }
.mono { font-variant-numeric: tabular-nums; }
.section-title { font-size: 18px; font-weight: 700; margin: 0 0 4px; }
.section-sub { color: var(--text-muted); font-size: 13.5px; margin: 0 0 16px; }
.filter-bar { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 16px; align-items: flex-end; }
.filter-bar .field { margin-bottom: 0; min-width: 150px; }
.pagination { display: flex; gap: 8px; align-items: center; justify-content: center; margin-top: 16px; flex-wrap: wrap; }
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 70; display: flex; align-items: flex-end; justify-content: center; }
@media (min-width: 700px) { .modal-backdrop { align-items: center; } }
.modal { background: var(--surface); border-radius: 16px 16px 0 0; width: 100%; max-width: 560px; max-height: 92vh; overflow-y: auto; padding: 22px; box-shadow: var(--shadow); }
@media (min-width: 700px) { .modal { border-radius: 16px; } }
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.modal-title { font-size: 17px; font-weight: 700; margin: 0; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 18px; flex-wrap: wrap; }

/* ---- Cart (POS) ---- */
.pos-layout { display: grid; grid-template-columns: 1.3fr 1fr; gap: 16px; align-items: start; }
@media (max-width: 1000px) { .pos-layout { grid-template-columns: 1fr; } }
.cart-item { display: grid; grid-template-columns: 1fr auto; gap: 6px 10px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.cart-item:last-child { border-bottom: none; }
.cart-item-name { font-weight: 600; font-size: 14.5px; }
.cart-item-meta { font-size: 12.5px; color: var(--text-muted); }
.cart-qty-input { width: 64px; min-height: 38px; text-align: center; }
.cart-price-input { width: 96px; min-height: 38px; text-align: right; }
.search-result { padding: 10px 12px; border-bottom: 1px solid var(--border); cursor: pointer; display: flex; justify-content: space-between; gap: 10px; align-items: center; }
.search-result:hover { background: var(--surface-2); }
.search-result-list { max-height: 340px; overflow-y: auto; border: 1px solid var(--border); border-radius: 10px; margin-top: 8px; }
.totals-row { display: flex; justify-content: space-between; padding: 6px 0; font-size: 14.5px; }
.totals-row.net { font-size: 19px; font-weight: 700; border-top: 1px solid var(--border); margin-top: 8px; padding-top: 12px; }

/* ---- Print receipt ---- */
.receipt-page { max-width: 210mm; margin: 20px auto; background: #fff; color: #111; }
.receipt-toolbar { max-width: 210mm; margin: 0 auto 12px; display: flex; gap: 10px; justify-content: flex-end; }
.receipt-sheet { background: #fff; color: #111; padding: 14mm 12mm; border: 1px solid #ddd; font-size: 10.5pt; }
.receipt-header { display: flex; gap: 14px; align-items: flex-start; border-bottom: 2px solid #111; padding-bottom: 10px; margin-bottom: 10px; }
.receipt-logo-box { width: 104px; height: 104px; border: 1px solid #ccc; display: flex; align-items: center; justify-content: center; flex-shrink: 0; padding: 6px; }
.receipt-logo-box img { max-width: 100%; max-height: 100%; object-fit: contain; }
.receipt-store-info { flex: 1; }
.receipt-store-name { font-size: 20pt; font-weight: 700; margin: 0 0 2px; }
.receipt-doc-title { text-align: right; }
.receipt-doc-title h2 { font-size: 16pt; margin: 0 0 4px; }
.receipt-meta-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 24px; margin-bottom: 10px; font-size: 10pt; }
.receipt-table { width: 100%; border-collapse: collapse; margin-bottom: 10px; }
.receipt-table th, .receipt-table td { border-bottom: 1px solid #ccc; padding: 5px 6px; font-size: 9.5pt; text-align: left; white-space: normal; }
.receipt-table th { background: #f2f2f2; }
.receipt-totals { margin-left: auto; width: 260px; }
.receipt-totals .totals-row { font-size: 10pt; padding: 3px 0; }
.receipt-totals .net { font-size: 13pt; }
.receipt-signatures { display: grid; grid-template-columns: 1fr 1fr; gap: 30px; margin-top: 40px; }
.receipt-sign-line { border-top: 1px solid #333; margin-top: 40px; padding-top: 6px; text-align: center; font-size: 9.5pt; }
.receipt-watermark { position: fixed; top: 40%; left: 10%; font-size: 60pt; color: rgba(220,38,38,0.25); transform: rotate(-25deg); font-weight: 800; z-index: 5; pointer-events: none; }
.receipt-void-note { border: 1.5px solid #dc2626; color: #dc2626; padding: 8px 12px; border-radius: 8px; margin-bottom: 10px; font-weight: 600; }

@media print {
  @page { size: A4; margin: 10mm; }
  body { background: #fff !important; }
  .app-shell, .receipt-toolbar, .toast-root { display: none !important; }
  .receipt-page { margin: 0; max-width: none; }
  .receipt-sheet { border: none; padding: 0; }
}

@media (max-width: 700px) {
  .receipt-header { flex-direction: column; }
  .receipt-doc-title { text-align: left; }
  .receipt-meta-grid { grid-template-columns: 1fr; }
}
