:root {
  --brand: #4F2493;
  --brand-dark: #3a1a6e;
  --brand-tint: #f1ecf8;
  --ink: #1d1a22;
  --muted: #6b6475;
  --line: #ddd6e8;
  --bg: #f7f5fa;
  --card: #ffffff;
  --danger: #b3261e;
  --danger-tint: #fdecea;
  --ok: #1f7a4a;
  --radius: 12px;
  --tap: 48px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 17px;
  line-height: 1.45;
  color: var(--ink);
  background: var(--bg);
}

button, input, textarea, select { font: inherit; color: inherit; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 18px;
  background: var(--brand); color: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,.15);
}
.topbar .brand { font-weight: 700; letter-spacing: .2px; }
.topbar .brand small { font-weight: 400; opacity: .8; margin-left: 6px; }
.topbar .spacer { flex: 1; }
.topbar .back {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 40px; padding: 0 14px; border-radius: 999px;
  background: rgba(255,255,255,.15); color: #fff; text-decoration: none; border: 0;
}
.topbar .status { font-size: 13px; opacity: .85; }

main { max-width: 1000px; margin: 0 auto; padding: 20px 16px 120px; }

/* ---------- Home ---------- */
.home h1 { font-size: 26px; margin: 8px 0 4px; }
.home .lead { color: var(--muted); margin: 0 0 18px; }
.search {
  width: 100%; min-height: var(--tap); padding: 10px 16px;
  border: 1px solid var(--line); border-radius: var(--radius); background: #fff;
  margin-bottom: 18px;
}
.cat-title { font-size: 14px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); margin: 22px 0 10px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.card {
  display: block; padding: 18px; border-radius: var(--radius);
  background: var(--card); border: 1px solid var(--line);
  text-decoration: none; color: inherit;
  transition: border-color .15s, box-shadow .15s;
}
.card:hover, .card:focus-visible { border-color: var(--brand); box-shadow: 0 4px 14px rgba(79,36,147,.12); outline: none; }
.card h3 { margin: 0 0 6px; font-size: 19px; color: var(--brand-dark); }
.card p { margin: 0; color: var(--muted); font-size: 15px; }
.card .meta { margin-top: 12px; display: flex; gap: 8px; flex-wrap: wrap; font-size: 13px; }
.pill { padding: 2px 10px; border-radius: 999px; background: var(--brand-tint); color: var(--brand-dark); }
.pill.draft { background: #fff4d6; color: #7a5600; }
.empty { color: var(--muted); padding: 30px 0; text-align: center; }

/* ---------- Form ---------- */
.form-head h1 { font-size: 24px; margin: 6px 0 2px; color: var(--brand-dark); }
.form-head .sub { color: var(--muted); margin: 0 0 10px; font-weight: 600; }
.form-head .intro { margin: 0 0 18px; }

.panel {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px 18px; margin-bottom: 16px;
}
.panel > h2, .group-title {
  margin: 0 0 12px; font-size: 18px; color: var(--brand);
  padding-bottom: 8px; border-bottom: 2px solid var(--brand-tint);
}

.row { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; }
.field { margin-bottom: 14px; }
.row .field { margin-bottom: 0; }
.field > label, .field > .label {
  display: block; font-weight: 600; font-size: 15px; margin-bottom: 6px;
}
.req { color: var(--danger); margin-left: 2px; }
.hint { color: var(--muted); font-size: 14px; margin-top: 4px; }

input[type=text], input[type=date], textarea {
  width: 100%; min-height: var(--tap); padding: 10px 12px;
  border: 1px solid #c9c1d6; border-radius: 10px; background: #fff;
}
textarea { min-height: 84px; resize: vertical; }
input:focus, textarea:focus { outline: 3px solid rgba(79,36,147,.25); border-color: var(--brand); }

/* segmented choice */
.seg { display: inline-flex; flex-wrap: wrap; gap: 8px; }
.seg button {
  min-height: var(--tap); min-width: 64px; padding: 0 16px;
  border: 2px solid #c9c1d6; border-radius: 10px; background: #fff; cursor: pointer;
  font-weight: 600;
}
.seg button[aria-pressed=true] { border-color: var(--brand); background: var(--brand); color: #fff; }
.seg button[data-v="No"][aria-pressed=true] { background: #a8321e; border-color: #a8321e; }
.seg button[data-v="N/A"][aria-pressed=true] { background: #5d5866; border-color: #5d5866; }
.other-input { margin-top: 10px; }

/* checklist (Yes/No/NA rows) */
.cl-group + .cl-group { margin-top: 18px; }
.cl-item {
  display: grid; grid-template-columns: 1fr auto; gap: 10px 16px; align-items: center;
  padding: 12px 0; border-top: 1px solid var(--line);
}
.cl-item:first-of-type { border-top: 0; }
.cl-item .text { font-size: 16px; }
.cl-item .extra { grid-column: 1 / -1; display: none; gap: 12px; grid-template-columns: 2fr 1fr; }
.cl-item.needs-extra .extra { display: grid; }
.cl-item .extra label { font-size: 14px; font-weight: 600; display: block; margin-bottom: 4px; }
.cl-item .seg button { min-width: 62px; padding: 0 10px; }
.progress { font-size: 14px; color: var(--muted); font-weight: 400; float: right; }

/* checkbox groups */
.checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 8px; }
.check {
  display: flex; align-items: center; gap: 10px;
  min-height: var(--tap); padding: 6px 12px;
  border: 1px solid #d8d0e4; border-radius: 10px; background: #fff; cursor: pointer;
  user-select: none;
}
.check.indent { margin-left: 26px; }
.check input { width: 22px; height: 22px; accent-color: var(--brand); flex: none; }
.check:has(input:checked) { border-color: var(--brand); background: var(--brand-tint); }
.follow { margin-top: 12px; display: none; }
.follow.show { display: block; }

.subgroups { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 14px; }
.subgroup { border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: #fcfbfe; }
.subgroup h3 { margin: 0 0 10px; font-size: 16px; }

/* signature */
.sig-wrap { position: relative; border: 2px dashed #c9c1d6; border-radius: 10px; background: #fff; }
.sig-wrap canvas { display: block; width: 100%; height: 180px; touch-action: none; }
.sig-wrap .sig-line { position: absolute; left: 20px; right: 20px; bottom: 38px; border-bottom: 1px solid #ccc; pointer-events: none; }
.sig-actions { margin-top: 8px; }

/* errors */
.has-error input, .has-error textarea, .has-error .sig-wrap, .has-error .seg button { border-color: var(--danger); }
.has-error.cl-item { background: var(--danger-tint); border-radius: 8px; padding-left: 8px; padding-right: 8px; }
.err-msg { color: var(--danger); font-size: 14px; margin-top: 4px; font-weight: 600; }
.err-summary {
  background: var(--danger-tint); border: 1px solid #f2b8b5; color: #601410;
  border-radius: var(--radius); padding: 12px 16px; margin-bottom: 16px; display: none;
}
.err-summary.show { display: block; }
.err-summary ul { margin: 6px 0 0; padding-left: 20px; }
.err-summary a { color: inherit; }

/* action bar */
.actionbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
  display: flex; gap: 10px; justify-content: flex-end; align-items: center;
  padding: 12px 18px calc(12px + env(safe-area-inset-bottom));
  background: rgba(255,255,255,.96); border-top: 1px solid var(--line);
  backdrop-filter: blur(6px);
}
.actionbar .saved { margin-right: auto; color: var(--muted); font-size: 14px; }
.btn {
  min-height: var(--tap); padding: 0 20px; border-radius: 10px; border: 2px solid var(--brand);
  background: #fff; color: var(--brand); font-weight: 700; cursor: pointer;
}
.btn.primary { background: var(--brand); color: #fff; }
.btn.ghost { border-color: transparent; color: var(--muted); }
.btn:disabled { opacity: .5; }

/* modal */
.modal-bg {
  position: fixed; inset: 0; z-index: 50; background: rgba(20,10,35,.5);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.modal {
  background: #fff; border-radius: 16px; padding: 22px; max-width: 460px; width: 100%;
  box-shadow: 0 20px 50px rgba(0,0,0,.3);
}
.modal h2 { margin: 0 0 8px; color: var(--brand-dark); }
.modal p { margin: 0 0 14px; }
.modal .btns { display: flex; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
.modal ol { padding-left: 20px; margin: 0 0 16px; }

@media (max-width: 600px) {
  .cl-item { grid-template-columns: 1fr; }
  .cl-item .extra { grid-template-columns: 1fr; }
  .actionbar .saved { display: none; }
}

[hidden] { display: none !important; }
.err-summary, [data-key] { scroll-margin-top: 84px; scroll-margin-bottom: 90px; }
