/* ============================================
   OPTIMIZACIONES MÓVIL Y PWA
   ============================================ */

/* Safe area para notch de iPhone */
:root {
    --safe-area-inset-top: env(safe-area-inset-top, 0px);
    --safe-area-inset-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-inset-left: env(safe-area-inset-left, 0px);
    --safe-area-inset-right: env(safe-area-inset-right, 0px);
}

/* Prevenir zoom en inputs en iOS */
@media screen and (max-width: 768px) {
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="tel"],
    input[type="url"],
    select,
    textarea {
        font-size: 16px !important;
    }
}

/* Mejorar tap targets para móvil */
@media (max-width: 768px) {
    button,
    a,
    .clickable {
        min-height: 44px;
        min-width: 44px;
    }
    
    /* Espacio arriba para los botones flotantes */
    #main-content {
        padding-top: 80px !important;
    }
    
    /* Primera card con margen adicional */
    #main-content > .card:first-child {
        margin-top: 0 !important;
    }
    
    /* Header más compacto en móvil */
    header {
        padding: 8px 12px;
        padding-top: calc(8px + var(--safe-area-inset-top));
        min-height: auto;
        position: sticky;
        top: 0;
        z-index: 100;
    }
    
    header h1 {
        font-size: 1.1rem;
        margin: 0;
    }
    
    header nav {
        gap: 6px;
    }
    
    header nav button {
        padding: 6px 10px;
        font-size: 0.8rem;
        min-height: 36px;
    }
    
    /* Sidebar más compacto */
    aside {
        padding: 16px;
        padding-bottom: calc(16px + var(--safe-area-inset-bottom));
    }
    
    aside h2 {
        font-size: 1.1rem;
    }
    
    /* Main content con safe area */
    main {
        padding: 16px;
        padding-bottom: calc(16px + var(--safe-area-inset-bottom));
    }
    
    /* Perfil más compacto */
    .profile-card {
        padding: 12px;
    }
    
    .profile-card h3 {
        font-size: 0.95rem;
    }
    
    .profile-card p {
        font-size: 0.8rem;
    }
    
    /* Grid de perfiles optimizado */
    .profile-grid {
        gap: 12px;
        grid-template-columns: repeat(2, 1fr) !important;
    }
    
    /* Modales fullscreen en móvil */
    .modal-content {
        width: 100%;
        height: 100%;
        max-width: 100%;
        max-height: 100%;
        border-radius: 0;
        margin: 0;
    }
    
    /* Chat optimizado para móvil */
    .chat-container {
        height: 100vh;
        height: 100dvh; /* Dynamic viewport height */
        border-radius: 0;
        max-width: 100vw;
        overflow-x: hidden;
    }
    
    .chat-messages {
        padding-bottom: calc(100px + var(--safe-area-inset-bottom)) !important;
        max-width: 100%;
        overflow-x: hidden;
        padding-left: 12px;
        padding-right: 12px;
    }
    
    .chat-input-container {
        padding-bottom: calc(12px + var(--safe-area-inset-bottom));
        max-width: 100%;
    }
    
    /* Mensajes no deben salirse del contenedor */
    .message {
        max-width: 85%;
        word-wrap: break-word;
        overflow-wrap: break-word;
    }
    
    .message-content {
        max-width: 100%;
        word-wrap: break-word;
        overflow-wrap: break-word;
    }
    
    /* Mensajero fullscreen */
    #mensajero-modal-overlay {
        padding: 0;
        overflow-x: hidden;
    }
    
    #mensajero-modal {
        width: 100%;
        height: 100vh;
        height: 100dvh;
        max-width: 100%;
        max-height: 100%;
        border-radius: 0;
        overflow-x: hidden;
    }
    
    /* Chat overlay fullscreen */
    #chat-overlay-modal {
        overflow-x: hidden;
    }
    
    #chat-overlay-modal .chat-modal-content {
        width: 100%;
        height: 100vh;
        height: 100dvh;
        max-width: 100%;
        border-radius: 0;
        overflow-x: hidden;
    }
    
    #chat-overlay-modal .chat-messages {
        padding-bottom: calc(100px + var(--safe-area-inset-bottom)) !important;
    }
    
    /* Galería optimizada */
    #gallery-modal {
        padding-top: var(--safe-area-inset-top);
        padding-bottom: var(--safe-area-inset-bottom);
    }
    
    /* Botones de galería más grandes */
    #gallery-modal button {
        min-width: 50px;
        min-height: 50px;
    }
    
    /* Auth page centrada */
    .auth-container {
        padding: 20px;
        padding-top: calc(20px + var(--safe-area-inset-top));
        padding-bottom: calc(20px + var(--safe-area-inset-bottom));
    }
    
    .auth-box {
        padding: 24px 20px;
    }
    
    /* Formularios más espaciados */
    .form-group {
        margin-bottom: 16px;
    }
    
    /* Tabs más grandes */
    .tabs button {
        padding: 12px 20px;
        font-size: 0.95rem;
    }
    
    /* Notificaciones con safe area y z-index alto */
    .toast-notification {
        top: calc(60px + var(--safe-area-inset-top));
        left: 10px;
        right: 10px;
        width: auto;
        z-index: 10001 !important;
    }
    
    .notification-popup {
        top: calc(60px + var(--safe-area-inset-top));
        right: 10px;
        z-index: 10001 !important;
    }
}

