:root {
  --bg: #f5f7fb;
  --panel: #ffffff;
  --ink: #1f2937;
  --muted: #6b7280;
  --line: #e5e7eb;
  --brand: #1e3a8a;
  --brand-2: #2563eb;
  --accent: #0ea5e9;
  --danger: #dc2626;
  --ok: #16a34a;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 8px 24px rgba(16, 24, 40, .06);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "Segoe UI", Tahoma, system-ui, -apple-system, Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.45;
}

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  background: linear-gradient(135deg, var(--brand) 0%, var(--brand-2) 100%);
  color: #fff;
  box-shadow: 0 4px 14px rgba(30, 58, 138, .25);
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand-mark {
  width: 44px; height: 44px; border-radius: 12px;
  display: grid; place-items: center;
  background: rgba(255,255,255,.16);
  font-weight: 800; letter-spacing: 1px;
  backdrop-filter: blur(4px);
}
.topbar h1 { font-size: 18px; margin: 0; font-weight: 700; }
.topbar p  { margin: 2px 0 0; font-size: 12px; opacity: .85; }
.actions { display: flex; gap: 8px; }

/* Buttons */
.btn {
  appearance: none;
  border: 1px solid transparent;
  background: #fff;
  color: var(--brand);
  padding: 9px 14px;
  border-radius: 9px;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  transition: transform .05s ease, background .15s ease, box-shadow .15s ease;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn-primary {
  background: #fff;
  color: var(--brand);
  box-shadow: 0 2px 8px rgba(0,0,0,.12);
}
.btn-ghost {
  background: rgba(255,255,255,.12);
  color: #fff;
  border-color: rgba(255,255,255,.25);
}
.btn-ghost:hover { background: rgba(255,255,255,.22); }
.btn-secondary {
  background: var(--accent);
  color: #fff;
}
.btn-tiny {
  padding: 4px 8px;
  font-size: 11px;
  background: #eef2ff;
  color: var(--brand);
  border-radius: 6px;
}
.btn-tiny:hover { background: #e0e7ff; }
.btn-block { width: 100%; }

/* Layout */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 20px;
  padding: 20px 24px 60px;
  max-width: 1280px;
  margin: 0 auto;
}
@media (max-width: 960px) {
  .layout { grid-template-columns: 1fr; }
}

/* Card / Fieldset */
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  margin: 0 0 16px;
  box-shadow: var(--shadow);
}
.card.sticky { position: sticky; top: 86px; }
.card legend, .card h2 {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--brand);
  padding: 0 6px;
}
.card h2 { padding: 0; margin: 0 0 8px; }
fieldset.card { padding-top: 8px; }
.form-panel { min-width: 0; }

/* Grid helpers */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
@media (max-width: 620px) {
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
}

/* Fields */
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span {
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}
input[type="text"],
input[type="number"],
input[type="date"],
textarea, select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 10px;
  font-size: 14px;
  font-family: inherit;
  background: #fff;
  color: var(--ink);
  transition: border-color .15s, box-shadow .15s;
}
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--brand-2);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .15);
}
textarea { resize: vertical; min-height: 80px; }

/* Checks */
.checks { display: flex; flex-direction: column; gap: 6px; }
.check {
  display: inline-flex; align-items: center; gap: 8px;
  font-size: 14px;
  padding: 4px 0;
  cursor: pointer;
}
.check input { width: 16px; height: 16px; accent-color: var(--brand-2); }

