/* ============================================
   PWA & OFFLINE STYLES
   Estilos específicos para funcionalidades PWA
   ============================================ */

/* Variables PWA */
:root {
  --pwa-primary: #007bff;
  --pwa-success: #28a745;
  --pwa-warning: #ffc107;
  --pwa-danger: #dc3545;
  --pwa-info: #17a2b8;
  
  --offline-bg: rgba(220, 53, 69, 0.1);
  --offline-border: rgba(220, 53, 69, 0.3);
  --online-bg: rgba(40, 167, 69, 0.1);
  --online-border: rgba(40, 167, 69, 0.3);
  --syncing-bg: rgba(255, 193, 7, 0.1);
  --syncing-border: rgba(255, 193, 7, 0.3);
  
  --install-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  --sync-gradient: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
}

/* ============================================
   INDICADORES DE ESTADO PWA
   ============================================ */

/* Indicador principal de conectividad */
.connectivity-indicator {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 1050;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  border-radius: 25px;
  font-size: 0.85rem;
  font-weight: 600;
  color: white;
  transition: all 0.3s ease;
  opacity: 0;
  transform: translateX(100%);
  pointer-events: none;
}

.connectivity-indicator.show {
  opacity: 1;
  transform: translateX(0);
  pointer-events: auto;
}

.connectivity-indicator.online {
  background: linear-gradient(135deg, #28a745, #20c997);
  box-shadow: 0 4px 15px rgba(40, 167, 69, 0.3);
}

.connectivity-indicator.offline {
  background: linear-gradient(135deg, #dc3545, #c82333);
  box-shadow: 0 4px 15px rgba(220, 53, 69, 0.3);
}

.connectivity-indicator.reconnecting {
  background: linear-gradient(135deg, #ffc107, #e0a800);
  box-shadow: 0 4px 15px rgba(255, 193, 7, 0.3);
  color: #212529;
}

.connectivity-indicator i {
  animation: none;
}

.connectivity-indicator.reconnecting i {
  animation: pulse-icon 1.5s ease-in-out infinite;
}

@keyframes pulse-icon {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.1); opacity: 0.7; }
}

/* ============================================
   ESTADOS DE APLICACIÓN
   ============================================ */

/* Aplicación en modo offline */
.app-offline {
  position: relative;
}

.app-offline::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: repeating-linear-gradient(
    90deg,
    #dc3545 0px,
    #dc3545 10px,
    #ffffff 10px,
    #ffffff 20px
  );
  z-index: 9999;
  animation: offline-stripe 2s linear infinite;
}

@keyframes offline-stripe {
  0% { transform: translateX(-20px); }
  100% { transform: translateX(0px); }
}

/* Aplicación sincronizando */
.app-syncing {
  position: relative;
}

.app-syncing::after {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #ffc107, #e0a800, #ffc107);
  background-size: 200% 100%;
  z-index: 9999;
  animation: syncing-progress 2s ease-in-out infinite;
}

