:root {
  --brand: #2563eb;
  --brand-dark: #1d4ed8;
  --brand-soft: #eff6ff;
  --ink: #0f172a;
  --muted: #64748b;
  --subtle: #94a3b8;
  --line: #e2e8f0;
  --line-dark: #cbd5e1;
  --bg: #f8fafc;
  --panel: #ffffff;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --success: #16a34a;
  --success-soft: #f0fdf4;
  --warning: #d97706;
  --warning-soft: #fffbeb;
  --orange: #ea580c;
  --radius: 8px;
  --radius-sm: 4px;
  --shadow-sm: 0 1px 2px rgba(15,23,42,.05);
  --shadow: 0 4px 6px -1px rgba(15,23,42,.08), 0 2px 4px -2px rgba(15,23,42,.05);
  --shadow-lg: 0 10px 15px -3px rgba(15,23,42,.08), 0 4px 6px -4px rgba(15,23,42,.05);
  --focus: 0 0 0 3px rgba(37,99,235,.25);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

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

.app { display: flex; height: 100vh; overflow: hidden; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

/* Sidebar */
.sidebar {
  width: 248px; flex-shrink: 0; background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; transition: transform .2s ease;
}
.sidebar .brand {
  height: 60px; display: flex; align-items: center; padding: 0 18px;
  border-bottom: 1px solid var(--line); gap: 10px;
}
.brand .logo {
  width: 28px; height: 28px; background: var(--brand); color: #fff; border-radius: var(--radius-sm);
  display: grid; place-items: center; font-weight: 700; font-size: 14px;
}
.brand .title { font-weight: 700; letter-spacing: -.02em; }

.nav-section { padding: 12px 10px; }
.nav-section small {
  display: block; padding: 6px 10px; font-size: 10px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--subtle); font-weight: 600;
}

.nav-link {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: var(--radius);
  color: var(--muted); font-size: 14px; cursor: pointer; user-select: none;
  transition: background .15s, color .15s; border: none; background: transparent; width: 100%; text-align: left;
}
.nav-link:hover { background: var(--bg); color: var(--ink); }
.nav-link.active { background: var(--brand-soft); color: var(--brand); font-weight: 600; }
.nav-link .icon { width: 18px; text-align: center; }
.nav-link .badge {
  margin-left: auto; min-width: 20px; text-align: center; padding: 2px 7px; border-radius: 999px;
  background: var(--line); color: var(--muted); font-size: 11px; font-weight: 700;
}

.user-card {
  margin: auto 12px 12px; padding: 12px; border: 1px solid var(--line); border-radius: var(--radius);
  display: flex; align-items: center; gap: 10px; cursor: pointer;
}
.user-avatar {
  width: 34px; height: 34px; border-radius: 50%; background: #c7d2fe; color: var(--brand-dark);
  display: grid; place-items: center; font-size: 13px; font-weight: 700;
}
.user-info { flex: 1; min-width: 0; }
.user-name { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.user-email { font-size: 11px; color: var(--subtle); overflow: hidden; text-overflow: ellipsis; }

/* Main */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.topbar {
  height: 60px; background: var(--panel); border-bottom: 1px solid var(--line);
  display: flex; align-items: center; padding: 0 24px; gap: 16px; flex-shrink: 0;
}
.topbar .burger { display: none; background: transparent; border: none; color: var(--muted); cursor: pointer; }
.topbar h1 { font-size: 18px; font-weight: 700; flex: 1; }
.global-actions { display: flex; align-items: center; gap: 8px; }

.content { flex: 1; overflow: auto; padding: 24px; }
.content.padded { padding: 24px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: var(--radius); border: 1px solid transparent;
  background: var(--bg); color: var(--ink); font-weight: 600; font-size: 13px;
  cursor: pointer; transition: all .15s;
}
.btn:hover { background: var(--line); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }

.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #b91c1c; }

.btn-ghost { background: transparent; border-color: var(--line); color: var(--muted); }
.btn-ghost:hover { background: var(--bg); color: var(--ink); }

.btn-sm { padding: 5px 10px; font-size: 12px; }

/* Cards and panels */
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.card-body { padding: 20px; }
.card-header {
  padding: 16px 20px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center;
}
.card-title { font-size: 16px; font-weight: 700; }

/* Tabs */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 20px; }
.tab {
  padding: 10px 16px; font-size: 13px; font-weight: 600; color: var(--muted);
  cursor: pointer; background: transparent; border: none; border-bottom: 2px solid transparent;
}
.tab:hover { color: var(--ink); }
.tab.active { color: var(--brand); border-bottom-color: var(--brand); }

/* Forms */
.form-group { margin-bottom: 16px; }
.form-label { display: block; font-size: 12px; font-weight: 600; margin-bottom: 6px; color: var(--ink); }
.form-input, .form-select, .form-textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel);
}
.form-input:focus, .form-select:focus, .form-textarea:focus {
  outline: none; border-color: var(--brand); box-shadow: var(--focus);
}
.form-row { display: flex; gap: 12px; }
.form-row .form-group { flex: 1; }

