:root {
  --bg: #f3f4f0; --card: #ffffff; --ink: #1b1f1b; --muted: #66706a; --line: #dfe3dc;
  --accent: #2f5d3a; --accent-ink: #ffffff; --red: #b3261e; --red-bg: #fde8e6;
  --amber: #8a5a00; --amber-bg: #fff2d6; --blue: #1f4e8c; --blue-bg: #e3edfb;
  --green: #22693a; --green-bg: #e2f3e6; --grey-bg: #eceee9; --bar: #1f2a1f; --bar-ink: #f3f4f0;
  --radius: 14px; --shadow: 0 1px 2px rgba(0,0,0,.06);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111411; --card: #1b201c; --ink: #e8ece6; --muted: #9aa49c; --line: #2c332d;
    --accent: #6fb27f; --accent-ink: #0d140f; --red: #ff8a80; --red-bg: #3b1714;
    --amber: #f4c066; --amber-bg: #3a2b0c; --blue: #9cc1f5; --blue-bg: #17263b;
    --green: #8fd6a2; --green-bg: #15301d; --grey-bg: #252b26; --bar: #0b0e0b; --bar-ink: #e8ece6;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  -webkit-text-size-adjust: 100%; }
body { padding-bottom: env(safe-area-inset-bottom); }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.center { text-align: center; padding: 40px 16px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* top bar */
.bar { position: sticky; top: 0; z-index: 10; background: var(--bar); color: var(--bar-ink);
  padding: calc(env(safe-area-inset-top) + 10px) 12px 10px; display: flex; align-items: center; gap: 8px; }
.bar h1 { font-size: 18px; margin: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar button { background: transparent; border: 0; color: var(--bar-ink); padding: 8px 10px;
  border-radius: 10px; font-size: 16px; min-height: 40px; cursor: pointer; }
.bar button:active { background: rgba(255,255,255,.12); }
.bar .primary { background: var(--accent); color: var(--accent-ink); font-weight: 600; }

.wrap { max-width: 720px; margin: 0 auto; padding: 12px 12px 90px; }

/* login */
.login { max-width: 360px; margin: 12vh auto 0; padding: 0 16px; }
.login h1 { font-size: 24px; margin: 0 0 4px; }
.login .card { padding: 18px; }

/* inputs */
label.f { display: block; margin: 0 0 12px; }
label.f > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
input[type=text], input[type=password], input[type=search], input[type=date], input[type=number],
input[type=tel], select, textarea {
  width: 100%; border: 1px solid var(--line); background: var(--card); border-radius: 10px;
  padding: 11px 12px; min-height: 44px; font-size: 16px; }
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; }
.check input { width: 22px; height: 22px; accent-color: var(--accent); }
.hint { font-size: 12px; color: var(--muted); margin-top: 3px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--line);
  background: var(--card); border-radius: 10px; padding: 10px 14px; min-height: 44px; cursor: pointer; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.danger { color: var(--red); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row > .btn { flex: 1; }

/* list */
.search { position: sticky; top: calc(env(safe-area-inset-top) + 60px); z-index: 5; background: var(--bg);
  padding: 4px 0 8px; }
.chips { display: flex; gap: 6px; overflow-x: auto; padding: 2px 0 8px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; border: 1px solid var(--line); background: var(--card); border-radius: 999px;
  padding: 6px 12px; font-size: 14px; cursor: pointer; white-space: nowrap; }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.group { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 14px 4px 6px; }
.list { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.item { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-bottom: 1px solid var(--line);
  cursor: pointer; text-decoration: none; color: inherit; }
.item:last-child { border-bottom: 0; }
.item:active { background: var(--grey-bg); }
.avatar { flex: none; width: 46px; height: 46px; border-radius: 50%; background: var(--grey-bg); display: grid;
  place-items: center; font-weight: 700; color: var(--muted); overflow: hidden; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.item .who { flex: 1; min-width: 0; }
.item .who b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .who span { display: block; font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tags { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; flex: none; max-width: 45%; }

.tag { display: inline-block; font-size: 12px; font-weight: 700; padding: 3px 7px; border-radius: 6px;
  background: var(--grey-bg); color: var(--ink); white-space: nowrap; }
.tag.blood { background: var(--blue-bg); color: var(--blue); min-width: 38px; text-align: center; }
.tag.allergy { background: var(--red-bg); color: var(--red); }
.tag.s-active { background: var(--green-bg); color: var(--green); }
.tag.s-warn { background: var(--amber-bg); color: var(--amber); }
.tag.s-bad { background: var(--red-bg); color: var(--red); }
.tag.donor { background: var(--green-bg); color: var(--green); }

/* record */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; margin-bottom: 12px; }
.card h2 { font-size: 15px; margin: 0 0 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.hero { display: flex; gap: 14px; align-items: center; }
.hero .avatar { width: 72px; height: 72px; font-size: 24px; }
.hero h2 { text-transform: none; letter-spacing: 0; color: var(--ink); font-size: 22px; margin: 0; }
.alert { border-radius: 12px; padding: 12px 14px; margin-bottom: 12px; font-weight: 600; }
.alert.red { background: var(--red-bg); color: var(--red); }
.alert.blue { background: var(--blue-bg); color: var(--blue); }
.crit { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.crit .card { margin: 0; }
.crit .big { font-size: 28px; font-weight: 800; }
dl { margin: 0; }
dl div { display: grid; grid-template-columns: 40% 60%; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); }
dl div:last-child { border-bottom: 0; }
dt { color: var(--muted); font-size: 14px; }
dd { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
dd a { text-decoration: none; }
.empty { color: var(--muted); font-style: italic; }

.photos { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.photos figure { margin: 0; position: relative; aspect-ratio: 1; background: var(--grey-bg); border-radius: 10px; overflow: hidden; cursor: pointer; }
.photos img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photos figcaption { position: absolute; left: 0; right: 0; bottom: 0; font-size: 11px; color: #fff;
  background: linear-gradient(transparent, rgba(0,0,0,.7)); padding: 14px 6px 4px; }
.addphoto { display: grid; place-items: center; aspect-ratio: 1; border: 2px dashed var(--line); border-radius: 10px;
  color: var(--muted); cursor: pointer; font-size: 14px; text-align: center; }

.logitem { padding: 10px 0; border-bottom: 1px solid var(--line); }
.logitem:last-child { border-bottom: 0; }
.logitem .meta { font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; gap: 8px; }
.logitem p { margin: 4px 0 0; white-space: pre-wrap; }
.linkbtn { background: none; border: 0; color: var(--red); padding: 0; font-size: 12px; cursor: pointer; }

details summary { cursor: pointer; color: var(--muted); padding: 4px 0; }
.hist { font-size: 13px; }
.hist div { padding: 6px 0; border-bottom: 1px solid var(--line); overflow-wrap: anywhere; }

/* viewer */
.viewer { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.92); display: flex; flex-direction: column; }
.viewer img { flex: 1; min-height: 0; object-fit: contain; width: 100%; }
.viewer .vbar { display: flex; gap: 8px; padding: calc(env(safe-area-inset-top) + 8px) 10px 8px; color: #fff; align-items: center; }
.viewer .vbar span { flex: 1; }
.viewer button { background: rgba(255,255,255,.15); color: #fff; border: 0; border-radius: 10px; padding: 10px 14px; }

/* fab and toast */
.fab { position: fixed; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom)); z-index: 20; width: 58px; height: 58px;
  border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); font-size: 30px; box-shadow: 0 4px 12px rgba(0,0,0,.25); cursor: pointer; }
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(90px + env(safe-area-inset-bottom)); z-index: 60;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; max-width: 90vw; }
.savebar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; background: var(--card); border-top: 1px solid var(--line);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); display: flex; gap: 8px; }
.savebar .btn { flex: 1; }
.menu a, .menu button { display: block; width: 100%; text-align: left; padding: 14px; border: 0; border-bottom: 1px solid var(--line);
  background: none; text-decoration: none; color: var(--ink); cursor: pointer; font-size: 16px; }
.menu > :last-child { border-bottom: 0; }
table.map { width: 100%; border-collapse: collapse; font-size: 14px; }
table.map td { padding: 6px 4px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.map td:first-child { width: 42%; overflow-wrap: anywhere; }
.bloodgrid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 12px; }
.bloodgrid .card { margin: 0; text-align: center; padding: 10px 4px; }
.bloodgrid b { display: block; font-size: 20px; }
@media print { .bar, .fab, .savebar, .noprint { display: none !important; } .card { box-shadow: none; border: 1px solid #ccc; } }
.logdate { flex: 0 0 170px; }
label.f.gap { margin-top: 8px; }
.card.menu { padding: 0; }
.spacer { height: 70px; }
.alert.amber { background: var(--amber-bg); color: var(--amber); font-weight: 500; white-space: pre-wrap; }
.alert .small { font-weight: 600; }
.avatarbtn { display: flex; flex-direction: column; align-items: center; gap: 2px; cursor: pointer; flex: none; }
.phead { margin: 12px 0 6px; }
.photos.private img { filter: blur(10px); }
