:root {
  --primary: #c2571b;
  --primary-dark: #9a4415;
  --app-bg: #faf6f2;
  --sidebar-bg: #241f1a;
  --card-bg: #ffffff;
  --card-border: #e7ded3;
  --text-main: #2b2420;
  --text-secondary: #8a7f74;
  --text-muted: #a89c8f;
  --green: #2f7d4f;
  --red: #b3392c;
  --blue-gray: #3d5a80;
  --row-border: #f0e9de;
  --thead-bg: #f3ede4;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--app-bg);
  color: var(--text-main);
  font-family: 'Prompt', 'Noto Sans Thai', sans-serif;
}

input, select, button, textarea {
  font-family: 'Prompt', 'Noto Sans Thai', sans-serif;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary) !important;
  box-shadow: none !important;
}

a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-dark); }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #e0d5c8; border-radius: 6px; }

/* ---------- Content ---------- */
.page-title { font-size: 22px; font-weight: 700; margin-bottom: 4px; }
.page-desc { font-size: 14px; color: var(--text-secondary); margin-bottom: 22px; }

/* ---------- Cards / panels ---------- */
.card-panel {
  background: var(--card-bg); border: 1px solid var(--card-border); border-radius: 16px; padding: 20px;
}
.card-panel-title { font-size: 14px; font-weight: 600; margin-bottom: 12px; }

.menu-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; max-width: 960px; }
.menu-card {
  background: var(--card-bg); border: 1px solid var(--card-border); border-radius: 18px; padding: 26px;
  cursor: pointer; display: flex; flex-direction: column; gap: 14px; transition: box-shadow .15s;
  text-decoration: none; color: inherit;
}
.menu-card:hover { box-shadow: 0 12px 30px rgba(43,36,32,0.12); color: inherit; }
.menu-icon { width: 52px; height: 52px; border-radius: 14px; display: flex; align-items: center; justify-content: center; }
.menu-card-title { font-size: 17px; font-weight: 600; }
.menu-card-desc { font-size: 13px; color: var(--text-secondary); margin-top: 2px; }

