/**
 * Chat Widget Styles
 *
 * Intercom-style floating chat widget.
 * Modern gradient purple design with smooth animations.
 *
 * @version 1.0.0
 */

/* ========================================
   Ancla flotante arrastrable (solo panel del gestor)
   ========================================

   El widget del portal del cliente usa las MISMAS clases pero sin este
   envoltorio, así que estas reglas van acotadas a `.chat-widget-fab` y no
   alteran su comportamiento. El envoltorio se queda el `position: fixed` para
   poder colgar la «×» de ocultar en una esquina del botón (un <button> dentro
   de otro <button> es HTML inválido). */

.chat-widget-fab {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 9998;
    width: 60px;
    height: 60px;
}

/* Con envoltorio, el botón deja de posicionarse solo. Hay que anular
   bottom/right explícitamente: en `position: relative` no se ignoran, DESPLAZAN
   el elemento 24px arriba y a la izquierda. */
.chat-widget-fab .chat-widget-button {
    position: relative;
    top: auto;
    right: auto;
    bottom: auto;
    left: auto;
    width: 100%;
    height: 100%;
    touch-action: none; /* que arrastrar el botón no haga scroll de la página */
}

/* Botón de ocultar: esquina superior IZQUIERDA, porque la derecha la ocupa el
   contador de mensajes sin leer. */
.chat-widget-close {
    position: absolute;
    top: -6px;
    left: -6px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 1px solid #e2e8f0;
    background: #ffffff;
    color: #4a5568;
    font-size: 12px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.18);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s ease, transform 0.15s ease;
}

.chat-widget-fab:hover .chat-widget-close,
.chat-widget-fab:focus-within .chat-widget-close {
    opacity: 1;
    pointer-events: auto;
}

.chat-widget-close:hover {
    transform: scale(1.1);
    color: #e53e3e;
    border-color: #feb2b2;
}

/* En pantallas táctiles no hay hover: si se esconde tras él, no hay forma de
   pulsarlo. */
@media (hover: none) {
    .chat-widget-close {
        opacity: 1;
        pointer-events: auto;
    }
}

/* Pestañita que queda cuando el asistente está oculto: discreta, pero SIEMPRE
   visible y en el sitio donde estaba el botón. Es el camino de vuelta. */
.chat-widget-restore {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 9998;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: 1px solid #e2e8f0;
    background: #ffffff;
    color: #667eea;
    font-size: 14px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    opacity: 0.55;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
    transition: opacity 0.15s ease, transform 0.15s ease;
}

.chat-widget-restore:hover,
.chat-widget-restore:focus {
    opacity: 1;
    transform: scale(1.08);
}

/* Mientras se arrastra: sin transición (los 0.3s de `transition: all` harían
   que el botón fuera por detrás del cursor) y sin el scale del hover. */
.chat-widget-fab.is-dragging .chat-widget-button {
    transition: none;
    transform: none;
    cursor: grabbing;
}

body.chat-widget-dragging {
    user-select: none;
    -webkit-user-select: none;
    cursor: grabbing;
}

/* La cabecera de la ventana también arrastra el widget entero. */
.chat-widget-window .chat-widget-header {
    cursor: move;
    touch-action: none;
}

.chat-widget-window .chat-widget-header-btn {
    cursor: pointer;
}

/* Enlace «Mostrar asistente» del menú de perfil: vive siempre en el menú, pero
   solo se ve cuando el asistente está oculto. `!important` porque
   `.dropdown-item` y `.d-flex` de Bootstrap ganan por orden de carga. */
.chat-widget-restore-item {
    display: none !important;
}

body.chat-widget-hidden .chat-widget-restore-item {
    display: flex !important;
}

/* ========================================
   Chat Button (Floating)
   ======================================== */

.chat-widget-button {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 9998;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    color: white;
    font-size: 24px;
}

.chat-widget-button:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 16px rgba(102, 126, 234, 0.6);
}

.chat-widget-button:active {
    transform: scale(0.95);
}

