/* ============================================================================
   Color system — semantic tokens (THEME-01). Light là default trên :root;
   [data-theme="dark"] override. KHÔNG vá màu rời rạc ở component.
   ============================================================================ */
:root {
  --bg: #f3f5f7;
  --card: #ffffff;
  --ink: #182230;
  --muted: #687386;
  --pri: #1565c0;
  --pri-ink: #ffffff;
  --ok: #1b7f3b;
  --warn: #a66500;
  --bad: #b3261e;
  --line: #dde3ea;
  --radius: 14px;

  /* component tokens */
  --topbar-bg: var(--pri);
  --topbar-ink: #ffffff;
  --topbar-muted: #cfe0f5;
  --nav-bg: #ffffff;
  --nav-ink: var(--muted);
  --nav-on: var(--pri);
  --input-bg: #ffffff;
  --btn-bg: #e9edf2;
  --chip-bg: #ffffff;
  --stepper-bg: #eef2f6;
  --suggest-bg: #ffffff;
  --suggest-active: #eef2f6;
  --tag-bg: #e8eef5;
  --pill-bg: #e8eef5;
  --pill-ok-bg: #cdeed6; --pill-ok-ink: #0d5c26;
  --pill-warn-bg: #ffe6b8; --pill-warn-ink: #7a4d00;
  --pill-bad-bg: #ffd7d3; --pill-bad-ink: #8f1d17;
  --tag-need-bg: #ffe6b8; --tag-need-ink: #7a4d00;
  --tag-ordered-bg: #d3e4ff; --tag-ordered-ink: #174ea6;
  --tag-received-bg: #cdeed6; --tag-received-ink: #0d5c26;
  --tag-dead-bg: #eeeeee; --tag-dead-ink: #888888;
  --fab-bg: #ff8f00; --fab-ink: #3a2500;
  --modal-mask: rgba(10, 20, 35, .45);
  --modal-head-bg: var(--card);
  --icon-btn-bg: rgba(255, 255, 255, .16);
  --modal-close-bg: #eef2f6;
  --focus-ring: #9cc3ea;
  --card-shadow: 0 1px 2px rgba(16, 30, 54, .06);
  --toast-shadow: 0 4px 14px rgba(0, 0, 0, .2);
  --fab-shadow: 0 4px 14px rgba(255, 143, 0, .45);
  --dot-high: #e07b00;
  --dot-low: #8a97a8;
  --chart-gap: #c8c4bd;
  --chart-closed: #a8a29a;
  --chart-neg: #c25b55;
  --chart-zero: #999999;

  /* layout tokens — mobile-first */
  --nav-h: 66px;            /* bottom nav usable height (64–72px target) */
  --fab-h: 44px;
}

[data-theme="dark"] {
  --bg: #0d1420;
  --card: #16202f;
  --ink: #e7edf5;
  --muted: #93a1b5;
  --pri: #5b9fe8;
  --pri-ink: #081422;
  --ok: #54c377;
  --warn: #e0a03a;
  --bad: #ff7b72;
  --line: #2a3a50;

  --topbar-bg: #12233b;
  --topbar-muted: #8fb0d8;
  --nav-bg: #101a2a;
  --nav-on: #7fb2ee;
  --input-bg: #0f1926;
  --btn-bg: #23344c;
  --chip-bg: #16202f;
  --stepper-bg: #23344c;
  --suggest-bg: #16202f;
  --suggest-active: #23344c;
  --tag-bg: #23344c;
  --pill-bg: #23344c;
  --pill-ok-bg: #123a22; --pill-ok-ink: #7cd99b;
  --pill-warn-bg: #45300a; --pill-warn-ink: #ffca7a;
  --pill-bad-bg: #4a1713; --pill-bad-ink: #ff9d95;
  --tag-need-bg: #45300a; --tag-need-ink: #ffca7a;
  --tag-ordered-bg: #16345e; --tag-ordered-ink: #9cc3f5;
  --tag-received-bg: #123a22; --tag-received-ink: #7cd99b;
  --tag-dead-bg: #23344c; --tag-dead-ink: #7d8ba0;
  --fab-bg: #ffb74d; --fab-ink: #2d1c00;
  --modal-mask: rgba(0, 0, 0, .62);
  --icon-btn-bg: rgba(255, 255, 255, .10);
  --modal-close-bg: #23344c;
  --focus-ring: #2e5f9e;
  --card-shadow: 0 1px 2px rgba(0, 0, 0, .35);
  --toast-shadow: 0 4px 14px rgba(0, 0, 0, .55);
  --fab-shadow: 0 4px 14px rgba(255, 160, 50, .3);
  --dot-high: #f0a04a;
  --dot-low: #5d6c82;
  --chart-gap: #3a4a60;
  --chart-closed: #4a5a70;
  --chart-neg: #c96a64;
  --chart-zero: #4a5a70;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.45 -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  /* nội dung cuối trang luôn scroll lên được phía trên bottom nav + FAB zone */
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 58px);
  overflow-x: hidden;                      /* chống horizontal page scroll */
}
b { font-weight: 650; }

