:root {
  --bg: #eef0f3;
  --panel: #ffffff;
  --border: #dde1e6;
  --text: #1d2228;
  --muted: #6b7380;
  --accent: #1a66d9;
  --accent-soft: #e6effc;
  --red: #d93025;
  --shadow: 0 1px 3px rgba(16, 24, 40, .12), 0 1px 2px rgba(16, 24, 40, .06);
  --radius: 8px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }

svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
svg.mini { width: 14px; height: 14px; }

/* ---------- Pantallas de acceso ---------- */
.screen { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; background: var(--bg); }
.card { background: var(--panel); border-radius: 14px; box-shadow: var(--shadow); padding: 36px 32px; width: min(400px, 100%); text-align: center; }
.card h1 { font-size: 22px; margin: 16px 0 8px; }
.card p { color: var(--muted); margin: 0 0 22px; line-height: 1.5; }
.card .error { color: var(--red); margin: 14px 0 0; font-size: 14px; }
.logo { display: inline-grid; place-items: center; width: 56px; height: 56px; border-radius: 14px; background: var(--red); color: #fff; font-weight: 800; font-size: 16px; letter-spacing: .5px; }
.logo.denied { background: #f29900; font-size: 26px; }
.company-logo { display: block; height: 38px; width: auto; margin: 0 auto; }
.card .company-logo { margin-bottom: 4px; }
.brand-logo { height: 22px; width: auto; display: block; }
.brand-sep { width: 1px; height: 22px; background: var(--border); }
.logo.sm { width: 30px; height: 30px; border-radius: 7px; font-size: 10px; }

.spinner { width: 34px; height: 34px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Botones ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: 34px; padding: 0 14px; border-radius: 7px; border: 1px solid var(--border); background: var(--panel); cursor: pointer; font-size: 14px; font-weight: 500; white-space: nowrap; user-select: none; text-decoration: none; color: var(--text); }
.btn:hover { background: #f5f7f9; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #155ac2; }
.btn.accent { background: var(--red); border-color: var(--red); color: #fff; }
.btn.accent:hover:not(:disabled) { background: #c1271c; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.big { height: 44px; padding: 0 20px; font-size: 15px; width: 100%; }
.btn.small { height: 28px; padding: 0 10px; font-size: 13px; }
.icon-btn { display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 7px; border: 1px solid transparent; background: transparent; cursor: pointer; }
.icon-btn:hover { background: #f0f2f5; }
.icon-btn.toggle.on { background: var(--accent-soft); color: var(--accent); border-color: #c5d9f7; }

/* ---------- Barra superior ---------- */
#app { display: flex; flex-direction: column; height: 100%; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 8px 14px; background: var(--panel); border-bottom: 1px solid var(--border); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; }
.group { display: flex; gap: 8px; }
.spacer { flex: 1; }
.user { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--muted); min-width: 0; }
.user img { width: 28px; height: 28px; border-radius: 50%; }
#user-email { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }

.toolbar { display: flex; align-items: center; gap: 6px; padding: 6px 14px; background: var(--panel); border-bottom: 1px solid var(--border); overflow-x: auto; scrollbar-width: thin; }
.tools, .props, .actions, .zoom { display: flex; align-items: center; gap: 2px; }
.props { gap: 6px; }
.sep { width: 1px; height: 26px; background: var(--border); margin: 0 6px; flex: none; }
.tool { display: inline-flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-width: 62px; height: 46px; padding: 4px 6px; border: 1px solid transparent; border-radius: 7px; background: transparent; cursor: pointer; font-size: 11px; color: #3c4450; }
.tool:hover { background: #f0f2f5; }
.tool.active { background: var(--accent-soft); color: var(--accent); border-color: #c5d9f7; }
.prop { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); }
.props input[type=color] { width: 30px; height: 30px; padding: 2px; border: 1px solid var(--border); border-radius: 6px; background: var(--panel); cursor: pointer; }
.props select, .props input[type=number] { height: 30px; border: 1px solid var(--border); border-radius: 6px; padding: 0 6px; background: var(--panel); }
#prop-size, #prop-stroke { width: 56px; }
.zoom-label { min-width: 54px; height: 30px; border: 0; background: transparent; cursor: pointer; font-size: 13px; color: var(--muted); border-radius: 6px; }
.zoom-label:hover { background: #f0f2f5; }

/* ---------- Área de trabajo ---------- */
.workspace { flex: 1; display: flex; min-height: 0; }
.sidebar { width: 200px; flex: none; background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.tabs { display: flex; border-bottom: 1px solid var(--border); }
.tab { flex: 1; height: 38px; border: 0; background: transparent; cursor: pointer; font-size: 13px; color: var(--muted); border-bottom: 2px solid transparent; }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.badge { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px; background: var(--accent); color: #fff; font-size: 11px; line-height: 18px; }
.tab-panel { flex: 1; overflow-y: auto; padding: 10px; }
.panel-actions { display: flex; justify-content: center; }
.hint { color: var(--muted); font-size: 12px; margin: 8px 0; text-align: center; }

#thumbs { display: flex; flex-direction: column; align-items: center; gap: 12px; padding-bottom: 20px; }
.thumb { position: relative; width: 140px; padding: 6px; border-radius: 8px; border: 2px solid transparent; cursor: grab; text-align: center; }
.thumb:hover { background: #f5f7f9; }
.thumb.current { border-color: var(--accent); }
.thumb canvas { display: block; margin: 0 auto; box-shadow: var(--shadow); background: #fff; max-width: 124px; }
.thumb-bar { display: flex; align-items: center; justify-content: center; gap: 2px; margin-top: 6px; }
.thumb-num { font-size: 12px; color: var(--muted); margin-right: auto; padding-left: 4px; }
.thumb-bar button { width: 24px; height: 24px; border: 0; border-radius: 5px; background: transparent; cursor: pointer; color: var(--muted); display: grid; place-items: center; }
.thumb-bar button svg { width: 14px; height: 14px; }
.thumb-bar button:hover { background: #e6e9ed; color: var(--text); }
.thumb-bar button[data-act=del]:hover { background: #fce8e6; color: var(--red); }
.sortable-ghost { opacity: .35; }

#viewer { flex: 1; overflow: auto; position: relative; }
#viewer.drop { outline: 3px dashed var(--accent); outline-offset: -10px; }
#pages { display: flex; flex-direction: column; align-items: center; gap: 22px; padding: 24px 16px 60px; width: max-content; min-width: 100%; }
.page { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.page-sheet { position: relative; background: #fff; box-shadow: 0 2px 8px rgba(16, 24, 40, .15); }
.page-sheet .pdf-layer { position: absolute; top: 0; left: 0; }
.page-sheet .canvas-container { position: absolute !important; top: 0; left: 0; }
.page-num { font-size: 12px; color: var(--muted); }
.text-hover { position: absolute; z-index: 5; display: none; pointer-events: none; border: 2px solid var(--accent); border-radius: 3px; background: rgba(26, 102, 217, .08); box-shadow: 0 0 0 3px rgba(26, 102, 217, .15); }

.empty { position: absolute; inset: 24px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; border: 2px dashed #c4cad3; border-radius: 16px; cursor: pointer; color: var(--muted); text-align: center; padding: 16px; }
.empty:hover { border-color: var(--accent); background: rgba(26, 102, 217, .03); }
.empty svg { width: 48px; height: 48px; stroke-width: 1.5; color: var(--accent); margin-bottom: 8px; }
.empty strong { color: var(--text); font-size: 18px; }
.empty small { margin-top: 14px; font-size: 12px; }

.dedication { margin: 22px 0 0 !important; padding-top: 16px; border-top: 1px solid var(--border); font-family: Georgia, "Times New Roman", serif; font-style: italic; font-size: 15px; color: #8a4b5c !important; }
.loading-box { display: flex; flex-direction: column; align-items: center; gap: 14px; text-align: center; color: var(--muted); }
.loading-box p { margin: 0; }
.loading-box .dedication { border-top: 0; padding-top: 0; font-size: 17px; }
.empty .dedication { border-top: 0; padding-top: 0; margin-top: 18px !important; }
.heart { color: var(--red); font-style: normal; display: inline-block; animation: beat 1.6s ease-in-out infinite; }
@keyframes beat { 0%, 60%, 100% { transform: scale(1); } 30% { transform: scale(1.25); } }

/* ---------- Formulario ---------- */
.field-group h4 { font-size: 12px; margin: 6px 0 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.field { margin-bottom: 12px; }
.field label { display: flex; justify-content: space-between; gap: 6px; font-size: 12px; font-weight: 600; margin-bottom: 4px; word-break: break-word; }
.field label a { font-weight: 400; color: var(--accent); cursor: pointer; white-space: nowrap; text-decoration: none; }
.field input[type=text], .field textarea, .field select { width: 100%; border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; font-size: 13px; background: #fff; }
.field textarea { resize: vertical; min-height: 54px; }
.field .check { display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 400; }
.field.changed input, .field.changed textarea, .field.changed select { border-color: var(--accent); background: #f7faff; }
.fields-note { font-size: 12px; color: var(--muted); background: #f5f7f9; border-radius: 6px; padding: 8px; margin-bottom: 12px; line-height: 1.4; }

/* ---------- Firma ---------- */
dialog { border: 0; border-radius: 14px; padding: 0; box-shadow: 0 20px 50px rgba(0, 0, 0, .25); width: min(680px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(16, 24, 40, .45); }
.sig { padding: 20px; margin: 0; }
.sig h3 { margin: 0; }
.sig .hint { text-align: left; }
#sig-pad { width: 100%; height: auto; aspect-ratio: 640 / 220; border: 1px dashed #b8bfc9; border-radius: 10px; background: #fafbfc; touch-action: none; cursor: crosshair; display: block; }
.sig-saved { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.sig-saved img { height: 44px; max-width: 140px; object-fit: contain; border: 1px solid var(--border); border-radius: 6px; padding: 4px; background: #fff; cursor: pointer; }
.sig-saved img:hover { border-color: var(--accent); }
.sig-actions { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }

/* ---------- Fuentes ---------- */
.fonts { padding: 20px; margin: 0; max-height: calc(100vh - 60px); overflow-y: auto; }
.fonts-head { display: flex; align-items: center; justify-content: space-between; }
.fonts-head h3 { margin: 0; }
.fonts section { margin-top: 18px; }
.fonts h4 { margin: 0 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .4px; color: var(--muted); }
.fonts-row { display: flex; gap: 8px; }
.fonts-row input { flex: 1; min-width: 0; height: 34px; border: 1px solid var(--border); border-radius: 7px; padding: 0 10px; font-size: 14px; }
.hint.left { text-align: left; }
.hint a { color: var(--accent); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip { border: 1px solid var(--border); background: var(--panel); border-radius: 16px; padding: 4px 11px; font-size: 13px; cursor: pointer; }
.chip:hover { border-color: var(--accent); color: var(--accent); }
.chip small { color: var(--muted); font-size: 11px; }
.font-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid #eef0f3; }
.font-info { flex: 1; min-width: 0; }
.font-sample { font-size: 20px; line-height: 1.3; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.font-meta { font-size: 12px; color: var(--muted); margin-top: 3px; display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.font-meta .pill { display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 10px; background: #f0f2f5; }
.btn-remove { border: 0; background: transparent; color: var(--muted); cursor: pointer; padding: 6px; border-radius: 6px; display: grid; place-items: center; }
.btn-remove:hover { background: #fce8e6; color: var(--red); }
#prop-font { max-width: 150px; }

/* ---------- Compresión ---------- */
.levels { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }
.level { display: flex; gap: 12px; align-items: flex-start; padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px; cursor: pointer; }
.level:hover { border-color: #b9c9e4; }
.level:has(input:checked) { border-color: var(--accent); background: #f7faff; box-shadow: 0 0 0 1px var(--accent); }
.level input { margin-top: 3px; accent-color: var(--accent); }
.level b { font-size: 14px; }
.level small { display: block; color: var(--muted); font-size: 13px; margin-top: 3px; line-height: 1.4; }
.level .pill { font-size: 11px; padding: 1px 7px; border-radius: 10px; background: #e6f4ea; color: #137333; font-weight: 600; }
.level .pill.warn { background: #fef7e0; color: #b06000; }
.compress-result { margin-top: 14px; padding: 12px 14px; border-radius: 10px; background: #e6f4ea; color: #0d652d; font-size: 14px; line-height: 1.5; }
.compress-result.neutral { background: #f1f3f4; color: var(--text); }
.compress-result b { font-size: 16px; }

/* ---------- Avisos ---------- */
#busy { position: fixed; inset: 0; z-index: 50; background: rgba(255, 255, 255, .7); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; font-weight: 500; }
#toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 60; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; width: calc(100% - 32px); }
.toast { background: #1d2228; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 14px; box-shadow: var(--shadow); max-width: 520px; animation: pop .2s ease-out; }
.toast.error { background: var(--red); }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } }

.only-mobile { display: none !important; }
.backdrop { display: none; }

/* ---------- Menú «⋮» (celular) ---------- */
.more { position: relative; }
.menu { position: absolute; right: 0; top: calc(100% + 6px); z-index: 40; min-width: 230px; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 12px 32px rgba(16, 24, 40, .18); padding: 6px; display: flex; flex-direction: column; }
.menu button, .menu a { display: flex; align-items: center; gap: 12px; width: 100%; padding: 12px; border: 0; border-radius: 8px; background: transparent; color: var(--text); font-size: 15px; text-align: left; text-decoration: none; cursor: pointer; }
.menu button:active, .menu a:active, .menu button:hover, .menu a:hover { background: #f0f2f5; }
.menu button:disabled { opacity: .4; }
.menu svg { width: 20px; height: 20px; color: var(--muted); }
.menu-user { display: flex; align-items: center; gap: 10px; padding: 10px 12px 12px; margin-bottom: 4px; border-bottom: 1px solid var(--border); font-size: 13px; color: var(--muted); word-break: break-all; }
.menu-user img { width: 28px; height: 28px; border-radius: 50%; }
.menu-sep { height: 1px; background: var(--border); margin: 4px 0; }

@media (max-width: 820px) {
  .only-mobile { display: inline-grid !important; }
  .more.only-mobile { display: block !important; }
  .hide-mobile, .desktop-only { display: none !important; }
  .topbar { padding: 8px 10px; gap: 8px; }
  .brand-sep { display: none; }
  .brand-logo { height: 18px; }
  .btn { padding: 0 12px; height: 36px; }

  /* Herramientas: barra fija abajo, al alcance del pulgar. */
  .tools {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
    background: var(--panel); border-top: 1px solid var(--border); box-shadow: 0 -2px 10px rgba(16, 24, 40, .06);
    padding: 6px 6px calc(6px + env(safe-area-inset-bottom)); overflow-x: auto; scrollbar-width: none; gap: 2px;
  }
  .tools::-webkit-scrollbar { display: none; }
  .tool { min-width: 58px; height: 50px; font-size: 10.5px; flex: none; }
  .tool svg { width: 20px; height: 20px; }

  /* Propiedades, deshacer y zoom: una fila deslizable arriba. */
  .toolbar { padding: 6px 10px; gap: 4px; scrollbar-width: none; }
  .toolbar::-webkit-scrollbar { display: none; }
  .tools + .sep { display: none; }
  .props select, .props input[type=number] { font-size: 16px; height: 34px; } /* 16px evita el zoom automático de iOS */
  .props input[type=color] { width: 34px; height: 34px; }
  .icon-btn { width: 36px; height: 36px; }

  #pages { padding: 14px 8px 96px; gap: 14px; }
  .empty { inset: 12px 12px 84px; }
  .empty small { display: none; }
  #toasts { bottom: calc(80px + env(safe-area-inset-bottom)); }

  .sidebar { position: fixed; z-index: 20; top: 0; bottom: 0; left: 0; width: min(260px, 82vw); box-shadow: 4px 0 20px rgba(0, 0, 0, .15); transform: translateX(-105%); transition: transform .2s; }
  .sidebar.open { transform: none; }
  .backdrop { display: block; position: fixed; inset: 0; z-index: 19; background: rgba(16, 24, 40, .35); }
  .backdrop[hidden] { display: none; }

  dialog { width: calc(100vw - 20px); max-height: calc(100vh - 40px); }
  .sig, .fonts { padding: 16px; }
  .level { padding: 10px 12px; }
}
