/* ===== AI Chat Panel — Floating Widget ===== */

/* Floating trigger button */
.insights-floating-btn {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 52px;
    height: 52px;
    border-radius: 50%;
    background: var(--primary-color);
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    z-index: 8500;
    transition: transform 0.2s, box-shadow 0.2s, background 0.2s;
}

.insights-floating-btn:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
}

.insights-floating-btn.panel-open {
    background: #374151;
}

/* Panel — floating bottom-right widget */
.ai-chat-panel {
    position: fixed;
    bottom: 88px;
    right: 24px;
    width: 380px;
    height: 560px;
    background: var(--card-bg, #ffffff);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.16), 0 2px 8px rgba(0, 0, 0, 0.08);
    z-index: 9000;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    transform: translateY(16px) scale(0.97);
    pointer-events: none;
    transition: opacity 0.25s cubic-bezier(0.4, 0, 0.2, 1),
                transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    font-family: inherit;
}

.ai-chat-panel.open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: all;
}

/* Header */
.ai-chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px;
    border-bottom: 1px solid var(--border-color);
    background: var(--card-bg, #fff);
    flex-shrink: 0;
}

.ai-chat-header-left {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.ai-chat-header-avatar {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    background: linear-gradient(135deg, var(--primary-color) 0%, #1557b0 100%);
    color: white;
    font-size: 16px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.ai-chat-header-text {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.ai-chat-title {
    font-weight: 700;
    font-size: 0.9rem;
    color: var(--text-primary);
    line-height: 1.2;
}

.ai-chat-subtitle {
    font-size: 0.75rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ai-chat-header-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
}

.ai-chat-icon-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    border-radius: 6px;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
    font-family: inherit;
}

.ai-chat-icon-btn:hover {
    background: var(--bg-secondary, #f3f4f6);
    color: var(--text-primary);
}

.ai-chat-close-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    border-radius: 6px;
    width: 30px;
    height: 30px;
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s;
    font-family: inherit;
}

.ai-chat-close-btn:hover {
    background: var(--bg-secondary, #f3f4f6);
    color: var(--text-primary);
}

/* Messages area */
.ai-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    scroll-behavior: smooth;
}

.ai-chat-messages::-webkit-scrollbar {
    width: 4px;
}

.ai-chat-messages::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

/* Welcome state */
.ai-chat-welcome {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 24px 20px;
    gap: 6px;
    flex: 1;
}

.ai-chat-welcome-icon {
    font-size: 36px;
    margin-bottom: 8px;
}

.ai-chat-welcome-title {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-primary);
    margin: 0;
}

.ai-chat-welcome-desc {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin: 0 0 16px 0;
    line-height: 1.5;
    max-width: 280px;
}

.ai-chat-welcome-actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: 100%;
    max-width: 260px;
}

.ai-chat-welcome-btn {
    padding: 10px 16px;
    border-radius: 10px;
    border: 1.5px solid var(--border-color);
    background: var(--card-bg, #fff);
    color: var(--text-primary);
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s;
    font-family: inherit;
    display: flex;
    align-items: center;
    gap: 8px;
}

.ai-chat-welcome-btn:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
    background: #eef3ff;
}

.ai-chat-welcome-btn-icon {
    font-size: 16px;
}

/* Message rows */
.ai-message {
    display: flex;
    flex-direction: column;
    gap: 4px;
    max-width: 100%;
    animation: msg-in 0.2s ease;
}

@keyframes msg-in {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}

.ai-message-label {
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 0 2px;
}

/* AI bubble — left accent border style */
.ai-message-bubble {
    background: var(--bg-secondary, #f8f9fa);
    border: 1px solid var(--border-color);
    border-left: 3px solid var(--primary-color);
    border-radius: 0 12px 12px 12px;
    padding: 12px 14px;
    font-size: 0.875rem;
    line-height: 1.65;
    color: var(--text-primary);
    position: relative;
    transition: opacity 0.15s ease;
}

/* Formatted content inside AI bubbles */
.ai-message-bubble .insight-header {
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--text-primary);
    margin: 14px 0 6px 0;
    padding-bottom: 5px;
    border-bottom: 1px solid var(--border-color);
}

.ai-message-bubble .insight-header:first-child {
    margin-top: 0;
}

.ai-message-bubble .insight-subheader {
    font-size: 0.875rem;
    font-weight: 600;
    margin: 10px 0 4px 0;
}

.ai-message-bubble .insight-list {
    margin: 6px 0 10px 0;
    padding-left: 0;
    list-style: none;
}

.ai-message-bubble .insight-list li {
    padding-left: 16px;
    position: relative;
    margin: 5px 0;
    line-height: 1.6;
}

.ai-message-bubble .insight-list li::before {
    content: '•';
    position: absolute;
    left: 3px;
    color: var(--primary-color);
    font-weight: bold;
}

.ai-message-bubble .insight-numbered-list {
    margin: 6px 0 10px 0;
    padding-left: 0;
    list-style: none;
    counter-reset: insight-counter;
}

.ai-message-bubble .insight-numbered-list li {
    padding-left: 22px;
    position: relative;
    margin: 7px 0;
    line-height: 1.6;
    counter-increment: insight-counter;
}

.ai-message-bubble .insight-numbered-list li::before {
    content: counter(insight-counter) ".";
    position: absolute;
    left: 0;
    color: var(--primary-color);
    font-weight: 700;
    font-size: 0.8rem;
}

.ai-message-bubble .insight-paragraph {
    margin: 0 0 8px 0;
}

.ai-message-bubble .insight-paragraph:last-child {
    margin-bottom: 0;
}

.ai-message-bubble strong {
    font-weight: 600;
}

/* Emoji indicator pills */
.ai-message-bubble .emoji-success {
    display: inline-block;
    background: #dcfce7;
    border-radius: 4px;
    padding: 1px 5px;
    margin-right: 3px;
}

.ai-message-bubble .emoji-warning {
    display: inline-block;
    background: #fef9c3;
    border-radius: 4px;
    padding: 1px 5px;
    margin-right: 3px;
}

.ai-message-bubble .emoji-critical {
    display: inline-block;
    background: #fee2e2;
    border-radius: 4px;
    padding: 1px 5px;
    margin-right: 3px;
}

.ai-message-bubble .emoji-info {
    display: inline-block;
    background: #dbeafe;
    border-radius: 4px;
    padding: 1px 5px;
    margin-right: 3px;
}

/* Streaming cursor */
.ai-streaming-cursor {
    display: inline-block;
    width: 2px;
    height: 0.9em;
    background: var(--primary-color);
    vertical-align: text-bottom;
    margin-left: 2px;
    border-radius: 1px;
    animation: blink-cursor 0.75s step-end infinite;
}

@keyframes blink-cursor {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}

/* Message footer — copy button */
.ai-message-footer {
    display: flex;
    justify-content: flex-end;
    margin-top: 6px;
}

.ai-copy-btn {
    background: none;
    border: 1px solid var(--border-color);
    border-radius: 5px;
    padding: 3px 8px;
    font-size: 0.72rem;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.15s;
    font-family: inherit;
    display: flex;
    align-items: center;
    gap: 4px;
}

.ai-copy-btn:hover {
    background: var(--bg-secondary, #f3f4f6);
    color: var(--text-primary);
}

.ai-copy-btn.copied {
    color: #16a34a;
    border-color: #16a34a;
}

/* User message */
.user-message {
    align-items: flex-end;
}

.user-message .ai-message-label {
    text-align: right;
}

.user-message .ai-message-bubble {
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: 12px 12px 2px 12px;
    max-width: 82%;
    align-self: flex-end;
    font-size: 0.875rem;
}

/* Typing indicator */
.ai-typing-indicator .ai-message-bubble {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 14px 16px;
    width: fit-content;
}

/* Thinking bubble — shown during Gemini's planning delay */
.ai-thinking-bubble {
    display: flex !important;
    align-items: center;
    gap: 10px;
    width: fit-content;
}

.ai-thinking-text {
    font-size: 0.82rem;
    color: var(--text-secondary);
    font-style: italic;
    transition: opacity 0.4s;
}

.ai-thinking-dots {
    display: flex;
    align-items: center;
    gap: 4px;
}

.ai-typing-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--text-secondary);
    animation: typing-bounce 1.2s ease-in-out infinite;
}

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

