/* Mobile-first: el diseño base es para celular; desde 760px se acomoda a pantalla grande. */
:root {
  --bg: #f3f5f4;
  --panel: #ffffff;
  --text: #1c2421;
  --muted: #66736e;
  --line: #e2e8e5;
  --primary: #0a7d5a;
  --ok: #138a4b;
  --warn: #b7791f;
  --bad: #c53030;
  --wa: #25d366;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 16px; overscroll-behavior-y: none; }
button { touch-action: manipulation; }

/* ---------- barra superior ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px; background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 8px; min-width: 0; font-size: 16px; }
.brand strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--primary); flex: none; }
.tabs { display: flex; gap: 2px; flex: none; }
.tab { border: 0; background: none; padding: 8px 12px; border-radius: 10px; cursor: pointer; color: var(--muted); font: inherit; font-size: 15px; min-height: 40px; }
.tab.active { background: var(--bg); color: var(--text); font-weight: 600; }
.tab-icon { font-size: 19px; line-height: 1; }
.tab-label { display: none; }

main { max-width: 1000px; margin: 0 auto; padding: 14px 14px calc(110px + var(--safe-b)); }
.view { display: none; }
.view.active { display: block; }

/* ---------- contadores = filtros ---------- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 12px; }
.stat {
  font: inherit; text-align: left; cursor: pointer; background: var(--panel); border: 1.5px solid var(--line);
  border-radius: 12px; padding: 9px 10px; color: var(--text); min-width: 0;
}
.stat span { display: block; font-size: 22px; font-weight: 750; line-height: 1.1; }
.stat small { display: block; color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat.ok span { color: var(--ok); }
.stat.warn span { color: var(--warn); }
.stat.bad span { color: var(--bad); }
.stat.active { border-color: var(--text); box-shadow: inset 0 0 0 1px var(--text); }

/* ---------- buscador ---------- */
.toolbar { display: flex; gap: 8px; margin-bottom: 12px; align-items: stretch; }
.toolbar input[type=search] { flex: 1; min-width: 0; }
input, select, textarea {
  font: inherit; font-size: 16px; /* 16px evita el zoom automático de iPhone */
  padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; background: #fff; color: var(--text); width: 100%;
}
textarea { resize: vertical; }

/* ---------- botones ---------- */
.btn {
  font: inherit; font-size: 15px; border: 1px solid var(--line); background: #fff; color: var(--text);
  padding: 10px 14px; border-radius: 10px; cursor: pointer; white-space: nowrap; min-height: 44px;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }
.btn.wa { background: var(--wa); border-color: var(--wa); color: #08361d; font-weight: 650; }
.btn.danger { color: var(--bad); border-color: #f3caca; }
.btn.icon { width: 46px; padding: 0; font-size: 22px; line-height: 1; }
.btn.small { padding: 6px 10px; font-size: 14px; min-height: 36px; }
.btn.big { width: 100%; padding: 15px; font-size: 17px; }
.btn.link { border: 0; background: none; color: var(--muted); text-decoration: underline; padding: 6px 0; min-height: 0; margin-right: 14px; }
.desktop-only { display: none; }

.menu-wrap { position: relative; }
.menu { display: none; position: absolute; right: 0; top: calc(100% + 6px); background: #fff; border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.12); min-width: 240px; z-index: 10; overflow: hidden; }
.menu.open { display: block; }
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 14px 16px; font: inherit; cursor: pointer; }
.menu button + button { border-top: 1px solid var(--line); }
.menu button:active, .menu button:hover { background: var(--bg); }

/* ---------- lista de clientes (tarjetas) ---------- */
.lista { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }
.item {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 12px 12px 14px; display: flex; align-items: center; gap: 12px;
}
.item-info { flex: 1; min-width: 0; cursor: pointer; }
.item-top { display: flex; align-items: baseline; gap: 8px; }
.item-nombre { font-weight: 650; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-desc { flex: none; font-size: 13px; font-weight: 700; color: var(--primary); background: color-mix(in srgb, var(--primary) 12%, white); padding: 1px 7px; border-radius: 99px; }
.item-sub { color: var(--muted); font-size: 13px; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-estado { margin-top: 6px; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.item .btn.wa { flex: none; padding: 10px 14px; }

.badge { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: 12px; font-weight: 650; }
.badge.enviada { background: #e3f5ea; color: var(--ok); }
.badge.pendiente { background: #fdf3e1; color: var(--warn); }
.badge.vencida { background: #fde8e8; color: var(--bad); }
.badge-sub { font-size: 12px; color: var(--muted); text-decoration: underline dotted; cursor: pointer; }
.warn-text { font-size: 12px; color: var(--bad); }
.empty { text-align: center; color: var(--muted); padding: 40px 20px; margin: 0; }

.fab {
  position: fixed; right: 18px; bottom: calc(18px + var(--safe-b)); z-index: 6;
  width: 60px; height: 60px; border-radius: 50%; border: 0; background: var(--primary); color: #fff;
  font-size: 32px; line-height: 1; cursor: pointer; box-shadow: 0 8px 22px rgba(0,0,0,.22);
}
.fab:active { transform: scale(.95); }

/* ---------- configuración ---------- */
.config-grid { display: grid; grid-template-columns: 1fr; gap: 14px; }
.config-grid > .card-panel:last-child { order: -1; position: sticky; top: 62px; z-index: 2; padding: 10px; }
.config-grid > .card-panel:last-child h2 { display: none; }
.card-panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.card-panel h2, dialog h2 { margin: 0 0 14px; font-size: 18px; }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
label input, label textarea, label select { margin-top: 4px; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
input[type=color] { height: 46px; padding: 4px; }
input[type=file] { font-size: 14px; }
.hint { color: var(--muted); font-size: 12px; }
#formConfig .btn.primary { width: 100%; }

.preview { width: 100%; height: auto; border-radius: 12px; box-shadow: 0 6px 20px rgba(0,0,0,.14); display: block; }

/* ---------- diálogos: pantalla completa en celular ---------- */
dialog {
  border: 0; padding: 0; margin: 0; width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh;
  background: var(--panel); color: var(--text);
}
dialog[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: rgba(10, 20, 15, .45); }
dialog > form, dialog > div { display: flex; flex-direction: column; height: 100%; min-height: 0; }
dialog h2 { padding: calc(16px + env(safe-area-inset-top, 0px)) 16px 0; margin-bottom: 10px; }
.dlg-body { flex: 1; overflow-y: auto; padding: 0 16px 16px; -webkit-overflow-scrolling: touch; }
.dlg-grid { display: grid; grid-template-columns: 1fr; gap: 14px; }
.dlg-grid > div:last-child { order: -1; }
.dlg-actions {
  display: flex; gap: 8px; padding: 12px 16px calc(12px + var(--safe-b)); border-top: 1px solid var(--line); background: var(--panel);
}
.dlg-actions .btn { flex: 1; }
.dlg-actions .spacer { display: none; }
.dlg-actions .btn.danger { flex: 0 0 auto; }
.enviar p { margin: 0 0 10px; }
.enviar .preview { margin-bottom: 14px; }
.pasos ol { padding-left: 20px; margin: 6px 0; }
.pasos li { margin-bottom: 8px; }
.ok-text { color: var(--ok); font-size: 13px; }
.historial { list-style: none; padding: 0; margin: 0; }
.historial li { padding: 12px 0; border-bottom: 1px solid var(--line); font-size: 15px; }

#toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-b)); transform: translateX(-50%) translateY(20px);
  background: #1c2421; color: #fff; padding: 11px 16px; border-radius: 12px; opacity: 0; transition: .2s; pointer-events: none; z-index: 100;
  max-width: calc(100vw - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }


/* ---------- escanear ---------- */
.scan-box { position: relative; width: 100%; max-width: 480px; margin: 0 auto 14px; aspect-ratio: 1; background: #111; border-radius: 18px; overflow: hidden; }
.scan-box video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scan-marco { position: absolute; inset: 18%; border: 3px solid rgba(255,255,255,.9); border-radius: 18px; box-shadow: 0 0 0 999px rgba(0,0,0,.35); pointer-events: none; }
.scan-off { position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 14px; padding: 24px; text-align: center; color: #fff; background: #16201c; }
.scan-off p { margin: 0; line-height: 1.4; }
.scan-off .hint { color: #f3b0b0; font-size: 14px; }
.scan-box.activa .scan-off { display: none; }
.scan-box:not(.activa) .scan-marco { display: none; }

.resultado { max-width: 480px; margin: 0 auto 14px; background: var(--panel); border-radius: 18px; overflow: hidden; border: 1px solid var(--line); }
.res-estado { padding: 22px 16px; text-align: center; color: #fff; font-size: 30px; font-weight: 850; letter-spacing: .02em; }
.res-estado small { display: block; font-size: 15px; font-weight: 600; opacity: .92; letter-spacing: 0; margin-top: 4px; }
.res-estado.vigente { background: var(--ok); }
.res-estado.vencida { background: var(--bad); }
.res-estado.dudosa { background: var(--warn); }
.res-cuerpo { padding: 16px; text-align: center; }
.res-desc { font-size: 44px; font-weight: 900; color: var(--primary); line-height: 1; }
.res-nombre { font-size: 20px; font-weight: 700; margin-top: 8px; }
.res-datos { color: var(--muted); margin-top: 4px; font-size: 15px; }
.res-nota { margin-top: 10px; font-size: 14px; color: var(--warn); }
.resultado .btn.big { border-radius: 0; }
.buscar-manual { max-width: 480px; margin: 0 auto; display: flex; gap: 8px; }
.buscar-manual input { flex: 1; min-width: 0; }

/* ---------- pantallas grandes ---------- */
@media (min-width: 760px) {
  main { padding: 20px 20px 60px; }
  .topbar { padding: 12px 20px; }
  .tab-icon { display: none; }
  .tab-label { display: inline; }
  .stat { padding: 14px 16px; }
  .stat span { font-size: 26px; }
  .stat small { font-size: 13px; }
  .desktop-only { display: inline-block; }
  .fab { display: none; }
  .lista { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .config-grid { grid-template-columns: 1fr 1fr; align-items: start; }
  .config-grid > .card-panel:last-child { order: 0; top: 80px; padding: 16px; }
  .config-grid > .card-panel:last-child h2 { display: block; }

  dialog { width: min(860px, calc(100vw - 40px)); height: auto; max-height: calc(100dvh - 40px); margin: auto; border-radius: 18px; }
  #dlgEnviar, #dlgHistorial { width: min(480px, calc(100vw - 40px)); }
  dialog h2 { padding: 20px 22px 0; }
  .dlg-body { padding: 0 22px 18px; }
  .dlg-grid { grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; }
  .dlg-grid > div:last-child { order: 0; }
  .dlg-actions { padding: 14px 22px; }
  .dlg-actions .btn { flex: 0 0 auto; }
  .dlg-actions .spacer { display: block; flex: 1; }
}
@media (min-width: 1100px) {
  .lista { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

/* ---------- login / carga ---------- */
.login { display: none; min-height: 100dvh; align-items: center; justify-content: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 20px; padding: 26px 20px; }
.login-logo { display: block; width: 190px; max-width: 70%; margin: 0 auto 10px; }
.login-card h1 { text-align: center; font-size: 22px; margin: 6px 0 2px; }
.login-sub { text-align: center; color: var(--muted); margin: 0 0 18px; font-size: 14px; }
.login-error { color: var(--bad); text-align: center; font-size: 14px; min-height: 20px; margin: 10px 0 0; }
.cargando { display: none; min-height: 100dvh; align-items: center; justify-content: center; gap: 10px; color: var(--muted); }
.spinner { width: 20px; height: 20px; border: 3px solid var(--line); border-top-color: var(--primary); border-radius: 50%; animation: girar .8s linear infinite; }
@keyframes girar { to { transform: rotate(360deg); } }
body.arrancando .cargando { display: flex; }
body.sin-sesion .login { display: flex; }
body.arrancando .topbar, body.arrancando main, body.sin-sesion .topbar, body.sin-sesion main { display: none; }

.aviso { background: #fff8e6; border: 1px solid #f1d9a6; border-radius: 14px; padding: 12px 14px; margin-bottom: 12px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.aviso p { margin: 0; flex: 1 1 220px; font-size: 14px; }
.sesion { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line); font-size: 14px; color: var(--muted); flex-wrap: wrap; }
.sesion b { color: var(--text); word-break: break-all; }

/* que "hidden" siempre oculte, aunque otra regla le ponga display */
[hidden] { display: none !important; }

/* ---------- credencial Black ---------- */
.item-black { flex: none; font-size: 11px; font-weight: 800; letter-spacing: .08em; color: #F6DD8C; background: linear-gradient(135deg, #1b1b1b, #3a3a3a); padding: 2px 7px; border-radius: 99px; border: 1px solid #b38b3a; }
.res-tipo { display: inline-block; margin-top: 8px; font-size: 13px; font-weight: 800; letter-spacing: .1em; color: #F6DD8C; background: #141414; border: 1px solid #b38b3a; padding: 4px 12px; border-radius: 99px; }
