* {
  box-sizing: border-box;
}

:root {
  --bg: #faf9f7;
  --surface: #ffffff;
  --border: #e8e6e3;
  --muted: #6b6b6b;
  --text: #1a1a1a;
  --primary: #1a1a1a;
  --primary-hover: #333;
  --accent: #2d7d7d;
  --accent-soft: #e8f2f2;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
  --shadow-hover: 0 4px 12px rgba(0, 0, 0, 0.06);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  margin: 0;
  padding: 2rem 1rem;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.app {
  max-width: 1280px;
  margin: 0 auto;
}

header {
  text-align: center;
  margin-bottom: 2.5rem;
}

header h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.subtitle {
  margin: 0.5rem 0 0;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 400;
}

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

.card h2 {
  margin: 0 0 1.25rem;
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.hidden {
  display: none !important;
}

/* Step 1: Upload */
.upload-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

@media (max-width: 520px) {
  .upload-row {
    grid-template-columns: 1fr;
  }
}

.upload-box {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 1.25rem;
  border: 1.5px dashed var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s, box-shadow 0.2s;
}

.upload-box:hover {
  border-color: var(--muted);
  background: var(--surface);
}

.upload-box.has-file {
  border-style: solid;
  border-color: var(--accent);
  background: var(--accent-soft);
}

.upload-label {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

/* Visible file input so the browser always shows the chosen file name */
.upload-box input[type="file"].file-input-native {
  position: static;
  width: 100%;
  height: auto;
  opacity: 1;
  padding: 0.5rem 0;
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--text);
  background: transparent;
  border: none;
}
.upload-box input[type="file"].file-input-native::-webkit-file-upload-button {
  padding: 0.4rem 0.75rem;
  margin-right: 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 0.85rem;
}
.upload-box input[type="file"].file-input-native::file-selector-button {
  padding: 0.4rem 0.75rem;
  margin-right: 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 0.85rem;
}

.file-name {
  font-size: 0.85rem;
  color: var(--text);
  word-break: break-all;
}

.upload-box:not(.has-file) .file-name {
  color: var(--muted);
  font-style: italic;
}

.step-upload .btn-primary {
  display: block;
  width: 100%;
  padding: 0.85rem 1.5rem;
  font-size: 1rem;
  font-weight: 500;
  border-radius: var(--radius);
  transition: background 0.2s, transform 0.1s, box-shadow 0.2s;
}

.btn {
  padding: 0.6rem 1.2rem;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  cursor: pointer;
  font-weight: 500;
}

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

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
  box-shadow: var(--shadow-hover);
}

.btn-primary:active:not(:disabled) {
  transform: translateY(0.5px);
}

.btn-primary:disabled {
  background: #c4c4c4;
  color: #fff;
  cursor: not-allowed;
  box-shadow: none;
}

.btn-primary.ready {
  background: var(--accent);
}

.btn-primary.ready:hover:not(:disabled) {
  background: #246;
}

.error-message {
  margin: 0 0 1rem;
  padding: 0.75rem 1rem;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: var(--radius-sm);
  color: #b91c1c;
  font-size: 0.9rem;
}

.error-message.hidden {
  display: none !important;
}

.step-tip {
  margin: 1rem 0 0;
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.5;
}

.step-tip code {
  padding: 0.15rem 0.35rem;
  background: var(--bg);
  border-radius: 4px;
  font-size: 0.85em;
}

/* Output / Generate settings */
.output-settings-wrap {
  margin-top: 1rem;
}

.btn-settings-toggle {
  margin-bottom: 0.5rem;
}

.output-settings-panel {
  padding: 1rem 0;
  border-top: 1px solid var(--border);
}

.output-settings-panel.hidden {
  display: none;
}

.output-settings-panel h4 {
  margin: 0 0 0.5rem;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.output-settings-hint {
  margin: 0 0 0.75rem;
  font-size: 0.85rem;
  color: var(--muted);
  line-height: 1.4;
}

.output-settings-row {
  margin-bottom: 0.75rem;
}

.output-settings-label {
  display: block;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text);
  margin-bottom: 0.35rem;
}

.output-settings-input {
  width: 100%;
  max-width: 320px;
  padding: 0.5rem 0.65rem;
  font-size: 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
}

.output-settings-input:focus {
  outline: none;
  border-color: var(--accent);
}

.output-columns-add {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 0.5rem;
}

.output-column-select {
  flex: 1;
  max-width: 240px;
  padding: 0.45rem 0.65rem;
  font-size: 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
}

.btn-add-column {
  padding: 0.45rem 0.75rem;
  font-size: 0.85rem;
  font-weight: 500;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
}

.btn-add-column:hover {
  background: var(--border);
}

.output-columns-list {
  margin: 0;
  padding: 0;
  list-style: none;
  min-height: 2rem;
}

.output-columns-list li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem 0;
  font-size: 0.85rem;
  border-bottom: 1px solid var(--border);
}

