:root {
  --bg: #f6f3ee;
  --surface: #ffffff;
  --ink: #1f1a14;
  --muted: #6f665b;
  --line: #e4ddd2;
  --accent: #c8161d;   /* đỏ thương hiệu IN 24H */
  --gold: #e8b923;
  --accent-ink: #ffffff;
  --accent-soft: #fdeceb;
  --danger: #b91c1c;
  --profit: #15803d;
  --series-1: #257de0;  /* Doanh thu */
  --series-2: #f47230;  /* Tiền lời */
  --radius: 10px;
  --s-moi: #1d4ed8;          --s-moi-bg: #e8eefc;
  --s-dang_lam: #a16207;     --s-dang_lam-bg: #fdf3d8;
  --s-da_giao: #6d28d9;      --s-da_giao-bg: #efe8fc;
  --s-da_thanh_toan: #15803d; --s-da_thanh_toan-bg: #e3f4e8;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  color: var(--ink);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
.hidden { display: none !important; }
h2 { font-size: 1.15rem; margin: 0; }
input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
label { display: block; font-size: .85rem; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: 4px; color: var(--ink); font-size: .95rem; }
label.inline { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
label.inline input { margin: 0; width: auto; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line); background: #fff;
  cursor: pointer; white-space: nowrap; font-weight: 500;
}
.btn:hover { border-color: #cfc5b6; background: #fbf9f6; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: #a51218; border-color: #a51218; }
.btn-danger { color: var(--danger); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-sm { padding: 5px 10px; font-size: .85rem; }
.btn-block { width: 100%; }
.btn:disabled { opacity: .6; cursor: default; }

/* Brand */
.brand { display: flex; align-items: center; gap: 12px; }
.brand b { display: block; font-size: 1rem; }
.brand small { color: var(--muted); font-size: .78rem; }
.brand-logo { height: 44px; width: auto; border-radius: 8px; display: block; }
.login-logo { width: 100%; border-radius: 10px; display: block; }
.login-title { text-align: center; }
.login-title b { display: block; font-size: 1.2rem; }
.login-title small { color: var(--muted); }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: radial-gradient(ellipse at top, #e3262c 0%, #9e1015 70%); }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; padding: 24px; display: grid; gap: 16px; box-shadow: 0 20px 60px rgba(60, 0, 0, .35);
}

/* Topbar */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 24px;
  padding: 8px 20px; background: var(--surface); border-bottom: 3px solid var(--accent);
}
.tabs { display: flex; gap: 4px; }
.tabs button {
  border: 0; background: none; padding: 8px 12px; border-radius: 8px; cursor: pointer; color: var(--muted); font-weight: 500;
}
.tabs button.active { background: var(--accent-soft); color: var(--accent); }
.user-menu { margin-left: auto; display: flex; align-items: center; gap: 4px; color: var(--muted); font-size: .9rem; }
#me-name { margin-right: 6px; color: var(--ink); font-weight: 500; }

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

/* Stats */
.stats { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; text-align: left; cursor: pointer; border-left: 4px solid var(--c, var(--line));
}
.stat:hover { border-color: #cfc5b6; border-left-color: var(--c, #cfc5b6); }
.stat.active { outline: 2px solid var(--c, var(--accent)); outline-offset: -1px; }
.stat .label { font-size: .8rem; color: var(--muted); }
.stat .value { font-size: 1.5rem; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat.warn .value { color: var(--danger); }
.stat.money .value { font-size: 1.2rem; padding-top: 5px; }

/* Toolbar */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 12px; }
.toolbar input[type=search] { max-width: 340px; }
.toolbar select { width: auto; }
.spacer { flex: 1; }

/* Tables */
.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: .78rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; background: #fbf9f6; position: sticky; top: 0; }
tbody tr:last-child td { border-bottom: 0; }
.orders tbody tr { cursor: pointer; }
.orders tbody tr:hover { background: #fbf8f3; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.code { font-weight: 600; white-space: nowrap; }
.sub { display: block; color: var(--muted); font-size: .82rem; margin-top: 2px; }
.debt { color: var(--danger); font-weight: 600; }
.overdue { color: var(--danger); font-weight: 600; }
.soon { color: #a16207; font-weight: 600; }
.empty { padding: 40px; text-align: center; color: var(--muted); margin: 0; }
.clip { margin-left: 4px; color: var(--muted); font-size: .8rem; }

/* Status badge / select */
.badge, .status-select {
  display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: .8rem; font-weight: 600;
  color: var(--sc); background: var(--sbg); border: 0; white-space: nowrap;
}
.status-select { width: auto; cursor: pointer; padding-right: 6px; }
[data-s=moi] { --sc: var(--s-moi); --sbg: var(--s-moi-bg); }
[data-s=dang_lam] { --sc: var(--s-dang_lam); --sbg: var(--s-dang_lam-bg); }
[data-s=da_giao] { --sc: var(--s-da_giao); --sbg: var(--s-da_giao-bg); }
[data-s=da_thanh_toan] { --sc: var(--s-da_thanh_toan); --sbg: var(--s-da_thanh_toan-bg); }

/* Dialogs */
.dialog {
  border: 0; border-radius: 14px; padding: 0; width: min(480px, calc(100vw - 24px));
  max-height: calc(100vh - 40px); box-shadow: 0 20px 60px rgba(31, 26, 20, .25);
}
.dialog-lg { width: min(900px, calc(100vw - 24px)); }
.dialog::backdrop { background: rgba(31, 26, 20, .45); }
.dialog form, #detail { display: flex; flex-direction: column; max-height: calc(100vh - 40px); }
.dialog-head { display: flex; align-items: center; gap: 12px; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.dialog-head .x { margin-left: auto; border: 0; background: none; font-size: 1.6rem; line-height: 1; cursor: pointer; color: var(--muted); }
.dialog-body { padding: 16px 20px; overflow: auto; display: grid; gap: 14px; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 20px; border-top: 1px solid var(--line); background: #fbf9f6; flex-wrap: wrap; }
.dialog-foot .left { margin-right: auto; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px 14px; margin: 0; display: grid; gap: 12px; }
legend { font-weight: 600; padding: 0 6px; font-size: .9rem; }
.grid { display: grid; gap: 12px; }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.span2 { grid-column: span 2; }
.span3 { grid-column: 1 / -1; }
.totals { display: flex; flex-wrap: wrap; gap: 8px 24px; padding: 10px 12px; background: var(--accent-soft); border-radius: 8px; font-size: .92rem; }
.totals b { font-variant-numeric: tabular-nums; }
.form-error { color: var(--danger); margin: 0; min-height: 1em; font-size: .9rem; }
.hint { color: var(--muted); font-size: .85rem; margin: 0; }

/* Detail */
.detail-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.box { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.box h3 { margin: 0 0 8px; font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
dl { display: grid; grid-template-columns: 120px 1fr; gap: 6px 12px; margin: 0; font-size: .92rem; }
dt { color: var(--muted); }
dd { margin: 0; word-break: break-word; }
.status-steps { display: flex; gap: 6px; flex-wrap: wrap; }
.status-steps button { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 6px 12px; cursor: pointer; font-size: .85rem; }
.status-steps button.current { background: var(--sbg); color: var(--sc); border-color: transparent; font-weight: 600; }
.files { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.files li { display: flex; align-items: center; gap: 10px; font-size: .9rem; }
.files img { width: 44px; height: 44px; object-fit: cover; border-radius: 6px; border: 1px solid var(--line); }
.files .fi { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 6px; background: #f1ece4; font-size: .7rem; font-weight: 700; color: var(--muted); }
.files a { color: var(--ink); }
.files .meta { color: var(--muted); font-size: .8rem; }
.files .del { margin-left: auto; }
.history { list-style: none; padding: 0; margin: 0; font-size: .85rem; display: grid; gap: 6px; max-height: 180px; overflow: auto; }
.history time { color: var(--muted); margin-right: 6px; }
.note { white-space: pre-wrap; }
.order-name { font-weight: 500; color: var(--muted); font-size: .95rem; margin-left: 6px; }
a.btn { text-decoration: none; color: inherit; }

/* Tiền lời */
.profit { color: var(--profit); font-weight: 600; }
.loss { color: var(--danger); font-weight: 600; }
.muted { color: var(--muted); }
.cost-box { background: #fcfaf7; }

/* Thống kê */
.report-basis { margin: -4px 0 12px; }
.kpis { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 12px; margin-bottom: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.kpi .label { font-size: .8rem; color: var(--muted); }
.kpi .value { font-size: 1.3rem; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .delta { font-size: .8rem; margin-top: 2px; color: var(--muted); }
.kpi.hero { border-top: 3px solid var(--series-1); }
.kpi.hero-2 { border-top: 3px solid var(--series-2); }
.up { color: var(--profit); } .down { color: var(--danger); }
.warn-line { background: #fdf3d8; color: #7c4a03; border-radius: 8px; padding: 8px 12px; margin: 0 0 12px; font-size: .9rem; }
.panel { position: relative; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px 8px; margin-bottom: 16px; }
.panel-head { display: flex; align-items: baseline; gap: 16px; flex-wrap: wrap; margin-bottom: 8px; }
.panel-head h3 { margin: 0; font-size: 1rem; }
.legend { display: flex; gap: 16px; font-size: .85rem; color: var(--ink); margin-left: auto; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid line { stroke: #eee9e1; }
.chart .axis text { fill: var(--muted); font-size: 11px; }
.chart .zero { stroke: #cfc5b6; }
.chart .col { cursor: pointer; }
.chart .col .hit { fill: transparent; }
.chart .col:hover .hit, .chart .col.sel .hit { fill: #f6f1e9; }
.chart .col.sel .mlabel { fill: var(--ink); font-weight: 700; }
.chart-tip {
  position: absolute; pointer-events: none; opacity: 0; background: var(--surface); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px; font-size: .82rem; box-shadow: 0 6px 20px rgba(31,26,20,.12); transition: opacity .1s; white-space: nowrap; z-index: 2;
}
.chart-tip.show { opacity: 1; }
.chart-tip b { display: block; margin-bottom: 4px; }
.chart-tip .row { display: flex; gap: 14px; justify-content: space-between; font-variant-numeric: tabular-nums; }
.chart-tip i { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 6px; }
.report-table th, .report-table td { white-space: nowrap; }
.report-table tbody tr { cursor: pointer; }
.report-table tbody tr:hover { background: #fbf8f3; }
.report-table tr.sel td { background: var(--accent-soft); font-weight: 600; }
.report-table tfoot td { font-weight: 700; background: #fbf9f6; border-top: 2px solid var(--line); }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); opacity: 0;
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 8px; transition: .2s; pointer-events: none; z-index: 100;
}
.toast.show { opacity: 1; transform: translateX(-50%); }
.toast.err { background: var(--danger); }

/* Print */
#print-area { display: none; }
@media print {
  body > *:not(#print-area) { display: none !important; }
  #print-area { display: block; font-size: 13px; color: #000; }
  #print-area h1 { font-size: 20px; margin: 0 0 4px; }
  #print-area table { border: 1px solid #000; }
  #print-area th, #print-area td { border: 1px solid #000; padding: 6px 8px; position: static; background: none; color: #000; text-transform: none; letter-spacing: 0; font-size: 13px; }
  #print-area .ph { display: flex; justify-content: space-between; border-bottom: 2px solid #000; padding-bottom: 8px; margin-bottom: 12px; }
  #print-area .sign { display: flex; justify-content: space-around; margin-top: 40px; text-align: center; }
  #print-area .sign div { min-height: 90px; }
  @page { margin: 14mm; }
}

/* Mobile */
@media (max-width: 900px) {
  .stats, .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .topbar { flex-wrap: wrap; gap: 8px 16px; padding: 10px 12px; }
  .user-menu { width: 100%; margin: 0; justify-content: flex-end; }
  main { padding: 12px; }
  .g3, .g4, .detail-grid { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 640px) {
  .stats, .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .toolbar input[type=search] { max-width: none; }
  .g3, .g4, .detail-grid { grid-template-columns: 1fr; }
  .span2, .span3 { grid-column: auto; }
  .brand small, .brand b { display: none; }
  .brand-logo { height: 38px; }
  .chart { overflow-x: auto; }
  .chart svg { min-width: 700px; }
  .orders thead { display: none; }
  .orders, .orders tbody, .orders tr, .orders td { display: block; width: 100%; }
  .orders tr { padding: 10px 12px; border-bottom: 1px solid var(--line); display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; }
  .orders td { border: 0; padding: 0; text-align: left; }
  .orders td[data-k=product], .orders td[data-k=qty], .orders td[data-k=total], .orders td[data-k=profit], .orders td[data-k=cost], .orders td[data-k=margin] { display: none; }
  .orders td[data-k=code] { grid-column: 1; grid-row: 1; }
  .orders td[data-k=status] { grid-column: 2; grid-row: 1; text-align: right; }
  .orders td[data-k=customer] { grid-column: 1; grid-row: 2; }
  .orders td[data-k=debt], .orders td[data-k=rprofit] { grid-column: 2; grid-row: 2; text-align: right; }
  .orders td[data-k=due] { grid-column: 1 / -1; grid-row: 3; font-size: .85rem; }
  dl { grid-template-columns: 100px 1fr; }
}
