:root {
  --bg: #f3f6f7; --card: #fff; --text: #16242b; --muted: #5b6e78; --line: #dce5e8; --soft: #eef3f5;
  --primary: #0f766e; --primary-d: #0b5d57; --primary-l: #e0f2f0; --on-primary: #fff;
  --danger: #b42318; --danger-l: #fdeceb; --warn: #9a5b00; --warn-l: #fff3dc; --ok: #146c36; --ok-l: #e3f5ea; --info: #1b5fb0; --info-l: #e5effb;
  --radius: 10px; --shadow: 0 1px 2px rgba(16, 36, 43, .06), 0 4px 14px rgba(16, 36, 43, .06);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e181c; --card: #15242a; --text: #e4edf0; --muted: #94a8b0; --line: #27393f; --soft: #1b2d34;
    --primary: #2cc4b4; --primary-d: #5ad8ca; --primary-l: #12363a; --on-primary: #04201d;
    --danger: #ff8a7d; --danger-l: #3a1f1f; --warn: #f5b84d; --warn-l: #3a2d14; --ok: #5fd68a; --ok-l: #14331f; --info: #7db4f5; --info-l: #16293f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 14px rgba(0, 0, 0, .3);
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font: 15px/1.5 var(--font); color: var(--text); background: var(--bg); }
body.noscroll { overflow: hidden; }
h1, h2, h3 { line-height: 1.25; margin: 0; }
h1 { font-size: 22px; } h2 { font-size: 17px; } h3 { font-size: 15px; }
a { color: var(--primary); }
p { margin: 0 0 8px; }
[hidden] { display: none !important; }
.ico { display: inline-flex; vertical-align: middle; flex: none; }
.muted { color: var(--muted); } .r { text-align: right; } .c { text-align: center; } .nowrap { white-space: nowrap; }
.err-text { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 4px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- Nút & form ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 40px; padding: 0 14px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--card); color: var(--text); font: inherit; font-weight: 500; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { background: var(--soft); }
.btn.sm { min-height: 32px; padding: 0 10px; font-size: 13.5px; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn.primary:hover { background: var(--primary-d); border-color: var(--primary-d); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.outline-danger { color: var(--danger); border-color: var(--danger); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.loading { position: relative; color: transparent !important; }
.btn.loading::after { content: ""; position: absolute; width: 16px; height: 16px; border: 2px solid var(--muted); border-top-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; width: 40px; height: 40px; border: 0; border-radius: 8px; background: transparent; color: inherit; cursor: pointer; }
.icon-btn:hover { background: var(--soft); }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
input, select, textarea { width: 100%; min-height: 40px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); color: var(--text); font: inherit; font-size: 16px; }
textarea { resize: vertical; min-height: 70px; }
input[type=checkbox], input[type=radio] { width: 18px; height: 18px; min-height: 0; accent-color: var(--primary); flex: none; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
input[readonly], input:disabled { background: var(--soft); color: var(--muted); }
.field { display: block; margin-bottom: 12px; min-width: 0; }
.field .lbl { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.field .help { display: block; font-size: 12.5px; color: var(--muted); margin-top: 3px; }
.req { color: var(--danger); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 14px; }
.form-grid .full { grid-column: 1 / -1; }
.check { display: flex; align-items: center; gap: 8px; min-height: 40px; cursor: pointer; }

/* ---------- Đăng nhập ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(160deg, var(--primary-l), var(--bg) 55%); }
.auth-card { width: 100%; max-width: 380px; background: var(--card); border-radius: 14px; box-shadow: var(--shadow); padding: 28px 24px; }
.auth-card .brand { text-align: center; margin-bottom: 18px; }
.logo-mark { width: 52px; height: 52px; border-radius: 14px; background: var(--primary); color: var(--on-primary); display: inline-grid; place-items: center; font-size: 28px; font-weight: 700; margin-bottom: 8px; }
.auth-card h1 { font-size: 20px; }
.auth-card .btn { width: 100%; }

/* ---------- Khung ứng dụng ---------- */
.app { display: grid; grid-template-columns: 248px minmax(0, 1fr); min-height: 100vh; }
.side { position: sticky; top: 0; height: 100vh; overflow-y: auto; background: var(--card); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 14px 10px; }
.side .brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 14px; font-weight: 700; }
.side .brand .logo-mark { width: 36px; height: 36px; font-size: 20px; border-radius: 10px; margin: 0; }
.side .brand small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }
.nav a { display: flex; align-items: center; gap: 10px; min-height: 42px; padding: 0 12px; margin-bottom: 2px; border-radius: 8px; color: var(--text); text-decoration: none; font-weight: 500; }
.nav a:hover { background: var(--soft); }
.nav a.active { background: var(--primary-l); color: var(--primary-d); font-weight: 600; }
.nav .group { margin: 14px 12px 4px; font-size: 11.5px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.side .me { margin-top: auto; padding: 12px 8px 4px; border-top: 1px solid var(--line); font-size: 13.5px; }
.side .me b { display: block; }
.side .me .row { display: flex; gap: 6px; margin-top: 8px; }
.side .me .btn { flex: 1; }
.main { padding: 22px 26px 60px; min-width: 0; max-width: 1280px; width: 100%; }
.topbar { display: none; }
.scrim { display: none; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-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); padding: 16px; margin-bottom: 16px; }
.card > h2, .card > h3 { margin-bottom: 10px; }
.card.flush { padding: 0; overflow: hidden; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }

/* ---------- Bộ lọc, tab ---------- */
.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.f-dates { display: flex; align-items: center; gap: 6px; }
.f-dates input { width: 150px; }
.f-presets { display: flex; gap: 6px; flex-wrap: wrap; }
.f-search { position: relative; flex: 1 1 220px; min-width: 180px; max-width: 360px; }
.f-search .ico { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); }
.f-search input { padding-left: 32px; }
.filters select { width: auto; min-width: 140px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; overflow-x: auto; scrollbar-width: thin; }
.tabs button, .tabs a { flex: none; min-height: 42px; padding: 0 14px; border: 0; border-bottom: 3px solid transparent; background: none; color: var(--muted); font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; }
.tabs .on { color: var(--primary-d); border-bottom-color: var(--primary); }

/* ---------- Bảng ---------- */
.table-wrap { overflow-x: auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }
.card .table-wrap { border: 0; border-radius: 0; }
.tbl { width: 100%; border-collapse: collapse; }
.tbl th { text-align: left; font-size: 12.5px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: var(--soft); padding: 10px 12px; white-space: nowrap; }
.tbl td { padding: 10px 12px; border-top: 1px solid var(--line); vertical-align: middle; }
.tbl th.r, .tbl td.r { text-align: right; } .tbl th.c, .tbl td.c { text-align: center; }
.tbl tr.click { cursor: pointer; } .tbl tr.click:hover td { background: var(--soft); }
.tbl tr.dim td { opacity: .6; }
.tbl .acts { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.tbl td.acts-cell { text-align: right; }
.empty { padding: 36px 16px; text-align: center; color: var(--muted); background: var(--card); border: 1px dashed var(--line); border-radius: var(--radius); }
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12.5px; font-weight: 600; white-space: nowrap; }
.badge.ok { background: var(--ok-l); color: var(--ok); } .badge.warn { background: var(--warn-l); color: var(--warn); }
.badge.info { background: var(--info-l); color: var(--info); } .badge.danger { background: var(--danger-l); color: var(--danger); }
.badge.muted { background: var(--soft); color: var(--muted); }
.badges { display: flex; gap: 4px; flex-wrap: wrap; }
.sum-row td { font-weight: 700; background: var(--soft); }

/* ---------- Thẻ thống kê ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 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 .k { font-size: 13px; color: var(--muted); font-weight: 600; }
.stat .v { font-size: 26px; font-weight: 700; margin-top: 2px; }
.stat .s { font-size: 12.5px; color: var(--muted); }
.stat.hl { background: var(--primary-l); border-color: transparent; } .stat.hl .v { color: var(--primary-d); }
.quick { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; }
.quick a { display: flex; align-items: center; gap: 10px; min-height: 56px; padding: 0 16px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); color: var(--text); text-decoration: none; font-weight: 600; }
.quick a:hover { border-color: var(--primary); background: var(--primary-l); }

/* ---------- Thông tin chi tiết ---------- */
.kv { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 10px 18px; }
.kv .k { font-size: 12.5px; color: var(--muted); font-weight: 600; } .kv .v { font-weight: 500; word-break: break-word; }
.totals { margin-left: auto; width: 100%; max-width: 340px; margin-top: 10px; }
.totals .row { display: flex; justify-content: space-between; padding: 4px 0; }
.totals .grand { border-top: 2px solid var(--line); margin-top: 4px; padding-top: 8px; font-size: 18px; font-weight: 700; color: var(--primary-d); }
.bar { height: 8px; border-radius: 4px; background: var(--primary); min-width: 2px; }
.bar-cell { width: 28%; }

/* ---------- Chọn bệnh nhân / dịch vụ ---------- */
.picker { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin-bottom: 14px; background: var(--soft); }
.picker .results { margin-top: 8px; max-height: 220px; overflow: auto; border-radius: 8px; background: var(--card); }
.pick-item { display: block; width: 100%; text-align: left; padding: 9px 12px; border: 0; border-bottom: 1px solid var(--line); background: var(--card); color: inherit; font: inherit; cursor: pointer; }
.pick-item:hover, .pick-item:focus-visible { background: var(--primary-l); }
.pick-item small { display: block; color: var(--muted); }
.picked { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 12px; background: var(--card); border: 1px solid var(--primary); border-radius: 8px; }
.picker-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 8px; font-weight: 600; }
.svc-pick { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; }
.svc-list { max-height: 300px; overflow: auto; border: 1px solid var(--line); border-radius: 8px; background: var(--card); }
.svc-list .grp { position: sticky; top: 0; padding: 4px 12px; background: var(--soft); font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; }
.svc-item { display: flex; align-items: center; justify-content: space-between; gap: 8px; width: 100%; padding: 8px 12px; border: 0; border-bottom: 1px solid var(--line); background: var(--card); color: inherit; font: inherit; text-align: left; cursor: pointer; }
.svc-item:hover { background: var(--primary-l); }
.svc-item small { display: block; color: var(--muted); }
.svc-item .p { font-weight: 600; white-space: nowrap; }
.cart { border: 1px solid var(--line); border-radius: 8px; background: var(--card); max-height: 300px; overflow: auto; }
.cart-row { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 8px; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.qty { display: inline-flex; align-items: center; gap: 2px; }
.qty button { width: 30px; height: 30px; border: 1px solid var(--line); border-radius: 6px; background: var(--card); color: inherit; cursor: pointer; font-size: 16px; }
.qty input { width: 48px; min-height: 30px; padding: 2px; text-align: center; }
.cart-total { display: flex; justify-content: space-between; padding: 10px 4px 0; font-weight: 700; }

/* ---------- QR ---------- */
.qr-box { text-align: center; }
.qr-box img { width: min(300px, 80vw); height: auto; background: #fff; padding: 8px; border-radius: 10px; border: 1px solid var(--line); }
.qr-amount { font-size: 28px; font-weight: 800; color: var(--primary-d); margin: 6px 0; }
.pay-list label { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-bottom: 1px solid var(--line); cursor: pointer; }
.pay-list label > span.grow { flex: 1; min-width: 0; }
.methods { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 6px 0 12px; }
.methods label { display: flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; font-weight: 600; text-align: center; }
.methods label:has(input:checked) { border-color: var(--primary); background: var(--primary-l); color: var(--primary-d); }
.methods input { position: absolute; opacity: 0; width: 1px; height: 1px; }

/* ---------- Modal, toast ---------- */
.overlay { position: fixed; inset: 0; z-index: 50; background: rgba(8, 20, 24, .55); display: flex; align-items: flex-start; justify-content: center; padding: 4vh 12px; overflow-y: auto; }
.modal { width: 100%; max-width: 640px; background: var(--card); border-radius: 14px; box-shadow: 0 18px 60px rgba(0, 0, 0, .35); display: flex; flex-direction: column; max-height: 92vh; }
.modal.sm { max-width: 440px; } .modal.lg { max-width: 860px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 12px 18px; border-bottom: 1px solid var(--line); }
.modal-body { padding: 16px 18px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; padding: 12px 18px; border-top: 1px solid var(--line); background: var(--soft); border-radius: 0 0 14px 14px; }
#toasts { position: fixed; z-index: 100; right: 14px; bottom: 14px; display: flex; flex-direction: column; gap: 8px; max-width: calc(100vw - 28px); }
.toast { padding: 11px 16px; border-radius: 10px; background: #12312d; color: #fff; box-shadow: var(--shadow); animation: pop .2s ease-out; }
.toast.err { background: #8f1d14; } .toast.warn { background: #7a4a00; }
@keyframes pop { from { opacity: 0; transform: translateY(8px); } }
.loading-block { padding: 40px; text-align: center; color: var(--muted); }
.code-box { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; background: var(--soft); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; word-break: break-all; }
.copy-row { display: flex; gap: 8px; align-items: stretch; } .copy-row .code-box { flex: 1; }
.tpl-editor { width: 100%; min-height: 360px; font: 13px/1.5 ui-monospace, Menlo, Consolas, monospace; font-size: 13px; }
.preview-frame { width: 100%; height: 480px; border: 1px solid var(--line); border-radius: 8px; background: #fff; }
.vars { display: flex; flex-wrap: wrap; gap: 6px; }
.vars code { padding: 2px 7px; border-radius: 6px; background: var(--soft); border: 1px solid var(--line); font-size: 12.5px; }

/* ---------- Mobile ---------- */
@media (max-width: 900px) {
  .app { display: block; }
  .topbar { display: flex; align-items: center; gap: 6px; position: sticky; top: 0; z-index: 30; height: 54px; padding: 0 8px; background: var(--card); border-bottom: 1px solid var(--line); font-weight: 700; }
  .topbar .t { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .side { position: fixed; z-index: 45; left: 0; top: 0; width: 284px; max-width: 86vw; transform: translateX(-102%); transition: transform .2s; box-shadow: var(--shadow); }
  .app.open .side { transform: none; }
  .app.open .scrim { display: block; position: fixed; inset: 0; z-index: 40; background: rgba(8, 20, 24, .5); }
  .main { padding: 14px 12px 70px; }
  h1 { font-size: 19px; }
  .form-grid { grid-template-columns: 1fr; }
  .svc-pick { grid-template-columns: 1fr; }
  .f-dates input { width: 138px; }
  .f-search { max-width: none; flex-basis: 100%; }
  .page-actions .btn { flex: 1; }
  .overlay { padding: 0; align-items: flex-end; }
  .modal, .modal.sm, .modal.lg { max-width: none; max-height: 94vh; border-radius: 16px 16px 0 0; }
  .modal-foot { border-radius: 0; position: sticky; bottom: 0; }
  .modal-foot .btn { flex: 1; }
  #toasts { left: 12px; right: 12px; bottom: 12px; }

  /* Bảng -> danh sách thẻ */
  .table-wrap { background: none; border: 0; overflow: visible; }
  .tbl thead { display: none; }
  .tbl, .tbl tbody, .tbl tr, .tbl td { display: block; width: 100%; }
  .tbl tr { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 10px; padding: 6px 12px; box-shadow: var(--shadow); }
  .tbl td { display: flex; justify-content: space-between; gap: 14px; border: 0; padding: 5px 0; text-align: right !important; }
  .tbl td::before { content: attr(data-label); flex: none; max-width: 42%; text-align: left; font-size: 12.5px; font-weight: 600; color: var(--muted); }
  .tbl td.acts-cell { display: block; padding-top: 8px; }
  .tbl td.acts-cell::before { display: none; }
  .tbl .acts { justify-content: flex-start; }
  .tbl .acts .btn { flex: 1 1 auto; }
  .card .table-wrap .tbl tr { box-shadow: none; }
  .bar-cell { width: auto; }
  .methods { grid-template-columns: 1fr; }
}
@media print { .side, .topbar, .page-actions, .filters { display: none !important; } .main { padding: 0; } }
