:root {
  --primary: #1e3a8a;
  --primary-dark: #172554;
  --primary-light: #3b82f6;
  --accent: #f59e0b;
  --success: #10b981;
  --danger: #ef4444;
  --warning: #f97316;
  --bg: #f8fafc;
  --card-bg: #ffffff;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #cbd5e1;
  --radius: 12px;
  --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background-color: var(--bg);
  color: var(--text);
  line-height: 1.5;
  padding-bottom: 70px;
}

/* Evita mostrar contenido mientras se determina el tipo de dispositivo. */
body.preflight > * {
  visibility: hidden;
}

/* Portada institucional a pantalla completa, independiente de la aplicación. */
html.desktop-mode { height: 100%; overflow: hidden; overflow: clip; }
body.desktop-mode {
  height: 100vh; height: 100dvh; min-height: 0; padding: 0; overflow: hidden; overflow: clip;
  color: #f5f6f7;
  background: radial-gradient(ellipse at 78% 45%, rgba(210,8,18,.09), transparent 38%), linear-gradient(135deg,#0c0e10,#070809 52%,#101113);
  -webkit-font-smoothing: antialiased;
}
body.desktop-mode > *:not(.desktop-landing) { display: none !important; }
.desktop-landing {
  position: relative; isolation: isolate; width: 100%; height: 100%; min-height: 0;
  display: grid; grid-template-columns: minmax(0,1fr); grid-template-rows: minmax(0,1fr); place-items: center;
  padding: clamp(24px,4.5vw,72px); overflow: hidden;
}
.desktop-landing::before {
  content: ''; position: absolute; z-index: -2; inset: 0; pointer-events: none;
  background-image: linear-gradient(rgba(255,255,255,.025) 1px,transparent 1px),linear-gradient(90deg,rgba(255,255,255,.025) 1px,transparent 1px);
  background-size: 72px 72px; mask-image: radial-gradient(ellipse at 78% 45%,#000,transparent 76%);
}
.desktop-landing::after {
  content: ''; position: absolute; z-index: -1; width: 55vw; height: 1px; right: -10%; top: 18%; transform: rotate(-28deg);
  background: linear-gradient(90deg,transparent,rgba(255,255,255,.13),transparent); box-shadow: 0 380px 0 rgba(255,255,255,.025); pointer-events: none;
}
.desktop-landing__panel {
  width: min(1440px,100%); position: relative; transform-origin: center;
  display: grid; grid-template-columns: minmax(0,1.1fr) minmax(0,.9fr); align-items: center;
  column-gap: clamp(32px,6vw,96px); row-gap: clamp(30px,5vh,64px);
}
.desktop-landing__copy { position: relative; z-index: 2; }
.desktop-landing__eyebrow {
  max-width: 510px; margin: 0 0 25px; color: #a3a5aa; font-size: clamp(11px,1.05vw,14px);
  font-weight: 500; letter-spacing: .16em; text-transform: uppercase; line-height: 1.8;
}
.desktop-landing__eyebrow strong { color: #fff; font-weight: 750; white-space: nowrap; }
.desktop-landing__separator { width: min(100%,460px); height: 1px; margin-bottom: 32px; background: linear-gradient(90deg,rgba(255,255,255,.14),transparent); }
.desktop-landing__separator::before { content: ''; display: block; width: 86px; height: 2px; background: #e51221; box-shadow: 0 0 24px rgba(229,18,33,.4); }
.desktop-landing h1 { margin: 0; color: #f5f6f7; font-size: clamp(45px,7.8vw,120px); font-weight: 300; letter-spacing: -.065em; line-height: .95; }
.desktop-landing h1 span { display: block; margin-top: .06em; font-weight: 780; letter-spacing: -.055em; }
.desktop-landing h2 { margin: 30px 0 0; color: #eceef1; font-size: clamp(18px,1.65vw,25px); font-weight: 500; line-height: 1.4; letter-spacing: -.018em; }
.desktop-landing__message { margin: 17px 0 0; color: #9da2a9; font-size: clamp(13px,1.1vw,16px); line-height: 1.8; }
.brand-lockup { position: relative; display: grid; place-items: center; width: 100%; aspect-ratio: 1; max-width: 590px; justify-self: end; }
.brand-lockup::before { content: ''; position: absolute; inset: 8%; border-radius: 50%; background: radial-gradient(circle,rgba(210,8,18,.13),transparent 65%); filter: blur(30px); }
.desktop-orbit { position: absolute; border: 1px solid rgba(255,255,255,.08); border-radius: 50%; }
.desktop-orbit--outer { inset: 1%; border-style: dashed; }
.desktop-orbit--inner { inset: 14%; border-color: rgba(210,8,18,.19); }
.desktop-device {
  position: relative; z-index: 1; width: 49%; aspect-ratio: .52; display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 20px; border: 2px solid #44454b; border-radius: 38px; transform: rotate(-9deg);
  background: linear-gradient(155deg,#26282c 0%,#121316 30%,#0c0d0f 70%,#202125);
  box-shadow: 28px 40px 65px rgba(0,0,0,.6),inset 0 0 0 5px #070809,0 0 0 1px #111214;
}
.desktop-device::after { content: ''; position: absolute; inset: 6px; border-radius: 30px; border: 1px solid rgba(255,255,255,.06); pointer-events: none; }
.desktop-device__camera { position: absolute; top: 14px; width: 32%; height: 11px; border-radius: 99px; background: #030405; border: 1px solid #28292b; }
.brand-lockup__logo { display: flex; align-items: center; justify-content: center; min-width: 0; }
.brand-lockup__logo img { display: block; width: 100%; height: auto; object-fit: contain; }
.brand-lockup__logo--servijur { width: 94%; }
.desktop-device__label { margin-top: 20px; text-align: center; color: #a5a6ab; font-size: clamp(5px,.55vw,8px); letter-spacing: .12em; line-height: 1.7; }
.desktop-device__lines { display: flex; gap: 6px; margin-top: 26px; align-items: center; }
.desktop-device__lines i { display: block; width: 24px; height: 3px; border-radius: 9px; background: #282a2e; }
.desktop-device__lines i:first-child { background: #d20812; }
.desktop-device__home { position: absolute; bottom: 14px; width: 31%; height: 3px; border-radius: 9px; background: #65676c; }
.desktop-node { position: absolute; z-index: 2; width: 7px; height: 7px; border-radius: 50%; background: #e51221; box-shadow: 0 0 0 6px rgba(229,18,33,.07),0 0 16px rgba(229,18,33,.4); }
.desktop-node--one { top: 14%; right: 17%; }
.desktop-node--two { bottom: 12%; left: 21%; width: 5px; height: 5px; }
.desktop-landing__footer { grid-column: 1 / -1; display: flex; align-items: center; justify-content: space-between; gap: 24px; border-top: 1px solid rgba(255,255,255,.09); padding-top: 24px; }
.desktop-landing__footer > p { color: #c4c7cc; font-size: 11px; font-weight: 700; letter-spacing: .08em; }
.desktop-landing__footer > p span { display: inline-block; margin-left: 18px; color: #727780; font-size: 11px; font-weight: 400; letter-spacing: 0; }
.desktop-partner { display: flex; align-items: center; gap: 18px; }
.desktop-partner > span { color: #727780; font-size: 10px; }
.brand-lockup__logo--nps { width: auto; max-width: 80px; }
.brand-lockup__logo--nps img { width: auto; height: 48px; max-width: 80px; filter: brightness(0) invert(1); opacity: .85; }
@media (max-width: 900px) {
  .desktop-landing__panel { column-gap: 24px; }
  .desktop-landing h1 { font-size: clamp(40px,7.7vw,72px); }
  .desktop-device { border-radius: 28px; padding: 13px; }
  .desktop-device::after { border-radius: 22px; }
  .desktop-landing h2 { font-size: 18px; }
}
@media (max-width: 650px) {
  .desktop-landing { padding: 24px; }
  .desktop-landing__panel { grid-template-columns: minmax(0,1fr); row-gap: 28px; }
  .desktop-landing__copy { text-align: center; }
  .desktop-landing__eyebrow { margin: 0 0 18px; font-size: 10px; }
  .desktop-landing__separator { width: 75%; margin: 0 auto 24px; }
  .desktop-landing__separator::before { margin: 0 auto; }
  .desktop-landing h1 { font-size: clamp(35px,10.3vw,65px); }
  .desktop-landing h2 { margin-top: 20px; font-size: 17px; }
  .desktop-landing__message { font-size: 12px; margin-top: 12px; }
  .brand-lockup { width: min(76vw,340px); justify-self: center; }
  .desktop-device__label { font-size: 5px; margin-top: 12px; }
  .desktop-device__lines { margin-top: 14px; }
  .desktop-device__lines i { width: 16px; }
  .desktop-landing__footer { padding-top: 16px; gap: 12px; }
  .desktop-landing__footer > p { font-size: 9px; }
  .desktop-landing__footer > p span { display: block; margin: 4px 0 0; font-size: 8px; }
  .desktop-partner { gap: 8px; }
  .desktop-partner > span { font-size: 8px; }
  .brand-lockup__logo--nps img { height: 40px; max-width: 54px; }
}

@media (max-height: 750px) and (min-width: 651px) {
  .desktop-landing { padding: 24px 4.5vw; }
  .desktop-landing__panel { row-gap: 24px; }
  .brand-lockup { width: min(100%,calc(100dvh - 160px)); }
  .desktop-landing h1 { font-size: clamp(45px,min(7.8vw,14vh),100px); }
  .desktop-landing__eyebrow { margin-bottom: 20px; }
  .desktop-landing__separator { margin-bottom: 26px; }
  .desktop-landing h2 { margin-top: 24px; }
  .desktop-landing__footer { padding-top: 18px; }
}

/* En el navegador móvil no se expone ninguna parte de la aplicación. */
body.mobile-install-mode {
  min-height: 100vh;
  padding: 0;
  background: linear-gradient(155deg, #07152f, #1e3a8a);
}

body.mobile-install-mode > *:not(.install-gate):not(.app-dialog) {
  display: none !important;
}

.install-gate {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.install-gate[hidden] {
  display: none !important;
}

.install-only-button {
  width: min(100%, 340px);
  min-height: 76px;
  padding: 18px 30px;
  border: 2px solid rgba(255, 255, 255, 0.8);
  border-radius: 18px;
  background: #fff;
  color: #172554;
  font: inherit;
  font-size: 1.35rem;
  font-weight: 900;
  letter-spacing: 0.08em;
  cursor: pointer;
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.35);
}

.install-only-button:disabled {
  opacity: 0.75;
  cursor: default;
}

/* En celular, antes de validar el login, no existe acceso visual a la aplicación. */
body.mobile-mode.auth-locked {
  min-height: 100vh;
  padding-bottom: 0;
  background: linear-gradient(160deg, var(--primary-dark), var(--primary));
}

body.mobile-mode.auth-locked .desktop-landing,
body.mobile-mode.auth-locked .app-header,
body.mobile-mode.auth-locked .install-gate,
body.mobile-mode.auth-locked .bottom-nav,
body.mobile-mode.auth-locked .view:not(#viewLogin) {
  display: none !important;
}

body.mobile-mode.auth-locked .container {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}

body.mobile-mode.auth-locked #viewLogin {
  display: block !important;
  width: 100%;
  max-width: 430px;
}

body.mobile-mode.auth-locked #viewLogin .card {
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
}

.login-brand {
  display: block;
  width: 160px;
  max-width: 100%;
  height: auto;
  margin: 0 auto 18px;
}

.login-error {
  min-height: 24px;
  margin: -4px 0 10px;
  color: var(--danger);
  font-size: 0.85rem;
  font-weight: 700;
  text-align: center;
}

/* Header & Status Bar */
header.app-header {
  background: linear-gradient(135deg, var(--primary-dark), var(--primary));
  color: white;
  padding: 12px 16px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
}

.header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.brand-title {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.brand-sub {
  font-size: 0.75rem;
  opacity: 0.85;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
}

.status-badge.online {
  background-color: #064e3b;
  color: #a7f3d0;
  border: 1px solid #059669;
}

.status-badge.offline {
  background-color: #7f1d1d;
  color: #fecaca;
  border: 1px solid #dc2626;
}

.sync-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid rgba(255, 255, 255, 0.15);
  font-size: 0.8rem;
}

.sync-btn {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.4);
  color: white;
  padding: 4px 12px;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
}

.sync-btn:active {
  background: rgba(255, 255, 255, 0.35);
}

/* Contenedor Principal */
main.container {
  max-width: 600px;
  margin: 0 auto;
  padding: 16px;
}

/* Vistas */
.view {
  display: none;
}

.view.active {
  display: block;
}

/* Tarjetas */
.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.card-title {
  font-size: 1.1rem;
  font-weight: 700;
  margin-bottom: 12px;
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* Formularios y Controles Operativos */
.form-group {
  margin-bottom: 14px;
}

label {
  display: block;
  font-weight: 600;
  font-size: 0.85rem;
  margin-bottom: 6px;
  color: var(--text);
}

.required-badge {
  color: var(--danger);
}

input[type="text"],
input[type="password"],
input[type="search"],
select,
textarea {
  width: 100%;
  padding: 14px 12px;
  font-size: 1rem;
  border: 2px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
}

input:focus, select:focus, textarea:focus {
  border-color: var(--primary-light);
}

/* Botones Operativos de Gran Tamaño */
.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 52px;
  padding: 12px 20px;
  font-size: 1rem;
  font-weight: 700;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  text-decoration: none;
  box-shadow: var(--shadow);
  transition: transform 0.1s, opacity 0.2s;
}

.btn:active {
  transform: scale(0.98);
}

.btn-primary {
  background-color: var(--primary);
  color: white;
}

.btn-success {
  background-color: var(--success);
  color: white;
}

.btn-danger {
  background-color: var(--danger);
  color: white;
}

.btn-secondary {
  background-color: #64748b;
  color: white;
}

.btn-outline {
  background: transparent;
  border: 2px solid var(--border);
  color: var(--text);
}

.btn-group {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}

/* Listado de Piezas */
.search-container {
  margin-bottom: 12px;
}

.tabs-filter {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  overflow-x: auto;
}

.tab-btn {
  flex: 1;
  padding: 10px 8px;
  font-size: 0.8rem;
  font-weight: 700;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
}

.tab-btn.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.pieza-card {
  background: white;
  border: 1px solid var(--border);
  border-left: 6px solid var(--primary);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.05);
  cursor: pointer;
  transition: transform 0.1s;
}

.pieza-card:active {
  transform: translateY(1px);
}

.pieza-card.entregado {
  border-left-color: var(--success);
}

.pieza-card.no_entregado {
  border-left-color: var(--danger);
}

.pieza-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.pieza-id {
  font-weight: 800;
  font-size: 0.95rem;
  color: var(--primary-dark);
}

.pieza-orden {
  background: #e2e8f0;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 12px;
}

.pieza-cliente {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--primary);
  margin-bottom: 4px;
}

.pieza-destinatario {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 4px;
}

.pieza-domicilio {
  font-size: 0.85rem;
  color: #334155;
  margin-bottom: 8px;
}

.pieza-badge {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 6px;
}

.badge-pendiente {
  background: #fef3c7;
  color: #92400e;
}

.badge-entregado {
  background: #dcfce7;
  color: #166534;
}

.badge-no-entregado {
  background: #fee2e2;
  color: #991b1b;
}

/* Componente de Firma (Canvas) */
.signature-box {
  border: 2px dashed var(--border);
  border-radius: 8px;
  background: #fafafa;
  position: relative;
  touch-action: none;
}

canvas#signatureCanvas {
  width: 100%;
  height: 180px;
  display: block;
  background: #ffffff;
  border-radius: 6px;
}

.signature-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px;
  background: #f1f5f9;
  border-top: 1px solid var(--border);
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* Captura de Fotos */
.photo-inputs-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.photo-capture-card {
  border: 2px dashed var(--border);
  border-radius: 8px;
  padding: 12px;
  text-align: center;
  background: #f8fafc;
  cursor: pointer;
}

.photo-preview {
  width: 100%;
  height: 110px;
  object-fit: cover;
  border-radius: 6px;
  margin-top: 8px;
  display: none;
  border: 1px solid var(--border);
}

/* Geolocalización GPS */
.gps-box {
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  border-radius: 8px;
  padding: 12px;
  font-size: 0.85rem;
  margin-bottom: 14px;
}

.gps-row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 4px;
}

/* Barra de Navegación Inferior */
nav.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 60px;
  background: white;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-around;
  align-items: center;
  z-index: 100;
  box-shadow: 0 -2px 5px rgba(0,0,0,0.05);
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  width: 33%;
  height: 100%;
}

.nav-item.active {
  color: var(--primary);
}

.nav-icon {
  font-size: 1.3rem;
  margin-bottom: 2px;
}

.badge-counter {
  background: var(--accent);
  color: white;
  border-radius: 10px;
  padding: 1px 6px;
  font-size: 0.7rem;
  font-weight: 700;
  margin-left: 4px;
}

/* Mensajes integrados: diálogo modal, foco contenido y fondo oscurecido. */
html.message-open, html.message-open body { overflow: hidden; }
.app-dialog {
  --dialog-color: #1e3a8a; --dialog-tint: #eff6ff;
  position: fixed; top: 50%; left: 50%; transform: translate(-50%,-50%); margin: 0;
  width: min(420px,calc(100% - 36px)); max-height: calc(100vh - 36px); max-height: calc(100dvh - 36px);
  padding: 28px; overflow: auto; border: 1px solid #e2e8f0; border-top: 3px solid var(--dialog-color); border-radius: 24px;
  background: #fff; color: #0f172a; box-shadow: 0 30px 90px rgba(2,6,23,.4); font: inherit; line-height: 1.5;
}
.app-dialog:not([open]) { display: none; }
.app-dialog::backdrop { background: rgba(4,10,25,.72); backdrop-filter: blur(5px); -webkit-backdrop-filter: blur(5px); }
.app-dialog[data-tone="warning"], .app-dialog[data-tone="logout"] { --dialog-color: #b45309; --dialog-tint: #fff7ed; }
.app-dialog[data-tone="error"] { --dialog-color: #b91c1c; --dialog-tint: #fef2f2; }
.app-dialog[data-tone="success"] { --dialog-color: #047857; --dialog-tint: #ecfdf5; }
.app-dialog__brand { display: flex; align-items: center; gap: 7px; margin-bottom: 24px; color: #64748b; font-size: 10px; font-weight: 800; letter-spacing: .13em; }
.app-dialog__brand-dot { width: 6px; height: 6px; border-radius: 50%; background: #d20812; }
.app-dialog__icon { display: grid; place-items: center; width: 54px; height: 54px; margin-bottom: 19px; border: 1px solid color-mix(in srgb,var(--dialog-color) 12%,transparent); border-radius: 17px; color: var(--dialog-color); background: var(--dialog-tint); }
.app-dialog__icon svg { width: 26px; height: 26px; }
.app-dialog__title { margin: 0; color: #0f172a; font-size: 23px; font-weight: 780; letter-spacing: -.035em; line-height: 1.2; }
.app-dialog__message { margin: 13px 0 25px; color: #64748b; font-size: 14px; line-height: 1.7; white-space: pre-line; overflow-wrap: anywhere; }
.app-dialog__actions { display: flex; flex-direction: column-reverse; gap: 10px; }
.app-dialog__button { display: block; width: 100%; min-height: 48px; padding: 12px 16px; border-radius: 11px; border: 1px solid transparent; font: inherit; font-size: 14px; font-weight: 750; cursor: pointer; }
.app-dialog__button--primary { color: #fff; background: var(--primary); }
.app-dialog[data-tone="logout"] .app-dialog__button--primary { background: #b91c1c; }
.app-dialog__button--secondary { color: #334155; background: #f8fafc; border-color: #e2e8f0; }
.app-dialog__button:focus-visible { outline: 3px solid #93c5fd; outline-offset: 3px; }
.app-dialog__button:active { filter: brightness(.95); }
#btnCerrarSesion:focus-visible { outline: 2px solid var(--primary-light); outline-offset: -4px; border-radius: 8px; }
@media (max-height: 520px) { .app-dialog { padding: 20px 24px; } .app-dialog__brand { margin-bottom: 14px; } .app-dialog__icon { margin-bottom: 12px; } .app-dialog__message { margin-bottom: 18px; } }