.stat-card {
  background: var(--card-bg); border: 1px solid var(--card-border); border-radius: 14px; padding: 18px;
}
.stat-card.pad-lg { padding: 20px; }
.stat-label { font-size: 12.5px; color: var(--text-secondary); }
.stat-value { font-size: 24px; font-weight: 700; margin-top: 6px; }
.stat-value.lg { font-size: 26px; margin-top: 8px; }
.stat-sub { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

/* ---------- Forms ---------- */
.form-label-sm { font-size: 12.5px; color: var(--text-secondary); display: block; margin-bottom: 4px; }
.form-control-custom, select.form-control-custom {
  width: 100%; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--card-border);
  font-size: 14px; background: #fff; color: var(--text-main);
}
.form-control-custom:disabled { background: var(--app-bg); color: #6b6058; }

.seg-btn {
  padding: 10px 18px; border-radius: 10px; border: 1px solid var(--card-border); background: #fff;
  color: #6b6058; font-weight: 600; font-size: 13.5px; cursor: pointer;
}
.seg-btn.active { border-color: var(--primary); background: var(--primary); color: #fff; }

.tab-btn {
  padding: 9px 16px; border-radius: 10px; border: 1px solid var(--card-border); background: #fff;
  color: #6b6058; font-weight: 600; font-size: 13px; cursor: pointer; text-decoration: none; display: inline-block;
}
.tab-btn.active { border-color: var(--primary); background: var(--primary); color: #fff; }
.tab-btn:hover { color: inherit; }
.tab-btn.active:hover { color: #fff; }

.line-total-box {
  display: flex; align-items: center; justify-content: space-between; background: var(--app-bg);
  border-radius: 10px; padding: 12px 14px; margin-top: 2px;
}
.line-total-value { font-size: 16px; font-weight: 700; }

.btn-dashed-add {
  margin-top: 4px; padding: 11px; border-radius: 10px; border: 1px dashed var(--primary); background: #fff;
  color: var(--primary); font-weight: 600; font-size: 14px; cursor: pointer; display: flex;
  align-items: center; justify-content: center; gap: 8px; width: 100%;
}
.btn-dashed-add:hover { background: #fff8f3; }

.btn-solid { padding: 13px; border-radius: 10px; border: none; color: #fff; font-weight: 600; font-size: 14.5px; cursor: pointer; }
.btn-solid.bg-green { background: var(--green); }
.btn-solid.bg-red { background: var(--red); }
.btn-solid.bg-blue { background: var(--blue-gray); }
.btn-solid.bg-primary { background: var(--primary); }
.btn-solid:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- Tables ---------- */
.table-custom { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.table-custom thead tr { background: var(--thead-bg); text-align: left; }
.table-custom th { padding: 10px 12px; font-weight: 600; color: #6b6058; }
.table-custom td { padding: 10px 12px; }
.table-custom tbody tr { border-bottom: 1px solid var(--row-border); }
.table-empty { text-align: center; color: var(--text-muted); font-size: 13px; padding: 40px 0; }
.remove-link { color: var(--red); cursor: pointer; background: none; border: none; }

/* ---------- Toast ---------- */
.app-toast {
  position: fixed; top: 14px; right: 36px; background: var(--green); color: #fff; padding: 10px 18px;
  border-radius: 10px; font-size: 13px; font-weight: 500; box-shadow: 0 8px 20px rgba(0,0,0,0.15); z-index: 1080;
  transition: opacity .3s ease;
}
.app-toast.error { background: var(--red); }

/* ---------- Modal (bill receipt / invoice) ---------- */
.invoice-dialog { max-width: 660px; margin: 1.75rem auto; }
.invoice-content { border-radius: 14px; overflow: hidden; display: flex; flex-direction: column; }

/* The whole document is one <table> so the header (thead) repeats on every
   printed page when the item list spans more than one A4 sheet. */
.invoice-doc-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.invoice-doc-table thead { display: table-header-group; }
.invoice-doc-table .invoice-block-row > td { padding: 0; border: none; }
.invoice-doc-table tr, .invoice-doc-table .invoice-block-row { page-break-inside: avoid; break-inside: avoid; }

.invoice-header { display: flex; justify-content: space-between; gap: 20px; padding: 26px 30px 18px; border-bottom: 2px solid var(--card-border); }
.invoice-shop-name { font-size: 18px; font-weight: 700; }
.invoice-shop-line { font-size: 12px; color: var(--text-secondary); margin-top: 3px; line-height: 1.5; }
.invoice-doc-block { text-align: right; flex: none; }
.invoice-doc-title { font-size: 15px; font-weight: 700; color: var(--primary); }
.invoice-doc-sub { font-size: 11.5px; color: var(--text-secondary); margin-top: 2px; }
.invoice-doc-meta { margin-top: 10px; margin-left: auto; border-collapse: collapse; font-size: 12px; }
.invoice-doc-meta td { padding: 1px 0; }
.invoice-doc-meta td:first-child { color: var(--text-secondary); padding-right: 10px; }
.invoice-doc-meta td:last-child { font-weight: 600; text-align: right; }

.invoice-billto { padding: 14px 30px; border-bottom: 1px dashed var(--card-border); background: var(--app-bg); }
.invoice-billto-label { font-size: 11px; color: var(--text-secondary); text-transform: uppercase; letter-spacing: .04em; }
.invoice-billto-name { font-size: 14.5px; font-weight: 700; margin-top: 4px; }
.invoice-billto-line { font-size: 12px; color: #6b6058; margin-top: 2px; }

.invoice-body { padding: 18px 30px 10px; }

.invoice-colhead td { background: var(--thead-bg); padding: 9px 10px; font-weight: 600; color: #6b6058; border-bottom: 2px solid var(--card-border); text-align: left; }
.invoice-doc-table tbody tr:not(.invoice-block-row):not(.invoice-colhead) td { padding: 9px 10px; border-bottom: 1px solid var(--row-border); }
.invoice-doc-table .col-no { width: 44px; text-align: center; }
.invoice-doc-table .col-qty { width: 90px; text-align: center; }
.invoice-doc-table .col-money { width: 110px; text-align: right; }

.invoice-totals { margin-top: 4px; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.invoice-totals-line { display: flex; justify-content: space-between; gap: 30px; width: 100%; max-width: 300px; font-size: 13px; }
.invoice-totals-words { font-size: 12px; font-style: italic; color: var(--text-secondary); }
.invoice-totals-grand { display: flex; justify-content: space-between; gap: 30px; width: 100%; max-width: 300px; font-size: 16px; font-weight: 700; padding-top: 8px; border-top: 2px solid var(--card-border); }
.invoice-totals-qty { font-size: 12px; color: var(--text-secondary); margin-top: 2px; }

/* Contact-form + signature block: sits after the item table, and gets
   pushed down to the bottom of the printed page (see @media print below)
   instead of trailing right under the totals with a big gap under it. */
.invoice-page-footer { padding: 18px 30px 26px; margin-top: auto; }

.invoice-contact-form { margin-bottom: 34px; }
.invoice-contact-row { display: flex; align-items: baseline; gap: 10px; font-size: 12.5px; color: #6b6058; margin-bottom: 16px; flex-wrap: wrap; }
.invoice-contact-row:last-child { margin-bottom: 0; }
.invoice-fill-line { flex: 1; min-width: 80px; align-self: stretch; border-bottom: 1px dotted var(--text-muted); }
.invoice-fill-short { flex: 0 0 130px; }
.invoice-checkbox { display: inline-flex; align-items: center; gap: 6px; }
.invoice-checkbox-box { width: 12px; height: 12px; border: 1.4px solid var(--text-muted); border-radius: 2px; display: inline-block; }

.invoice-signatures { display: flex; justify-content: space-between; gap: 24px; }
.invoice-sign-box { flex: 1; text-align: center; }
.invoice-sign-line { border-top: 1px solid var(--text-muted); margin-bottom: 8px; }
.invoice-sign-label { font-size: 12.5px; font-weight: 600; }

.invoice-footer-note { margin-top: 26px; text-align: center; font-size: 11px; color: var(--text-muted); }

.invoice-actions { padding: 16px 30px 24px; display: flex; gap: 10px; border-top: 1px solid var(--card-border); }

/* ---------- Login ---------- */
.login-wrap {
  width: 100%; min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: radial-gradient(circle at 30% 20%, #fdf1e7 0%, #faf6f2 55%);
}
.login-card {
  width: 380px; background: #ffffff; border: 1px solid var(--card-border); border-radius: 20px;
  padding: 40px 36px; box-shadow: 0 12px 30px rgba(43,36,32,0.08); display: flex; flex-direction: column; gap: 18px;
}
.login-logo {
  width: 56px; height: 56px; border-radius: 14px; background: var(--primary); display: flex;
  align-items: center; justify-content: center; margin: 0 auto 6px;
}
.login-title { font-size: 20px; font-weight: 700; }
.login-sub { font-size: 13px; color: var(--text-secondary); margin-top: 4px; }
.login-help { text-align: center; font-size: 12px; color: var(--text-muted); }
.login-error { color: var(--red); font-size: 12.5px; text-align: center; }

/* ---------- Launcher (post-login hub) ---------- */
.launcher-shell { min-height: 100vh; display: flex; flex-direction: column; background: var(--app-bg); }

.launcher-topbar {
  display: flex; align-items: center; justify-content: space-between; padding: 20px 32px; flex: none;
}
.launcher-brand { display: flex; align-items: center; gap: 12px; }
.launcher-logo {
  width: 44px; height: 44px; border-radius: 13px; background: var(--primary);
  display: flex; align-items: center; justify-content: center; flex: none;
}
.launcher-brand-title { font-size: 15px; font-weight: 700; }
.launcher-brand-sub { font-size: 12px; color: var(--text-secondary); }

.launcher-user { display: flex; align-items: center; gap: 12px; }
.launcher-avatar {
  width: 40px; height: 40px; border-radius: 50%; background: var(--sidebar-bg); color: #fff;
  display: flex; align-items: center; justify-content: center; font-size: 15px; font-weight: 600; flex: none;
}
.launcher-user-name { font-size: 14px; font-weight: 600; }
.launcher-user-role { font-size: 12px; color: var(--text-secondary); }
.launcher-logout-btn {
  cursor: pointer; color: var(--text-secondary); background: #fff; border: 1px solid var(--card-border);
  border-radius: 10px; width: 38px; height: 38px; display: flex; align-items: center; justify-content: center;
  margin-left: 6px;
}
.launcher-logout-btn:hover { color: var(--red); border-color: var(--red); }

.launcher-body { flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px 32px; overflow-y: auto; }

.launcher-grid {
  width: 100%;
  display: flex; flex-wrap: wrap; justify-content: center; gap: clamp(22px, 3vmin, 34px);
}
.launcher-card {
  flex: 0 0 clamp(280px, 30vmin, 380px); aspect-ratio: 1 / 1;
  background: var(--card-bg); border: 1px solid var(--card-border); border-radius: 28px;
  padding: clamp(20px, 2.6vmin, 30px); display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: clamp(14px, 2vmin, 22px); text-align: center; text-decoration: none; color: inherit;
  transition: box-shadow .15s, transform .1s; -webkit-tap-highlight-color: transparent;
}
.launcher-card:hover { box-shadow: 0 16px 36px rgba(43,36,32,0.14); color: inherit; }
.launcher-card:active { transform: scale(0.97); }
.launcher-icon {
  width: clamp(76px, 10vmin, 140px); height: clamp(76px, 10vmin, 140px); border-radius: 22px;
  display: flex; align-items: center; justify-content: center; flex: none;
}
.launcher-icon svg { width: 44%; height: 44%; }
.launcher-title { font-size: clamp(19px, 2.4vmin, 27px); font-weight: 700; }
.launcher-desc { font-size: clamp(12.5px, 1.5vmin, 16px); color: var(--text-secondary); margin-top: 2px; line-height: 1.4; }

@media (max-width: 560px) {
  .launcher-topbar { padding: 16px 18px; }
  .launcher-body { padding: 16px 18px; }
  .launcher-user-name, .launcher-user-role { display: none; }
  .launcher-grid { gap: 14px; }
  .launcher-card { flex-basis: 150px; }
  .launcher-icon { width: 52px; height: 52px; }
  .launcher-title { font-size: 14px; }
  .launcher-desc { font-size: 10.5px; }
}

/* ---------- Workspace (sidebar-less work pages) ---------- */
.workspace-shell { height: 100vh; display: flex; flex-direction: column; background: var(--app-bg); }
.workspace-topbar {
  display: flex; align-items: center; justify-content: space-between; padding: 16px 32px;
  background: #fff; border-bottom: 1px solid var(--card-border); flex: none;
}
.workspace-back {
  display: flex; align-items: center; gap: 8px; color: var(--text-main); font-weight: 600; font-size: 14px;
  text-decoration: none; padding: 9px 16px; border-radius: 10px; border: 1px solid var(--card-border); background: #fff;
}
.workspace-back:hover { background: var(--app-bg); color: inherit; }
.workspace-user { display: flex; align-items: center; gap: 10px; }
.workspace-avatar {
  width: 36px; height: 36px; border-radius: 50%; background: var(--sidebar-bg); color: #fff;
  display: flex; align-items: center; justify-content: center; font-size: 14px; font-weight: 600; flex: none;
}
.workspace-user-name { font-size: 13.5px; font-weight: 600; }
.workspace-user-role { font-size: 11.5px; color: var(--text-secondary); }
.workspace-logout-btn {
  cursor: pointer; color: var(--text-secondary); background: #fff; border: 1px solid var(--card-border);
  border-radius: 10px; width: 34px; height: 34px; display: flex; align-items: center; justify-content: center; margin-left: 4px;
}
.workspace-logout-btn:hover { color: var(--red); border-color: var(--red); }
.workspace-content { flex: 1; min-height: 0; overflow-y: auto; padding: 24px 32px; position: relative; display: flex; flex-direction: column; }
.workspace-hub-center { flex: 1; display: flex; align-items: center; justify-content: center; }

@media (max-width: 560px) {
  .workspace-topbar { padding: 12px 16px; }
  .workspace-content { padding: 16px; }
  .workspace-user-name, .workspace-user-role { display: none; }
}

@page { size: A4; margin: 14mm; }

@media print {
  .no-print { display: none !important; }
  .workspace-topbar, .launcher-topbar { display: none !important; }
  .workspace-shell, .workspace-content, .launcher-shell, .launcher-body { all: unset; }

  /* Printing the receipt modal (พิมพ์บิล): show only #billModal, remove everything
     else from layout (display:none, not visibility:hidden) so no blank leading
     page is left behind, and un-fix its positioning so a long item list can
     paginate across multiple A4 pages instead of being clipped to one viewport. */
  body.printing-modal > *:not(#billModal) { display: none !important; }
  body.printing-modal .modal-backdrop { display: none !important; }

  body.printing-modal #billModal {
    position: static !important;
    display: block !important;
    height: auto !important;
    overflow: visible !important;
    padding: 0 !important;
  }
  body.printing-modal #billModal .invoice-dialog {
    max-width: 100% !important;
    width: 100% !important;
    margin: 0 !important;
  }
  body.printing-modal #billModal .invoice-content {
    border-radius: 0 !important;
    border: 0 !important;
    box-shadow: none !important;
    /* Fills one A4 page (297mm minus the @page margins) so invoice-page-footer's
       margin-top:auto pushes the contact/signature block to the page bottom
       instead of leaving a gap right under the totals on a short bill. */
    min-height: 267mm !important;
  }
  body.printing-modal #billModal .invoice-header,
  body.printing-modal #billModal .invoice-billto,
  body.printing-modal #billModal .invoice-body,
  body.printing-modal #billModal .invoice-page-footer {
    padding-left: 0 !important;
    padding-right: 0 !important;
  }
}