/* ---------- top bar ---------- */
#topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 7px 12px;                       /* MOBILE: header thấp hơn ~20% */
  padding-top: calc(7px + env(safe-area-inset-top, 0px));
  padding-left: calc(12px + env(safe-area-inset-left, 0px));
  padding-right: calc(12px + env(safe-area-inset-right, 0px));
  background: var(--topbar-bg); color: var(--topbar-ink);
}
.brand { font-weight: 750; letter-spacing: .02em; white-space: nowrap; }
.brand .muted { color: var(--topbar-muted); font-weight: 500; font-size: 12px; }
.top-actions { display: flex; align-items: center; gap: 6px; }
.pill {
  border: 0; border-radius: 999px; padding: 6px 10px; font-size: 12px; font-weight: 650;
  background: var(--pill-bg); color: var(--ink); white-space: nowrap; text-decoration: none;
  min-height: 28px; display: inline-flex; align-items: center;
}
.pill.ok { background: var(--pill-ok-bg); color: var(--pill-ok-ink); }
.pill.warn { background: var(--pill-warn-bg); color: var(--pill-warn-ink); }
.pill.bad { background: var(--pill-bad-bg); color: var(--pill-bad-ink); }
.icon-btn {
  border: 0; background: var(--icon-btn-bg); color: var(--topbar-ink); border-radius: 10px;
  width: 40px; height: 40px; font-size: 17px; display: inline-flex; align-items: center;
  justify-content: center; text-decoration: none; flex: none;
}

/* ---------- layout ---------- */
#view { max-width: 720px; margin: 0 auto; padding: 12px 12px 0; outline: none;
  padding-left: calc(12px + env(safe-area-inset-left, 0px));
  padding-right: calc(12px + env(safe-area-inset-right, 0px)); }
.card { background: var(--card); border-radius: var(--radius); padding: 12px;
  box-shadow: var(--card-shadow); margin-bottom: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.row { display: flex; align-items: center; min-width: 0; }
.row.between { justify-content: space-between; }
.row.gap { gap: 8px; }
.grow { flex: 1; min-width: 0; }
.wrap { flex-wrap: wrap; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.big { font-size: 26px; font-weight: 750; }
.xbig { font-size: 34px; font-weight: 800; letter-spacing: -.01em; }
.num { font-variant-numeric: tabular-nums; }
.err { color: var(--bad); font-size: 13px; min-height: 1em; }
.oktxt { color: var(--ok); } .warntxt { color: var(--warn); } .badtxt { color: var(--bad); }
h2.sec { font-size: 13px; text-transform: uppercase; letter-spacing: .08em;
  color: var(--muted); margin: 14px 2px 7px; }
hr.soft { border: 0; border-top: 1px solid var(--line); margin: 10px 0; }

/* ---------- stats ---------- */
.stat { background: var(--card); border-radius: 12px; padding: 9px 11px;
  border: 1px solid var(--line); min-width: 0; }
.stat .lbl { font-size: 12px; color: var(--muted); }
.stat .val { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums;
  overflow-wrap: break-word; }
.stat.hero .val { font-size: 30px; font-weight: 800; color: var(--pri); }
.stat.neg .val { color: var(--bad); }

/* ---------- buttons / inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 46px; padding: 10px 16px; border-radius: 12px; border: 1px solid transparent;
  font: inherit; font-weight: 650; cursor: pointer; text-decoration: none; color: var(--ink);
  background: var(--btn-bg);
}
.btn.primary { background: var(--pri); color: var(--pri-ink); }
.btn.danger { background: var(--bad); color: #fff; }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.sm { min-height: 38px; padding: 6px 12px; font-size: 14px; border-radius: 9px; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; }
input, select, textarea {
  width: 100%; min-height: 46px; padding: 10px 12px; font: inherit; font-size: 16px;
  /* 16px tránh iOS auto-zoom khi focus */
  border: 1.5px solid var(--line); border-radius: 12px;
  background: var(--input-bg); color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--focus-ring); border-color: var(--pri); }
