/* KCPOS shared styles.
   Building blocks (use these instead of inventing new ones):
     layout   .page-head (h1 + .actions) · .card · .stats > .stat · .toolbar · .tabs > .tab(.active) · .empty
     buttons  .btn + .btn-primary | .btn-danger | .btn-ghost, size .btn-sm
     forms    .form-grid > .field (.field-wide spans the row) · .check · .form-actions · .choices (radio pills)
     tables   .table-wrap > table.table · td.num for numbers · .muted
     picker   .picker > input + .picker-results (buttons with .name, or a .note)
     badges   .badge + .badge-ok | .badge-warn | .badge-danger | .badge-muted
*/
:root {
  /* Colours: the "ฟ้าโซลาร์" theme. Screens take every colour from here, except .dlg::backdrop below. The theme
     colour is also written in templates/base.html (theme-color), core/views.py (manifest), templates/pos/display.html,
     templates/customers/card.html and static/img/icon.svg with its icon-*.png; core/tests.py checks the first two. */
  --brand: #1f6fd1;
  --brand-dark: #17569f;
  --brand-soft: #e7f0fb;
  --brand-line: #c3d8f3;    /* border of a brand-soft box */
  --accent: #f59e0b;
  --ok: #14602c;            /* done / paid / in use: stays green whatever the theme */
  --ok-soft: #e6f4ea;
  --ink: #1b2533;
  --muted: #5a6a7f;
  --bg: #f2f6fa;
  --card: #ffffff;
  --tint: #f7f9fc;          /* table heads, hovered rows and buttons */
  --line: #d9e2ec;
  --line-soft: #e9eef4;     /* lines between rows */
  --line-strong: #b3c2d3;
  --field: #c6d2df;         /* input borders */
  --danger: #c62828;
  --danger-soft: #fdecea;
  --warn: #b45309;
  --warn-soft: #fff4e0;
  --side: #0f2a47;
  --side-ink: #dce8f5;
  --side-muted: #a3b8cf;
  --side-label: #8fa9c4;
  --shade: 15, 42, 71;      /* --side as r, g, b, for see-through shadows */
  --radius: 10px;
  --shadow: 0 1px 2px rgba(var(--shade), 0.06), 0 2px 8px rgba(var(--shade), 0.05);
  --font: -apple-system, "Segoe UI", "Leelawadee UI", Thonburi, "Noto Sans Thai", Tahoma, sans-serif;
}

