:root {
  --bg: #f3f4f7;
  --surface: #ffffff;
  --ink: #1b2330;
  --muted: #667085;
  --line: #dfe3ea;
  --accent: #0b5cad;
  --accent-soft: #e7f0fb;
  --danger: #b8322a;
  --danger-soft: #fdecea;
  --ok: #17794a;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .1);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  color: var(--ink);
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); min-height: 100vh; }
h1, h2, h3, p { margin: 0; }
.muted { color: var(--muted); }

/* ---------- Thanh trên cùng ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px 20px; flex-wrap: wrap;
  padding: 10px 20px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 1.05rem; }
.nav { display: flex; gap: 4px; flex: 1; }
.nav a {
  padding: 7px 12px; border-radius: 8px;
  color: var(--muted); text-decoration: none; font-weight: 600; white-space: nowrap;
}
.nav a:hover { background: var(--bg); color: var(--ink); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.account { display: flex; align-items: center; gap: 10px; }

.view { padding: 20px; max-width: 1400px; margin: 0 auto; }

/* ---------- Thành phần chung ---------- */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px;
}
.card + .card { margin-top: 14px; }
.card h2 { font-size: 1rem; margin-bottom: 12px; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.card-head h2 { margin: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--surface);
  color: var(--ink); font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap; text-decoration: none;
}
.btn:hover { background: var(--bg); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #094c90; }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.small { min-height: 30px; padding: 0 10px; font-size: .88rem; }
.btn.block { width: 100%; }
.link { border: 0; background: none; padding: 0; color: var(--accent); font: inherit; font-weight: 600; cursor: pointer; text-decoration: underline; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.field { display: block; }
.field > span { display: block; margin-bottom: 4px; font-size: .88rem; font-weight: 600; color: var(--muted); }
input, select {
  width: 100%; min-height: 38px; padding: 6px 10px;
  border: 1px solid #c9cfd9; border-radius: 8px; background: #fff;
  color: var(--ink); font: inherit;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
.form-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 12px; }
.form-grid .wide { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.form-error { margin: 10px 0; padding: 8px 10px; border-radius: 8px; background: var(--danger-soft); color: var(--danger); font-size: .92rem; }
.hint { margin-top: 8px; font-size: .88rem; color: var(--muted); }

.calc {
  min-width: 0;
  display: grid; grid-template-columns: 1fr 1fr; gap: 12px;
  margin-top: 12px; padding: 10px 12px; border-radius: 8px; background: var(--bg);
}
.calc-label { display: block; font-size: .82rem; color: var(--muted); }
.calc-value { font-variant-numeric: tabular-nums; font-weight: 600; }
.calc-value.strong { font-size: 1.15rem; color: var(--accent); }

table.data { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
table.data th, table.data td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
table.data th { font-size: .82rem; color: var(--muted); font-weight: 600; }
table.data td.num, table.data th.num { text-align: right; }
table.data tbody tr:last-child td { border-bottom: 0; }
table.data tr.editing td { background: var(--accent-soft); }
table.data tr.clickable { cursor: pointer; }
table.data tr.clickable:hover td { background: var(--bg); }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.table-scroll { overflow-x: auto; }
.empty { padding: 18px 8px; text-align: center; color: var(--muted); }
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-size: .82rem; font-weight: 600; }
.badge.off { background: var(--bg); color: var(--muted); }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 50;
  max-width: min(92vw, 520px); padding: 10px 16px; border-radius: 8px;
  background: #1b2330; color: #fff; box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
}
.toast.err { background: var(--danger); }

/* ---------- Đăng nhập ---------- */
.login-page { display: grid; place-items: center; padding: 20px; }
.login-card {
  width: min(100%, 380px); padding: 28px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow);
}
.login-card h1 { font-size: 1.35rem; }
.login-card p.muted { margin: 4px 0 18px; }
.login-card .field { margin-bottom: 12px; }
.login-card .btn { margin-top: 6px; }

/* ---------- Màn hình lập biên nhận ---------- */
.editor { display: grid; grid-template-columns: minmax(340px, 420px) minmax(0, 1fr); gap: 20px; align-items: start; }
.side, .stage { min-width: 0; }
.stage { position: sticky; top: 72px; }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 12px; }
.toolbar .title { font-weight: 700; margin-right: auto; }
.toolbar .group { display: flex; align-items: center; gap: 6px; }
.toolbar select { width: auto; }
.paper-wrap { max-height: calc(100vh - 150px); overflow: auto; padding: 4px; }

