: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 200px 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; }
/* The user box stays fixed in the top-right corner, above the page as it scrolls. */
.who { position: fixed; top: 8px; right: 24px; z-index: 6; display: flex; gap: 12px; align-items: center; color: var(--muted); background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 6px 12px; max-width: 46vw; }
.who #who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 640px) { .topbar { padding-right: 24px; } .who { top: 6px; right: 12px; max-width: 60vw; } }

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, select { font: inherit; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: #fff; font-weight: 400; min-width: 220px; }
input:focus, select: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; }
.slice-controls { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.slice-index { width: 72px; padding: 3px 6px; font-size: 13px; }
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; }
.pms-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 6px; margin-top: 6px; }
.pms-tile { aspect-ratio: 4 / 3; padding: 0; border-radius: 6px; background: #111; color: #ccd; font-size: 11px; overflow: hidden; cursor: zoom-in; }
.pms-tile img { width: 100%; height: 100%; object-fit: cover; display: block; }
.pms-tile.loading { opacity: .5; cursor: progress; }
.tabs { display: flex; flex-wrap: wrap; gap: 4px; margin: 4px 0 10px; border-bottom: 1px solid var(--border); padding-bottom: 6px; }
.tab { padding: 4px 10px; font-size: 13px; border-radius: 6px; border: 1px solid transparent; background: none; color: var(--muted); }
.tab[aria-selected="true"] { border-color: var(--border); background: var(--bg); color: var(--text); font-weight: 600; }
.tab-body { display: flex; flex-direction: column; gap: 8px; }
table.mini { font-size: 12px; margin-top: 6px; }
table.mini th, table.mini td { padding: 4px 6px; }
.note-text { white-space: pre-wrap; margin: 4px 0 0; font-size: 13px; }
.table-wrap { overflow-x: auto; }
table.mini td:last-child, table.mini th:last-child { text-align: right; white-space: nowrap; }
.kept-bar { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.section-head { margin: 10px 0 0; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; color: var(--muted, #6b7280); }
.doc-color { width: 12px; height: 12px; border-radius: 3px; flex: none; border: 1px solid rgba(0,0,0,.15); }
.pd-photo { width: 72px; height: 72px; border-radius: 8px; object-fit: cover; border: 1px solid var(--border, #e5e7eb); background: #f3f4f6; }
.detail-head-main { display: flex; align-items: center; gap: 12px; min-width: 0; }
.detail-head-main h2 { margin: 0; min-width: 0; }

/* Workspace tags: chips + the tag/assignment modals */
.head-actions { display: flex; gap: 10px; align-items: center; }
.chiplist { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.chip { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--accent-soft); color: var(--accent); white-space: nowrap; }
.chip.muted-chip { background: #eef0f4; color: var(--muted); font-weight: 500; }
.modal { position: fixed; inset: 0; z-index: 20; background: rgba(10, 14, 22, .45); display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; overflow: auto; }
.modal-box { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); width: 100%; max-width: 560px; padding: 18px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 10px; }
.modal-head h2 { margin: 0; }
.pick { display: flex; flex-direction: column; gap: 2px; max-height: 320px; overflow: auto; border: 1px solid var(--border); border-radius: 8px; padding: 6px; }
.pick label { flex-direction: row; align-items: center; gap: 8px; padding: 5px 6px; border-radius: 6px; cursor: pointer; }
.pick label:hover { background: var(--accent-soft); }
.pick label input { min-width: 0; }
.pick .pick-sub { color: var(--muted); font-size: 12px; }
.tag-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.tag-row:last-child { border-bottom: 0; }
.tag-row .grow { flex: 1; min-width: 0; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 14px; }

/* "Act as user": sticky banner + hiding admin-only chrome while acting */
.acting-bar { position: sticky; top: 0; z-index: 50; display: flex; align-items: center; gap: 10px;
  padding: 8px 20px; background: #7c2d12; color: #fff; font-size: 14px; box-shadow: 0 1px 4px rgba(0,0,0,.25); }
.acting-bar .acting-text { flex: 1; min-width: 0; }
.acting-bar .acting-meta { opacity: .85; }
.acting-bar .acting-dot { width: 9px; height: 9px; border-radius: 50%; background: #fbbf24; flex: none;
  box-shadow: 0 0 0 0 rgba(251,191,36,.7); animation: acting-pulse 1.8s infinite; }
.acting-bar button.ghost { color: #fff; border-color: rgba(255,255,255,.5); }
.acting-bar button.ghost:hover { background: rgba(255,255,255,.12); }
@keyframes acting-pulse { 0% { box-shadow: 0 0 0 0 rgba(251,191,36,.6); } 70% { box-shadow: 0 0 0 7px rgba(251,191,36,0); } 100% { box-shadow: 0 0 0 0 rgba(251,191,36,0); } }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
body.acting .nav-admin { display: none; }
@media (prefers-color-scheme: dark) { .acting-bar { background: #9a3412; } }

/* API console */
.console-line { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.console-line #con-path { flex: 1; min-width: 220px; font-family: ui-monospace, Menlo, Consolas, monospace; }
.console-line #con-method { min-width: 90px; }
.con-field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted, #667); }
.con-field select, .con-field input, #con-body { font-size: 14px; }
#con-body { font-family: ui-monospace, Menlo, Consolas, monospace; width: 100%; resize: vertical; }
.console-status { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.console-pre { background: #0b1020; color: #d6e2ff; padding: 12px 14px; border-radius: 8px; overflow: auto; max-height: 460px;
  font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12.5px; line-height: 1.5; white-space: pre-wrap; word-break: break-word; }
#con-headers { max-height: 160px; opacity: .85; margin-bottom: 8px; }
.badge.ok { background: #16a34a; color: #fff; } .badge.warn { background: #d97706; color: #fff; } .badge.err { background: #dc2626; color: #fff; }

/* API console: searchable param pickers (workspace / patient) */
.console-params { display: flex; gap: 12px; flex-wrap: wrap; }
.picker-field { position: relative; min-width: 260px; }
.picker { position: relative; }
.picker-input { width: 100%; }
.picker-list { position: absolute; z-index: 60; left: 0; right: 0; top: 100%; margin-top: 2px; max-height: 280px; overflow: auto;
  background: var(--card, #fff); border: 1px solid var(--line, #d7dbe6); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.14); }
.picker-item { display: flex; flex-direction: column; gap: 1px; padding: 7px 11px; cursor: pointer; font-size: 14px; }
.picker-item:hover { background: rgba(21,117,240,.10); }
.picker-item.muted { cursor: default; color: var(--muted, #889); }
.picker-item .sub { font-size: 12px; color: var(--muted, #889); }
@media (prefers-color-scheme: dark) { .picker-list { background: #161a24; border-color: #2a3040; } .picker-item:hover { background: rgba(21,117,240,.22); } }
.con-img { max-width: 320px; max-height: 320px; border-radius: 8px; display: block; margin-bottom: 8px; background: #fff; }