* { box-sizing: border-box; }
/* The hidden attribute must win over component rules that set display (grid, flex, ...). */
[hidden] { display: none !important; }
html { font-size: 16px; }
body { margin: 0; font-family: var(--font); color: var(--ink); background: var(--bg); line-height: 1.5; }
a { color: var(--brand-dark); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.nowrap { white-space: nowrap; }

/* ---- app shell ---- */
.app { display: grid; grid-template-columns: 232px minmax(0, 1fr); min-height: 100vh; }
.sidebar { background: var(--side); color: var(--side-ink); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { display: block; padding: 16px 16px 12px; color: #fff; font-weight: 700; font-size: 1.25rem; letter-spacing: 0.04em; }
.brand:hover { text-decoration: none; }
.shop-name { padding: 0 16px 12px; font-size: 0.8rem; color: var(--side-muted); border-bottom: 1px solid rgba(255, 255, 255, 0.08); }
.branch-switch { padding: 0 12px 12px; border-bottom: 1px solid rgba(255, 255, 255, 0.08); }
.branch-switch select { width: 100%; background: rgba(255, 255, 255, 0.08); color: #fff; border: 1px solid rgba(255, 255, 255, 0.2); border-radius: 6px; padding: 6px 8px; font: inherit; font-size: 0.85rem; }
.nav { padding: 8px; flex: 1; }
.nav-group { margin: 12px 8px 4px; font-size: 0.72rem; letter-spacing: 0.04em; color: var(--side-label); }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px; color: var(--side-ink); }
.nav a:hover { background: rgba(255, 255, 255, 0.07); text-decoration: none; }
.nav a.active { background: var(--brand); color: #fff; font-weight: 600; }
.nav a.nav-pos { background: var(--accent); color: #2b1d00; font-weight: 700; justify-content: center; margin-bottom: 4px; }
.nav a.nav-pos:hover { filter: brightness(1.05); }
.side-user { padding: 12px 16px; border-top: 1px solid rgba(255, 255, 255, 0.08); font-size: 0.875rem; }
.side-user form { margin-top: 6px; }
.side-user button { background: none; border: 0; padding: 0; color: var(--side-muted); cursor: pointer; font: inherit; }
.side-user button:hover { color: #fff; text-decoration: underline; }
.side-user .side-role { color: var(--side-muted); }
.side-user .side-link { display: inline-block; margin-top: 6px; color: var(--side-muted); }
.side-user .side-link:hover { color: #fff; }
.main { padding: 22px 26px 40px; min-width: 0; }
.btn.menu-toggle { display: none; }

@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; z-index: 40; width: 250px; transform: translateX(-100%); transition: transform 0.18s; }
  body.menu-open .sidebar { transform: none; box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.35); }
  .main { padding: 14px 14px 32px; }
  .btn.menu-toggle { display: inline-flex; margin-bottom: 10px; }
}

/* ---- page pieces ---- */
.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.page-head .actions { display: flex; flex-wrap: wrap; gap: 8px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 16px; }
.card > h2 { margin-bottom: 12px; }
/* Product list: the category → brand tree beside the table (stacks on narrow screens) */
.with-tree { display: grid; grid-template-columns: 240px minmax(0, 1fr); gap: 16px; align-items: start; }
.tree { padding: 10px 8px; font-size: 0.925rem; }
.tree a { display: flex; justify-content: space-between; gap: 8px; padding: 5px 8px; border-radius: 6px; color: var(--ink); }
.tree a:hover { background: var(--brand-soft); text-decoration: none; }
.tree a.active { background: var(--brand); color: #fff; font-weight: 600; }
.tree a.active .count { color: inherit; }
.tree .tree-root { font-weight: 600; margin-bottom: 4px; }
.tree summary { display: flex; align-items: center; gap: 4px; cursor: pointer; list-style: none; }
.tree summary::before { content: "▸"; width: 14px; color: var(--muted); font-size: 0.8em; }
.tree details[open] > summary::before { content: "▾"; }
.tree summary a { flex: 1; }
.tree summary > .count { padding-right: 8px; }
.tree .tree-leaf { margin-left: 18px; }
.tree .count { color: var(--muted); font-size: 0.8em; }
@media (max-width: 860px) { .with-tree { grid-template-columns: 1fr; } }
.card.flush { padding: 0; overflow: hidden; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat .label { color: var(--muted); font-size: 0.85rem; }
.stat .value { font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat.good .value { color: var(--brand-dark); }
.stat.bad .value { color: var(--danger); }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.toolbar .grow { flex: 1 1 220px; }
.tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.tab { padding: 7px 14px; border-radius: 999px; background: var(--card); border: 1px solid var(--line); color: var(--ink); font-size: 0.92rem; }
.tab:hover { text-decoration: none; border-color: var(--brand); }
.tab.active { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 600; }
.empty { text-align: center; color: var(--muted); padding: 36px 12px; }

/* ---- buttons ---- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 9px 16px; border-radius: 8px; border: 1px solid var(--line); background: #fff; color: var(--ink); font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap; }
.btn:hover { text-decoration: none; border-color: var(--line-strong); background: var(--tint); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); border-color: var(--brand-dark); }
.btn-danger { background: #fff; border-color: #e6b3b0; color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-sm { padding: 5px 10px; font-size: 0.875rem; }

/* ---- forms ---- */
input[type="text"], input[type="number"], input[type="password"], input[type="email"], input[type="date"], input[type="search"], input[type="tel"], input[type="file"], select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--field); border-radius: 8px; background: #fff; color: var(--ink); font: inherit;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; border-color: var(--brand); }
textarea { min-height: 84px; resize: vertical; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 18px; }
.field label { display: block; font-weight: 600; font-size: 0.92rem; margin-bottom: 4px; }
.field .help { color: var(--muted); font-size: 0.82rem; margin-top: 3px; }
.field .error, .errorlist { color: var(--danger); font-size: 0.85rem; margin: 3px 0 0; padding: 0; list-style: none; }
.field.has-error input, .field.has-error select, .field.has-error textarea { border-color: var(--danger); }
.field-wide { grid-column: 1 / -1; }
.check { display: flex; gap: 9px; align-items: flex-start; }
.check input { width: 18px; height: 18px; margin-top: 3px; flex: none; }
.check-row { display: flex; flex-wrap: wrap; gap: 6px 18px; }
.bundle-box { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line); }
.bundle-box .table input { width: 100%; }
.bundle-box .table input.num { text-align: right; }
.bundle-box h2 { margin: 0 0 4px; }
.print-only { display: none; }
.check-row .check { align-items: center; }
.check label { margin: 0; font-weight: 600; }
.form-actions { display: flex; gap: 8px; margin-top: 18px; }
@media (max-width: 640px) { .form-grid { grid-template-columns: 1fr; } }
/* Radio choices drawn as pills (.field > .choices), and the customer form's tabs and address blocks */
.field > .label { display: block; font-weight: 600; font-size: 0.92rem; margin-bottom: 6px; }
.choices { display: flex; flex-wrap: wrap; gap: 6px; }
.field .choices label { display: inline-flex; align-items: center; margin: 0; padding: 7px 14px; border-radius: 999px; border: 1px solid var(--field); background: #fff; font-weight: 400; cursor: pointer; }
.choices input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.field .choices label:has(input:checked) { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 600; }
.choices label:has(input:focus-visible) { outline: 2px solid var(--brand); outline-offset: 2px; }
.choices label:has(input:disabled) { cursor: default; }
button.tab { font: inherit; cursor: pointer; }
.tab.tab-error { border-color: var(--danger); }
.form-section { margin: 22px 0 10px; font-size: 1.05rem; }
.form-section:first-child { margin-top: 4px; }
.paste-box { display: grid; gap: 8px; padding: 12px 14px; margin-bottom: 14px; border-radius: 8px; background: var(--bg); border: 1px solid var(--line); }
.paste-box textarea { min-height: 60px; }
.address-preview { margin-top: 12px; padding: 10px 12px; border-radius: 8px; background: var(--brand-soft); }

/* ---- tables ---- */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 0.94rem; }
.table th { text-align: left; font-weight: 600; color: var(--muted); font-size: 0.82rem; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; background: var(--tint); }
.table td { padding: 10px 12px; border-bottom: 1px solid var(--line-soft); vertical-align: top; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: var(--tint); }
.table .num, .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table tfoot td { font-weight: 700; border-top: 2px solid var(--line); background: var(--tint); }
.row-void td { color: var(--muted); text-decoration: line-through; }
.row-void td.keep { text-decoration: none; }

/* ---- product picker (type-ahead search that adds rows to a document) ---- */
.picker { position: relative; }
.picker-results { position: absolute; z-index: 20; top: 100%; left: 0; right: 0; margin-top: 4px; max-height: 340px; overflow-y: auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 8px 24px rgba(var(--shade), 0.18); }
.picker-results button { display: flex; gap: 12px; align-items: baseline; width: 100%; padding: 9px 12px; border: 0; border-bottom: 1px solid var(--line); background: none; color: inherit; font: inherit; text-align: left; cursor: pointer; }
.picker-results button:last-child { border-bottom: 0; }
.picker-results button:hover, .picker-results button.active { background: var(--brand-soft); }
.picker-results .name { flex: 1; }
.picker-results .note { padding: 12px; color: var(--muted); }

/* ---- bar chart of days (home page, reports) ---- */
.week { display: grid; grid-template-columns: repeat(7, 1fr); gap: 10px; align-items: end; height: 170px; }
.week .col { display: flex; flex-direction: column; justify-content: flex-end; align-items: center; height: 100%; gap: 4px; min-width: 0; }
.week .bar { width: 100%; max-width: 54px; background: var(--brand); border-radius: 6px 6px 0 0; min-height: 3px; }
.week .col.today .bar { background: var(--accent); }
.week .amount { font-size: 0.75rem; font-variant-numeric: tabular-nums; color: var(--muted); white-space: nowrap; }
.week .day { font-size: 0.8rem; color: var(--muted); white-space: nowrap; }

/* ---- badges, messages, pagination ---- */
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 0.78rem; font-weight: 600; white-space: nowrap; }
.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(--line-soft); color: var(--muted); }
.messages { list-style: none; padding: 0; margin: 0 0 14px; display: grid; gap: 8px; }
.messages li { padding: 10px 14px; border-radius: 8px; background: var(--brand-soft); color: var(--brand-dark); border: 1px solid var(--brand-line); }
.messages li.error { background: var(--danger-soft); color: var(--danger); border-color: #f0c2bf; }
.messages li.warning { background: var(--warn-soft); color: var(--warn); border-color: #f1d9a8; }
.pagination { display: flex; align-items: center; gap: 8px; justify-content: flex-end; padding: 12px; font-size: 0.9rem; }

/* ---- dialogs (<dialog class="dlg">; the POS adds its variants in pos.css) ---- */
.dlg { border: 0; border-radius: 14px; padding: 22px; width: min(560px, 94vw); max-height: 92vh; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3); color: var(--ink); }
.dlg::backdrop { background: rgba(15, 42, 71, 0.55); } /* --side written out: browsers before 2024 give ::backdrop no variables */
.dlg h2 { margin-bottom: 14px; }
.dlg-confirm { width: min(440px, 94vw); }
.dlg-confirm h2 { margin: 0 0 6px; font-size: 1.15rem; }
.dlg-confirm p { margin: 0; color: var(--muted); white-space: pre-line; }
.dlg-confirm input { margin-top: 12px; }
.confirm-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.confirm-actions .btn { min-width: 96px; padding: 10px 18px; }
.dlg-confirm .btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }

/* ---- sign-in ---- */
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: linear-gradient(160deg, var(--side) 0%, var(--brand) 100%); }
.login-card { width: 100%; max-width: 380px; background: #fff; border-radius: 14px; padding: 28px; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25); }
.login-card .logo { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; }
.login-card .logo img { width: 52px; height: 52px; border-radius: 12px; }
.login-card .field { margin-bottom: 12px; }
.login-card .btn { width: 100%; margin-top: 6px; padding: 11px; }
/* two-step sign-in (accounts/two_factor.html, registration/two_factor.html) */
.two-factor-steps { padding-left: 20px; margin: 8px 0; }
.two-factor-qr svg { display: block; width: 200px; height: 200px; margin: 8px 0; }
.two-factor-secret { word-break: break-all; }
.recovery-codes { display: grid; grid-template-columns: repeat(2, max-content); gap: 6px 24px; margin: 12px 0; font-size: 1.15rem; }

@media print {
  .sidebar, .menu-toggle, .no-print, .messages { display: none !important; }
  .print-only { display: flex !important; }
  .app { display: block; }
  .main { padding: 0; }
  body { background: #fff; }
  .card { box-shadow: none; border: 0; padding: 0; }
}

/* "วางข้อความที่ลูกค้าส่งมา" (templates/customers/_paste_customer.html) */
.paste-details { margin-bottom: 14px; }
.paste-details > summary { cursor: pointer; font-weight: 600; color: var(--brand-dark); padding: 2px 0; }
.paste-details > .paste-box { margin: 8px 0 0; }
.paste-result { display: grid; gap: 4px; }
.paste-result .warn { color: var(--warn); }
.paste-result ul { margin: 2px 0 0; padding-left: 20px; }