.chat-widget-button.chat-open {
    background: linear-gradient(135deg, #f56565 0%, #e53e3e 100%);
}

/* Badge de mensajes no leídos */
.chat-widget-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    background: #e53e3e;
    color: white;
    border-radius: 12px;
    padding: 2px 6px;
    font-size: 11px;
    font-weight: 600;
    min-width: 20px;
    text-align: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

/* ========================================
   Chat Window
   ======================================== */

.chat-widget-window {
    position: fixed;
    bottom: 100px;
    right: 24px;
    z-index: 9999;
    width: 380px;
    max-height: 600px;
    background: white;
    border-radius: 16px;
    box-shadow: 0 12px 48px rgba(0, 0, 0, 0.15);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: slideUp 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

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

/* Header del chat */
.chat-widget-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 16px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.chat-widget-header-title {
    font-weight: 600;
    font-size: 16px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.chat-widget-header-actions {
    display: flex;
    gap: 8px;
}

.chat-widget-header-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s;
}

.chat-widget-header-btn:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* Indicador online */
.chat-status-indicator {
    width: 8px;
    height: 8px;
    background: #48bb78;
    border-radius: 50%;
    animation: pulse 2s infinite;
}

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

/* ========================================
   Messages Container
   ======================================== */

.chat-widget-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    background: #f7fafc;
    max-height: 400px;
}

/* Scrollbar personalizado */
.chat-widget-messages::-webkit-scrollbar {
    width: 6px;
}

.chat-widget-messages::-webkit-scrollbar-track {
    background: #f1f1f1;
}

.chat-widget-messages::-webkit-scrollbar-thumb {
    background: #cbd5e0;
    border-radius: 3px;
}

.chat-widget-messages::-webkit-scrollbar-thumb:hover {
    background: #a0aec0;
}

/* Mensaje individual */
.chat-message {
    margin-bottom: 12px;
    animation: fadeIn 0.3s ease-in;
}

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

.chat-message-user {
    display: flex;
    justify-content: flex-end;
}

.chat-message-assistant {
    display: flex;
    justify-content: flex-start;
}

/* Burbuja de mensaje */
.chat-message-bubble {
    max-width: 80%;
    padding: 10px 14px;
    border-radius: 12px;
    word-wrap: break-word;
    line-height: 1.4;
    font-size: 14px;
}

.chat-message-user .chat-message-bubble {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-bottom-right-radius: 4px;
}

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

.chat-message-bubble p {
    margin: 0 0 8px 0;
}

.chat-message-bubble p:last-child {
    margin-bottom: 0;
}

.chat-message-bubble ul {
    margin: 8px 0;
    padding-left: 20px;
}

.chat-message-bubble li {
    margin: 4px 0;
}

/* Timestamp */
.chat-message-timestamp {
    font-size: 11px;
    color: #a0aec0;
    margin-top: 4px;
    text-align: right;
}

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

.chat-message-assistant .chat-message-timestamp {
    text-align: left;
}

/* Typing indicator */
.chat-typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 10px 14px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
    width: fit-content;
}

.chat-typing-dot {
    width: 8px;
    height: 8px;
    background: #cbd5e0;
    border-radius: 50%;
    animation: typingBounce 1.4s infinite;
}

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

.chat-typing-dot:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typingBounce {
    0%, 60%, 100% {
        transform: translateY(0);
    }
    30% {
        transform: translateY(-8px);
    }
}

/* ========================================
   Suggested Actions
   ======================================== */

.chat-suggested-actions {
    padding: 0 16px 12px 16px;
    background: #f7fafc;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.chat-suggested-action-btn {
    padding: 8px 12px;
    background: white;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    font-size: 13px;
    color: #4a5568;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.chat-suggested-action-btn:hover {
    background: #667eea;
    color: white;
    border-color: #667eea;
    transform: translateY(-1px);
}

/* ========================================
   Rate Limit Warning
   ======================================== */

.chat-rate-limit-warning {
    padding: 12px 16px;
    background: #fed7d7;
    border-top: 1px solid #fc8181;
    color: #c53030;
    font-size: 13px;
    text-align: center;
}

/* ========================================
   Input Area
   ======================================== */

.chat-widget-input {
    padding: 16px;
    background: white;
    border-top: 1px solid #e2e8f0;
}

