/* AI Chat Widget Styles */
.ai-chat-widget {
    position: fixed;
    bottom: 60px;
    right: 20px;
    width: 500px;
    height: 700px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
    display: flex;
    flex-direction: column;
    z-index: 9999;
    transition: left 0.3s ease, top 0.3s ease, bottom 0.3s ease;
    resize: both;
    overflow: hidden;
}

.ai-chat-widget.collapsed .ai-chat-body {
    display: none;
}

.ai-chat-widget.collapsed {
    max-height: 60px;
    height: 60px !important;
    min-height: 60px;
    resize: none;
}

.ai-chat-widget.minimized {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    bottom: 20px;
    right: 20px;
}

.ai-chat-widget.minimized .ai-chat-header,
.ai-chat-widget.minimized .ai-chat-body {
    display: none;
}

.ai-chat-header {
    background: linear-gradient(135deg, #28b9b8 0%, #20928f 100%);
    color: white;
    padding: 16px;
    border-radius: 12px 12px 0 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: move;
    user-select: none;
}

.ai-chat-header.dragging {
    cursor: grabbing;
}

/* Disable transitions during drag for instant response */
.ai-chat-widget.dragging {
    transition: none !important;
}

.ai-chat-widget.collapsed .ai-chat-header {
    cursor: pointer;
}

.chat-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    font-size: 16px;
}

.chat-controls {
    display: flex;
    gap: 4px;
}

.chat-controls .btn {
    padding: 4px 8px;
}

.ai-chat-body {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    background: #f8f9fa;
}

.chat-message {
    margin-bottom: 12px;
    animation: slideIn 0.3s ease;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.chat-message.user-message {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

.chat-message.user-message .message-bubble {
    background: #28b9b8;
    color: white;
    border-radius: 18px 18px 4px 18px;
}

.chat-message.ai-message .message-bubble {
    background: white;
    color: #333;
    border-radius: 18px 18px 18px 4px;
    border: 1px solid #e0e0e0;
}

.message-name-label {
    font-size: 0.75em;
    font-weight: 600;
    margin-bottom: 4px;
    padding-left: 4px;
    color: #666;
}

.chat-message.user-message .message-name-label {
    text-align: right;
    padding-right: 4px;
    padding-left: 0;
    color: #28b9b8;
}

.chat-message.ai-message .message-name-label {
    color: #1976d2;
}

.chat-message.system-message {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 8px;
    background: #e3f2fd;
    border-radius: 8px;
    font-size: 13px;
    color: #1976d2;
}

.message-bubble {
    padding: 10px 14px;
    max-width: 80%;
    word-wrap: break-word;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
    font-size: 0.9em;
    line-height: 1.5;
}

.message-bubble h1, .message-bubble h2, .message-bubble h3,
.message-bubble h4, .message-bubble h5, .message-bubble h6 {
    margin: 12px 0 8px 0;
    font-weight: 600;
    line-height: 1.3;
}

/* Override Bootstrap's "bolder" on strong tags - it's too heavy and looks blurry */
.message-bubble strong {
    font-weight: 600;
}

/* Headings already have font-weight: 600, so strong inside them should just inherit */
.message-bubble h1 strong, .message-bubble h2 strong, .message-bubble h3 strong,
.message-bubble h4 strong, .message-bubble h5 strong, .message-bubble h6 strong {
    font-weight: inherit;
}

.message-bubble h1 { font-size: 1.4em; }
.message-bubble h2 { font-size: 1.25em; }
.message-bubble h3 { font-size: 1.1em; }
.message-bubble h4, .message-bubble h5, .message-bubble h6 { font-size: 1em; }

/* Horizontal rule / separator */
.message-bubble hr.chat-separator {
    border: none;
    border-top: 1px solid #999;
    margin: 12px 0;
}

.message-bubble code {
    background: rgba(0, 0, 0, 0.1);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 12px;
}

.message-bubble pre {
    background: rgba(0, 0, 0, 0.1);
    padding: 8px;
    border-radius: 4px;
    overflow-x: auto;
    margin: 8px 0;
}

.chat-input-container {
    border-top: 1px solid #e0e0e0;
    padding: 12px;
    background: white;
    border-radius: 0 0 12px 12px;
}

.chat-input-container .input-group {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}

.context-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: #666;
    margin-bottom: 8px;
    padding: 4px 8px;
    background: #f0f0f0;
    border-radius: 4px;
}

.context-indicator i {
    color: #28b9b8;
}

#contextToggleBtn {
    margin-left: auto;
    padding: 2px 6px;
    color: #28b9b8;
}

