/* Chat Widget - Premium Glassmorphism Design */
:root {
    /* Local scoped variables for better control, falling back to app vars */
    --chat-primary: var(--primary, #3b82f6);
    --chat-primary-dark: var(--primary-hover, #2563eb);
    --chat-bg: #0f172a;
    /* Slate 900 */
    --chat-surface: #1e293b;
    /* Slate 800 */
    --chat-surface-hover: #334155;
    /* Slate 700 */
    --chat-text: #f8fafc;
    /* Slate 50 */
    --chat-text-muted: #94a3b8;
    /* Slate 400 */
    --chat-border: #334155;
    /* Slate 700 */
    --chat-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.5), 0 4px 6px -2px rgba(0, 0, 0, 0.3);
    --chat-radius: 16px;
    --chat-font: 'Inter', system-ui, sans-serif;
}

/* Base Container */
.chatbot-container {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 420px;
    /* Slightly wider for better history panel */
    height: 650px;
    max-height: 85vh;
    background-color: var(--chat-bg);
    border: 1px solid var(--chat-border);
    border-radius: var(--chat-radius);
    box-shadow: var(--chat-shadow);
    display: flex;
    flex-direction: row;
    /* Sidebar + Main */
    z-index: 10000;
    font-family: var(--chat-font);
    overflow: hidden;
    transform: translateY(20px) scale(0.95);
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    backdrop-filter: blur(12px);
    /* Glass effect if BG has transparency */
}

.chatbot-container.open {
    transform: translateY(0) scale(1);
    opacity: 1;
    visibility: visible;
}

/* --- Sidebar (History) --- */
.chatbot-history-panel {
    width: 260px;
    background-color: rgba(15, 23, 42, 0.95);
    /* Nearly opaque slate */
    border-right: 1px solid var(--chat-border);
    display: flex;
    flex-direction: column;
    position: absolute;
    /* Mobile-first approach for slide-in */
    top: 0;
    bottom: 0;
    left: 0;
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 20;
}

/* Show sidebar if 'history-open' class is present on container */
.chatbot-container.history-open .chatbot-history-panel {
    transform: translateX(0);
    box-shadow: 4px 0 15px rgba(0, 0, 0, 0.3);
}

.history-header {
    padding: 1rem;
    border-bottom: 1px solid var(--chat-border);
}

.new-chat-btn {
    width: 100%;
    padding: 0.75rem;
    background: linear-gradient(135deg, var(--chat-primary), var(--chat-primary-dark));
    color: white;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
}

.new-chat-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.conversation-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.conversation-item {
    padding: 0.75rem 1rem;
    border-radius: 8px;
    color: var(--chat-text-muted);
    font-size: 0.9rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    transition: all 0.2s;
    position: relative;
    border: 1px solid transparent;
}

.conversation-item:hover {
    background-color: var(--chat-surface);
    color: var(--chat-text);
}

.conversation-item.active {
    background-color: rgba(59, 130, 246, 0.1);
    color: var(--chat-primary);
    border-color: rgba(59, 130, 246, 0.2);
}

.conversation-title {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
}

.conversation-actions {
    display: flex;
    gap: 4px;
    opacity: 0;
    /* Hidden by default */
    transition: opacity 0.2s;
}

.conversation-item:hover .conversation-actions {
    opacity: 1;
}

.conversation-actions button {
    background: transparent;
    border: none;
    color: var(--chat-text-muted);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    font-size: 0.8rem;
}

.conversation-actions button:hover {
    background-color: rgba(255, 255, 255, 0.1);
    color: white;
}

.conversation-actions .delete-conversation-btn:hover {
    color: #ef4444;
    /* Red for delete */
}

/* --- Main Panel --- */
.chatbot-main-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background-color: var(--chat-bg);
    position: relative;
    /* Ensure it handles the full width */
}

