:root {
  color-scheme: dark;
  --bg: #0b1117;
  --panel: #111b26;
  --card: #162231;
  --line: #26374a;
  --text: #e8eef6;
  --muted: #91a2b7;
  --accent: #54a7ff;
  --accent-2: #67e8a5;
  --warn: #ffd166;
  --danger: #ff6b7a;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
  font-family: Inter, "Segoe UI", system-ui, -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
}

button,
textarea,
input {
  font: inherit;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.25rem;
  background: rgba(17, 27, 38, 0.96);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
  z-index: 10;
}

.topbar h1 {
  margin: 0;
  font-size: 1.25rem;
}

.topbar p {
  margin: 0.2rem 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-left: auto;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.status-line {
  margin: 0;
  max-width: min(28rem, 42vw);
  font-size: 0.82rem;
  line-height: 1.35;
  color: var(--warn);
  text-align: right;
}

.actions,
.row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.wrap {
  flex-wrap: wrap;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 0.55rem;
  background: #1c2a3a;
  color: var(--text);
  padding: 0.6rem 0.85rem;
  min-height: 2.75rem;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.button:hover:not(:disabled) {
  border-color: var(--accent);
}

.button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.button.primary {
  background: #173f68;
  border-color: var(--accent);
}

.button.accent {
  background: var(--accent-2);
  border-color: var(--accent-2);
  color: #07100b;
  font-weight: 700;
}

.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 390px;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.preview-panel {
  min-width: 0;
  min-height: 0;
  background: #070b10;
  overflow: auto;
  padding: 1rem;
}

.side-panel {
  border-left: 1px solid var(--line);
  background: var(--panel);
  padding: 1rem;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 0.8rem;
  padding: 1rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow);
}

.card h2 {
  margin: 0 0 0.65rem;
  font-size: 1rem;
}

.hint,
.muted,
.status {
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.45;
}

textarea,
input[type="text"],
input[type="number"],
select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 0.55rem;
  background: #0c141d;
  color: var(--text);
  padding: 0.65rem;
}

textarea {
  resize: vertical;
}

.empty {
  max-width: 42rem;
  margin: 8vh auto;
  text-align: left;
  color: var(--muted);
}

.empty .eyebrow {
  margin: 0 0 0.55rem;
  color: var(--accent-2);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.empty h2 {
  margin: 0;
  color: var(--text);
  font-size: clamp(2rem, 4vw, 3.2rem);
  line-height: 1.05;
}

.empty-lead {
  margin: 1rem 0 1.25rem;
  color: #c9d6e5;
  font-size: 1.08rem;
  line-height: 1.55;
}

.empty-steps {
  display: grid;
  gap: 0.75rem;
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: steps;
}

.empty-steps li {
  counter-increment: steps;
  position: relative;
  padding: 0.85rem 0.95rem 0.85rem 3.25rem;
  border: 1px solid var(--line);
  border-radius: 0.75rem;
  background: rgba(22, 34, 49, 0.72);
  line-height: 1.45;
}

.empty-steps li::before {
  content: counter(steps);
  position: absolute;
  left: 0.95rem;
  top: 0.85rem;
  display: grid;
  width: 1.45rem;
  height: 1.45rem;
  place-items: center;
  border-radius: 999px;
  background: var(--accent);
  color: #06111b;
  font-size: 0.8rem;
  font-weight: 800;
}

.empty-steps strong {
  display: block;
  margin-bottom: 0.15rem;
  color: var(--text);
}

.empty-note {
  margin: 1.25rem 0 0;
  font-size: 0.92rem;
  line-height: 1.5;
}

.pages {
  display: grid;
  gap: 1.25rem;
  justify-items: center;
}

.page {
  position: relative;
  width: 100%;
  max-width: 100%;
  background: white;
  box-shadow: var(--shadow);
}

.page canvas {
  display: block;
  max-width: 100%;
  height: auto;
}

.page-label {
  position: absolute;
  top: 0.5rem;
  left: 0.5rem;
  padding: 0.2rem 0.45rem;
  border-radius: 0.35rem;
  background: rgba(11, 17, 23, 0.78);
  color: #e8eef6;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  pointer-events: none;
  z-index: 2;
}

.overlay {
  position: absolute;
  inset: 0;
}

.field-box {
  position: absolute;
  border: 1px solid rgba(84, 167, 255, 0.85);
  background: rgba(84, 167, 255, 0.12);
  color: #06111b;
  cursor: grab;
  touch-action: none;
  overflow: visible;
}

.field-box.filled {
  border-color: rgba(24, 184, 111, 0.95);
  background: rgba(103, 232, 165, 0.18);
}

.field-box.selected {
  border-width: 2px;
  box-shadow: 0 0 0 2px rgba(84, 167, 255, 0.28);
}

.field-value-preview {
  position: absolute;
  inset: 0;
  padding: 1px 3px;
  color: #0a0a0a;
  font-size: clamp(8px, 1.5vw, 18px);
  line-height: 1.05;
  pointer-events: none;
  overflow: hidden;
}

.field-value-preview img,
.field-value-preview.signature-img,
.field-value-preview .signature-svg,
.signature-surface .signature-svg {
  width: 100%;
  height: 100%;
  display: block;
  padding: 0;
}

.field-value-preview img,
.field-value-preview.signature-img {
  object-fit: contain;
  object-position: left center;
}

.field-value-preview .signature-svg,
.signature-surface .signature-svg {
  overflow: visible;
  shape-rendering: geometricPrecision;
  text-rendering: geometricPrecision;
}

#sig-saved-preview .signature-svg {
  max-height: 5rem;
}

.sig-badge {
  display: inline-block;
  margin-top: 0.4rem;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
}

