:root {
  --bg: #f3f2ed;
  --surface: #ffffff;
  --ink: #15303a;
  --muted: #5b6f77;
  --line: #dce2e0;
  --accent: #0f766e;
  --accent-strong: #0b5d57;
  --accent-soft: #d8efeb;
  --warn: #9a5b00;
  --warn-soft: #fdefd0;
  --ok: #17703a;
  --ok-soft: #dbf2e2;
  --info: #1d4f91;
  --info-soft: #dce9fb;
  --danger: #b42318;
  --danger-soft: #fde4e1;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(21, 48, 58, 0.06), 0 4px 16px rgba(21, 48, 58, 0.05);
  --font: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --serif: Georgia, "Times New Roman", serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--font);
}
h1, h2, h3 { line-height: 1.2; margin: 0; }
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

:focus-visible { outline: 3px solid #2a9d8f; outline-offset: 2px; }

/* ---------- botones ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 18px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); cursor: pointer; font-weight: 600;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:hover { background: #f1f5f4; }
.btn:active { transform: translateY(1px); }
.btn[disabled] { opacity: 0.45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not([disabled]) { background: var(--accent-strong); }
.btn.danger { color: var(--danger); border-color: #f0b8b2; background: #fff; }
.btn.danger:hover { background: var(--danger-soft); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { background: rgba(21, 48, 58, 0.06); }
.btn.sm { min-height: 34px; padding: 0 12px; font-size: 14px; border-radius: 8px; }

/* ---------- formularios ---------- */
label.field { display: grid; gap: 6px; font-size: 14px; font-weight: 600; color: var(--ink); }
label.field .hint { font-weight: 400; color: var(--muted); }
.input {
  width: 100%; min-height: 44px; padding: 9px 12px;
  border: 1px solid #c4cdcb; border-radius: 10px; background: #fff;
}
.input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.stack { display: grid; gap: 14px; }

.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.card-pad { padding: 20px; }
.muted { color: var(--muted); }
.error-box {
  background: var(--danger-soft); color: var(--danger);
  border: 1px solid #f0b8b2; border-radius: 10px; padding: 10px 12px; font-size: 14px;
}

/* ---------- login ---------- */
.login-wrap { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login-card { width: min(400px, 100%); padding: 32px; display: grid; gap: 18px; }
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 40px; height: 40px; border-radius: 11px; background: var(--accent); color: #fff;
  display: grid; place-items: center; flex: none;
}
.brand-mark svg { width: 22px; height: 22px; }
.brand-name { font-weight: 700; letter-spacing: -0.01em; }
.brand-sub { font-size: 13px; color: var(--muted); }
.demo-hint { font-size: 13px; background: var(--accent-soft); color: var(--accent-strong); padding: 10px 12px; border-radius: 10px; }

/* ---------- app shell ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 12px 24px; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.topbar .spacer { flex: 1; }
.pill {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 5px 12px; border-radius: 999px; font-size: 13px; font-weight: 600;
  background: var(--warn-soft); color: var(--warn);
}
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.info { background: var(--info-soft); color: var(--info); }

.tabs { display: flex; gap: 4px; padding: 0 24px; background: var(--surface); border-bottom: 1px solid var(--line); }
.tab {
  border: 0; background: none; cursor: pointer; padding: 12px 16px; font-weight: 600; color: var(--muted);
  border-bottom: 3px solid transparent; margin-bottom: -1px;
}
.tab:hover { color: var(--ink); }
.tab[aria-selected="true"] { color: var(--accent-strong); border-bottom-color: var(--accent); }

main { max-width: 1240px; margin: 0 auto; padding: 24px; }

/* ---------- nuevo consentimiento ---------- */
.new-layout { display: grid; grid-template-columns: minmax(340px, 440px) 1fr; gap: 24px; align-items: start; }
.step-title { display: flex; align-items: center; gap: 10px; font-size: 17px; margin-bottom: 14px; }
.step-num {
  width: 26px; height: 26px; border-radius: 50%; background: var(--accent); color: #fff;
  font-size: 13px; font-weight: 700; display: grid; place-items: center; flex: none;
}
.form-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 4px; }
.form-actions .primary { flex: 1; min-width: 200px; }

