:root {
  --bg: #f5f7fa; --surface: #fff; --text: #1d2433; --muted: #66708a; --border: #e2e6ee;
  --accent: #2f6fed; --accent-soft: #eaf1ff; --good: #1d8a4e; --warn: #b26a00; --bad: #c43a3a;
  --radius: 10px; --shadow: 0 1px 2px rgba(20, 30, 50, .06), 0 2px 8px rgba(20, 30, 50, .04);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; font-size: 14px; color: var(--text);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
[hidden] { display: none !important; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 16px; margin: 0 0 4px; }
h3 { font-size: 14px; margin: 12px 0 4px; }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.muted { color: var(--muted); margin: 0; }
.small-text { font-size: 12px; }
.error { color: var(--bad); margin: 0; min-height: 1em; }

.topbar { display: flex; justify-content: space-between; align-items: center; padding: 10px 24px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 2; }
.brand { font-weight: 700; color: var(--text); font-size: 16px; margin-right: 12px; }
.mainnav { display: flex; gap: 16px; align-items: baseline; }
.mainnav a:not(.brand) { color: var(--muted); font-weight: 600; }
.mainnav a.active { color: var(--accent); }
button.danger { color: var(--bad); border-color: #f0c9c9; }
.badge.bad { background: #fbe9e9; color: var(--bad); }
.brand span { color: var(--accent); font-weight: 600; }
.who { display: flex; gap: 12px; align-items: center; color: var(--muted); }

main { max-width: 1200px; margin: 0 auto; padding: 24px; }
.view { display: flex; flex-direction: column; gap: 16px; }
.center { align-items: center; padding-top: 10vh; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.narrow { width: 100%; max-width: 380px; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 860px) { .grid2 { grid-template-columns: 1fr; } }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; }
.crumbs { color: var(--muted); }
.crumbs span { margin: 0 4px; }

label { display: flex; flex-direction: column; gap: 4px; font-weight: 600; font-size: 13px; }
input { font: inherit; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: #fff; font-weight: 400; min-width: 220px; }
input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.inline-form { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.inline-form .error { flex-basis: 100%; }
label.check { flex-direction: row; align-items: center; gap: 6px; padding-bottom: 8px; }
label.check input { min-width: 0; }
.search { display: flex; gap: 8px; }
.search input { min-width: 280px; }

button { font: inherit; cursor: pointer; border-radius: 8px; padding: 8px 14px; border: 1px solid var(--border); background: var(--surface); color: var(--text); }
button:disabled { opacity: .55; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.ghost { background: transparent; }
button.small { padding: 4px 10px; font-size: 13px; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
td.num, th.num { text-align: right; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--accent-soft); }
tr.selected td { background: var(--accent-soft); }
.table-card { overflow-x: auto; }
.sub { display: block; color: var(--muted); font-size: 12px; }
.empty { color: var(--muted); text-align: center; padding: 20px; margin: 0; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: #eef0f4; color: var(--muted); }
.badge.good { background: #e3f5ea; color: var(--good); }

.key-result { border: 1px dashed var(--accent); background: var(--accent-soft); border-radius: 8px; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.kv { display: grid; grid-template-columns: 140px 1fr auto; gap: 8px; align-items: center; }
.kv span { font-weight: 600; font-size: 13px; }
.kv code { background: #fff; border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; word-break: break-all; user-select: all; }

.pager { display: flex; justify-content: space-between; align-items: center; padding-top: 10px; }
.patients-layout { display: grid; grid-template-columns: 1fr; gap: 16px; align-items: start; }
.patients-layout:has(.detail:not([hidden])) { grid-template-columns: minmax(0, 1fr) 340px; }
@media (max-width: 1000px) { .patients-layout:has(.detail:not([hidden])) { grid-template-columns: 1fr; } }
.detail-head { display: flex; justify-content: space-between; align-items: center; }
.facts { display: grid; grid-template-columns: 110px 1fr; gap: 6px 10px; margin: 0; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; word-break: break-word; }
.studies { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.studies li { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: #fff; padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow); }

.study-head, .file-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.files { display: flex; flex-direction: column; gap: 8px; margin-top: 6px; }
.files:empty { display: none; }
.file { border-top: 1px dashed var(--border); padding-top: 6px; display: flex; flex-direction: column; gap: 4px; }
.file-name { font-size: 13px; word-break: break-all; }
.file-view { display: flex; flex-direction: column; gap: 4px; }
.file-view:empty { display: none; }
img.thumb { max-width: 100%; max-height: 240px; object-fit: contain; background: #111; border-radius: 6px; cursor: zoom-in; }
.viewer { position: fixed; inset: 0; z-index: 10; background: rgba(10, 14, 22, .92); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; padding: 16px; }
.viewer-bar { display: flex; gap: 16px; align-items: center; color: #fff; max-width: 100%; }
.viewer-bar span { word-break: break-all; }
.viewer-bar a { color: #9cc0ff; }
.viewer-bar button { color: #fff; border-color: rgba(255, 255, 255, .3); }
.viewer img { max-width: 100%; max-height: calc(100vh - 80px); object-fit: contain; }
.study-row { display: flex; gap: 10px; align-items: center; }
.study-text { flex: 1; min-width: 0; }
.study-thumb { width: 72px; height: 48px; flex: none; object-fit: cover; border-radius: 6px; background: #111; }
.study-thumb.clickable { cursor: pointer; }
.empty-thumb { display: flex; align-items: center; justify-content: center; background: #eef0f4; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .04em; }
