/* Reset y estilos base */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: #f0f2f5;
    color: #333;
    height: 100vh;
    overflow: hidden;
    /* Mejorar performance en móviles */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    /* Prevenir bounce en iOS */
    position: fixed;
    width: 100%;
}

/* Contenedor principal */
.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    position: relative;
    overflow: hidden;
}

/* Header */
.app-header {
    background: linear-gradient(135deg, #0081FF, #0066CC);
    color: white;
    padding: 1rem 2rem;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.header-content {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.header-content i {
    font-size: 2rem;
}

.header-content h1 {
    font-size: 1.5rem;
    font-weight: 600;
}

/* Contenido principal */
.main-content {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: 350px;
    background: white;
    border-right: 1px solid #e5e7eb;
    display: flex;
    flex-direction: column;
    box-shadow: 2px 0 10px rgba(0, 0, 0, 0.05);
}

.sidebar-header {
    padding: 1.5rem;
    border-bottom: 1px solid #e5e7eb;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #f8f9fa;
}

.sidebar-header h2 {
    font-size: 1.2rem;
    color: #374151;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.refresh-btn {
    background: #0081FF;
    color: white;
    border: none;
    border-radius: 50%;
    width: 35px;
    height: 35px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
}

.refresh-btn:hover {
    background: #0066CC;
    transform: rotate(180deg);
}

/* Búsqueda */
.search-container {
    position: relative;
    padding: 1rem;
    border-bottom: 1px solid #e5e7eb;
}

.search-input {
    width: 100%;
    padding: 0.75rem 1rem 0.75rem 2.5rem;
    border: 1px solid #d1d5db;
    border-radius: 25px;
    font-size: 0.9rem;
    background: #f9fafb;
    transition: all 0.3s ease;
}

.search-input:focus {
    outline: none;
    border-color: #0081FF;
    background: white;
    box-shadow: 0 0 0 3px rgba(0, 129, 255, 0.1);
}

.search-icon {
    position: absolute;
    left: 1.75rem;
    top: 50%;
    transform: translateY(-50%);
    color: #9ca3af;
    font-size: 0.9rem;
}

/* Lista de contactos */
.contacts-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem 0;
}

.contact-item {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid #f3f4f6;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    gap: 1rem;
}

.contact-item:hover {
    background: #f8f9fa;
}

.contact-item.active {
    background: #e6f3ff;
    border-right: 3px solid #0081FF;
}

.contact-item.active .contact-time {
    color: #0066CC;
    font-weight: 600;
}

.contact-avatar {
    width: 45px;
    height: 45px;
    background: #0081FF;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 1.2rem;
    flex-shrink: 0;
}

.contact-info {
    flex: 1;
    min-width: 0;
}

.contact-main {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.25rem;
}

.contact-info h4 {
    font-size: 1rem;
    color: #374151;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    margin-right: 0.5rem;
}

.contact-time {
    font-size: 0.75rem;
    color: #0081FF;
    font-weight: 500;
    white-space: nowrap;
}

.contact-number {
    font-size: 0.85rem;
    color: #6b7280;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Área de chat */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: white;
    height: 100%;
    overflow: hidden;
}

/* Pantalla de bienvenida */
.welcome-screen {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #f8f9fa, #e9ecef);
}

.welcome-content {
    text-align: center;
    padding: 2rem;
}

.welcome-content i {
    font-size: 4rem;
    color: #0081FF;
    margin-bottom: 1rem;
}

.welcome-content h2 {
    color: #374151;
    margin-bottom: 1rem;
    font-size: 1.5rem;
}

.welcome-content p {
    color: #6b7280;
    font-size: 1rem;
}

/* Contenedor de chat */
.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

/* Header del chat */
.chat-header {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid #e5e7eb;
    background: #f8f9fa;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.contact-info {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.contact-details h3 {
    font-size: 1.1rem;
    color: #374151;
    margin-bottom: 0.25rem;
}

.contact-details span {
    font-size: 0.9rem;
    color: #6b7280;
}

.chat-actions {
    display: flex;
    gap: 0.5rem;
}

.action-btn {
    background: #6b7280;
    color: white;
    border: none;
    border-radius: 50%;
    width: 35px;
    height: 35px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
}

.action-btn:hover {
    background: #374151;
}

/* Contenedor de mensajes */
.messages-container {
    flex: 1;
    padding: 1rem;
    overflow-y: auto;
    overflow-x: hidden;
    background: #f8f9fa;
    height: 0; /* Esto fuerza que tome el espacio disponible */
    min-height: 0; /* Permite que el contenedor se contraiga */
}

/* Separador de fecha */
.date-separator {
    display: flex;
    justify-content: center;
    margin: 1rem 0;
}

.date-label {
    background: rgba(0, 0, 0, 0.05);
    color: #6b7280;
    padding: 0.5rem 1rem;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 500;
    text-transform: capitalize;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* Mensajes */
.message {
    margin-bottom: 1rem;
    display: flex;
    animation: slideIn 0.3s ease;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message.sent {
    justify-content: flex-end;
}

.message.received {
    justify-content: flex-start;
}

.message-bubble {
    max-width: 70%;
    padding: 0.75rem 1rem;
    border-radius: 18px;
    word-wrap: break-word;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    position: relative;
}

/* Mensajes enviados (IA/Bot) */
.message.sent .message-bubble {
    background: #dcf8c6;
    color: #333;
    border-bottom-right-radius: 5px;
}

/* Mensajes recibidos (Cliente) */
.message.received .message-bubble {
    background: white;
    color: #333;
    border-bottom-left-radius: 5px;
    border-left: 3px solid #0081FF;
}

/* Indicador visual para mensajes de IA */
.message.sent .message-bubble::before {
    content: "🤖";
    position: absolute;
    top: -5px;
    right: -5px;
    background: #0081FF;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

/* Indicador visual para mensajes de cliente */
.message.received .message-bubble::before {
    content: "👤";
    position: absolute;
    top: -5px;
    left: -5px;
    background: #007bff;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.message-text {
    font-size: 0.9rem;
    line-height: 1.4;
}

.message-time {
    font-size: 0.75rem;
    color: #6b7280;
    margin-top: 0.25rem;
    text-align: right;
}

/* Estados de carga */
.loading {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 2rem;
    color: #6b7280;
}

.loading i {
    font-size: 1.2rem;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-content {
    background: white;
    margin: 15% auto;
    padding: 0;
    border-radius: 10px;
    width: 90%;
    max-width: 500px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
    animation: slideInModal 0.3s ease;
}

@keyframes slideInModal {
    from {
        opacity: 0;
        transform: scale(0.7) translateY(-50px);
    }
    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

.modal-header {
    padding: 1.5rem;
    border-bottom: 1px solid #e5e7eb;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h3 {
    color: #dc2626;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.close-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: #6b7280;
    padding: 0;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: all 0.3s ease;
}

.close-btn:hover {
    background: #f3f4f6;
    color: #374151;
}

.modal-body {
    padding: 1.5rem;
}

.modal-footer {
    padding: 1.5rem;
    border-top: 1px solid #e5e7eb;
    text-align: right;
}

.btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: all 0.3s ease;
}

.btn-primary {
    background: #0081FF;
    color: white;
}

.btn-primary:hover {
    background: #0066CC;
}

/* Scrollbar personalizado */
.contacts-list::-webkit-scrollbar,
.messages-container::-webkit-scrollbar {
    width: 8px;
}

.contacts-list::-webkit-scrollbar-track,
.messages-container::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 4px;
}

.contacts-list::-webkit-scrollbar-thumb,
.messages-container::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 4px;
    border: 1px solid #f1f1f1;
}

.contacts-list::-webkit-scrollbar-thumb:hover,
.messages-container::-webkit-scrollbar-thumb:hover {
    background: #a8a8a8;
}

/* Mobile-first responsive design */
@media (max-width: 768px) {
    /* Ajustar el layout para móviles */
    body {
        overflow: auto;
        height: 100vh;
        -webkit-overflow-scrolling: touch;
    }
    
    .app-container {
        height: 100vh;
        overflow: hidden;
    }

    /* Header móvil optimizado */
    .app-header {
        padding: 0.75rem 1rem;
        position: relative;
        z-index: 200;
    }

    .header-content {
        gap: 0.75rem;
        justify-content: space-between;
    }

    .header-content h1 {
        font-size: 1.1rem;
        font-weight: 600;
    }

    .header-content img {
        height: 1.5rem !important;
    }

    /* Agregar botón de menú hamburguesa */
    .menu-toggle {
        display: block;
        background: none;
        border: none;
        color: white;
        font-size: 1.5rem;
        cursor: pointer;
        padding: 0.25rem;
        border-radius: 4px;
        transition: background-color 0.3s ease;
    }

    .menu-toggle:hover {
        background-color: rgba(255, 255, 255, 0.1);
    }

    /* Contenido principal móvil */
    .main-content {
        position: relative;
        height: calc(100vh - 60px);
        overflow: hidden;
    }

    /* Sidebar móvil */
    .sidebar {
        position: fixed;
        top: 60px;
        left: 0;
        width: 100%;
        height: calc(100vh - 60px);
        background: white;
        z-index: 150;
        transform: translateX(-100%);
        transition: transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    /* Overlay para cerrar sidebar */
    .sidebar-overlay {
        display: none;
        position: fixed;
        top: 60px;
        left: 0;
        width: 100%;
        height: calc(100vh - 60px);
        background: rgba(0, 0, 0, 0.5);
        z-index: 140;
    }

    .sidebar-overlay.active {
        display: block;
    }

    /* Header del sidebar móvil */
    .sidebar-header {
        padding: 1rem;
        background: #f8f9fa;
        border-bottom: 1px solid #e5e7eb;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .sidebar-header h2 {
        font-size: 1.1rem;
    }

    /* Búsqueda móvil */
    .search-container {
        padding: 1rem;
    }

    .search-input {
        padding: 0.75rem 1rem 0.75rem 2.5rem;
        font-size: 1rem; /* Evita zoom en iOS */
        border-radius: 8px;
    }

    /* Lista de contactos móvil */
    .contacts-list {
        padding: 0;
        height: calc(100vh - 200px);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .contact-item {
        padding: 1rem;
        border-bottom: 1px solid #f3f4f6;
        display: flex;
        align-items: center;
        gap: 1rem;
        min-height: 70px;
        touch-action: manipulation;
    }

    .contact-item:active {
        background: #e6f3ff;
        transform: scale(0.98);
        transition: all 0.1s ease;
    }

    .contact-avatar {
        width: 50px;
        height: 50px;
        font-size: 1.3rem;
        flex-shrink: 0;
    }

    .contact-info h4 {
        font-size: 1rem;
        font-weight: 600;
    }

    .contact-number {
        font-size: 0.9rem;
    }

    .contact-time {
        font-size: 0.8rem;
    }

    /* Área de chat móvil */
    .chat-area {
        width: 100%;
        height: calc(100vh - 60px);
        overflow: hidden;
    }

    /* Header del chat móvil */
    .chat-header {
        padding: 1rem;
        background: white;
        border-bottom: 1px solid #e5e7eb;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
        position: relative;
        z-index: 10;
    }

    .contact-info {
        flex: 1;
        min-width: 0;
    }

    .contact-details h3 {
        font-size: 1rem;
        font-weight: 600;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .contact-details span {
        font-size: 0.85rem;
    }

    .chat-actions {
        gap: 0.5rem;
    }

    .action-btn {
        width: 40px;
        height: 40px;
        font-size: 1rem;
        touch-action: manipulation;
    }

    .action-btn:active {
        transform: scale(0.9);
        transition: transform 0.1s ease;
    }

    /* Contenedor de mensajes móvil */
    .messages-container {
        height: calc(100vh - 140px);
        padding: 1rem 0.75rem;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        background: #f8f9fa;
    }

    /* Mensajes móviles */
    .message {
        margin-bottom: 1.5rem;
    }

    .message-bubble {
        max-width: 85%;
        padding: 1rem;
        border-radius: 16px;
        font-size: 1rem;
        line-height: 1.5;
        word-wrap: break-word;
        overflow-wrap: break-word;
    }

    /* Ajustar posición de indicadores en móvil */
    .message.sent .message-bubble::before,
    .message.received .message-bubble::before {
        width: 24px;
        height: 24px;
        font-size: 12px;
    }

    .message-time {
        font-size: 0.8rem;
        margin-top: 0.5rem;
    }

    /* Pantalla de bienvenida móvil */
    .welcome-screen {
        padding: 2rem 1rem;
        text-align: center;
    }

    .welcome-content {
        padding: 1rem;
    }

    .welcome-content h2 {
        font-size: 1.3rem;
        margin-bottom: 1rem;
    }

    .welcome-content p {
        font-size: 1rem;
    }

    /* Modal móvil */
    .modal-content {
        margin: 10% auto;
        width: 95%;
        max-width: none;
        border-radius: 12px;
    }

    .modal-header,
    .modal-body,
    .modal-footer {
        padding: 1rem;
    }

    /* Estados de carga móvil */
    .loading {
        padding: 3rem 1rem;
        flex-direction: column;
        gap: 1rem;
    }

    .loading i {
        font-size: 2rem;
    }

    /* Mejorar área táctil de botones */
    .refresh-btn {
        min-width: 44px;
        min-height: 44px;
        touch-action: manipulation;
    }

    .refresh-btn:active {
        transform: scale(0.9);
    }

    /* Separador de fecha móvil */
    .date-separator {
        margin: 1.5rem 0;
    }

    .date-label {
        padding: 0.75rem 1rem;
        font-size: 0.85rem;
    }

    /* Scrollbar para móvil (webkit) */
    .contacts-list::-webkit-scrollbar,
    .messages-container::-webkit-scrollbar {
        width: 4px;
    }
}

/* Tablets y pantallas medianas */
@media (min-width: 769px) and (max-width: 1024px) {
    .sidebar {
        width: 300px;
    }

    .contact-item {
        padding: 0.875rem 1.25rem;
    }

    .message-bubble {
        max-width: 75%;
    }

    .chat-header {
        padding: 1.25rem 1.5rem;
    }

    .messages-container {
        padding: 1rem 1.25rem;
    }
}

/* Pantallas grandes */
@media (min-width: 1025px) {
    .menu-toggle {
        display: none;
    }

    .sidebar {
        position: relative;
        transform: none;
        width: 350px;
    }

    .sidebar-overlay {
        display: none !important;
    }
}

/* Estados adicionales móviles */
@media (max-width: 768px) {
    .empty-state,
    .error-state {
        padding: 3rem 1rem;
    }

    .empty-state i,
    .error-state i {
        font-size: 4rem;
        margin-bottom: 1.5rem;
    }

    /* Optimizaciones para iOS Safari */
    .app-container {
        -webkit-overflow-scrolling: touch;
    }

    /* Prevenir zoom en inputs en iOS */
    input[type="text"],
    input[type="search"] {
        font-size: 16px;
    }

    /* Mejorar rendimiento en móviles */
    .message,
    .contact-item {
        will-change: transform;
    }

    /* Animaciones optimizadas para móvil */
    .contact-item:hover .contact-avatar {
        transform: none;
    }

    .message-bubble:hover {
        transform: none;
    }

    /* Mejorar experiencia táctil */
    .contact-item,
    .action-btn,
    .refresh-btn,
    .menu-toggle {
        -webkit-tap-highlight-color: transparent;
        -webkit-touch-callout: none;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
    }

    /* Asegurar que los elementos táctiles tengan tamaño mínimo */
    .contact-item,
    .action-btn,
    .refresh-btn,
    .menu-toggle {
        min-height: 44px;
        min-width: 44px;
    }

    /* Mejorar legibilidad en móvil */
    .message-text {
        line-height: 1.6;
        word-break: break-word;
        hyphens: auto;
    }

    /* Optimizaciones adicionales para iOS */
    body {
        -webkit-overflow-scrolling: touch;
        overflow-scrolling: touch;
    }

    /* Prevenir zoom accidental en inputs */
    input, textarea, select {
        font-size: 16px !important;
    }

    /* Mejorar contraste en pantallas pequeñas */
    .contact-time {
        font-weight: 600;
        color: #059669;
    }

    .message-time {
        font-weight: 500;
    }

    /* Asegurar que los modales sean accesibles en móvil */
    .modal {
        padding: 1rem;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .modal-content {
        max-height: 90vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Mejorar el área de carga */
    .loading {
        min-height: 200px;
    }

    /* Asegurar que el contenido no se oculte detrás del notch en iOS */
    @supports (padding-top: env(safe-area-inset-top)) {
        .app-header {
            padding-top: calc(0.75rem + env(safe-area-inset-top));
        }

        .sidebar {
            top: calc(60px + env(safe-area-inset-top));
            height: calc(100vh - 60px - env(safe-area-inset-top));
        }

        .chat-area {
            height: calc(100vh - 60px - env(safe-area-inset-top));
        }
    }
}

/* Estados adicionales */
.empty-state {
    text-align: center;
    padding: 2rem;
    color: #6b7280;
}

.empty-state i {
    font-size: 3rem;
    margin-bottom: 1rem;
    opacity: 0.5;
}

.error-state {
    text-align: center;
    padding: 2rem;
    color: #dc2626;
}

.error-state i {
    font-size: 3rem;
    margin-bottom: 1rem;
}

/* Efectos de hover mejorados */
.contact-item:hover .contact-avatar {
    transform: scale(1.05);
}

.message-bubble:hover {
    transform: scale(1.02);
    transition: transform 0.2s ease;
}