:root {
  --brand: #8C1515;
  --brand-dark: #660a0a;
  --bg: #f4f5f7;
  --panel: #ffffff;
  --ink: #1d2330;
  --muted: #6b7280;
  --line: #e5e7eb;
  --green: #18794e;
  --green-bg: #e7f4ec;
  --amber: #9a6700;
  --amber-bg: #fff4d6;
  --grey-bg: #eef0f3;
  --blue: #1f6feb;
  --shadow: 0 1px 3px rgba(16,24,40,.08), 0 1px 2px rgba(16,24,40,.06);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--brand); text-decoration: none; }
button { font-family: inherit; cursor: pointer; }

/* ---------- Login ---------- */
.login-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #8C1515 0%, #4a0a0a 100%);
  padding: 24px;
}
.login-card {
  background: var(--panel); width: 100%; max-width: 400px; border-radius: 14px;
  padding: 36px 32px; box-shadow: 0 20px 50px rgba(0,0,0,.25);
}
.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 6px; }
.brand .dot {
  width: 38px; height: 38px; border-radius: 9px; background: var(--brand);
  display: grid; place-items: center; color: #fff; font-weight: 800; font-size: 18px;
}
.brand h1 { font-size: 19px; margin: 0; letter-spacing: -.2px; }
.login-card .sub { color: var(--muted); font-size: 13px; margin: 2px 0 22px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12.5px; font-weight: 600; color: #374151; margin-bottom: 6px; }
.field input {
  width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 9px;
  font-size: 14px; outline: none; transition: border .15s, box-shadow .15s;
}
.field input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(140,21,21,.12); }

