:root {
  --brand: #f57c1f;
  --brand-dark: #d9650c;
  --brand-soft: #fff1e6;
  --ink: #1d2433;
  --muted: #6b7385;
  --line: #e4e7ee;
  --bg: #f4f6fa;
  --card: #ffffff;
  --side: #1b2130;
  --side-ink: #c9cfdc;
  --green: #1f9d55;
  --green-soft: #e3f6eb;
  --red: #d64545;
  --red-soft: #fde8e8;
  --blue: #2f6fdb;
  --blue-soft: #e6eefc;
  --orange-soft: #fff1e0;
  --gray-soft: #eef0f4;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  font-family: 'Inter', 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 14px;
  color: var(--ink);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }
.boot { padding: 40px; color: var(--muted); }
.mono { font-family: ui-monospace, 'Cascadia Mono', Consolas, monospace; font-size: 12.5px; }
.muted { color: var(--muted); }
.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.neg { color: var(--red); }
.pos { color: var(--green); }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 248px 1fr; min-height: 100vh; }
.side { background: var(--side); color: var(--side-ink); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 10px; padding: 18px 18px 14px; color: #fff; font-weight: 700; letter-spacing: 0.3px; }
.brand .logo { width: 34px; height: 34px; border-radius: 9px; background: var(--brand); display: grid; place-items: center; font-size: 12px; }
.brand small { display: block; font-weight: 400; color: var(--side-ink); font-size: 11.5px; }
.nav { padding: 6px 10px 20px; display: flex; flex-direction: column; gap: 2px; }
.nav .group { font-size: 11px; text-transform: uppercase; letter-spacing: 0.8px; color: #7d869a; padding: 14px 10px 6px; }
.nav a { color: var(--side-ink); padding: 8px 10px; border-radius: 8px; display: flex; align-items: center; gap: 10px; }
.nav a:hover { background: rgba(255, 255, 255, 0.06); text-decoration: none; color: #fff; }
.nav a.active { background: var(--brand); color: #fff; }
.nav .ico { width: 18px; text-align: center; opacity: 0.9; }
.main { min-width: 0; display: flex; flex-direction: column; }
.top { height: 56px; background: var(--card); border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; padding: 0 22px; position: sticky; top: 0; z-index: 20; }
.top .sync { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #b5bccb; display: inline-block; }
.dot.on { background: var(--green); }
.dot.flash { box-shadow: 0 0 0 4px rgba(31, 157, 85, 0.25); }
.top .right { display: flex; align-items: center; gap: 14px; }
.who { text-align: right; line-height: 1.2; }
.who b { display: block; }
.who small { color: var(--muted); }
.content { padding: 22px; }
.page { max-width: 1500px; }

/* ---------- headings, cards ---------- */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 21px; }
.page-head .sub { color: var(--muted); margin-top: 4px; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 16px; }
.card > h3 { margin: 0; padding: 14px 16px; font-size: 15px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.card .body { padding: 16px; }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat .label { color: var(--muted); font-size: 12px; }
.stat .value { font-size: 20px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kv { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px 20px; }
.kv div span { display: block; color: var(--muted); font-size: 12px; margin-bottom: 2px; }
.kv div b { font-weight: 500; word-break: break-word; }

/* ---------- buttons, badges ---------- */
.btn { border: 1px solid var(--line); background: #fff; color: var(--ink); border-radius: 8px; padding: 7px 13px; cursor: pointer; font: inherit; white-space: nowrap; }
.btn:hover { border-color: #c8cdd8; background: #fafbfc; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { color: var(--red); border-color: #f1c2c2; }
.btn.green { background: var(--green); border-color: var(--green); color: #fff; }
.btn.sm { padding: 4px 9px; font-size: 12.5px; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.icon-btn { border: none; background: transparent; cursor: pointer; font-size: 18px; color: var(--muted); padding: 2px 6px; border-radius: 6px; }
.icon-btn:hover { background: var(--gray-soft); }
.icon-btn.danger:hover { color: var(--red); }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 500; white-space: nowrap; background: var(--gray-soft); color: #4a5164; }
.badge.green { background: var(--green-soft); color: var(--green); }
.badge.red { background: var(--red-soft); color: var(--red); }
.badge.blue { background: var(--blue-soft); color: var(--blue); }
.badge.orange { background: var(--orange-soft); color: var(--brand-dark); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.chip { border: 1px solid var(--line); border-radius: 999px; padding: 5px 12px; background: #fff; cursor: pointer; font: inherit; font-size: 13px; }
.chip.on { background: var(--ink); color: #fff; border-color: var(--ink); }

/* ---------- tables ---------- */
.table-tools { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.table-tools .left, .table-tools .right { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.search { border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; min-width: 240px; font: inherit; }
.table-wrap { overflow: auto; max-height: calc(100vh - 230px); }
table.data { width: 100%; border-collapse: separate; border-spacing: 0; }
table.data th { position: sticky; top: 0; background: #f8f9fb; z-index: 1; text-align: left; font-weight: 600; font-size: 12.5px; color: #4a5164; padding: 9px 10px; border-bottom: 1px solid var(--line); cursor: pointer; user-select: none; white-space: nowrap; }
table.data th.num { text-align: right; }
table.data td { padding: 8px 10px; border-bottom: 1px solid #eef0f4; vertical-align: top; }
table.data tr.clickable { cursor: pointer; }
table.data tbody tr:hover td { background: #fbfaf7; }
table.data tfoot td { font-weight: 700; background: #f8f9fb; position: sticky; bottom: 0; border-top: 1px solid var(--line); }
table.data td.empty { text-align: center; color: var(--muted); padding: 28px; }
table.data .chk { width: 34px; text-align: center; }
table.grid { width: 100%; border-collapse: collapse; }
table.grid th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 6px 4px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.grid td { padding: 4px; border-bottom: 1px solid #f0f1f5; }
table.grid input, table.grid select { width: 100%; min-width: 70px; border: 1px solid var(--line); border-radius: 6px; padding: 6px 7px; font: inherit; background: #fffdf9; }
table.grid input:focus, table.grid select:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }
table.grid td.num { padding-right: 8px; }

/* ---------- forms, modal ---------- */
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 16px; }
.form-grid .full { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; }
.field > span { color: #4a5164; font-weight: 500; }
.field .req { color: var(--red); }
.field input, .field select, .field textarea { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font: inherit; background: #fffdf9; }
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }
.field input:disabled, .field select:disabled, .field textarea:disabled { background: var(--gray-soft); color: var(--muted); }
.field small { color: var(--muted); }
.field.check { flex-direction: row; align-items: center; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.modal-back { position: fixed; inset: 0; background: rgba(17, 22, 34, 0.45); display: grid; place-items: start center; padding: 6vh 16px; z-index: 100; overflow-y: auto; }
.modal { background: #fff; border-radius: 12px; width: min(720px, 100%); box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25); }
.modal.wide { width: min(1200px, 100%); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 18px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--line); }
#toasts { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast { background: var(--ink); color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 420px; }
.toast.error { background: var(--red); }
.notice { padding: 10px 12px; border-radius: 8px; background: var(--brand-soft); color: #7a3e09; margin-bottom: 12px; }
.notice.red { background: var(--red-soft); color: #8b1f1f; }
.notice.green { background: var(--green-soft); color: #11603a; }

/* ---------- tabs ---------- */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 14px; flex-wrap: wrap; }
.tabs button { border: none; background: none; padding: 10px 14px; cursor: pointer; font: inherit; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs button.on { color: var(--ink); border-bottom-color: var(--brand); font-weight: 600; }

/* ---------- dashboard ---------- */
.tasks { list-style: none; margin: 0; padding: 0; }
.tasks li { display: flex; justify-content: space-between; align-items: center; padding: 10px 2px; border-bottom: 1px solid #f0f1f5; }
.tasks .count { background: var(--brand); color: #fff; border-radius: 999px; padding: 1px 10px; font-weight: 600; font-size: 12.5px; }
.flow { display: flex; flex-wrap: wrap; gap: 8px; align-items: stretch; }
.flow .step { flex: 1 1 150px; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; background: #fcfcfd; position: relative; }
.flow .step b { display: block; font-size: 13px; }
.flow .step small { color: var(--muted); }
.flow .step .who-tag { margin-top: 6px; display: inline-block; font-size: 11.5px; background: var(--brand-soft); color: var(--brand-dark); border-radius: 999px; padding: 1px 8px; }
.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { padding: 8px 0 8px 18px; border-left: 2px solid var(--line); position: relative; }
.timeline li::before { content: ''; position: absolute; left: -6px; top: 13px; width: 10px; height: 10px; border-radius: 50%; background: var(--brand); }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; background: linear-gradient(135deg, #1b2130 0%, #2a3247 60%, #f57c1f 160%); padding: 20px; }
.login .box { background: #fff; border-radius: 14px; padding: 30px; width: min(380px, 100%); box-shadow: 0 30px 60px rgba(0, 0, 0, 0.3); }
.login h1 { margin: 0 0 4px; font-size: 22px; }
.login p { margin: 0 0 20px; color: var(--muted); }
.login form { display: flex; flex-direction: column; gap: 14px; }

/* ---------- notifications ---------- */
.bell { position: relative; }
.bell .n { position: absolute; top: -4px; right: -4px; background: var(--red); color: #fff; border-radius: 999px; font-size: 10.5px; padding: 0 5px; min-width: 16px; text-align: center; }
.dropdown { position: absolute; right: 22px; top: 52px; width: 380px; max-height: 70vh; overflow-y: auto; background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 12px 30px rgba(0, 0, 0, 0.15); z-index: 50; }
.dropdown .item { padding: 10px 14px; border-bottom: 1px solid #f0f1f5; cursor: pointer; }
.dropdown .item.unread { background: var(--brand-soft); }
.dropdown .item small { color: var(--muted); display: block; margin-top: 2px; }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .side { position: static; height: auto; }
  .form-grid { grid-template-columns: 1fr; }
}

/* ---------- tab Công thức ---------- */
.fx-group { font-size: 16px; margin: 22px 0 10px; color: #4a5164; }
.fx-card .body { display: flex; flex-direction: column; gap: 10px; }
.fx-key { font-size: 11.5px; color: var(--muted); font-weight: 400; }
.fx-pretty { font-size: 16px; line-height: 2.1; padding: 10px 14px; background: #fbfaf7; border: 1px dashed #ecd9c6; border-radius: 10px; }
.fx-result { font-weight: 700; color: var(--brand-dark); }
.fx-var { background: var(--blue-soft); color: var(--blue); border-radius: 6px; padding: 2px 8px; margin: 0 1px; font-weight: 600; white-space: nowrap; }
.fx-op { color: #4a5164; font-weight: 600; white-space: pre; }
.fx-num { background: var(--green-soft); color: var(--green); border-radius: 6px; padding: 2px 6px; font-weight: 600; }
.fx-fn { color: #8a3ffc; font-weight: 700; }
.fx-err { color: var(--red); }
.fx-desc { margin: 0; }
.fx-chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 12.5px; }
.fx-chip { border: 1px solid #cfdcf7; background: var(--blue-soft); color: var(--blue); border-radius: 999px; padding: 3px 10px; cursor: pointer; font: inherit; font-size: 12.5px; }
.fx-chip.fn { border-color: #dccbfb; background: #f3edff; color: #6d28d9; }
.fx-chip:hover { filter: brightness(0.96); }
.fx-input { width: 100%; font-family: ui-monospace, 'Cascadia Mono', Consolas, monospace; font-size: 14px; border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; background: #fffdf9; resize: vertical; }
.fx-input:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }
.fx-msg { font-size: 13px; min-height: 16px; }
.fx-msg.err { color: var(--red); }
.fx-msg.warn { color: var(--brand-dark); }
.fx-test { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; background: #f8f9fb; border-radius: 10px; padding: 10px 12px; }
.fx-test-title { width: 100%; font-size: 12.5px; color: var(--muted); }
.fx-var-input { display: flex; flex-direction: column; font-size: 12px; color: #4a5164; gap: 3px; }
.fx-var-input input { width: 150px; border: 1px solid var(--line); border-radius: 6px; padding: 5px 7px; font: inherit; }
.fx-test-result { margin-left: auto; font-size: 15px; padding: 6px 10px; background: #fff; border-radius: 8px; border: 1px solid var(--line); }
.fx-foot { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; font-size: 12.5px; }
.fx-flow { display: flex; flex-direction: column; gap: 8px; }
.fx-flow-row { display: grid; grid-template-columns: 1fr 40px 260px; align-items: center; gap: 6px; }
.fx-flow-in { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.fx-flow-arrow { text-align: center; color: var(--brand); font-size: 20px; font-weight: 700; }
.fx-node { border-radius: 8px; padding: 4px 10px; font-size: 12.5px; font-weight: 600; white-space: nowrap; }
.fx-node.in { background: var(--blue-soft); color: var(--blue); }
.fx-node.calc { background: #f3edff; color: #6d28d9; }
.fx-node.out { background: var(--brand); color: #fff; }
.fx-legend { display: flex; gap: 8px; margin-bottom: 12px; }
.fx-codes { display: grid; grid-template-columns: repeat(auto-fill, minmax(420px, 1fr)); gap: 14px; }
.fx-code { border: 1px solid var(--line); border-radius: 10px; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.fx-help { margin: 0; padding-left: 18px; line-height: 1.9; }

/* ---------- căn chỉnh (rà UI 04/12) ---------- */
.page-head .actions { align-items: center; }
table.data td { white-space: nowrap; }
table.data td.wrap { white-space: normal; min-width: 160px; max-width: 360px; }
table.data td.mono { white-space: nowrap; }
.dropdown .item { line-height: 1.4; }
.stat .value { overflow-wrap: anywhere; }
.dropdown { position: fixed; right: 16px; top: 52px; width: min(380px, calc(100vw - 16px)); }

/* ---------- màn hình nhỏ (điện thoại / máy tính bảng dọc) ---------- */
@media (max-width: 900px) {
  .side { position: sticky; top: 0; z-index: 30; height: auto; overflow: visible; }
  .brand { padding: 10px 14px 6px; }
  .nav { flex-direction: row; overflow-x: auto; padding: 0 8px 8px; gap: 4px; scrollbar-width: thin; }
  .nav .group { display: none; }
  .nav a { white-space: nowrap; padding: 6px 10px; flex: 0 0 auto; }
  .top { height: auto; padding: 8px 12px; position: static; }
  .top > .muted { display: none; }
  .top .right { flex-wrap: wrap; gap: 8px; justify-content: flex-end; width: 100%; }
  .content { padding: 12px; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .stat .value { font-size: 17px; }
  .page-head h1 { font-size: 18px; }
  .search { min-width: 0; width: 100%; }
  .dropdown { top: 8px; right: 8px; }
  .modal-back { padding: 8px; }
  .modal-body { padding: 12px; }
  .fx-flow-row { grid-template-columns: 1fr; }
  .fx-flow-in { justify-content: flex-start; }
  .fx-flow-arrow { text-align: left; }
  .fx-codes { grid-template-columns: 1fr; }
}
@media (max-width: 900px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .side, .main { min-width: 0; }
}
@media (max-width: 900px) {
  .page { overflow-wrap: anywhere; }
  .fx-var, .fx-node { white-space: normal; }
  .fx-op { white-space: normal; }
  .fx-var-input { flex: 1 1 140px; }
  .fx-var-input input { width: 100%; }
  .fx-test-result { margin-left: 0; width: 100%; }
  .fx-foot > span { flex: 1 1 100%; }
  .kv { grid-template-columns: 1fr 1fr; }
  .form-actions { flex-wrap: wrap; }
}
