:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-2: #f8f9f6;
  --line: #dfe3da;
  --text: #1f2a1c;
  --muted: #65705f;
  --green: #4f7f42;
  --green-soft: #e6f0e1;
  --maroon: #7b1f14;
  --maroon-soft: #f7e7e4;
  --amber: #a86b00;
  --amber-soft: #fff3d6;
  --sel: #1d5fbf;
  --sel-soft: #e3edfc;
  --radius: 8px;
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}
button, input, select { font: inherit; color: inherit; }
.spacer { flex: 1; }
[hidden] { display: none !important; }

/* ---------- Thanh trên ---------- */
.topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand-logo { height: 40px; width: auto; }
.brand-text h1 { margin: 0; font-size: 17px; color: var(--maroon); }
.brand-text p { margin: 0; font-size: 12px; color: var(--muted); }
.sync { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.sync-status { font-size: 12px; color: var(--muted); text-align: right; }
.sync-status.busy { color: var(--amber); }
.sync-status.err { color: #b3261e; font-weight: 600; }
.sync-status.warn { color: var(--amber); }

.btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 6px;
  padding: 6px 12px; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--green); }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--green); border-color: var(--green); color: #fff; font-weight: 600; }
.btn-primary:hover { background: #426c37; }
.btn-ghost { background: transparent; }

/* ---------- Bộ lọc ---------- */
.filters { padding: 12px 16px 6px; display: grid; gap: 8px; }
.search input {
  width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); font-size: 15px;
}
.search input:focus { outline: 2px solid var(--green-soft); border-color: var(--green); }
.filter-row { display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; }
.filter-row label { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.filter-row select {
  padding: 5px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface);
  max-width: 230px; color: var(--text);
}
.filter-row .chk { color: var(--text); font-size: 13px; }
.result-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.result-row b { color: var(--text); }

/* ---------- Cây dữ liệu ---------- */
.tree { padding: 6px 16px 90px; display: grid; gap: 10px; }
.empty { padding: 40px; text-align: center; color: var(--muted); background: var(--surface); border-radius: var(--radius); }
.caret { display: inline-block; width: 14px; color: var(--muted); transition: transform .12s; }
.open > header .caret, .open > .det-head .caret { transform: rotate(90deg); }
.meta { font-size: 12px; color: var(--muted); }
.code { font: 12px ui-monospace, Consolas, monospace; color: var(--muted); }
.tag { font-size: 11px; padding: 1px 7px; border-radius: 10px; background: var(--green-soft); color: var(--green); }
.tag-set { background: var(--maroon-soft); color: var(--maroon); }
.note { margin: 0 0 8px; font-size: 12px; color: var(--amber); }

