:root {
  --bg: #FAFAF7;
  --surface: #FFFFFF;
  --surface-soft: #F4F3EE;
  --ink: #23262B;
  --ink-soft: #6B6E73;
  --border: #E4E1D8;
  --accent: #2E6E8E;
  --accent-soft: #DCEAF1;
  --danger: #A6402C;
  --danger-soft: #F5E1DB;
  --success: #3F7554;
  --success-soft: #E1ECE3;
  --warning: #C8701E;
  --warning-soft: #F8E8D6;
  --radius: 6px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
}

[hidden] { display: none !important; }

/* ---------- Bo cuc ---------- */
.app {
  display: grid;
  grid-template-columns: 210px 1fr;
  min-height: 100vh;
  /* An giao dien cho toi khi xac thuc xong (xem khoiDong() trong app.js) — tranh thoang thay
     giao dien truoc khi kip chuyen ve login.html */
  visibility: hidden;
}
body.da-xac-thuc .app { visibility: visible; }

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 16px; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 8px;
  background: var(--accent); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
}
.brand-name { font-weight: 600; font-size: 15px; }

.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  text-align: left;
  border: none;
  background: none;
  padding: 9px 12px;
  border-radius: var(--radius);
  font-size: 14px;
  color: var(--ink);
  cursor: pointer;
}
.nav-item:hover { background: var(--surface-soft); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-separator { height: 1px; background: var(--border); margin: 8px 4px; }

.sidebar-footer {
  margin-top: auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 8px 0;
  border-top: 1px solid var(--border);
  font-size: 13px;
}
.app-version { font-size: 11px; color: var(--ink-soft); padding: 8px 8px 0; }

.main { padding: 24px 28px 48px; min-width: 0; }

.mobile-topbar, .sidebar-backdrop { display: none; }

.view-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.view-head h2 { margin: 0; font-size: 20px; }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.toolbar input, .toolbar select { width: auto; min-width: 140px; }
.toolbar .grow { flex: 1; min-width: 200px; }

/* ---------- Nut & o nhap ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  padding: 8px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-soft); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: #255a75; }
.btn-success { background: var(--success); border-color: var(--success); color: #fff; }
.btn-success:hover { background: #335f44; }
.btn-danger { color: var(--danger); border-color: var(--danger-soft); }
.btn-danger:hover { background: var(--danger-soft); }
.btn-ghost { border-color: transparent; background: none; }
.btn-sm { padding: 4px 9px; font-size: 13px; }
.btn-lg { padding: 12px 18px; font-size: 16px; font-weight: 600; }

input, select, textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font: inherit;
  font-size: 14px;
  background: var(--surface);
  color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
input.so { text-align: right; }

.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--ink-soft); margin-bottom: 12px; }
.field input, .field select, .field textarea { color: var(--ink); }
.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 0 12px; }
.checkbox { display: flex; align-items: center; gap: 8px; font-size: 14px; margin-bottom: 12px; }
.checkbox input { width: auto; }

.form-feedback { font-size: 13px; min-height: 18px; margin-bottom: 8px; }
.form-feedback.error { color: var(--danger); }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }

/* ---------- Bang ---------- */
.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
}
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { background: var(--surface-soft); font-weight: 600; font-size: 13px; color: var(--ink-soft); white-space: nowrap; }
tr:last-child td { border-bottom: none; }
td.so, th.so { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
td.hanh-dong { text-align: right; white-space: nowrap; }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--surface-soft); }
.link { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
tr.da-huy td { color: var(--ink-soft); text-decoration: line-through; }
tr.da-huy td.hanh-dong, tr.da-huy td .badge { text-decoration: none; }
tfoot td { font-weight: 600; background: var(--surface-soft); }
.trong { padding: 28px; text-align: center; color: var(--ink-soft); }
td input { padding: 5px 8px; }
td input.so { width: 110px; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 12px;
  font-weight: 500;
  background: var(--surface-soft);
  color: var(--ink-soft);
  white-space: nowrap;
}
.badge-ok { background: var(--success-soft); color: var(--success); }
.badge-warn { background: var(--warning-soft); color: var(--warning); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.text-danger { color: var(--danger); }
.text-success { color: var(--success); }
.text-soft { color: var(--ink-soft); font-size: 13px; }

/* ---------- The so lieu ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 20px; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.card-label { font-size: 13px; color: var(--ink-soft); }
.card-value { font-size: 22px; font-weight: 600; margin-top: 4px; font-variant-numeric: tabular-nums; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.panel-title { font-size: 15px; font-weight: 600; margin: 0 0 8px; }

/* ---------- Ban hang (POS) ---------- */
.pos { display: grid; grid-template-columns: 1fr 340px; gap: 16px; align-items: start; }
.pos-left { min-width: 0; }
.pos-right {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  position: sticky;
  top: 16px;
}
.pos-dong { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 6px 0; font-size: 14px; }
.pos-dong input { width: 140px; }
.pos-dong.tong { font-size: 18px; font-weight: 700; border-top: 1px solid var(--border); margin-top: 6px; padding-top: 10px; }
.pos-right .btn-lg { width: 100%; margin-top: 12px; }
.pos-search { position: relative; margin-bottom: 12px; }
.pos-search input { font-size: 16px; padding: 11px 12px; }

/* O tim hang hoa co danh sach goi y */
.tim-hang { position: relative; }
.goi-y {
  position: absolute;
  z-index: 30;
  left: 0; right: 0; top: calc(100% + 2px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.1);
  max-height: 320px;
  overflow-y: auto;
}
.goi-y-item { display: flex; justify-content: space-between; gap: 12px; padding: 8px 12px; cursor: pointer; font-size: 14px; }
.goi-y-item.chon, .goi-y-item:hover { background: var(--accent-soft); }
.goi-y-item small { color: var(--ink-soft); }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(20, 22, 26, 0.45);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 6vh 16px;
  z-index: 50;
  overflow-y: auto;
}
.modal {
  background: var(--surface);
  border-radius: 8px;
  width: 520px;
  max-width: 100%;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}
.modal.rong { width: 860px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; font-size: 17px; }
.modal-close { border: none; background: none; font-size: 24px; line-height: 1; cursor: pointer; color: var(--ink-soft); }
.modal-body { padding: 18px; }
.chi-tiet { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 6px 16px; font-size: 14px; margin-bottom: 14px; }
.chi-tiet span { color: var(--ink-soft); }

.toast {
  position: fixed;
  bottom: 24px; left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 10px 18px;
  border-radius: var(--radius);
  font-size: 14px;
  z-index: 60;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
}
.toast.error { background: var(--danger); }

/* ---------- Dien thoai ---------- */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .mobile-topbar {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 20;
  }
  .mobile-menu-btn { border: none; background: none; font-size: 22px; cursor: pointer; }
  .mobile-topbar-name { font-weight: 600; }
  .sidebar {
    position: fixed; left: 0; top: 0; z-index: 45;
    width: 240px;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  body.mo-menu .sidebar { transform: none; }
  body.mo-menu .sidebar-backdrop { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, 0.3); z-index: 40; }
  .main { padding: 16px; }
  .pos, .grid-2 { grid-template-columns: 1fr; }
  .pos-right { position: static; }
}

@media print {
  .sidebar, .mobile-topbar, .toolbar, .view-head .btn { display: none !important; }
}
