:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --ink: #1f2933;
  --muted: #6b7785;
  --line: #e2e8f0;
  --brand: #2f6f57;
  --brand-dark: #245544;
  --brand-soft: #e7f1ec;
  --accent: #c9882f;

  /* stage colors */
  --pending: #d99a2b;
  --current: #2f6f57;
  --aftercare: #3877b8;
  --discharged: #b1592e;
  --archived: #6b7785;

  --shadow: 0 1px 3px rgba(16,24,40,.08), 0 1px 2px rgba(16,24,40,.06);
  --shadow-lg: 0 12px 40px rgba(16,24,40,.18);
}

* { 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);
  font-size: 15px;
  line-height: 1.45;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 12px 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 42px; height: 42px; border-radius: 10px;
  background: var(--brand); color: #fff;
  display: grid; place-items: center; font-weight: 700; letter-spacing: .5px;
}
.brand h1 { font-size: 18px; margin: 0; }
.tagline { margin: 0; font-size: 12px; color: var(--muted); }

.nav { display: flex; gap: 4px; margin-left: 8px; }
.nav-btn {
  border: none; background: none; padding: 8px 14px; border-radius: 8px;
  font-size: 14px; font-weight: 600; color: var(--muted); cursor: pointer;
}
.nav-btn:hover { background: var(--bg); color: var(--ink); }
.nav-btn.active { background: var(--brand-soft); color: var(--brand-dark); }

.topbar-actions { margin-left: auto; display: flex; gap: 8px; }

