/* Layout: one-screen ward sheet — 9 stacked columns that fit a 13–15" laptop with no sideways scroll;
   below 1024px each patient becomes a compact block. */
:root {
  --bg: #F1F4F2;
  --surface: #FFFFFF;
  --surface-2: #F6F8F7;
  --head: #EEF2F0;
  --ink: #15201C;
  --muted: #5A6863;
  --line: #D9E1DD;
  --grid: #E3E9E6;
  --accent: #1D6858;
  --accent-ink: #FFFFFF;
  --accent-soft: #DFEDE8;
  --warn: #9E4C18;
  --warn-soft: #FAEBDF;
  --danger: #B32418;
  --danger-soft: #FBE6E3;
  --focus: #2D8C77;
  --shadow: 0 1px 2px rgba(20, 40, 34, .06);
  /* System fonts: render Vietnamese well, work offline, and need no third-party request. */
  --font-body: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --radius: 12px;
  --bar-h: 58px;
  /* Minimum widths sum to 986px so the sheet fits 1024px minus 16px gutters; dates need room for "dd/mm/yyyy · T6".
     Last column: action icons (Operate · Discharge · Xóa) stacked. */
  --cols: 26px 50px 136px minmax(104px, 1.1fr) 162px 64px 96px minmax(152px, 1.6fr) minmax(162px, 1.6fr) 34px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0E1412; --surface: #151D1A; --surface-2: #1A2420; --head: #1C2622; --ink: #E3EBE8; --muted: #93A29C;
    --line: #26322E; --grid: #222D29; --accent: #5DBFA6; --accent-ink: #08201A; --accent-soft: #17302A;
    --warn: #E89A62; --warn-soft: #34251A; --danger: #F08074; --danger-soft: #3A1F1C;
    --focus: #6FD0B6; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0E1412; --surface: #151D1A; --surface-2: #1A2420; --head: #1C2622; --ink: #E3EBE8; --muted: #93A29C;
  --line: #26322E; --grid: #222D29; --accent: #5DBFA6; --accent-ink: #08201A; --accent-soft: #17302A;
  --warn: #E89A62; --warn-soft: #34251A; --danger: #F08074; --danger-soft: #3A1F1C;
  --focus: #6FD0B6; --shadow: none; color-scheme: dark;
}

[hidden] { display: none !important; }
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.45;
  overflow-x: hidden;
}
body::before {
  content: ""; position: fixed; inset: 0 0 auto; height: env(safe-area-inset-top, 0px);
  z-index: 10; background: var(--surface);
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }

.wrap {
  margin: 0 auto; max-width: 1600px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}

