/* ================================================================
   MOBILE TABLES FIX - Solución definitiva para tablas en móvil
   ================================================================ */

/* PASO 1: Resetear configuración base de HTML y BODY */
@media screen and (max-width: 768px) {
  html {
    overflow-x: hidden !important;
    overflow-y: auto !important;
    width: 100vw !important;
    max-width: 100vw !important;
    height: auto !important;
    max-height: none !important;
    position: static !important;
    box-sizing: border-box !important;
  }
  
  body {
    overflow-x: hidden !important;
    overflow-y: auto !important;
    width: 100vw !important;
    max-width: 100vw !important;
    height: auto !important;
    max-height: none !important;
    min-height: 100vh !important;
    position: static !important;
    margin: 0 !important;
    padding-top: 60px !important; /* Espacio para menú fijo */
    padding-bottom: 50px !important;
    box-sizing: border-box !important;
  }
}

/* PASO 2: Contenedor principal - debe permitir scroll natural */
@media screen and (max-width: 768px) {
  .container,
  body > .container {
    width: 100% !important;
    max-width: 100vw !important;
    height: auto !important;
    max-height: none !important;
    min-height: auto !important;
    overflow: visible !important;
    position: static !important;
    display: block !important;
    padding: 10px !important;
    margin: 0 auto !important;
    box-sizing: border-box !important;
  }
}

/* PASO 3: Contenedor de tabla - scroll horizontal permitido */
@media screen and (max-width: 768px) {
  .table-container,
  .table-responsive,
  .table-wrapper {
    width: 100% !important;
    max-width: 100vw !important;
    height: auto !important;
    max-height: none !important;
    overflow-x: auto !important;
    overflow-y: visible !important;
    -webkit-overflow-scrolling: touch !important;
    position: relative !important;
    display: block !important;
    margin: 0 !important;
    padding: 0 !important;
    box-sizing: border-box !important;
    border: none !important;
    border-radius: 0 !important;
  }
}

/* PASO 4: Tabla - puede ser más ancha que el viewport */
@media screen and (max-width: 768px) {
  table,
  #seasonsTable,
  #warsTable,
  #playersTable,
  #cwlTable,
  .attack-table,
  .war-members-table {
    width: max-content !important;
    min-width: 100% !important;
    max-width: none !important; /* CLAVE: Sin límite máximo */
    height: auto !important;
    display: table !important;
    table-layout: auto !important;
    border-collapse: collapse !important;
    margin: 0 !important;
    box-sizing: border-box !important;
  }
}

/* PASO 5: Celdas y headers - tamaño automático sin restricciones */
@media screen and (max-width: 768px) {
  table th,
  table td,
  #seasonsTable th,
  #seasonsTable td,
  #warsTable th,
  #warsTable td,
  #playersTable th,
  #playersTable td,
  #cwlTable th,
  #cwlTable td,
  .attack-table th,
  .attack-table td {
    width: auto !important;
    min-width: 80px !important; /* Mínimo razonable */
    max-width: none !important; /* Sin límite máximo */
    padding: 8px 10px !important;
    font-size: 0.85em !important;
    white-space: nowrap !important;
    overflow: visible !important;
    text-overflow: clip !important;
    box-sizing: border-box !important;
  }
  
  /* Headers sticky */
  table th,
  #seasonsTable th,
  #warsTable th,
  #playersTable th,
  #cwlTable th {
    position: sticky !important;
    top: 0 !important;
    z-index: 10 !important;
    background-color: #374151 !important;
    color: white !important;
  }
}

/* PASO 6: Filtros container - debe wrappear sin overflow */
@media screen and (max-width: 768px) {
  .filters-container {
    width: 100% !important;
    max-width: 100vw !important;
    overflow: visible !important;
    flex-wrap: wrap !important;
    padding: 10px !important;
    margin-bottom: 10px !important;
    box-sizing: border-box !important;
    display: flex !important;
    gap: 8px !important;
  }
  
  .filters-container > * {
    flex-shrink: 1 !important;
  }
}

/* PASO 7: Eliminar restricciones globales problemáticas */
@media screen and (max-width: 768px) {
  /* Resetear la regla problemática que limita todo a 100vw */
  * {
    max-width: none !important;
  }
  
  /* Excepto elementos que sí deben estar limitados */
  html,
  body,
  .container,
  .filters-container,
  .table-container,
  .table-responsive,
  .table-wrapper {
    max-width: 100vw !important;
  }
}

/* PASO 8: Landscape mode - mismo comportamiento */
@media screen and (max-width: 768px) and (orientation: landscape) {
  html,
  body {
    overflow-x: hidden !important;
    overflow-y: auto !important;
    height: auto !important;
    max-height: none !important;
    position: static !important;
  }
  
  body {
    padding-top: 60px !important;
    padding-bottom: 30px !important;
  }
  
  .container,
  .table-container,
  .table-responsive,
  .table-wrapper {
    max-height: none !important;
    overflow-y: visible !important;
    height: auto !important;
  }
  
  table {
    width: max-content !important;
    max-width: none !important;
  }
}

/* PASO 9: iOS Safari específico */
@supports (-webkit-touch-callout: none) {
  @media screen and (max-width: 768px) {
    body {
      min-height: 100vh;
      min-height: -webkit-fill-available;
    }
    
    .table-container {
      -webkit-overflow-scrolling: touch;
      padding-bottom: env(safe-area-inset-bottom, 0px);
    }
  }
}

/* PASO 10: Scrollbar visible en móvil */
@media screen and (max-width: 768px) {
  .table-container::-webkit-scrollbar {
    height: 8px !important;
    display: block !important;
  }
  
  .table-container::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.1) !important;
  }
  
  .table-container::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.3) !important;
    border-radius: 4px !important;
  }
}

/* ================================================================
   FIN DE MOBILE TABLES FIX
   ================================================================ */