/* ---------- Buttons ---------- */
.btn {
  border: 1px solid transparent; border-radius: 8px; padding: 8px 14px;
  font-size: 14px; font-weight: 600; cursor: pointer; background: var(--bg);
}
.btn.primary { background: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.ghost { background: #fff; border-color: var(--line); color: var(--ink); }
.btn.ghost:hover { background: var(--bg); }
.btn.danger { background: #fdecec; color: #b42318; border-color: #f5c7c2; }
.btn.danger:hover { background: #fbdbd8; }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.icon-btn {
  border: none; background: none; font-size: 26px; line-height: 1;
  cursor: pointer; color: var(--muted); padding: 0 6px;
}
.icon-btn:hover { color: var(--ink); }

/* ---------- Layout ---------- */
main { padding: 24px; max-width: 1200px; margin: 0 auto; }
.view-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.view-head h2 { margin: 0; font-size: 22px; }
.view-head p { margin: 4px 0 0; color: var(--muted); font-size: 14px; }

/* ---------- Cards ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px,1fr)); gap: 14px; margin-bottom: 28px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 18px; box-shadow: var(--shadow); cursor: pointer; transition: transform .08s;
}
.card:hover { transform: translateY(-2px); }
.card .num { font-size: 34px; font-weight: 700; line-height: 1; }
.card .label { color: var(--muted); font-size: 13px; margin-top: 6px; font-weight: 600; }
.card .dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; margin-right: 6px; }

.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 20px; box-shadow: var(--shadow); margin-bottom: 22px;
}
.panel h3 { margin: 0 0 14px; font-size: 16px; }
.panel-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 22px; }

/* ---------- Stage badges ---------- */
.badge {
  display: inline-block; padding: 3px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 700; color: #fff;
}
.badge.pending { background: var(--pending); }
.badge.current { background: var(--current); }
.badge.aftercare { background: var(--aftercare); }
.badge.discharged { background: var(--discharged); }
.badge.archived { background: var(--archived); }

/* ---------- Table ---------- */
.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar input[type="search"] {
  flex: 1; min-width: 200px; padding: 9px 12px; border: 1px solid var(--line);
  border-radius: 8px; font-size: 14px;
}
.filter-chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line); background: #fff; border-radius: 999px;
  padding: 6px 12px; font-size: 13px; font-weight: 600; cursor: pointer; color: var(--muted);
}
.chip.active { background: var(--ink); color: #fff; border-color: var(--ink); }

table { width: 100%; border-collapse: collapse; background: var(--panel); }
.table-wrap { border: 1px solid var(--line); border-radius: 12px; overflow: hidden; box-shadow: var(--shadow); }
thead th {
  text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .4px;
  color: var(--muted); padding: 12px 16px; background: #fafbfc; border-bottom: 1px solid var(--line);
}
tbody td { padding: 12px 16px; border-bottom: 1px solid var(--line); font-size: 14px; }
tbody tr:last-child td { border-bottom: none; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--brand-soft); }
.empty { text-align: center; padding: 50px 20px; color: var(--muted); }

/* ---------- Forms ---------- */
.form-section { border-top: 1px solid var(--line); padding-top: 18px; margin-top: 18px; }
.form-section:first-child { border-top: none; padding-top: 0; margin-top: 0; }
.form-section h4 { margin: 0 0 14px; font-size: 14px; text-transform: uppercase; letter-spacing: .4px; color: var(--brand-dark); }
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field-grid.three { grid-template-columns: 1fr 1fr 1fr; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 13px; font-weight: 600; color: var(--muted); }
.field input, .field select, .field textarea {
  padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px;
  font-family: inherit; background: #fff;
}
.field textarea { resize: vertical; min-height: 70px; }
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft);
}
.contact-group { margin-top: 14px; }
.contact-title { font-size: 13px; font-weight: 700; color: var(--ink); margin-bottom: 8px; }
.po-row { border: 1px dashed var(--line); border-radius: 8px; padding: 10px; margin-bottom: 8px; }
.po-row .po-remove { margin-top: 8px; }
.checks { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.checks.three { grid-template-columns: 1fr 1fr 1fr; }
.check {
  display: flex; align-items: center; gap: 9px; padding: 9px 12px;
  border: 1px solid var(--line); border-radius: 8px; font-size: 14px; cursor: pointer;
}
.check input { width: 17px; height: 17px; accent-color: var(--brand); }
.check.on { background: var(--brand-soft); border-color: var(--brand); }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(16,24,40,.45);
  display: grid; place-items: center; z-index: 50; padding: 20px;
}
.modal-overlay[hidden] { display: none; }
.modal {
  background: var(--panel); border-radius: 14px; width: 100%; max-width: 720px;
  max-height: 92vh; display: flex; flex-direction: column; box-shadow: var(--shadow-lg);
}
.modal.wide { max-width: 860px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 22px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-body { padding: 22px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 16px 22px; border-top: 1px solid var(--line); }

/* ---------- Detail view ---------- */
.detail-head { display: flex; align-items: center; gap: 14px; margin-bottom: 6px; }
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 28px; margin: 14px 0 4px; }
.dl { display: flex; flex-direction: column; padding: 7px 0; border-bottom: 1px dashed var(--line); }
.dl dt { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .3px; }
.dl dd { margin: 2px 0 0; font-size: 14px; }
.pill-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.pill { background: var(--brand-soft); color: var(--brand-dark); border-radius: 999px; padding: 4px 11px; font-size: 13px; font-weight: 600; }
.note { background: #fafbfc; border: 1px solid var(--line); border-radius: 8px; padding: 11px 13px; margin-bottom: 8px; }
.note .meta { font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.detail-actions { display: flex; gap: 8px; margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--line); }
.note-add { display: flex; gap: 8px; margin-top: 10px; }
.note-add input { flex: 1; padding: 8px 11px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; }

/* ---------- Reports ---------- */
.bar-row { display: grid; grid-template-columns: 180px 1fr 48px; align-items: center; gap: 12px; margin-bottom: 9px; }
.bar-label { font-size: 14px; }
.bar-track { background: var(--bg); border-radius: 6px; height: 22px; overflow: hidden; }
.bar-fill { background: var(--brand); height: 100%; border-radius: 6px; min-width: 2px; }
.bar-val { text-align: right; font-weight: 700; font-size: 14px; }
.report-table th, .report-table td { text-align: center; }
.report-table th:first-child, .report-table td:first-child { text-align: left; }

.hint { font-size: 13px; color: var(--muted); background: var(--brand-soft); border-left: 3px solid var(--brand); padding: 10px 14px; border-radius: 6px; margin-bottom: 18px; }

/* ---------- Drug testing ---------- */
.dt-history { display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px; }
.dt-record { border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; background: #fafbfc; }
.dt-record-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.dt-record-head strong { font-size: 15px; }
.dt-results { display: flex; flex-wrap: wrap; gap: 6px; }
.dt-result { display: inline-flex; gap: 5px; align-items: baseline; padding: 4px 9px; border-radius: 6px; font-size: 12px; font-weight: 700; }
.dt-result b { font-weight: 600; }
.dt-result.pos { background: #fdecec; color: #b42318; }
.dt-result.neg { background: #e7f4ee; color: #1f7a4d; }
.dt-result.na { background: var(--bg); color: var(--muted); }
.dt-result.pos b, .dt-result.neg b, .dt-result.na b { color: var(--ink); }

.dt-actions { white-space: nowrap; }
.dt-actions .btn { margin: 0 2px; }
.dt-mode { font-size: 13px; font-weight: 700; color: var(--brand-dark); margin-bottom: 10px; }
.dt-add { border: 1px solid var(--line); border-radius: 8px; margin-top: 8px; }
.dt-add summary { cursor: pointer; padding: 10px 14px; font-weight: 600; color: var(--brand-dark); list-style: none; }
.dt-add summary::-webkit-details-marker { display: none; }
.dt-add[open] summary { border-bottom: 1px solid var(--line); }
.dt-form { padding: 14px; }
.dt-toggles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.dt-toggle {
  display: flex; flex-direction: column; gap: 2px; align-items: flex-start;
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px;
  cursor: pointer; background: #fff; text-align: left;
}
.dt-toggle .dt-name { font-size: 13px; font-weight: 600; color: var(--ink); }
.dt-toggle .dt-state { font-size: 12px; font-weight: 700; }
.dt-toggle.neg { background: #e7f4ee; border-color: #b7e0ca; }
.dt-toggle.neg .dt-state { color: #1f7a4d; }
.dt-toggle.pos { background: #fdecec; border-color: #f5c7c2; }
.dt-toggle.pos .dt-state { color: #b42318; }

@media (max-width: 620px) { .dt-toggles { grid-template-columns: repeat(2, 1fr); } }

.clip {
  display: inline-block; margin-left: 8px; font-size: 12px; font-weight: 700;
  color: var(--brand-dark); background: var(--brand-soft); border-radius: 999px; padding: 1px 8px;
  vertical-align: middle;
}

/* ---------- Attachments ---------- */
.at-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.at-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; background: #fafbfc;
}
.at-info { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.at-name { font-weight: 600; font-size: 14px; word-break: break-word; }
.at-meta { font-size: 12px; color: var(--muted); }
.at-cat { background: var(--brand-soft); color: var(--brand-dark); border-radius: 999px; padding: 1px 8px; font-weight: 700; }
.at-btns { display: flex; gap: 6px; white-space: nowrap; }
.at-add { border: 1px solid var(--line); border-radius: 8px; padding: 14px; margin-top: 4px; }
.at-add input[type="file"] { padding: 7px; }

@media print { .at-add, .at-btns { display: none !important; } }

/* ---------- Auth / users ---------- */
.user-chip {
  font-size: 13px; font-weight: 600; color: var(--brand-dark);
  background: var(--brand-soft); border-radius: 999px; padding: 6px 12px; white-space: nowrap;
}
.auth-screen {
  position: fixed; inset: 0; z-index: 100; background: var(--bg);
  display: grid; place-items: center; padding: 20px;
}
.auth-screen[hidden] { display: none; }
.auth-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  box-shadow: var(--shadow-lg); padding: 32px; width: 100%; max-width: 380px; text-align: center;
}
.auth-logo { width: 54px; height: 54px; font-size: 20px; margin: 0 auto 14px; border-radius: 12px; }
.auth-card h1 { margin: 0; font-size: 22px; }
.auth-card .tagline { margin: 2px 0 0; }
.auth-card .field { text-align: left; margin-top: 12px; }
.auth-btn { width: 100%; margin-top: 14px; }
.auth-hint { font-size: 13px; color: var(--muted); background: var(--brand-soft); border-radius: 8px; padding: 9px 12px; margin: 14px 0 4px; }
.auth-error { color: #b42318; font-size: 13px; font-weight: 600; margin-top: 10px; min-height: 16px; }
.access-note { background: #fff8ec; border: 1px solid #f0d9a8; border-radius: 8px; padding: 12px 14px; font-size: 14px; color: #7a5a12; }
.auth-reset { background: none; border: none; color: var(--brand); font-size: 13px; font-weight: 600; cursor: pointer; margin-top: 14px; text-decoration: underline; padding: 0; }
.auth-reset:hover { color: var(--brand-dark); }
.auth-ver { margin-top: 16px; font-size: 11px; color: var(--muted); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 12px 20px; border-radius: 10px;
  font-size: 14px; font-weight: 600; box-shadow: var(--shadow-lg); z-index: 80;
}

@media (max-width: 760px) {
  .panel-grid, .field-grid, .field-grid.three, .checks, .checks.three, .detail-grid { grid-template-columns: 1fr; }
  .topbar { flex-wrap: wrap; }
  .bar-row { grid-template-columns: 110px 1fr 40px; }
}

/* ---------- Print: only the open participant record ---------- */
.print-only { display: none; }

@media print {
  /* Hide everything that isn't the record */
  .topbar, main, #formModal, .toast { display: none !important; }
  .modal-head .icon-btn, .detail-actions, .note-add, .dt-add, .dt-actions { display: none !important; }

  body { background: #fff; color: #000; }

  /* Turn the open detail modal into a plain full-page document */
  #detailModal:not([hidden]) {
    position: static !important; inset: auto !important; background: none !important;
    display: block !important; padding: 0 !important;
  }
  #detailModal .modal {
    box-shadow: none !important; max-width: none !important; max-height: none !important;
    width: 100% !important; border-radius: 0 !important; display: block !important;
  }
  #detailModal .modal-head { border-bottom: none; padding: 0; }
  #detailModal .modal-head h2 { display: none; } /* name shown in print header instead */
  #detailModal .modal-body { overflow: visible !important; max-height: none !important; padding: 0 !important; }

  .print-only { display: block; }
  .print-header { margin-bottom: 14px; border-bottom: 2px solid #000; padding-bottom: 10px; }
  .print-brand { font-size: 13px; letter-spacing: .5px; text-transform: uppercase; color: #444; }
  .print-name { font-size: 24px; font-weight: 700; margin: 2px 0; }
  .print-meta { font-size: 12px; color: #666; }

  /* Keep cards/notes from splitting awkwardly across pages */
  .dt-record, .note, .panel { break-inside: avoid; }
  .dt-result { border: 1px solid #ccc; }

  /* Stay readable even if the browser drops background colors */
  .badge { background: none !important; color: #000 !important; border: 1px solid #000; }
  .dt-result.pos { color: #b42318 !important; }
  .pill { background: none !important; border: 1px solid #999; color: #000 !important; }
}