.checkbox, .radio { display: flex; align-items: center; gap: 8px; cursor: pointer; font-size: 13px; }
.checkbox input, .radio input { accent-color: var(--brand); }

/* Dashboard */
.dashboard-header {
  display: flex; justify-content: space-between; align-items: center; gap: 16px; margin-bottom: 20px; flex-wrap: wrap;
}
.search-bar {
  position: relative; min-width: 260px; max-width: 400px; flex: 1;
}
.search-bar input { width: 100%; padding-left: 32px; }
.search-bar::before {
  content: "\260c"; position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--subtle); font-size: 14px;
}

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 16px; }

.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 12px 16px; text-align: left; border-bottom: 1px solid var(--line); }
th { background: var(--bg); font-weight: 700; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .03em; }
tr:hover { background: var(--bg); }

.pill {
  display: inline-flex; align-items: center; gap: 5px; padding: 3px 9px; border-radius: 999px; font-size: 11px; font-weight: 700;
}
.pill-blue { background: var(--brand-soft); color: var(--brand); }
.pill-green { background: var(--success-soft); color: var(--success); }
.pill-orange { background: var(--warning-soft); color: var(--warning); }
.pill-red { background: var(--danger-soft); color: var(--danger); }
.pill-gray { background: var(--bg); color: var(--muted); }

.row-actions { position: relative; }
.row-actions button { background: transparent; border: none; color: var(--muted); cursor: pointer; padding: 4px; }
.row-actions button:hover { color: var(--ink); }

.dropdown {
  position: absolute; right: 0; top: 100%; margin-top: 4px; min-width: 160px; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow-lg); z-index: 50; display: none;
}
.dropdown.open { display: block; }
.dropdown a, .dropdown button { display: flex; width: 100%; padding: 8px 12px; border: none; background: transparent; color: var(--ink); text-align: left; cursor: pointer; font-size: 13px; }
.dropdown a:hover, .dropdown button:hover { background: var(--bg); }

.pagination { display: flex; justify-content: flex-end; gap: 6px; margin-top: 16px; }
.pagination button { min-width: 32px; height: 32px; border: 1px solid var(--line); background: var(--panel); border-radius: var(--radius); cursor: pointer; }
.pagination button.active { background: var(--brand); color: #fff; border-color: var(--brand); }

.empty-state { text-align: center; padding: 48px 20px; color: var(--muted); }
.empty-state h3 { color: var(--ink); margin-bottom: 6px; font-size: 16px; }

/* Bulk bar */
.bulk-bar {
  position: sticky; bottom: 0; background: var(--ink); color: #fff; padding: 12px 20px; border-radius: var(--radius);
  display: flex; justify-content: space-between; align-items: center; margin-top: 16px;
}

/* Prepare */
.prepare-layout { display: grid; grid-template-columns: 220px 1fr 300px; gap: 16px; height: calc(100vh - 120px); }
.prepare-panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); display: flex; flex-direction: column; overflow: hidden; }
.prepare-panel .panel-head { padding: 14px 16px; border-bottom: 1px solid var(--line); font-weight: 700; }
.prepare-panel .panel-body { flex: 1; overflow: auto; padding: 12px; }

