/* HP Finance — giao diện xanh da trời, đơn giản, thân thiện mobile */
:root {
  --blue: #1e88e5;
  --blue-dark: #1565c0;
  --blue-600: #1976d2;
  --blue-050: #e3f2fd;
  --blue-100: #bbdefb;
  --sky: #4fc3f7;
  --ink: #1a2733;
  --muted: #64748b;
  --line: #e2e8f0;
  --bg: #f4f7fb;
  --card: #ffffff;
  --ok: #2e7d32;
  --ok-bg: #e8f5e9;
  --warn: #ef6c00;
  --warn-bg: #fff3e0;
  --danger: #c62828;
  --danger-bg: #ffebee;
  --info: #0277bd;
  --info-bg: #e1f5fe;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(16, 42, 67, .08), 0 1px 2px rgba(16, 42, 67, .06);
  --shadow-lg: 0 10px 30px rgba(16, 42, 67, .12);
}

* { box-sizing: border-box; }

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

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

/* ---------- Layout ---------- */
.topbar {
  background: linear-gradient(135deg, var(--blue) 0%, var(--blue-dark) 100%);
  color: #fff;
  padding: 6px 16px;
  min-height: 58px;
  display: flex;
  align-items: center;
  gap: 14px;
  position: sticky;
  top: 0;
  z-index: 40;
  box-shadow: var(--shadow);
}
.topbar .brand {
  font-weight: 800;
  font-size: 28px;
  letter-spacing: .5px;
  color: #fff;
  display: flex;
  align-items: center;
  gap: 9px;
  white-space: nowrap;
}
.topbar .brand:hover { text-decoration: none; }
.topbar .brand .logo {
  width: 30px; height: 30px;
  background: #fff; color: var(--blue-dark);
  border-radius: 8px;
  display: grid; place-items: center;
  font-weight: 800; font-size: 15px;
}
.topbar .brand .logo-img {
  height: 48px; width: auto; max-width: 180px;
  background: #fff; border-radius: 8px; padding: 3px 7px;
  object-fit: contain; display: block;
}
.topbar .spacer { flex: 1; }
.topbar .user {
  display: flex; align-items: center; gap: 16px;
  font-size: 24px; color: #eaf3fd;
}
.topbar .user .uname { font-weight: 600; }
.topbar .user .role-chip {
  background: rgba(255,255,255,.18);
  padding: 6px 16px; border-radius: 20px; font-size: 19px;
}
.topbar .user .btn.sm {
  font-size: 19px; padding: 9px 18px; border-radius: 10px;
}
.topbar .burger {
  display: none; background: none; border: 0; color: #fff;
  font-size: 24px; cursor: pointer; padding: 4px 8px;
}

.shell { display: flex; min-height: calc(100vh - 58px); }

.sidebar {
  width: 232px;
  background: var(--card);
  border-right: 1px solid var(--line);
  padding: 14px 10px;
  flex-shrink: 0;
}
.sidebar .nav-group { margin-bottom: 18px; }
.sidebar .nav-group h6 {
  margin: 0 0 6px 12px;
  font-size: 11px; text-transform: uppercase; letter-spacing: .5px;
  color: var(--muted); font-weight: 700;
}
.sidebar a.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border-radius: 9px;
  color: var(--ink); font-size: 14px; font-weight: 500;
}
.sidebar a.nav-item:hover { background: var(--blue-050); text-decoration: none; }
.sidebar a.nav-item.active {
  background: var(--blue); color: #fff;
}
.sidebar a.nav-item .ico { width: 20px; text-align: center; }
/* Nhãn gộp (không bấm được) — VD "Đối tượng" gồm Khách hàng + Nhà cung cấp */
.sidebar .nav-grouplabel {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px 4px; color: var(--ink); font-size: 14px; font-weight: 700;
}
.sidebar .nav-grouplabel .ico { width: 20px; text-align: center; }
/* Menu con dưới "Công nợ phải thu" */
.sidebar .nav-sub {
  margin: 2px 0 2px 30px; display: flex; flex-direction: column; gap: 1px;
  border-left: 2px solid var(--line); padding-left: 8px;
}
.sidebar a.nav-subitem {
  padding: 6px 12px; border-radius: 7px; color: var(--muted);
  font-size: 13px; font-weight: 500;
}
.sidebar a.nav-subitem:hover { background: var(--blue-050); color: var(--ink); text-decoration: none; }
.sidebar a.nav-subitem.active { background: var(--blue-050); color: var(--blue-dark); font-weight: 700; }

.main { flex: 1; padding: 22px 26px; min-width: 0; }
.page-head {
  display: flex; align-items: center; gap: 14px;
  margin-bottom: 18px; flex-wrap: wrap;
}
.page-head h1 { margin: 0; font-size: 22px; font-weight: 700; }
.page-head .sub { color: var(--muted); font-size: 14px; }
.page-head .spacer { flex: 1; }