.line-list { list-style: none; margin: 0; padding: 0; }
.line-list li { display: flex; align-items: center; gap: 10px; padding: 8px 6px; border-bottom: 1px solid var(--line); border-radius: 6px; }
.line-list li:last-child { border-bottom: 0; }
.line-list li.editing { background: var(--accent-soft); }
.line-no { width: 1.6em; text-align: center; color: var(--muted); font-variant-numeric: tabular-nums; }
.line-main { flex: 1; min-width: 0; }
.line-sub { color: var(--muted); font-size: .86rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.line-amount { font-variant-numeric: tabular-nums; white-space: nowrap; }

.invoice-list { list-style: none; margin: 0 0 10px; padding: 0; }
.invoice-list li { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.invoice-list .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; color: var(--accent); }
.invoice-list .size { color: var(--muted); font-size: .85rem; white-space: nowrap; }
.file-input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

/* ---------- Tờ biên nhận (xem trước và in) ---------- */
.paper {
  container-type: inline-size;
  width: 100%; max-width: 820px; margin: 0 auto;
  background: #fff; color: #000; border: 1px solid var(--line); box-shadow: var(--shadow);
  font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
}
.paper-inner { padding: 3.2em 2.6em 3.6em; font-size: clamp(7.5px, 2cqw, 15px); line-height: 1.3; }
.p-title { margin: 0; text-align: center; font-size: 1.75em; font-weight: 700; }
.p-sub { margin: .35em 0 1.6em; text-align: center; font-size: 1.1em; }
.p-table { width: 100%; border-collapse: collapse; table-layout: fixed; }
.p-table th, .p-table td {
  border: 1px solid #000; padding: .5em .35em;
  text-align: center; vertical-align: middle; overflow-wrap: anywhere;
}
.p-table th { font-weight: 700; }
.p-table .c1 { width: 8%; }
.p-table .c2 { width: 12%; }
.p-table .c3 { width: 17%; }
.p-table .c4 { width: 14%; }
.p-table .c5 { width: 16%; }
.p-table .c6 { width: 13%; }
.p-table .c7 { width: 20%; }
.p-table .p-total-label, .p-table .p-total { font-weight: 700; }
.p-table .p-placeholder { color: #888; font-style: italic; padding: 1.4em .5em; }
.p-sign { width: 40%; margin: 2em 4% 0 auto; text-align: center; }
.p-sign-title { font-weight: 700; font-style: italic; }
.p-sign-space { height: 7em; }
.p-sign-date { margin-top: .25em; }

/* ---------- Cài đặt ---------- */
.settings { display: grid; gap: 14px; max-width: 860px; }
.settings .card + .card { margin-top: 0; }
.routes-table input { min-width: 90px; }
.routes-table td { padding: 6px 6px 6px 0; }

.list-filters { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; margin-bottom: 14px; }
.list-filters .field { width: 170px; }
.list-filters .spacer { flex: 1; }

/* ---------- Màn hình nhỏ ---------- */
@media (max-width: 900px) {
  .editor { grid-template-columns: minmax(0, 1fr); }
  .stage { position: static; }
  .paper-wrap { max-height: none; }
  .view { padding: 12px; }
  .topbar { padding: 8px 12px; }
  .nav { order: 3; flex-basis: 100%; }
  .account { margin-left: auto; }
}

/* ---------- In ---------- */
@media print {
  html, body { background: #fff; }
  .topbar, .side, .toolbar, .toast { display: none !important; }
  .view, .stage, .paper-wrap { display: block; position: static; max-width: none; max-height: none; margin: 0; padding: 0; overflow: visible; }
  .editor { display: block; }
  .paper { max-width: none; border: 0; box-shadow: none; }
  .paper-inner { padding: 0; font-size: 12pt; }
  body[data-paper="A4-landscape"] .paper-inner { font-size: 13pt; }
  body[data-paper="A5-portrait"] .paper-inner { font-size: 8.5pt; }
  body[data-paper="A5-landscape"] .paper-inner { font-size: 10pt; }
  .p-table tr { break-inside: avoid; }
  .p-sign { break-inside: avoid; }
  .p-table .p-placeholder { visibility: hidden; }
}
