:root {
  --bg-deep: #1f2933;
  --bg-page: #f4f1ea;
  --bg-card: #ffffff;
  --sand: #fffcf6;
  --ink: #1c2430;
  --muted: #5b6572;
  --line: #d7d0c3;
  --accent: #b8922a;
  --accent-soft: #f3e6c0;
  --accent-ink: #1c1404;
  --danger: #b42318;
  --ok: #0f6b45;
  --radius: 14px;
  --shadow: 0 10px 28px rgba(28, 36, 48, 0.08);
  --font-display: "Cormorant Garamond", Georgia, serif;
  --font-body: "Source Sans 3", system-ui, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

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

html,
body {
  margin: 0;
  min-height: 100%;
  min-height: 100dvh;
  font-family: var(--font-body);
  color: var(--ink);
  background:
    radial-gradient(900px 420px at 8% -8%, #ffe9b8 0%, transparent 55%),
    radial-gradient(800px 380px at 100% 0%, #e8f0ff 0%, transparent 50%),
    linear-gradient(180deg, #faf7f1 0%, var(--bg-page) 45%, #ebe6dc 100%);
  overflow-x: hidden;
}

body {
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px)
    env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
  -webkit-tap-highlight-color: rgba(184, 146, 42, 0.15);
  touch-action: manipulation;
}

.hidden {
  display: none !important;
}

.login-view {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-card {
  width: min(100%, 400px);
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 28px 24px 24px;
  box-shadow: var(--shadow);
  text-align: center;
}

.logo-login {
  display: block;
  width: 132px;
  height: 132px;
  object-fit: cover;
  margin: 0 auto 10px;
  border-radius: 16px;
  background: #000;
}

.brand-sm {
  margin: 0;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--accent);
}

.brand-inline {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  color: var(--accent);
}

.login-card h1,
.topbar h1 {
  margin: 6px 0 0;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 2rem;
  line-height: 1.1;
}

.login-card label {
  text-align: left;
}

.login-sub {
  margin: 8px 0 20px;
  color: var(--muted);
}

.login-card label,
.field {
  display: grid;
  gap: 6px;
  margin-bottom: 14px;
  font-size: 0.92rem;
  font-weight: 500;
}

input,
select,
button {
  font: inherit;
}

input,
select {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 10px;
  padding: 12px 12px;
  color: var(--ink);
  font-size: 16px; /* evita zoom automático en iOS */
  line-height: 1.3;
  max-width: 100%;
}

input:focus,
select:focus {
  outline: 2px solid rgba(184, 146, 42, 0.35);
  border-color: var(--accent);
}

input.inline,
select.inline {
  width: auto;
  min-width: 8rem;
  display: inline-block;
  margin: 0 4px 6px;
  padding: 6px 8px;
  border: none;
  border-bottom: 1.5px solid var(--line);
  border-radius: 0;
  background: transparent;
}

input.inline.short {
  min-width: 5.5rem;
  max-width: 8rem;
}

input.inline.tiny {
  min-width: 3rem;
  max-width: 4rem;
}

input.inline.wide {
  min-width: min(100%, 16rem);
  width: min(100%, 18rem);
}

.btn {
  appearance: none;
  border: none;
  border-radius: 999px;
  padding: 12px 18px;
  min-height: 44px;
  cursor: pointer;
  font-weight: 600;
  -webkit-user-select: none;
  user-select: none;
}

.btn-primary {
  width: 100%;
  background: linear-gradient(180deg, #d4a93a, var(--accent));
  color: #fff;
  box-shadow: 0 6px 16px rgba(184, 146, 42, 0.28);
}

.btn-secondary {
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--line);
}

.btn-ghost {
  background: rgba(255, 255, 255, 0.7);
  color: var(--ink);
  border: 1px solid var(--line);
  padding: 8px 12px;
  font-size: 0.9rem;
}

.btn-danger {
  background: #fff;
  color: #b42318;
  border: 1px solid #f0b4ae;
}

.error {
  color: var(--danger);
  margin: 0 0 12px;
  font-size: 0.9rem;
}

.status {
  margin: 0 0 8px;
  font-size: 0.92rem;
}

.status.ok {
  color: var(--ok);
}

.status.err {
  color: var(--danger);
}

.app-view {
  max-width: 820px;
  margin: 0 auto;
  padding: 12px 12px calc(220px + env(safe-area-inset-bottom, 0px));
  width: 100%;
}

.topbar-actions {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-shrink: 0;
}

.home-view {
  padding: 8px 0 40px;
}

.home-intro {
  color: var(--ink);
  margin: 0 0 16px;
  font-size: 1.08rem;
  font-weight: 500;
}

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

.chooser-card {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--bg-card);
  color: var(--ink);
  border-radius: 16px;
  padding: 18px 16px;
  text-align: left;
  cursor: pointer;
  display: grid;
  gap: 4px;
  box-shadow: var(--shadow);
  transition: border-color 0.15s ease, transform 0.12s ease, box-shadow 0.15s ease;
}

.chooser-card:hover {
  border-color: var(--accent);
  box-shadow: 0 12px 30px rgba(184, 146, 42, 0.16);
}

.chooser-card:active {
  transform: scale(0.99);
}

.chooser-kicker {
  color: var(--accent);
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-weight: 700;
}

.chooser-card strong {
  font-family: var(--font-display);
  font-size: 1.45rem;
  font-weight: 700;
  color: var(--ink);
}

.chooser-card span:last-child {
  color: var(--muted);
  font-size: 0.95rem;
}

.chooser-admin {
  border-color: #e0c56a;
  background: linear-gradient(160deg, #fff9e8, #fff);
}

.admin-view {
  padding-bottom: 40px;
}

.admin-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
  color: var(--ink);
}

.admin-toolbar h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.6rem;
}

.admin-list {
  display: grid;
  gap: 10px;
}

.admin-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: stretch;
}