@keyframes typing-bounce {
    0%, 80%, 100% { transform: translateY(0); opacity: 0.35; }
    40% { transform: translateY(-5px); opacity: 1; }
}

/* Suggested chips */
.ai-chat-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 6px 16px 8px;
    flex-shrink: 0;
}

.ai-chat-chips:empty {
    display: none;
}

.ai-chip {
    padding: 5px 11px;
    background: var(--card-bg, #fff);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    font-size: 0.78rem;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.15s;
    font-family: inherit;
    line-height: 1.3;
    text-align: left;
    max-width: 100%;
}

.ai-chip:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
    background: #eef3ff;
}

/* Input area */
.ai-chat-input-area {
    border-top: 1px solid var(--border-color);
    padding: 10px 12px;
    flex-shrink: 0;
    background: var(--card-bg, #fff);
    border-radius: 0 0 16px 16px;
}

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

.ai-chat-input {
    flex: 1;
    padding: 8px 12px;
    border: 1.5px solid var(--border-color);
    border-radius: 10px;
    font-size: 0.875rem;
    font-family: inherit;
    color: var(--text-primary);
    background: var(--bg-secondary, #f8f9fa);
    resize: none;
    outline: none;
    line-height: 1.5;
    max-height: 100px;
    overflow-y: auto;
    transition: border-color 0.2s, background 0.2s;
}

.ai-chat-input:focus {
    border-color: var(--primary-color);
    background: var(--card-bg, #fff);
}

.ai-chat-input::placeholder {
    color: var(--text-tertiary, #9ca3af);
}

.ai-chat-send-btn {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: var(--primary-color);
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: opacity 0.2s, transform 0.1s;
}

.ai-chat-send-btn:hover {
    opacity: 0.88;
}

.ai-chat-send-btn:active {
    transform: scale(0.94);
}

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

/* Error state inside chat */
.ai-error-bubble {
    background: #fef2f2;
    border: 1px solid #fecaca;
    border-left: 3px solid #ef4444;
    border-radius: 0 10px 10px 10px;
    padding: 10px 14px;
    font-size: 0.85rem;
    color: #dc2626;
    display: flex;
    align-items: flex-start;
    gap: 8px;
}

.ai-error-retry {
    margin-top: 8px;
    padding: 5px 12px;
    background: none;
    border: 1px solid #dc2626;
    border-radius: 6px;
    color: #dc2626;
    font-size: 0.8rem;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.15s;
}

.ai-error-retry:hover {
    background: #dc2626;
    color: white;
}

/* Mobile: expand to near full-screen */
@media (max-width: 480px) {
    .ai-chat-panel {
        right: 12px;
        left: 12px;
        width: auto;
        bottom: 80px;
        height: 70vh;
    }

    .insights-floating-btn {
        right: 16px;
        bottom: 16px;
    }
}