/* Header */
.chatbot-header {
    padding: 1rem 1.25rem;
    background-color: rgba(30, 41, 59, 0.8);
    /* Semi-transparent */
    border-bottom: 1px solid var(--chat-border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    backdrop-filter: blur(8px);
}

.chatbot-title {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.chatbot-icon-svg {
    font-size: 1.25rem;
    color: var(--chat-primary);
    background: rgba(59, 130, 246, 0.1);
    padding: 8px;
    border-radius: 8px;
}

.chatbot-title h3 {
    margin: 0;
    font-size: 1rem;
    font-weight: 600;
    color: var(--chat-text);
}

.chatbot-status {
    font-size: 0.7rem;
    background-color: rgba(16, 185, 129, 0.15);
    color: #34d399;
    padding: 2px 8px;
    border-radius: 9999px;
    font-weight: 500;
}

.chatbot-close,
.chatbot-history-toggle {
    background: transparent;
    border: none;
    color: var(--chat-text-muted);
    cursor: pointer;
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    font-size: 1rem;
}

.chatbot-close:hover,
.chatbot-history-toggle:hover {
    background-color: var(--chat-surface);
    color: var(--chat-text);
}

/* Messages Area */
.chatbot-messages {
    flex: 1;
    padding: 1.5rem;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    scroll-behavior: smooth;
    background-image: radial-gradient(circle at 50% 50%, rgba(59, 130, 246, 0.03) 0%, transparent 50%);
}

.message {
    max-width: 85%;
    padding: 1rem;
    border-radius: 12px;
    line-height: 1.6;
    font-size: 0.95rem;
    position: relative;
    word-wrap: break-word;
    animation: messageEnter 0.3s ease-out both;
}

@keyframes messageEnter {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message.user {
    align-self: flex-end;
    background: linear-gradient(135deg, var(--chat-primary) 0%, var(--chat-primary-dark) 100%);
    color: white;
    border-bottom-right-radius: 4px;
    box-shadow: 0 4px 6px -1px rgba(59, 130, 246, 0.3);
}

.message.bot {
    align-self: flex-start;
    background-color: var(--chat-surface);
    color: var(--chat-text);
    border: 1px solid var(--chat-border);
    border-bottom-left-radius: 4px;
}

.message.welcome-message {
    background: linear-gradient(to right, #1e293b, #0f172a);
    border: 1px solid var(--chat-primary);
}

/* Markdown Styling */
.message strong {
    font-weight: 600;
    color: inherit;
}

.message ul,
.message ol {
    padding-left: 1.25rem;
    margin: 0.5rem 0;
}

.message code {
    background-color: rgba(0, 0, 0, 0.3);
    padding: 0.1rem 0.4rem;
    border-radius: 4px;
    font-family: 'Consolas', monospace;
    font-size: 0.85em;
}

.message pre {
    background-color: #020617;
    padding: 1rem;
    border-radius: 8px;
    overflow-x: auto;
    margin: 0.75rem 0;
    border: 1px solid var(--chat-border);
}

/* Navigation Buttons inside chat */
.chat-nav-button {
    display: block;
    width: 100%;
    text-align: left;
    background-color: rgba(59, 130, 246, 0.1);
    color: var(--chat-primary);
    padding: 0.75rem;
    border-radius: 8px;
    border: 1px solid rgba(59, 130, 246, 0.2);
    margin-top: 0.5rem;
    cursor: pointer;
    transition: all 0.2s;
    font-weight: 500;
}

.chat-nav-button:hover {
    background-color: rgba(59, 130, 246, 0.2);
    border-color: var(--chat-primary);
}

.chat-nav-button::before {
    content: '🔗 ';
    margin-right: 5px;
}

/* Feedback & Redirect */
.redirect-btn {
    display: inline-block;
    color: var(--chat-primary);
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 500;
}

.redirect-btn:hover {
    text-decoration: underline;
}

.feedback-container {
    display: flex;
    gap: 8px;
    margin-top: 8px;
    justify-content: flex-end;
    opacity: 0.7;
    transition: opacity 0.2s;
}

.feedback-container:hover {
    opacity: 1;
}

.feedback-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 1px solid var(--chat-border);
    background: transparent;
    color: var(--chat-text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
}

.feedback-btn:hover {
    color: var(--chat-primary);
    border-color: var(--chat-primary);
    background: rgba(59, 130, 246, 0.1);
}

/* Typing Indicator */
.chatbot-typing {
    padding: 0 1.5rem 1rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    color: var(--chat-text-muted);
    font-size: 0.85rem;
    opacity: 0.8;
}

.typing-indicator span {
    width: 6px;
    height: 6px;
    background-color: var(--chat-primary);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out both;
}

.typing-indicator span:nth-child(1) {
    animation-delay: -0.32s;
}

.typing-indicator span:nth-child(2) {
    animation-delay: -0.16s;
}

/* Input Area */
.chatbot-input-container {
    padding: 1.25rem;
    background-color: var(--chat-surface);
    border-top: 1px solid var(--chat-border);
}

.chatbot-input {
    display: flex;
    gap: 0.75rem;
    background-color: var(--chat-bg);
    padding: 6px;
    border: 1px solid var(--chat-border);
    border-radius: 50px;
    /* Pill shape */
    transition: all 0.2s;
    align-items: center;
}

.chatbot-input:focus-within {
    border-color: var(--chat-primary);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

.chatbot-input input {
    flex: 1;
    background: transparent;
    border: none;
    padding: 0.5rem 1rem;
    color: var(--chat-text);
    font-size: 0.95rem;
    outline: none;
    min-width: 0;
}

.chatbot-send {
    background: linear-gradient(135deg, var(--chat-primary), var(--chat-primary-dark));
    color: white;
    border: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.2s;
    margin-right: 4px;
}

.chatbot-send:hover {
    transform: scale(1.05);
}

.chatbot-send:disabled {
    background: var(--chat-surface-hover);
    color: var(--chat-text-muted);
    cursor: not-allowed;
    transform: none;
}

/* Suggestions */
.chatbot-suggestions {
    display: flex;
    gap: 0.5rem;
    padding: 0.75rem 0 0;
    overflow-x: auto;
    /* ✅ FIX: Forzar que no haya salto de línea y scroll funcione */
    flex-wrap: nowrap;
    scrollbar-width: none;
    /* Asegurar que el contenedor tenga ancho para scrollear */
    width: 100%;
}

.chatbot-suggestions::-webkit-scrollbar {
    display: none;
}

.suggestion-btn {
    white-space: nowrap;
    /* ✅ FIX: Evitar que los botones se encojan */
    flex-shrink: 0;
    background-color: var(--chat-bg);
    border: 1px solid var(--chat-border);
    color: var(--chat-text-muted);
    padding: 0.4rem 0.8rem;
    font-size: 0.8rem;
    border-radius: 9999px;
    cursor: pointer;
    transition: all 0.2s;
}

.suggestion-btn:hover {
    background-color: var(--chat-surface-hover);
    color: var(--chat-primary);
    border-color: var(--chat-primary);
}

/* Floating Toggle Button */
.chatbot-toggle {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--chat-primary), var(--chat-primary-dark));
    color: white;
    border: none;
    box-shadow: 0 4px 15px rgba(59, 130, 246, 0.4);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.6rem;
    z-index: 9999;
    transition: transform 0.2s;
}

.chatbot-toggle:hover {
    transform: scale(1.1) rotate(5deg);
}

/* Notification Badge */
.chatbot-notification {
    position: absolute;
    top: 0;
    right: 0;
    width: 14px;
    height: 14px;
    background-color: #ef4444;
    border-radius: 50%;
    border: 2px solid var(--chat-bg);
}

/* Mobile & Overlay */
.chatbot-history-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 15;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s;
}

.chatbot-container.history-open .chatbot-history-overlay {
    opacity: 1;
    visibility: visible;
}

@media (max-width: 480px) {
    .chatbot-container {
        width: 100%;
        height: 100%;
        max-height: 100%;
        bottom: 0;
        right: 0;
        border-radius: 0;
        transform: translateY(100%);
        /* Ensure z-index is high enough to cover everything */
        z-index: 10000;
        border: none;
    }

    .chatbot-container.open {
        transform: translateY(0);
    }

    .chatbot-history-panel {
        width: 85%;
        max-width: 320px;
        z-index: 25;
    }

    .chatbot-toggle {
        bottom: 20px;
        right: 20px;
        width: 56px;
        height: 56px;
        font-size: 1.4rem;
    }

    /* Adjust padding for message area to account for mobile keyboard potentially */
    .chatbot-messages {
        padding: 1rem;
        padding-bottom: 2rem;
    }

    .chatbot-input-container {
        padding: 1rem;
        padding-bottom: env(safe-area-inset-bottom, 1rem);
        /* iPhone X+ safe area */
    }
}