:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --border: #dde1e7;
  --text: #1c2230;
  --muted: #667085;
  --hover: #eef1f5;
  --accent: #2f6fed;
  --accent-soft: #e4ecfd;
  --accent-ink: #ffffff;
  --danger: #c62828;
  --viewer: #e4e7ec;
  --shadow: 0 1px 3px rgba(16, 24, 40, .18), 0 6px 18px rgba(16, 24, 40, .08);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17191e;
    --panel: #1f2229;
    --border: #31353f;
    --text: #e7e9ee;
    --muted: #9aa3b2;
    --hover: #2a2e37;
    --accent: #5b8ff5;
    --accent-soft: #263654;
    --accent-ink: #0d1220;
    --danger: #ef6b6b;
    --viewer: #111216;
    --shadow: 0 1px 3px rgba(0, 0, 0, .6), 0 6px 18px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

/* ---------- Barras ---------- */
.topbar, .toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex: none;
}
.toolbar { padding-block: 6px; overflow-x: auto; }
.topbar { overflow-x: auto; scrollbar-width: none; }
.propbar {
  display: flex; align-items: center; gap: 16px;
  height: 44px; padding: 0 14px; flex: none;
  background: var(--panel); border-bottom: 1px solid var(--border);
  overflow-x: auto; overflow-y: hidden;
}
.hint-text { color: var(--muted); font-size: 13px; white-space: nowrap; }
.brand { font-weight: 650; letter-spacing: -.01em; margin-right: 4px; white-space: nowrap; }
.group { display: flex; align-items: center; gap: 4px; }
.file-info {
  flex: 1; min-width: 0;
  display: flex; gap: 10px; justify-content: flex-end; align-items: baseline;
  color: var(--muted); font-size: 13px;
}
#fileName { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#pageInfo { white-space: nowrap; font-variant-numeric: tabular-nums; }

.check { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; font-size: 13px; cursor: pointer; }
.check input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); cursor: pointer; }

.ic {
  width: 18px; height: 18px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round;
}
.ic.big { width: 44px; height: 44px; stroke-width: 1.3; color: var(--accent); }

