/* VGHTransplantDB design system (docs/03 §3): tokens, components, the FileMaker-position canvas and the phone flow. */

:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --border: #d5dbe3;
  --border-strong: #b7c0cc;
  --text: #17202b;
  --text-2: #4d5968;
  --muted: #7a8696;
  --accent: #1f5fa8;
  --accent-2: #0f7a6b;
  --accent-soft: #e3edf9;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --ok: #157f3c;
  --warn: #9a6700;
  --warn-soft: #fff4d6;
  --focus: #2f80ed;
  --input-bg: #ffffff;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 10%);
  --radius: 6px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, monospace;
  --fs: 13px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f141a; --surface: #161c24; --surface-2: #1d2530; --border: #2a3441; --border-strong: #3a4656;
    --text: #e6ebf1; --text-2: #b3bdc9; --muted: #8592a3; --accent: #6ea8ff; --accent-2: #3cc4ae;
    --accent-soft: #1b2a3f; --danger: #ff7b72; --danger-soft: #3a1d1d; --ok: #4ac26b; --warn: #e3b341;
    --warn-soft: #3a2f12; --focus: #6ea8ff; --input-bg: #10161d; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f141a; --surface: #161c24; --surface-2: #1d2530; --border: #2a3441; --border-strong: #3a4656;
  --text: #e6ebf1; --text-2: #b3bdc9; --muted: #8592a3; --accent: #6ea8ff; --accent-2: #3cc4ae;
  --accent-soft: #1b2a3f; --danger: #ff7b72; --danger-soft: #3a1d1d; --ok: #4ac26b; --warn: #e3b341;
  --warn-soft: #3a2f12; --focus: #6ea8ff; --input-bg: #10161d; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font: var(--fs)/1.35 var(--font); -webkit-text-size-adjust: 100%; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }

/* ---------- top bar ---------- */
.topbar { position: sticky; top: 0; z-index: 50; display: flex; align-items: center; gap: 12px; padding: 8px 16px;
  background: var(--surface); border-bottom: 1px solid var(--border); }
.brand { font-weight: 650; letter-spacing: .2px; color: var(--text); text-decoration: none; white-space: nowrap; }
.brand span { color: var(--accent); }
.env { font-size: 11px; padding: 2px 6px; border-radius: 99px; background: var(--warn-soft); color: var(--warn); }
.topbar form.search { flex: 1; max-width: 420px; }
.topbar .spacer { flex: 1; }
.topbar nav { display: flex; gap: 10px; align-items: center; white-space: nowrap; }
.topbar nav a { color: var(--text-2); text-decoration: none; }
.topbar nav a:hover { color: var(--text); }

/* ---------- controls ---------- */
input, select, textarea, button { font: inherit; color: inherit; }
.in, .screen input[type=text], .screen input[type=date], .screen input[type=time], .screen input[type=datetime-local],
.screen select, .screen textarea, .form input, .form select, .form textarea {
  width: 100%; height: 100%; min-height: 20px; padding: 2px 6px; border: 1px solid var(--border-strong);
  border-radius: 4px; background: var(--input-bg); }
.screen textarea, .form textarea { resize: vertical; line-height: 1.3; }
.in:disabled, .screen [disabled], .screen [readonly] { background: var(--surface-2); color: var(--text-2); }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 4px 12px; min-height: 26px;
  border: 1px solid var(--border-strong); border-radius: var(--radius); background: var(--surface); cursor: pointer;
  text-decoration: none; color: var(--text); white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.quiet { border-color: transparent; background: transparent; }
.badge { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 11px; font-weight: 600;
  background: var(--surface-2); color: var(--text-2); }
.badge.final { background: var(--accent-soft); color: var(--accent); }
.badge.draft { background: var(--warn-soft); color: var(--warn); }
.badge.deleted { background: var(--danger-soft); color: var(--danger); }

/* save state on a field */
.saving { box-shadow: 0 0 0 2px var(--accent-soft); }
.saved { box-shadow: 0 0 0 2px color-mix(in srgb, var(--ok) 35%, transparent); transition: box-shadow 1s; }
.error-field { border-color: var(--danger) !important; box-shadow: 0 0 0 2px var(--danger-soft); }

