:root {
  --bg: #f4f6fa; --surface: #fff; --ink: #1b2333; --muted: #66708a; --line: #e3e8f1;
  --brand: #2f5bea; --brand-ink: #fff; --brand-soft: #e8eeff;
  --ok: #138a52; --ok-soft: #e1f6ec; --warn: #a15c00; --warn-soft: #fff1d6;
  --bad: #c0332b; --bad-soft: #fde8e6; --info: #0f6f94; --info-soft: #dff3fa;
  --radius: 10px; --shadow: 0 1px 2px rgba(20,30,60,.06), 0 4px 14px rgba(20,30,60,.05);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font-size: 15px; line-height: 1.45; }
button, input, select, textarea { font: inherit; color: inherit; }
.app { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.boot { grid-column: 1 / -1; display: grid; place-items: center; align-content: center; gap: 8px; color: var(--muted); min-height: 60vh; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Sidebar */
.side { background: #101a33; color: #cfd8f0; padding: 18px 12px; display: flex; flex-direction: column; gap: 4px; position: sticky; top: 0; height: 100vh; }
.brand { font-weight: 700; font-size: 17px; color: #fff; padding: 4px 10px 14px; display: flex; gap: 8px; align-items: center; }
.brand .logo { background: #fff; border-radius: 10px; padding: 6px 8px; display: inline-flex; flex: none; }
.brand .logo img { display: block; height: auto; max-width: 84px; }
.brand .bname { font-size: 15px; line-height: 1.2; }
.nav { overflow-y: auto; flex: 1 1 auto; min-height: 0; }
.nav a.grp, .nav .grp { font-weight: 600; display: flex; align-items: center; justify-content: space-between; }
.nav .grp.cur { color: #fff; }
.nav a.grp.on, .nav .grp.on { background: var(--brand); color: #fff; }
.nav .chev { width: 7px; height: 7px; border-right: 2px solid currentColor; border-bottom: 2px solid currentColor; transform: rotate(45deg); margin: 0 4px 2px 0; opacity: .7; }
.nav .grp[aria-expanded="true"] .chev { transform: rotate(-135deg); margin-top: 4px; }
.nav .sub { display: flex; flex-direction: column; gap: 1px; margin: 0 0 6px 10px; padding-left: 8px; border-left: 1px solid rgba(255,255,255,.14); }
.nav .sub button, .nav .sub a { padding: 6px 10px; font-size: 14px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav button, .nav a { all: unset; cursor: pointer; padding: 9px 12px; border-radius: 8px; color: #cfd8f0; display: block; }
.nav button:hover, .nav a:hover { background: rgba(255,255,255,.07); }
.nav a[aria-current="page"], .nav button[aria-current="page"] { background: var(--brand); color: #fff; font-weight: 600; }
.nav a:focus-visible, .nav button:focus-visible { outline: 2px solid #9db4ff; outline-offset: 1px; }
.side .me { margin-top: auto; padding: 10px; font-size: 13px; border-top: 1px solid rgba(255,255,255,.1); word-break: break-all; }
.me .me-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 4px; }
.side .lang { display: flex; gap: 3px; margin: 0; flex: none; }
.lang-btn { background: transparent; color: #cfd8e3; border: 1px solid rgba(255,255,255,.25); border-radius: 5px; padding: 1px 7px; font-size: 10.5px; line-height: 1.5; font-weight: 600; cursor: pointer; }
.lang-btn[aria-pressed="true"] { background: #fff; color: #1b2a3a; }
.me b { display: block; color: #fff; font-weight: 600; }

/* Main */
.main { padding: 22px 26px 40px; min-width: 0; }
.head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 10px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.grid { display: grid; gap: 14px; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); margin-bottom: 14px; }
.kpi { min-width: 0; }
.kpi .v { font-size: clamp(17px, 4.6vw, 23px); font-weight: 700; letter-spacing: -.01em; overflow-wrap: anywhere; }
.kpi .l { color: var(--muted); font-size: 13px; }
.kpi.warn .v { color: var(--warn); }
.kpi-link { cursor: pointer; }
.kpi-link:hover, .kpi-link:focus-visible { box-shadow: 0 0 0 2px var(--accent, #2563eb); outline: none; }
.cols2 { grid-template-columns: 1.4fr 1fr; }
.cols2b { grid-template-columns: 1fr 1fr; margin-top: 14px; }
.cols3 { grid-template-columns: 1fr 1.1fr 1.5fr; }
#account-info th { text-align: left; color: var(--muted); font-weight: 500; width: 40%; }

/* Controls */
.btn { cursor: pointer; border: 1px solid var(--line); background: var(--surface); padding: 8px 14px; border-radius: 8px; font-weight: 500; }
.btn:hover { background: #f1f4fb; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, th button:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar input[type=search] { flex: 1; min-width: 180px; }
input[type=text], input[type=search], input[type=email], input[type=number], input[type=date], input[type=tel], select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid #cfd6e4; border-radius: 8px; background: #fff;
}
textarea { min-height: 72px; resize: vertical; }
input:disabled, select:disabled { background: #f1f3f8; color: var(--muted); }
.toolbar select { width: auto; min-width: 150px; }


/* Droplist có tìm kiếm (bọc <select>) */
.cb { position: relative; display: block; width: 100%; }
.cb select { opacity: 0; pointer-events: none; }
.cb .cb-input { position: absolute; inset: 0; width: 100%; height: 100%; padding-right: 28px; text-overflow: ellipsis;
  background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%23687089' stroke-width='1.6' stroke-linecap='round'/%3E%3C/svg%3E") no-repeat right 10px center; }
.cb .cb-input::placeholder { color: var(--muted); opacity: 1; }
.cb.disabled .cb-input { background-color: #f1f3f8; }
.toolbar .cb, .recbank .cb, .head .cb { display: inline-block; width: auto; }
.cb-menu { position: fixed; z-index: 100000; overflow-y: auto; background: #fff; border: 1px solid #cfd6e4; border-radius: 8px; box-shadow: 0 8px 24px rgba(20,30,60,.18); padding: 4px; }
.cb-opt { padding: 7px 10px; border-radius: 6px; cursor: pointer; white-space: nowrap; }
.cb-opt.active { background: #e8edff; }
.cb-opt.sel { font-weight: 700; }
.cb-opt.dis { color: var(--muted); cursor: not-allowed; }
.cb-none { padding: 8px 10px; color: var(--muted); }

.tx-attach { margin-top: 10px; padding-top: 8px; border-top: 1px dashed #dfe4ee; font-size: 13px; }
.tx-attach ul.plain { margin: 6px 0; }
.tx-attach .actions { margin-top: 6px; }
.dnd-over { outline: 2px dashed var(--brand); outline-offset: 2px; background: rgba(37,99,235,.05); }
.quickadd { margin-top: 4px; font-size: 13px; }
td.overdue, .overdue { color: var(--danger, #c0392b); font-size: 12px; }
.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; padding-top: 10px; }
.cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 24px; }
.dl { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0 0 12px; }
.dl dt { color: var(--muted); } .dl dd { margin: 0; overflow-wrap: anywhere; }
.ageing { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 14px; }
.ageing .age { flex: 1; min-width: 100px; padding: 8px 10px; border: 1px solid var(--border, #dfe4ee); border-radius: 8px; text-align: right; }
.notes { white-space: pre-wrap; }
@media print { .tabs, .toolbar, .head .actions, nav, #sidebar { display: none !important; } }
.cashchart .bars { height: 150px; } .card.inner { margin-bottom: 12px; box-shadow: none; }
.seg { display: inline-flex; gap: 4px; }
.crumbs { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 13px; margin: 0 0 8px; color: var(--muted, #64748b); }
.crumbs .sep { opacity: .6; } .crumbs .cur { color: inherit; font-weight: 600; }
.modal header .crumbs { margin: 0; flex: 1; }
/* Table */
.tablewrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td.wrap { white-space: normal; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); background: #f8f9fd; position: sticky; top: 0; }
th button { all: unset; cursor: pointer; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr:hover td { background: #fafbff; }
.actions { display: flex; gap: 6px; justify-content: flex-end; }
.pager { display: flex; align-items: center; justify-content: space-between; margin-top: 12px; color: var(--muted); font-size: 13px; gap: 10px; flex-wrap: wrap; }
.empty { text-align: center; color: var(--muted); padding: 34px 10px; }

/* Badges */
.badge { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: 12px; font-weight: 600; background: #eef0f6; color: #46506a; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.info { background: var(--info-soft); color: var(--info); }
.badge.brand { background: var(--brand-soft); color: var(--brand); }

/* Modal */
.overlay { position: fixed; inset: 0; background: rgba(15,22,45,.5); display: grid; place-items: center; padding: 16px; z-index: 20; }
.modal { background: #fff; border-radius: 14px; width: min(560px, 100%); max-height: 92vh; overflow: auto; box-shadow: 0 20px 60px rgba(10,15,40,.35); }
.modal header { padding: 16px 20px 0; display: flex; justify-content: space-between; align-items: center; }
.modal h2 { margin: 0; font-size: 18px; }
.modal .body { padding: 14px 20px; display: grid; gap: 12px; }
.docpage .body { padding: 0; display: grid; gap: 12px; } .docpage footer { margin-top: 16px; display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 8px; }
.modal footer { padding: 12px 20px 18px; display: flex; justify-content: flex-end; gap: 8px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 4px; }
.field .req { color: var(--bad); }
.field .err { color: var(--bad); font-size: 12.5px; margin-top: 3px; }
.field.has-err input, .field.has-err select, .field.has-err textarea { border-color: var(--bad); }
.field .hint { color: var(--muted); font-size: 12px; margin-top: 3px; }
.check { display: flex; gap: 8px; align-items: center; }
.check input { width: auto; }

/* Charts (CSS thuần, không cần thư viện) */
.bars { display: flex; align-items: flex-end; gap: 12px; height: 170px; padding-top: 8px; }
.bar { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 4px; min-width: 0; }
.bar .col { width: 100%; max-width: 56px; background: var(--brand); border-radius: 6px 6px 0 0; min-height: 2px; }
.bar .val { font-size: 11px; color: var(--muted); }
.bar .lab { font-size: 12px; color: var(--muted); }
.hbar { display: grid; grid-template-columns: 110px 1fr 36px; gap: 8px; align-items: center; margin: 6px 0; font-size: 13px; }
.hbar .track { background: #edf0f7; border-radius: 99px; height: 10px; overflow: hidden; }
.hbar .fill { background: var(--brand); height: 100%; border-radius: 99px; }
ul.plain { list-style: none; margin: 0; padding: 0; }
ul.plain li { display: flex; justify-content: space-between; gap: 10px; padding: 7px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
ul.plain li:last-child { border-bottom: 0; }

/* Toast */
.toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 40; }
.toast { background: #17203a; color: #fff; padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow); max-width: 360px; }
.toast.bad { background: var(--bad); }
.toast.ok { background: var(--ok); }

/* Màn hình lỗi toàn trang */
.fatal { grid-column: 1 / -1; display: grid; place-items: center; min-height: 70vh; padding: 20px; }
.fatal .card { max-width: 480px; text-align: center; }

@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .side { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: 10px; }
  .brand { padding: 0 8px 0 4px; }
  .nav { flex-direction: row; flex-wrap: wrap; }
  .side .me { margin: 0 0 0 auto; border: 0; padding: 0 6px; }
  .main { padding: 16px 12px 40px; }
  .cols2, .cols2b, .cols3 { grid-template-columns: 1fr; }
  .nav { width: 100%; overflow: visible; }
  .brand .logo img { max-width: 64px; }
  .nav .sub { flex: 1 0 100%; flex-direction: row; flex-wrap: wrap; border-left: 0; margin: 0 0 4px; padding: 0; }
}

/* Kế toán */
.muted { color: var(--muted); font-size: 13px; }
.linkbtn { all: unset; cursor: pointer; color: var(--brand); font-weight: 600; }
.linkbtn:hover { text-decoration: underline; }
.numlink { font-weight: 500; }
.linkbtn:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.modal.wide { width: min(900px, 100%); }
.dochead { display: flex; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.docno { font-size: 20px; font-weight: 700; }
.docmeta { text-align: right; display: grid; gap: 2px; }
.totals { margin-left: auto; width: min(320px, 100%); display: grid; gap: 4px; }
.trow { display: flex; justify-content: space-between; font-variant-numeric: tabular-nums; }
.trow.strong { font-weight: 700; border-top: 1px solid var(--line); padding-top: 4px; }
.lineform { background: #f8f9fd; border: 1px dashed #cfd6e4; border-radius: 10px; padding: 12px; display: grid; gap: 10px; }
.lf-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; }
.lineform .err { color: var(--bad); font-size: 13px; }
.txhead { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; margin-bottom: 6px; }
.txamt { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }
.txamt.in { color: var(--ok); } .txamt.out { color: var(--bad); }
.card.tx { margin-bottom: 12px; }
.tabs .btn[aria-selected="true"] { cursor: default; }
tr.sect td { font-weight: 700; background: #f8f9fd; }
tr.strong td { font-weight: 700; border-top: 2px solid var(--line); }
tr.neg td { color: var(--bad); } tr.pos td { color: var(--ok); }
.bar .pair { display: flex; align-items: flex-end; gap: 3px; height: 100%; width: 100%; justify-content: center; }
.bar .pair .col { max-width: 24px; }
.bar .col.exp { background: #e0803a; }
.legend { color: var(--muted); font-size: 12px; margin-top: 8px; }
.legend .dot { display: inline-block; width: 10px; height: 10px; border-radius: 3px; background: var(--brand); margin-left: 4px; }
.legend .dot.exp { background: #e0803a; }
@media print {
  #app, .toasts, .noprint { display: none !important; }
  .overlay { position: static; background: none; padding: 0; display: block; }
  .modal { box-shadow: none; max-height: none; width: 100%; overflow: visible; }
  .modal header button { display: none; }
  body { background: #fff; }
}
.dropzone { text-align: center; padding: 28px 16px; border: 2px dashed var(--border, #cbd5e1); cursor: pointer; margin-bottom: 12px; }
.dropzone.over, .dropzone:focus-visible { border-color: var(--accent, #2563eb); background: rgba(37,99,235,.06); outline: none; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.rec-card { display: grid; grid-template-columns: minmax(220px, 1fr) minmax(0, 1.6fr); gap: 16px; align-items: start; }
.rec-left .desc { margin: 2px 0 6px; word-break: break-word; }
.rec-right { border-left: 1px solid var(--line); padding-left: 16px; min-width: 0; }
.ptabs { display: flex; gap: 4px; margin-bottom: 10px; border-bottom: 1px solid var(--line); }
.ptab { all: unset; cursor: pointer; padding: 6px 12px; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; }
.ptab[aria-selected="true"] { color: var(--brand); border-bottom-color: var(--brand); }
.ptab:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.rec-right ul.plain li { display: flex; justify-content: space-between; gap: 10px; align-items: center; }
.findrow { display: flex; gap: 8px; margin-top: 8px; }
.findrow input { flex: 1; }
.fgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.check { display: flex; gap: 6px; align-items: center; margin: 8px 0; font-size: 13px; }
.balstrip { display: flex; gap: 32px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.balstrip .v { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.balstrip .grow { flex: 1; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: #98a2b3; }
.dot.on { background: var(--ok); }
.banner { background: var(--info-soft); color: var(--info); padding: 10px 14px; border-radius: 8px; margin-bottom: 12px; display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.okbox { background: var(--ok-soft); color: var(--ok); padding: 8px 12px; border-radius: 8px; margin-bottom: 8px; }
.menu { position: relative; display: inline-block; }
.menu summary { list-style: none; cursor: pointer; }
.menu summary::-webkit-details-marker { display: none; }
.menu-items { position: absolute; right: 0; top: 100%; z-index: 20; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow); padding: 6px; min-width: 240px; display: grid; }
.menu-items .mi { padding: 7px 10px; text-align: left; }
.menu-items .mi:hover { background: var(--info-soft); text-decoration: none; }
.modal.wide { width: min(760px, 100%); }
#recon-report tr.tot td { font-weight: 700; border-top: 1px solid var(--line); }
#recon-report tr.grp td { font-weight: 600; background: #f6f7fb; }
.recbank select { font-size: 16px; font-weight: 700; padding: 5px 8px; max-width: 100%; min-width: 260px; }
.banktiles { display: grid; gap: 10px; }
.banktile { border: 1px solid var(--line); border-radius: 10px; padding: 12px; display: grid; gap: 6px; }
.banktile .nm { font-weight: 700; }
.banktile .nums { display: flex; gap: 22px; flex-wrap: wrap; }
.banktile .nums .v { font-weight: 700; font-variant-numeric: tabular-nums; }
@media (max-width: 800px) { .rec-card { grid-template-columns: 1fr; } .rec-right { border-left: 0; padding-left: 0; } .fgrid { grid-template-columns: 1fr; } }
#cash-table select, #cash-table input[type=text] { min-width: 170px; width: 100%; }
#cash-table td:nth-child(3) { min-width: 200px; }
