:root {
  --bg: #f3f6f6;
  --surface: #ffffff;
  --text: #102a2a;
  --muted: #5b7272;
  --line: #dde6e6;
  --brand: #0f766e;
  --brand-dark: #115e59;
  --brand-soft: #e0f2f1;
  --danger: #c0392b;
  --danger-soft: #fdecea;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 42, 42, .06), 0 2px 8px rgba(16, 42, 42, .05);
  --khmer: "Noto Sans Khmer", "Khmer OS Siemreap", "Khmer OS", "Khmer UI", "Khmer Sangam MN";
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, var(--khmer), sans-serif;
}
/* Khmer script has stacked consonants (subscripts) and needs extra line height */
html[lang=km] body { line-height: 1.6; }
html[lang=km] .stat .label { text-transform: none; letter-spacing: 0; }
.alt-name { font-size: 12px; color: var(--muted); font-weight: 400; }
.lang-toggle { display: inline-flex; border: 1px solid rgba(127,127,127,.4); border-radius: 999px; overflow: hidden; }
.lang-toggle button, .sidebar .foot .lang-toggle button { border: 0; border-radius: 0; background: transparent; color: inherit; padding: 3px 10px; cursor: pointer; font-size: 12px; text-decoration: none !important; }
.lang-toggle button.on, .sidebar .foot .lang-toggle button.on { background: var(--brand); color: #fff; }
.sidebar .foot .lang-toggle button { color: #d7ecea; padding: 4px 12px; }
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 14px; }
h1, h2, h3 { margin: 0 0 12px; }
h2 { font-size: 20px; }
h3 { font-size: 15px; }
a { color: var(--brand); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.right { text-align: right; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.hidden { display: none !important; }

/* inputs & buttons */
input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: var(--brand); }
input[type=checkbox] { width: auto; }
label { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.field { margin-bottom: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 12px; }
.btn {
  border: 1px solid var(--line); background: #fff; padding: 8px 14px; border-radius: 8px; cursor: pointer;
  font-weight: 600; white-space: nowrap;
}
.btn:hover { background: var(--bg); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { color: var(--danger); border-color: #f0c3bd; }
.btn.danger:hover { background: var(--danger-soft); }
.btn.sm { padding: 4px 10px; font-size: 12px; }
.btn.lg { padding: 14px; font-size: 16px; width: 100%; }
.btn.active { background: var(--brand-soft); border-color: var(--brand); color: var(--brand-dark); }

/* layout */
.layout { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar { background: #0b3b38; color: #d7ecea; display: flex; flex-direction: column; padding: 16px 10px; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; padding: 4px 8px 18px; color: #fff; }
.brand .logo { width: 32px; height: 32px; border-radius: 8px; background: var(--brand); display: grid; place-items: center; font-size: 20px; }
.nav a { display: flex; gap: 10px; align-items: center; padding: 10px 12px; border-radius: 8px; color: inherit; text-decoration: none; margin-bottom: 2px; }
.nav a:hover { background: rgba(255,255,255,.07); }
.nav a.active { background: var(--brand); color: #fff; }
.sidebar .foot { margin-top: auto; font-size: 12px; padding: 8px; border-top: 1px solid rgba(255,255,255,.1); }
.sidebar .foot button { background: none; border: 0; color: #9fd3cd; cursor: pointer; padding: 4px 0; text-decoration: underline; }
.main { padding: 20px 24px; min-width: 0; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat .label { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.stat .value { font-size: 24px; font-weight: 700; margin-top: 6px; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.mb { margin-bottom: 16px; }

/* tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 12px; color: var(--muted); font-weight: 600; background: #f8fafa; position: sticky; top: 0; }
tr:hover td { background: #fafcfc; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* badges */
.badge { white-space: nowrap; display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 700; background: var(--bg); color: var(--muted); }
.badge.rx { background: #ede9fe; color: #6d28d9; }
.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); }

/* POS */
.pos { display: grid; grid-template-columns: 1fr 420px; gap: 16px; height: calc(100vh - 40px); }
.pos > .card { display: flex; flex-direction: column; min-height: 0; }
.search-big { font-size: 16px; padding: 12px 14px; }
.results { overflow-y: auto; margin-top: 12px; flex: 1; }
.result {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 8px; background: #fff; margin-bottom: 6px; cursor: pointer;
}
.result:hover:not(.disabled) { border-color: var(--brand); background: var(--brand-soft); }
.result.disabled { cursor: not-allowed; background: #fafafa; }
.result.disabled .name, .result.disabled .price { opacity: .55; }
.dual { display: grid; grid-template-columns: 1fr 90px; gap: 6px; }
.dual input[data-usd-for] { background: #f8fafa; }
.rate-chip { border: 1px solid #f5d48a; background: var(--warn-soft); color: var(--warn); border-radius: 999px; padding: 4px 10px; font-size: 12px; font-weight: 700; cursor: pointer; }
.rate-chip:disabled { cursor: default; }
.btn.usd { color: var(--ok); border-color: #b7e4c7; }
.badge.hot { background: #ffedd5; color: #c2410c; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: #fff; padding: 5px 10px; cursor: pointer; font-size: 12px; font-weight: 600; color: var(--muted); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--brand); color: #fff; }
td.rank { font-size: 18px; text-align: center; font-weight: 700; color: var(--muted); }
td.hl { font-weight: 800; color: var(--brand-dark); }
.trend { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.trend.up { background: var(--ok-soft); color: var(--ok); }
.trend.down { background: var(--danger-soft); color: var(--danger); }
.trend.new { background: #ffedd5; color: #c2410c; }
.trend.flat { background: var(--bg); color: var(--muted); }
.spark polyline { fill: none; stroke: var(--brand); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.brand .logo img, .login .logo img { width: 100%; height: 100%; object-fit: contain; border-radius: inherit; background: #fff; }
.brand .logo:has(img), .login .logo:has(img) { background: #fff; overflow: hidden; }
.logo-preview { width: 84px; height: 84px; flex: none; border-radius: 14px; border: 1px solid var(--line); display: grid; place-items: center;
  font-size: 40px; color: #fff; background: var(--brand); overflow: hidden; }
.logo-preview:has(img) { background: #fff; }
.logo-preview img { width: 100%; height: 100%; object-fit: contain; }
.receipt-logo { max-width: 120px; max-height: 70px; object-fit: contain; margin-bottom: 4px; }
.steps { margin: 0 0 10px; padding-left: 20px; }
.steps li { margin-bottom: 6px; }
.import-errors { background: var(--danger-soft); border: 1px solid #f0c3bd; border-radius: 10px; padding: 12px; color: var(--danger); }
.import-errors table td { padding: 4px 8px; border-color: #f0c3bd; color: var(--text); }
.import-ok { background: var(--ok-soft); border-radius: 10px; padding: 12px; }
.menu-btn { display: none; margin-left: auto; background: rgba(255,255,255,.1); color: #fff; border: 0; border-radius: 8px; font-size: 22px; width: 44px; height: 40px; cursor: pointer; position: relative; }
.menu-btn.warn::after { content: ''; position: absolute; top: 6px; right: 6px; width: 9px; height: 9px; border-radius: 50%; background: #ef4444; }
.weak-pw { background: #fee2e2; color: #991b1b; border-radius: 8px; padding: 8px; margin-bottom: 10px; font-size: 12px; font-weight: 600; }
.sidebar .foot .weak-pw button { color: #991b1b; text-decoration: underline; font-weight: 700; }
.together { border: 1px dashed #a78bfa; background: #faf5ff; border-radius: 10px; padding: 8px 10px; margin-top: 8px; font-size: 13px; }
.together.on { border-style: solid; border-color: #7c3aed; }
.together .line { display: flex; justify-content: space-between; margin-top: 4px; }
.together .line.grand { font-size: 20px; font-weight: 800; color: #6d28d9; }
.backdate { border: 1px dashed var(--line); border-radius: 10px; padding: 8px 10px; margin-bottom: 10px; color: var(--muted); font-size: 13px; }
.backdate.on { border: 2px solid var(--warn); background: var(--warn-soft); color: var(--warn); }
.backdate input[type=datetime-local] { font-size: 16px; font-weight: 700; }
.badge.online { background: #e0f2fe; color: #0369a1; }
.channel-seg { display: grid; grid-template-columns: 1fr 1fr; margin-bottom: 10px; width: 100%; }
.channel-seg button { padding: 8px; font-size: 13px; }
.channel-seg button[data-ch=online].on { background: #0369a1; }
.filter-bar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; padding: 10px 14px; }
.filter-bar select { width: auto; min-width: 180px; }
.mini-bar { height: 6px; background: var(--bg); border-radius: 3px; margin-top: 4px; overflow: hidden; }
.mini-bar > div { height: 100%; background: var(--brand); border-radius: 3px; }
.mini-bar.online > div { background: #0369a1; }
.add-line { background: #f0fdfa; border-color: #99f6e4; }
.line-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0 12px; }
.line-grid .span2 { grid-column: span 2; }
.inv-total { font-size: 16px; margin-right: 12px; }
.print-doc { font-family: system-ui, var(--khmer), sans-serif; color: #000; }
.print-doc h2 { font-size: 16px; }
@media (max-width: 900px) { .line-grid { grid-template-columns: 1fr 1fr; } }
.pos-head { border-radius: 10px; padding: 10px 14px; margin-bottom: 12px; }
.pos-head h2 { margin: 0; font-size: 18px; }
.pos-head.sale { background: #e0e7ff; color: #3730a3; }
.pos-head.compound { background: var(--brand-soft); color: var(--brand-dark); }
.total-edit { background: var(--warn-soft); border: 1px solid #f5d48a; border-radius: 10px; padding: 10px; }
.total-edit label { color: var(--warn); }
.total-edit input { font-weight: 800; font-size: 20px; }
.total-edit input.edited { border-color: var(--warn); background: #fff7e6; }
#btnPay:disabled { background: #9ca3af; border-color: #9ca3af; }
.pos-tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.pos-tabs button {
  border: 2px solid var(--line); background: #fff; border-radius: 10px; padding: 10px 12px; cursor: pointer;
  font-weight: 700; font-size: 15px; text-align: left;
}
.pos-tabs button span { display: block; font-size: 12px; font-weight: 400; color: var(--muted); }
.pos-tabs button.on[data-mode=counter] { border-color: var(--brand); background: var(--brand-soft); color: var(--brand-dark); }
.pos-tabs button.on[data-mode=main] { border-color: #6366f1; background: #e0e7ff; color: #3730a3; }
.open-btn { padding: 2px 8px; font-size: 11px; background: var(--warn-soft); border-color: #f5d48a; color: var(--warn); }
.badge.kit { background: #fce7f3; color: #be185d; }
.badge.main { background: #e0e7ff; color: #3730a3; }
.badge.sub { background: #fef3c7; color: #92400e; }
.btn.sub-btn { background: #fffbeb; border-color: #fcd34d; color: #92400e; font-weight: 600; }
.btn.sub-btn:disabled { opacity: .45; cursor: not-allowed; }
.kit-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 12px; }
.kit-card.inactive { opacity: .6; }
.kit-row { display: grid; grid-template-columns: 1fr 80px 28px; gap: 8px; align-items: center; margin-bottom: 6px; }
.open-card { background: var(--warn-soft); border-color: #f5d48a; box-shadow: none; margin-bottom: 12px; }
.result .info { flex: 1; min-width: 0; }
.result .name { font-weight: 700; }
.result .price { font-weight: 700; font-size: 15px; }
.cart-lines { overflow-y: auto; flex: 1; min-height: 120px; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); margin: 0 -16px; padding: 0 16px; }
.cart-line { display: grid; grid-template-columns: 1fr auto auto 24px; gap: 8px; align-items: center; padding: 8px 0; border-bottom: 1px dashed var(--line); }
.cart-line:last-child { border-bottom: 0; }
.qty { display: flex; align-items: center; gap: 2px; }
.qty input { width: 52px; text-align: center; padding: 4px; }
.qty button { width: 28px; height: 28px; border: 1px solid var(--line); background: #fff; border-radius: 6px; cursor: pointer; }
.x { border: 0; background: none; color: var(--danger); cursor: pointer; font-size: 18px; }
.totals { padding: 10px 0; }
.totals .line { display: flex; justify-content: space-between; padding: 2px 0; }
.totals .grand { font-size: 22px; font-weight: 800; color: var(--brand-dark); padding-top: 6px; }
.pay-methods { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-bottom: 10px; }
.quick-cash { display: flex; gap: 6px; flex-wrap: wrap; margin: 6px 0 10px; }
.change { font-size: 16px; font-weight: 700; }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }
.rx-needed input { border-color: #a78bfa; background: #faf5ff; }

/* bars */
.bars { display: flex; align-items: flex-end; gap: 8px; height: 160px; padding-top: 10px; }
.bar { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; }
.bar .fill { width: 100%; background: var(--brand); border-radius: 4px 4px 0 0; min-height: 2px; }
.bar .lbl { font-size: 11px; color: var(--muted); margin-top: 4px; }
.bar .val { font-size: 11px; font-weight: 600; margin-bottom: 2px; }

/* modal */
.modal-bg { position: fixed; inset: 0; background: rgba(10, 30, 30, .45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: #fff; border-radius: 12px; width: 100%; max-width: 640px; max-height: 92vh; overflow-y: auto; padding: 20px; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.modal.wide { max-width: 900px; }
.modal.narrow { max-width: 400px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.modal-head h2 { margin: 0; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }

/* toast */
#toasts { position: fixed; bottom: 16px; right: 16px; z-index: 100; display: flex; flex-direction: column; gap: 8px; }
.toast { background: #102a2a; color: #fff; padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow); max-width: 360px; }
.toast.error { background: var(--danger); }
.toast.ok { background: var(--ok); }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; background: linear-gradient(135deg, #0b3b38, #0f766e); padding: 16px; }
.login .card { width: 100%; max-width: 360px; padding: 28px; }
.login .logo { width: 56px; height: 56px; border-radius: 14px; background: var(--brand); color: #fff; display: grid; place-items: center; font-size: 32px; margin: 0 auto 12px; }
.error-msg { color: var(--danger); font-size: 13px; min-height: 18px; margin-bottom: 8px; }

/* receipt */
.receipt { font-family: "Courier New", ui-monospace, monospace; font-size: 12px; width: 300px; margin: 0 auto; color: #000; }
.receipt.km { font-family: var(--khmer), "Courier New", monospace; line-height: 1.5; }
.receipt .c { text-align: center; }
.receipt hr { border: 0; border-top: 1px dashed #000; margin: 6px 0; }
.receipt table td, .receipt table th { padding: 2px 0; border: 0; background: none; font-size: 12px; }
.receipt .big { font-size: 15px; font-weight: 700; }
.receipt .void { text-align: center; font-size: 18px; font-weight: 700; border: 2px solid #000; margin: 6px 0; }
#printArea { display: none; }

@media print {
  body > *:not(#printArea) { display: none !important; }
  #printArea { display: block; }
  @page { margin: 4mm; }
  .receipt { width: 100%; max-width: 76mm; }
}

@media (max-width: 1000px) {
  .pos { grid-template-columns: 1fr; height: auto; }
  .pos .results { max-height: 50vh; }
  .two-col { grid-template-columns: 1fr; }
}
@media (max-width: 720px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: 8px; }
  .brand { padding: 4px 8px; }
  .nav { display: flex; flex-wrap: wrap; }
  .nav a { padding: 8px 10px; }
  .sidebar .foot { margin: 0; border: 0; }
  /* phone: compact top bar, menu opens with the ☰ button */
  .sidebar { position: sticky; top: 0; z-index: 20; flex-direction: column; align-items: stretch; padding: 6px 8px; }
  .brand { padding: 4px; }
  .brand #brandName { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .menu-btn { display: block; }
  .sidebar:not(.open) .nav, .sidebar:not(.open) .foot { display: none; }
  .sidebar.open { max-height: 100vh; overflow-y: auto; }
  .nav a { flex: 1 1 45%; }
  .sidebar .foot { padding-top: 8px; }
  .table-wrap { overflow-x: auto; }
  .main { padding: 12px 16px; }
  .grid2, .grid3 { grid-template-columns: 1fr; }
}

/* grid items may shrink below their content width (long selects / inputs) */
.two-col > *, .grid2 > *, .grid3 > * { min-width: 0; }