/* ---------- Header ---------- */
.bar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 6;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.bar .wrap { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; padding-block: 9px; }
.brand { font-weight: 700; font-size: 17px; letter-spacing: -.01em; margin-right: auto; display: flex; align-items: baseline; gap: 8px; }
.brand small { font-weight: 500; font-size: 12px; color: var(--muted); }
/* min-width: 0 lets the long date label shrink (with …) instead of pushing past a phone screen. */
.datenav { display: flex; align-items: center; gap: 4px; order: 3; flex: 1 1 100%; min-width: 0; }
.datebox {
  flex: 1; min-width: 0; display: block; text-align: center; padding: 6px 10px; border-radius: 8px;
  background: var(--surface-2); border: 1px solid var(--line); font-weight: 600;
}
.datebox:hover, .datebox[aria-expanded="true"] { border-color: var(--accent); }
.datebox .date-label { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 8px;
  min-width: 36px; height: 36px; display: inline-grid; place-items: center; font-size: 18px; line-height: 1;
}
.btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 8px;
  padding: 6px 12px; font-size: 14px; font-weight: 500; white-space: nowrap;
}
.btn:hover, .icon-btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--ink); background: var(--surface-2); }
.btn.small { padding: 3px 9px; font-size: 13px; }
.btn.danger { color: var(--danger); }
.btn.danger.armed { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn.today.is-today { opacity: .45; pointer-events: none; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
@media (min-width: 720px) { .datenav { order: 0; flex: 0 0 auto; } }
@media (max-width: 520px) {
  .actions { flex: 1 1 100%; justify-content: space-between; gap: 2px; }
  .actions .btn { padding: 6px 8px; font-size: 13.5px; }
}

.subbar .wrap { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; padding-block: 10px 4px; }
.summary { font-size: 13.5px; color: var(--muted); font-variant-numeric: tabular-nums; margin-right: auto; }
.summary b { color: var(--ink); font-weight: 600; }
.summary .warn { color: var(--warn); }
.search {
  flex: 0 1 260px; min-width: 0; border: 1px solid var(--line); background: var(--surface);
  border-radius: 8px; padding: 6px 10px; font-size: 14px;
}
@media (max-width: 520px) { .search { flex: 1 1 100%; } }

/* Tabs All · Working · Reminder, each with its count */
.tabs { display: inline-flex; gap: 2px; padding: 3px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; }
.tab {
  display: inline-flex; align-items: baseline; gap: 6px; border: 0; border-radius: 7px; background: transparent;
  padding: 4px 12px; font-size: 13.5px; font-weight: 600; color: var(--muted); white-space: nowrap;
}
.tab b { font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums; }
.tab:hover { background: var(--surface-2); color: var(--ink); }
.tab[aria-selected="true"] { background: var(--accent); color: var(--accent-ink); }
@media (max-width: 520px) { .tabs { flex: 1 1 100%; } .tab { flex: 1; justify-content: center; } .summary { flex: 1 1 100%; } }

/* ---------- Quét ảnh (photo of the HIS screen → Thêm bệnh nhân) ---------- */
.scan-btn {
  display: inline-flex; align-items: center; gap: 6px; margin-left: auto; padding: 5px 11px; border-radius: 8px;
  border: 1px solid var(--accent); background: var(--accent-soft); color: var(--accent); font-size: 13.5px; font-weight: 600; white-space: nowrap;
}
.scan-btn svg { width: 16px; height: 16px; flex: none; }
.scan-btn:hover { filter: brightness(.97); }
.dlg-head .scan-btn + .dlg-x { margin-left: 0; }
.scan-note { display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: baseline; font-size: 13px; border-radius: 8px; padding: 7px 10px; background: var(--accent-soft); color: var(--ink); }
.scan-note.warn { background: var(--warn-soft); color: var(--warn); }
.scan-note .link-btn { font-size: 13px; font-weight: 600; text-decoration: underline; color: inherit; }
/* Boxes filled from a photo stay tinted until edited; uncertain readings in amber */
.ifld.scanned { background: var(--accent-soft); }
.ifld.scanned > span::after { content: " · từ ảnh"; font-weight: 500; color: var(--accent); }
.ifld.unsure { background: var(--warn-soft); }
.ifld.unsure > span::after { content: " · đọc chưa chắc?"; font-weight: 600; color: var(--warn); }

dialog#dlgScan.scan-wide { width: min(820px, calc(100vw - 24px)); }
.scan-step { display: grid; gap: 10px; }
.scan-sources { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.scan-src {
  display: grid; justify-items: center; gap: 3px; text-align: center; padding: 16px 8px; border: 1px dashed var(--line);
  border-radius: 12px; background: var(--surface-2); cursor: pointer;
}
.scan-src svg { width: 26px; height: 26px; color: var(--accent); }
.scan-src b { font-size: 14px; }
.scan-src small { font-size: 12px; color: var(--muted); }
.scan-src:hover, .scan-src:focus-within, .scan-drop.over { border-style: solid; border-color: var(--accent); }
.scan-drop { cursor: default; }
/* Camera only where there is one to point at the screen (phones, tablets); paste / drop only with a mouse */
@media (pointer: fine) { .scan-camera { display: none; } .scan-sources { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (pointer: coarse) { .scan-drop { display: none; } .scan-sources { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.scan-hint { margin: 0; font-size: 12.5px; color: var(--muted); }
.scan-photo { position: relative; width: fit-content; max-width: 100%; margin: 0 auto; overflow: hidden; border-radius: 8px; user-select: none; touch-action: none; }
.scan-photo img { display: block; max-width: 100%; max-height: min(58vh, 520px); border-radius: 8px; }
.scan-box { position: absolute; border: 2px solid #fff; border-radius: 4px; box-shadow: 0 0 0 9999px rgba(0, 0, 0, .45); cursor: move; }
.scan-box i { position: absolute; width: 22px; height: 22px; border-radius: 50%; background: #fff; border: 2px solid var(--accent); }
.scan-box i[data-h="nw"] { left: -12px; top: -12px; cursor: nwse-resize; }
.scan-box i[data-h="ne"] { right: -12px; top: -12px; cursor: nesw-resize; }
.scan-box i[data-h="sw"] { left: -12px; bottom: -12px; cursor: nesw-resize; }
.scan-box i[data-h="se"] { right: -12px; bottom: -12px; cursor: nwse-resize; }
.scan-progress { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.scan-track { height: 8px; border-radius: 99px; border: 1px solid var(--line); background: var(--surface-2); overflow: hidden; }
.scan-fill { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.scan-table { max-height: 52vh; overflow: auto; border: 1px solid var(--line); border-radius: 10px; }
.scan-table table { width: 100%; border-collapse: collapse; font-size: 13px; }
.scan-table th { position: sticky; top: 0; background: var(--surface-2); text-align: left; padding: 6px 8px; font-size: 11.5px; color: var(--muted); border-bottom: 1px solid var(--line); }
.scan-table td { padding: 3px 5px; border-bottom: 1px solid var(--grid); }
.scan-table td input:not([type="checkbox"]) { width: 100%; min-width: 0; padding: 3px 5px; border: 1px solid transparent; border-radius: 6px; background: transparent; }
.scan-table td input:not([type="checkbox"]):hover { border-color: var(--line); }
.scan-table td input:not([type="checkbox"]):focus { border-color: var(--focus); outline: none; }
.scan-table td input.unsure { background: var(--warn-soft); }
.scan-table .c-room { width: 60px; } .scan-table .c-dob { width: 70px; } .scan-table .c-code { width: 104px; } .scan-table .c-admit { width: 112px; }
.scan-table tr.known td { color: var(--muted); }
.scan-tag { font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 99px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
@media (max-width: 700px) {
  .scan-table { max-height: none; }
  .scan-table .c-dob, .scan-table .c-admit { display: none; }
  .scan-photo img { max-height: 60vh; }
}

/* ---------- Custom sort (Working) ---------- */
.sortbtn { display: inline-flex; align-items: center; gap: 6px; padding: 5px 11px; font-size: 13.5px; }
.sortbtn svg { width: 14px; height: 14px; flex: none; }
.sortbtn.on { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); font-weight: 600; }
.hrow > .h-sort { padding: 0; }
.h-sort-btn {
  display: block; width: 100%; height: 100%; padding: 6px 8px; border: 0; background: transparent; text-align: left;
  font: inherit; font-size: 11.5px; font-weight: 600; color: var(--muted); line-height: 1.25;
}
.h-sort-btn:hover { background: var(--accent-soft); color: var(--ink); }
.h-sort-btn small { display: block; font-weight: 500; opacity: .85; }
.sort-mark { color: var(--accent); font-weight: 700; margin-left: 3px; white-space: nowrap; }
.sort-mark sup { font-size: 9px; }
.sort-menu {
  position: fixed; z-index: 30; min-width: 150px; display: grid; padding: 4px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 10px 28px rgba(0, 0, 0, .16);
}
.sort-menu button { display: flex; justify-content: space-between; gap: 10px; border: 0; background: none; text-align: left; padding: 6px 10px; border-radius: 6px; font-size: 13.5px; font-weight: 500; }
.sort-menu button:hover, .sort-menu button:focus-visible { background: var(--surface-2); }
/* Values of the sorted columns get a light tint so the order is easy to follow */
.sheet[data-sorted~="room"] [data-field="room"], .sheet[data-sorted~="name"] [data-field="name"],
.sheet[data-sorted~="age"] .age, .sheet[data-sorted~="diagnosis"] .c-dx [data-field="diagnosis"],
.sheet[data-sorted~="admissionDate"] [data-field="admissionDate"], .sheet[data-sorted~="opDate"] [data-op-field="date"],
.sheet[data-sorted~="ap"] [data-field="ap"], .sheet[data-sorted~="surgeon"] [data-op-field="surgeon"] { background: var(--accent-soft); }
.sheet[data-sorted~="postOp"] .podbadge .big { box-shadow: 0 0 0 2px var(--accent-soft); }

.sort-levels { display: grid; gap: 6px; }
.sort-level { display: grid; grid-template-columns: 74px minmax(0, 1fr) minmax(0, 1.1fr) auto; gap: 6px 8px; align-items: center; }
.sort-level > b { font-size: 12.5px; color: var(--muted); font-weight: 600; }
.sort-level select { width: 100%; min-width: 0; border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; background: var(--surface); }
.sort-tools { display: flex; gap: 2px; }
.ico-btn { width: 28px; height: 30px; border: 1px solid transparent; border-radius: 7px; background: none; color: var(--muted); font-size: 14px; }
.ico-btn:hover:not(:disabled) { border-color: var(--line); color: var(--ink); }
.ico-btn:disabled { opacity: .3; cursor: default; }
.sort-edit { grid-column: 3; justify-self: start; margin-top: -2px; font-size: 12.5px; }
.sort-tail { margin: 0; font-size: 12.5px; color: var(--muted); }
.clist { display: grid; gap: 8px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); }
.clist h3 { margin: 0; font-size: 13.5px; }
.clist p { margin: 0; font-size: 12.5px; color: var(--muted); }
.clist ol { margin: 0; padding: 0; list-style: none; display: grid; gap: 4px; max-height: 240px; overflow: auto; counter-reset: item; }
.clist li { counter-increment: item; display: flex; align-items: center; gap: 8px; padding: 2px 4px 2px 10px; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; }
.clist li::before { content: counter(item); min-width: 14px; font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.clist li span { flex: 1; min-width: 0; font-weight: 500; overflow-wrap: anywhere; }
@media (max-width: 700px) {
  .sort-level { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; }
  .sort-level > b { grid-column: 1 / -1; }
  .sort-edit { grid-column: 2; }
  .dlg-form .dlg > .dlg-actions { margin-top: auto; }
}

.banner { font-size: 13px; border-radius: 8px; padding: 6px 12px; display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center; }
.banner.offline { background: var(--surface-2); color: var(--muted); border: 1px dashed var(--line); }
.banner.other-day { background: var(--warn-soft); color: var(--warn); font-weight: 500; }
.banner button { border: 0; background: none; padding: 0; text-decoration: underline; color: inherit; font-size: 13px; font-weight: 600; }
.banner .close-banner { margin-left: auto; text-decoration: none; font-size: 16px; line-height: 1; }
.banners { display: grid; gap: 8px; padding-block: 8px; }

main.wrap { padding-block: 2px 96px; }
.empty { text-align: center; color: var(--muted); padding: 48px 16px; border: 1px dashed var(--line); border-radius: var(--radius); }

/* ---------- Sheet (laptop: 9 columns, no sideways scroll) ---------- */
.sheet {
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface);
  box-shadow: var(--shadow); overflow: clip; font-size: 13px; line-height: 1.4;
}
.hrow, .prow { display: grid; grid-template-columns: var(--cols); }
.hrow {
  position: sticky; top: calc(env(safe-area-inset-top, 0px) + var(--bar-h)); z-index: 4;
  background: var(--head); border-bottom: 1px solid var(--line);
}
.hrow > div {
  padding: 6px 8px; font-size: 11.5px; font-weight: 600; color: var(--muted); line-height: 1.25;
  border-right: 1px solid var(--grid); min-width: 0;
}
.hrow > div:last-child, .prow > .c-act { border-right: 0; }
.hrow .h-act { text-align: center; padding-inline: 0; }
.hrow small { display: block; font-weight: 500; opacity: .85; }

.prow { border-bottom: 1px solid var(--grid); transition: background-color .5s; }
/* All tab: one heading row per week */
.wrow { display: flex; align-items: baseline; gap: 10px; padding: 6px 12px; background: var(--head); border-bottom: 1px solid var(--line); font-size: 12.5px; }
.wrow span { color: var(--muted); font-size: 12px; }
.prow:last-child { border-bottom: 0; }
.prow.leaving { background: var(--surface-2); }
.prow.flash { background: var(--accent-soft); }
.cell { padding: 6px 8px; border-right: 1px solid var(--grid); min-width: 0; position: relative; }
/* Excel-style active cell */
.cell:focus-within { box-shadow: inset 0 0 0 2px var(--focus); z-index: 1; }
.c-idx { text-align: right; color: var(--muted); font-family: var(--font-mono); font-size: 11.5px; background: var(--head); padding-inline: 4px 6px; }
.c-r { font-family: var(--font-mono); font-weight: 600; font-size: 15px; color: var(--accent); background: var(--accent-soft); text-align: center; padding-inline: 2px; }
.c-r.is-er { background: var(--danger-soft); color: var(--danger); }
.er-cap { display: block; font-family: var(--font-body); font-size: 9px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase; }

.namebox { display: flex; gap: 2px; align-items: flex-start; min-width: 0; }
.namebox .name { flex: 1; min-width: 0; font-weight: 600; font-size: 13.5px; }
/* Laptop: the row itself opens the history (name, # cell, empty spots); text cells still edit on click */
@media (min-width: 1024px) {
  .prow > .cell:not(.follow):not(.notes):not(.c-act) { cursor: pointer; }
  .prow [contenteditable="true"] { cursor: text; }
  .namebox .name:not(:focus) { cursor: pointer; }
  .namebox .name:not(:focus):hover { background: transparent; text-decoration: underline; text-decoration-color: var(--accent); text-underline-offset: 3px; }
  .prow.is-open:not(.flash) { background: color-mix(in srgb, var(--accent-soft) 45%, var(--surface)); }
}

.kv { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 0 5px; align-items: baseline; min-width: 0; }
.k { font-size: 10.5px; font-weight: 600; color: var(--muted); letter-spacing: .02em; min-width: 2.1em; }
.sub { color: var(--muted); }
.age { color: var(--muted); white-space: nowrap; }
/* Diagnosis column: "Δ:" chẩn đoán (bold), "θ:" điều trị — the shorthand used in handwritten notes */
.sym { font-weight: 600; white-space: nowrap; min-width: 1.4em; }
.dx-line, .dx-line .diag { font-weight: 700; }

[contenteditable] { display: block; border-radius: 3px; outline: none; min-height: 1.4em; min-width: 1.5em; overflow-wrap: anywhere; }
[contenteditable]:hover { background: var(--surface-2); }
[contenteditable]:empty::before { content: attr(data-ph); color: var(--muted); opacity: .4; font-weight: 400; }
.inline-ce { display: inline-block; }

/* ---------- DatePicker (spec: UI_component.md) ---------- */
.dp {
  display: inline-flex; align-items: center; gap: 6px; width: 100%; min-width: 0; text-align: left; white-space: nowrap;
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: var(--surface-2);
  font-variant-numeric: tabular-nums;
}
.dp svg { width: 14px; height: 14px; flex: none; color: var(--muted); }
.dp .dp-text { overflow: hidden; text-overflow: ellipsis; }
.dp.is-empty { color: var(--muted); }
.dp:hover, .dp[aria-expanded="true"] { border-color: var(--focus); }
/* Compact in table cells, like the old date inputs */
.dp.cell-date { border: 0; background: transparent; padding: 0 2px 0 0; gap: 4px; font-size: 12px; height: 1.45em; border-radius: 3px; }
.dp.cell-date svg { width: 10px; height: 10px; opacity: .7; }
.dp.cell-date:hover { background: var(--surface-2); }
/* Op.Date stands out once the patient has a surgery date */
.dp.cell-date[data-op-field="date"]:not(.is-empty) { font-weight: 700; }
.dp.cell-date[aria-expanded="true"] { box-shadow: inset 0 0 0 2px var(--focus); }

.dp-pop {
  position: fixed; z-index: 40; width: 296px; max-width: calc(100vw - 16px); display: grid; gap: 8px; padding: 10px;
  background: var(--surface); color: var(--ink); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, .18); font-size: 14px; line-height: 1.3; text-align: left; font-weight: 400;
}
.dp-quick { display: flex; gap: 6px; }
.dp-quick button {
  flex: 1; border: 1px solid var(--line); background: var(--surface-2); border-radius: 8px; padding: 6px 4px;
  font-size: 12.5px; font-weight: 500; white-space: nowrap;
}
.dp-quick button:hover { border-color: var(--accent); color: var(--accent); }
.dp-input {
  width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; background: var(--surface-2);
  font-size: 15px; font-variant-numeric: tabular-nums; letter-spacing: .02em;
}
.dp-input:focus { outline: none; border-color: var(--focus); background: var(--surface); }
.dp-input.invalid { border-color: var(--danger); box-shadow: 0 0 0 1px var(--danger); }
.dp-head { display: flex; gap: 4px; align-items: center; }
.dp-head select { flex: 1; min-width: 0; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); padding: 5px 6px; font-size: 13.5px; }
.dp-head button { flex: none; width: 32px; height: 30px; border: 1px solid var(--line); background: var(--surface); border-radius: 8px; font-size: 17px; line-height: 1; }
.dp-head button:hover { background: var(--surface-2); }
.dp-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; text-align: center; }
.dp-wd { font-size: 11px; font-weight: 600; color: var(--muted); padding: 2px 0 4px; }
.dp-day {
  position: relative; height: 34px; border: 0; background: none; border-radius: 8px;
  font-size: 13.5px; font-variant-numeric: tabular-nums; color: var(--ink);
}
.dp-day:hover:not(:disabled) { background: var(--accent-soft); }
.dp-day.out { color: var(--muted); }
.dp-day.sel { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.dp-day.today::after {
  content: ""; position: absolute; left: 50%; bottom: 4px; width: 4px; height: 4px; margin-left: -2px;
  border-radius: 50%; background: var(--accent);
}
.dp-day.sel.today::after { background: var(--accent-ink); }
.dp-day:disabled { opacity: .28; cursor: not-allowed; }
.dp-foot { display: flex; justify-content: flex-end; }
.dp-foot .link-btn { color: var(--danger); font-size: 12.5px; }

.podbadge { display: grid; justify-items: start; gap: 2px; font-variant-numeric: tabular-nums; }
.podbadge .big {
  font-family: var(--font-mono); font-weight: 600; font-size: 13px; padding: 0 5px; border-radius: 5px;
  background: var(--accent); color: var(--accent-ink);
}
.podbadge.pre .big { background: var(--surface-2); color: var(--ink); border: 1px solid var(--line); }
.podbadge small { font-size: 10.5px; color: var(--muted); line-height: 1.25; }
.podbadge.not-yet .big { font-family: var(--font-body); font-size: 11px; white-space: nowrap; }
.podbadge .dis-date { font-variant-numeric: tabular-nums; }
/* The Post-Op column is ~48px inside: keep "Ra viện" on one line */
.podbadge .pill { white-space: nowrap; padding: 0 4px; font-size: 10px; }
.pill { display: inline-block; font-size: 10.5px; font-weight: 600; padding: 0 6px; border-radius: 99px; background: var(--danger-soft); color: var(--danger); }

/* Following: two lines at rest, full height while editing */
.follow textarea {
  display: block; width: 100%; resize: none; border: 0; background: transparent; padding: 0; outline: none;
  font-size: 13px; line-height: 1.4; min-height: 1.4em; max-height: 2.8em; overflow: hidden;
}
.follow textarea:focus { max-height: none; }
.follow textarea::placeholder { color: var(--muted); opacity: .45; }
.follow.more:not(:focus-within)::after {
  content: "…"; position: absolute; right: 8px; bottom: 5px; padding-left: 10px; color: var(--muted); font-weight: 700;
  background: linear-gradient(90deg, transparent, var(--surface) 40%);
}
.prow.leaving .follow.more:not(:focus-within)::after { background: linear-gradient(90deg, transparent, var(--surface-2) 40%); }

/* Notes: first two tasks at rest (unfinished first), all when opened */
.tasks { list-style: none; margin: 0; padding: 0; display: grid; gap: 0; }
.notes.clamped .task:nth-child(n+3) { display: none; }
.task { display: flex; align-items: flex-start; gap: 5px; padding: 1px 1px; border-radius: 4px; min-width: 0; }
.task:hover { background: var(--surface-2); }
.task input[type="checkbox"] { width: 14px; height: 14px; accent-color: var(--accent); margin: 2px 0 0; flex: none; cursor: pointer; }
.task-body { flex: 1; min-width: 0; }
.ticon {
  display: inline-grid; place-items: center; width: 20px; height: 18px; border-radius: 5px; margin-right: 3px; vertical-align: -3px;
  background: var(--accent-soft); font-size: 12.5px; line-height: 1; cursor: default;
}
.task-note.inline-ce { display: inline; min-width: 3em; }
.carry { font-size: 10.5px; font-weight: 600; color: var(--warn); white-space: nowrap; margin-left: 3px; }
.task.done .task-note { text-decoration: line-through; color: var(--muted); }
.task.done .ticon { opacity: .45; filter: grayscale(1); }
.x { border: 0; background: none; color: var(--muted); font-size: 14px; line-height: 1; padding: 1px 4px; border-radius: 4px; flex: none; }
.x:hover { color: var(--danger); background: var(--danger-soft); }
.task .x { opacity: 0; }
.task:hover .x, .task:focus-within .x { opacity: 1; }
@media (hover: none) { .task .x { opacity: .5; } }
.notes-foot { display: flex; flex-wrap: wrap; gap: 2px 10px; align-items: center; }
.link-btn { border: 0; background: none; padding: 0; color: var(--accent); font-size: 12px; font-weight: 500; }
.link-btn:hover { text-decoration: underline; }
.link-btn.more-tasks { color: var(--warn); font-weight: 600; }
.addrow { display: flex; flex-wrap: wrap; gap: 3px; align-items: center; margin-top: 3px; }
.type-btn { border: 1px dashed var(--line); background: var(--surface); border-radius: 99px; padding: 0 8px; font-size: 11.5px; font-weight: 500; }
.type-btn:hover { border-style: solid; border-color: var(--accent); color: var(--accent); }

/* Expanded detail spans the whole row */
/* The outer box has no padding so it can shrink to 0 while the history closes */
.pdetail { grid-column: 1 / -1; overflow: hidden; cursor: default; }
.pdetail-pad { padding: 0 8px 10px 38px; }
.d-collapse { display: flex; justify-content: center; max-width: 900px; }
.collapse-btn {
  display: inline-grid; place-items: center; width: 34px; height: 26px; padding: 0; border: 1px solid var(--line);
  border-radius: 99px; background: var(--surface); color: var(--muted);
}
.collapse-btn svg { width: 14px; height: 14px; }
.collapse-btn:hover { color: var(--accent); border-color: var(--accent); }
.detail-inner {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px 20px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); padding: 12px 14px; font-size: 13px;
}
.d-sec { display: grid; gap: 6px; align-content: start; min-width: 0; }
.d-sec h3 { margin: 0; font-size: 11px; font-weight: 600; letter-spacing: .07em; text-transform: uppercase; color: var(--muted); }
.d-actions { grid-column: 1 / -1; display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 6px; border-top: 1px solid var(--line); padding-top: 8px; }

.history { display: grid; gap: 10px; max-width: 900px; }
.h-day {
  display: grid; grid-template-columns: 66px 48px minmax(0, 1fr); gap: 0 10px; align-items: start;
  border-left: 2px solid var(--line); padding-left: 10px;
}
.h-day.is-current { border-left-color: var(--accent); }
/* Date and POD share the first line height of the day's content */
.h-when { display: grid; justify-items: start; gap: 2px; font-size: 12px; line-height: 19px; color: var(--muted); white-space: nowrap; }
.h-when .h-tag { line-height: 16px; }
.h-pod { font-family: var(--font-mono); font-size: 12.5px; line-height: 19px; font-weight: 600; color: var(--ink); white-space: nowrap; }
.h-body { display: grid; gap: 3px; min-width: 0; }
.h-body > .h-dx { line-height: 19px; }
@media (max-width: 520px) { .h-day { grid-template-columns: 54px 44px minmax(0, 1fr); gap: 0 8px; padding-left: 8px; } }
.h-tag { font-size: 10.5px; font-weight: 600; padding: 0 6px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); white-space: nowrap; }
/* Diagnosis set or changed that day */
.h-dx { display: flex; align-items: baseline; gap: 4px; font-weight: 600; }
.h-dx .diag { flex: 1; min-width: 0; }
.h-dx [contenteditable]:hover { background: var(--surface); }
.h-dx .x { opacity: 0; }
.h-dx:hover .x, .h-dx:focus-within .x { opacity: 1; }
@media (hover: none) { .h-dx .x { opacity: .5; } }
/* Surgery that day (POD0); planned ones sit above the days */
.h-planned { display: grid; gap: 4px; }
.h-op { display: grid; gap: 1px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); padding: 5px 9px; cursor: pointer; }
.h-op:hover { border-color: var(--accent); }
.h-op:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
.h-op.mine { border-left: 3px solid var(--accent); }
.h-op.planned { border-style: dashed; background: transparent; color: var(--muted); }
.h-op.planned b { color: var(--muted); }
.h-op-title, .h-op-dx { display: flex; align-items: baseline; gap: 6px; }
.h-op-dx b { flex: 1 1 auto; min-width: 0; }
.h-op-dx .h-ico { width: 14px; justify-content: center; font-weight: 700; }
.h-op-title b { flex: 1 1 auto; min-width: 0; }
.h-op-title .h-ico { align-self: center; }
.h-op-sub { font-size: 12px; color: var(--muted); padding-left: 20px; white-space: pre-wrap; }
.h-op-sub i { font-style: normal; font-weight: 600; }
.h-ico { display: inline-flex; color: var(--muted); flex: none; }
.h-ico svg { width: 14px; height: 14px; }
.h-op.mine .h-ico { color: var(--accent); }
.h-op .h-op-dx .h-ico { color: var(--ink); }
.h-dis { display: flex; align-items: center; gap: 6px; color: var(--danger); font-weight: 600; font-size: 12.5px; }
.h-dis .h-ico { color: var(--danger); }
/* Following of that day, edited in place */
.h-note {
  width: 100%; border: 0; border-radius: 4px; background: transparent; color: var(--ink); font: inherit; line-height: 19px;
  padding: 0 4px; margin-left: -4px; resize: none; overflow: hidden; display: block;
}
.h-note:hover { background: var(--surface); }
.h-note:focus { background: var(--surface); outline: 1px solid var(--focus); }
.h-note::placeholder { color: var(--muted); opacity: .7; }
.h-day ul { margin: 0; padding: 0; list-style: none; font-size: 12px; color: var(--muted); }
/* The owner wants every word in the history in black ink (icons and the placeholder keep their colour). */
.pdetail .d-sec h3, .h-when, .h-tag, .h-op-sub, .h-op.planned, .h-op.planned b, .h-dis, .h-day ul { color: var(--ink); }
.muted { font-size: 12.5px; color: var(--muted); margin: 0; }


/* ---------- Narrow screens: one compact block per patient ---------- */
@media (max-width: 1023px) {
  .sheet { border: 0; background: transparent; box-shadow: none; overflow: visible; display: grid; gap: 10px; font-size: 14px; }
  .wrow { background: transparent; border: 0; padding: 6px 2px 0; }
  .hrow { display: none; }
  .prow {
    grid-template-columns: 52px minmax(0, 1fr) minmax(0, 1fr) auto;
    grid-template-areas:
      "r name name pod"
      "r dx dx dx"
      "dates dates docs docs"
      "follow follow follow follow"
      "notes notes notes notes"
      "act act act act";
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
    overflow: clip;
  }
  .prow:last-child { border-bottom: 1px solid var(--line); }
  .cell { border-right: 0; padding: 6px 10px; }
  .c-idx { display: none; }
  .c-r { grid-area: r; font-size: 18px; padding-top: 10px; }
  .c-name { grid-area: name; padding-top: 9px; }
  .c-dx { grid-area: dx; padding-top: 0; }
  .c-pod { grid-area: pod; padding-top: 9px; }
  .c-dates { grid-area: dates; border-top: 1px solid var(--grid); }
  .c-docs { grid-area: docs; border-top: 1px solid var(--grid); border-left: 1px solid var(--grid); }
  .follow { grid-area: follow; border-top: 1px solid var(--grid); }
  .notes { grid-area: notes; border-top: 1px solid var(--grid); }
  .follow::before, .notes::before {
    content: attr(data-label); display: block; font-size: 10.5px; font-weight: 600; letter-spacing: .05em;
    text-transform: uppercase; color: var(--muted); margin-bottom: 2px;
  }
  .namebox .name { font-size: 15px; }
  .c-name .kv-row { display: flex; flex-wrap: wrap; gap: 0 12px; }
  .follow textarea { font-size: 14px; }
  .dp.cell-date { font-size: 13px; }
  .pdetail-pad { padding: 10px 10px 10px; border-top: 1px solid var(--grid); }
  .task input[type="checkbox"] { width: 18px; height: 18px; margin-top: 1px; }
}

/* ---------- Dialogs ---------- */
dialog {
  border: 1px solid var(--line); border-radius: 14px; padding: 0; background: var(--surface); color: var(--ink);
  width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 40px);
}
dialog::backdrop { background: rgba(10, 20, 17, .45); }
.dlg { display: grid; gap: 14px; padding: 18px; }
.dlg h2 { margin: 0; font-size: 18px; text-wrap: balance; }
.dlg .hint { margin: -8px 0 0; font-size: 13px; color: var(--muted); }
.fld { display: grid; gap: 4px; min-width: 0; }
.fld > span { font-size: 12.5px; font-weight: 600; color: var(--muted); }
.fld > span i { font-style: normal; font-weight: 400; }
.fld input, .type-add input, .type-list input {
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: var(--surface-2); width: 100%; min-width: 0;
}
.fld input:focus, .type-add input:focus, .type-list input:focus { outline: none; border-color: var(--focus); background: var(--surface); }
.fld .err { font-size: 12.5px; color: var(--danger); font-weight: 500; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.dlg-actions .push-left { margin-right: auto; }

.type-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.type-list li { display: flex; gap: 8px; align-items: center; }
.icon-select {
  flex: none; width: 52px; border: 1px solid var(--line); border-radius: 8px; padding: 7px 4px; background: var(--surface-2);
  font-size: 16px; text-align: center; color: var(--ink);
}
.type-btn .ticon { width: auto; height: auto; background: none; margin-right: 2px; vertical-align: 0; }
.type-add { display: flex; gap: 8px; }
.type-add input { flex: 1; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(16px + env(safe-area-inset-bottom, 0px)); z-index: 20;
  background: var(--ink); color: var(--bg); border-radius: 10px; padding: 10px 14px; font-size: 14px;
  display: flex; gap: 14px; align-items: center; max-width: calc(100vw - 32px); box-shadow: 0 6px 20px rgba(0,0,0,.2);
}
.toast button { border: 0; background: none; color: var(--accent-soft); font-weight: 700; padding: 0; text-decoration: underline; }

/* ---------- Sync status ---------- */
.sync { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); white-space: nowrap; border: 0; background: none; padding: 2px 4px; border-radius: 6px; }
.sync:hover { background: var(--surface-2); }
.sync::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.sync[data-state="synced"]::before { background: var(--accent); }
.sync[data-state="saving"]::before { background: var(--warn); }
.sync[data-state="offline"]::before, .sync[data-state="error"]::before { background: var(--danger); }
.sync[data-state="auth"] { color: var(--danger); font-weight: 600; }
.sync[data-state="auth"]::before { background: var(--danger); }

/* ---------- Login gate ---------- */
.gate {
  position: fixed; inset: 0; z-index: 30; background: var(--bg); display: grid; place-items: center;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
}
.gate-card {
  width: min(400px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 16px;
  padding: 28px 22px; display: grid; gap: 14px; box-shadow: var(--shadow); text-align: center;
}
.gate-card h1 { margin: 0; font-size: 24px; letter-spacing: -.01em; }
.gate-card p { margin: 0; color: var(--muted); font-size: 14px; }
.gate-card form { display: grid; gap: 10px; text-align: left; }
.gate-card .btn.primary { padding: 11px 14px; font-size: 15.5px; }
.gate-card .link-btn { font-size: 13.5px; justify-self: center; }
.gate-err { color: var(--danger) !important; font-weight: 500; }
.gate-logo { width: 56px; height: 56px; justify-self: center; border-radius: 14px; }

/* ---------- Settings ---------- */
.set-sec { display: grid; gap: 8px; border-top: 1px solid var(--line); padding-top: 12px; }
.set-sec h3 { margin: 0; font-size: 11px; font-weight: 600; letter-spacing: .07em; text-transform: uppercase; color: var(--muted); }
.set-sec p { margin: 0; font-size: 13.5px; }
.set-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.pair-code { font-family: var(--font-mono); font-size: 24px; font-weight: 600; letter-spacing: .12em; color: var(--accent); }
.passkey-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; font-size: 13.5px; }
.passkey-list small { color: var(--muted); }
.arch-tools { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: space-between; font-size: 13px; color: var(--muted); }

/* ---------- Action column: Operate · Discharge · Xóa ---------- */
.c-act { display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 5px 3px; }
.act-btn {
  display: inline-flex; align-items: center; justify-content: center; width: 26px; height: 24px; padding: 0;
  border: 1px solid transparent; border-radius: 6px; background: none; color: var(--muted); font-size: 13px; font-weight: 500;
}
.act-btn svg { width: 15px; height: 15px; flex: none; }
.act-btn .act-label { display: none; }
.act-btn:hover { background: var(--surface-2); color: var(--ink); border-color: var(--line); }
.act-btn.on { color: var(--accent); background: var(--accent-soft); }
.act-btn.del:hover { color: var(--danger); background: var(--danger-soft); border-color: transparent; }
.act-btn.detail-btn { display: none; } /* laptop opens the history from the row itself */
.mine-link { font-size: 12px; white-space: nowrap; color: var(--muted); }
.mine-link.is-mine { color: var(--accent); font-weight: 600; }

/* ---------- Operate / Discharge / Confirm dialogs ---------- */
.op-pick { display: flex; flex-wrap: wrap; gap: 6px; margin-top: -4px; }
.op-chip {
  border: 1px solid var(--line); background: var(--surface); border-radius: 99px; padding: 3px 11px; font-size: 13px; font-weight: 500;
}
.op-chip:hover { border-color: var(--accent); color: var(--accent); }
.op-chip.sel { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.seg span { display: inline-block; border: 1px solid var(--line); border-radius: 8px; padding: 7px 14px; font-size: 14px; font-weight: 500; background: var(--surface); }
.seg input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.seg input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 2px; }
.dis-today { padding: 10px 14px; font-size: 15px; }
.btn.danger-solid { background: var(--danger); border-color: var(--danger); color: #fff; font-weight: 600; }
#dlgDischarge, #dlgConfirm { width: min(420px, calc(100vw - 24px)); }

/* ---------- Thêm bệnh nhân / Operate: one compact form that fits without scrolling ----------
   Laptop: 608px dialog, 6-column grid (.s1–.s6). Phone ≤700px: full-screen form, 12-column grid (.p4–.p12). */
dialog.dlg-form { width: min(608px, calc(100vw - 24px)); }
.dlg-form .dlg { gap: 12px; padding: 16px 18px; }
.dlg-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.dlg-x { border: 0; background: none; font-size: 22px; line-height: 1; padding: 2px 8px; color: var(--muted); border-radius: 6px; }
.dlg-x:hover { color: var(--ink); background: var(--surface-2); }
/* The title takes the opening focus (instead of ×) so no ring shows and the phone keyboard stays closed */
.dlg-head h2:focus { outline: none; }
.dlg-form .op-pick { margin-top: -6px; }
.fgrid { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 8px; }
.fgrid .s2 { grid-column: span 2; } .fgrid .s3 { grid-column: span 3; } .fgrid .s4 { grid-column: span 4; } .fgrid .s6 { grid-column: 1 / -1; }
/* The "my case" fields join the parent grid; hidden until a role is chosen */
.mine-group { display: contents; }
.mine-group[hidden] { display: none; }
.fgrid .sep { grid-column: 1 / -1; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding-top: 6px; margin-top: 2px; border-top: 1px solid var(--line);
  font-size: 11px; font-weight: 600; letter-spacing: .07em; text-transform: uppercase; color: var(--muted); }
.fgrid .sep i { font-style: normal; text-transform: none; letter-spacing: 0; font-weight: 400; font-size: 12px; }
.fgrid .seg { gap: 4px; flex-wrap: nowrap; }
.fgrid .seg span { padding: 3px 10px; font-size: 13px; border-radius: 7px; white-space: nowrap; text-transform: none; letter-spacing: 0; color: var(--ink); }
.fgrid .seg input:checked + span { color: var(--accent-ink); }

/* Field with its small label inside the box. No fill and no frame at rest (the owner found grey blocks busy);
   the frame shows on hover and while typing. The border stays transparent so nothing shifts when it appears. */
.ifld {
  display: grid; gap: 1px; min-width: 0; padding: 4px 10px 5px; cursor: text;
  border: 1px solid transparent; border-radius: 8px; background: transparent;
}
.ifld:hover { border-color: var(--line); }
.ifld:focus-within { border-color: var(--focus); }
.ifld > span { font-size: 11px; line-height: 14px; font-weight: 600; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ifld > span i { font-style: normal; font-weight: 400; }
.ifld input, .ifld textarea {
  border: 0; border-radius: 0; background: transparent; padding: 0; width: 100%; min-width: 0; outline: none;
  font-size: 14.5px; line-height: 20px;
}
.ifld textarea { resize: none; overflow: hidden; display: block; }
.ifld input::placeholder, .ifld textarea::placeholder { color: var(--muted); opacity: .7; }
.ifld .dp { border: 0; background: transparent; padding: 0; border-radius: 0; font-size: 14.5px; line-height: 20px; gap: 6px; }
.ifld .dp svg { width: 13px; height: 13px; }
.ifld .dp:focus-visible { outline: none; }
.pct-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.pct-row input { width: 44px; flex: none; font-variant-numeric: tabular-nums; }
.pct-row b { color: var(--muted); font-weight: 600; margin-right: 4px; }
.pct-chips { display: flex; gap: 4px; flex-wrap: wrap; }
.pct-chips .op-chip { padding: 0 9px; font-size: 12.5px; line-height: 20px; }
.form-err { margin: 0; font-size: 12.5px; color: var(--danger); font-weight: 500; }

@media (max-width: 700px) {
  /* Full-screen form on the phone; 12 columns so 2–3 fields share a row */
  dialog.dlg-form {
    width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border: 0; border-radius: 0;
  }
  /* Column flex: the field grid takes the free height, the buttons stay at the bottom */
  .dlg-form .dlg {
    display: flex; flex-direction: column; min-height: 100%; gap: 10px;
    padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  }
  .dlg-form .fgrid { flex: 1 0 auto; }
  .dlg-form .dlg h2 { font-size: 17px; }
  .fgrid { grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 7px; align-content: start; }
  .fgrid .p4 { grid-column: span 4; } .fgrid .p6 { grid-column: span 6; } .fgrid .p8 { grid-column: span 8; } .fgrid .p12 { grid-column: 1 / -1; }
  /* 14px as the owner asked; the viewport meta has maximum-scale=1 so iPhone does not zoom into the field */
  .ifld input, .ifld textarea, .ifld .dp { font-size: 14px; line-height: 19px; }
  .ifld { padding: 3px 9px 4px; }
  /* Patient fields first, so Ad.Date sits next to Op.Date; the "Phẫu thuật" rule is dropped to save a line */
  .fgrid .ph-first { order: -1; }
  .fgrid .sep-op { display: none; }
  .dlg-form .dlg > .dlg-actions .btn { flex: 1; padding: 10px 14px; font-size: 15px; }
  .dlg-form .dlg > .dlg-actions .push-left { margin-right: 0; }
}

/* Phone / narrow: an icon row at the bottom of each block — delete alone on the left; Operate · Discharge · Detail on the right */
@media (max-width: 1023px) {
  .c-act { grid-area: act; flex-direction: row; justify-content: flex-end; gap: 6px; border-top: 1px solid var(--grid); padding: 6px 10px; }
  .act-btn { width: 40px; height: 36px; border: 1px solid var(--line); }
  .act-btn svg { width: 18px; height: 18px; }
  .act-btn.detail-btn { display: inline-flex; }
  .act-btn.detail-btn.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
  .act-btn.del { order: -1; margin-right: auto; border-color: transparent; color: var(--danger); }
}

/* ---------- Select mode: tick many patients, then Set one field or delete them ---------- */
.sortbtn[aria-pressed="true"] { border-color: var(--accent); background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.ck {
  display: inline-grid; place-items: center; width: 18px; height: 18px; flex: none;
  border: 1.6px solid var(--muted); border-radius: 5px; background: var(--surface);
}
.ck.on, .ck.some { background: var(--accent); border-color: var(--accent); }
.ck.on::after { content: ""; width: 9px; height: 5px; border: solid var(--accent-ink); border-width: 0 0 2px 2px; transform: translateY(-1px) rotate(-45deg); }
.ck.some::after { content: ""; width: 9px; height: 2px; border-radius: 1px; background: var(--accent-ink); }

/* While selecting, a click anywhere on a row ticks it: the cells inside stop taking clicks (no editing, no buttons) */
.sheet.selecting .prow { cursor: pointer; user-select: none; -webkit-user-select: none; }
.sheet.selecting .prow > * { pointer-events: none; }
.sheet.selecting .prow.sel { background: var(--accent-soft); }
.sheet.selecting .prow.sel > .c-idx { box-shadow: inset 3px 0 0 var(--accent); }
.sheet.selecting .c-idx { display: grid; place-items: start center; padding-top: 8px; }
.sheet.selecting .c-act { visibility: hidden; }
.hrow .h-ck { display: grid; place-items: center; padding: 0; }
.h-ck button { border: 0; background: none; padding: 6px; display: grid; place-items: center; }
@media (min-width: 1024px) { .sheet.selecting .prow:not(.sel):hover { background: color-mix(in srgb, var(--accent-soft) 45%, var(--surface)); } }
@media (max-width: 1023px) {
  .sheet.selecting .prow { position: relative; }
  .sheet.selecting .prow.sel { border-color: var(--accent); box-shadow: 0 0 0 1.5px var(--accent); }
  .sheet.selecting .prow.sel > .c-idx { box-shadow: none; }
  .sheet.selecting .c-idx { position: absolute; top: 10px; left: 0; width: 52px; padding: 0; background: none; z-index: 1; }
  .sheet.selecting .c-idx .ck { width: 24px; height: 24px; border-radius: 50%; }
  .sheet.selecting .c-r { padding-top: 40px; }
  .sheet.selecting .c-act { display: none; }
}

.selbar {
  position: fixed; z-index: 21; left: 50%; /* above the toast (20), so the Set menu is never covered */ transform: translateX(-50%); bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  display: flex; align-items: center; gap: 6px; white-space: nowrap;
  background: var(--ink); color: var(--bg); border-radius: 14px; padding: 6px 6px 6px 10px; box-shadow: 0 8px 24px rgba(0, 0, 0, .22);
}
.sel-all { display: inline-flex; align-items: center; gap: 7px; border: 0; background: none; color: inherit; padding: 4px 10px 4px 0; border-right: 1px solid color-mix(in srgb, var(--bg) 25%, transparent); }
.sel-all .ck { border-color: var(--bg); background: transparent; }
.sel-all .ck.on, .sel-all .ck.some { background: var(--accent); border-color: var(--accent); }
.sel-count { font-weight: 600; font-variant-numeric: tabular-nums; min-width: 6.5em; margin: 0 4px; }
.sel-btn { display: inline-flex; align-items: center; gap: 6px; border: 0; border-radius: 9px; padding: 7px 12px; font-weight: 600; color: inherit; background: color-mix(in srgb, var(--bg) 14%, transparent); }
.sel-btn svg { width: 15px; height: 15px; flex: none; }
.sel-btn:disabled { opacity: .45; cursor: default; }
.sel-btn.del { color: color-mix(in srgb, var(--danger) 55%, var(--bg)); }
.sel-btn.done { background: var(--accent); color: var(--accent-ink); }
.sel-menu {
  position: absolute; bottom: calc(100% + 8px); left: 50%; transform: translateX(-50%); min-width: 220px;
  display: grid; padding: 6px; background: var(--surface); color: var(--ink); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 10px 28px rgba(0, 0, 0, .2);
}
.sel-menu .mh { font-size: 11px; font-weight: 600; letter-spacing: .07em; text-transform: uppercase; color: var(--muted); padding: 4px 10px 2px; }
.sel-menu button { display: flex; justify-content: space-between; gap: 16px; border: 0; background: none; text-align: left; padding: 8px 10px; border-radius: 8px; font-weight: 500; }
.sel-menu button:hover, .sel-menu button:focus-visible { background: var(--surface-2); }
.sel-menu small { color: var(--muted); font-weight: 400; }
/* The toast sits above the bar while selecting */
body.selecting .toast { bottom: calc(80px + env(safe-area-inset-bottom, 0px)); }
@media (max-width: 520px) {
  .selbar { left: 10px; right: 10px; transform: none; padding: 6px; gap: 4px; }
  .sel-count { min-width: 0; margin-right: auto; }
  .sel-btn { padding: 9px 11px; }
  .sel-btn .sel-text { display: none; }
  .sel-menu { left: auto; right: 0; transform: none; }
}

.bulk-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.bulk-chips:empty { display: none; }
.bulk-chips button { border: 1px solid var(--line); background: var(--surface); border-radius: 99px; padding: 3px 10px; font-size: 13px; }
.bulk-chips button.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.bulk-plan { display: grid; gap: 6px; font-size: 13.5px; }
.bulk-plan:empty { display: none; }
.bulk-plan .ok { color: var(--accent); font-weight: 600; }
.bulk-plan .info { color: var(--muted); }
.bulk-plan .skip { background: var(--warn-soft); color: var(--warn); border-radius: 8px; padding: 6px 10px; }
.bulk-plan ul { margin: 2px 0 0; padding-left: 18px; }

/* ---------- Accounts: who sees what ---------- */
/* Students: no deleting, no All tab, Select, Sắp xếp, Loại việc, export (the server refuses these too) */
body.role-student .no-student { display: none !important; }
body:not(.role-owner) .owner-only { display: none !important; }
body:not(.role-admin) .admin-only { display: none !important; }
.book-label { font-size: 12.5px; font-weight: 600; color: var(--warn); background: var(--warn-soft); border-radius: 6px; padding: 2px 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
/* What a student wrote carries their name */
.by-tag { display: inline-block; font-size: 10.5px; font-weight: 600; color: var(--warn); margin-left: 4px; white-space: nowrap; }
.by-line { line-height: 1; }
.follow .by-tag { display: block; margin: 0; text-align: right; }
.role-badge { display: inline-block; font-size: 10.5px; font-weight: 700; letter-spacing: .04em; border-radius: 5px; padding: 0 6px; background: var(--accent-soft); color: var(--accent); }
.role-badge.admin { background: var(--ink); color: var(--bg); }
.role-badge.student { background: var(--warn-soft); color: var(--warn); }
.role-badge.locked { background: var(--surface-2); color: var(--muted); border: 1px solid var(--line); }
.count-pill { display: inline-block; margin-left: 6px; min-width: 18px; padding: 0 6px; border-radius: 99px; background: var(--warn); color: #fff; font-size: 11.5px; font-weight: 700; }

/* Sign-in gate */
.gate-buttons { display: grid; gap: 10px; }
.google-btn { display: flex; align-items: center; justify-content: center; gap: 10px; padding: 11px 14px; font-size: 15.5px; font-weight: 600; }
.g-mark { width: 20px; height: 20px; border-radius: 50%; display: grid; place-items: center; font-weight: 800; font-size: 13px; color: #1A73E8; border: 1.6px solid #1A73E8; }
.gate .or { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 12px; }
.gate .or::before, .gate .or::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.gate-fine { font-size: 12.5px !important; }
/* App lock: above the gate and the date picker; opaque, so nothing of the book shows through */
.lock-screen { z-index: 50; }
.lock-screen .gate-card p.lock-who { color: var(--ink); font-weight: 600; overflow-wrap: anywhere; }
.gate-state { display: grid; gap: 4px; text-align: left; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 4px; font-size: 14px; }

/* ---------- Settings page: menu on the left, one section at a time ---------- */
/* While it is open the sheet, its toolbar and the date bar step aside; "← Danh sách" goes back */
body.in-settings .subbar, body.in-settings #list, body.in-settings .datenav, body.in-settings #openAdd { display: none !important; }
.back-btn { display: none; }
body.in-settings .back-btn { display: inline-flex; }
#openSettings[aria-current="page"] { color: var(--accent); background: var(--accent-soft); font-weight: 600; }
.settings-layout { display: grid; grid-template-columns: 230px minmax(0, 1fr); gap: 0; padding-block: 16px 96px; align-items: stretch; }
/* The menu stretches to the section's height, so the two read as one card */
.set-nav {
  display: grid; gap: 2px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius) 0 0 var(--radius); padding: 10px 8px; min-height: 420px; align-content: start;
}
.set-nav .nav-h { font-size: 11px; font-weight: 600; letter-spacing: .07em; text-transform: uppercase; color: var(--muted); padding: 10px 10px 4px; }
.set-nav .nav-h:first-child { padding-top: 4px; }
.set-nav button { display: flex; align-items: center; justify-content: space-between; gap: 8px; border: 0; background: none; text-align: left; padding: 8px 10px; border-radius: 8px; font-size: 14px; color: var(--ink); }
.set-nav button:hover { background: var(--surface-2); }
.set-nav button[aria-current="true"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.set-pane {
  display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; align-content: start; min-width: 0; min-height: 420px; padding: 20px 26px 28px;
  background: var(--surface); border: 1px solid var(--line); border-left: 0; border-radius: 0 var(--radius) var(--radius) 0;
}
.set-pane[hidden] { display: none; }
.set-pane h2 { margin: 0; font-size: 19px; text-wrap: balance; }
.set-pane h3 { margin: 8px 0 0; font-size: 12px; font-weight: 600; letter-spacing: .07em; text-transform: uppercase; color: var(--muted); }
.set-pane .lead { margin: -4px 0 0; color: var(--muted); font-size: 13.5px; max-width: 80ch; }
.set-pane p { margin: 0; font-size: 14px; }
.set-pane .small { font-size: 12.5px; }
.set-pane .type-list, .set-pane .type-add { max-width: 520px; }
.perm-list { list-style: none; margin: 0; padding: 12px 14px; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px 24px; font-size: 13px; border: 1px solid var(--line); border-radius: 10px; }
.perm-list li::before { content: "✓ "; color: var(--accent); font-weight: 700; }
.perm-list li.no { color: var(--muted); }
.perm-list li.no::before { content: "✕ "; color: var(--danger); }
@media (max-width: 719px) {
  /* Phone: the menu becomes a row of chips above the section */
  .settings-layout { grid-template-columns: minmax(0, 1fr); gap: 10px; padding-block: 10px 96px; }
  .set-nav { position: static; display: flex; gap: 6px; overflow-x: auto; min-height: 0; border: 0; background: none; padding: 0 0 2px; scrollbar-width: none; }
  .set-nav .nav-h { display: none; }
  .set-nav button { flex: none; border: 1px solid var(--line); background: var(--surface); border-radius: 99px; padding: 6px 12px; font-size: 13.5px; white-space: nowrap; }
  .set-pane { border-left: 1px solid var(--line); border-radius: var(--radius); padding: 16px 14px 22px; min-height: 0; }
  .perm-list { grid-template-columns: minmax(0, 1fr); }
}
.table-box { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; }
table.people { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.people th { text-align: left; font-size: 11.5px; font-weight: 600; color: var(--muted); background: var(--head); padding: 6px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.people td { padding: 7px 10px; border-bottom: 1px solid var(--grid); vertical-align: top; }
table.people tr:last-child td { border-bottom: 0; }
table.people td small { display: block; color: var(--muted); font-size: 12px; }
table.people td.acts { text-align: right; white-space: nowrap; }
table.people td.acts .btn + .btn { margin-left: 4px; }
table.people tr.locked td { color: var(--muted); }
table.people .soon { color: var(--warn); font-weight: 600; }
table.log td.when { white-space: nowrap; font-family: var(--font-mono); font-size: 12px; color: var(--muted); }
table.log .diff del { color: var(--danger); }
table.log .diff ins { color: var(--accent); text-decoration: none; font-weight: 600; }
.add-student { display: grid; grid-template-columns: minmax(0, 1fr) 190px auto; gap: 10px; align-items: end; }
@media (max-width: 600px) { .add-student { grid-template-columns: 1fr; } }
.err-line { margin: 0; color: var(--danger); font-size: 13px; font-weight: 500; }
.log-filters { display: flex; gap: 8px; flex-wrap: wrap; }
.log-filters select { border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; background: var(--surface); max-width: 100%; }
.admin-line { margin: 0; font-size: 13.5px; }
.passkey-list li { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.passkey-list li .btn { margin-left: auto; }
.bar .book-label { margin-right: auto; }
.brand:has(+ .book-label:not([hidden])) { margin-right: 0; }

/* ---------- Photos of a day ---------- */
/* Camera icon in the Following cell (top right), with the number of photos of the day shown */
.follow { padding-right: 34px; }
.photo-btn {
  position: absolute; top: 4px; right: 4px; z-index: 1; display: inline-flex; align-items: center; gap: 2px;
  height: 24px; min-width: 26px; padding: 0 4px; border: 1px solid transparent; border-radius: 6px; background: none; color: var(--muted);
}
.photo-btn svg { width: 16px; height: 16px; flex: none; }
.photo-btn b { font-size: 11px; font-weight: 700; font-variant-numeric: tabular-nums; }
.photo-btn:hover { background: var(--surface-2); border-color: var(--line); color: var(--ink); }
.photo-btn.has { color: var(--accent); }
@media (max-width: 1023px) {
  .follow { padding-right: 46px; }
  .photo-btn { top: 6px; right: 8px; height: 32px; min-width: 36px; border-color: var(--line); justify-content: center; }
  .photo-btn svg { width: 18px; height: 18px; }
}

/* Thumbnails in the history */
.h-photos { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 2px; }
.h-photo {
  position: relative; display: grid; gap: 3px; width: 112px; padding: 0; border: 0; background: none; text-align: left; cursor: zoom-in;
}
.h-photo img { width: 112px; height: 84px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); background: var(--surface-2); display: block; }
.h-photo .cap { font-size: 11.5px; color: var(--muted); line-height: 1.25; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.h-photo .pend { position: absolute; top: 4px; left: 4px; font-size: 10px; font-weight: 600; background: var(--warn-soft); color: var(--warn); border-radius: 4px; padding: 0 4px; }
.h-photo.missing img { opacity: .4; }
@media (max-width: 520px) { .h-photo, .h-photo img { width: 96px; } .h-photo img { height: 72px; } }

/* "Thêm ảnh": one card per photo with its caption */
.photo-new { display: grid; gap: 8px; max-height: min(60vh, 520px); overflow-y: auto; }
.photo-new:empty { display: none; }
.pn-item { display: grid; grid-template-columns: 88px minmax(0, 1fr) auto; gap: 10px; align-items: center; border: 1px solid var(--line); border-radius: 10px; padding: 6px; }
.pn-item.bad { border-color: var(--danger); }
.pn-img { width: 88px; height: 66px; border-radius: 6px; overflow: hidden; background: var(--surface-2); display: grid; place-items: center; }
.pn-img img { width: 100%; height: 100%; object-fit: cover; }
.pn-body { display: grid; gap: 4px; min-width: 0; }
.pn-cap { border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; background: var(--surface-2); min-width: 0; font-size: 14px; }
.pn-cap:focus { outline: none; border-color: var(--focus); background: var(--surface); }
.pn-body small { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.spin { width: 20px; height: 20px; border-radius: 50%; border: 2.5px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Big view: the whole screen */
dialog.photo-viewer {
  width: 100vw; height: 100dvh; max-width: none; max-height: none; margin: 0; border: 0; border-radius: 0;
  background: #0B0F0E; color: #E8EEEC; display: none; grid-template-rows: auto minmax(0, 1fr) auto;
}
dialog.photo-viewer[open] { display: grid; }
dialog.photo-viewer::backdrop { background: #0B0F0E; }
.pv-head { display: flex; align-items: center; gap: 10px; padding: calc(10px + env(safe-area-inset-top, 0px)) max(14px, env(safe-area-inset-right, 0px)) 10px max(14px, env(safe-area-inset-left, 0px)); }
.pv-title { margin-right: auto; min-width: 0; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pv-title span { color: #9FB0AA; }
.pv-head .btn.danger { color: #FF9C90; background: transparent; border-color: #3A2A28; }
.pv-x { width: 38px; height: 38px; border: 0; border-radius: 50%; background: #1F2725; color: inherit; font-size: 22px; line-height: 1; }
/* The photo is placed and zoomed by photo-zoom.js (transform); the stage takes every press, drag and pinch */
.pv-stage { position: relative; overflow: hidden; min-height: 0; touch-action: none; cursor: zoom-in; user-select: none; -webkit-user-select: none; }
.pv-stage.zoomed { cursor: grab; }
.pv-stage.zoomed.dragging { cursor: grabbing; }
.pv-stage img {
  position: absolute; left: 0; top: 0; max-width: none; max-height: none; transform-origin: 0 0; will-change: transform;
  pointer-events: none; -webkit-user-drag: none;
}
.pv-nav {
  position: absolute; top: 50%; transform: translateY(-50%); z-index: 1; width: 44px; height: 64px; border: 0; border-radius: 10px;
  background: rgba(20, 28, 26, .55); color: #fff; font-size: 30px; line-height: 1;
}
.pv-nav.prev { left: 10px; }
.pv-nav.next { right: 10px; }
.pv-nav[hidden] { display: none; }
.pv-foot { display: grid; gap: 4px; padding: 10px max(14px, env(safe-area-inset-right, 0px)) calc(12px + env(safe-area-inset-bottom, 0px)) max(14px, env(safe-area-inset-left, 0px)); }
.pv-foot input { border: 1px solid #2E3936; border-radius: 10px; background: #151C1A; color: inherit; padding: 9px 12px; font-size: 15px; }
.pv-foot input:focus { outline: none; border-color: #5DBFA6; }
.pv-meta { font-size: 12px; color: #9FB0AA; min-height: 1.2em; }
