:root {
  --brand: #24569b;
  --brand-dark: #173d74;
  --brand-soft: #eef4ff;
  --text: #1f2937;
  --muted: #667085;
  --border: #d8dee9;
  --surface: #ffffff;
  --background: #f4f7fb;
  --danger: #b42318;
  --danger-soft: #fee4e2;
  --success: #067647;
  --success-soft: #dcfae6;
  --warning: #b54708;
  --warning-soft: #fef0c7;
  --info: #175cd3;
  --info-soft: #dbeafe;
  --shadow: 0 8px 24px rgba(16, 24, 40, 0.08);
}

* { box-sizing: border-box; }
html { min-height: 100%; }
body {
  margin: 0;
  min-height: 100%;
  background: var(--background);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.5;
}
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select, textarea { font: inherit; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  min-height: 64px;
  padding: 0.65rem 1.25rem;
  background: #fff;
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .7rem; font-weight: 800; color: var(--brand-dark); }
.brand-mark {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: var(--brand);
  color: #fff;
  font-size: 1.15rem;
}
.nav-links { display: flex; flex-wrap: wrap; align-items: center; gap: .35rem; }
.nav-links a, .nav-button {
  display: inline-flex;
  align-items: center;
  border: 0;
  background: transparent;
  color: #344054;
  padding: .55rem .7rem;
  border-radius: 7px;
  cursor: pointer;
}
.nav-links a:hover, .nav-button:hover { background: var(--brand-soft); text-decoration: none; }
.tenant-pill { color: var(--muted); font-size: .86rem; margin-left: .4rem; }

