:root {
  --bg: #f4f6f7;
  --surface: #ffffff;
  --surface-2: #f8fafa;
  --border: #dde3e5;
  --text: #17222b;
  --muted: #5d6b75;
  --primary: #0f766e;
  --primary-soft: #e0f2ef;
  --on-primary: #ffffff;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn: #b54708;
  --warn-soft: #fff4e5;
  --ok: #067647;
  --focus: #14b8a6;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1518; --surface: #172025; --surface-2: #1c272d; --border: #2b3940; --text: #e6edf0; --muted: #9aabb4;
    --primary: #2dd4bf; --primary-soft: #11332f; --on-primary: #06201d; --danger: #f97066; --danger-soft: #3a1a17;
    --warn: #fdb022; --warn-soft: #36270c; --ok: #47cd89; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1518; --surface: #172025; --surface-2: #1c272d; --border: #2b3940; --text: #e6edf0; --muted: #9aabb4;
  --primary: #2dd4bf; --primary-soft: #11332f; --on-primary: #06201d; --danger: #f97066; --danger-soft: #3a1a17;
  --warn: #fdb022; --warn-soft: #36270c; --ok: #47cd89; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 20px; font-weight: 700; }
h2 { font-size: 16px; font-weight: 600; }
h3 { font-size: 14px; font-weight: 600; }
a { color: var(--primary); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- top bar & tabs */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top)); background: var(--surface); border-bottom: 1px solid var(--border);
  padding-left: calc(16px + env(safe-area-inset-left)); padding-right: calc(16px + env(safe-area-inset-right));
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; font-weight: 700; white-space: nowrap; }
.brand svg { width: 28px; height: 28px; }
.brand rect { fill: var(--primary); }
.brand path { fill: none; stroke: var(--on-primary); stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }
.patient-pick { flex: 1; max-width: 420px; margin-left: auto; }
.patient-pick select { width: 100%; }
.user-box { position: relative; }
.menu { position: absolute; right: 0; top: calc(100% + 4px); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); min-width: 160px; padding: 4px; }
.menu button { display: block; width: 100%; text-align: left; padding: 8px 10px; background: none; border: 0; color: var(--text); border-radius: 6px; cursor: pointer; font: inherit; }
.menu button:hover { background: var(--surface-2); }
.tabs {
  display: flex; gap: 2px; padding: 0 16px; background: var(--surface); border-bottom: 1px solid var(--border);
  overflow-x: auto; scrollbar-width: none; position: sticky; top: 53px; z-index: 19;
}
.tabs a { display: flex; align-items: center; gap: 6px; padding: 11px 12px; color: var(--muted); text-decoration: none; white-space: nowrap; border-bottom: 2px solid transparent; font-weight: 500; -webkit-tap-highlight-color: transparent; }
.tabs svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.l-short { display: none; }
.menu-who { padding: 8px 10px 6px; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--border); margin-bottom: 4px; }
.netbar { padding: 6px 16px; font-size: 13px; text-align: center; background: var(--warn-soft); color: var(--warn); font-weight: 500; }
.netbar.ok { background: var(--primary-soft); color: var(--primary); }
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding: 8px 0 2px; }
.chips::-webkit-scrollbar, .tabs::-webkit-scrollbar { display: none; }
.chip { flex: none; padding: 5px 11px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font-size: 13px; font-weight: 500; cursor: pointer; }
.chip b { color: var(--primary); font-weight: 600; margin-left: 4px; }
.only-mobile { display: none; }
.timebar, .entry-tools { display: flex; gap: 8px; align-items: center; }
.timebar input { width: auto; }
.toolbar { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; }
.range-form .field { width: auto; }
.range-form input { width: auto; }
.sheet-wrap { max-height: calc(100vh - 220px); max-height: calc(100dvh - 220px); }
img, canvas, video { max-width: 100%; }
.plist { display: grid; gap: 10px; }
.pcard { padding: 12px 14px; display: grid; gap: 6px; }
.pcard .top { display: flex; gap: 8px; align-items: baseline; }
.pcard .acts { display: flex; gap: 8px; margin-top: 4px; }
.pcard .acts .btn { flex: 1; }
.tabs a:hover { color: var(--text); }
.tabs a.active { color: var(--primary); border-bottom-color: var(--primary); }
.view { padding: 16px; max-width: 1440px; margin: 0 auto; }