.output-columns-list li .remove-column {
  color: #b91c1c;
  cursor: pointer;
  font-size: 0.8rem;
  background: none;
  border: none;
  padding: 0 0.25rem;
}

.output-columns-list li .remove-column:hover {
  text-decoration: underline;
}

.output-preview {
  margin: 0.75rem 0 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.output-preview em {
  font-style: normal;
  color: var(--text);
}

.btn-generate {
  margin-top: 1.25rem;
  width: 100%;
  padding: 0.85rem;
  border-radius: var(--radius);
}

.btn-secondary {
  margin-top: 0.5rem;
  width: 100%;
  padding: 0.75rem;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--bg);
  border-color: var(--muted);
}

.btn-secondary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* Step 2 */
.step-map .hint {
  margin: 0 0 1.25rem;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.55;
}

.step2-buttons {
  display: flex;
  gap: 0.75rem;
  align-items: stretch;
  margin-bottom: 1rem;
}

/* Same row width as generate button; each button half size, styled like generate but secondary */
.step2-buttons .btn {
  flex: 1;
  margin: 0;
  padding: 0.85rem 1rem;
  font-size: 0.95rem;
  font-weight: 500;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  min-width: 0;
}

.step2-buttons .btn:hover:not(:disabled) {
  background: var(--bg);
  border-color: var(--muted);
  box-shadow: var(--shadow-hover);
}

.csv-data-wrap {
  margin-bottom: 1.5rem;
}

.csv-data-wrap .csv-data-title {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.map-section {
  margin-bottom: 1.5rem;
}

.map-section h3 {
  margin: 0 0 0.6rem;
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.csv-section {
  padding-bottom: 1.25rem;
  border-bottom: 1px solid var(--border);
}

.pdf-section {
  padding-top: 0.5rem;
}

/* Dropdown shown when clicking a PDF field to choose CSV column */
.pdf-field-column-dropdown {
  position: fixed;
  z-index: 1000;
  min-width: 160px;
  max-height: 280px;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-hover);
  padding: 0.25rem 0;
  font-size: 0.9rem;
}

.pdf-field-column-dropdown.hidden {
  display: none !important;
}

.pdf-field-column-dropdown-option {
  display: block;
  width: 100%;
  padding: 0.5rem 0.75rem;
  text-align: left;
  border: none;
  background: none;
  cursor: pointer;
  font-size: inherit;
  color: var(--text);
}

.pdf-field-column-dropdown-option:hover {
  background: var(--accent-soft);
}

.pdf-field-column-dropdown-option.none {
  color: var(--muted);
  border-bottom: 1px solid var(--border);
}

.csv-wrap {
  overflow: auto;
  max-height: 260px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
}

.csv-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
}

.csv-table th,
.csv-table td {
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
}

.csv-table th {
  background: var(--surface);
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  position: sticky;
  top: 0;
  z-index: 1;
}

.csv-table th:hover {
  background: var(--bg);
}

.csv-table th.selected {
  background: var(--primary);
  color: #fff;
}

.csv-meta {
  margin: 0.5rem 0 0;
  font-size: 0.8rem;
  color: var(--muted);
}

.selected-hint {
  margin: 0.6rem 0 0.75rem;
  padding: 0.75rem 1rem;
  background: var(--accent-soft);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  color: var(--text);
}

.selected-hint .next-step {
  display: block;
  margin-top: 0.25rem;
  color: var(--muted);
  font-weight: normal;
}

.map-instruction {
  margin: 0 0 0.75rem;
  font-size: 0.9rem;
  color: var(--muted);
  line-height: 1.5;
}

.pdf-view-wrap {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: auto;
  max-height: 85vh;
  min-height: 560px;
  background: #f5f5f4;
}

.pdf-canvas-wrap {
  position: relative;
  display: inline-block;
  line-height: 0;
}