input:disabled, button:disabled { opacity: .55; }
label.f { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 4px; }
.stepper { display: flex; align-items: stretch; gap: 8px; }
.stepper button { width: 56px; min-height: 52px; font-size: 24px; font-weight: 700;
  border-radius: 12px; border: 1px solid var(--line); background: var(--stepper-bg);
  color: var(--ink); flex: none; }
.stepper input { text-align: center; font-size: 22px; font-weight: 700; }

/* ---------- list rows ---------- */
.lrow { display: flex; align-items: center; gap: 10px; padding: 10px 2px;
  border-top: 1px solid var(--line); }
.lrow:first-child { border-top: 0; }
.lrow .main { flex: 1; min-width: 0; }
.lrow .t { font-weight: 600; }
.lrow .s { font-size: 13px; color: var(--muted); }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.dot.urgent { background: var(--bad); } .dot.high { background: var(--dot-high); }
.dot.normal { background: var(--pri); } .dot.low { background: var(--dot-low); }
.tag { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px;
  background: var(--tag-bg); color: var(--muted); white-space: nowrap; }
.tag.need { background: var(--tag-need-bg); color: var(--tag-need-ink); }
.tag.ordered { background: var(--tag-ordered-bg); color: var(--tag-ordered-ink); }
.tag.received { background: var(--tag-received-bg); color: var(--tag-received-ink); }
.tag.cancelled, .tag.deleted { background: var(--tag-dead-bg); color: var(--tag-dead-ink); }
.tag.pending { background: var(--tag-need-bg); color: var(--tag-need-ink); }
.tag.conflict, .tag.error { background: var(--pill-bad-bg); color: var(--pill-bad-ink); }

/* ---------- chips / segmented ---------- */
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chips.wrap { flex-wrap: wrap; overflow-x: visible; }
.chip { border: 1px solid var(--line); background: var(--chip-bg); color: var(--ink);
  border-radius: 999px; padding: 8px 14px; font-size: 14px; font-weight: 600;
  white-space: nowrap; min-height: 38px; cursor: pointer; }
.chip.on { background: var(--pri); border-color: var(--pri); color: var(--pri-ink); }

/* O2 mobile: 4 trạng thái ngày luôn hiển thị đủ — grid 2×2, không hàng ngang tràn */
.seg2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.seg2 .chip, .seg3 .chip {
  width: 100%; border-radius: 12px; min-height: 46px; padding: 10px 8px;
  white-space: normal; line-height: 1.2; text-align: center;
}
.seg3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
@media (max-width: 380px) {
  .seg3 { grid-template-columns: 1fr; }
}

/* ---------- FAB & bottom nav ---------- */
.fab {
  position: fixed; right: 12px;
  right: calc(12px + env(safe-area-inset-right, 0px));
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 10px);
  z-index: 40; border: 0; border-radius: 999px;
  min-height: var(--fab-h); padding: 10px 16px;   /* compact — không che Save/toast */
  background: var(--fab-bg); color: var(--fab-ink); font-weight: 800; font-size: 14px;
  box-shadow: var(--fab-shadow);
}
#bottomnav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  display: flex; background: var(--nav-bg); border-top: 1px solid var(--line);
  min-height: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
}
#bottomnav a {
  flex: 1 1 0; min-width: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 1px;
  padding: 6px 2px; text-decoration: none; color: var(--nav-ink); font-size: 18px;
}
#bottomnav a span {
  font-size: 10.5px; line-height: 1.15; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; max-width: 100%;
}
#bottomnav a.on { color: var(--nav-on); font-weight: 700; }

