/**
 * AWS Chat Agent - Frontend Styles
 * Diseño moderno y profesional con chat expandible
 * CORREGIDO: Ajuste del teclado móvil
 */

/* Variables CSS */
:root {
    --aws-chat-primary: #0066CC;
    --aws-chat-primary-hover: #0052A3;
    --aws-chat-primary-dark: #004080;
    --aws-chat-secondary: #F5F7FA;
    --aws-chat-text: #1A1A1A;
    --aws-chat-text-light: #666666;
    --aws-chat-border: #E1E8ED;
    --aws-chat-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    --aws-chat-shadow-lg: 0 10px 40px rgba(0, 0, 0, 0.15);
    --aws-chat-radius: 24px;
    --aws-chat-radius-sm: 12px;
    --aws-chat-transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    --aws-chat-z-index: 999999;
}

/* Contenedor principal */
#aws-chat-container {
    position: fixed;
    z-index: var(--aws-chat-z-index);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    font-size: 14px;
    line-height: 1.5;
}

/* Posicionamiento flotante */
#aws-chat-container.aws-chat-floating {
    bottom: 24px !important;
    right: 24px !important;
    left: auto !important;
    top: auto !important;
}

#aws-chat-container.aws-chat-floating.position-bottom-left {
    left: 24px !important;
    right: auto !important;
}

#aws-chat-container.aws-chat-floating.position-top-right {
    top: 24px !important;
    bottom: auto !important;
}

#aws-chat-container.aws-chat-floating.position-top-left {
    top: 24px !important;
    left: 24px !important;
    bottom: auto !important;
    right: auto !important;
}

/* Chat inline */
#aws-chat-container.aws-chat-inline {
    position: relative;
    bottom: auto;
    right: auto;
    max-width: 600px;
    margin: 20px auto;
}

/* ========================================
   CHAT COMPACTO (Estado inicial)
   ======================================== */

.aws-chat-compact {
    display: flex;
    align-items: center;
    gap: 12px;
    background: white;
    border-radius: var(--aws-chat-radius);
    box-shadow: var(--aws-chat-shadow);
    padding: 8px;
    transition: var(--aws-chat-transition);
    min-width: 368px;
    max-width: 460px;
}

.aws-chat-compact:hover {
    box-shadow: var(--aws-chat-shadow-lg);
    transform: translateY(-2px);
}

.aws-chat-compact-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--aws-chat-primary), var(--aws-chat-primary-dark));
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    overflow: hidden;
}

.aws-chat-compact-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
}

.aws-chat-compact-input {
    flex: 1;
    border: none;
    outline: none;
    font-size: 15px;
    color: var(--aws-chat-text);
    background: transparent;
    padding: 8px 0;
    font-family: inherit;
}

.aws-chat-compact-input::placeholder {
    color: var(--aws-chat-text-light);
}

.aws-chat-send-compact {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--aws-chat-primary);
    border: none;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--aws-chat-transition);
    flex-shrink: 0;
    padding: 20px;
}

.aws-chat-send-compact .aws-chat-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 18px;
    font-weight: bold;
    line-height: 1;
}

.aws-chat-send-compact:hover {
    background: var(--aws-chat-primary-hover);
    transform: scale(1.05);
}

.aws-chat-send-compact:active {
    transform: scale(0.95);
}

/* ========================================
   CHAT EXPANDIDO
   ======================================== */

.aws-chat-expanded {
    background: var(--aws-chat-secondary);
    border-radius: var(--aws-chat-radius);
    box-shadow: var(--aws-chat-shadow-lg);
    width: 50vw;
    min-width: 400px;
    max-width: 90vw;
    min-height: 400px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: slideUp 0.3s ease-out;
    position: fixed;
    top: 10vh;
    right: 24px;
    bottom: 24px;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* Header del chat */
.aws-chat-header {
    background: linear-gradient(135deg, var(--aws-chat-primary), var(--aws-chat-primary-dark));
    color: white;
    padding: 8px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.aws-chat-header-content {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1;
}

.aws-chat-header-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.aws-chat-header-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
}

.aws-chat-header-info {
    flex: 1;
    padding-left: 10px;
}

.aws-chat-title {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: white;
}

.aws-chat-status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    opacity: 0.9;
}

.aws-chat-status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #00FF88;
    animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.5;
    }
}

.aws-chat-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.aws-chat-btn-header {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.15);
    border: none;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--aws-chat-transition);
    padding: 20px;
}