/* Signatures */
.sigs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
}
@media (max-width: 720px) {
  .sigs { grid-template-columns: 1fr; }
}
.sig-slot {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  display: flex; flex-direction: column; gap: 10px;
  background: #fcfcfd;
}
.sig-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  min-height: 22px;
}
.sig-head strong { font-size: 13px; }
.sig-preview {
  background:
    linear-gradient(#fff, #fff),
    repeating-linear-gradient(45deg, #f1f5f9 0 6px, transparent 6px 12px);
  background-blend-mode: multiply;
  border: 1px solid var(--line);
  border-radius: 8px;
  height: 90px;
  display: grid;
  place-items: center;
  overflow: hidden;
}
.sig-preview svg {
  width: 100%;
  height: 100%;
  display: block;
}
.sig-preview .placeholder {
  font-size: 11px;
  color: var(--muted);
  font-style: italic;
}
.sig-name-display {
  font-size: 12px;
  color: var(--ink);
  text-align: center;
  font-weight: 600;
  padding: 4px 0;
  border-top: 1px dashed var(--line);
}
.sig-name-input {
  font-size: 12px;
  text-align: center;
  font-weight: 600;
}
.sig-locked-hint {
  font-size: 11px;
  color: var(--muted);
  text-align: center;
  background: #f1f5f9;
  padding: 6px 8px;
  border-radius: 6px;
  border: 1px dashed #cbd5e1;
}
.sig-tools {
  margin-top: 14px;
  display: flex; justify-content: flex-end;
}
.btn-danger {
  background: #fee2e2;
  color: var(--danger);
}
.btn-danger:hover { background: #fecaca; }
.btn-block { width: 100%; }

/* Fullscreen signature modal */
.sig-modal {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, .9);
  display: flex; align-items: stretch; justify-content: stretch;
  z-index: 100;
  padding: 24px;
}
.sig-modal[hidden] { display: none; }
.sig-modal-inner {
  background: #fff;
  border-radius: 16px;
  display: flex; flex-direction: column;
  width: 100%;
  max-width: 1200px;
  margin: auto;
  overflow: hidden;
  box-shadow: 0 30px 80px rgba(0,0,0,.4);
}
.sig-modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 22px;
  border-bottom: 1px solid var(--line);
  background: #fafbff;
}
.sig-modal-head h3 { margin: 0; font-size: 18px; color: var(--brand); }
.sig-modal-head .muted { margin: 2px 0 0; font-size: 12px; }
.sig-canvas-wrap {
  flex: 1;
  position: relative;
  background: #ffffff;
  display: grid;
  place-items: stretch;
  min-height: 360px;
  overflow: hidden;
}
.sig-svg {
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
  display: block;
  user-select: none;
}
.sig-baseline {
  position: absolute;
  left: 8%;
  right: 8%;
  bottom: 28%;
  border-bottom: 1.5px dashed #cbd5e1;
  pointer-events: none;
}
.sig-hint {
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  font-size: 16px;
  color: #94a3b8;
  font-style: italic;
  pointer-events: none;
  transition: opacity .2s;
}
.sig-hint.hidden { opacity: 0; }
.sig-modal-tools {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; flex-wrap: wrap;
  padding: 14px 22px;
  border-top: 1px solid var(--line);
  background: #fafbff;
}
.tool-group {
  display: flex; gap: 8px; flex-wrap: wrap;
}

/* Side panel */
.side-panel { min-width: 0; }
.muted { color: var(--muted); font-size: 12px; margin: 0 0 12px; }
.template-list {
  list-style: none;
  margin: 0 0 12px;
  padding: 0;
  max-height: 280px;
  overflow-y: auto;
}
.template-list li {
  display: flex; align-items: center; gap: 10px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-bottom: 6px;
  background: #fff;
}
.template-list li.empty {
  justify-content: center;
  color: var(--muted);
  background: #fafafa;
  font-style: italic;
}
.template-list img {
  width: 56px; height: 28px; object-fit: contain;
  background: #f8fafc; border-radius: 4px;
  border: 1px solid var(--line);
}
.template-list .meta { flex: 1; min-width: 0; }
.template-list .meta strong {
  display: block; font-size: 13px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.template-list .meta small { color: var(--muted); font-size: 11px; }
.template-list .del {
  background: transparent; border: none; color: var(--danger);
  cursor: pointer; padding: 4px 6px; border-radius: 6px;
  font-size: 12px;
}
.template-list .del:hover { background: #fee2e2; }
.tips { margin: 0; padding-left: 18px; font-size: 13px; color: var(--muted); }
.tips li { margin-bottom: 4px; }

/* Modal */
.modal {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, .55);
  display: grid; place-items: center;
  padding: 20px;
  z-index: 9999;
}
.modal[hidden] { display: none; }
.modal-card {
  background: #fff;
  border-radius: 14px;
  padding: 22px;
  width: min(480px, 100%);
  box-shadow: 0 20px 50px rgba(0,0,0,.3);
  max-height: 90vh; overflow-y: auto;
}
.modal-card h3 { margin: 0 0 12px; font-size: 16px; color: var(--brand); }
.modal-foot {
  display: flex; justify-content: flex-end; gap: 8px;
  margin-top: 16px;
}
.save-source { display: flex; flex-direction: column; gap: 10px; }
.save-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 12px;
  margin-bottom: 4px;
}
@media (max-width: 480px) {
  .save-grid { grid-template-columns: 1fr; }
}

/* Copy row */
.copy-row {
  display: flex; gap: 6px;
}
.copy-row input {
  flex: 1;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 12px;
}

/* Toast */
.toast {
  position: fixed;
  left: 50%; bottom: 28px;
  transform: translateX(-50%);
  background: #111827;
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 300;
  box-shadow: 0 10px 30px rgba(0,0,0,.25);
  opacity: 0;
  transition: opacity .2s ease, transform .2s ease;
  pointer-events: none;
}
.toast[hidden] { display: none; }
.toast.show { opacity: 1; transform: translate(-50%, -4px); }
.toast.error { background: var(--danger); }
.toast.ok    { background: var(--ok); }
