:root {
  color-scheme: light;
  --bg: #f5f7f9;
  --surface: #ffffff;
  --line: #d8dee6;
  --text: #17202a;
  --muted: #657181;
  --blue: #174a7c;
  --blue-strong: #0d3966;
  --green: #1f7a4d;
  --warning: #a76114;
  --shadow: 0 18px 45px rgba(23, 32, 42, 0.08);
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.topbar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 22px 32px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 24px;
  line-height: 1.2;
}

h2 {
  font-size: 16px;
  line-height: 1.2;
}

.status-pill {
  min-width: 170px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  background: #fbfcfd;
}

.status-pill.ready {
  border-color: rgba(31, 122, 77, 0.25);
  color: var(--green);
  background: #f0f8f3;
}

.status-pill.error {
  border-color: rgba(167, 97, 20, 0.25);
  color: var(--warning);
  background: #fff7ed;
}

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(420px, 560px) minmax(420px, 1fr);
  gap: 18px;
  padding: 18px;
}

.panel {
  min-height: 0;
  height: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.form-panel {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.preview-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}

.form-panel .section-title {
  flex-shrink: 0;
  margin-bottom: 0;
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--line);
}

.section-title span {
  color: var(--muted);
  font-size: 13px;
}

form {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.form-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: grid;
  gap: 14px;
  align-content: start;
  padding: 18px 20px;
}

.grid {
  display: grid;
  gap: 12px;
}

.grid.two {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.grid.three {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.field {
  display: grid;
  gap: 7px;
}

.field span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 11px;
  color: var(--text);
  background: #fff;
  font: inherit;
  font-size: 14px;
  outline: none;
}

textarea {
  min-height: 48px;
  resize: vertical;
}

input[readonly] {
  color: var(--muted);
  background: #eef2f6;
  cursor: not-allowed;
}

.signature-preview {
  width: 170px;
  max-width: 100%;
  height: 72px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  padding: 8px;
}

.signature-tools {
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcfd;
}

.signature-guide-panel {
  display: grid;
  gap: 8px;
}

.signature-guide-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.signature-guide-header span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.page-switch {
  display: inline-flex;
  gap: 4px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.page-switch-button {
  min-height: 28px;
  border: 0;
  border-radius: 4px;
  padding: 0 9px;
  color: var(--muted);
  background: transparent;
  font-size: 12px;
}

.page-switch-button.active {
  color: #fff;
  background: var(--blue);
}

.signature-guide {
  position: relative;
  overflow: hidden;
  width: 100%;
  aspect-ratio: 792 / 612;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #eef2f6;
}

.signature-guide > img:first-child {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  user-select: none;
}

#signatureGuideOverlay {
  position: absolute;
  display: none;
  cursor: move;
  touch-action: none;
  user-select: none;
  outline: 1px dashed rgba(23, 74, 124, 0.55);
  outline-offset: 2px;
}

.signature-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(23, 74, 124, 0.12);
}

.actions {
  flex-shrink: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid var(--line);
  background: var(--surface);
}

button,
.secondary-button {
  min-height: 40px;
  border-radius: 6px;
  padding: 0 14px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  transition: transform 160ms cubic-bezier(0.23, 1, 0.32, 1), background 160ms ease, border-color 160ms ease;
}

button:active,
.secondary-button:active {
  transform: scale(0.97);
}

.primary-button {
  border: 1px solid var(--blue-strong);
  color: #fff;
  background: var(--blue);
}

@media (hover: hover) and (pointer: fine) {
  .primary-button:hover {
    background: var(--blue-strong);
  }

  .secondary-button:hover,
  .ghost-button:hover {
    border-color: var(--blue);
  }
}

.primary-button:disabled {
  cursor: wait;
  opacity: 0.65;
}

.secondary-button,
.ghost-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  color: var(--blue);
  background: #fff;
  text-decoration: none;
}

.ghost-button {
  min-height: 34px;
  font-size: 13px;
}

.summary {
  display: grid;
  grid-template-columns: 1.2fr 0.7fr 1fr;
  gap: 1px;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: var(--line);
}

.summary div {
  min-width: 0;
  padding: 13px 14px;
  background: #fbfcfd;
}

