:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #334155;
  --accent: #22c55e;
  --accent-d: #16a34a;
  --danger: #ef4444;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --border: #334155;
}

* { box-sizing: border-box; }

html, body { height: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

.hidden { display: none !important; }

label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 6px; }

input, select, textarea, button { font-size: 16px; font-family: inherit; }

input, select, textarea {
  width: 100%;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
}
textarea { resize: vertical; }

button {
  cursor: pointer;
  border: none;
  border-radius: 10px;
  padding: 13px 16px;
  font-weight: 600;
  color: #fff;
  background: var(--panel-2);
  transition: filter .15s ease;
}
button:hover { filter: brightness(1.1); }
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary { background: var(--accent); }
button.primary:hover { background: var(--accent-d); }
button.danger { background: var(--danger); }
button.block { width: 100%; }
button.ghost { background: transparent; border: 1px solid var(--border); color: var(--text); }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 16px;
}

.row { display: flex; gap: 10px; }
.row > * { flex: 1; }

.muted { color: var(--muted); font-size: 13px; }
a.link { color: var(--accent); }

/* ---------------------------- VIEWS ---------------------------- */
.view { min-height: 100dvh; }
.view-centered { display: flex; align-items: center; justify-content: center; padding: 20px; }

/* Barra superior de las vistas con scroll (páginas/enviar) */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; background: var(--panel); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.topbar h2 { font-size: 17px; margin: 0; }
.view main { padding: 16px; max-width: 760px; margin: 0 auto; }

.icon-btn {
  background: rgba(0,0,0,.35); color: #fff; border-radius: 50%;
  width: 40px; height: 40px; padding: 0; font-size: 20px; line-height: 1;
  display: flex; align-items: center; justify-content: center;
}

/* --------------------- VISTA CÁMARA (full) --------------------- */
.view-camera {
  position: fixed; inset: 0; background: #000; overflow: hidden;
}
#scannerWrap, #editWrap { position: absolute; inset: 0; }
#video { width: 100%; height: 100%; object-fit: cover; display: block; }
#overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }

.cam-top {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top) + 12px) 14px 12px;
  background: linear-gradient(to bottom, rgba(0,0,0,.55), transparent);
}
.cam-select {
  flex: 1; width: auto; background: rgba(0,0,0,.45); color: #fff;
  border: 1px solid rgba(255,255,255,.2); padding: 8px 10px; font-size: 14px;
}

.cam-bottom {
  position: absolute; bottom: 0; left: 0; right: 0;
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 22px calc(env(safe-area-inset-bottom) + 22px);
  background: linear-gradient(to top, rgba(0,0,0,.6), transparent);
}
.shutter {
  width: 74px; height: 74px; border-radius: 50%; padding: 0;
  background: #fff; border: 5px solid rgba(255,255,255,.55);
  box-shadow: 0 0 0 2px rgba(0,0,0,.3);
}
.shutter:active { filter: brightness(.85); }

