:root {
  --bg: #eef0f4;
  --panel: #ffffff;
  --line: #d9dde5;
  --text: #1c2230;
  --muted: #6b7385;
  --accent: #2f6df6;
  --accent-dark: #2557c9;
  --danger: #d6384a;
  --viewer: #5b6270;
  --radius: 8px;
}

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

/* ---------- Barra superior ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  z-index: 20;
}
.brand { font-weight: 700; font-size: 15px; letter-spacing: .2px; }
.group { display: flex; align-items: center; gap: 6px; }
.group + .group { padding-left: 10px; border-left: 1px solid var(--line); }
.spacer { flex: 1; }
.zoom { min-width: 46px; text-align: center; color: var(--muted); font-variant-numeric: tabular-nums; }

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  padding: 7px 10px;
  border-radius: var(--radius);
  font: inherit;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { background: #f4f6fa; }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-dark); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn.ghost:hover:not(:disabled) { background: #f0f2f7; color: var(--text); }
.btn.danger { color: var(--danger); border-color: #f0c6cb; }
.btn.danger:hover:not(:disabled) { background: #fdf0f1; }
.btn.sq { width: 34px; padding: 7px 0; }
.btn.sq.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.armed { background: #fff4d6; border-color: #e6b800; }
.btn.block { width: 100%; margin-top: 10px; }

select, input[type="number"] {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 7px 8px;
  font: inherit;
  background: #fff;
}
input[type="number"] { width: 64px; }
input[type="color"] {
  width: 36px; height: 34px; padding: 2px;
  border: 1px solid var(--line); border-radius: var(--radius); background: #fff; cursor: pointer;
}

/* ---------- Estructura principal ---------- */
.main { flex: 1; display: flex; min-height: 0; }