#contextToggleBtn:hover {
    color: #20928f;
}

#contextToggleBtn i {
    font-size: 16px;
}

#chatInput {
    flex: 1;
    resize: none;
    border-radius: 20px;
    padding: 8px 16px;
    border: 1px solid #e0e0e0;
    font-size: 14px;
    max-height: 120px;
    line-height: 1.5;
    overflow-y: auto;
}

#chatInput:focus {
    border-color: #28b9b8;
    box-shadow: 0 0 0 3px rgba(40, 185, 184, 0.1);
    outline: none;
}

#chatSendBtn {
    border-radius: 50%;
    width: 36px;
    height: 36px;
    padding: 0;
    background: #28b9b8;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

#chatSendBtn i {
    margin-top: -2px;
    font-size: 14px;
}

#chatSendBtn:hover {
    background: #20928f;
}

#chatSendBtn:disabled {
    background: #ccc;
    cursor: not-allowed;
}

.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 10px 14px;
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    background: #999;
    border-radius: 50%;
    animation: typing 1.4s infinite;
}

.typing-indicator span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typing {
    0%, 60%, 100% {
        transform: translateY(0);
    }
    30% {
        transform: translateY(-10px);
    }
}

/* Scrollbar styling */
.chat-messages::-webkit-scrollbar {
    width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
    background: #f1f1f1;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* Floating button when minimized */
.ai-chat-widget.minimized::before {
    content: '\f086';
    font-family: 'Font Awesome 5 Free';
    font-weight: 900;
    font-size: 24px;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #28b9b8 0%, #20928f 100%);
    border-radius: 50%;
    cursor: pointer;
}

/* Thinking section styles */
.thinking-section {
    margin-bottom: 12px;
    border-left: 3px solid #28b9b8;
    padding-left: 10px;
}

.thinking-toggle {
    background: transparent;
    border: none;
    color: #28b9b8;
    font-size: 12px;
    padding: 4px 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 4px;
    font-weight: 500;
}

.thinking-toggle:hover {
    color: #20928f;
}

.thinking-toggle i {
    font-size: 11px;
}

.thinking-content {
    margin-top: 8px;
    padding: 10px;
    background: rgba(40, 185, 184, 0.05);
    border-radius: 6px;
    font-style: italic;
    font-size: 0.85em;
    color: #666;
    line-height: 1.5;
}

.answer-content {
    margin-top: 8px;
}

/* Streaming thinking styles */
.thinking-section.streaming {
    border-left: 3px solid #28b9b8;
    padding-left: 10px;
    margin-bottom: 12px;
}

.thinking-label {
    color: #28b9b8;
    font-size: 12px;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 4px;
    margin-bottom: 8px;
}

.thinking-label i {
    animation: pulse 1.5s infinite;
}

@keyframes pulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.5;
    }
}

.thinking-content-streaming {
    padding: 10px;
    background: rgba(40, 185, 184, 0.05);
    border-radius: 6px;
    font-style: italic;
    font-size: 0.85em;
    color: #666;
    line-height: 1.5;
}

/* Collapsed thinking (hidden during generation) */
.thinking-section.collapsed-thinking {
    padding: 6px 10px;
}

.thinking-section.collapsed-thinking .thinking-label {
    margin-bottom: 0;
    font-size: 0.8em;
}

/* Animated thinking indicator when hidden */
.thinking-section.collapsed-thinking .thinking-label i {
    animation: thinking-pulse 1.5s ease-in-out infinite;
}

.thinking-section.collapsed-thinking .thinking-dots::after {
    content: '';
    animation: thinking-dots 1.5s steps(4, end) infinite;
}

@keyframes thinking-pulse {
    0%, 100% {
        opacity: 1;
        transform: scale(1);
    }
    50% {
        opacity: 0.5;
        transform: scale(1.1);
    }
}

@keyframes thinking-dots {
    0% { content: ''; }
    25% { content: '.'; }
    50% { content: '..'; }
    75% { content: '...'; }
    100% { content: ''; }
}