.aws-chat-btn-header .aws-chat-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 20px;
    font-weight: bold;
    line-height: 1;
}

#aws-chat-reset-session .aws-chat-icon {
    font-size: 23px;
}

.aws-chat-btn-header:hover {
    background: rgba(255, 255, 255, 0.25);
    transform: scale(1.05);
}

.aws-chat-btn-header:active {
    transform: scale(0.95);
}

/* Área de mensajes */
.aws-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    background: var(--aws-chat-secondary) !important;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.aws-chat-messages::-webkit-scrollbar {
    width: 6px;
}

.aws-chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.aws-chat-messages::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 3px;
}

.aws-chat-messages::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.3);
}

/* Mensaje individual */
.aws-chat-message {
    display: flex;
    gap: 10px;
    animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.aws-chat-message-user {
    flex-direction: row-reverse;
}

.aws-chat-message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--aws-chat-primary);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 14px;
    overflow: hidden;
}

.aws-chat-message-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
}

.aws-chat-message-user .aws-chat-message-avatar {
    background: #6C757D;
}

.aws-chat-message-content {
    max-width: 70%;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.aws-chat-message-bubble {
    padding: 12px 16px;
    border-radius: 18px;
    line-height: 1.5;
    word-wrap: break-word;
}

.aws-chat-message-assistant .aws-chat-message-bubble {
    background: white;
    color: var(--aws-chat-text);
    border-bottom-left-radius: 6px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.aws-chat-message-user .aws-chat-message-bubble {
    background: var(--aws-chat-primary);
    color: white;
    border-bottom-right-radius: 6px;
}

.aws-chat-message-time {
    font-size: 11px;
    color: var(--aws-chat-text-light);
    padding: 0 8px;
}

.aws-chat-message-user .aws-chat-message-time {
    text-align: right;
}

/* Typing indicator */
.aws-chat-typing {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 12px 20px;
}

.aws-chat-typing-dot {
    width: 8px;
    height: 8px;
    background: var(--aws-chat-text-light);
    border-radius: 50%;
    animation: typingBounce 1.4s ease-in-out infinite both;
}

.aws-chat-typing-dot:nth-child(1) {
    animation-delay: -0.32s;
}

.aws-chat-typing-dot:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes typingBounce {
    0%, 80%, 100% {
        transform: scale(0.8);
        opacity: 0.5;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* ========================================
   FORMULARIO DE DATOS
   ======================================== */

.aws-chat-form {
    padding: 24px;
    background: white;
    border-top: 1px solid var(--aws-chat-border);
}

.aws-chat-form-header {
    margin-bottom: 16px;
}

.aws-chat-form-header h4 {
    margin: 0 0 4px 0;
    font-size: 16px;
    font-weight: 600;
    color: var(--aws-chat-text);
}

.aws-chat-form-header p {
    margin: 0;
    font-size: 13px;
    color: var(--aws-chat-text-light);
}

.aws-chat-form-fields {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.aws-chat-input {
    padding: 12px 16px;
    border: 1px solid var(--aws-chat-border);
    border-radius: var(--aws-chat-radius-sm);
    font-size: 14px;
    font-family: inherit;
    color: var(--aws-chat-text);
    transition: var(--aws-chat-transition);
    background: white;
}

.aws-chat-input:focus {
    outline: none;
    border-color: var(--aws-chat-primary);
    box-shadow: 0 0 0 3px rgba(0, 102, 204, 0.1);
}

.aws-chat-btn-primary {
    padding: 12px 24px;
    background: var(--aws-chat-primary);
    color: white;
    border: none;
    border-radius: var(--aws-chat-radius-sm);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: var(--aws-chat-transition);
}

.aws-chat-btn-primary:hover {
    background: var(--aws-chat-primary-hover);
    transform: translateY(-1px);
}

.aws-chat-btn-primary:active {
    transform: translateY(0);
}

/* ========================================
   INPUT AREA
   ======================================== */

.aws-chat-input-container {
    display: flex;
    align-items: flex-end;
    gap: 12px;
    padding: 12px 20px;
    background: white;
    border-top: 1px solid var(--aws-chat-border);
}

.aws-chat-textarea {
    flex: 1;
    padding: 12px 16px;
    border: 1px solid var(--aws-chat-border);
    border-radius: var(--aws-chat-radius-sm);
    font-size: 14px;
    font-family: inherit;
    color: var(--aws-chat-text);
    resize: none;
    min-height: 44px;
    max-height: 120px;
    transition: var(--aws-chat-transition);
    background: white;
}

.aws-chat-textarea:focus {
    outline: none;
    border-color: var(--aws-chat-primary);
    box-shadow: 0 0 0 3px rgba(0, 102, 204, 0.1);
}

.aws-chat-textarea::placeholder {
    color: var(--aws-chat-text-light);
}

.aws-chat-btn-send {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--aws-chat-primary);
    border: none;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--aws-chat-transition);
    flex-shrink: 0;
    padding: 20px;
}

.aws-chat-btn-send .aws-chat-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 18px;
    font-weight: bold;
    line-height: 1;
}

.aws-chat-btn-send:hover {
    background: var(--aws-chat-primary-hover);
    transform: scale(1.05);
}

.aws-chat-btn-send:active {
    transform: scale(0.95);
}

.aws-chat-btn-send:disabled {
    background: #CCCCCC;
    cursor: not-allowed;
    transform: none;
}

/* Footer */
.aws-chat-footer {
    padding: 7px 20px;
    text-align: center;
    font-size: 12px;
    color: var(--aws-chat-text-light);
    background: var(--aws-chat-secondary);
    border-top: 1px solid var(--aws-chat-border);
}

/* ========================================
   RESPONSIVE - CORRECCIÓN TECLADO MÓVIL
   ======================================== */

@media (max-width: 768px) {
    /* Prevenir overflow horizontal en toda la página */
    html, body {
        max-width: 100vw;
        overflow-x: hidden !important;
    }

    #aws-chat-container.aws-chat-floating {
        bottom: 0 !important;
        right: 0 !important;
        left: 0 !important;
        max-width: 100vw;
        overflow-x: hidden;
    }

    .aws-chat-compact {
        margin: 16px;
        min-width: auto;
        max-width: calc(100vw - 32px);
    }

    /* ====== CORRECCIÓN PRINCIPAL DEL TECLADO ====== */
    
    /* Estilos base para el chat expandido en móvil */
    .aws-chat-expanded {
        border-radius: 0 !important;
    }

    /* Chat expandido visible - usa 100dvh para viewport dinámico */
    .aws-chat-expanded.is-visible {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        /* CLAVE: Usar dvh (dynamic viewport height) que se ajusta al teclado */
        height: 100dvh !important;
        height: 100vh !important; /* Fallback para navegadores antiguos */
        max-height: 100dvh !important;
        max-height: 100vh !important;
        min-height: 0 !important;
        overflow: hidden !important;
        display: flex !important;
        flex-direction: column !important;
        margin: 0 !important;
        padding: 0 !important;
        border: none !important;
    }

    /* Cuando el teclado está abierto */
    .aws-chat-expanded.is-visible.keyboard-open {
        /* La altura se controlará via JavaScript con visualViewport */
        height: var(--viewport-height, 100dvh) !important;
        max-height: var(--viewport-height, 100dvh) !important;
    }

    /* Header fijo */
    .aws-chat-header {
        border-radius: 0;
        padding: 8px !important;
        flex: 0 0 auto !important;
        min-height: 56px;
    }

    /* Área de mensajes - flexible */
    .aws-chat-messages {
        overflow-y: auto !important;
        overflow-x: hidden !important;
        flex: 1 1 auto !important;
        -webkit-overflow-scrolling: touch;
        min-height: 0 !important; /* Importante para que flex funcione */
        padding: 10px;
    }

    /* Formulario de datos - scrolleable si es necesario */
    .aws-chat-form {
        padding: 16px !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        flex: 1 1 auto !important;
        max-height: none !important;
        -webkit-overflow-scrolling: touch;
        min-height: 0;
    }

    /* Contenedor de input - FIJO en la parte inferior */
    .aws-chat-input-container {
        padding: 8px !important;
        flex: 0 0 auto !important;
        background: white;
        border-top: 1px solid var(--aws-chat-border);
        min-height: auto;
        overflow-x: hidden;
        display: flex !important;
        gap: 8px;
        align-items: flex-end;
        /* Safe area para dispositivos con notch */
        padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px)) !important;
    }

    /* Footer - pequeño y fijo */
    .aws-chat-footer {
        padding: 6px 12px !important;
        padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px)) !important;
        font-size: 10px !important;
        flex: 0 0 auto !important;
        min-height: auto;
        overflow-x: hidden;
    }

    /* Textarea */
    .aws-chat-input,
    .aws-chat-textarea {
        font-size: 16px !important; /* Evita zoom en iOS */
        min-height: 44px !important;
        padding: 10px !important;
        max-height: 100px !important;
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
        border: 1px solid var(--aws-chat-border) !important;
        flex: 1 1 auto;
        min-width: 0;
    }

    /* Botón enviar */
    .aws-chat-btn-send {
        width: 44px !important;
        height: 44px !important;
        min-width: 44px !important;
        min-height: 44px !important;
        flex: 0 0 auto !important;
    }

    .aws-chat-btn-header {
        width: 36px !important;
        height: 36px !important;
        min-width: 36px !important;
        min-height: 36px !important;
    }

    /* Asegurar que los mensajes no causen overflow */
    .aws-chat-message {
        max-width: 100% !important;
        box-sizing: border-box;
        word-wrap: break-word;
        overflow-wrap: break-word;
        hyphens: auto;
    }

    .aws-chat-message-content {
        max-width: 75% !important;
        min-width: 0;
        overflow: hidden;
    }

    .aws-chat-message-bubble {
        max-width: 100% !important;
        word-wrap: break-word !important;
        overflow-wrap: break-word !important;
        hyphens: auto;
        overflow: hidden;
    }

    /* Ocultar scrollbar pero mantener funcionalidad */
    .aws-chat-messages::-webkit-scrollbar {
        width: 4px;
    }

    .aws-chat-messages::-webkit-scrollbar-thumb {
        background-color: rgba(0, 0, 0, 0.2);
        border-radius: 2px;
    }
}