.mo { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.mo-head {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; padding: 10px 12px;
  cursor: pointer; background: var(--surface-2); border-bottom: 1px solid var(--line);
}
.mo:not(.open) .mo-head { border-bottom: 0; }
.mo-no { font-weight: 700; color: var(--maroon); font-size: 15px; }
.mo-cust { font-weight: 600; }
.mo-body { padding: 10px 12px; display: grid; gap: 8px; }

.det { border: 1px solid var(--line); border-radius: var(--radius); }
.det-head { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; padding: 8px 10px; cursor: pointer; }
.det.open .det-head { border-bottom: 1px dashed var(--line); }
.det-stt { font-size: 12px; font-weight: 600; color: var(--green); min-width: 52px; }
.det-name { font-weight: 600; }
.det-body { padding: 8px 10px 10px; display: grid; gap: 10px; }

.prog { display: inline-block; width: 70px; height: 6px; border-radius: 3px; background: #e5e8e1; overflow: hidden; }
.prog i { display: block; height: 100%; background: var(--green); }
.prog-t { font-size: 11px; color: var(--muted); min-width: 30px; }

.scope {
  border: 1px solid var(--line); background: var(--surface); border-radius: 6px; padding: 2px 8px;
  font-size: 12px; cursor: pointer; white-space: nowrap;
}
.scope.some { border-color: var(--sel); color: var(--sel); }
.scope.all { background: var(--sel); border-color: var(--sel); color: #fff; }
.link { border: 0; background: none; color: var(--sel); cursor: pointer; padding: 2px 4px; font-size: 12px; }
.link:hover { text-decoration: underline; }

.grp { border-left: 3px solid var(--line); padding-left: 10px; }
.grp-cum { border-left-color: var(--green); }
.grp-sp { border-left-color: var(--maroon); }
.grp-loose { border-left-color: var(--amber); }
.grp-head { display: flex; align-items: center; gap: 8px; padding: 2px 0 6px; font-size: 13px; }
.parts { display: grid; gap: 4px; }

.part {
  display: grid; grid-template-columns: minmax(220px, 300px) 1fr; gap: 10px;
  padding: 6px 8px; border-radius: 6px; background: var(--surface-2);
}
.part.kind-cum { background: var(--green-soft); }
.part.kind-sp { background: var(--maroon-soft); }
.part-name { font-weight: 600; }
.kind { font-size: 10px; font-weight: 500; color: var(--muted); border: 1px solid var(--line); border-radius: 8px; padding: 0 5px; margin-left: 4px; background: var(--surface); }
.part-sub { display: flex; flex-wrap: wrap; gap: 2px 10px; font-size: 12px; color: var(--muted); }
.part-act { margin-top: 3px; display: flex; gap: 6px; align-items: center; }
.steps { display: flex; flex-wrap: wrap; gap: 4px; align-content: flex-start; }
.step {
  display: inline-flex; align-items: center; gap: 4px; border: 1px solid var(--line); background: var(--surface);
  border-radius: 14px; padding: 2px 9px 2px 3px; font-size: 12px; cursor: pointer; line-height: 1.5;
}
.step .sn {
  display: inline-grid; place-items: center; min-width: 18px; height: 18px; border-radius: 9px;
  background: #eef0eb; font-size: 10px; font-weight: 700; color: var(--muted);
}
.step small { color: var(--muted); font-size: 10px; }
.step:hover { border-color: var(--sel); }
.step.doing { border-color: var(--amber); background: var(--amber-soft); }
.step.done { border-color: var(--green); background: var(--green-soft); }
.step.done .sn { background: var(--green); color: #fff; }
.step.sel { border-color: var(--sel); background: var(--sel-soft); box-shadow: inset 0 0 0 1px var(--sel); }
.step.sel .sn { background: var(--sel); color: #fff; }

/* ---------- Thanh chọn ---------- */
.selbar {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: 14px; z-index: 30;
  display: flex; align-items: center; gap: 10px; width: min(640px, calc(100% - 32px));
  padding: 10px 14px; background: var(--surface); border: 1px solid var(--sel);
  border-radius: 10px; box-shadow: 0 6px 24px rgba(0, 0, 0, .15);
}

/* ---------- Xem trước ---------- */
.preview { position: fixed; inset: 0; z-index: 50; background: #5d625a; overflow: auto; }
body.previewing { overflow: hidden; }
.pv-toolbar {
  position: sticky; top: 0; z-index: 2; display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.pv-toolbar label { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.pv-toolbar select { padding: 5px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); color: var(--text); }
.pv-toolbar input { width: 56px; padding: 5px 6px; border: 1px solid var(--line); border-radius: 6px; }
.print-pages { padding: 20px 0 40px; display: grid; justify-content: center; gap: 18px; }

/* ---------- Trang in & phiếu (đơn vị mm) ---------- */
.page { background: #fff; color: #000; box-shadow: 0 2px 12px rgba(0, 0, 0, .35); overflow: hidden; }
.page-a5 { width: 210mm; height: 147.5mm; }
.page-a4 { width: 210mm; height: 296.5mm; display: flex; flex-direction: column; }
.page-a4 .phieu { height: 148mm; }
.cut { border-top: 0.3mm dashed #999; height: 0; }

.phieu {
  width: 210mm; height: 147.5mm; padding: 6mm 7mm 5mm;
  display: flex; flex-direction: column;
  font-family: "Times New Roman", Times, serif; font-size: 10.5pt; color: #000;
}
.ph-title { text-align: center; font-weight: 700; font-size: 17pt; line-height: 1.1; margin-bottom: 2mm; letter-spacing: .2pt; }
.ph-head { display: grid; grid-template-columns: 30mm 1fr 25mm; gap: 3mm; align-items: center; }
.ph-logo img { width: 30mm; height: auto; display: block; }
.ph-info { border-collapse: collapse; width: 100%; table-layout: fixed; }
.ph-info th { text-align: right; font-weight: 400; white-space: nowrap; padding: 0.4mm 1.5mm 0.4mm 0; }
.ph-info td.sm { font-size: 9pt; white-space: normal; line-height: 1.05; }
.ph-info td {
  font-weight: 700; padding: 0.3mm 1mm; border-bottom: 0.2mm solid #bbb; height: 5.3mm;
  overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
}
.ph-info td small { font-weight: 400; font-size: 8pt; }
.ph-info td.big { font-size: 12pt; }
.ph-info td.fill { border-bottom: 0.2mm dotted #555; }
.ph-qr { text-align: center; }
.ph-qr svg { width: 23mm; height: 23mm; display: block; margin: 0 auto; }
.ph-id { font: 6.5pt/1.1 Arial, sans-serif; margin-top: 0.8mm; word-break: break-all; }
.ph-cert {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 3mm;
  margin: 1.5mm 0 1.2mm;
}
.ph-cert > b { font-size: 15pt; }
.ph-flow { font-size: 9pt; }
.ph-flow b { font-size: 10.5pt; }
.ph-due { font-size: 8.5pt; text-align: right; }
.ph-log { border-collapse: collapse; width: 100%; table-layout: fixed; }
.ph-log th, .ph-log td { border: 0.3mm solid #000; }
.ph-log th { font-weight: 700; height: 7.5mm; font-size: 10.5pt; }
.ph-log td { height: 7.6mm; }
.ph-sign {
  display: grid; grid-template-columns: 1fr 1fr; margin-top: 2mm; font-weight: 700; font-size: 11pt; text-align: center;
  flex: 1; min-height: 12mm;
}

/* ---------- In ---------- */
@media print {
  html, body { background: #fff; }
  body.previewing { overflow: visible; }
  #app { display: none !important; }
  .preview { position: static; overflow: visible; background: #fff; }
  .pv-toolbar { display: none !important; }
  .print-pages { display: block; padding: 0; }
  .page { box-shadow: none; margin: 0; break-after: page; page-break-after: always; }
  .page:last-child { break-after: auto; page-break-after: auto; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; }
  .sync { margin-left: 0; width: 100%; justify-content: space-between; }
  .part { grid-template-columns: 1fr; }
  .filter-row select { max-width: 160px; }
}
