/* Tema nocturno por defecto. El modo día se activa con data-theme="light". */
:root {
  color-scheme: dark;
  --bg: #0b1220;
  --surface: #111a2b;
  --border: #24314a;
  --text: #e7ecf5;
  --text-muted: #98a6bd;
  --primary: #3b82f6;
  --primary-hover: #60a5fa;
  --primary-soft: #1b2c4d;
  --success: #34d399;
  --success-soft: #0d2b1f;
  --danger: #f87171;
  --danger-soft: #3a1318;
  --warning: #fbbf24;
  --warning-soft: #3a2a08;
  --pdf-viewer-bg: #070c16;
  --row-hover-bg: #16223a;
  --badge-neutral-bg: #1e2a41;
  --badge-neutral-fg: #cbd5e1;
  --badge-muted-fg: #7c8aa3;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.45), 0 1px 2px rgba(0, 0, 0, 0.35);
  --shadow-lg: 0 10px 30px rgba(0, 0, 0, 0.55);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f6fb;
  --surface: #ffffff;
  --border: #e3e8f0;
  --text: #1a2233;
  --text-muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eef4ff;
  --success: #16a34a;
  --success-soft: #e8f7ee;
  --danger: #dc2626;
  --danger-soft: #fdeaea;
  --warning: #d97706;
  --warning-soft: #fef3e2;
  --pdf-viewer-bg: #eef1f6;
  --row-hover-bg: #fafbfe;
  --badge-neutral-bg: #eef1f6;
  --badge-neutral-fg: #475569;
  --badge-muted-fg: #94a3b8;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08), 0 1px 2px rgba(16, 24, 40, 0.04);
  --shadow-lg: 0 10px 30px rgba(16, 24, 40, 0.12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
}

.hidden { display: none !important; }

/* ---------- Topbar ---------- */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 20px;
  display: flex;
  align-items: center;
  gap: 8px;
  position: sticky;
  top: 0;
  z-index: 50;
  height: 58px;
}
.brand {
  font-weight: 700;
  font-size: 17px;
  margin-right: 16px;
  color: var(--primary);
  white-space: nowrap;
}
.brand small { color: var(--text-muted); font-weight: 500; }
.nav { display: flex; gap: 2px; flex: 1; flex-wrap: wrap; }
.nav a {
  text-decoration: none;
  color: var(--text-muted);
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  font-weight: 500;
  cursor: pointer;
}
.nav a:hover { background: var(--primary-soft); color: var(--primary); }
.nav a.active { background: var(--primary-soft); color: var(--primary); }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.lang-select {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}
.icon-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius-sm);
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  flex: 0 0 auto;
}
.icon-btn:hover { background: var(--bg); }
.login-controls {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}
.balance-pill {
  background: var(--success-soft);
  color: var(--success);
  font-weight: 700;
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 13px;
}
.user-chip { color: var(--text-muted); font-size: 13px; }

/* ---------- Layout ---------- */
.app { max-width: 1080px; margin: 0 auto; padding: 24px 20px 80px; }

.page-title { font-size: 24px; font-weight: 700; margin: 0 0 4px; }
.page-sub { color: var(--text-muted); margin: 0 0 20px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 16px;
}
.card h3 { margin: 0 0 12px; font-size: 16px; }

.row { display: flex; gap: 16px; flex-wrap: wrap; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.grid-4 { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s, border-color 0.15s;
  font-family: inherit;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-secondary { background: var(--surface); border-color: var(--border); color: var(--text); }
.btn-secondary:hover:not(:disabled) { background: var(--bg); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: #b91c1c; }
.btn-ghost { background: transparent; color: var(--primary); }
.btn-sm { padding: 6px 10px; font-size: 13px; }
.btn-block { width: 100%; }

/* ---------- Forms ---------- */
.field { margin-bottom: 14px; }
.field label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 5px; }
.field .hint { color: var(--text-muted); font-size: 12px; font-weight: 400; margin-top: 4px; }
.input, .select, .textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: inherit;
  background: var(--surface);
  color: var(--text);
}
.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

/* ---------- Login ---------- */
.login-wrap { max-width: 420px; margin: 8vh auto 0; }
.login-card { padding: 28px; }
.login-brand { text-align: center; margin-bottom: 18px; }
.login-brand .logo { font-size: 26px; font-weight: 800; color: var(--primary); }

/* ---------- Steps ---------- */
.steps { display: flex; gap: 6px; margin-bottom: 22px; flex-wrap: wrap; }
.step {
  flex: 1;
  min-width: 120px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  text-align: center;
}
.step.active { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); }
.step.done { background: var(--success-soft); border-color: var(--success); color: var(--success); }