.cam-side {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  width: 84px;
}
.cam-side-label { color: #fff; font-size: 11px; text-shadow: 0 1px 3px rgba(0,0,0,.9); }
.finish-btn {
  background: var(--accent); color: #fff; padding: 11px 12px; border-radius: 12px;
  font-size: 14px; white-space: nowrap; box-shadow: 0 2px 10px rgba(0,0,0,.4);
}
.finish-btn.pulse { animation: finishPulse 1s ease-in-out 3; }
@keyframes finishPulse {
  0%, 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(34,197,94,.6); }
  50% { transform: scale(1.06); box-shadow: 0 0 0 10px rgba(34,197,94,0); }
}
button.accent { background: #3b82f6; }
button.accent:hover { background: #2563eb; }

.thumb-btn {
  position: relative; width: 56px; height: 56px; padding: 0; border-radius: 10px;
  background: rgba(0,0,0,.4); border: 1px solid rgba(255,255,255,.25); overflow: visible;
}
.thumb-btn:disabled { opacity: .4; }
.thumb-btn img { width: 100%; height: 100%; object-fit: cover; border-radius: 9px; display: block; }
.count-badge {
  position: absolute; top: -8px; right: -8px; min-width: 22px; height: 22px;
  background: var(--accent); color: #fff; border-radius: 11px; font-size: 12px;
  display: flex; align-items: center; justify-content: center; padding: 0 6px;
}
.cam-hint {
  position: absolute; bottom: 120px; left: 0; right: 0; text-align: center;
  color: #fff; font-size: 13px; text-shadow: 0 1px 4px rgba(0,0,0,.8); pointer-events: none;
}

/* ------------------- VISTA AJUSTAR BORDES --------------------- */
#editCanvas { width: 100%; height: 100%; object-fit: contain; display: block; }
#editHandles { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
.edit-top {
  position: absolute; top: 0; left: 0; right: 0; text-align: center; color: #fff;
  padding: calc(env(safe-area-inset-top) + 14px) 14px 14px; font-size: 14px;
  background: linear-gradient(to bottom, rgba(0,0,0,.55), transparent);
}
.edit-bottom {
  position: absolute; bottom: 0; left: 0; right: 0; display: flex; gap: 12px;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 16px);
  background: linear-gradient(to top, rgba(0,0,0,.6), transparent);
}
.edit-bottom button { flex: 1; }

/* --------------------- LISTA DE PÁGINAS ----------------------- */
.pages-list { display: flex; flex-direction: column; gap: 10px; }
.page-row {
  display: flex; align-items: center; gap: 12px; padding: 10px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
}
.page-row.dragging { opacity: .5; }
.page-row img { width: 64px; height: 84px; object-fit: cover; border-radius: 8px; background:#000; }
.page-row .pinfo { flex: 1; }
.page-row .pnum { font-weight: 600; }
.page-row .pactions { display: flex; gap: 6px; }
.page-row .pactions button { padding: 8px 10px; font-size: 14px; }
.drag-handle { cursor: grab; color: var(--muted); font-size: 20px; padding: 0 4px; }

/* --------------------- VISTA ENVIANDO ------------------------- */
.sending-card { width: 100%; max-width: 380px; text-align: center; }
.sending-card h2 { margin: 8px 0 4px; }

.spinner {
  width: 64px; height: 64px; margin: 8px auto 16px;
  border: 6px solid var(--panel-2); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.result-icon {
  width: 64px; height: 64px; margin: 8px auto 16px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 34px; color: #fff;
}
.result-icon.ok { background: var(--accent); }
.result-icon.queued { background: #d97706; }
.result-icon.err { background: var(--danger); }

.progress {
  height: 10px; background: var(--panel-2); border-radius: 6px; overflow: hidden;
  margin: 16px 0 8px;
}
.progress-bar {
  height: 100%; width: 0%; background: var(--accent);
  border-radius: 6px; transition: width .2s ease;
}
.progress.indeterminate .progress-bar {
  width: 40%; animation: indet 1.1s ease-in-out infinite;
}
@keyframes indet {
  0% { margin-left: -40%; } 100% { margin-left: 100%; }
}

/* ------------------------- QUEUE / MISC ----------------------- */
.queue-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; margin-bottom: 8px; font-size: 13px;
}
.queue-item .meta { color: var(--muted); font-size: 12px; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 20px; }
.badge.pending { background: #422006; color: #fbbf24; }
.badge.sending { background: #0c4a6e; color: #38bdf8; }
.badge.error { background: #450a0a; color: #f87171; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--border);
  padding: 12px 18px; border-radius: 10px; font-size: 14px; z-index: 100; max-width: 90%;
}
.toast.ok { border-color: var(--accent); }
.toast.err { border-color: var(--danger); }

/* Header reutilizado por la zona admin */
header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; background: var(--panel); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
header h1 { font-size: 18px; margin: 0; }
header .sub { font-size: 12px; color: var(--muted); }