.tablet-card { padding: 16px 18px; display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 18px; }
.tablet-card .info { flex: 1; min-width: 200px; }
.tablet-card .title { font-weight: 700; }
.tablet-card .sub { font-size: 14px; color: var(--muted); }
.tablet-card.pending { border-color: #a9c4ec; background: #f5f9ff; }
.pulse { width: 12px; height: 12px; border-radius: 50%; background: var(--info); position: relative; flex: none; }
.pulse::after {
  content: ""; position: absolute; inset: -6px; border-radius: 50%; border: 2px solid var(--info);
  animation: pulse 1.6s ease-out infinite;
}
@keyframes pulse { from { transform: scale(0.5); opacity: 1; } to { transform: scale(1.5); opacity: 0; } }
.pair-help { font-size: 13px; background: var(--bg); border-radius: 10px; padding: 10px 12px; width: 100%; }
.pair-help code { background: #fff; border: 1px solid var(--line); padding: 1px 6px; border-radius: 6px; font-size: 13px; }

/* documento (vista previa y tablet) */
.paper {
  background: #fff; border: 1px solid var(--line); border-radius: 6px;
  box-shadow: var(--shadow); padding: 36px 40px; font-family: var(--serif);
  font-size: 15px; line-height: 1.65; color: #22292b;
}
.paper .paper-head { display: flex; justify-content: space-between; border-bottom: 1px solid var(--line); padding-bottom: 10px; margin-bottom: 18px; font-family: var(--font); font-size: 12px; color: var(--muted); }
.paper .paper-head b { color: var(--accent-strong); font-size: 13px; }
.paper h3 { font-family: var(--font); font-size: 14.5px; margin: 18px 0 4px; color: var(--ink); }
.paper h3:first-of-type { font-size: 19px; margin-top: 0; }
.paper p { margin: 0 0 10px; }
.paper mark { background: var(--accent-soft); color: #064e48; padding: 0 3px; border-radius: 3px; font-weight: 600; }
.paper mark.empty { background: var(--warn-soft); color: var(--warn); font-weight: 400; }
.preview-label { font-size: 13px; color: var(--muted); margin: 0 0 8px 2px; display: flex; justify-content: space-between; }

/* ---------- histórico ---------- */
.filters { display: grid; grid-template-columns: 2fr 1.3fr 1fr 1fr auto; gap: 12px; align-items: end; padding: 16px; margin-bottom: 16px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14.5px; }
th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); padding: 12px 16px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { padding: 12px 16px; border-bottom: 1px solid #eef1f0; vertical-align: middle; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: #f6faf9; }
tbody tr:last-child td { border-bottom: 0; }
.cell-main { font-weight: 600; }
.cell-sub { font-size: 12.5px; color: var(--muted); }
.tag { display: inline-block; padding: 2px 10px; border-radius: 999px; background: var(--accent-soft); color: var(--accent-strong); font-size: 13px; font-weight: 600; white-space: nowrap; }
.empty-state { padding: 48px 16px; text-align: center; color: var(--muted); }
.count-line { font-size: 14px; color: var(--muted); margin: 0 4px 10px; }

/* diálogo de detalle */
dialog { border: 0; padding: 0; border-radius: 16px; width: min(1040px, 94vw); max-height: 92dvh; box-shadow: 0 20px 60px rgba(0,0,0,0.3); color: var(--ink); }
dialog::backdrop { background: rgba(15, 30, 36, 0.55); }
.dlg { display: grid; grid-template-columns: 320px 1fr; height: min(780px, 90dvh); }
.dlg-side { padding: 22px; overflow-y: auto; border-right: 1px solid var(--line); display: grid; gap: 14px; align-content: start; background: #fafbfa; }
.dlg-side dl { margin: 0; display: grid; gap: 10px; }
.dlg-side dt { font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.dlg-side dd { margin: 0; font-size: 14px; word-break: break-word; }
.dlg-side .hash { font: 12px/1.4 ui-monospace, Consolas, monospace; color: #35504f; }
.dlg-pdf { width: 100%; height: 100%; border: 0; background: #e8ecea; }

/* ---------- plantillas ---------- */
.tpl-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 18px; }
.tpl-card { padding: 20px; display: grid; gap: 12px; align-content: start; }
.tpl-card h3 { font-size: 18px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { font: 12.5px ui-monospace, Consolas, monospace; background: var(--accent-soft); color: var(--accent-strong); padding: 2px 8px; border-radius: 6px; }
.chip.extra { background: var(--info-soft); color: var(--info); }
details summary { cursor: pointer; font-weight: 600; font-size: 14px; color: var(--accent-strong); }
details .paper { margin-top: 10px; padding: 18px 20px; font-size: 13.5px; box-shadow: none; max-height: 340px; overflow: auto; }

/* ---------- toasts ---------- */
.toasts { position: fixed; right: 20px; bottom: 20px; display: grid; gap: 10px; z-index: 50; }
.toast {
  background: var(--ink); color: #fff; padding: 14px 16px; border-radius: 12px; box-shadow: 0 8px 28px rgba(0,0,0,0.25);
  display: flex; align-items: center; gap: 14px; max-width: 420px; animation: toast-in 0.25s ease-out;
}
.toast.ok { background: var(--ok); }
.toast .btn { background: rgba(255,255,255,0.18); border-color: transparent; color: #fff; min-height: 32px; font-size: 13px; padding: 0 12px; }
@keyframes toast-in { from { transform: translateY(12px); opacity: 0; } }

/* ---------- tablet ---------- */
body.tablet { min-height: 100dvh; overflow: hidden; user-select: none; -webkit-user-select: none; background: #eef2f0; }
.t-screen { position: fixed; inset: 0; display: flex; flex-direction: column; }
.t-center { flex: 1; display: grid; place-items: center; text-align: center; padding: 32px; }
.t-center .big { font-size: clamp(28px, 4.5vw, 46px); font-weight: 700; letter-spacing: -0.02em; }
.t-center .lead { font-size: clamp(17px, 2.2vw, 22px); color: var(--muted); margin-top: 10px; }
.t-logo { width: 84px; height: 84px; border-radius: 24px; background: var(--accent); color: #fff; display: grid; place-items: center; margin: 0 auto 26px; }
.t-logo svg { width: 46px; height: 46px; }
.t-breathe { animation: breathe 3.2s ease-in-out infinite; }
@keyframes breathe { 50% { transform: scale(1.06); } }
.t-corner { position: absolute; bottom: 14px; left: 18px; right: 18px; display: flex; justify-content: space-between; align-items: center; font-size: 13px; color: var(--muted); }
.t-corner .btn { min-height: 36px; }
.conn-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--ok); margin-right: 6px; }
.conn-dot.off { background: var(--warn); }

.t-sign-head { padding: 14px 24px; background: #fff; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; gap: 16px; align-items: baseline; flex-wrap: wrap; }
.t-sign-head h2 { font-size: clamp(18px, 2.4vw, 24px); }
.t-sign-head .who { color: var(--muted); font-size: 15px; }
.t-doc { flex: 1; overflow-y: auto; padding: 20px 24px; -webkit-overflow-scrolling: touch; user-select: text; -webkit-user-select: text; }
.t-doc .paper { max-width: 860px; margin: 0 auto; font-size: clamp(17px, 2vw, 20px); line-height: 1.7; padding: 28px 34px; }
.t-doc .paper h3 { font-size: 1.05em; }
.t-doc .paper h3:first-of-type { font-size: 1.3em; }
.t-scroll-hint { text-align: center; color: var(--warn); font-weight: 600; font-size: 15px; padding: 6px 0 0; }
.t-sign { background: #fff; border-top: 1px solid var(--line); padding: 14px 24px 18px; display: grid; gap: 12px; box-shadow: 0 -6px 20px rgba(21,48,58,0.06); }
.t-check { display: flex; gap: 12px; align-items: center; font-size: clamp(16px, 1.9vw, 19px); font-weight: 600; cursor: pointer; }
.t-check input { width: 28px; height: 28px; accent-color: var(--accent); flex: none; }
.t-check.disabled { color: var(--muted); cursor: not-allowed; }
.sig-wrap { position: relative; height: clamp(130px, 22dvh, 220px); border: 2px dashed #9fb3b0; border-radius: 14px; background: #fcfdfc; }
.sig-wrap.active { border-style: solid; border-color: var(--accent); }
.sig-wrap canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: crosshair; border-radius: 12px; }
.sig-wrap .sig-label { position: absolute; left: 18px; top: 10px; font-size: 13px; color: #8aa09c; pointer-events: none; }
.sig-wrap .sig-line { position: absolute; left: 18px; right: 18px; bottom: 34px; border-bottom: 1.5px solid #cfd9d7; pointer-events: none; }
.sig-wrap .sig-x { position: absolute; left: 20px; bottom: 38px; font-size: 22px; color: #9fb3b0; pointer-events: none; }
.sig-wrap.signed .sig-label, .sig-wrap.signed .sig-x { display: none; }
.t-buttons { display: flex; gap: 12px; }
.t-buttons .btn { min-height: 58px; font-size: 18px; border-radius: 14px; }
.t-buttons .primary { flex: 1; }
.t-err { color: var(--danger); font-weight: 600; text-align: center; }

.t-done .check { width: 110px; height: 110px; border-radius: 50%; background: var(--ok); color: #fff; display: grid; place-items: center; margin: 0 auto 24px; animation: pop 0.4s ease-out; }
.t-done .check svg { width: 60px; height: 60px; }
@keyframes pop { from { transform: scale(0.4); opacity: 0; } }

.pair-card { width: min(440px, 92vw); padding: 30px; display: grid; gap: 16px; text-align: left; }
.code-input { text-align: center; font-size: 30px; letter-spacing: 0.4em; font-weight: 700; min-height: 60px; }

/* Tablets/pantallas bajas: más sitio para leer el documento */
@media (max-height: 720px) {
  .sig-wrap { height: 120px; }
  .t-sign { padding: 10px 20px 12px; gap: 8px; }
  .t-buttons .btn { min-height: 50px; }
  .t-sign-head { padding: 10px 20px; }
}

@media (max-width: 980px) {
  .new-layout { grid-template-columns: 1fr; }
  .filters { grid-template-columns: 1fr 1fr; }
  .dlg { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .dlg-side { border-right: 0; border-bottom: 1px solid var(--line); max-height: 36dvh; }
  main { padding: 16px; }
  .topbar, .tabs { padding-left: 16px; padding-right: 16px; }
  .paper { padding: 22px 20px; }
}
@media (max-width: 560px) {
  .grid-2, .filters { grid-template-columns: 1fr; }
}

/* Móvil: más sitio para leer, bloque de firma compacto */
@media (max-width: 600px) {
  .t-sign-head { padding: 10px 16px; }
  .t-sign-head h2 { font-size: 17px; }
  .t-doc { padding: 12px; }
  .t-doc .paper { padding: 16px 16px; font-size: 16px; line-height: 1.6; }
  .t-sign { padding: 10px 14px 12px; gap: 8px; }
  .t-check { font-size: 14px; gap: 10px; }
  .t-check input { width: 24px; height: 24px; }
  .sig-wrap { height: 118px; }
  .t-buttons .btn { min-height: 48px; font-size: 15px; padding: 0 10px; }
  .t-buttons .primary { flex: 1.4; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