/* ---------- page ---------- */
main { padding: 16px; }
.page-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin: 0 0 12px; }
.page-head h1 { font-size: 18px; margin: 0; font-weight: 650; }
.page-head .meta { color: var(--muted); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow); }
.messages { list-style: none; margin: 0 0 12px; padding: 0; }
.messages li { padding: 8px 12px; border-radius: var(--radius); background: var(--accent-soft); margin-bottom: 6px; }
.messages li.error { background: var(--danger-soft); color: var(--danger); }
.toast { position: fixed; right: 16px; bottom: 16px; z-index: 100; padding: 10px 14px; border-radius: var(--radius);
  background: var(--text); color: var(--bg); box-shadow: var(--shadow); opacity: 0; transition: opacity .2s; pointer-events: none; }
.toast.show { opacity: 1; }
.toast.error { background: var(--danger); color: #fff; }

/* ---------- the screen canvas (desktop: FileMaker positions) ---------- */
.screen-wrap { overflow: auto; }
.screen { position: relative; background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow); transform-origin: 0 0; }
.screen .o { position: absolute; }
.screen .label { color: var(--text-2); font-size: 12px; white-space: pre-line; overflow: hidden; line-height: 1.15;
  display: flex; align-items: center; }
.screen .box { border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); }
.screen .box.title { background: var(--accent-soft); border-color: transparent; }
.screen .line { border-top: 1px solid var(--border); }
.screen .btn { width: 100%; height: 100%; padding: 2px 6px; min-height: 0; font-size: 12px; }
.screen .btn.head { background: var(--surface-2); border-color: var(--border); color: var(--text-2); cursor: default;
  font-weight: 600; justify-content: flex-start; border-radius: 0; }
.screen .btn.accent1 { background: var(--accent-2); border-color: var(--accent-2); color: #fff; cursor: default; }
.screen .btn.accent2 { background: var(--accent); border-color: var(--accent); color: #fff; }
.screen .btn.stat { flex-direction: column; font-weight: 650; cursor: default; background: var(--surface-2); }
.screen .value { display: flex; align-items: center; width: 100%; height: 100%; padding: 2px 6px; border: 1px solid transparent;
  border-radius: 4px; background: var(--surface-2); color: var(--text); overflow: hidden; white-space: nowrap;
  text-overflow: ellipsis; }
.screen .value.multi { white-space: pre-wrap; align-items: flex-start; overflow: auto; }
.screen .choices { display: flex; flex-wrap: wrap; gap: 2px 10px; align-content: flex-start; height: 100%;
  overflow: auto; padding: 1px 2px; }
.screen .choices label { display: inline-flex; gap: 4px; align-items: center; white-space: nowrap; font-size: 12px; }
.screen .unknown { background: repeating-linear-gradient(45deg, transparent 0 6px, var(--surface-2) 6px 12px);
  border: 1px dashed var(--border); border-radius: 4px; }

.flowlabel { display: none; }
.screen .label.right { justify-content: flex-end; text-align: right; white-space: nowrap; overflow: visible; padding-right: 4px; }

/* tabs */
.screen .tabs { display: flex; flex-direction: column; }
.tabbar { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: thin; flex: none; }
.tabbar button { border: 1px solid var(--border); border-bottom: none; background: var(--surface-2); color: var(--text-2);
  padding: 0 12px; border-radius: 6px 6px 0 0; cursor: pointer; white-space: nowrap; font-size: 12px; }
.tabbar button[aria-selected=true] { background: var(--surface); color: var(--text); font-weight: 600;
  box-shadow: inset 0 2px 0 var(--accent); }
.tabpanel { position: relative; flex: 1; border: 1px solid var(--border); border-radius: 0 6px 6px 6px;
  background: var(--surface); }
.tabpanel[hidden] { display: none; }
.tabinner { position: absolute; left: 0; right: 0; height: 0; } /* FileMaker places tab contents from the tab control's top */

/* portals */
.portal { border: 1px solid var(--border); border-radius: 6px; overflow-y: auto; overflow-x: hidden; background: var(--surface); }
.portal .row { position: relative; border-bottom: 1px solid var(--border); }
.portal .row:nth-child(even) { background: color-mix(in srgb, var(--surface-2) 55%, transparent); }
.portal .row.link { cursor: pointer; }
.portal .row.link:hover { background: var(--accent-soft); }
.portal .empty { padding: 8px; color: var(--muted); }
.portal .add { position: sticky; bottom: 0; display: flex; justify-content: flex-end; padding: 3px; background: var(--surface); }

/* popovers */
.popover { display: none; position: absolute; z-index: 30; min-width: 380px; min-height: 120px; background: var(--surface);
  border: 1px solid var(--border-strong); border-radius: 8px; box-shadow: 0 8px 24px rgb(0 0 0 / 18%); }
.popover.open { display: block; }

/* menus (Copy Text, New visit) */
.menu { position: relative; display: inline-block; }
.menu .items { display: none; position: absolute; bottom: 100%; left: 0; z-index: 40; min-width: 180px; padding: 4px;
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: 8px; box-shadow: var(--shadow); }
.menu.open .items { display: block; }
.menu .items button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 6px 10px;
  border-radius: 4px; cursor: pointer; }
.menu .items button:hover { background: var(--accent-soft); }

/* ---------- tables and forms (find, lists, admin-like pages) ---------- */
table.list { width: 100%; border-collapse: collapse; background: var(--surface); }
table.list th, table.list td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--border); }
table.list th { color: var(--muted); font-weight: 600; font-size: 12px; background: var(--surface-2); }
table.list tr.link { cursor: pointer; }
table.list tr.link:hover td { background: var(--accent-soft); }
.form { display: grid; grid-template-columns: max-content minmax(0, 360px); gap: 8px 12px; align-items: center; }
.form label { color: var(--text-2); }
dialog { border: 1px solid var(--border-strong); border-radius: 10px; padding: 16px; background: var(--surface);
  color: var(--text); box-shadow: 0 10px 40px rgb(0 0 0 / 25%); max-width: min(760px, 94vw); }
