:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --ink: #1f2733;
  --muted: #7a869a;
  --line: #e6eaf0;
  --brand: #1f6feb;
  --brand-d: #1551b5;
  --ok: #1a9d5a;
  --warn: #d98a00;
  --danger: #d64545;
  --in: #1a9d5a;
  --out: #d64545;
  --shadow: 0 2px 10px rgba(20,30,50,.08);
  --radius: 14px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, "Segoe UI", "Microsoft YaHei", "Noto Sans Thai", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.45;
}
#app { max-width: 720px; margin: 0 auto; min-height: 100vh; padding-bottom: 28px; }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; background: var(--card); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.brand .logo { font-size: 20px; }
.brand .t { font-size: 15px; }
.topright { display: flex; align-items: center; gap: 10px; }
.langs { display: flex; gap: 4px; background: var(--bg); border-radius: 999px; padding: 3px; }
.langs button { border: 0; background: transparent; padding: 4px 9px; border-radius: 999px; font-size: 12px; cursor: pointer; color: var(--muted); font-weight: 600; }
.langs button.on { background: var(--brand); color: #fff; }
.conn { font-size: 11px; color: var(--muted); display: inline-flex; align-items: center; gap: 5px; }
.conn.on { color: var(--ok); } .conn.on::before, .conn.off::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.conn.off { color: var(--danger); }

main { padding: 16px; }
h2 { font-size: 19px; margin: 4px 0 14px; }
h3 { font-size: 15px; margin: 0 0 12px; }

/* Login */
.login { display: flex; align-items: center; justify-content: center; min-height: 70vh; }
.login-card { width: 100%; max-width: 420px; text-align: center; }
.tagline { color: var(--muted); margin: 6px 0 18px; }
.roles { display: flex; flex-direction: column; gap: 12px; }
.role {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; cursor: pointer; text-align: left; box-shadow: var(--shadow); transition: .15s;
}
.role:hover { border-color: var(--brand); transform: translateY(-1px); }
.ricon { font-size: 26px; }
.rt { font-weight: 700; font-size: 16px; }
.rd { color: var(--muted); font-size: 12px; }

/* Home menu */
.role-pill { display: inline-block; background: #eaf1fe; color: var(--brand-d); padding: 4px 12px; border-radius: 999px; font-size: 12px; font-weight: 600; margin-bottom: 14px; }
.menu { display: flex; flex-direction: column; gap: 10px; }
.menu-item {
  display: flex; align-items: center; gap: 14px; background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px; cursor: pointer; box-shadow: var(--shadow); text-align: left; width: 100%;
}
.menu-item:active { transform: scale(.99); }
.mi-icon { font-size: 24px; width: 36px; text-align: center; }
.mi-text { font-weight: 600; flex: 1; font-size: 16px; }
.mi-arrow { color: var(--muted); font-size: 22px; }
.logout { margin-top: 18px; width: 100%; background: var(--card); border: 1px solid var(--line); color: var(--muted); padding: 12px; border-radius: var(--radius); cursor: pointer; }

/* Cards */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 14px; }

/* Buttons */
button { font-family: inherit; }
.primary { background: var(--brand); color: #fff; border: 0; border-radius: 10px; padding: 13px 16px; font-size: 15px; font-weight: 600; cursor: pointer; width: 100%; }
.danger { background: var(--danger); color: #fff; border: 0; border-radius: 10px; padding: 13px 16px; font-size: 15px; font-weight: 600; cursor: pointer; width: 100%; }
.secondary { background: #eef1f6; color: var(--ink); border: 0; border-radius: 10px; padding: 12px 16px; font-weight: 600; cursor: pointer; }
.back { background: transparent; border: 0; color: var(--muted); padding: 12px; cursor: pointer; font-size: 14px; }

/* Forms */
.form-page label, .items label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 5px; font-weight: 600; }
.form-page label:first-child, .items .card h3 + label { margin-top: 0; }
input, select { width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; font-size: 15px; background: #fff; color: var(--ink); }
input:focus, select:focus { outline: none; border-color: var(--brand); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
#f-sub, #n-add { margin-top: 16px; }

/* Scanner */
.scanner-box { position: relative; background: #11161f; border-radius: var(--radius); overflow: hidden; aspect-ratio: 4/3; display: flex; align-items: center; justify-content: center; }
.scanner-box video { width: 100%; height: 100%; object-fit: cover; }
.scan-overlay { position: absolute; inset: 0; pointer-events: none; }
.scan-line { position: absolute; left: 10%; right: 10%; top: 50%; height: 2px; background: var(--brand); box-shadow: 0 0 12px var(--brand); animation: scan 2.2s linear infinite; }
@keyframes scan { 0% { top: 12%; } 50% { top: 88%; } 100% { top: 12%; } }
.cam-msg { position: absolute; bottom: 8px; left: 0; right: 0; text-align: center; color: #cfe; font-size: 12px; padding: 0 10px; }
.scan-btns, .scan-btns { display: flex; gap: 10px; margin: 12px 0; }
.scan-btns .primary, .scan-btns .secondary { flex: 1; }
.manual { margin-top: 6px; }
.manual label { font-size: 13px; color: var(--muted); display: block; margin-bottom: 6px; }
.manual .row { display: flex; gap: 8px; }
.manual .row input { flex: 1; }
#scan-result { margin-top: 14px; }

/* Item card */
.item-card { margin-bottom: 0; }
.ic-head { display: flex; justify-content: space-between; align-items: center; }
.ic-code { font-weight: 700; color: var(--muted); font-size: 13px; }
.ic-name { font-size: 18px; font-weight: 700; margin: 4px 0 10px; }
.ic-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 14px; }
.ic-grid > div { display: flex; flex-direction: column; }
.ic-grid span { font-size: 11px; color: var(--muted); }
.ic-grid b { font-size: 14px; }
.ic-actions { display: flex; gap: 10px; margin-top: 14px; }
.ic-actions .primary, .ic-actions .danger { flex: 1; }
.badge.low, .tag.low { background: #fdeaea; color: var(--danger); }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 999px; font-weight: 700; }

/* Tables */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { background: #f7f9fc; color: var(--muted); font-weight: 600; font-size: 12px; }
tbody tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr.low td { background: #fff6f6; }
tr.low td:nth-child(4) { color: var(--danger); font-weight: 700; }
.inv-tools { display: flex; gap: 8px; margin-bottom: 12px; }
.inv-tools input { flex: 1; }
.muted { color: var(--muted); }

/* Dashboard */
.dash-head { display: flex; justify-content: space-between; align-items: center; }
.live { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 700; color: var(--ok); }
.live .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); animation: pulse 1.4s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .35; } }
.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 14px; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; text-align: center; box-shadow: var(--shadow); }
.kpi .kv { font-size: 24px; font-weight: 800; line-height: 1; }
.kpi .kl { font-size: 11px; color: var(--muted); margin-top: 6px; }
.kpi.b .kv { color: var(--brand); } .kpi.g .kv { color: var(--ok); }
.kpi.in .kv { color: var(--in); } .kpi.out .kv { color: var(--out); } .kpi.low .kv { color: var(--danger); }
.bars { display: flex; flex-direction: column; gap: 9px; }
.bar-row { display: grid; grid-template-columns: 120px 1fr 48px; align-items: center; gap: 10px; font-size: 13px; }
.bar-label { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-track { background: #eef1f6; border-radius: 6px; height: 14px; overflow: hidden; }
.bar-fill { display: block; height: 100%; background: linear-gradient(90deg, #3b8bff, #1f6feb); border-radius: 6px; }
.bar-val { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.tag { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 11px; font-weight: 700; }
.tag.in { background: #e7f6ee; color: var(--in); }
.tag.out { background: #fdeaea; color: var(--out); }
.tag.adj { background: #eef1f6; color: var(--muted); }

/* Login form */
.login-card .hint { color: var(--muted); font-size: 12px; margin-top: 12px; line-height: 1.5; }
.who { font-size: 11px; color: var(--muted); max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Dashboard new controls */
.card-h { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.card-h h3 { margin: 0; }
.dbtns { display: flex; gap: 4px; }
.dbtn { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 4px 10px; font-size: 12px; cursor: pointer; color: var(--muted); font-weight: 600; }
.dbtn.on { background: var(--brand); color: #fff; border-color: var(--brand); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.legend .lg { display: inline-flex; align-items: center; gap: 5px; }
.legend .lg::before { content: ''; width: 12px; height: 3px; border-radius: 2px; }
.legend .lg.in::before { background: var(--in); }
.legend .lg.out::before { background: var(--out); }
.trend-wrap { width: 100%; overflow-x: auto; }
.trend-svg { width: 100%; min-width: 320px; height: auto; display: block; }
.exp-btns { display: flex; gap: 8px; }
.exp { border: 1px solid var(--brand); background: #fff; color: var(--brand); border-radius: 8px; padding: 5px 12px; font-size: 12px; font-weight: 600; cursor: pointer; }
.exp:hover { background: #eaf1fe; }

/* Toast */
.toast { position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%) translateY(20px); background: #1f2733; color: #fff; padding: 11px 18px; border-radius: 999px; font-size: 14px; opacity: 0; pointer-events: none; transition: .25s; z-index: 50; box-shadow: 0 6px 20px rgba(0,0,0,.25); max-width: 90%; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.ok { background: var(--ok); }
.toast.err { background: var(--danger); }
.loading { color: var(--muted); padding: 20px; text-align: center; }
.warn { color: var(--danger); background: #fdeaea; padding: 12px; border-radius: 10px; text-align: center; }

/* Delete / clear buttons */
.del-btn { background: #fff0f0; color: #d64545; border: 1px solid #f3c2c2; border-radius: 8px; padding: 4px 10px; font-size: 12px; cursor: pointer; white-space: nowrap; }
.del-btn:hover { background: #d64545; color: #fff; }
.danger-outline { display: block; width: 100%; margin: 14px 0 4px; padding: 12px; background: #fff; color: #d64545; border: 1.5px dashed #d64545; border-radius: 12px; font-size: 14px; cursor: pointer; }
.danger-outline:hover { background: #fff0f0; }
.edit-btn { background: #eef4ff; color: #2f6fed; border: 1px solid #c9d8f7; border-radius: 8px; padding: 4px 10px; font-size: 12px; cursor: pointer; white-space: nowrap; }
.edit-btn:hover { background: #2f6fed; color: #fff; }
.act-cell { display: flex; gap: 6px; flex-wrap: wrap; }
.mini-danger { background: #fff0f0; color: #d64545; border: 1px solid #f3c2c2; border-radius: 8px; padding: 12px 14px; font-size: 14px; font-weight: 600; cursor: pointer; white-space: nowrap; }
.mini-danger:hover { background: #d64545; color: #fff; }
.inv-exp { margin: 10px 0 4px; display: flex; gap: 8px; }

/* Stock-count import */
.import-card .imp-btns { display: flex; gap: 10px; margin-top: 8px; }
.import-card .hint { font-size: 12.5px; color: #7a869a; line-height: 1.6; margin: 6px 0 4px; }
.imp-ok { margin-top: 10px; padding: 10px 12px; background: #eefaf2; border: 1px solid #bfe8cf; border-radius: 10px; font-size: 13.5px; color: #16794c; }
.imp-fails { margin: 8px 0 0; padding: 8px 12px 8px 26px; background: #fff6f6; border: 1px solid #f3c2c2; border-radius: 10px; font-size: 12.5px; color: #b23a3a; }
.imp-fails li { margin: 2px 0; }

/* Change-password button + modal */
.pw-btn { display: block; width: 100%; margin: 12px 0 0; padding: 13px; background: #fff; color: #2f6fed; border: 1.5px solid #c9d8f7; border-radius: 12px; font-size: 15px; cursor: pointer; }
.pw-btn:hover { background: #f0f5ff; }
.modal-mask { position: fixed; inset: 0; background: rgba(20, 30, 50, .45); display: flex; align-items: center; justify-content: center; z-index: 200; padding: 20px; }
.modal-mask[hidden] { display: none; }
.modal-card { background: #fff; border-radius: 16px; padding: 22px 20px; width: 100%; max-width: 360px; box-shadow: 0 12px 40px rgba(0,0,0,.18); }
.modal-card h3 { margin: 0 0 12px; font-size: 17px; }
.modal-card label { display: block; margin: 10px 0 4px; font-size: 13px; color: #5a6779; }
.modal-card input { width: 100%; padding: 11px 12px; border: 1px solid #d4dbe6; border-radius: 10px; font-size: 15px; box-sizing: border-box; }
.modal-card .hint { font-size: 12px; color: #8a94a6; margin: 8px 0 2px; }
.modal-btns { display: flex; gap: 10px; margin-top: 14px; }
.modal-btns button { flex: 1; padding: 12px; border-radius: 10px; font-size: 15px; cursor: pointer; }
.btn-secondary { background: #f2f4f8; color: #444; border: 1px solid #dfe4ec; }
.btn-primary { background: #2f6fed; color: #fff; border: none; }
.btn-primary:hover { background: #275cc9; }