.admin-delete-inline {
  align-self: center;
  white-space: nowrap;
  padding: 10px 14px;
  min-width: 44px;
}

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

  .admin-delete-inline {
    width: 100%;
  }
}

.admin-item {
  appearance: none;
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  background: var(--bg-card);
  color: var(--ink);
  border-radius: 14px;
  padding: 14px 14px;
  cursor: pointer;
  display: grid;
  gap: 4px;
  box-shadow: var(--shadow);
}

.admin-item:hover {
  border-color: var(--accent);
}

.admin-item strong {
  font-size: 1.02rem;
}

.admin-item span {
  color: var(--muted);
  font-size: 0.9rem;
}

.admin-detail {
  margin-top: 14px;
  background: transparent;
  box-shadow: none;
  padding: 0;
}

.admin-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 14px;
}

.preview-doc {
  background: var(--sand);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 20px 18px 24px;
  color: var(--ink);
  box-shadow: var(--shadow);
}

.preview-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
  color: #555;
  font-weight: 500;
}

.preview-header img {
  width: 44px;
  height: 44px;
  border-radius: 8px;
  object-fit: cover;
  background: #000;
}

.preview-title {
  margin: 0 0 12px;
  text-align: center;
  font-size: 1.05rem;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.preview-meta {
  margin: 0 0 14px;
  line-height: 1.45;
  font-size: 0.95rem;
}

.preview-doc h4 {
  margin: 0 0 8px;
  font-size: 1rem;
}

.preview-box {
  background: #efe8da;
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 14px;
}

.preview-box pre {
  margin: 0;
  white-space: pre-wrap;
  font-family: "Courier New", Courier, monospace;
  font-size: 0.86rem;
  line-height: 1.45;
  background: transparent;
  padding: 0;
}

.preview-closing,
.preview-date {
  margin: 0 0 10px;
  text-align: justify;
  font-size: 0.95rem;
}

.preview-signs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 14px;
}

.preview-sign-col {
  min-width: 0;
}

.preview-sign-box {
  position: relative;
  min-height: 120px;
  background: #fff;
  border: 1px solid #d2c7b2;
  border-radius: 10px;
  padding: 10px;
  overflow: hidden;
}

.preview-sign-box.digi-sign {
  min-height: 120px;
}

.preview-client-sign {
  display: block;
  width: 100%;
  height: 100px;
  object-fit: contain;
}

.preview-saved {
  margin: 16px 0 0;
  color: #666056;
  font-size: 0.82rem;
}

@media (max-width: 640px) {
  .preview-signs {
    grid-template-columns: 1fr;
  }
}

