:root {
  --plum: #511d47;
  --plum-2: #6b2a5f;
  --accent: #cd3a90;
  --ink: #1f1620;
  --muted: #6f6270;
  --line: #ece4ea;
  --bg: #f7f3f6;
  --card: #ffffff;
  --chip: #ede6f2;
  --chip-ink: #54436a;
  --green: #1f8a5b;
  --amber: #b7791f;
  --red: #c0392b;
  --teal: #13857e;
  --blue: #2f6fb3;
  --violet: #6b4fbb;
  --pink: #cd3a90;
  --grey: #7b6f7c;
  --radius: 14px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--bg); }
body { min-height: 100vh; padding-bottom: env(safe-area-inset-bottom); -webkit-tap-highlight-color: transparent; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--plum); }

.top {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px; background: var(--plum); color: #fff;
}
.top .brand { height: 24px; width: auto; }
.top-title { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.15; }
.top-title strong { font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-title span { font-size: 12.5px; opacity: .8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { background: none; border: 0; color: #fff; padding: 6px; border-radius: 10px; display: grid; place-items: center; }
.icon-btn:active { background: rgba(255,255,255,.12); }
.offline { background: #fff4d6; color: #6b4e00; padding: 8px 14px; font-size: 14px; text-align: center; }

main { max-width: 760px; margin: 0 auto; padding: 12px 12px 40px; }

.search { position: sticky; top: 0; display: flex; gap: 8px; margin-bottom: 10px; }
.search input {
  flex: 1; border: 1px solid var(--line); background: var(--card); border-radius: 12px; padding: 11px 14px; font-size: 16px; outline: none;
}
.search input:focus { border-color: var(--accent); }

.btn {
  border: 0; border-radius: 12px; padding: 12px 16px; font-weight: 650; font-size: 15.5px;
  background: var(--plum); color: #fff; display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.btn:disabled { opacity: .55; cursor: default; }
.btn.accent { background: var(--accent); }
.btn.ghost { background: transparent; color: var(--plum); border: 1.5px solid #d9c9d5; }
.btn.red { background: var(--red); color: #fff; }
.dialog.small { max-width: 420px; }
.overlay.confirm .dialog { height: auto; border-radius: 18px; }
.overlay.confirm { padding: 16px; z-index: 70; }
.btn.danger { background: transparent; color: var(--red); border: 1.5px solid #efc9c4; }
.btn.small { padding: 8px 12px; font-size: 14px; border-radius: 10px; }
.btn.block { width: 100%; }

.list { background: var(--card); border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); }
.row { display: flex; gap: 12px; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--line); background: none; border-left: 0; border-right: 0; border-top: 0; width: 100%; text-align: left; }
.row:last-child { border-bottom: 0; }
.row:active { background: #faf6f9; }
.avatar { flex: none; width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; color: #fff; background: var(--plum-2); font-size: 16px; }
.row-main { flex: 1; min-width: 0; }
.row-top { display: flex; align-items: baseline; gap: 8px; }
.row-name { font-weight: 650; font-size: 16px; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-date { font-size: 12.5px; color: var(--muted); flex: none; }
.row-sub { font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.tag { display: inline-block; font-size: 11px; font-weight: 700; letter-spacing: .03em; padding: 1px 6px; border-radius: 6px; background: #f3e8f0; color: var(--plum); margin-left: 6px; vertical-align: 1px; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
.card h3 { margin: 0 0 8px; font-size: 13px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); font-weight: 700; display: flex; align-items: center; justify-content: space-between; }
.patient-head { display: flex; gap: 12px; align-items: center; margin-bottom: 12px; }
.patient-head .avatar { width: 52px; height: 52px; font-size: 19px; }
.patient-head h2 { margin: 0; font-size: 20px; }
.patient-head p { margin: 2px 0 0; color: var(--muted); font-size: 14px; }

.summary-line { display: flex; gap: 10px; padding: 9px 0; border-top: 1px solid var(--line); }
.summary-line:first-of-type { border-top: 0; padding-top: 2px; }
.dot { flex: none; width: 9px; height: 9px; border-radius: 50%; margin-top: 6px; background: var(--grey); }
.dot.alert { background: var(--red); }
.dot.due { background: var(--amber); }
.dot.ok { background: var(--green); }
.dot.info { background: var(--blue); }
.summary-text { font-size: 15px; line-height: 1.4; }
.summary-why { font-size: 13px; color: var(--muted); margin-top: 2px; line-height: 1.35; }

textarea.note {
  width: 100%; min-height: 200px; border: 1px solid var(--line); border-radius: 12px; padding: 12px; font-size: 16px; line-height: 1.45;
  resize: vertical; outline: none; background: #fffefe; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
textarea.note:focus { border-color: var(--accent); }
.note-actions { display: flex; gap: 8px; align-items: center; margin-top: 10px; }
.note-actions .grow { flex: 1; font-size: 12.5px; color: var(--muted); }
.notice { font-size: 13px; color: var(--muted); line-height: 1.4; margin-top: 10px; }
.error { background: #fdecea; color: #8a2c22; border-radius: 10px; padding: 10px 12px; font-size: 14px; margin-top: 10px; line-height: 1.4; }

.chips { display: flex; gap: 6px; overflow-x: auto; padding: 2px 0 10px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip-btn { flex: none; border: 1px solid #dccfd8; background: var(--card); border-radius: 20px; padding: 6px 12px; font-size: 14px; color: var(--plum); }
.chip-btn.on { background: var(--plum); color: #fff; border-color: var(--plum); }

.enc { margin-bottom: 14px; }
.enc-head { display: flex; justify-content: space-between; align-items: baseline; margin: 0 2px 6px; }
.enc-head strong { font-size: 15px; }
.enc-head span { font-size: 13px; color: var(--muted); }
.tl-item { display: flex; gap: 10px; padding: 9px 12px; border-bottom: 1px solid var(--line); align-items: flex-start; }
.tl-item:last-child { border-bottom: 0; }
.tl-kind { flex: none; font-size: 11.5px; font-weight: 700; padding: 3px 7px; border-radius: 6px; min-width: 86px; text-align: center; }
.tl-body { flex: 1; min-width: 0; font-size: 14.5px; line-height: 1.4; }
.tl-body .sub { color: var(--muted); font-size: 13px; }
.tl-note { white-space: pre-wrap; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; background: #faf7f9; border-radius: 8px; padding: 8px 10px; margin-top: 4px; }

.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11.5px; font-weight: 700; color: var(--chip-ink); background: var(--chip); border-radius: 5px; padding: 1px 6px; margin-left: 6px; white-space: nowrap; display: inline-block; }
.struck { text-decoration: line-through; text-decoration-thickness: 2px; color: #8b8090; }

/* preview dialog */
.overlay { position: fixed; inset: 0; z-index: 50; background: rgba(25, 12, 22, .5); display: flex; align-items: center; justify-content: center; padding: 16px; }
.dialog { background: var(--card); width: 100%; max-width: 760px; max-height: calc(100vh - 32px); border-radius: 18px; display: flex; flex-direction: column; overflow: hidden; }
.dialog-head { padding: 14px 16px; background: #f6eef4; border-bottom: 1px solid #ead7e4; display: flex; align-items: center; gap: 10px; }
.dialog-head strong { flex: 1; color: var(--plum); font-size: 17px; }
.dialog-head .systems { font-family: ui-monospace, monospace; font-size: 11px; font-weight: 700; color: #7a6875; }
.dialog-body { overflow-y: auto; padding: 4px 0 8px; flex: 1; }
.dialog-foot { padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); display: flex; gap: 8px; flex-wrap: wrap; }
.dialog-foot .btn { flex: 1; min-width: 100px; }
.group { display: flex; gap: 12px; padding: 10px 16px; border-bottom: 1px solid #f0e9ee; }
.group-label { flex: none; width: 104px; }
.group-label span { display: block; text-align: center; font-family: ui-monospace, monospace; font-size: 12px; font-weight: 700; padding: 5px 0; border-radius: 8px; }
.group-items { flex: 1; min-width: 0; }
.p-item { padding: 3px 0 7px; font-size: 15.5px; line-height: 1.4; }
.p-src { font-size: 12.5px; color: var(--muted); margin-top: 1px; }
.p-src q { font-style: italic; }
.skeleton { height: 20px; border-radius: 8px; margin: 22px 18px; background: #ece5ea; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .45; } }
.reading { text-align: center; color: var(--plum); font-weight: 650; padding: 18px 0 0; }
.dialog-note { margin: 10px 16px 6px; }
.dialog-note summary { font-size: 13px; color: var(--muted); cursor: pointer; }

.g-history .group-label span { background: #13857e1f; color: var(--teal); }
.g-allergy .group-label span { background: #c0392b1f; color: var(--red); }
.g-examination .group-label span { background: #2f6fb31f; color: var(--blue); }
.g-vitals .group-label span { background: #6b4fbb1f; color: var(--violet); }
.g-investigation .group-label span { background: #0e74901f; color: #0e7490; }
.g-diagnosis .group-label span { background: #cd3a901f; color: var(--pink); }
.g-medication .group-label span { background: #b7791f1f; color: var(--amber); }
.g-plan .group-label span { background: #1f8a5b1f; color: var(--green); }
.g-other .group-label span { background: #7b6f7c1f; color: var(--grey); }

.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px); opacity: 0; transition: .25s; background: #1f1620; color: #fff; padding: 11px 16px; border-radius: 12px; font-size: 14.5px; z-index: 60; pointer-events: none; max-width: calc(100vw - 32px); }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.ok { background: var(--green); }

.field { display: block; margin-bottom: 12px; }
.field span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.field input, .field select { width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; font-size: 16px; background: #fff; }
.seg { display: flex; gap: 6px; }
.seg button { flex: 1; border: 1px solid #dccfd8; background: #fff; border-radius: 10px; padding: 10px; }
.seg button.on { background: var(--plum); color: #fff; border-color: var(--plum); }
.muted { color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }
.fab-row { display: flex; gap: 8px; margin: 12px 0; }
.foot { text-align: center; font-size: 12.5px; color: var(--muted); margin-top: 18px; line-height: 1.45; }
.keybox { font-family: ui-monospace, monospace; font-size: 13px; background: #faf7f9; border: 1px dashed #d9c9d5; padding: 8px 10px; border-radius: 8px; word-break: break-all; }

@media (max-width: 640px) {
  .overlay { padding: 0; }
  .dialog { max-height: 100vh; height: 100%; border-radius: 0; }
  .group { flex-direction: column; gap: 6px; }
  .group-label { width: auto; }
  .group-label span { display: inline-block; padding: 4px 10px; }
  .tl-kind { min-width: 72px; }
}