.sig-badge[data-kind="vector"] {
  background: rgba(103, 232, 165, 0.16);
  color: var(--accent-2);
  border: 1px solid rgba(103, 232, 165, 0.4);
}

.sig-badge[data-kind="legacy"] {
  background: rgba(255, 209, 102, 0.14);
  color: var(--warn);
  border: 1px solid rgba(255, 209, 102, 0.4);
}

.signature-tabs .button[data-active="1"] {
  border-color: var(--accent);
  background: rgba(84, 167, 255, 0.16);
}

.signature-surface {
  min-height: 5.5rem;
  margin-top: 0.35rem;
  border: 1px dashed var(--line);
  border-radius: 0.45rem;
  background: #fff;
  color: #111;
  display: grid;
  place-items: center;
  overflow: hidden;
}

.signature-surface.muted-surface {
  background: rgba(255, 255, 255, 0.04);
  color: var(--muted);
  font-size: 0.82rem;
  border-style: solid;
}

#sig-type-preview {
  min-height: 4.5rem;
  padding: 0.35rem 0.75rem;
  justify-content: flex-start;
  font-size: 2rem;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
}

.signature-pad {
  padding: 0;
  display: block;
}

.signature-pad canvas {
  width: 100%;
  height: auto;
  touch-action: none;
  cursor: crosshair;
  display: block;
  border: 0;
  background: transparent;
}

#sig-saved-preview img {
  max-width: 100%;
  max-height: 5rem;
  object-fit: contain;
}

.resize-handle {
  position: absolute;
  right: -8px;
  bottom: -8px;
  width: 18px;
  height: 18px;
  border-radius: 999px;
  background: var(--accent);
  border: 2px solid #06111b;
  cursor: nwse-resize;
}

.field-list {
  display: grid;
  gap: 0.45rem;
  max-height: 42vh;
  overflow: auto;
}

.field-item {
  border: 1px solid var(--line);
  border-radius: 0.55rem;
  background: #101927;
  color: var(--text);
  padding: 0.6rem;
  text-align: left;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.field-item:hover,
.field-item.selected {
  border-color: var(--accent);
}

.field-item strong {
  display: block;
  font-size: 0.9rem;
}

.field-item span {
  display: block;
  color: var(--muted);
  font-size: 0.75rem;
  margin-top: 0.2rem;
}

.meter {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 0.82rem;
  margin-bottom: 0.65rem;
}

.field-editor {
  display: grid;
  gap: 0.6rem;
}

.grid2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.label {
  display: grid;
  gap: 0.25rem;
  color: var(--muted);
  font-size: 0.78rem;
}

.notes {
  margin-top: 0.75rem;
  padding: 0.65rem;
  background: #0c141d;
  border-left: 3px solid var(--accent-2);
  border-radius: 0.4rem;
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.45;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 1rem;
  transform: translateX(-50%);
  background: #1c2a3a;
  border: 1px solid var(--line);
  border-left: 4px solid var(--accent);
  color: var(--text);
  padding: 0.75rem 1rem;
  border-radius: 0.65rem;
  box-shadow: var(--shadow);
  z-index: 50;
}

@media (max-width: 900px) {
  body {
    height: auto;
    min-height: 100dvh;
    overflow-x: hidden;
    overflow-y: auto;
    padding-bottom: env(safe-area-inset-bottom, 0);
  }

  .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 0.75rem;
    padding: 0.85rem 1rem;
    padding-top: max(0.85rem, env(safe-area-inset-top, 0));
  }

  .topbar h1 {
    font-size: 1.1rem;
  }

  .topbar p {
    font-size: 0.84rem;
  }

  .topbar-right {
    flex-direction: column;
    align-items: stretch;
    width: 100%;
    margin-left: 0;
    gap: 0.65rem;
  }

  .status-line {
    max-width: none;
    text-align: left;
  }

  .topbar,
  .app {
    display: block;
  }

  .app {
    overflow: visible;
    min-height: auto;
  }

  .preview-panel {
    min-height: auto;
    overflow: visible;
    padding: 0.65rem;
    -webkit-overflow-scrolling: touch;
  }

  .side-panel {
    min-height: auto;
    overflow: visible;
    border-left: 0;
    border-top: 1px solid var(--line);
    padding: 0.85rem;
    padding-bottom: max(0.85rem, env(safe-area-inset-bottom, 0));
  }

  .actions {
    margin-top: 0;
    flex-wrap: wrap;
    width: 100%;
    gap: 0.45rem;
  }

  .actions .button {
    flex: 1 1 calc(50% - 0.25rem);
    min-width: 0;
    font-size: 0.88rem;
    padding: 0.65rem 0.55rem;
  }

  .card {
    padding: 0.85rem;
    margin-bottom: 0.85rem;
  }

  .field-list {
    max-height: none;
  }

  .grid2 {
    grid-template-columns: 1fr;
  }

  .empty {
    margin: 6vh auto;
    padding: 0 1rem;
  }

  .toast {
    left: 1rem;
    right: 1rem;
    bottom: max(1rem, env(safe-area-inset-bottom, 0));
    transform: none;
    max-width: none;
  }

  textarea,
  input[type="text"],
  input[type="number"],
  select {
    font-size: 16px;
  }
}

@media (max-width: 900px) and (pointer: coarse) {
  .resize-handle {
    right: -12px;
    bottom: -12px;
    width: 28px;
    height: 28px;
  }

  .field-item {
    min-height: 2.75rem;
    padding: 0.75rem;
  }
}

@media (max-width: 480px) {
  .topbar p {
    display: none;
  }

  .actions .button {
    flex: 1 1 100%;
  }

  .row.wrap .button {
    flex: 1 1 calc(50% - 0.25rem);
  }
}
