:root {
  --bg: #f4f5f7; --panel: #fff; --ink: #1d2330; --muted: #6b7280; --line: #dfe3ea;
  --accent: #1f5f8b; --accent-ink: #fff; --accent-soft: #e6f0f7;
  --err: #b42318; --err-soft: #fdecea; --warn: #9a6700; --warn-soft: #fff4d6; --ok: #1a7f37; --info: #3b5b9a;
  --res: #f7f9fb; --ovr: #7a3ea8;
  font: 13px/1.4 "Segoe UI", Arial, sans-serif; color: var(--ink);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; background: var(--bg); }
body { display: flex; flex-direction: column; overflow: hidden; }
button, input, select { font: inherit; color: inherit; }
button { border: 1px solid var(--line); background: #fff; border-radius: 5px; padding: 4px 10px; cursor: pointer; }
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.icon { padding: 2px 7px; }
button.mini { padding: 1px 6px; font-size: 12px; }
button.ghost { border-color: transparent; background: transparent; }
button.danger:hover { color: var(--err); border-color: var(--err); }
button.link { border: 0; background: none; color: var(--accent); padding: 0; }
input[type=text], select { border: 1px solid var(--line); border-radius: 4px; padding: 3px 5px; background: #fff; min-width: 0; }
input[type=text]:focus, select:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
input.num { text-align: right; width: 70px; }
input.bad { border-color: var(--err); background: var(--err-soft); }
h2 { font-size: 15px; margin: 0; } h3 { font-size: 14px; margin: 0; } h4 { margin: 12px 0 4px; font-size: 12px; text-transform: uppercase; color: var(--muted); letter-spacing: .03em; }
.hint { color: var(--muted); font-size: 12px; }
.err { color: var(--err); }

/* Thanh công cụ */
#topbar { background: var(--panel); border-bottom: 1px solid var(--line); }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; padding: 6px 12px; }
.brand { font-size: 14px; white-space: nowrap; } .brand b { color: var(--accent); }
.prjname { display: flex; gap: 4px; } .prjname input { width: 260px; font-weight: 600; }
.group { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.group .sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; }
.settings label { color: var(--muted); white-space: nowrap; } .settings select { color: var(--ink); }
a.helplink { color: var(--accent); margin-left: 6px; text-decoration: none; } a.helplink:hover { text-decoration: underline; }
ul.samples { list-style: none; padding: 0; margin: 0; } ul.samples li { padding: 4px 0; }
.ver { color: var(--muted); font-size: 11px; margin-left: 4px; }

/* Bố cục 3 cột */
#layout { flex: 1; display: grid; grid-template-columns: 250px minmax(0, 1fr) 340px; min-height: 0; }
#tree, #detail { background: var(--panel); overflow: auto; }
#tree { border-right: 1px solid var(--line); } #detail { border-left: 1px solid var(--line); }
#center { overflow: auto; padding: 10px; min-width: 0; }

/* Cây tủ */
.tree { display: flex; flex-direction: column; height: 100%; }
.tree-head { display: flex; justify-content: space-between; align-items: center; padding: 10px; border-bottom: 1px solid var(--line); }
.tree-body { flex: 1; overflow: auto; } .tree > .hint { padding: 8px 10px; margin: 0; }
.tnode { padding: 6px 8px; border-bottom: 1px solid #f0f2f5; cursor: pointer; }
.tnode:hover { background: #f8fafc; } .tnode.active { background: var(--accent-soft); box-shadow: inset 3px 0 var(--accent); }
.tline { display: flex; gap: 6px; align-items: center; }
.tid { font-weight: 700; color: var(--accent); } .tname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tmeta { color: var(--muted); font-size: 11.5px; }
.tact { display: none; gap: 4px; margin-top: 4px; } .tnode.active .tact, .tnode:hover .tact { display: flex; }
.tlost { padding: 8px; background: var(--err-soft); }
.badge { border-radius: 9px; padding: 0 7px; font-size: 11.5px; font-weight: 600; white-space: nowrap; }
.badge.err { background: var(--err-soft); color: var(--err); } .badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.ok { background: #e7f6ec; color: var(--ok); } .badge.info { background: #eaf0fb; color: var(--info); }

/* Thẻ */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin-bottom: 10px; }
.sum-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.chip { background: var(--bg); border-radius: 10px; padding: 0 8px; color: var(--muted); font-size: 12px; }
.sum-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 6px 14px; }
.kv { display: flex; flex-direction: column; } .kv .k { color: var(--muted); font-size: 11.5px; } .kv .v { font-weight: 600; } .kv .v.big { color: var(--accent); font-size: 15px; }
.phasebars { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; margin: 10px 0 6px; }
.pb { display: flex; align-items: center; gap: 5px; } .pl { font-weight: 700; width: 12px; }
.bar { width: 120px; height: 8px; background: var(--bg); border-radius: 4px; overflow: hidden; } .fill { height: 100%; background: var(--accent); }
.pv { width: 62px; text-align: right; font-variant-numeric: tabular-nums; }
.imb { color: var(--muted); } .imb.hi { color: var(--warn); font-weight: 600; }

/* Bảng lộ */
.table-head { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; } .table-head .hint { flex: 1; }
.tablewrap { overflow: auto; max-height: calc(100vh - 330px); min-height: 120px; }
table.grid { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
.grid th { position: sticky; top: 0; background: #eef1f5; z-index: 1; font-weight: 600; font-size: 12px; text-align: left; padding: 5px 4px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.grid th.res { background: #e6edf3; }
.grid td { padding: 2px 4px; border-bottom: 1px solid #eef0f3; white-space: nowrap; }
.grid td.res { background: var(--res); } .grid td.num { text-align: right; }
.grid td.ovr { color: var(--ovr); font-weight: 700; } .grid td.bad { color: var(--err); font-weight: 700; }
.grid td.c { text-align: center; }
.grid td.w-name input { width: 100%; min-width: 220px; } .grid .w-name { min-width: 230px; }
.grid tr.active td { background: var(--accent-soft); } .grid tr.spare td { color: var(--muted); }
.grid tr.feeder td { color: var(--accent); font-style: italic; cursor: pointer; }
.grid td.ord { color: var(--muted); } .grid td.ord button { padding: 0 3px; font-size: 9px; }
.grid select { padding: 2px; }
table.small td, table.small th { padding: 3px 6px; }
.wi { cursor: help; } .wi.err { color: var(--err); } .wi.warn { color: var(--warn); } .wi.info { color: var(--info); }

/* Khung phải */
.detail { padding: 10px 12px; }
.dhead { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; gap: 8px; }
.form { display: flex; flex-direction: column; gap: 5px; }
.frow { display: grid; grid-template-columns: 140px minmax(0, 1fr); align-items: center; gap: 6px; }
.frow .flabel { color: var(--muted); font-size: 12px; } .frow input[type=text], .frow select { width: 100%; }
.frow input.num { width: 100%; text-align: left; } .frow .hint { grid-column: 2; }
label.inl { margin-left: 8px; color: var(--muted); }
.wlist { padding-left: 18px; margin: 10px 0; } .wlist li.error { color: var(--err); } .wlist li.warning { color: var(--warn); } .wlist li.info { color: var(--info); }
details.steps { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 6px; }
details.steps summary { font-weight: 600; cursor: pointer; color: var(--accent); }
details.steps table { width: 100%; border-collapse: collapse; margin-top: 4px; }
details.steps td { border-bottom: 1px solid #f0f2f5; padding: 4px 2px; vertical-align: top; }
.sl { font-size: 12px; } .sf { color: var(--muted); font-size: 11px; font-family: Consolas, monospace; word-break: break-word; }
.sv { text-align: right; font-weight: 600; white-space: nowrap; font-variant-numeric: tabular-nums; }

/* Cảnh báo */
#warnbar { background: var(--panel); border-top: 1px solid var(--line); }
.wsum { border: 0; width: 100%; text-align: left; padding: 6px 12px; border-radius: 0; }
.wall { margin: 0; padding: 4px 12px 8px 28px; max-height: 26vh; overflow: auto; }
.wall li { cursor: pointer; padding: 1px 0; } .wall li:hover { text-decoration: underline; }
.wall li.error { color: var(--err); } .wall li.warning { color: var(--warn); } .wall li.info { color: var(--info); }
code { font-size: 11px; background: var(--bg); padding: 0 4px; border-radius: 3px; color: var(--muted); }

/* Hộp thoại & thông báo */
.modal-back { position: fixed; inset: 0; background: rgba(15, 23, 42, .35); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal { background: #fff; border-radius: 10px; width: min(760px, 94vw); max-height: 88vh; display: flex; flex-direction: column; box-shadow: 0 10px 40px rgba(0, 0, 0, .25); }
.modal-head, .modal-foot { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; }
.modal-foot { justify-content: flex-end; gap: 8px; border-top: 1px solid var(--line); }
.modal-head { border-bottom: 1px solid var(--line); }
.modal-body { padding: 12px 14px; overflow: auto; }
#toast { position: fixed; bottom: 46px; left: 50%; transform: translateX(-50%); z-index: 60; }
.toast { background: #1f2937; color: #fff; padding: 8px 14px; border-radius: 6px; box-shadow: 0 4px 14px rgba(0, 0, 0, .2); cursor: pointer; max-width: 80vw; }
.toast.error { background: var(--err); }
.empty { padding: 40px; text-align: center; color: var(--muted); }
pre.fatal { color: var(--err); padding: 20px; white-space: pre-wrap; }

@media (max-width: 1100px) {
  #layout { grid-template-columns: 210px minmax(0, 1fr); }
  #detail { grid-column: 1 / -1; border-left: 0; border-top: 1px solid var(--line); max-height: 40vh; }
  body { overflow: auto; } #layout { min-height: auto; }
}
@media (max-width: 700px) {
  #layout { display: block; } #tree { max-height: 30vh; }
  .prjname input { width: 100%; }
}