.summary span {
  display: block;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.summary strong {
  display: block;
  overflow: hidden;
  font-size: 14px;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preview-empty {
  display: grid;
  flex: 1;
  place-items: center;
  color: var(--muted);
  background: #eef2f6;
}

.pdf-preview {
  flex: 1;
  width: 100%;
  min-height: 580px;
  border: 0;
  background: #eef2f6;
}

.hidden {
  display: none !important;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

/* ---------- Bandeja "Para enviar" ---------- */
.outbox {
  flex-shrink: 0;
  margin: 0 20px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfd;
}

.outbox-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
  font-size: 13px;
}

.outbox-list {
  display: grid;
  gap: 6px;
  max-height: 168px;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  list-style: none;
}

.outbox-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-width: 0;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.outbox-item a {
  overflow: hidden;
  color: var(--blue);
  font-size: 12px;
  text-decoration: none;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.outbox-remove {
  flex-shrink: 0;
  min-height: 26px;
  min-width: 26px;
  padding: 0;
  color: var(--muted);
  border: 1px solid var(--line);
  background: #fff;
  font-size: 13px;
  line-height: 1;
}

.whatsapp-button {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid #1da851;
  color: #fff;
  background: #25d366;
}

.whatsapp-button .wa-icon {
  flex-shrink: 0;
}

.whatsapp-button:disabled {
  cursor: wait;
  opacity: 0.65;
}

@media (hover: hover) and (pointer: fine) {
  .whatsapp-button:hover {
    background: #1ebe5d;
  }

  .outbox-remove:hover {
    border-color: #b23b3b;
    color: #b23b3b;
  }
}

/* ---------- Modal de ajustes ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(23, 32, 42, 0.45);
}

.modal {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 620px;
  max-height: 90vh;
  border-radius: 10px;
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
}

.tabs {
  display: flex;
  gap: 4px;
  padding: 10px 16px 0;
  border-bottom: 1px solid var(--line);
}

.tab {
  min-height: 36px;
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  padding: 0 12px;
  color: var(--muted);
  background: transparent;
  font-size: 13px;
}

.tab.active {
  color: var(--blue);
  border-bottom-color: var(--blue);
}

.modal-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px 20px;
}

.settings-list {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 8px;
  margin: 0 0 16px;
  padding: 0;
  list-style: none;
}

.settings-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcfd;
}

.settings-item-text {
  flex: 1;
  min-width: 0;
}

.settings-item-text strong {
  display: block;
  font-size: 13px;
  line-height: 1.3;
  overflow-wrap: anywhere;
}

.settings-item-text span {
  display: -webkit-box;
  overflow: hidden;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
  overflow-wrap: anywhere;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.settings-item-actions {
  display: flex;
  flex-shrink: 0;
  gap: 6px;
}

.settings-empty {
  padding: 12px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.settings-form {
  display: grid;
  gap: 12px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.settings-form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.ghost-button.danger {
  color: #b23b3b;
  border-color: rgba(178, 59, 59, 0.3);
}

/* ---------- Mobile: rápido, sin previsualización ---------- */
@media (max-width: 760px) {
  .layout {
    grid-template-columns: 1fr;
    gap: 0;
    padding: 12px;
  }

  .preview-panel {
    display: none;
  }

  .panel {
    box-shadow: none;
  }

  .form-panel .section-title {
    padding: 16px;
  }

  .form-scroll {
    gap: 13px;
    padding: 16px;
  }

  /* iOS: 16px evita el auto-zoom de Safari al enfocar un campo */
  input,
  select,
  textarea {
    font-size: 16px;
    padding: 12px 12px;
  }

  /* tap targets cómodos (iOS recomienda 44px) */
  button,
  .secondary-button {
    min-height: 44px;
  }

  .ghost-button {
    min-height: 40px;
  }

  .section-title .ghost-button {
    min-height: 38px;
  }

  .actions {
    position: sticky;
    bottom: 0;
    gap: 8px;
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
    box-shadow: 0 -8px 20px rgba(23, 32, 42, 0.08);
  }

  /* botones full-width apilados, mejor alcance del pulgar */
  .actions > .primary-button,
  .actions > .whatsapp-button,
  .actions > .secondary-button {
    flex: 1 1 100%;
    width: 100%;
  }

  .outbox {
    margin: 0 16px;
  }

  .modal {
    max-height: 94vh;
  }
}

@media (max-width: 980px) {
  body {
    height: auto;
    overflow: auto;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
    gap: 12px;
    padding: calc(14px + env(safe-area-inset-top)) 16px 14px;
  }

  .topbar-actions {
    justify-content: space-between;
  }

  .status-pill {
    min-width: 0;
    flex: 1;
  }

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

  .panel {
    height: auto;
    min-height: auto;
  }

  .form-panel {
    overflow: visible;
  }

  .form-scroll {
    overflow: visible;
    min-height: auto;
  }

  .preview-panel {
    min-height: 560px;
  }
}

@media (max-width: 620px) {
  .layout {
    padding: 10px;
  }

  .grid.two,
  .grid.three,
  .summary {
    grid-template-columns: 1fr;
  }

  h1 {
    font-size: 20px;
  }

  .modal-backdrop {
    padding: 0;
  }

  .modal {
    max-width: 100%;
    max-height: 100vh;
    height: 100vh;
    border-radius: 0;
  }

  .modal-header,
  .modal-body {
    padding: 14px 16px;
  }

  .tabs {
    padding: 10px 12px 0;
  }

  .tab {
    padding: 0 8px;
    font-size: 12px;
  }

  .settings-item {
    flex-direction: column;
    align-items: stretch;
  }

  .settings-item-actions {
    justify-content: flex-end;
  }
}