/* ---------- controls */
input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; min-height: 36px; width: 100%;
}
textarea { min-height: 70px; resize: vertical; }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible, a:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
input[type="checkbox"] { width: auto; min-height: 0; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 36px; padding: 7px 14px;
  border-radius: 8px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-weight: 500; white-space: nowrap; text-decoration: none; color: var(--text);
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); color: var(--on-primary); border-color: var(--primary); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { min-height: 30px; padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 12px; color: var(--muted); font-weight: 500; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; background: none; padding: 6px 11px; cursor: pointer; color: var(--muted); font-weight: 500; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.on { background: var(--primary-soft); color: var(--primary); }

/* ---------- cards & layout */
.page-head { display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.page-head .sub { color: var(--muted); margin-top: 2px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-h { display: flex; align-items: center; gap: 8px; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.card-b { padding: 14px; }
.grid { display: grid; gap: 14px; }
.charts { grid-template-columns: repeat(auto-fill, minmax(min(100%, 520px), 1fr)); }
.chart-box { position: relative; height: 260px; }
.empty { padding: 28px 16px; text-align: center; color: var(--muted); }

.kpis { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); margin-bottom: 14px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; }
.kpi .k { font-size: 12px; color: var(--muted); }
.kpi .v { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpi .u { font-size: 12px; color: var(--muted); font-weight: 400; margin-left: 3px; }
.kpi .d { font-size: 12px; color: var(--muted); }
.kpi.lo, .kpi.hi { border-color: var(--warn); background: var(--warn-soft); }

.patient-strip { display: flex; flex-wrap: wrap; gap: 6px 18px; padding: 10px 14px; margin-bottom: 14px; }
.patient-strip b { font-weight: 600; }
.tag { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--primary-soft); color: var(--primary); }
.tag.grey { background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.tag.warn { background: var(--warn-soft); color: var(--warn); }
.tag.red { background: var(--danger-soft); color: var(--danger); }

/* ---------- entry form */
.entry-head { position: sticky; top: 98px; z-index: 10; background: var(--bg); padding: 8px 0 10px; }
.groups { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 420px), 1fr)); align-items: start; }
.pgrid { display: grid; gap: 10px 12px; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
.pgrid .wide { grid-column: 1 / -1; }
.pfield { display: flex; flex-direction: column; }
.pfield label { flex: 1; display: block; font-size: 12px; color: var(--muted); font-weight: 500; margin-bottom: 3px; }
.pfield .unit { font-weight: 400; }
.pfield .prev { font-size: 11px; color: var(--muted); margin-top: 2px; min-height: 15px; }
.pfield.dirty input, .pfield.dirty select, .pfield.dirty textarea { border-color: var(--primary); background: var(--primary-soft); }
.pfield.warn input { border-color: var(--warn); }
.pfield .calc { min-height: 36px; display: flex; align-items: center; padding: 0 10px; border: 1px dashed var(--border); border-radius: 8px; color: var(--muted); }
.savebar { position: sticky; bottom: 0; display: flex; gap: 8px; align-items: center; padding: 10px 16px; margin: 16px -16px -16px; background: var(--surface); border-top: 1px solid var(--border); flex-wrap: wrap; }

/* ---------- tables */
.table-wrap { overflow: auto; max-width: 100%; }
table { border-collapse: collapse; width: 100%; }
th, td { padding: 7px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--surface-2); }
td.n, th.n { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:hover td { background: var(--surface-2); }

.sheet { font-size: 12.5px; }
.sheet th, .sheet td { padding: 4px 6px; border: 1px solid var(--border); white-space: nowrap; text-align: center; }
.sheet .rowh { text-align: left; position: sticky; left: 0; background: var(--surface); z-index: 1; min-width: 190px; }
.sheet thead th { position: sticky; top: 0; z-index: 2; }
.sheet thead th.rowh { z-index: 3; }
.sheet .grp td { background: var(--primary-soft); color: var(--primary); font-weight: 600; text-align: left; position: sticky; left: 0; }
.sheet td.cell { cursor: pointer; font-variant-numeric: tabular-nums; min-width: 46px; }
.sheet td.cell:hover { outline: 2px solid var(--focus); outline-offset: -2px; }
.sheet td.lo, .sheet td.hi { background: var(--warn-soft); color: var(--warn); font-weight: 600; }
.sheet td.yes { color: var(--danger); font-weight: 600; }
.sheet td.tot { font-weight: 700; background: var(--surface-2); }
.sheet th.has { color: var(--primary); }
.sheet td.txt { max-width: 160px; overflow: hidden; text-overflow: ellipsis; }

/* ---------- login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card { width: 100%; max-width: 380px; padding: 24px; }
.login h1 { margin-bottom: 4px; }
.login form { display: grid; gap: 12px; margin-top: 18px; }
.err { color: var(--danger); font-size: 13px; min-height: 18px; }

/* ---------- modal & toast */
dialog { border: 1px solid var(--border); border-radius: 12px; padding: 0; background: var(--surface); color: var(--text); width: min(640px, calc(100vw - 32px)); max-height: calc(100vh - 32px); }
dialog::backdrop { background: rgba(10, 20, 25, .45); }
dialog .card-h { position: sticky; top: 0; background: var(--surface); }
dialog form { display: contents; }
.form-grid { display: grid; gap: 10px 12px; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); }
.form-grid .wide { grid-column: 1 / -1; }
.dlg-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 14px; border-top: 1px solid var(--border); }
.toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none; background: var(--text); color: var(--surface); padding: 10px 16px; border-radius: 8px; transition: .2s; z-index: 100; max-width: calc(100vw - 32px); }
.toast.show { opacity: 1; transform: translateX(-50%); }
.toast.error { background: var(--danger); color: #fff; }

.checks { display: flex; flex-wrap: wrap; gap: 6px 14px; max-height: 200px; overflow: auto; }
.checks label { display: inline-flex; gap: 6px; align-items: center; font-size: 13px; }
details.picker summary { cursor: pointer; color: var(--primary); font-weight: 500; }
details.picker .grp-name { width: 100%; font-size: 12px; color: var(--muted); font-weight: 600; margin-top: 6px; }

@media (max-width: 720px) {
  :root { --nav-h: 60px; }
  body { padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); }
  .only-mobile { display: block; }
  .plist.only-mobile { display: grid; }
  .toolbar { width: 100%; flex-direction: column; align-items: stretch; gap: 6px; }
  .toolbar .entry-tools .btn { flex: 1; }
  .range-form { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); align-items: end; }
  .range-form input, .range-form .field { min-width: 0; }
  .form-grid > *, .pgrid > *, .kpis > * { min-width: 0; }
  .range-form .field, .range-form input { width: 100%; }
  .range-form .btn { width: 100%; }
  .sheet-wrap { max-height: calc(100dvh - 250px - var(--nav-h)); }
  .table-wrap { -webkit-overflow-scrolling: touch; }
  table { font-size: 13px; }
  .only-desktop { display: none !important; }
  .brand span { display: none; }
  .topbar { gap: 8px; padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top)); }
  .patient-pick { max-width: none; }
  .avatar-btn { padding: 0; width: 40px; min-height: 40px; border-radius: 50%; background: var(--primary-soft); color: var(--primary); font-weight: 700; }
  .avatar-btn .tag, .avatar-btn .nm { display: none; }
  /* Thanh tab chuyển xuống đáy màn hình */
  .tabs {
    position: fixed; top: auto; bottom: 0; left: 0; right: 0; z-index: 30; gap: 0; padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    border-bottom: 0; border-top: 1px solid var(--border); justify-content: space-around; overflow: visible;
  }
  .tabs a { flex: 1 1 0; flex-direction: column; gap: 2px; padding: 7px 2px 6px; min-height: var(--nav-h); justify-content: center; font-size: 11px; border-bottom: 0; border-top: 2px solid transparent; min-width: 0; }
  .tabs a.active { border-top-color: var(--primary); }
  .tabs svg { width: 22px; height: 22px; }
  .l-full { display: none; }
  .l-short { display: block; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
  .view { padding: 12px 12px 16px; padding-left: calc(12px + env(safe-area-inset-left)); padding-right: calc(12px + env(safe-area-inset-right)); }
  h1 { font-size: 18px; }
  /* Ô nhập ≥16px để iOS không tự phóng to, vùng chạm ≥44px */
  input, select, textarea { font-size: 16px; min-height: 44px; }
  .btn { min-height: 42px; }
  .btn.sm { min-height: 36px; }
  .page-head { gap: 8px; margin-bottom: 10px; }
  .page-head > .spacer { display: none; }
  .page-head > .row, .page-head > form.row { width: 100%; }
  .seg { width: 100%; overflow-x: auto; scrollbar-width: none; }
  .seg button { flex: 1 0 auto; padding: 9px 10px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .kpi { padding: 9px 10px; }
  .kpi .v { font-size: 20px; }
  .charts { gap: 10px; }
  .chart-box { height: 220px; }
  .card-b { padding: 10px; }
  .patient-strip { gap: 4px 12px; font-size: 13px; }
  .entry-head { position: static; padding: 0; }
  .entry-head .page-head { display: none; }
  .timebar { display: grid; grid-template-columns: 44px 1fr 44px; gap: 6px; width: 100%; }
  .timebar .btn { padding: 0; }
  .timebar input { min-width: 0; }
  .entry-tools { width: 100%; display: flex; gap: 6px; }
  .entry-tools .btn { flex: 1; font-size: 13px; padding: 6px 8px; white-space: normal; line-height: 1.2; }
  .chips { position: sticky; top: calc(61px + env(safe-area-inset-top)); z-index: 9; background: var(--bg); margin: 0 -12px; padding: 8px 12px; }
  .groups { gap: 10px; }
  .pgrid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px 10px; }
  .pgrid .pfield:has(textarea), .pgrid .pfield:has(input:not([inputmode])) { grid-column: 1 / -1; }
  .savebar { bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); margin: 12px -12px -16px; padding: 8px 12px; gap: 6px; }
  .savebar { flex-wrap: nowrap; }
  .savebar #dirtyInfo { font-size: 12px; line-height: 1.2; max-width: 34%; }
  .savebar > .spacer, .savebar .hide-m { display: none; }
  .savebar .btn.primary { flex: 1; }
  .sheet .rowh { min-width: 130px; max-width: 150px; white-space: normal; }
  dialog { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; border: 0; }
  dialog .card-h { padding-top: calc(12px + env(safe-area-inset-top)); }
  dialog .dlg-foot { position: sticky; bottom: 0; background: var(--surface); padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  dialog .dlg-foot .btn { flex: 1; }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-grid .field:has(select), .form-grid .field:has(input[type="datetime-local"]) { grid-column: span 1; }
  .toast { bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom)); }
}

@media (max-width: 360px) {
  .tabs a { font-size: 10px; padding-left: 0; padding-right: 0; }
  .tabs svg { width: 20px; height: 20px; }
}

@media (max-width: 340px) {
  .form-grid, .pgrid { grid-template-columns: 1fr; }
}

/* Máy tính bảng dọc: giữ thanh tab trên, 2 cột form */
@media (min-width: 721px) and (max-width: 1024px) {
  .l-full { display: none; }
  .l-short { display: inline; }
  .tabs { justify-content: space-between; }
  .tabs a { padding: 11px 8px; }
  .groups { grid-template-columns: 1fr; }
}

@media print {
  .topbar, .tabs, .no-print, .savebar, .netbar, .chips { display: none !important; }
  body { padding-bottom: 0 !important; }
  body { background: #fff; color: #000; }
  .view { padding: 0; max-width: none; }
  .card { box-shadow: none; border: 0; }
  .sheet { font-size: 9px; }
  .sheet .rowh { position: static; min-width: 0; }
  .table-wrap { overflow: visible; }
  @page { size: A4 landscape; margin: 8mm; }
}