.btn, .tool, .toggle {
  display: inline-flex; align-items: center; gap: 6px;
  height: 34px; padding: 0 10px;
  border: 1px solid transparent; border-radius: 8px;
  background: transparent; color: inherit;
  font: inherit; white-space: nowrap; cursor: pointer;
}
.btn { border-color: var(--border); background: var(--panel); }
.btn.icon { padding: 0 8px; }
.btn:hover, .tool:hover, .toggle:hover { background: var(--hover); }
.btn:disabled, .tool:disabled { opacity: .45; cursor: default; pointer-events: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn.danger { color: var(--danger); }
.zoom-label { min-width: 62px; justify-content: center; font-variant-numeric: tabular-nums; }
:is(.btn, .tool, .toggle, input, select):focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.tools { display: flex; align-items: center; gap: 2px; }
.tool.active { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.sep { width: 1px; height: 22px; background: var(--border); margin: 0 6px; }

.props { display: flex; align-items: center; gap: 14px; margin-left: auto; flex: none; }
.prop { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.prop input[type="color"] { width: 30px; height: 28px; padding: 1px; border: 1px solid var(--border); border-radius: 6px; background: var(--panel); cursor: pointer; }
.prop input[type="number"] { width: 62px; }
.prop input[type="number"], .prop select {
  height: 30px; padding: 0 6px;
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--panel); color: var(--text); font: inherit;
}
.prop input[type="range"] { width: 96px; accent-color: var(--accent); }
.prop output { min-width: 26px; color: var(--text); font-variant-numeric: tabular-nums; }
.swatches { gap: 4px; }
.swatch { width: 20px; height: 20px; border-radius: 50%; border: 1px solid rgba(128, 128, 128, .5); padding: 0; cursor: pointer; }
.swatch:hover { transform: scale(1.15); }
.toggle { width: 30px; height: 30px; padding: 0; justify-content: center; border-color: var(--border); color: var(--text); font-family: Georgia, serif; font-size: 15px; }
.toggle.on { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }

/* ---------- Zona de trabajo ---------- */
.workspace { flex: 1; display: flex; min-height: 0; }

.sidebar {
  width: 184px; flex: none;
  display: flex; flex-direction: column;
  background: var(--panel); border-right: 1px solid var(--border);
}
.thumbs { flex: 1; overflow-y: auto; padding: 12px 12px 4px; }
.sidebar-foot { padding: 10px 12px; border-top: 1px solid var(--border); }
.sidebar-foot .btn { width: 100%; justify-content: center; }

.thumb { position: relative; padding: 6px; margin-bottom: 8px; border-radius: 10px; border: 2px solid transparent; cursor: pointer; }
.thumb:hover { background: var(--hover); }
.thumb.active { border-color: var(--accent); background: var(--accent-soft); }
.thumb.dragging { opacity: .4; }
.thumb.drop-before::before, .thumb.drop-after::after {
  content: ""; position: absolute; left: 4px; right: 4px; height: 3px; border-radius: 2px; background: var(--accent);
}
.thumb.drop-before::before { top: -6px; }
.thumb.drop-after::after { bottom: -6px; }
.thumb-canvas { display: flex; justify-content: center; }
.thumb-canvas canvas { display: block; max-width: 100%; background: #fff; box-shadow: 0 0 0 1px rgba(128, 128, 128, .35); }
.thumb-meta { display: flex; align-items: center; justify-content: space-between; margin-top: 4px; height: 24px; }
.thumb-num { font-size: 12px; color: var(--muted); padding-left: 2px; font-variant-numeric: tabular-nums; }
.thumb-actions { display: flex; gap: 1px; opacity: 0; }
.thumb:hover .thumb-actions, .thumb.active .thumb-actions, .thumb:focus-within .thumb-actions { opacity: 1; }
.thumb-actions button {
  display: inline-flex; align-items: center; justify-content: center;
  width: 24px; height: 24px; padding: 0; border: 0; border-radius: 6px;
  background: transparent; color: var(--muted); cursor: pointer;
}
.thumb-actions button:hover { background: var(--panel); color: var(--text); }
.thumb-actions button[data-act="del"]:hover { color: var(--danger); }
.thumb-actions .ic { width: 15px; height: 15px; }

.viewer { flex: 1; position: relative; overflow: auto; background: var(--viewer); user-select: none; -webkit-user-select: none; }
.viewer.dragover { outline: 3px dashed var(--accent); outline-offset: -10px; }
.pages {
  --inv: 1;
  display: flex; flex-direction: column; align-items: center; gap: 18px;
  width: max-content; min-width: 100%;
  padding: 24px;
}
.pages:empty { display: none; }

.page { position: relative; flex: none; background: #fff; box-shadow: var(--shadow); overflow: hidden; }
.page-inner { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
.page-inner > canvas { position: absolute; left: 0; top: 0; width: 100%; height: 100%; }
.layer { position: absolute; left: 0; top: 0; width: 100%; height: 100%; }
.viewer:not(.tool-select) .layer { touch-action: none; }

/* Cursores según herramienta */
.viewer.tool-text .layer { cursor: text; }
.viewer.tool-edittext .layer { cursor: default; }
.viewer:is(.tool-draw, .tool-hl, .tool-cover, .tool-rect, .tool-ellipse, .tool-line, .tool-arrow) .layer { cursor: crosshair; }

/* Elementos añadidos */
.annot { position: absolute; left: 0; top: 0; pointer-events: none; }
.a-shape { overflow: visible; }
.hit { pointer-events: none; }
.a-text { white-space: pre; color: #111; min-width: 2px; outline: none; }
.a-image { -webkit-user-drag: none; }

.tool-select .hit { pointer-events: visiblePainted; cursor: move; }
.tool-select :is(.a-text, .a-image) { pointer-events: auto; cursor: move; }
:is(.tool-text, .tool-edittext) .a-text { pointer-events: auto; cursor: text; }
.a-text.editing {
  pointer-events: auto; cursor: text;
  user-select: text; -webkit-user-select: text;
  min-width: 0.6em;
  outline: calc(1.5px * var(--inv)) dashed #2f6fed;
  outline-offset: calc(2px * var(--inv));
}

.selbox {
  position: absolute; pointer-events: none;
  outline: calc(1.5px * var(--inv)) solid #2f6fed;
  outline-offset: calc(2px * var(--inv));
}
.handle {
  position: absolute; pointer-events: auto; touch-action: none;
  width: calc(11px * var(--inv)); height: calc(11px * var(--inv));
  margin: calc(-5.5px * var(--inv)) 0 0 calc(-5.5px * var(--inv));
  background: #fff; border: calc(1.5px * var(--inv)) solid #2f6fed; border-radius: 50%;
}
.line-hover {
  position: absolute; pointer-events: none;
  background: rgba(47, 111, 237, .14);
  outline: calc(1px * var(--inv)) solid rgba(47, 111, 237, .8);
  border-radius: calc(2px * var(--inv));
}

/* ---------- Estado vacío ---------- */
.empty { position: absolute; inset: 0; overflow-y: auto; padding: 32px 16px 24px; user-select: text; -webkit-user-select: text; }
.landing { width: min(880px, 100%); margin: 0 auto; display: flex; flex-direction: column; align-items: center; }
.hero { text-align: center; max-width: 640px; margin-bottom: 22px; }
.hero h1 { margin: 0; font-size: clamp(26px, 4.2vw, 36px); font-weight: 700; letter-spacing: -.02em; line-height: 1.15; }
.lead { margin: 10px 0 0; font-size: 16px; color: var(--muted); }
.lead strong { color: var(--text); font-weight: 600; }
.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  width: min(520px, 100%); padding: 40px 28px;
  border: 2px dashed var(--border); border-radius: 18px;
  background: var(--panel); text-align: center;
}
.drop-title { margin: 10px 0 0 !important; font-size: 20px; font-weight: 650; letter-spacing: -.01em; color: var(--text) !important; }

.seo-sec { width: 100%; margin-top: 44px; }
.seo-sec h2 { margin: 0 0 14px; font-size: 21px; font-weight: 650; letter-spacing: -.01em; }
.seo-sec p { margin: 0; color: var(--muted); max-width: 72ch; }
.features { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.features li { padding: 14px 16px; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; }
.features h3 { margin: 0 0 4px; font-size: 15px; font-weight: 650; }
.features p { font-size: 13.5px; }
.steps { margin: 0; padding-left: 22px; display: grid; gap: 8px; color: var(--muted); }
.steps strong { color: var(--text); }
.faq details { border-bottom: 1px solid var(--border); padding: 12px 0; }
.faq details:first-of-type { border-top: 1px solid var(--border); }
.faq summary { cursor: pointer; font-weight: 600; }
.faq details p { margin-top: 8px; }
.site-foot { margin-top: 44px; padding-top: 16px; width: 100%; border-top: 1px solid var(--border); text-align: center; font-size: 12.5px; color: var(--muted); }
a.brand { color: inherit; text-decoration: none; }
.dropzone p { margin: 0; color: var(--muted); }
.dropzone .btn { margin: 4px 0 10px; padding: 0 18px; height: 38px; }
.dropzone .hint { font-size: 12.5px; }

/* ---------- Diálogo de firma ---------- */
.dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 20px;
  background: var(--panel); color: var(--text); box-shadow: var(--shadow);
  width: min(580px, calc(100vw - 32px));
}
.dialog::backdrop { background: rgba(10, 14, 22, .45); }
.dialog h2 { margin: 0 0 12px; font-size: 16px; }
#signPad {
  display: block; width: 100%; aspect-ratio: 26 / 10;
  background: #fff; border: 1px solid var(--border); border-radius: 10px;
  touch-action: none; cursor: crosshair;
}
.dialog-row { display: flex; align-items: center; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.spacer { flex: 1; }

/* ---------- Diálogo de metadatos ---------- */
.meta-dialog { width: min(640px, calc(100vw - 32px)); }
.meta-body { max-height: min(68vh, 640px); overflow-y: auto; margin: 0 -20px; padding: 0 20px; }
.meta-sec + .meta-sec { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--border); }
.meta-sec h3 { margin: 0 0 8px; font-size: 12px; font-weight: 650; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); }
.meta-list { display: grid; grid-template-columns: minmax(120px, max-content) 1fr; gap: 6px 16px; margin: 0; }
.meta-list dt { color: var(--muted); }
.meta-list dd { margin: 0; min-width: 0; overflow-wrap: anywhere; user-select: text; -webkit-user-select: text; }
.meta-empty { color: var(--muted); opacity: .6; }
.meta-note { margin: 10px 0 0; font-size: 12.5px; color: var(--muted); }
.meta-body > .meta-note { margin: 16px 0 4px; }
.meta-raw { margin-top: 10px; }
.meta-raw summary, .meta-group > summary { cursor: pointer; color: var(--accent); }
.meta-raw pre {
  max-height: 240px; overflow: auto; margin: 8px 0 0; padding: 10px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
  font-size: 11.5px; white-space: pre-wrap; overflow-wrap: anywhere;
}
.meta-group { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; }
.meta-group + .meta-group { margin-top: 10px; }
.meta-group > summary { font-weight: 600; color: var(--text); }
.meta-group[open] > summary { margin-bottom: 12px; }
@media (max-width: 520px) {
  .meta-list { grid-template-columns: 1fr; gap: 2px; }
  .meta-list dd { margin-bottom: 6px; }
}

/* ---------- Avisos ---------- */
.busy {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center; gap: 12px;
  background: rgba(10, 14, 22, .45); color: #fff; font-size: 15px;
}
.spinner { width: 22px; height: 22px; border: 3px solid rgba(255, 255, 255, .35); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.toasts { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 60; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast {
  max-width: min(560px, calc(100vw - 32px)); padding: 10px 14px; border-radius: 10px;
  background: #1c2230; color: #fff; font-size: 13.5px; box-shadow: var(--shadow);
  animation: toast-in .18s ease-out;
}
.toast.error { background: #a02222; }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } }

@media (max-width: 1080px) {
  .hint-text, #fileName { display: none; }
  .props { margin-left: 0; }
  .tool span, .btn:not(.primary):not(.zoom-label) span { display: none; }
  .sidebar { width: 132px; }
  .brand { display: none; }
}