.chat-input-wrapper {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

.chat-input-field {
    flex: 1;
    padding: 10px 12px;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    font-size: 14px;
    resize: none;
    max-height: 100px;
    transition: border-color 0.2s;
}

.chat-input-field:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.chat-input-field:disabled {
    background: #f7fafc;
    cursor: not-allowed;
}

.chat-send-btn {
    padding: 10px 16px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    height: 44px;
}

.chat-send-btn:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.chat-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ========================================
   Mobile Responsive
   ======================================== */

@media (max-width: 480px) {
    .chat-widget-window {
        right: 0;
        bottom: 0;
        left: 0;
        width: 100%;
        max-height: 100vh;
        border-radius: 0;
        animation: slideUpMobile 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

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

    .chat-widget-button {
        bottom: 16px;
        right: 16px;
        width: 56px;
        height: 56px;
    }

    /* El envoltorio es quien manda cuando existe (panel del gestor). */
    .chat-widget-fab {
        bottom: 16px;
        right: 16px;
        width: 56px;
        height: 56px;
    }

    .chat-widget-restore {
        bottom: 16px;
        right: 16px;
    }

    .chat-widget-messages {
        max-height: calc(100vh - 180px);
    }
}

/* ========================================
   Dark Mode Support (Optional)
   ======================================== */

/* ========================================
   Markdown Rendering in Chat Bubbles
   ======================================== */

.chat-md h1, .chat-md h2, .chat-md h3, .chat-md h4 {
    font-size: 0.95em;
    font-weight: 700;
    margin: 0.6em 0 0.3em 0;
    line-height: 1.3;
    color: #1e293b;
}

.chat-md h1:first-child, .chat-md h2:first-child,
.chat-md h3:first-child, .chat-md h4:first-child {
    margin-top: 0;
}

.chat-md p {
    margin: 0 0 0.5em 0;
}

.chat-md p:last-child {
    margin-bottom: 0;
}

.chat-md strong {
    font-weight: 700;
    color: #1e293b;
}

.chat-md em {
    font-style: italic;
}

.chat-md ul, .chat-md ol {
    margin: 0.4em 0;
    padding-left: 1.4em;
}

.chat-md li {
    margin-bottom: 0.2em;
    line-height: 1.5;
}

.chat-md li::marker {
    color: #667eea;
}

.chat-md code {
    background: rgba(102, 126, 234, 0.08);
    color: #4338ca;
    padding: 0.15em 0.4em;
    border-radius: 4px;
    font-size: 0.88em;
    font-family: 'SF Mono', 'Fira Code', monospace;
}

.chat-md pre {
    background: #1e293b;
    color: #e2e8f0;
    border-radius: 8px;
    padding: 0.75em 1em;
    margin: 0.5em 0;
    overflow-x: auto;
    font-size: 0.85em;
    line-height: 1.5;
}

.chat-md pre code {
    background: none;
    color: inherit;
    padding: 0;
    border-radius: 0;
    font-size: inherit;
}

.chat-md a {
    color: #667eea;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.chat-md a:hover {
    color: #4338ca;
}

.chat-md blockquote {
    border-left: 3px solid #667eea;
    margin: 0.5em 0;
    padding: 0.3em 0.8em;
    color: #475569;
    background: rgba(102, 126, 234, 0.04);
    border-radius: 0 6px 6px 0;
}

.chat-md hr {
    border: none;
    border-top: 1px solid #e2e8f0;
    margin: 0.6em 0;
}

.chat-md table {
    border-collapse: collapse;
    width: 100%;
    margin: 0.5em 0;
    font-size: 0.9em;
}

.chat-md th, .chat-md td {
    border: 1px solid #e2e8f0;
    padding: 0.4em 0.6em;
    text-align: left;
}

.chat-md th {
    background: #f1f5f9;
    font-weight: 600;
}

/* Oscuro solo con el tema del panel (`data-theme`): ningún layout sigue la preferencia del sistema. */
:root[data-theme="dark"] .chat-widget-window {
    background: #1a202c;
}

:root[data-theme="dark"] .chat-widget-messages {
    background: #2d3748;
}

:root[data-theme="dark"] .chat-message-assistant .chat-message-bubble {
    background: #4a5568;
    color: #f7fafc;
}

:root[data-theme="dark"] .chat-input-field {
    background: #2d3748;
    border-color: #4a5568;
    color: #f7fafc;
}

:root[data-theme="dark"] .chat-suggested-action-btn {
    background: #2d3748;
    border-color: #4a5568;
    color: #f7fafc;
}