.container { width: min(1180px, calc(100% - 2rem)); margin: 1.5rem auto 3rem; }
.container.narrow { width: min(680px, calc(100% - 2rem)); }
.page-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; margin-bottom: 1.25rem; }
.page-header h1 { margin: 0 0 .2rem; font-size: clamp(1.5rem, 3vw, 2.15rem); }
.page-header p { margin: 0; color: var(--muted); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 1.15rem;
}
.card + .card { margin-top: 1rem; }
.card h2, .card h3 { margin-top: 0; }
.card-header { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-bottom: .8rem; }
.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.stat { padding: 1rem; border-radius: 10px; background: #fff; border: 1px solid var(--border); }
.stat-value { font-size: 1.8rem; font-weight: 800; }
.stat-label { color: var(--muted); font-size: .9rem; }

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .9rem 1rem; }
.form-grid .full { grid-column: 1 / -1; }
.form-group { display: flex; flex-direction: column; gap: .35rem; }
.form-group label { font-weight: 650; font-size: .92rem; }
.form-control {
  width: 100%;
  min-height: 42px;
  padding: .58rem .68rem;
  color: var(--text);
  background: #fff;
  border: 1px solid #bfc8d6;
  border-radius: 8px;
  outline: none;
}
textarea.form-control { min-height: 92px; resize: vertical; }
.form-control:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(36, 86, 155, .12); }
.form-help { color: var(--muted); font-size: .82rem; }
.field-errors { margin: 0; padding: 0; list-style: none; color: var(--danger); font-size: .82rem; }
.checkbox-row { display: flex; align-items: flex-start; gap: .55rem; }
.checkbox-row input { margin-top: .25rem; width: 18px; height: 18px; }
fieldset { border: 1px solid var(--border); border-radius: 10px; padding: 1rem; margin: 0 0 1rem; }
legend { padding: 0 .45rem; font-weight: 750; color: var(--brand-dark); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  min-height: 40px;
  padding: .55rem .85rem;
  border: 1px solid transparent;
  border-radius: 8px;
  font-weight: 700;
  cursor: pointer;
  text-decoration: none;
}
.btn:hover { text-decoration: none; filter: brightness(.98); }
.btn-primary { background: var(--brand); color: #fff; }
.btn-secondary { background: #fff; border-color: var(--border); color: #344054; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-success { background: var(--success); color: #fff; }
.btn-small { min-height: 32px; padding: .35rem .55rem; font-size: .85rem; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.actions { display: flex; flex-wrap: wrap; gap: .55rem; align-items: center; }

.flash-stack { width: min(1180px, calc(100% - 2rem)); margin: 1rem auto 0; }
.alert { padding: .8rem 1rem; border-radius: 9px; border: 1px solid transparent; margin-bottom: .65rem; }
.alert-success { background: var(--success-soft); color: var(--success); border-color: #a9efc5; }
.alert-danger { background: var(--danger-soft); color: var(--danger); border-color: #fda29b; }
.alert-warning { background: var(--warning-soft); color: var(--warning); border-color: #fedf89; }
.alert-info { background: var(--info-soft); color: var(--info); border-color: #b2ccff; }

.badge { display: inline-flex; padding: .22rem .5rem; border-radius: 999px; font-size: .78rem; font-weight: 750; }
.badge-neutral { background: #eaecf0; color: #344054; }
.badge-info { background: var(--info-soft); color: var(--info); }
.badge-warning { background: var(--warning-soft); color: var(--warning); }
.badge-success { background: var(--success-soft); color: var(--success); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 10px; background: #fff; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: .72rem .75rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: #475467; font-size: .82rem; background: #f8fafc; }
tr:last-child td { border-bottom: 0; }
.muted { color: var(--muted); }
.small { font-size: .84rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; word-break: break-all; }

.pdf-frame { width: 100%; min-height: 720px; border: 1px solid var(--border); border-radius: 8px; background: #fff; }
.detail-list { display: grid; grid-template-columns: minmax(145px, .35fr) 1fr; gap: .55rem 1rem; }
.detail-list dt { font-weight: 700; color: #475467; }
.detail-list dd { margin: 0; }
.audit-list { list-style: none; margin: 0; padding: 0; }
.audit-list li { display: grid; grid-template-columns: 150px 1fr; gap: .7rem; padding: .72rem 0; border-bottom: 1px solid var(--border); }
.audit-list li:last-child { border-bottom: 0; }

.signature-box { border: 1px dashed #98a2b3; border-radius: 10px; background: #fff; overflow: hidden; }
#signature-canvas { width: 100%; height: 220px; display: block; touch-action: none; cursor: crosshair; }
.signature-toolbar { display: flex; justify-content: flex-end; padding: .55rem; border-top: 1px solid var(--border); }

.auth-shell { min-height: 100vh; display: grid; place-items: center; padding: 2rem 1rem; }
.auth-card { width: min(470px, 100%); }
.auth-logo { text-align: center; margin-bottom: 1rem; }
.auth-logo .brand-mark { margin: 0 auto .55rem; }

.public-header { background: var(--brand-dark); color: #fff; padding: 1rem; }
.public-header-inner { width: min(980px, calc(100% - 2rem)); margin: auto; display: flex; justify-content: space-between; gap: 1rem; align-items: center; }
.public-main { width: min(980px, calc(100% - 2rem)); margin: 1.4rem auto 3rem; }
.success-panel { text-align: center; padding: 2rem 1rem; }
.success-icon { width: 70px; height: 70px; display: grid; place-items: center; margin: 0 auto 1rem; border-radius: 50%; background: var(--success-soft); color: var(--success); font-size: 2rem; font-weight: 900; }

.mapper-layout { display: grid; grid-template-columns: 310px minmax(0, 1fr); gap: 1rem; align-items: start; }
.mapper-sidebar { position: sticky; top: 78px; max-height: calc(100vh - 96px); overflow: auto; }
.mapper-page { position: relative; width: fit-content; max-width: 100%; margin: 0 auto 1.2rem; border: 1px solid var(--border); box-shadow: var(--shadow); background: #fff; }
.mapper-page img { display: block; max-width: 100%; height: auto; user-select: none; }
.mapper-overlay { position: absolute; inset: 0; cursor: crosshair; }
.mapping-box { position: absolute; border: 2px solid #d92d20; background: rgba(217,45,32,.12); color: #7a271a; font-size: 10px; overflow: hidden; pointer-events: none; }
.mapping-row { border: 1px solid var(--border); border-radius: 8px; padding: .55rem; margin-bottom: .5rem; background: #fff; }
.mapper-status { min-height: 24px; font-size: .86rem; }

.empty-state { text-align: center; padding: 2.5rem 1rem; color: var(--muted); }
code { background: #f2f4f7; border-radius: 5px; padding: .1rem .28rem; }

@media (max-width: 900px) {
  .grid-4, .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .mapper-layout { grid-template-columns: 1fr; }
  .mapper-sidebar { position: static; max-height: none; }
}
@media (max-width: 680px) {
  .topbar { align-items: flex-start; flex-direction: column; }
  .nav-links { width: 100%; }
  .tenant-pill { display: none; }
  .grid-4, .grid-3, .grid-2, .form-grid { grid-template-columns: 1fr; }
  .page-header { flex-direction: column; }
  .detail-list { grid-template-columns: 1fr; }
  .detail-list dd { margin-bottom: .5rem; }
  .audit-list li { grid-template-columns: 1fr; }
  .pdf-frame { min-height: 520px; }
}