/* Móviles pequeños */
@media (max-width: 480px) {
    .profile-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 8px;
    }
    
    .profile-card {
        padding: 8px;
    }
    
    .profile-card img {
        height: 150px;
    }
    
    header {
        padding: 6px 10px;
        padding-top: calc(6px + var(--safe-area-inset-top));
    }
    
    header h1 {
        font-size: 1rem;
    }
    
    header nav button {
        padding: 5px 8px;
        font-size: 0.75rem;
        min-height: 32px;
    }
    
    /* Ocultar texto en botones muy pequeños, solo iconos */
    header nav button span {
        display: none;
    }
}

/* Landscape en móvil */
@media (max-width: 768px) and (orientation: landscape) {
    .auth-container {
        padding: 10px;
    }
    
    .auth-box {
        padding: 20px;
        max-width: 600px;
    }
    
    .chat-container {
        height: 100vh;
    }
}

/* Prevenir scroll bounce en iOS y horizontal */
body {
    overscroll-behavior-y: none;
    -webkit-overflow-scrolling: touch;
    overflow-x: hidden;
    max-width: 100vw;
}

/* Prevenir scroll horizontal en todos los contenedores */
@media (max-width: 768px) {
    * {
        max-width: 100%;
    }
    
    html, body {
        overflow-x: hidden;
        max-width: 100vw;
    }
    
    #app {
        overflow-x: hidden;
        max-width: 100vw;
    }
    
    main {
        overflow-x: hidden;
        max-width: 100vw;
    }
}

/* Mejorar scroll en móvil */
.scrollable {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

/* Ocultar scrollbar en móvil pero mantener funcionalidad */
@media (max-width: 768px) {
    ::-webkit-scrollbar {
        width: 4px;
        height: 4px;
    }
    
    ::-webkit-scrollbar-track {
        background: transparent;
    }
    
    ::-webkit-scrollbar-thumb {
        background: rgba(255, 255, 255, 0.2);
        border-radius: 2px;
    }
}

/* Pull to refresh personalizado */
@media (max-width: 768px) {
    body {
        overscroll-behavior-y: contain;
    }
}

/* Mejorar rendimiento en móvil */
@media (max-width: 768px) {
    * {
        -webkit-tap-highlight-color: transparent;
    }
    
    img {
        will-change: transform;
    }
    
    .profile-card {
        will-change: transform;
    }
}

/* Instalación PWA prompt */
.install-prompt {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 16px;
    padding-bottom: calc(16px + var(--safe-area-inset-bottom));
    display: none;
    align-items: center;
    justify-content: space-between;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.3);
    z-index: 10000;
    animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
    from {
        transform: translateY(100%);
    }
    to {
        transform: translateY(0);
    }
}