@keyframes syncing-progress {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ============================================
   BOTONES PWA
   ============================================ */

/* Botón de instalación PWA */
.pwa-install-button {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 1040;
  background: var(--install-gradient);
  color: white;
  border: none;
  border-radius: 50px;
  padding: 1rem 1.5rem;
  font-size: 0.9rem;
  font-weight: 600;
  box-shadow: 0 4px 20px rgba(0, 123, 255, 0.3);
  transition: all 0.3s ease;
  opacity: 0;
  transform: translateY(100px);
  pointer-events: none;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.pwa-install-button.show {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

.pwa-install-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 25px rgba(0, 123, 255, 0.4);
  color: white;
}

.pwa-install-button:active {
  transform: translateY(-1px);
}

.pwa-install-button i {
  font-size: 1.1rem;
}

/* Botón de actualización */
.pwa-update-button {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 1050;
  background: linear-gradient(135deg, #17a2b8, #138496);
  color: white;
  border: none;
  border-radius: 15px;
  padding: 1.5rem 2rem;
  font-size: 1rem;
  font-weight: 600;
  box-shadow: 0 8px 30px rgba(23, 162, 184, 0.3);
  opacity: 0;
  transform: translate(-50%, -50%) scale(0.8);
  pointer-events: none;
  transition: all 0.3s ease;
}

.pwa-update-button.show {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
  pointer-events: auto;
}

.pwa-update-button:hover {
  box-shadow: 0 10px 35px rgba(23, 162, 184, 0.4);
  color: white;
}

/* ============================================
   SINCRONIZACIÓN
   ============================================ */

/* Indicador de sincronización */
.sync-indicator {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 600;
  transition: all 0.3s ease;
  cursor: pointer;
}

.sync-indicator.idle {
  background: var(--online-bg);
  color: var(--pwa-success);
  border: 1px solid var(--online-border);
}

.sync-indicator.syncing {
  background: var(--syncing-bg);
  color: #856404;
  border: 1px solid var(--syncing-border);
}

.sync-indicator.error {
  background: var(--offline-bg);
  color: var(--pwa-danger);
  border: 1px solid var(--offline-border);
}

.sync-indicator:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.sync-indicator i {
  transition: transform 0.3s ease;
}

.sync-indicator.syncing i {
  animation: sync-rotate 1s linear infinite;
}

@keyframes sync-rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* Progress de sincronización */
.sync-progress {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: rgba(255,255,255,0.2);
  z-index: 9999;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.sync-progress.show {
  opacity: 1;
}

.sync-progress-bar {
  height: 100%;
  background: var(--sync-gradient);
  background-size: 30px 30px;
  animation: sync-progress-move 2s linear infinite;
  transition: width 0.5s ease;
  border-radius: 0 2px 2px 0;
}

@keyframes sync-progress-move {
  0% { background-position: 0 0; }
  100% { background-position: 30px 0; }
}

/* ============================================
   NOTIFICACIONES OFFLINE
   ============================================ */

/* Toast offline */
.offline-toast {
  position: fixed;
  bottom: 20px;
  left: 20px;
  right: 20px;
  max-width: 400px;
  margin: 0 auto;
  z-index: 1050;
  background: rgba(220, 53, 69, 0.95);
  color: white;
  border-radius: 15px;
  padding: 1rem 1.5rem;
  transform: translateY(100px);
  opacity: 0;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
}

.offline-toast.show {
  transform: translateY(0);
  opacity: 1;
}

.offline-toast-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.offline-toast-body {
  font-size: 0.9rem;
  opacity: 0.9;
}

/* Toast de sincronización exitosa */
.sync-success-toast {
  background: rgba(40, 167, 69, 0.95);
}

/* Toast de error de sincronización */
.sync-error-toast {
  background: rgba(220, 53, 69, 0.95);
}

/* ============================================
   ALMACENAMIENTO LOCAL
   ============================================ */

/* Indicador de almacenamiento */
.storage-indicator {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1rem;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  margin-bottom: 1rem;
}

.storage-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  font-size: 1.2rem;
}

.storage-info {
  flex: 1;
}

.storage-title {
  font-weight: 600;
  margin: 0 0 0.25rem 0;
  color: #2c3e50;
}

.storage-details {
  font-size: 0.85rem;
  color: #6c757d;
  margin: 0;
}

.storage-progress {
  height: 8px;
  background: rgba(0,0,0,0.1);
  border-radius: 4px;
  overflow: hidden;
  margin-top: 0.5rem;
}

.storage-progress-bar {
  height: 100%;
  background: linear-gradient(90deg, #28a745, #20c997);
  border-radius: 4px;
  transition: width 0.5s ease;
}

.storage-progress-bar.warning {
  background: linear-gradient(90deg, #ffc107, #e0a800);
}

.storage-progress-bar.danger {
  background: linear-gradient(90deg, #dc3545, #c82333);
}

/* ============================================
   MODO STANDALONE (APP INSTALADA)
   ============================================ */

/* Estilos cuando la app está instalada */
@media all and (display-mode: standalone) {
  /* Ocultar elementos específicos del navegador */
  .browser-only {
    display: none !important;
  }
  
  /* Ajustar padding superior para compensar la falta de barra del navegador */
  body {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }
  
  /* Header de la app ajustado */
  .main-header {
    padding-top: calc(1rem + env(safe-area-inset-top));
  }
  
  /* Botón de instalación no es necesario */
  .pwa-install-button {
    display: none !important;
  }
}

/* ============================================
   GESTOS Y INTERACCIONES TÁCTILES
   ============================================ */

/* Pull to refresh */
.pull-to-refresh {
  position: relative;
  overflow: hidden;
}

.pull-to-refresh-indicator {
  position: absolute;
  top: -60px;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--pwa-primary);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  opacity: 0;
}

.pull-to-refresh.pulling .pull-to-refresh-indicator {
  opacity: 1;
  transform: translateX(-50%) translateY(80px);
}

.pull-to-refresh.refreshing .pull-to-refresh-indicator i {
  animation: sync-rotate 1s linear infinite;
}

/* Swipe gestures */
.swipe-container {
  position: relative;
  overflow: hidden;
}

.swipe-actions {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  display: flex;
  align-items: center;
  background: linear-gradient(90deg, transparent, rgba(220, 53, 69, 0.1));
  padding: 0 1rem;
  transform: translateX(100%);
  transition: transform 0.3s ease;
}

.swipe-container.swiped .swipe-actions {
  transform: translateX(0);
}

/* ============================================
   LOADING ESTADOS
   ============================================ */

/* Skeleton loading */
.skeleton {
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: skeleton-loading 1.5s infinite;
  border-radius: 4px;
}

@keyframes skeleton-loading {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.skeleton-text {
  height: 1rem;
  margin-bottom: 0.5rem;
}

.skeleton-text:last-child {
  width: 60%;
  margin-bottom: 0;
}

.skeleton-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.skeleton-button {
  height: 40px;
  width: 120px;
  border-radius: 8px;
}

/* Loading overlay */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.loading-overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.loading-spinner {
  text-align: center;
  color: var(--pwa-primary);
}

.loading-spinner i {
  font-size: 3rem;
  margin-bottom: 1rem;
  animation: sync-rotate 1s linear infinite;
}

.loading-spinner p {
  font-size: 1.1rem;
  font-weight: 600;
  margin: 0;
}

/* ============================================
   RESPONSIVE PWA
   ============================================ */

/* Móviles */
@media (max-width: 768px) {
  .connectivity-indicator {
    top: 10px;
    right: 10px;
    padding: 0.4rem 0.8rem;
    font-size: 0.8rem;
  }
  
  .pwa-install-button {
    bottom: 10px;
    right: 10px;
    padding: 0.8rem 1.2rem;
    font-size: 0.85rem;
  }
  
  .offline-toast {
    bottom: 10px;
    left: 10px;
    right: 10px;
    padding: 0.8rem 1rem;
  }
  
  .storage-indicator {
    padding: 0.8rem;
    flex-direction: column;
    text-align: center;
    gap: 0.8rem;
  }
}

/* Tablets */
@media (min-width: 769px) and (max-width: 1024px) {
  .pwa-install-button {
    bottom: 30px;
    right: 30px;
  }
  
  .connectivity-indicator {
    top: 30px;
    right: 30px;
  }
}

/* ============================================
   DARK MODE PWA
   ============================================ */

@media (prefers-color-scheme: dark) {
  .storage-indicator {
    background: #2d2d2d;
    color: white;
  }
  
  .storage-title {
    color: white;
  }
  
  .storage-details {
    color: #ccc;
  }
  
  .skeleton {
    background: linear-gradient(90deg, #2d2d2d 25%, #3d3d3d 50%, #2d2d2d 75%);
    background-size: 200% 100%;
  }
  
  .loading-overlay {
    background: rgba(26, 26, 26, 0.9);
  }
}

/* ============================================
   ANIMACIONES ESPECÍFICAS PWA
   ============================================ */

/* Bounce para instalación exitosa */
@keyframes pwa-bounce {
  0%, 20%, 53%, 80%, 100% { transform: translate3d(0,0,0); }
  40%, 43% { transform: translate3d(0,-30px,0); }
  70% { transform: translate3d(0,-15px,0); }
  90% { transform: translate3d(0,-4px,0); }
}

.pwa-installed {
  animation: pwa-bounce 1s ease;
}

/* Slide para notificaciones */
@keyframes slide-in-right {
  from { transform: translateX(100%); }
  to { transform: translateX(0); }
}

@keyframes slide-out-right {
  from { transform: translateX(0); }
  to { transform: translateX(100%); }
}

.notification-slide-in {
  animation: slide-in-right 0.3s ease;
}

.notification-slide-out {
  animation: slide-out-right 0.3s ease;
}

/* ============================================
   ACCESSIBILITY PWA
   ============================================ */

/* High contrast mode */
@media (prefers-contrast: high) {
  .connectivity-indicator {
    border: 2px solid currentColor;
  }
  
  .pwa-install-button {
    border: 2px solid white;
  }
  
  .sync-indicator {
    border: 2px solid currentColor;
  }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .sync-indicator.syncing i,
  .loading-spinner i,
  .pull-to-refresh.refreshing .pull-to-refresh-indicator i {
    animation: none;
  }
  
  .connectivity-indicator,
  .pwa-install-button,
  .offline-toast {
    transition: none;
  }
}

/* Focus visible para navegación por teclado */
.connectivity-indicator:focus-visible,
.pwa-install-button:focus-visible,
.sync-indicator:focus-visible {
  outline: 2px solid var(--pwa-primary);
  outline-offset: 2px;
}

/* ============================================
   UTILIDADES PWA
   ============================================ */

/* Ocultar elementos cuando no hay JS */
.no-js .js-only {
  display: none !important;
}

/* Mostrar elementos solo cuando no hay conexión */
.offline-only {
  display: none;
}

.app-offline .offline-only {
  display: block;
}

/* Mostrar elementos solo cuando hay conexión */
.online-only {
  display: block;
}

.app-offline .online-only {
  display: none;
}

/* Estados de carga específicos */
.loading-state {
  pointer-events: none;
  opacity: 0.6;
  position: relative;
}

.loading-state::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 20px;
  height: 20px;
  margin: -10px 0 0 -10px;
  border: 2px solid rgba(0,0,0,0.1);
  border-top-color: var(--pwa-primary);
  border-radius: 50%;
  animation: sync-rotate 1s linear infinite;
}

/* Badges para estados PWA */
.pwa-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.25rem 0.5rem;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.pwa-badge.installed {
  background: rgba(40, 167, 69, 0.1);
  color: var(--pwa-success);
}

.pwa-badge.offline {
  background: rgba(220, 53, 69, 0.1);
  color: var(--pwa-danger);
}

.pwa-badge.cached {
  background: rgba(23, 162, 184, 0.1);
  color: var(--pwa-info);
}

.pwa-badge.syncing {
  background: rgba(255, 193, 7, 0.1);
  color: #856404;
}