/* Invisible hit areas over each form field; only the divs receive pointer events */
.pdf-field-overlays {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.pdf-field-overlay {
  position: absolute;
  pointer-events: auto;
  cursor: pointer;
  background: rgba(255, 182, 193, 0.4);
  border: 1px solid rgba(219, 112, 147, 0.5);
  border-radius: 2px;
  transition: background 0.15s ease, border-color 0.15s ease;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  padding: 2px 4px;
}

.pdf-field-overlay:hover,
.pdf-field-overlay.hover {
  background: rgba(255, 182, 193, 0.6);
  border-color: rgba(219, 112, 147, 0.8);
}

.pdf-field-overlay.tin-preview {
  padding: 1px 2px;
}

.pdf-field-preview {
  font-family: Helvetica, Arial, sans-serif;
  font-size: 18px;
  font-weight: normal;
  line-height: 1.2;
  color: #05198D;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 100%;
  min-width: 0;
  pointer-events: none;
  display: block;
}

.pdf-field-preview.has-value {
  font-weight: normal;
  color: #05198D;
}

/* Multiline fields (e.g. address): allow 2 lines, clamp with ellipsis */
.pdf-field-overlay.multiline .pdf-field-preview {
  white-space: pre-wrap;
  word-break: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  line-clamp: 2;
}

/* TIN: 9 digits in 9 equal boxes like the PDF */
.pdf-field-preview-tin {
  display: flex;
  align-items: stretch;
  justify-content: space-between;
  gap: 0;
  width: 100%;
  height: 100%;
  padding: 0 1px;
  box-sizing: border-box;
}

.pdf-field-preview-tin .tin-digit {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 18px;
  font-weight: normal;
  font-variant-numeric: tabular-nums;
  color: #05198D;
  border-right: 1px solid rgba(0, 0, 0, 0.08);
  pointer-events: none;
}

.pdf-field-preview-tin .tin-digit:last-child {
  border-right: none;
}

/* Checkbox: center checkmark, WYSIWYG */
.pdf-field-overlay[data-field-type="checkbox"] .pdf-field-preview-checkbox {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  color: var(--text);
}

.pdf-field-overlay[data-field-type="checkbox"] .pdf-field-preview-checkbox.has-value {
  color: var(--accent);
}

.pdf-fields-list-wrap {
  margin-top: 1.25rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
}

.pdf-fields-list-wrap h4 {
  margin: 0 0 0.5rem;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.selected-for-mapping {
  margin: 0 0 0.75rem;
  padding: 0.5rem 0.75rem;
  background: var(--accent-soft);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  color: var(--text);
}

.pdf-fields-list {
  margin: 0;
  padding: 0;
  list-style: none;
  max-height: 280px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
}

.pdf-field-item {
  display: flex;
  align-items: center;
  gap: 0.5rem 0.75rem;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
}

.pdf-field-item:last-child {
  border-bottom: none;
}

.pdf-field-num {
  flex-shrink: 0;
  width: 1.75rem;
  height: 1.75rem;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

.pdf-field-name {
  flex: 1;
  min-width: 0;
  font-family: ui-monospace, monospace;
  font-size: 0.8rem;
  color: var(--text);
  word-break: break-all;
}

.btn-map-here {
  flex-shrink: 0;
  padding: 0.35rem 0.65rem;
  font-size: 0.8rem;
  font-weight: 500;
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  color: var(--accent);
  cursor: pointer;
}

.btn-map-here:hover {
  background: var(--accent);
  color: #fff;
}

#pdfCanvas {
  display: block;
  max-width: 100%;
}

.mappings-list {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.mappings-list h4 {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.mappings-list ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

.mappings-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.45rem 0;
  font-size: 0.85rem;
  border-bottom: 1px solid var(--border);
}

.mappings-list li .remove {
  color: #b91c1c;
  cursor: pointer;
  font-size: 0.8rem;
  opacity: 0.9;
}

.mappings-list li .remove:hover {
  text-decoration: underline;
}

.mappings-list li.muted {
  color: var(--muted);
  font-style: italic;
}

/* Progress */
.progress-card {
  text-align: center;
}

.progress-card progress {
  width: 100%;
  height: 8px;
  margin: 0.5rem 0;
  border-radius: 4px;
  appearance: none;
  background: var(--border);
}

.progress-card progress::-webkit-progress-bar {
  background: var(--border);
  border-radius: 4px;
}

.progress-card progress::-webkit-progress-value {
  background: var(--primary);
  border-radius: 4px;
}

#progressText {
  margin: 0.25rem 0 0;
  font-size: 0.9rem;
  color: var(--muted);
}