.install-prompt.show {
    display: flex;
}

.install-prompt-content {
    flex: 1;
}

.install-prompt h3 {
    margin: 0 0 4px 0;
    font-size: 1rem;
}

.install-prompt p {
    margin: 0;
    font-size: 0.85rem;
    opacity: 0.9;
}

.install-prompt-buttons {
    display: flex;
    gap: 8px;
}

.install-prompt button {
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;
    font-size: 0.9rem;
}

.install-prompt .btn-install {
    background: white;
    color: #667eea;
}

.install-prompt .btn-dismiss {
    background: rgba(255, 255, 255, 0.2);
    color: white;
}

/* Splash screen para PWA */
.pwa-splash {
    position: fixed;
    inset: 0;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    z-index: 99999;
    opacity: 1;
    transition: opacity 0.3s ease-out;
}

.pwa-splash.hide {
    opacity: 0;
    pointer-events: none;
}

.pwa-splash h1 {
    color: white;
    font-size: 2.5rem;
    margin: 0;
    animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% {
        opacity: 1;
        transform: scale(1);
    }
    50% {
        opacity: 0.8;
        transform: scale(1.05);
    }
}


/* ============================================
   PERFIL OPTIMIZADO PARA MÓVIL
   ============================================ */

@media (max-width: 768px) {
    /* Layout del perfil en móvil: columna única */
    .profile-layout {
        display: flex !important;
        flex-direction: column !important;
        gap: 0 !important;
    }
    
    /* Sidebar (información personal) */
    .profile-sidebar {
        width: 100% !important;
        border-right: none !important;
        border-bottom: 1px solid var(--border) !important;
        padding: 16px !important;
    }
    
    /* Content (descripción y fotos) */
    .profile-content {
        width: 100% !important;
        padding: 16px !important;
    }
    
    /* Asegurar que la descripción ocupe todo el ancho */
    .profile-content > div {
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
    }
    
    .profile-content > div > div {
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
    }
    
    /* Grid de fotos más pequeño en móvil */
    #media-grid {
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 8px !important;
    }
}

@media (max-width: 480px) {
    /* Foto un poco más pequeña en pantallas muy pequeñas */
    .profile-layout > div:first-child > div:first-child {
        width: 220px !important;
        height: 220px !important;
    }
    
    /* Botones más compactos */
    .profile-layout > div:first-child > button,
    .profile-layout > div:first-child > div > button {
        padding: 12px 20px !important;
        font-size: 0.95rem !important;
    }
    
    /* Grid de fotos 2 columnas en móviles pequeños */
    #media-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }
    
    /* Información personal en 1 columna en móviles pequeños */
    .profile-layout > div:last-child > div:first-child > div {
        grid-template-columns: 1fr !important;
    }
}


/* Forzar alineación izquierda en perfil móvil */
@media (max-width: 768px) {
    .profile-container {
        border-radius: 0 !important;
    }
    
    .profile-header {
        padding: 12px !important;
    }
    
    .profile-header-content {
        flex-direction: column !important;
        align-items: flex-start !important;
        justify-content: flex-start !important;
    }
    
    .profile-header-info {
        width: 100% !important;
        min-width: 100% !important;
    }
    
    .profile-sidebar,
    .profile-sidebar *,
    .profile-info-section,
    .profile-info-section *,
    .profile-info-title,
    .profile-info-text,
    .profile-content,
    .profile-content * {
        text-align: left !important;
        margin-left: 0 !important;
        margin-right: 0 !important;
    }
    
    .profile-sidebar {
        display: block !important;
        align-items: flex-start !important;
    }
    
    .profile-info-section {
        width: 100% !important;
        max-width: 100% !important;
    }
}

/* Layout de 2 columnas responsive */
@media (max-width: 768px) {
    /* En móvil, apilar las columnas verticalmente */
    div[style*="grid-template-columns: 1fr 1fr"] {
        grid-template-columns: 1fr !important;
    }
}
