:root {
  --primary: #1a5f9e;
  --border: #1a5f9e;
  --bg: #f0f4f8;
  --card: #ffffff;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: 'Roboto', 'Segoe UI', Tahoma, sans-serif;
  background: var(--bg);
  color: #222;
  line-height: 1.45;
  padding: 20px;
}
.container {
  max-width: 1100px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
}
@media (max-width: 900px) {
  .container { grid-template-columns: 1fr; }
}
.card {
  background: var(--card);
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.08);
  padding: 24px;
}
h1 {
  font-size: 1.4rem;
  color: var(--primary);
  margin-bottom: 6px;
}
label {
  display: block;
  font-size: 0.85rem;
  font-weight: 500;
  margin-bottom: 4px;
  color: #444;
}
input, select, textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid #ccd6e0;
  border-radius: 8px;
  font-size: 0.95rem;
  font-family: inherit;
  transition: border 0.2s;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(26,95,158,0.15);
}
.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 12px;
}
.form-row.full { grid-template-columns: 1fr; }
.section-title {
  font-size: 1rem;
  font-weight: 700;
  color: var(--primary);
  margin: 18px 0 10px;
  padding-bottom: 6px;
  border-bottom: 2px solid #e8eef5;
}
table.items {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 0.9rem;
}
table.items th {
  background: #e8f0f8;
  padding: 8px 6px;
  text-align: left;
  font-weight: 600;
  border: 1px solid #c5d5e8;
}
table.items td {
  padding: 6px;
  border: 1px solid #dde5ee;
  vertical-align: middle;
}
table.items input {
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 0.9rem;
}
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 18px;
  border: none;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}