/* ---------- modal / toast ---------- */
.modal-mask { position: fixed; inset: 0; background: var(--modal-mask); z-index: 60;
  display: flex; align-items: flex-end; justify-content: center;
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px); }
.modal { background: var(--card); width: 100%; max-width: 560px; max-height: 88vh;
  border-radius: 18px 18px 0 0; overflow: auto; color: var(--ink);
  padding-bottom: env(safe-area-inset-bottom, 0px); }
.modal-head { display: flex; justify-content: space-between; align-items: center;
  padding: 12px 14px; border-bottom: 1px solid var(--line); position: sticky;
  top: 0; background: var(--modal-head-bg); }
.modal-head .icon-btn { background: var(--modal-close-bg); color: var(--ink); }
.modal-body { padding: 12px 14px 18px; }
@media (min-width: 640px) {
  .modal-mask { align-items: center; }
  .modal { border-radius: 18px; }
}
.toast { margin-top: 8px; padding: 12px 14px; border-radius: 12px; color: #fff;
  font-weight: 600; box-shadow: var(--toast-shadow); animation: tin .2s; }
.toast-ok { background: var(--ok); } .toast-err { background: var(--bad); }
.toast-warn { background: var(--warn); }
.toast.out { opacity: 0; transition: opacity .25s; }
@keyframes tin { from { transform: translateY(8px); opacity: 0; } }
#toasts { position: fixed; left: 12px; right: 12px;
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 10px);
  z-index: 70; display: flex; flex-direction: column; align-items: stretch; pointer-events: none; }

/* ---------- misc ---------- */
.chart { width: 100%; height: auto; display: block; }
.chart .bar { fill: var(--pri); }
.chart .bar.ok { fill: var(--ok); }
.chart .bar.neg { fill: var(--chart-neg); }
.chart .bar.closed { fill: var(--chart-closed); }
.chart .gap { fill: var(--chart-gap); }
.chart .zero { stroke: var(--chart-zero); }
.empty { text-align: center; color: var(--muted); padding: 24px 10px; }
.tablewrap { overflow-x: auto; }
table.mini { width: 100%; border-collapse: collapse; font-size: 14px; }
table.mini th, table.mini td { text-align: right; padding: 7px 6px;
  border-bottom: 1px solid var(--line); white-space: nowrap; }
table.mini th:first-child, table.mini td:first-child { text-align: left; }
.suggest { border: 1px solid var(--line); border-radius: 12px; overflow: hidden; margin-top: 6px; }
.suggest button { display: block; width: 100%; text-align: left; border: 0;
  background: var(--suggest-bg); color: var(--ink); padding: 12px; font: inherit;
  border-top: 1px solid var(--line); }
.suggest button:first-child { border-top: 0; }
.suggest button:active { background: var(--suggest-active); }
.cmp { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.cmp > div { border: 1px solid var(--line); border-radius: 10px; padding: 10px;
  font-size: 13px; word-break: break-word; }

/* ---------- narrow phones (320–414px): siết spacing ---------- */
@media (max-width: 480px) {
  body { font-size: 15px; }
  .grid3 { grid-template-columns: 1fr 1fr; }
  .card { padding: 11px; }
  .big { font-size: 23px; }
  .xbig { font-size: 30px; }
  .stat .val { font-size: 18px; }
  .stat.hero .val { font-size: 26px; }
  .brand { font-size: 15px; }
}
@media (max-width: 380px) {
  .brand .muted { display: none; }          /* giữ "BS PHÁT", ẩn descriptor dài */
  .pill { font-size: 11px; padding: 6px 8px; }
}
