:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --border: #dde3ec;
  --text: #1c2434;
  --muted: #6b778c;
  --primary: #1a56db;
  --primary-soft: #e8effd;
  --red-bg: #f8cbad;
  --red-text: #7a1c08;
  --amber-bg: #ffe699;
  --amber-text: #6e4b00;
  --changed-bg: #fff8e1;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1420;
    --surface: #1a2130;
    --border: #2c3547;
    --text: #e6eaf2;
    --muted: #96a1b5;
    --primary: #6b9bff;
    --primary-soft: #1f2d4d;
    --red-bg: #5c2516;
    --red-text: #ffd2c2;
    --amber-bg: #4d3c0a;
    --amber-text: #ffe7a3;
    --changed-bg: #2e2810;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

a { color: inherit; text-decoration: none; }
.muted { color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 32px 16px 0; }

.btn {
  font: inherit;
  padding: 10px 18px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }

/* ── sign-in ── */
.login {
  max-width: 340px;
  margin: 15vh auto 0;
  padding: 0 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.login h1 { margin: 0; font-size: 22px; }
.login p { margin: 0; color: var(--muted); }
.login input {
  font: inherit;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
.login .error { color: #d33; }
.login-mark {
  width: 64px;
  height: 64px;
  margin: 0 auto;
  border-radius: 16px;
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  font-size: 26px;
  display: grid;
  place-items: center;
}

/* ── top bar ── */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--primary);
  color: #fff;
}
.topbar > div { flex: 1; min-width: 0; }
.title { font-weight: 700; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.subtitle { font-size: 12px; opacity: 0.85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.offline { background: #fff3; padding: 0 6px; border-radius: 6px; }
.icon-btn {
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 0;
  background: #fff2;
  color: #fff;
  font-size: 22px;
  display: grid;
  place-items: center;
}
.icon-btn.disabled { opacity: 0.3; }

/* ── list ── */
.filters { padding: 12px 16px 4px; }
.filters input {
  width: 100%;
  font: inherit;
  padding: 11px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 10px 0 4px; scrollbar-width: none; }
.chip {
  flex: none;
  font: inherit;
  font-size: 13px;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
.chip.active { background: var(--primary); border-color: var(--primary); color: #fff; }

.list { padding: 8px 16px 32px; display: flex; flex-direction: column; gap: 10px; }
.pt-card {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
}
.pt-card:active { background: var(--primary-soft); }
.pt-card-top { display: flex; align-items: center; gap: 8px; }
.pt-name { font-weight: 700; font-size: 16px; color: var(--primary); }
.pt-sub { font-size: 13px; color: var(--muted); margin-top: 2px; }
.pt-labs { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 8px; font-size: 13px; color: var(--muted); }
.pt-badges { display: flex; gap: 6px; margin-top: 8px; }
.count { text-align: center; color: var(--muted); font-size: 13px; }

.pill { display: inline-block; padding: 0 6px; border-radius: 6px; color: var(--text); font-weight: 600; }
.flag-red { background: var(--red-bg); color: var(--red-text); }
.flag-amber { background: var(--amber-bg); color: var(--amber-text); }

.badge { font-size: 11.5px; font-weight: 600; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.badge-red { background: var(--red-bg); color: var(--red-text); }
.badge-amber { background: var(--amber-bg); color: var(--amber-text); }
.badge-blue { background: var(--primary-soft); color: var(--primary); }

/* ── patient page ── */
.patient-title { min-width: 0; }
.meta { display: flex; flex-wrap: wrap; gap: 6px; padding: 12px 16px 0; }
.pill-meta {
  font-size: 13px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.pill-meta.viral { background: var(--red-bg); color: var(--red-text); border-color: transparent; }

.jump {
  position: sticky;
  top: 60px;
  z-index: 5;
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 10px 16px;
  background: var(--bg);
  scrollbar-width: none;
}
.jump button {
  flex: none;
  font: inherit;
  font-size: 13px;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

.patient { padding: 0 16px 40px; display: flex; flex-direction: column; gap: 12px; }
.section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  scroll-margin-top: 112px;
}
.section summary { padding: 12px 14px; font-weight: 700; font-size: 16px; cursor: pointer; }
.section-body { padding: 0 14px 12px; }
.group {
  margin: 14px 0 4px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--primary);
}
.group:first-child { margin-top: 0; }

.fields { display: grid; grid-template-columns: 1fr 1fr; column-gap: 14px; }
.field { padding: 8px 0; border-top: 1px solid var(--border); min-width: 0; }
.field.wide { grid-column: 1 / -1; }
.field-label { font-size: 12.5px; color: var(--muted); }
.field-value { white-space: pre-wrap; overflow-wrap: anywhere; }
.field.changed { background: var(--changed-bg); margin: 0 -7px; padding: 8px 7px; border-radius: 6px; }
.field-was { font-size: 12.5px; color: var(--muted); }

.alert { padding: 10px 12px; border-radius: 10px; margin-bottom: 8px; font-size: 14px; }
.alert-red { background: var(--red-bg); color: var(--red-text); }
.alert-amber { background: var(--amber-bg); color: var(--amber-text); }

.change { padding: 8px 0; border-top: 1px solid var(--border); font-size: 14px; }
.change:first-child { border-top: 0; }
.change-head { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.change-was { color: var(--muted); }
.change-now { font-weight: 600; }

.labs { width: 100%; border-collapse: collapse; font-size: 14px; }
.labs th, .labs td { padding: 6px 4px; border-top: 1px solid var(--border); text-align: center; }
.labs thead th { border-top: 0; color: var(--muted); font-weight: 600; }
.labs tbody th { text-align: left; font-weight: 600; }
.unit { font-weight: 400; font-size: 11px; color: var(--muted); }
.dialysis-adequacy { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 10px; font-size: 14px; }

/* ── to-do ── */
.tasks-btn { position: relative; text-decoration: none; }
.count-dot {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: #e5484d;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
}
.link-btn { font: inherit; border: 0; background: none; color: var(--primary); padding: 0; text-decoration: underline; }
.badge-task { background: #ede7fb; color: #5b3cc4; }

.todo-add { margin-bottom: 8px; }
.tag-picker { display: flex; gap: 6px; margin: 0 -14px 8px; padding: 2px 14px; overflow-x: auto; scrollbar-width: none; }
.tag-choice {
  flex: none;
  font: inherit;
  font-size: 12.5px;
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
.tag-choice.active { border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary-soft); font-weight: 600; }
.todo-add-row { display: flex; gap: 8px; }
.todo-add-row input {
  flex: 1;
  min-width: 0;
  font: inherit;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

.todos { list-style: none; margin: 0; padding: 0; }
.todos-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 0 12px; }
.todo { display: flex; align-items: flex-start; gap: 6px; padding: 10px 0; border-top: 1px solid var(--border); }
.todo:first-child { border-top: 0; }
.todo-main { flex: 1; display: flex; gap: 10px; align-items: flex-start; min-width: 0; }
.todo-check { width: 22px; height: 22px; margin: 1px 0 0; flex: none; accent-color: var(--primary); }
.todo-body { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.todo-text { overflow-wrap: anywhere; }
.todo.done .todo-text { text-decoration: line-through; color: var(--muted); }
.todo-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 12px; }
.todo-patient { color: var(--primary); font-weight: 600; }
.todo-del { flex: none; border: 0; background: none; color: var(--muted); font-size: 16px; width: 32px; height: 32px; }
.age { color: var(--muted); }
.age-old { color: var(--amber-text); background: var(--amber-bg); padding: 0 6px; border-radius: 6px; }
.history summary { cursor: pointer; color: var(--muted); font-size: 13px; padding: 8px 0 4px; }

.tag { padding: 1px 8px; border-radius: 999px; font-weight: 600; white-space: nowrap; }
.tag-refill { background: #e3f4ea; color: #1d6b3e; }
.tag-referral { background: #e8effd; color: #1a4fb5; }
.tag-labs { background: #f0e9fb; color: #6b3fb3; }
.tag-complaint { background: #fde8e8; color: #a12828; }
.tag-access { background: #fdecef; color: #9b1c4a; }
.tag-follow-up { background: #fff3d6; color: #7a5600; }
.tag-other { background: #eceff3; color: #4a5568; }
@media (prefers-color-scheme: dark) {
  .badge-task { background: #2e2550; color: #c9b8ff; }
  .tag-refill { background: #173826; color: #9be2b8; }
  .tag-referral { background: #1f2d4d; color: #a9c4ff; }
  .tag-labs { background: #2e2550; color: #d2c0ff; }
  .tag-complaint { background: #4a1f1f; color: #ffb4b4; }
  .tag-access { background: #4a1c2c; color: #ffb3cc; }
  .tag-follow-up { background: #45360b; color: #ffe08a; }
  .tag-other { background: #2c3547; color: #c3cad6; }
}

.alert-task {
  margin-top: 6px;
  font: inherit;
  font-size: 12.5px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid currentColor;
  background: transparent;
  color: inherit;
}

.toast {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 50;
  padding: 12px 14px;
  border-radius: 10px;
  background: #1c2434;
  color: #fff;
  font-size: 14px;
  text-align: center;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: none; }

/* ───────────── temporary-dialysis referral (one A4 page) ───────────── */
.print-link { color: #005430; background: #e6f2ec; border-color: #b9d9c8; font-weight: 600; text-decoration: none; }
.rf-print-btn { margin-left: auto; background: #005430; border-color: #005430; white-space: nowrap; }
.rf-wrap { padding: 16px; overflow-x: auto; }

/* The sheet is paper: always light, in either theme. */
.rf-sheet {
  --kku: #005430;
  --kku-soft: #e6f2ec;
  --kku-line: #b9d9c8;
  --ink: #1a2420;
  --grey: #6b7570;
  --purple: #7a5c9e;
  width: 210mm;
  min-height: 297mm;
  margin: 0 auto;
  padding: 9mm 11mm;
  background: #fff;
  color: var(--ink);
  font: 10.5px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.18);
  border-radius: 4px;
  outline: none;
}
.rf-head { display: flex; align-items: center; gap: 12px; padding-bottom: 9px; border-bottom: 3px solid var(--kku); position: relative; }
.rf-head::after { content: ""; position: absolute; left: 0; right: 0; bottom: -6px; height: 1.5px; background: var(--purple); opacity: 0.6; }
.rf-logo { width: 74px; height: auto; flex: none; }
.rf-org { flex: 1; }
.rf-org-name { font-size: 16px; font-weight: 800; color: var(--kku); letter-spacing: 0.2px; }
.rf-org-unit { font-size: 13px; font-weight: 700; color: var(--grey); text-transform: uppercase; letter-spacing: 1.5px; }
.rf-doc { text-align: right; }
.rf-doc-title { display: inline-block; background: var(--kku); color: #fff; font-weight: 700; font-size: 11.5px; line-height: 1.25; padding: 6px 10px; border-radius: 6px; }
.rf-doc-date { margin-top: 4px; color: var(--grey); font-weight: 600; }

.rf-patient { display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px 12px; margin: 12px 0 9px; padding: 9px 11px; background: var(--kku-soft); border-radius: 8px; border-left: 4px solid var(--kku); }
.rf-patient .rf-wide { grid-column: span 2; }
.rf-label { font-size: 8.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.6px; color: var(--grey); }
.rf-value { font-size: 11px; font-weight: 600; overflow-wrap: anywhere; }
.rf-unit { font-size: 9px; font-weight: 500; color: var(--grey); }
.rf-blank { color: #a3aba7; font-weight: 400; }
.rf-fill { display: block; min-height: 14px; border-bottom: 1px dotted #8d9792; }

.rf-cols { display: flex; gap: 8px; }
.rf-cols > .rf-box { flex: 1; min-width: 0; }
.rf-box { border: 1px solid var(--kku-line); border-radius: 8px; padding: 0 10px 8px; margin-bottom: 8px; break-inside: avoid; }
.rf-box h2 { margin: 0 -10px 8px; padding: 5px 10px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.8px; color: #fff; background: var(--kku); border-radius: 7px 7px 0 0; }
.rf-h-sub { font-weight: 500; text-transform: none; letter-spacing: 0; opacity: 0.85; font-size: 9.5px; }
.rf-box-alert { border-color: #d98a73; }
.rf-isolate { background: #fde3da; color: #7a1c08; font-weight: 700; padding: 4px 8px; border-radius: 6px; margin-bottom: 7px; }
.rf-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 10px; }
.rf-wide { grid-column: 1 / -1; }


.rf-list { display: grid; grid-template-columns: max-content 1fr auto; gap: 3px 10px; margin: 0; }
.rf-row { display: contents; }
.rf-list dt { font-weight: 700; color: var(--kku); font-size: 9.5px; padding-top: 1px; }
.rf-list dd { margin: 0; overflow-wrap: anywhere; white-space: pre-line; }
.rf-allergy { margin: 0 0 4px; }

.rf-sign { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; margin-top: 12px; padding-top: 8px; border-top: 2px solid var(--kku); }
.rf-sign .rf-line { display: block; height: 26px; border-bottom: 1px solid #8d9792; }

.rf-lab-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 5px; }
.rf-lab-grid .rf-item { padding: 4px 7px; border-radius: 6px; background: #f4f8f6; }
.rf-lab-grid .rf-flag-red { background: #f8cbad; }
.rf-lab-grid .rf-flag-red .rf-value { color: #7a1c08; }
.rf-lab-grid .rf-flag-amber { background: #ffe699; }
.rf-lab-grid .rf-flag-amber .rf-value { color: #6e4b00; }

/* Editing (screen only): show what can be typed into */
@media screen {
  .rf-value, .rf-list dd, .rf-fill, .rf-line { border-radius: 3px; transition: background 0.15s; }
  .rf-value:hover, .rf-list dd:hover, .rf-fill:hover, .rf-line:hover { background: #fff8d6; cursor: text; }
  .rf-value:focus-within, .rf-fill:focus, .rf-line:focus { background: #fff3c4; outline: 1px dashed #c9a400; }
}
.rf-sign .rf-line { min-height: 26px; height: auto; padding-top: 8px; font-size: 11px; }

/* Remove buttons (screen only) */
.rf-item, .rf-sign > div, .rf-isolate { position: relative; }
@media screen { .rf-item, .rf-sign > div { padding-right: 20px; } }
.rf-x {
  border: 0;
  background: #fde3da;
  color: #a12828;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  font-size: 10px;
  line-height: 18px;
  padding: 0;
  cursor: pointer;
  opacity: 0.55;
}
.rf-x:hover { opacity: 1; }
.rf-item > .rf-x, .rf-sign .rf-x, .rf-isolate > .rf-x { position: absolute; top: 0; right: 0; }
.rf-box h2 { display: flex; align-items: center; gap: 6px; }
.rf-box h2 > .rf-x { margin-left: auto; background: rgba(255, 255, 255, 0.2); color: #fff; opacity: 0.8; }

@media print {
  @page { size: A4 portrait; margin: 0; }
  html, body { background: #fff !important; }
  .no-print, .toast { display: none !important; }
  .rf-wrap { padding: 0; overflow: visible; }
  .rf-sheet { box-shadow: none; border-radius: 0; min-height: 0; }
  .rf-sheet * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