.btn-primary {
  background: var(--primary);
  color: white;
}
.btn-primary:hover { background: #144a7a; }
.btn-outline {
  background: white;
  border: 1px solid #ccd6e0;
  color: #333;
}
.btn-outline:hover { background: #f5f8fb; }
.btn-danger {
  background: #e74c3c;
  color: white;
  padding: 6px 10px;
  font-size: 0.8rem;
}
.btn-danger:hover { background: #c0392b; }
.actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 16px;
}
.preview-wrapper {
  position: sticky;
  top: 20px;
}
/* ================= BỔ SUNG CHO BẢN ONLINE ================= */
[hidden] { display: none !important; }
body.app { padding: 0; }
.topbar {
  background: var(--primary); color: #fff; position: sticky; top: 0; z-index: 50;
  box-shadow: 0 2px 10px rgba(0,0,0,0.12);
}
.topbar-inner {
  max-width: 1100px; margin: 0 auto; padding: 8px 20px;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 1rem; white-space: nowrap; }
.tabs { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.tab {
  background: transparent; color: #dce9f6; border: none; padding: 8px 12px; border-radius: 8px;
  font: inherit; font-size: 0.9rem; cursor: pointer;
}
.tab:hover { background: rgba(255,255,255,0.12); color: #fff; }
.tab.active { background: #fff; color: var(--primary); font-weight: 600; }
main { padding: 20px; }
.view-head { max-width: 1100px; margin: 0 auto 14px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.view-head h2 { font-size: 1.2rem; color: var(--primary); flex: 1; }
.card + .card { margin-top: 0; }
.wide { max-width: 1100px; margin: 0 auto; }
.status-bar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; font-size: 0.85rem; color: #555; }
.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 0.78rem; font-weight: 600; white-space: nowrap; }
.badge.draft { background: #fff4d6; color: #8a6100; }
.badge.issued { background: #dff5e3; color: #1e7a34; }
.badge.new { background: #e8eef5; color: #444; }
.dirty { color: #c0392b; font-weight: 500; }
.btn-success { background: #1e8e3e; color: #fff; }
.btn-success:hover { background: #17722f; }
.btn-sm { padding: 6px 10px; font-size: 0.82rem; }
.btn:disabled { opacity: 0.55; cursor: default; }
.muted { color: #888; font-size: 0.85rem; }
/* Autocomplete */
.ac-wrap { position: relative; }
.ac-list {
  position: absolute; left: 0; right: 0; top: 100%; z-index: 40; background: #fff;
  border: 1px solid #ccd6e0; border-radius: 8px; margin-top: 2px; box-shadow: 0 8px 24px rgba(0,0,0,0.12);
  max-height: 260px; overflow-y: auto;
}
.ac-item { padding: 8px 12px; cursor: pointer; font-size: 0.88rem; border-bottom: 1px solid #f0f3f7; }
.ac-item:last-child { border-bottom: none; }
.ac-item:hover, .ac-item.active { background: #eef4fb; }
.ac-item small { display: block; color: #777; }
/* Bảng danh sách */
.filters { display: grid; grid-template-columns: 2fr 1fr 1fr 1fr auto; gap: 10px; align-items: end; margin-bottom: 14px; }
.table-wrap { overflow-x: auto; }
table.list { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
table.list th { background: #e8f0f8; text-align: left; padding: 8px; border-bottom: 2px solid #c5d5e8; white-space: nowrap; }
table.list td { padding: 8px; border-bottom: 1px solid #e6ecf2; vertical-align: top; }
table.list tr:hover td { background: #f7fafd; }
table.list td.num { text-align: right; white-space: nowrap; }
table.list .row-actions { display: flex; gap: 4px; flex-wrap: wrap; }
.summary { margin-top: 10px; font-size: 0.88rem; color: #444; display: flex; justify-content: space-between; flex-wrap: wrap; gap: 8px; }
.items-tools { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.items-tools select { width: auto; max-width: 100%; flex: 1; min-width: 180px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; max-width: 1100px; margin: 0 auto; }
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #222; color: #fff;
  padding: 10px 18px; border-radius: 8px; font-size: 0.9rem; z-index: 100; box-shadow: 0 6px 20px rgba(0,0,0,0.25);
}
.toast.error { background: #c0392b; }
.toast.ok { background: #1e8e3e; }
/* Đăng nhập */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-card { width: 100%; max-width: 380px; }
.login-card .btn { width: 100%; justify-content: center; margin-top: 14px; }
.error-text { color: #c0392b; font-size: 0.88rem; margin-top: 10px; min-height: 1.2em; }
.fab-preview { display: none; }
.invoice-scroll { overflow-x: auto; }
.container > *, .grid2 > * { min-width: 0; }
.empty { text-align: center; color: #888; padding: 30px 10px; }

@media (max-width: 900px) {
  .grid2 { grid-template-columns: 1fr; }
  .preview-wrapper { position: static; }
  .fab-preview { display: inline-flex; }
}
@media (max-width: 640px) {
  main { padding: 12px; }
  .card { padding: 16px; }
  .topbar-inner { padding: 8px 12px; }
  .tabs { order: 3; width: 100%; flex: none; overflow-x: auto; flex-wrap: nowrap; }
  .tab { white-space: nowrap; }
  .form-row { grid-template-columns: 1fr; }
  .filters { grid-template-columns: 1fr 1fr; }
  .filters .f-q { grid-column: 1 / -1; }
  /* bảng nhập hàng hóa thành dạng thẻ */
  table.items thead { display: none; }
  table.items, table.items tbody, table.items tr, table.items td { display: block; width: 100%; }
  table.items tr { display: grid; grid-template-columns: 34px 1fr 1fr 40px; gap: 6px; padding: 8px; border: 1px solid #dde5ee; border-radius: 8px; margin-bottom: 8px; }
  table.items td { border: none; padding: 0; }
  table.items td:nth-child(1) { grid-row: 1 / span 2; align-self: center; text-align: center; font-weight: 600; }
  table.items td:nth-child(2) { grid-column: 2 / 4; }
  table.items td:nth-child(6) { grid-column: 4; grid-row: 1 / span 2; align-self: center; }
  table.items td:nth-child(3), table.items td:nth-child(4), table.items td:nth-child(5) { grid-row: 2; }
  table.items tr { grid-template-columns: 30px 1fr 0.8fr 1.2fr 40px; }
  table.items td:nth-child(2) { grid-column: 2 / 5; grid-row: 1; }
  table.items td:nth-child(3) { grid-column: 2; }
  table.items td:nth-child(4) { grid-column: 3; }
  table.items td:nth-child(5) { grid-column: 4; }
  table.items td:nth-child(6) { grid-column: 5; }
  table.list thead { display: none; }
  table.list, table.list tbody, table.list tr, table.list td { display: block; width: 100%; }
  table.list tr { border: 1px solid #dde5ee; border-radius: 8px; margin-bottom: 10px; padding: 6px 4px; }
  table.list td { border: none; padding: 3px 8px; }
  table.list td.num { text-align: left; }
  table.list td[data-label]::before { content: attr(data-label) ": "; color: #888; font-size: 0.8rem; }
}

@media print {
  body, body.app { background: white; padding: 0; }
  .topbar, .view:not(#view-editor), .toast, .no-print { display: none !important; }
  main { padding: 0; }
  .container { display: block; }
  .form-side { display: none !important; }
  .preview-wrapper { position: static; }
  .preview-wrapper > .card { box-shadow: none; padding: 0 !important; }
  .invoice-scroll { overflow: visible; }
  .invoice-scroll .invoice { min-width: 0; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
#previewAnchor { scroll-margin-top: 110px; }

/* ================= v3: nút nhỏ gọn đồng màu xanh nhạt, chọn người bán, xem trước vừa màn hình ================= */
:root { --btn-bg: #e3f0fc; --btn-bg-hover: #cfe5fa; --btn-border: #b9d6f2; --btn-text: #1a5f9e; }
.btn, .btn-primary, .btn-outline, .btn-success, .btn-danger, .btn-sm, .tab {
  display: inline-flex; align-items: center; justify-content: center; gap: 4px;
  padding: 3px 10px; min-height: 26px; border-radius: 6px;
  font: inherit; font-size: 0.8rem; font-weight: 500; line-height: 1.3; white-space: nowrap;
  background: var(--btn-bg); color: var(--btn-text); border: 1px solid var(--btn-border);
  cursor: pointer; transition: background .15s, border-color .15s;
}
.btn:hover, .btn-primary:hover, .btn-outline:hover, .btn-success:hover, .btn-danger:hover, .tab:hover {
  background: var(--btn-bg-hover); color: var(--btn-text); border-color: #9cc5ec;
}
.btn-danger, .btn-danger:hover { color: #c0392b; }
.tab.active { background: #fff; color: var(--primary); border-color: #fff; font-weight: 600; }
.tab-logout { margin-left: auto; }
.topbar-inner { max-width: 1500px; padding: 6px 20px; gap: 10px; }
.brand { font-size: 0.95rem; }
.tabs { gap: 6px; }
.actions { gap: 6px; margin-top: 12px; }
.items-tools { gap: 6px; }
.login-card h1 { margin-bottom: 14px; }
.login-card .btn { width: auto; margin-top: 12px; padding: 4px 18px; }
.check { display: flex; align-items: center; gap: 6px; font-weight: 400; margin: 2px 0 4px; cursor: pointer; }
.check input { width: auto; }
.sub-title { font-weight: 600; font-size: 0.9rem; color: #444; margin-bottom: 8px; }
/* Chọn người bán: mỗi người bán một dòng */
.seller-pick-row { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 4px; }
.seller-pick-label { font-size: 0.85rem; font-weight: 600; color: var(--primary); padding-top: 5px; white-space: nowrap; }
.seller-pick { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; border: 1px solid #ccd6e0; border-radius: 8px; padding: 3px 8px; max-height: 112px; overflow-y: auto; font-size: 0.88rem; }
.seller-opt { display: flex; align-items: center; gap: 6px; cursor: pointer; padding: 2px 0; min-width: 0; }
.seller-opt input { width: auto; margin: 0; flex: none; box-shadow: none; }
.seller-opt .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.seller-opt small { color: #888; white-space: nowrap; flex: none; }
/* Khung soạn rộng hơn trên màn hình lớn để bản xem trước to hơn */
#view-editor .container { max-width: 1500px; gap: 20px; }
.preview-card { padding: 12px; }
.invoice-scroll.fit { overflow: hidden; }
.invoice-scroll.fit .invoice { transform-origin: top left; min-width: 0; }
@media (min-width: 901px) {
  #view-editor .container { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .preview-wrapper { align-self: start; }
}
@media (max-width: 640px) {
  .seller-opt small { display: none; }
  .tab-logout { margin-left: auto; }
}
@media print {
  .invoice-scroll, .invoice-scroll.fit { height: auto !important; overflow: visible !important; }
  .invoice, .invoice-scroll.fit .invoice, .invoice-scroll .invoice { transform: none !important; width: 210mm !important; margin-left: 0 !important; }
}

/* ================= v4: hóa đơn xem trước / in theo đúng mẫu HĐ GTGT (khổ A4) ================= */
.invoice {
  position: relative; box-sizing: border-box; width: 210mm; height: 297mm; overflow: hidden;
  background: #fff url(/img/invoice-border.jpg) 3.2pt 0.3pt / 98.8% 98.8% no-repeat;
  color: #000; font-family: "Times New Roman", Tinos, "Liberation Serif", Times, serif; font-size: 9.6pt; line-height: 1.2;
  box-shadow: 0 2px 10px rgba(0,0,0,0.12); text-align: left;
}
.invoice * { box-sizing: border-box; }
.invoice .vi-en, .invoice i { font-style: italic; }
.invoice .vi-en { font-size: 9.4pt; }
.invoice b { font-weight: 700; }
.invoice .vi-emblem {
  position: absolute; left: 20.1%; top: 29.2%; width: 59.9%; aspect-ratio: 1; pointer-events: none;
  background: url(/img/invoice-emblem.jpg) center / contain no-repeat; opacity: 0.32; mix-blend-mode: multiply;
}
.invoice .draft-stamp {
  position: absolute; top: 40%; left: 50%; transform: translate(-50%, -50%) rotate(-22deg);
  font: 700 32pt/1 Arial, Helvetica, sans-serif; letter-spacing: 3pt; color: rgba(210,40,40,0.16);
  border: 2pt solid rgba(210,40,40,0.18); border-radius: 5pt; padding: 3pt 10pt; pointer-events: none; z-index: 3; white-space: nowrap;
}
.invoice .vi-page { position: absolute; left: 31pt; right: 32pt; top: 29.5pt; transform-origin: top left; z-index: 1; }
.invoice .vi-head { position: relative; height: 66pt; padding-right: 44pt; text-align: center; }
.invoice .vi-title { font-weight: 700; font-size: 13.6pt; line-height: 17pt; margin-top: 2.4pt; }
.invoice .vi-title-en { font-weight: 700; font-style: italic; font-size: 12.6pt; line-height: 17pt; margin-top: -0.2pt; }
.invoice .vi-title-en + .vi-date { margin-top: -0.7pt; }
.invoice .vi-date { font-style: italic; line-height: 17.2pt; }
.invoice .vi-serial { position: absolute; right: 0; width: 129pt; top: -1.4pt; text-align: left; line-height: 17.2pt; white-space: nowrap; }
.invoice .vi-serial > div { display: flex; }
.invoice .vi-serial .vi-k { width: 66.8pt; flex: none; }
.invoice .vi-seller { position: relative; border-top: 0.75pt solid #9a9a9a; border-bottom: 0.75pt solid #9a9a9a; padding: 4.1pt 72pt 0 0; }
.invoice .vi-sname { font-weight: 700; font-size: 14pt; line-height: 17pt; font-size: 13.5pt; text-transform: uppercase; margin-bottom: -0.8pt; }
.invoice .vi-qr { position: absolute; right: 4pt; top: 8pt; width: 60pt; height: 60pt; }
.invoice .vi-line { line-height: 17.2pt; min-height: 17.2pt; }
.invoice .vi-buyer { padding: 0; }
.invoice .vi-pay { display: flex; }
.invoice .vi-pay > div:first-child { width: 266.8pt; flex: none; }
.invoice .vi-table { width: 100%; border-collapse: collapse; table-layout: fixed; margin: 0; }
.invoice .vi-table th, .invoice .vi-table td { border: 0.75pt solid #776b6b; padding: 0 3pt; vertical-align: middle; font-size: 9.65pt; }
.invoice .vi-table td.r, .invoice .vi-sum b, .invoice .vi-words b { font-size: 9.8pt; }
.invoice .vi-table td i { font-size: 9.4pt; }
.invoice .vi-table th { font-size: 9.75pt; font-weight: 700; text-align: center; line-height: 13pt; padding: 2.8pt 2pt 2.4pt; }
.invoice .vi-table th div { font-style: italic; font-size: 9pt; line-height: 12pt; }
.invoice .vi-table td.c { text-align: center; }
.invoice .vi-table td.r { text-align: right; }
.invoice .vi-table td.l { text-align: left; }
.invoice .vi-item td { border-top-color: #cecece; border-bottom-color: #cecece; line-height: 13.8pt; padding-top: 1.6pt; padding-bottom: 1.6pt; word-wrap: break-word; }
.invoice .vi-empty td { height: 17.2pt; padding-top: 0; padding-bottom: 0; }
.invoice .vi-item td:nth-child(2) { padding-left: 3.8pt; }
.invoice .vi-tq td { line-height: 13.8pt; height: 18.7pt; padding-top: 0; padding-bottom: 0; }
.invoice .vi-sum td { line-height: 13.8pt; height: 17.8pt; padding: 1pt 2.5pt 1pt 2pt; }
.invoice .vi-sg { display: grid; grid-template-columns: 242pt 1fr auto; }
.invoice .vi-vr { display: flex; justify-content: space-between; padding-right: 6pt; }
.invoice .vi-signs { display: grid; grid-template-columns: 1fr 1fr; text-align: center; padding-top: 5.6pt; height: 128pt; }
.invoice .vi-st { font-weight: 700; line-height: 17.6pt; }
.invoice .vi-st i { font-size: 9.4pt; }
.invoice .vi-sn { font-style: italic; font-size: 9.8pt; line-height: 17.6pt; }
.invoice .vi-se { font-style: italic; font-size: 9.35pt; line-height: 17pt; }
.invoice .vi-foot { position: absolute; left: 31pt; right: 32pt; top: 765.5pt; line-height: 17.2pt; z-index: 1; }
.invoice .vi-check { text-align: center; }
.invoice .vi-link { color: #0563c1; text-decoration: underline; }
.invoice-scroll .invoice { min-width: 0; }
@media (max-width: 900px) {
  .invoice-scroll { overflow: hidden; }
  .invoice-scroll .invoice { transform-origin: top left; }
}
@page { size: A4 portrait; margin: 0; }
@media print {
  html, body { width: 210mm; margin: 0 !important; padding: 0 !important; }
  .preview-wrapper, .preview-wrapper > .card, .invoice-scroll { margin: 0 !important; padding: 0 !important; border: 0 !important; box-shadow: none !important; background: none !important; }
  .invoice { box-shadow: none; border: 0; page-break-inside: avoid; break-inside: avoid; }
  main, .container, #view-editor { margin: 0 !important; padding: 0 !important; max-width: none !important; }
}

/* v5: các ô ít dùng ẩn gọn */
.more-toggle-row { display: flex; align-items: center; gap: 8px; margin: 2px 0 6px; min-width: 0; }
.more-toggle { min-height: 22px; padding: 1px 8px; font-size: 0.76rem; }
.more-summary { font-size: 0.78rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.more-fields { border-left: 2px solid var(--btn-border); padding-left: 10px; margin-bottom: 4px; }

/* v6: các trang Đã lưu / Khách hàng / Cài đặt cùng bề rộng & mép với trang Soạn */
.wide, .grid2, .view-head { max-width: 1500px; }
.grid2 { gap: 20px; }
@media (min-width: 901px) { .grid2 { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } }
.card > .view-head { max-width: none; margin-left: 0; margin-right: 0; }

/* v6: giao diện phẳng — bỏ mọi đổ bóng/gradient, dùng viền mảnh 1px */
*, *::before, *::after { box-shadow: none !important; text-shadow: none !important; }
@media screen {
  .card { border: 1px solid #e1e7ee; }
  .topbar { border-bottom: 1px solid rgba(0,0,0,0.08); }
  .ac-list { border: 1px solid #d5dde6; }
  .invoice { border: 1px solid #e1e7ee; }
  input:focus, select:focus, textarea:focus { border-color: var(--primary); }
}
/* v6: chọn nhanh khách đã lưu */
.buyer-head { display: flex; align-items: center; gap: 10px; }
.buyer-head > span { flex: none; }
.buyer-pick { flex: 1; min-width: 0; max-width: 340px; margin-left: auto; width: auto; padding: 3px 8px; font-size: 0.85rem; font-weight: 400; color: #333; min-height: 0; height: auto; }
@media (min-width: 901px) { .actions .fab-preview { display: none; } }
/* v7 */
.buyer-pick { width: 100%; max-width: none; margin: 0; }
@media (min-width: 901px) { .grid2.grid-60-40 { grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); } }
.items-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.items-head > span { flex: 1; }
.seg { display: inline-flex; border: 1px solid var(--btn-border, #b9d7f3); border-radius: 6px; overflow: hidden; font-size: 0.8rem; font-weight: 500; }
.seg label { display: inline-flex; margin: 0; cursor: pointer; }
.seg input { position: absolute; opacity: 0; width: 1px; height: 1px; pointer-events: none; }
.seg span { padding: 2px 10px; color: var(--primary); background: #fff; }
.seg label + label span { border-left: 1px solid var(--btn-border, #b9d7f3); }
.seg input:checked + span { background: var(--btn-bg, #e6f2fc); font-weight: 700; }
.seg input:focus-visible + span { outline: 2px solid var(--primary); outline-offset: -2px; }
.price-hint { font-size: 0.78rem; margin: 4px 0 0; }
/* v8 */
.buyer-head { display: flex; align-items: center; gap: 10px; }
.buyer-head > span { flex: none; }
.buyer-head .buyer-pick { flex: 1 1 auto; width: auto; min-width: 0; max-width: none; margin: 0; }
.pay-badge { border: 1px solid transparent; cursor: pointer; font-family: inherit; line-height: 1.45; }
.pay-badge:disabled { opacity: 0.6; cursor: wait; }
.badge.paid { background: #dff5e3; color: #1e7a34; border-color: #b8e3c2; }
.badge.unpaid { background: #fdeceb; color: #b3261e; border-color: #f4c7c3; }
@media (min-width: 641px) { .filters { grid-template-columns: 2fr 1fr 1fr 1fr 1fr auto; } }