/* ---------- Dropzone ---------- */
.dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 34px 20px;
  text-align: center;
  color: var(--text-muted);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.dropzone:hover, .dropzone.dragover { border-color: var(--primary); background: var(--primary-soft); }
.dropzone input { display: none; }

.file-list { list-style: none; padding: 0; margin: 12px 0 0; }
.file-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
  background: var(--surface);
}
.file-item .fname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-item .fmeta { color: var(--text-muted); font-size: 12px; }

/* ---------- PDF viewer ---------- */
.pdf-scroll { max-height: 70vh; overflow: auto; border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; background: var(--pdf-viewer-bg); }
.pdf-page { margin-bottom: 18px; text-align: center; }
.pdf-page-label { font-size: 12px; font-weight: 700; color: var(--text-muted); margin-bottom: 6px; }
.pdf-page-holder { position: relative; display: inline-block; background: #fff; box-shadow: var(--shadow-lg); }
.pdf-page-holder canvas { display: block; }
.pdf-overlay { position: absolute; inset: 0; cursor: crosshair; touch-action: none; }

/* ---------- Status badges ---------- */
.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  background: var(--bg);
  color: var(--text-muted);
}
.badge-draft { background: var(--badge-neutral-bg); color: var(--badge-neutral-fg); }
.badge-waiting_otp { background: var(--warning-soft); color: var(--warning); }
.badge-signing { background: var(--primary-soft); color: var(--primary); }
.badge-completed, .badge-signed { background: var(--success-soft); color: var(--success); }
.badge-failed { background: var(--danger-soft); color: var(--danger); }
.badge-expired, .badge-cancelled { background: var(--badge-neutral-bg); color: var(--badge-muted-fg); }
.badge-pending { background: var(--bg); color: var(--text-muted); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); }
th { font-size: 12px; text-transform: uppercase; letter-spacing: 0.03em; color: var(--text-muted); }
tr:hover td { background: var(--row-hover-bg); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.muted { color: var(--text-muted); }

/* ---------- Stats ---------- */
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.stat .v { font-size: 26px; font-weight: 800; color: var(--text); }
.stat .k { font-size: 13px; color: var(--text-muted); }

/* ---------- Toast ---------- */
.toast-root { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast {
  background: #1f2937;
  color: #fff;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  font-size: 14px;
  max-width: 360px;
  animation: slidein 0.2s ease;
}
.toast.error { background: #b91c1c; }
.toast.success { background: #15803d; }
@keyframes slidein { from { transform: translateY(10px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* ---------- Alert ---------- */
.alert { padding: 12px 14px; border-radius: var(--radius-sm); margin-bottom: 14px; font-size: 14px; }
.alert-error { background: var(--danger-soft); color: var(--danger); }
.alert-success { background: var(--success-soft); color: var(--success); }
.alert-info { background: var(--primary-soft); color: var(--primary); }
.alert-warning { background: var(--warning-soft); color: var(--warning); }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15,23,42,0.5);
  display: flex; align-items: center; justify-content: center; z-index: 200; padding: 20px;
}
.modal {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow-lg);
  max-width: 560px; width: 100%; max-height: 90vh; overflow: auto; padding: 24px;
}
.modal h3 { margin: 0 0 14px; }

.spinner {
  display: inline-block; width: 18px; height: 18px;
  border: 2px solid rgba(255,255,255,0.4); border-top-color: #fff;
  border-radius: 50%; animation: spin 0.7s linear infinite;
}
.spinner.dark { border-color: var(--border); border-top-color: var(--primary); }
@keyframes spin { to { transform: rotate(360deg); } }

.mt-1 { margin-top: 8px; } .mt-2 { margin-top: 16px; } .mb-1 { margin-bottom: 8px; } .mb-2 { margin-bottom: 16px; }
.text-center { text-align: center; }
.flex-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.grow { flex: 1; }

@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; height: auto; padding: 8px 12px; }
  .app { padding: 16px 12px 60px; }
}