.pw-wrap { position: relative; }
.pw-wrap input { padding-right: 60px; }
.pw-toggle {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  background: none; border: 0; color: var(--muted); font-size: 12px; font-weight: 600;
  padding: 6px 8px; border-radius: 6px;
}
.pw-toggle:hover { background: var(--grey-bg); color: #374151; }
.btn {
  width: 100%; padding: 12px; border: 0; border-radius: 9px; background: var(--brand);
  color: #fff; font-weight: 700; font-size: 14.5px; transition: background .15s;
}
.btn:hover { background: var(--brand-dark); }
.btn.secondary { background: #fff; color: var(--brand); border: 1px solid var(--brand); }
.btn.secondary:hover { background: #fbecec; }
.hint {
  margin-top: 18px; font-size: 12px; color: var(--muted); text-align: center;
  background: var(--grey-bg); padding: 9px; border-radius: 8px;
}
.error { background: #fdecec; color: #b42318; padding: 9px 12px; border-radius: 8px;
  font-size: 13px; margin-bottom: 14px; border: 1px solid #f7caca; }

/* ---------- App shell ---------- */
.topbar {
  background: var(--panel); border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 22px; height: 60px; position: sticky; top: 0; z-index: 20;
}
.topbar .left { display: flex; align-items: center; gap: 12px; }
.topbar .dot {
  width: 32px; height: 32px; border-radius: 8px; background: var(--brand);
  display: grid; place-items: center; color: #fff; font-weight: 800;
}
.topbar h1 { font-size: 16px; margin: 0; }
.topbar .right { display: flex; align-items: center; gap: 14px; }
.acct-chip {
  display: inline-flex; align-items: center; gap: 7px; background: var(--grey-bg);
  border: 1px solid var(--line); padding: 6px 11px; border-radius: 20px;
  font-size: 12.5px; color: #374151;
}
.acct-chip .gdot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); }
.logout { font-size: 13px; color: var(--muted); }

.container { max-width: 1120px; margin: 0 auto; padding: 26px 22px 60px; }

/* ---------- Page header ---------- */
.page-head { display: flex; align-items: flex-end; justify-content: space-between;
  margin-bottom: 20px; gap: 16px; flex-wrap: wrap; }
.page-title { font-size: 22px; margin: 0; letter-spacing: -.3px; }
.page-sub { margin: 4px 0 0; color: var(--muted); font-size: 13px; }

/* ---------- Search ---------- */
.search {
  width: 280px; max-width: 50vw; padding: 8px 12px; border: 1px solid var(--line);
  border-radius: 8px; font-size: 13px; outline: none; background: #fafbfc;
}
.search:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(140,21,21,.10); background: #fff; }

/* ---------- Enroll modal extras ---------- */
.assigned { margin-top: 4px; font-size: 12.5px; color: var(--muted);
  background: var(--grey-bg); border: 1px solid var(--line); padding: 9px 11px; border-radius: 8px; }
.assigned b { color: var(--ink); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.enroll-done { margin-top: 14px; font-size: 13px; color: var(--green); font-weight: 600; }

/* ---------- Provisioning console ---------- */
.modal.wide { max-width: 600px; transition: max-width .25s ease; }
.prov-bar { height: 3px; background: var(--line); border-radius: 2px; overflow: hidden; margin-top: 12px; }
.prov-bar > i { display: block; height: 100%; width: 0; background: var(--brand); transition: width .35s ease; }
.prov-steps { margin-bottom: 14px; }
.prov-steps .step { padding: 7px 0; }
.terminal {
  background: #0c1118; color: #cdd6e3; border-radius: 10px; padding: 12px 14px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px;
  line-height: 1.65; height: 188px; overflow-y: auto; border: 1px solid #1b2430;
}
.terminal .ln { white-space: pre-wrap; word-break: break-word; }
.terminal .ln.cmd { color: #8fb6ff; }
.terminal .ln.ok { color: #5bd49b; }
.terminal .ln.muted { color: #7b8794; }
.terminal::-webkit-scrollbar { width: 8px; }
.terminal::-webkit-scrollbar-thumb { background: #243042; border-radius: 4px; }

/* ---------- Stat cards ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-bottom: 22px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 16px 18px; box-shadow: var(--shadow); }
.stat .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat .value { font-size: 28px; font-weight: 800; margin-top: 6px; letter-spacing: -.5px; }
.stat .value small { font-size: 13px; font-weight: 600; color: var(--muted); }

/* ---------- Panel + table ---------- */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: var(--shadow); overflow: hidden; }
.panel-head { display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px; border-bottom: 1px solid var(--line); }
.panel-head h2 { font-size: 15px; margin: 0; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); padding: 11px 18px; background: #fafbfc; border-bottom: 1px solid var(--line); }
td { padding: 13px 18px; font-size: 13.5px; border-bottom: 1px solid var(--line); }
tr:last-child td { border-bottom: 0; }
tr.clickable:hover { background: #fafafa; cursor: pointer; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; color: #374151; }

.badge { display: inline-block; padding: 3px 9px; border-radius: 20px; font-size: 11.5px; font-weight: 700; }
.badge.connected { background: var(--green-bg); color: var(--green); }
.badge.completed { background: var(--grey-bg); color: var(--muted); }
.badge.pending_consent { background: var(--amber-bg); color: var(--amber); }
.badge.error { background: #fdecec; color: #b42318; }

/* ---------- Buttons ---------- */
.btn-inline {
  padding: 9px 15px; border: 0; border-radius: 8px; background: var(--brand);
  color: #fff; font-weight: 700; font-size: 13px;
}
.btn-inline:hover { background: var(--brand-dark); }
.btn-ghost { padding: 9px 15px; border: 1px solid var(--line); border-radius: 8px;
  background: #fff; font-weight: 600; font-size: 13px; color: #374151; }
.btn-ghost:hover { background: #f7f8fa; }

/* ---------- Modal ---------- */
.overlay { position: fixed; inset: 0; background: rgba(17,24,39,.55);
  display: none; align-items: center; justify-content: center; z-index: 50; padding: 20px; }
.overlay.show { display: flex; }
.modal { background: #fff; border-radius: 14px; width: 100%; max-width: 460px;
  box-shadow: 0 30px 60px rgba(0,0,0,.3); overflow: hidden; }
.modal-head { padding: 18px 22px; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-head p { margin: 4px 0 0; font-size: 12.5px; color: var(--muted); }
.modal-body { padding: 20px 22px; }
.modal-foot { padding: 14px 22px; border-top: 1px solid var(--line); display: flex; gap: 10px; justify-content: flex-end; }

.step { display: flex; align-items: center; gap: 12px; padding: 10px 0; color: var(--muted); font-size: 13.5px; }
.step .ic { width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--line);
  display: grid; place-items: center; font-size: 12px; flex-shrink: 0; }
.step.done { color: var(--ink); }
.step.done .ic { background: var(--green); border-color: var(--green); color: #fff; }
.step.run .ic { border-color: var(--brand); color: var(--brand); animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Patient page ---------- */
.back { font-size: 13px; color: var(--muted); display: inline-flex; gap: 6px; margin-bottom: 14px; }
.phead { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 20px; gap: 16px; flex-wrap: wrap; }
.phead h1 { margin: 0 0 4px; font-size: 22px; }
.info-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 22px; }
.info { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 13px 15px; box-shadow: var(--shadow); }
.info .k { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.info .v { font-size: 14px; font-weight: 600; margin-top: 4px; word-break: break-word; }

.charts { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 22px; }
.chart-card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px 16px 8px; box-shadow: var(--shadow); }
.chart-card h3 { margin: 0; font-size: 14px; }
.chart-card .now { font-size: 12px; color: var(--muted); margin: 2px 0 8px; }
.chart-card .now b { color: var(--ink); font-size: 13px; }

.legend-src { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--green);
  background: var(--green-bg); padding: 4px 10px; border-radius: 20px; }

.fresh-banner { background: var(--amber-bg); border: 1px solid #f1d99a; color: #7a5200;
  padding: 11px 14px; border-radius: 10px; font-size: 13px; margin-bottom: 18px; }
.fresh-banner b { color: #5c3d00; }

@media (max-width: 880px) {
  .stats, .info-grid { grid-template-columns: repeat(2, 1fr); }
  .charts { grid-template-columns: 1fr; }
  .acct-chip { display: none; }
}

/* ---------- Connect / OAuth result pages ---------- */
.center-wrap { min-height: calc(100vh - 60px); display: flex; align-items: center; justify-content: center; padding: 24px; }
.connect-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 30px 32px; max-width: 460px; width: 100%; box-shadow: var(--shadow); }
.connect-card h2 { margin: 0 0 4px; font-size: 18px; }
.connect-card .sub { color: var(--muted); font-size: 13px; margin: 0 0 20px; }
.cred-box { background: var(--grey-bg); border: 1px solid var(--line); border-radius: 10px;
  padding: 14px 16px; margin-bottom: 20px; }
.cred-box .row { display: flex; justify-content: space-between; align-items: center; padding: 5px 0; }
.cred-box .k { font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.cred-box .v { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13.5px; }
.reveal-btn { background: none; border: 1px solid var(--line); border-radius: 6px; padding: 3px 9px;
  font-size: 11.5px; color: #374151; }
.reveal-btn:hover { background: #fff; }
.connect-note { font-size: 12px; color: var(--muted); line-height: 1.5; margin-bottom: 20px; }
.result-icon { width: 52px; height: 52px; border-radius: 50%; display: grid; place-items: center;
  font-size: 26px; margin: 0 auto 16px; }
.result-icon.ok { background: var(--green-bg); color: var(--green); }
.result-icon.fail { background: #fdecec; color: #b42318; }
.connect-card.center-text { text-align: center; }
.err-detail { background: #fdecec; border: 1px solid #f7caca; color: #7a1f1f; font-size: 12px;
  border-radius: 8px; padding: 10px 12px; margin: 14px 0; text-align: left; word-break: break-word;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; max-height: 160px; overflow-y: auto; }

/* ---------- Query builder ---------- */
.qb-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 14px; }
.qb-grid .field { margin-bottom: 0; }
.qb-grid select, .qb-grid input {
  width: 100%; padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 13px;
}
.qb-actions { display: flex; align-items: center; gap: 12px; }
.qb-result { margin-top: 16px; }
.qb-result .summary { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; }
.qb-status { font-size: 12.5px; font-weight: 700; padding: 3px 10px; border-radius: 20px; }
.qb-status.ok2xx { background: var(--green-bg); color: var(--green); }
.qb-status.err { background: #fdecec; color: #b42318; }
.qb-count { font-size: 13px; color: var(--muted); }
.json-view { background: #0c1118; color: #cdd6e3; border-radius: 10px; padding: 12px 14px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11.5px; line-height: 1.6;
  max-height: 320px; overflow: auto; white-space: pre-wrap; word-break: break-word; }
.dl-row { display: flex; gap: 8px; margin-top: 10px; }

.history-table th, .history-table td { padding: 9px 14px; font-size: 12.5px; }
.method-pill { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 6px;
  background: var(--grey-bg); color: #374151; }

.autosync-row { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }
.switch { position: relative; width: 38px; height: 21px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; inset: 0; background: #d1d5db; border-radius: 21px; cursor: pointer; transition: .15s; }
.slider:before { content: ""; position: absolute; height: 15px; width: 15px; left: 3px; top: 3px;
  background: #fff; border-radius: 50%; transition: .15s; }
input:checked + .slider { background: var(--green); }
input:checked + .slider:before { transform: translateX(17px); }
.autosync-label { font-size: 13px; color: #374151; }
.autosync-label b { color: var(--ink); }

.pool-note { font-size: 12px; color: var(--muted); margin-top: 6px; }
.empty-pool { background: #fdecec; color: #b42318; border: 1px solid #f7caca; border-radius: 8px;
  padding: 9px 12px; font-size: 12.5px; margin-bottom: 14px; }