/* ---------- Cards ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 18px;
  overflow: hidden;
}
.card > .card-head {
  padding: 13px 18px; border-bottom: 1px solid var(--line);
  font-weight: 700; display: flex; align-items: center; gap: 10px;
}
.card > .card-body { padding: 16px 18px; }
.card > .card-body.tight { padding: 0; }

/* ---------- Stat tiles ---------- */
.stat-grid {
  display: grid; gap: 14px;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  margin-bottom: 20px;
}
.stat {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px 18px;
  box-shadow: var(--shadow); position: relative; overflow: hidden;
}
.stat::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px;
  background: var(--blue);
}
.stat.tone-danger::before { background: var(--danger); }
.stat.tone-warn::before { background: var(--warn); }
.stat.tone-ok::before { background: var(--ok); }
.stat.tone-info::before { background: var(--info); }
.stat .label { color: var(--muted); font-size: 13px; font-weight: 600; }
.stat .value { font-size: 24px; font-weight: 800; margin-top: 4px; letter-spacing: -.3px; }
.stat .value .unit { font-size: 13px; font-weight: 600; color: var(--muted); }
.stat .foot { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table.grid {
  width: 100%; border-collapse: collapse; font-size: 14px;
}
table.grid th, table.grid td {
  padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line);
  vertical-align: top;
}
table.grid thead th {
  background: var(--blue-050); color: var(--blue-dark);
  font-weight: 700; font-size: 12px; text-transform: uppercase;
  letter-spacing: .3px; white-space: nowrap; position: sticky; top: 0;
}
table.grid tbody tr:hover { background: #fafcff; }
table.grid td.num, table.grid th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.grid tr.total-row td { font-weight: 700; background: var(--blue-050); }
table.grid tr.total-row td.num { color: var(--blue-dark); }
/* Bảng nhiều cột: nén padding + cho tiêu đề xuống dòng để mọi cột nằm gọn trong trang */
table.grid.compact th, table.grid.compact td { padding: 8px 8px; font-size: 13px; }
table.grid.compact thead th { font-size: 11px; white-space: normal; }

.money { font-variant-numeric: tabular-nums; white-space: nowrap; }
.money.neg { color: var(--danger); }

/* ---------- Badges ---------- */
.badge {
  display: inline-block; padding: 3px 10px; border-radius: 20px;
  font-size: 12px; font-weight: 700; white-space: nowrap;
}
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.info { background: var(--info-bg); color: var(--info); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.danger { background: var(--danger-bg); color: var(--danger); }
.badge.muted { background: #eef2f6; color: var(--muted); }
.badge.group { background: var(--blue-050); color: var(--blue-dark); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 9px 16px; border-radius: 9px; border: 1px solid transparent;
  font-size: 14px; font-weight: 600; cursor: pointer; background: #eef2f6;
  color: var(--ink); transition: .15s; text-decoration: none; white-space: nowrap;
}
.btn:hover { text-decoration: none; filter: brightness(.97); }
.btn.primary { background: var(--blue); color: #fff; }
.btn.primary:hover { background: var(--blue-dark); }
.btn.success { background: var(--ok); color: #fff; }
.btn.danger { background: var(--danger); color: #fff; }
.btn.ghost { background: #fff; border-color: var(--line); }
.btn.sm { padding: 6px 11px; font-size: 13px; }
.btn.icon { padding: 7px 9px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }

/* ---------- Forms ---------- */
.form-grid { display: grid; gap: 15px; grid-template-columns: repeat(2, 1fr); }
.form-grid .full { grid-column: 1 / -1; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 5px; color: #37485a; }
.field .req { color: var(--danger); }
.field input, .field select, .field textarea {
  width: 100%; padding: 9px 12px; border: 1px solid var(--line);
  border-radius: 9px; font-size: 14px; font-family: inherit; background: #fff;
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-050);
}
.field .hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.field .err { font-size: 12px; color: var(--danger); margin-top: 4px; }
input.money-input { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Flash ---------- */
.flash-wrap { margin-bottom: 16px; }
.flash {
  padding: 11px 15px; border-radius: 9px; margin-bottom: 9px;
  font-size: 14px; font-weight: 500; border-left: 4px solid;
}
.flash.success { background: var(--ok-bg); color: var(--ok); border-color: var(--ok); }
.flash.danger { background: var(--danger-bg); color: var(--danger); border-color: var(--danger); }
.flash.warning { background: var(--warn-bg); color: var(--warn); border-color: var(--warn); }
.flash.info { background: var(--info-bg); color: var(--info); border-color: var(--info); }

/* ---------- Filter bar ---------- */
.filterbar {
  display: flex; gap: 12px; flex-wrap: wrap; align-items: end;
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px 16px; margin-bottom: 18px;
  box-shadow: var(--shadow);
}
.filterbar .field { margin: 0; }
.filterbar .field input, .filterbar .field select { padding: 8px 11px; }

.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  padding: 6px 14px; border-radius: 20px; background: #fff;
  border: 1px solid var(--line); font-size: 13px; font-weight: 600; color: var(--ink);
}
.chip:hover { text-decoration: none; border-color: var(--blue-100); }
.chip.active { background: var(--blue); color: #fff; border-color: var(--blue); }
/* Tab lớn trong trang (VD Nhà cung cấp | Khách hàng) */
.subtabs { margin-bottom: 14px; }
.subtabs .chip { padding: 9px 20px; font-size: 15px; border-radius: 10px; }

.empty {
  text-align: center; padding: 40px 20px; color: var(--muted);
}
.empty .big { font-size: 40px; margin-bottom: 8px; }

/* mini inline forms in tables */
.inline-form { display: flex; gap: 6px; align-items: center; }
.inline-form input, .inline-form select {
  padding: 6px 8px; border: 1px solid var(--line); border-radius: 7px; font-size: 13px;
}

/* line editor for contract */
.line-editor table { width: 100%; border-collapse: collapse; }
.line-editor th { font-size: 12px; color: var(--muted); text-align: left; padding: 6px; font-weight: 600; }
.line-editor td { padding: 4px; }
.line-editor input, .line-editor select { padding: 7px 9px; font-size: 13px; }
.line-editor .del-row { color: var(--danger); cursor: pointer; background: none; border: 0; font-size: 18px; }

.section-title {
  font-size: 13px; font-weight: 700; color: var(--blue-dark);
  background: var(--blue-050); padding: 8px 14px; border-radius: 8px;
  margin: 18px 0 10px;
}

.help-note {
  background: var(--info-bg); border-left: 4px solid var(--info);
  padding: 10px 14px; border-radius: 8px; font-size: 13px; color: #0d5a7a;
  margin-bottom: 14px;
}

/* ---------- Dashboard gradient cards ---------- */
.kpi-grid{
  display:grid; gap:14px; margin-bottom:18px;
  grid-template-columns:repeat(auto-fit,minmax(210px,1fr));
}
.kpi{
  border-radius:16px; padding:18px 20px; color:#fff; position:relative; overflow:hidden;
  box-shadow:0 6px 18px rgba(16,42,67,.14);
}
.kpi .k-label{font-size:12.5px; font-weight:700; text-transform:uppercase; letter-spacing:.4px; opacity:.95; display:flex; align-items:center; gap:7px;}
.kpi .k-value{font-size:26px; font-weight:800; margin-top:8px; letter-spacing:-.5px; line-height:1.1;}
.kpi .k-foot{font-size:12px; opacity:.9; margin-top:6px;}
.kpi::after{content:""; position:absolute; right:-30px; top:-30px; width:110px; height:110px; background:rgba(255,255,255,.12); border-radius:50%;}
.kpi.g-green{background:linear-gradient(135deg,#43a047,#2e7d32);}
.kpi.g-blue{background:linear-gradient(135deg,#1e88e5,#1565c0);}
.kpi.g-purple{background:linear-gradient(135deg,#8e24aa,#6a1b9a);}
.kpi.g-orange{background:linear-gradient(135deg,#fb8c00,#ef6c00);}
.kpi.g-red{background:linear-gradient(135deg,#e53935,#c62828);}

.dash-row{display:grid; gap:16px; grid-template-columns:1fr 1fr 1fr; margin-bottom:18px;}
.dash-row.two{grid-template-columns:1fr 1fr;}
@media (max-width:1050px){ .dash-row, .dash-row.two{grid-template-columns:1fr;} }

.alertbar{
  background:#fff8e1; border:1px solid #ffe082; border-left:4px solid #ffb300;
  border-radius:12px; padding:11px 16px; margin-bottom:18px; font-size:14px;
  display:flex; align-items:center; gap:8px; flex-wrap:wrap;
}

/* legend + bars */
.legend{display:flex; flex-direction:column; gap:7px; font-size:13px;}
.legend .row{display:flex; align-items:center; gap:8px; justify-content:space-between;}
.legend .dot{width:11px; height:11px; border-radius:3px; display:inline-block; flex:0 0 auto;}
.legend .nm{display:flex; align-items:center; gap:8px; color:#37485a;}
.legend .vl{font-variant-numeric:tabular-nums; font-weight:700;}

.hbars{display:flex; flex-direction:column; gap:12px;}
.hbar .hb-name{font-size:13px; margin-bottom:4px; color:#37485a; font-weight:600;}
.hbar .hb-track{display:flex; height:22px; border-radius:6px; overflow:hidden; background:#eef2f6;}
.hbar .hb-seg{height:100%;}

.pay-list{display:flex; flex-direction:column;}
.pay-item{display:flex; align-items:flex-start; gap:10px; padding:11px 0; border-bottom:1px solid var(--line);}
.pay-item:last-child{border-bottom:0;}
.pay-item .pay-main{flex:1; min-width:0;}
.pay-item .pay-name{font-weight:700; font-size:14px;}
.pay-item .pay-amt{color:var(--danger); font-weight:700; font-variant-numeric:tabular-nums; font-size:14px;}
.pay-item .pay-sub{font-size:12px; color:var(--muted); margin-top:1px;}
.pay-item .pay-days{font-size:11px; font-weight:700; padding:2px 8px; border-radius:12px; background:var(--blue-050); color:var(--blue-dark); white-space:nowrap;}
.pay-item .pay-days.soon{background:#fdecea; color:var(--danger);}

/* ---------- Mobile ----------
 * Chiến lược: thu nhỏ topbar (logo/chữ/nút desktop đang quá to cho màn hình nhỏ),
 * bảng vẫn cuộn ngang (.table-wrap) nhưng GHIM CỘT ĐẦU để cuộn ngang vẫn biết đang
 * xem dòng nào, và tăng vùng chạm (nút/ô nhập) lên gần chuẩn 44px cho ngón tay —
 * KHÔNG đổi mật độ hiển thị trên desktop (kế toán dùng chuột/bàn phím vẫn như cũ).
 */
@media (max-width: 860px) {
  .topbar { padding: 6px 10px; gap: 8px; }
  .topbar .burger { display: block; padding: 8px 10px; font-size: 22px; }
  .topbar .brand { font-size: 15px; gap: 6px; }
  .topbar .brand .logo-img { height: 34px; max-width: 96px; padding: 2px 5px; }
  .topbar .user { font-size: 12px; gap: 8px; }
  .topbar .user .uname { display: none; }
  .topbar .user .role-chip { padding: 4px 10px; font-size: 11px; }
  .topbar .user .btn.sm { padding: 8px 12px; font-size: 12px; }

  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; z-index: 50;
    width: 78vw; max-width: 280px; padding-top: 66px;
    transform: translateX(-100%); transition: transform .22s;
    box-shadow: var(--shadow-lg); overflow-y: auto;
  }
  .sidebar.open { transform: translateX(0); }
  .backdrop {
    display: none; position: fixed; inset: 0; background: rgba(16,42,67,.4); z-index: 45;
  }
  .backdrop.show { display: block; }
  .sidebar a.nav-item, .sidebar a.nav-subitem { padding: 11px 12px; font-size: 15px; }

  .main { padding: 14px 10px; }
  .form-grid { grid-template-columns: 1fr; }
  .page-head h1 { font-size: 19px; }
  .page-head { gap: 10px; }
  .stat .value { font-size: 21px; }
  .kpi .k-value { font-size: 22px; }

  /* Vùng chạm rộng hơn cho ngón tay + cỡ chữ ô nhập ≥16px để Safari iOS không tự
     zoom khi bấm vào (lỗi rất hay gặp, làm form nhảy loạn trên iPhone). */
  .btn { padding: 11px 15px; font-size: 14px; }
  .btn.sm { padding: 9px 13px; font-size: 13px; }
  .chip { padding: 9px 15px; }
  .field input, .field select, .field textarea,
  .filterbar .field input, .filterbar .field select,
  .inline-form input, .inline-form select {
    padding: 11px 12px; font-size: 16px;
  }
  input[type="checkbox"], input[type="radio"] { width: 19px; height: 19px; }

  /* Bảng vẫn cuộn ngang, nhưng ghim cột đầu (thường là tên đối tượng/hợp đồng) để
     cuộn sang phải xem số liệu vẫn biết đang ở dòng nào — đỡ phải cuộn qua cuộn lại. */
  table.grid td:first-child, table.grid th:first-child {
    position: sticky; left: 0; z-index: 1;
    box-shadow: 2px 0 4px rgba(16,42,67,.06);
  }
  table.grid thead th:first-child { z-index: 2; background: var(--blue-050); }
  table.grid tbody td:first-child { background: var(--card); }
  table.grid tbody tr:hover td:first-child { background: #fafcff; }
  table.grid tr.total-row td:first-child { background: var(--blue-050); }
}

@media (max-width: 480px) {
  .topbar .brand span { display: none; }
  .topbar .user .role-chip { display: none; }
  .stat-grid, .kpi-grid { grid-template-columns: 1fr; }
}

@media print {
  .topbar, .sidebar, .btn, .filterbar, .no-print, .backdrop { display: none !important; }
  .main { padding: 0; }
  .card { box-shadow: none; border: 1px solid #ccc; }
  body { background: #fff; }
}