dialog::backdrop { background: rgb(0 0 0 / 35%); }

/* ---------- auth pages ---------- */
.auth { max-width: 400px; margin: 8vh auto; padding: 28px; }
.auth h1 { font-size: 20px; margin-top: 0; }
.auth input:not([type=checkbox]) { width: 100%; padding: 8px 10px; border: 1px solid var(--border-strong);
  border-radius: var(--radius); background: var(--input-bg); }
.auth p { margin: 10px 0; }
.auth button { width: 100%; padding: 9px; }

/* ---------- phone and narrow tablet: the same objects, in reading order ---------- */
@media (max-width: 900px) {
  body { --fs: 15px; }
  main { padding: 10px; }
  .topbar { flex-wrap: wrap; padding: 8px 10px; }
  .topbar form.search { order: 3; max-width: none; flex-basis: 100%; }
  .screen-wrap { overflow: visible; }
  .screen { width: auto !important; height: auto !important; transform: none !important; padding: 10px; }
  .screen .o { position: static; width: auto !important; height: auto !important; margin: 0 0 6px; }
  .screen .o.label { margin: 10px 0 2px; font-weight: 600; }
  .screen .o.label.paired { display: none; }
  .flowlabel { display: block; margin: 8px 0 3px; font-weight: 600; color: var(--text-2); font-size: 13px; }
  .topbar nav { overflow-x: auto; max-width: 100%; gap: 14px; scrollbar-width: none; }
  body { overflow-x: hidden; }
  .screen .o.box, .screen .o.line, .screen .o.decor { display: none; }
  .screen input[type=text], .screen input[type=date], .screen input[type=time], .screen select { min-height: 40px; }
  .screen textarea { min-height: 96px; }
  .screen .btn { min-height: 40px; font-size: 14px; }
  .screen .tabs { display: block; }
  .tabbar { position: sticky; top: 92px; z-index: 5; background: var(--surface); padding-top: 4px; }
  .tabbar button { min-height: 36px; font-size: 14px; }
  .tabpanel { padding: 8px; }
  .tabinner { position: static; height: auto; }
  .portal { max-height: none !important; }
  .portal .row { padding: 8px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 8px; }
  .screen .choices { gap: 6px 14px; }
  .screen .choices label { font-size: 15px; min-height: 32px; }
  .popover { position: fixed; inset: auto 8px 8px 8px; min-width: 0; }
  .menu .items { bottom: auto; top: 100%; }
  .form { grid-template-columns: 1fr; }
  table.list .hide-sm { display: none; }
}
@media print {
  .topbar, .footer-actions { display: none; }
  body { background: #fff; }
}
