:root {
  --sig: #2563eb;
  --date: #16a34a;
  --init: #ea580c;
  --title: #9333ea;
  --bg: #f6f6f7;
  --fg: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --active: #ef4444;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

#signbar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: white;
  border-bottom: 1px solid var(--border);
  padding: 12px 14px;
  padding-top: max(12px, env(safe-area-inset-top));
  display: flex;
  align-items: center;
  gap: 10px;
}

#progress {
  flex: 1;
  font-size: 14px;
  font-weight: 500;
  color: var(--fg);
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.zoom-group {
  display: flex;
  gap: 6px;
}

button {
  font: inherit;
  cursor: pointer;
  -webkit-appearance: none;
}

button.zoom {
  font-weight: 500;
  border: 1px solid var(--border);
  background: white;
  color: var(--fg);
  border-radius: 8px;
  font-size: 20px;
  line-height: 1;
  width: 38px;
  height: 38px;
  padding: 0;
}

button[hidden] {
  display: none !important;
}

/* Big sticky-bottom SIGN button — the single primary action */
#action-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 90;
  background: linear-gradient(
    to top,
    rgba(255, 255, 255, 0.98) 60%,
    rgba(255, 255, 255, 0)
  );
  padding: 16px 16px max(16px, env(safe-area-inset-bottom));
  display: flex;
  justify-content: center;
  pointer-events: none;
}

.sign-action {
  pointer-events: auto;
  width: 100%;
  max-width: 540px;
  min-height: 64px;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: white;
  background: #16a34a;
  border: none;
  border-radius: 16px;
  box-shadow: 0 8px 26px color-mix(in srgb, #16a34a 55%, transparent);
  transition: transform 0.1s ease, box-shadow 0.2s ease, background 0.2s ease;
}

.sign-action:active {
  transform: scale(0.98);
  box-shadow: 0 4px 14px color-mix(in srgb, #16a34a 40%, transparent);
}

.sign-action:disabled {
  opacity: 0.6;
}

/* Download mode: distinctly different — outlined pill in slate-blue so it's
 * obvious the action has changed from "act now" to "grab your copy".
 */
.sign-action.download-mode {
  background: white;
  color: #1d4ed8;
  border: 2px solid #1d4ed8;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: none;
  min-height: 52px;
  box-shadow: 0 4px 14px rgba(29, 78, 216, 0.18);
}

.sign-action.download-mode:active {
  background: #eff6ff;
  box-shadow: 0 2px 8px rgba(29, 78, 216, 0.15);
}

/* Make room at the bottom of the document for the action bar */
main#pages-outer {
  padding-bottom: 110px;
}

main#pages-outer {
  padding: 14px 12px 60px;
  overflow-x: auto;
}

#pages {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  transform-origin: top center;
  transition: transform 0.18s ease;
}

.page {
  position: relative;
  background: white;
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.12);
  border-radius: 2px;
}

.page canvas {
  display: block;
  border-radius: 2px;
}

/* --- Field overlays --- */

.field {
  position: absolute;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: background 0.15s;
}

.field-signature { color: var(--sig); }
.field-date      { color: var(--date); }
.field-initials  { color: var(--init); }
.field-title     { color: var(--title); }
.field-name      { color: #db2777; }
.field-address   { color: #0891b2; }
.field-city_state { color: #65a30d; }
.field-commission_expires { color: var(--date); }
.field-commission_number  { color: var(--title); }

.field:hover {
  background: color-mix(in srgb, currentColor 8%, transparent);
}

.field.non-interactive {
  cursor: default;
  pointer-events: none;
}

.field.non-interactive:hover {
  background: transparent;
}

.field-underline {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 0;
  border-bottom: 2.5px dashed currentColor;
  pointer-events: none;
  border-radius: 1px;
}

.field.active {
  color: var(--active);
  background: color-mix(in srgb, var(--active) 10%, transparent);
  animation: pulse 1.4s ease-in-out infinite;
}

.field.active .field-underline {
  border-bottom-width: 3px;
}

@keyframes pulse {
  0%, 100% { background: color-mix(in srgb, var(--active) 8%, transparent); }
  50%      { background: color-mix(in srgb, var(--active) 18%, transparent); }
}

.field.filled {
  background: transparent;
  animation: none;
}

.field.filled .field-underline,
.field.filled .field-label {
  display: none;
}

.field-label {
  position: absolute;
  top: -18px;
  left: 0;
  background: white;
  padding: 1px 6px;
  border-radius: 3px;
  border: 1px solid currentColor;
  font-size: 10px;
  font-weight: 600;
  pointer-events: none;
  line-height: 1.2;
  white-space: nowrap;
}

.field-value {
  position: absolute;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0 2px;
  pointer-events: none;
  overflow: hidden;
}

.field-value img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  object-position: bottom center;
}

.field-value-text {
  font-family: "Caveat", "Snell Roundhand", "Brush Script MT", cursive;
  font-weight: 500;
  color: #111827;
  white-space: nowrap;
  line-height: 1;
}

.field-date .field-value-text,
.field-initials .field-value-text,
.field-title .field-value-text {
  font-family: "Times New Roman", Times, serif;
  font-weight: 500;
}

/* --- Modals --- */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  z-index: 200;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 12px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
}

.modal[hidden] {
  display: none;
}

.modal-card {
  background: white;
  border-radius: 16px;
  width: 100%;
  max-width: 540px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
}

.modal-title {
  font-size: 18px;
  font-weight: 600;
}

.modal-subtitle {
  margin-top: -8px;
  font-size: 13px;
  color: var(--muted);
}

#signature-canvas {
  width: 100%;
  height: 220px;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 10px;
  touch-action: none;
  cursor: crosshair;
}

#text-input {
  font: inherit;
  font-size: 18px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 100%;
}

.modal-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.modal-actions .spacer {
  flex: 1;
}

.modal-actions button {
  font-weight: 500;
  border: 1px solid var(--border);
  background: white;
  color: var(--fg);
  border-radius: 10px;
  padding: 10px 16px;
  min-height: 42px;
}

.modal-actions button.primary {
  background: var(--fg);
  color: white;
  border-color: var(--fg);
}

.modal-actions button.ghost {
  background: white;
  color: var(--muted);
}

/* Toast: appears briefly after signing completes, then fades out so the
 * document stays accessible for review.
 */
.toast {
  position: fixed;
  top: max(20px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 300;
  background: var(--date);
  color: white;
  padding: 12px 16px;
  border-radius: 14px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.22);
  display: inline-flex;
  align-items: center;
  gap: 12px;
  font-size: 15px;
  font-weight: 500;
  animation: toastIn 0.32s cubic-bezier(0.2, 0.7, 0.3, 1);
  max-width: calc(100vw - 32px);
}

.toast[hidden] {
  display: none !important;
}

.toast.fading {
  animation: toastOut 0.5s ease-in forwards;
}

@keyframes toastIn {
  from {
    opacity: 0;
    transform: translate(-50%, -16px);
  }
  to {
    opacity: 1;
    transform: translate(-50%, 0);
  }
}

@keyframes toastOut {
  to {
    opacity: 0;
    transform: translate(-50%, -16px);
  }
}

.toast-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.25);
  font-size: 14px;
  font-weight: 700;
}

.toast-text {
  white-space: nowrap;
}

.toast-action {
  background: white;
  color: var(--date);
  border: none;
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 14px;
  font-weight: 600;
}

.toast-action:active {
  transform: scale(0.97);
}