@media (min-width: 720px) {
  .chooser {
    grid-template-columns: 1fr 1fr;
  }

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

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  color: var(--ink);
  margin-bottom: 14px;
  position: sticky;
  top: 0;
  z-index: 20;
  padding: 8px 0 10px;
  background: linear-gradient(180deg, rgba(250, 247, 241, 0.96), rgba(250, 247, 241, 0.88));
  backdrop-filter: blur(8px);
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.topbar-brand > div {
  min-width: 0;
}

.topbar h1 {
  font-size: clamp(1.35rem, 5vw, 2rem);
  word-break: break-word;
}

.logo-top {
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: 10px;
  background: #111;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
}

.topbar .btn-ghost {
  color: var(--ink);
}

.doc-header {
  display: grid;
  gap: 18px;
  margin-bottom: 18px;
}

.doc-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-doc {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 8px;
  background: #000;
  flex-shrink: 0;
}

.doc-brand-name {
  font-family: var(--font-body);
  font-size: 1.35rem;
  font-weight: 500;
  color: #555555;
  letter-spacing: 0.01em;
}

.agency-block {
  font-size: 0.95rem;
}

.sheet {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 14px 24px;
  box-shadow: var(--shadow);
  overflow-wrap: anywhere;
}

.controls {
  background: #faf7f0;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
}

.controls .field input,
.controls .field select {
  width: 100%;
  display: block;
  min-width: 0;
  margin: 0;
  font-size: 16px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
}

.party-form,
.date-form {
  display: grid;
  gap: 4px;
}

.party-lead {
  margin: 0 0 10px !important;
  text-align: left !important;
  font-size: 0.98rem;
  color: var(--ink);
}

.field-control {
  width: 100%;
  min-width: 0;
  display: block;
  margin: 0;
  padding: 12px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  font-size: 16px;
}

.field-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0 10px;
  min-width: 0;
}

.field-grid > .field,
.date-form .field,
.party-form .field {
  min-width: 0;
  max-width: 100%;
}

/* iOS/Android: el control nativo de hora tiende a desbordar el marco */
input[type="time"],
input[type="time"].field-control {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  box-sizing: border-box;
  -webkit-appearance: none;
  appearance: none;
}