.page-thumbs { display: flex; flex-direction: column; gap: 10px; }
.thumb {
  border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 6px; cursor: pointer; background: var(--panel);
  text-align: center; font-size: 11px; color: var(--muted);
}
.thumb canvas { width: 100%; border-radius: 3px; background: #fff; box-shadow: var(--shadow-sm); display: block; }
.thumb.active { border-color: var(--brand); background: var(--brand-soft); }

.canvas-area { flex: 1; overflow: auto; padding: 24px; background: #f1f5f9; display: flex; justify-content: center; position: relative; }
.doc-scaler { transform-origin: top left; transition: transform .2s ease; }

.page {
  width: 612px; height: 792px; background: #fff; box-shadow: var(--shadow); margin: 0 auto 24px;
  position: relative; overflow: hidden; border: 1px solid var(--line-dark);
}
.page-content { padding: 54px; font-size: 13px; line-height: 1.7; color: #334155; position: relative; height: 100%; }
.page-content h2 { font-size: 18px; color: var(--ink); margin-bottom: 8px; }
.page-content p { margin: 0 0 12px; }

.field-palette { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.palette-item {
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 10px 6px; border: 1px solid var(--line);
  border-radius: var(--radius-sm); cursor: pointer; background: var(--panel); font-size: 12px; color: var(--muted);
}
.palette-item:hover { border-color: var(--brand); color: var(--brand); background: var(--brand-soft); }
.palette-item.active { border-color: var(--brand); color: var(--brand); background: var(--brand-soft); }

.field {
  position: absolute; border: 1.5px dashed; border-radius: 3px; background: rgba(255,255,255,.9); cursor: pointer;
  display: flex; align-items: center; padding: 3px 6px; font-size: 11px; user-select: none; min-width: 80px; min-height: 28px;
}
.field.selected { outline: 2px solid var(--brand); z-index: 10; }
.field .field-label { pointer-events: none; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.field.required { background: rgba(255,255,255,.7); }

.resize-handle {
  position: absolute; right: -4px; bottom: -4px; width: 10px; height: 10px; background: var(--brand); border-radius: 50%;
  cursor: se-resize; display: none;
}
.field.selected .resize-handle { display: block; }

.properties label { font-size: 11px; color: var(--subtle); display: block; margin: 8px 0 4px; }
.properties input, .properties select { width: 100%; margin-bottom: 8px; }

.zoom-bar { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.zoom-bar input { flex: 1; }

/* Stepper */
.stepper { display: flex; gap: 8px; align-items: center; margin-bottom: 20px; }
.step { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: var(--radius); color: var(--muted); font-weight: 600; font-size: 13px; cursor: default; }
.step .num { width: 22px; height: 22px; border-radius: 50%; background: var(--line); color: var(--muted); display: grid; place-items: center; font-size: 11px; }
.step.active .num { background: var(--brand); color: #fff; }
.step.active { color: var(--brand); }
.step.done .num { background: var(--success); color: #fff; }

.step-content { display: none; }
.step-content.active { display: block; }

/* Recipients */
.recipient-list { display: flex; flex-direction: column; gap: 10px; }
.recipient-card {
  border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; background: var(--panel);
  display: grid; grid-template-columns: 24px 1fr 1fr 110px 90px 36px; gap: 10px; align-items: center;
}
.recipient-card .color-dot { width: 14px; height: 14px; border-radius: 50%; }
.recipient-card .drag { color: var(--subtle); cursor: grab; }
.recipient-card input, .recipient-card select { padding: 6px 8px; font-size: 12px; border: 1px solid var(--line); border-radius: var(--radius-sm); }

/* Signer */
.signer-frame { display: flex; flex-direction: column; height: calc(100vh - 60px); }
.signer-consent {
  background: var(--brand-soft); border-bottom: 1px solid #dbeafe; padding: 14px 24px; display: flex; align-items: center; gap: 14px;
}
.signer-toolbar {
  padding: 12px 24px; background: var(--panel); border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 12px;
}
.signer-progress { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.signer-progress .counter { font-weight: 700; color: var(--brand); }

.signer-doc { flex: 1; overflow: auto; padding: 24px; background: #f1f5f9; display: flex; justify-content: center; }

.sign-field {
  position: absolute; border: 1.5px solid; border-radius: 4px; background: rgba(255,255,255,.95); cursor: pointer;
  display: flex; flex-direction: column; overflow: hidden;
}
.sign-field.assigned { border-style: solid; z-index: 2; }
.sign-field.assigned.incomplete { background: #fff7ed; }
.sign-field.assigned.complete { background: var(--success-soft); z-index: 3; }
.sign-field .sf-label { font-size: 10px; padding: 3px 6px; border-bottom: 1px solid rgba(0,0,0,.08); display: flex; justify-content: space-between; }
.sign-field .sf-value { flex: 1; padding: 4px 6px; display: flex; align-items: center; overflow: hidden; }
.sign-field .sf-value img { max-width: 100%; max-height: 100%; }
.sign-field .sf-input { border: none; width: 100%; height: 100%; font-size: 13px; padding: 4px; background: transparent; }
.sign-field.active { box-shadow: 0 0 0 3px rgba(37,99,235,.35); z-index: 5; }

/* Modals */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15,23,42,.55); z-index: 100; display: flex; align-items: center; justify-content: center; padding: 20px;
}
.modal {
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow-lg); max-width: 720px; width: 100%; max-height: 90vh; overflow: hidden; display: flex; flex-direction: column;
}
.modal-header { padding: 16px 20px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; }
.modal-body { padding: 20px; overflow: auto; flex: 1; }
.modal-footer { padding: 16px 20px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 10px; }

/* Signature modal */
.sig-tabs { display: flex; gap: 8px; margin-bottom: 16px; border-bottom: 1px solid var(--line); }
.sig-tab { padding: 8px 12px; border: none; background: transparent; color: var(--muted); font-weight: 600; cursor: pointer; border-bottom: 2px solid transparent; }
.sig-tab.active { color: var(--brand); border-bottom-color: var(--brand); }
.sig-preview { border: 1px dashed var(--line); border-radius: var(--radius); min-height: 120px; display: grid; place-items: center; background: var(--bg); margin: 12px 0; }
.sig-preview svg, .sig-preview img, .sig-preview canvas { max-width: 100%; max-height: 120px; }

.font-choices { display: flex; gap: 6px; flex-wrap: wrap; }
.font-choice { padding: 6px 10px; border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--panel); cursor: pointer; font-size: 16px; }
.font-choice.active { border-color: var(--brand); color: var(--brand); background: var(--brand-soft); }

.draw-canvas { border: 1px solid var(--line); border-radius: var(--radius); touch-action: none; cursor: crosshair; width: 100%; height: 140px; background: #fff; }

/* Toasts */
.toast-stack { position: fixed; bottom: 20px; right: 20px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: var(--ink); color: #fff; padding: 12px 16px; border-radius: var(--radius); box-shadow: var(--shadow-lg); font-size: 13px;
  display: flex; align-items: center; gap: 10px; animation: toastIn .2s ease;
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

@keyframes toastIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

/* Audit / certificate */
.timeline { position: relative; padding-left: 24px; }
.timeline::before { content: ""; position: absolute; left: 7px; top: 6px; bottom: 6px; width: 2px; background: var(--line); }
.event { position: relative; margin-bottom: 18px; }
.event::before { content: ""; position: absolute; left: -22px; top: 4px; width: 10px; height: 10px; border-radius: 50%; background: var(--brand); border: 2px solid var(--panel); }
.event-time { font-size: 11px; color: var(--subtle); }
.event-title { font-weight: 700; font-size: 14px; }
.event-meta { font-size: 12px; color: var(--muted); }

.certificate { border: 1px solid var(--line); padding: 40px; border-radius: var(--radius); background: #fff; max-width: 760px; margin: 0 auto; }
.certificate h2 { text-align: center; margin-bottom: 24px; }
.cert-row { display: flex; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--line); font-size: 13px; }

/* Print / download preview */
.print-page { width: 612px; min-height: 792px; background: #fff; margin: 0 auto 24px; padding: 54px; box-shadow: var(--shadow); page-break-after: always; }

/* Mobile */
@media (max-width: 1020px) {
  .prepare-layout { grid-template-columns: 160px 1fr 240px; }
  .recipient-card { grid-template-columns: 24px 1fr 1fr; }
}
@media (max-width: 820px) {
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; z-index: 60; transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); box-shadow: var(--shadow-lg); }
  .topbar .burger { display: block; }
  .prepare-layout { grid-template-columns: 1fr; height: auto; }
  .prepare-panel { overflow: visible; }
  .canvas-area { order: -1; min-height: 500px; }
  .signer-toolbar { flex-direction: column; align-items: stretch; }
  .recipient-card { grid-template-columns: 1fr; }
}

/* Accessibility */
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