/* Streaming cursor */
.streaming-cursor {
    display: inline-block;
    width: 8px;
    height: 16px;
    background: #28b9b8;
    margin-left: 2px;
    animation: blink 1s infinite;
}

@keyframes blink {
    0%, 50% {
        opacity: 1;
    }
    51%, 100% {
        opacity: 0;
    }
}

/* Research/Tool execution indicator */
.research-indicator {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 14px;
    background: linear-gradient(135deg, #f0f7ff 0%, #e6f2ff 100%);
    border: 1px solid #b8d4f0;
    border-radius: 20px;
    font-size: 0.85em;
    color: #2c5aa0;
    margin: 8px 0;
    font-weight: 500;
}

.research-indicator i {
    color: #2c5aa0;
}

/* KaTeX math rendering styles */
.message-bubble .katex {
    font-size: 1em;
}

.message-bubble .katex-display {
    margin: 12px 0;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 8px 0;
}

.message-bubble .katex-display > .katex {
    font-size: 1.1em;
}

/* Ensure fractions and other tall elements don't clip */
.message-bubble .katex .frac-line {
    border-bottom-width: 0.04em;
}

/* Responsive adjustments */
@media (max-width: 768px) {
    .ai-chat-widget {
        width: 100%;
        max-width: calc(100vw - 40px);
        right: 20px;
        left: 20px;
    }
}

/* Offline/Unavailable state styles */
.ai-chat-widget.agent-offline .ai-chat-header {
    background: linear-gradient(135deg, #6c757d 0%, #495057 100%);
}

.ai-chat-widget.agent-offline .ai-chat-header.offline {
    background: linear-gradient(135deg, #6c757d 0%, #495057 100%);
}

.offline-indicator {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin-left: 8px;
    padding: 2px 8px;
    background: rgba(255, 193, 7, 0.9);
    color: #212529;
    border-radius: 10px;
    font-size: 11px;
    font-weight: 500;
}

.offline-indicator i {
    font-size: 10px;
}

.ai-chat-widget.agent-offline #chatInput {
    background: #f5f5f5;
    color: #999;
}

.ai-chat-widget.agent-offline #chatSendBtn {
    background: #ccc;
    cursor: not-allowed;
}

.ai-chat-widget.agent-offline.minimized::before {
    background: linear-gradient(135deg, #6c757d 0%, #495057 100%);
}

/* Request Agent Banner */
.request-agent-banner {
    margin: 12px 0;
    animation: slideIn 0.3s ease;
}

.request-agent-content {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    background: linear-gradient(135deg, #fff3e0 0%, #ffe0b2 100%);
    border: 1px solid #ffb74d;
    border-radius: 12px;
}

.request-agent-content > i {
    font-size: 20px;
    color: #e65100;
    flex-shrink: 0;
}

.request-agent-text {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
}

.request-agent-text strong {
    font-size: 13px;
    color: #333;
}

.request-agent-text span {
    font-size: 12px;
    color: #666;
}

.btn-request-agent {
    background: #28b9b8;
    color: white;
    border: none;
    border-radius: 20px;
    padding: 6px 16px;
    font-size: 13px;
    font-weight: 500;
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    transition: background 0.2s;
    flex-shrink: 0;
}

.btn-request-agent:hover {
    background: #20928f;
    color: white;
}

/* Polling state */
.request-agent-content.polling {
    background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%);
    border-color: #64b5f6;
}

.request-agent-content.polling .request-agent-text strong {
    color: #1565c0;
}

.request-agent-content.polling .request-agent-text span {
    color: #1976d2;
}

.request-agent-spinner {
    width: 24px;
    height: 24px;
    border: 3px solid #bbdefb;
    border-top-color: #1565c0;
    border-radius: 50%;
    animation: agent-spin 0.8s linear infinite;
    flex-shrink: 0;
}

@keyframes agent-spin {
    to { transform: rotate(360deg); }
}

/* Error state */
.request-agent-content.error {
    background: linear-gradient(135deg, #fce4ec 0%, #f8bbd0 100%);
    border-color: #e57373;
}

.request-agent-content.error > i {
    color: #c62828;
}

.request-agent-content.error .request-agent-text strong {
    color: #c62828;
}

.request-agent-content.error .btn-request-agent {
    background: #e57373;
}

.request-agent-content.error .btn-request-agent:hover {
    background: #ef5350;
}