@media (min-width: 520px) {
  .field-grid {
    grid-template-columns: 1fr 1fr;
  }

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

@media (min-width: 720px) {
  .date-grid {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }

  .sheet {
    padding: 22px 18px 28px;
  }
}

.doc-title {
  margin: 0;
  text-align: center;
  font-family: var(--font-body);
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: #111;
}

.block {
  margin-bottom: 18px;
}

.block p {
  margin: 0 0 12px;
  line-height: 1.55;
  text-align: justify;
  text-justify: inter-word;
  hyphens: auto;
}

.block .exponen {
  text-align: left;
}

.signatures .sig-label {
  text-align: center;
}

.legal {
  color: var(--muted);
  font-size: 0.95rem;
}

.exponen {
  margin: 0 0 10px;
  font-family: var(--font-display);
  font-size: 1.25rem;
}

.controls h3 {
  margin: 0 0 12px;
  font-size: 1rem;
}

.hint {
  margin: -4px 0 10px;
  color: var(--muted);
  font-size: 0.85rem;
}

.indemn-box {
  margin: 0;
  padding: 12px 14px;
  background: var(--accent-soft);
  border-left: 4px solid var(--accent);
  border-radius: 8px;
  font-weight: 600;
  color: var(--ink);
}

.indemn-inline {
  color: var(--muted);
  font-size: 0.95rem;
}

.print-only {
  display: none;
}

.signatures {
  display: grid;
  gap: 20px;
  margin-top: 8px;
}

.signatures-single {
  max-width: 420px;
  margin-left: auto;
  margin-right: auto;
}

.sig-col {
  display: grid;
  gap: 8px;
}

.sig-label {
  margin: 0;
  text-align: center;
  font-weight: 600;
}

.sig-pad-wrap {
  display: grid;
  gap: 6px;
}

.sig-pad {
  display: block;
  width: 100%;
  height: 168px;
  max-width: 100%;
  touch-action: none;
  -ms-touch-action: none;
  background: #fff;
  border: 1.5px dashed var(--accent);
  border-radius: 12px;
  cursor: crosshair;
  box-shadow: inset 0 0 0 1px rgba(184, 146, 42, 0.08);
}

.sig-pad-hint {
  margin: 0;
  font-size: 0.82rem;
  color: var(--muted);
  text-align: center;
}

.digi-sign {
  position: relative;
  min-height: 140px;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 14px 12px 12px 16px;
  overflow: hidden;
}

.digi-sign-mark {
  position: absolute;
  left: 4px;
  top: 20px;
  width: 68px;
  height: 68px;
  object-fit: contain;
  opacity: 0.95;
  pointer-events: none;
  user-select: none;
}

.digi-sign-text {
  position: relative;
  z-index: 1;
  margin: 0 0 0 62px;
  font-family: "Courier New", Courier, monospace;
  font-size: 11.5px;
  line-height: 1.35;
  color: #111;
  white-space: pre-wrap;
  word-break: break-word;
}

@media (max-width: 520px) {
  .sig-pad {
    height: 190px;
  }

  .digi-sign {
    min-height: 120px;
    padding: 10px 8px 10px 10px;
  }

  .digi-sign-mark {
    width: 48px;
    height: 48px;
    top: 16px;
  }

  .digi-sign-text {
    margin-left: 48px;
    font-size: 10px;
  }

  .doc-title {
    font-size: 1rem;
  }

  .agency-block,
  .block p,
  .legal {
    text-align: left;
    hyphens: manual;
  }
}

.sig-auto-hint {
  margin: 0;
  font-size: 0.78rem;
  color: var(--muted);
  text-align: center;
}

.actions {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
  background: rgba(255, 252, 246, 0.97);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  display: grid;
  gap: 8px;
  box-shadow: 0 -8px 24px rgba(28, 36, 48, 0.08);
}

body.kbd-open .actions {
  display: none;
}

body.kbd-open .app-view {
  padding-bottom: 24px;
}

.actions .status.ok {
  color: var(--ok);
}

.actions .status.err {
  color: var(--danger);
}

.actions .btn {
  width: 100%;
  min-height: 46px;
  font-size: 1rem;
}

@media (min-width: 720px) {
  .signatures:not(.signatures-single) {
    grid-template-columns: 1fr 1fr;
  }

  .actions {
    max-width: 820px;
    margin: 0 auto;
    grid-template-columns: 1fr 1fr 1fr;
    background: transparent;
    position: static;
    padding: 16px 0 24px;
    box-shadow: none;
    border-top: none;
  }

  body.kbd-open .actions {
    display: grid;
  }

  .app-view {
    padding-bottom: 40px;
  }

  .actions .status {
    grid-column: 1 / -1;
  }
}

@media print {
  @page {
    size: A4;
    margin: 10mm;
  }

  body {
    background: #fff;
    padding: 0;
    font-size: 10.5pt;
    line-height: 1.35;
  }

  .no-print {
    display: none !important;
  }

  .print-only {
    display: block;
  }

  .app-view {
    max-width: none;
    padding: 0;
  }

  .sheet {
    box-shadow: none;
    border-radius: 0;
    padding: 0;
    background: #fff;
  }

  .doc-header {
    margin-bottom: 8px;
  }

  .logo-doc {
    width: 40px !important;
    height: 40px !important;
  }

  .doc-title {
    font-size: 1.05rem;
    margin: 6px 0 8px;
  }

  .block {
    margin-bottom: 8px;
  }

  .agency-block,
  .legal,
  #clauses-box p,
  #closing-text,
  #date-line {
    margin: 0 0 6px;
    font-size: 10pt;
    line-height: 1.35;
  }

  .exponen {
    font-size: 0.95rem;
    margin: 8px 0 6px;
  }

  input.inline,
  select.inline {
    border-bottom: 1px solid #999;
  }

  /* Firmas compactas en la misma carilla */
  .signatures {
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 10px !important;
    margin-top: 6px;
    page-break-inside: avoid;
    break-inside: avoid;
  }

  .sig-col {
    page-break-inside: avoid;
    break-inside: avoid;
    min-width: 0;
  }

  .sig-pad {
    border: 1px solid #ccc;
    height: 78px !important;
    min-height: 78px;
  }

  .digi-sign {
    min-height: 78px;
    height: 78px;
    padding: 6px 6px 6px 8px;
  }

  .digi-sign-mark {
    width: 40px;
    height: 40px;
    top: 16px;
    left: 4px;
  }

  .digi-sign-text {
    margin-left: 42px;
    font-size: 8.5px;
    line-height: 1.2;
  }

  .sig-label {
    font-size: 0.85rem;
    margin-bottom: 2px;
  }

  #closing-text,
  #date-line {
    page-break-after: avoid;
    break-after: avoid;
  }
}