@media (max-width: 480px) {
    .aws-chat-compact {
        min-width: auto;
    }

    .aws-chat-compact-input {
        font-size: 14px;
    }

    .aws-chat-message-content {
        max-width: 85%;
    }
}

/* ========================================
   INDICADORES DE ESTADO DE MENSAJES
   ======================================== */

.aws-chat-status-icon {
    display: inline-block;
    margin-left: 4px;
    font-size: 11px;
    opacity: 0.8;
}

.aws-chat-status-icon.sending {
    animation: pulse 1.5s ease-in-out infinite;
}

.aws-chat-status-icon.retrying {
    animation: spin 1s linear infinite;
}

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

.aws-chat-status-icon.error {
    color: #D32F2F;
    cursor: pointer;
}

.aws-chat-status-icon.sent {
    color: #4CAF50;
}

/* Contenedor de error con botón de reintento */
.aws-chat-retry-container {
    margin-top: 8px;
    padding: 8px 12px;
    background: #FFEBEE;
    border-left: 3px solid #D32F2F;
    border-radius: 6px;
    font-size: 13px;
}

.aws-chat-error-text {
    display: block;
    color: #C62828;
    margin-bottom: 8px;
    line-height: 1.4;
}

.aws-chat-retry-btn {
    background: #D32F2F;
    color: white;
    border: none;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: var(--aws-chat-transition);
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.aws-chat-retry-btn:hover {
    background: #C62828;
    transform: scale(1.05);
}

.aws-chat-retry-btn:active {
    transform: scale(0.95);
}

/* Mensaje con error */
.aws-chat-message[data-status="error"] .aws-chat-message-bubble {
    border: 1px solid #FFCDD2;
}

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

.aws-chat-hidden {
    display: none !important;
}

.aws-chat-fade-in {
    animation: fadeIn 0.3s ease-out;
}

.aws-chat-fade-out {
    animation: fadeOut 0.3s ease-out;
}

@keyframes fadeOut {
    from {
        opacity: 1;
    }
    to {
        opacity: 0;
    }
}

/* Asegurar colores correctos siempre */
.aws-chat-compact-input,
.aws-chat-textarea,
.aws-chat-input {
    color: var(--aws-chat-text) !important;
    background: white !important;
}

/* Personalización con color dinámico */
#aws-chat-container[data-primary-color] .aws-chat-compact-avatar,
#aws-chat-container[data-primary-color] .aws-chat-send-compact,
#aws-chat-container[data-primary-color] .aws-chat-header,
#aws-chat-container[data-primary-color] .aws-chat-message-avatar,
#aws-chat-container[data-primary-color] .aws-chat-message-user .aws-chat-message-bubble,
#aws-chat-container[data-primary-color] .aws-chat-btn-primary,
#aws-chat-container[data-primary-color] .aws-chat-btn-send {
    background: var(--aws-chat-primary);
}

#aws-chat-container[data-primary-color] .aws-chat-textarea:focus,
#aws-chat-container[data-primary-color] .aws-chat-input:focus {
    border-color: var(--aws-chat-primary);
}