.side {
  width: 260px;
  flex: none;
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.side[hidden] { display: none; }
.tabs { display: flex; align-items: center; border-bottom: 1px solid var(--line); }
.tab {
  flex: 1; border: 0; background: none; padding: 11px 8px; font: inherit; cursor: pointer;
  color: var(--muted); border-bottom: 2px solid transparent;
}
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.tab-close { border: 0; background: none; font-size: 20px; padding: 0 12px; cursor: pointer; color: var(--muted); }
.panel { padding: 12px; overflow: auto; flex: 1; }
.panel[hidden] { display: none; }
.hint { margin: 0 0 10px; color: var(--muted); font-size: 12.5px; }

.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.thumb {
  position: relative;
  aspect-ratio: 1;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background:
    repeating-conic-gradient(#f1f3f7 0% 25%, #fff 0% 50%) 50% / 14px 14px;
  display: grid; place-items: center;
  cursor: pointer; overflow: hidden;
}
.thumb:hover { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(47,109,246,.18); }
.thumb img { max-width: 86%; max-height: 86%; object-fit: contain; pointer-events: none; }
.thumb .del {
  position: absolute; top: 3px; right: 3px; width: 20px; height: 20px; border: 0; border-radius: 50%;
  background: rgba(0,0,0,.55); color: #fff; font-size: 13px; line-height: 20px; padding: 0; cursor: pointer; display: none;
}
.thumb:hover .del { display: block; }

.lista { list-style: none; margin: 0; padding: 0; }
.lista li {
  display: flex; align-items: center; gap: 6px; padding: 8px; border: 1px solid var(--line);
  border-radius: var(--radius); margin-bottom: 6px; cursor: pointer;
}
.lista li:hover { border-color: var(--accent); }
.lista .nom { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lista .meta { color: var(--muted); font-size: 11.5px; }
.lista button { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 16px; }
.lista button:hover { color: var(--danger); }
.lista-vacia { color: var(--muted); font-size: 13px; }

/* ---------- Visor ---------- */
.viewer {
  flex: 1;
  overflow: auto;
  background: var(--viewer);
  position: relative;
  padding: 18px 0 60px;
}
.viewer.dragover { outline: 3px dashed #fff; outline-offset: -12px; }

.empty { display: grid; place-items: center; min-height: 60vh; }
.empty[hidden] { display: none; }
.empty-card {
  background: #fff; border-radius: 14px; padding: 28px 34px; max-width: 460px;
  box-shadow: 0 8px 30px rgba(0,0,0,.18);
}
.empty-card h2 { margin: 0 0 6px; }
.empty-card p { color: var(--muted); margin: 0 0 12px; }
.empty-card ul { margin: 0; padding-left: 18px; color: var(--text); }
.empty-card li { margin: 6px 0; }

#pages { display: flex; flex-direction: column; align-items: center; gap: 16px; }
.page {
  position: relative;
  background: #fff;
  box-shadow: 0 2px 14px rgba(0,0,0,.35);
  flex: none;
}
.page canvas { display: block; }
.page.armed { cursor: crosshair; }
.page .tlayer, .page .alayer { position: absolute; inset: 0; }
.page .tlayer { z-index: 1; }
.page .alayer { z-index: 2; pointer-events: none; }
.page .alayer > * { pointer-events: auto; }

/* Cajas clicables sobre el texto original */
.hit { position: absolute; border-radius: 2px; cursor: text; }
.hit:hover { background: rgba(47,109,246,.14); outline: 1px solid rgba(47,109,246,.55); }

/* Anotaciones */
.cover { position: absolute; pointer-events: none !important; }
.ann { position: absolute; }
.ann.text { min-width: 8px; }
.ann.text .edit {
  outline: none;
  white-space: pre;
  line-height: 1.2;
  min-height: 1.2em;
  min-width: 6px;
  padding: 0 1px;
  cursor: text;
  background: transparent;
}
.ann.text:hover .edit { outline: 1px dashed rgba(47,109,246,.7); }
.ann.text.sel .edit { outline: 1.5px solid var(--accent); background: rgba(255,255,255,.0); }
.ann .drag {
  position: absolute; left: -2px; top: -18px; height: 17px; min-width: 22px; padding: 0 5px;
  background: var(--accent); color: #fff; font-size: 11px; line-height: 17px; text-align: center;
  border-radius: 4px 4px 0 0; cursor: move; display: none; user-select: none; touch-action: none;
}
.ann.sel .drag { display: block; }

.ann.img { cursor: move; touch-action: none; user-select: none; }
.ann.img img { width: 100%; height: 100%; display: block; pointer-events: none; -webkit-user-drag: none; }
.ann.img:hover { outline: 1px dashed rgba(47,109,246,.7); }
.ann.img.sel { outline: 1.5px solid var(--accent); }
.ann .rs {
  position: absolute; right: -6px; bottom: -6px; width: 12px; height: 12px; background: #fff;
  border: 2px solid var(--accent); border-radius: 3px; cursor: nwse-resize; display: none; touch-action: none;
}
.ann.sel .rs { display: block; }

/* ---------- Avisos ---------- */
.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%);
  background: #1c2230; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 50;
  box-shadow: 0 6px 24px rgba(0,0,0,.3); max-width: 90vw;
}
.toast.error { background: #b3261e; }
.toast[hidden], .loading[hidden] { display: none; }

.loading {
  position: fixed; inset: 0; background: rgba(255,255,255,.65); display: flex; gap: 12px;
  align-items: center; justify-content: center; z-index: 60; font-weight: 600;
}
.spinner {
  width: 22px; height: 22px; border: 3px solid #c9d3ee; border-top-color: var(--accent);
  border-radius: 50%; animation: giro .8s linear infinite;
}
@keyframes giro { to { transform: rotate(360deg); } }

/* ---------- Login ---------- */
.login-body { align-items: center; justify-content: center; }
.login {
  background: #fff; padding: 28px; border-radius: 14px; width: 320px; display: flex; flex-direction: column; gap: 10px;
  box-shadow: 0 8px 30px rgba(0,0,0,.12);
}
.login h1 { margin: 0; font-size: 20px; }
.login p { margin: 0; color: var(--muted); }
.login input { border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; font: inherit; }
.login-error { color: var(--danger); font-size: 13px; }

@media (max-width: 760px) {
  .side { position: absolute; z-index: 15; height: calc(100% - 0px); top: 0; left: 0; box-shadow: 4px 0 20px rgba(0,0,0,.2); }
  .main { position: relative; }
  .group + .group { border-left: 0; padding-left: 0; }
}

/* ---------- Ventana "Cambiar contraseña" ---------- */
.dlg {
  border: 0; border-radius: 14px; padding: 22px 24px; width: min(380px, 92vw);
  box-shadow: 0 12px 40px rgba(0,0,0,.3); color: var(--text);
}
.dlg::backdrop { background: rgba(20, 24, 36, .55); }
.dlg h3 { margin: 0 0 12px; font-size: 18px; }
.dlg label { display: block; margin-bottom: 10px; font-weight: 600; font-size: 13px; }
.dlg label small { font-weight: 400; color: var(--muted); }
.dlg input {
  display: block; width: 100%; margin-top: 4px; border: 1px solid var(--line); border-radius: var(--radius);
  padding: 9px 10px; font: inherit; font-weight: 400;
}
.dlg-error { color: var(--danger); font-size: 13px; margin: 2px 0 8px; }
.dlg-botones { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.login p b { color: var(--text); }

/* ---------- Menú "Convertir" y diálogos de conversión ---------- */
.menu-wrap { position: relative; }
.menu {
  position: absolute; top: calc(100% + 6px); left: 0; min-width: 270px; z-index: 40;
  background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 6px;
  box-shadow: 0 10px 30px rgba(0,0,0,.18);
}
.menu button {
  display: block; width: 100%; text-align: left; border: 0; background: none; padding: 9px 10px;
  border-radius: 8px; font: inherit; cursor: pointer; color: var(--text);
}
.menu button:hover { background: #eef3ff; }

.dlg.wide { width: min(660px, 94vw); }
.dlg select {
  display: block; width: 100%; margin-top: 4px; border: 1px solid var(--line); border-radius: var(--radius);
  padding: 9px 10px; font: inherit; font-weight: 400; background: #fff;
}
.dlg .hint { margin: 0 0 8px; }
.conv-acciones { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.conv-cam { margin-bottom: 10px; background: #000; border-radius: 10px; overflow: hidden; position: relative; }
.conv-cam.flash::after { content: ""; position: absolute; inset: 0; background: #fff; opacity: .85; }
#convVideo { display: block; width: 100%; max-height: 44vh; object-fit: contain; background: #000; }
.cam-barra { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; padding: 8px; background: #14171f; }
.conv-lista {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(112px, 1fr)); gap: 8px;
  max-height: 30vh; overflow: auto; margin-bottom: 12px; min-height: 36px;
}
.conv-vacia { grid-column: 1 / -1; color: var(--muted); font-size: 13px; padding: 6px 0; }
.conv-item { position: relative; border: 1px solid var(--line); border-radius: 8px; padding: 6px; background: #f3f5f9; text-align: center; }
.conv-item canvas { display: block; margin: 0 auto; max-width: 100%; max-height: 120px; background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.25); }
.conv-num { position: absolute; top: 4px; left: 4px; background: var(--accent); color: #fff; font-size: 11px; border-radius: 10px; padding: 0 7px; }
.conv-btns { display: flex; justify-content: center; gap: 4px; margin-top: 6px; }
.conv-btns button { border: 1px solid var(--line); background: #fff; border-radius: 6px; padding: 2px 8px; cursor: pointer; font: inherit; }
.conv-btns button:disabled { opacity: .35; cursor: default; }
.conv-opciones { display: flex; gap: 12px; flex-wrap: wrap; }
.conv-opciones label { flex: 1; min-width: 190px; }

/* Con muchos botones, el nombre de la app estorba */
@media (max-width: 1450px) { .brand { display: none; } }

/* ---------- OCR ---------- */
.dlg label.chk { display: flex; align-items: center; gap: 8px; font-weight: 400; }
.dlg input[type="checkbox"] { display: inline-block; width: auto; margin: 0; }
.ocr-prog { margin: 6px 0 10px; }
.ocr-prog .barra { height: 8px; background: #e3e7ef; border-radius: 6px; overflow: hidden; }
.ocr-prog .barra div { height: 100%; width: 0; background: var(--accent); transition: width .25s; }
.ocr-prog span { display: block; margin-top: 6px; font-size: 13px; color: var(--muted); }

/* ---------- Fila de opciones del elemento seleccionado (altura fija) ---------- */
.subbar {
  height: 50px; flex: none; display: flex; align-items: center; gap: 10px; padding: 0 12px;
  background: #f6f7fb; border-bottom: 1px solid var(--line); overflow-x: auto; white-space: nowrap;
}
.subbar-hint { color: var(--muted); font-size: 13px; }
.subbar .props { padding-left: 0; border-left: 0; }
