/**
 * ArchiFlow AI Chat Widget Styles
 * Enterprise-grade floating chat interface - ArchiFlow Design System v2
 */

/* ========================================
   CSS Variables - ArchiFlow Professional Theme
   ======================================== */
#archiflow-chat-container {
    /* Primary Brand Colors */
    --af-green-light: #6ee7b7;
    --af-green-primary: #10b981;
    --af-green-dark: #059669;
    --af-teal-light: #5eead4;
    --af-teal-primary: #14b8a6;
    --af-teal-dark: #0f766e;

    /* Background Layers */
    --af-bg-primary: #0f172a;
    --af-bg-secondary: #1e293b;
    --af-bg-tertiary: #334155;
    --af-bg-elevated: #1e293b;
    --af-bg-hover: rgba(255, 255, 255, 0.04);

    /* Text Hierarchy */
    --af-text-primary: #f1f5f9;
    --af-text-secondary: #94a3b8;
    --af-text-muted: #64748b;
    --af-text-brand: #6ee7b7;

    /* Borders */
    --af-border-subtle: rgba(148, 163, 184, 0.1);
    --af-border-default: rgba(148, 163, 184, 0.15);
    --af-border-hover: rgba(148, 163, 184, 0.25);
    --af-border-accent: var(--af-green-primary);

    /* Status Colors */
    --af-warning: #f59e0b;
    --af-danger: #ef4444;
    --af-blue: #3b82f6;
    --af-purple: #8b5cf6;

    /* Shadows */
    --af-shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --af-shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --af-shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.5);
    --af-shadow-glow: 0 0 20px rgba(16, 185, 129, 0.15);
}

/* ========================================
   Light Mode Overrides — follows NetBox data-bs-theme
   ======================================== */
[data-bs-theme="light"] #archiflow-chat-container,
[data-bs-theme="light"] .archiflow-teamchat-panel,
#archiflow-chat-container.af-light-mode,
.archiflow-teamchat-panel.af-light-mode {
    --af-bg-primary: #ffffff;
    --af-bg-secondary: #f8fafc;
    --af-bg-tertiary: #f1f5f9;
    --af-bg-elevated: #ffffff;
    --af-bg-hover: rgba(0, 0, 0, 0.03);

    --af-text-primary: #1e293b;
    --af-text-secondary: #475569;
    --af-text-muted: #94a3b8;
    --af-text-brand: #059669;

    --af-border-subtle: rgba(0, 0, 0, 0.06);
    --af-border-default: rgba(0, 0, 0, 0.1);
    --af-border-hover: rgba(0, 0, 0, 0.18);

    --af-shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06);
    --af-shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
    --af-shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.12);
    --af-shadow-glow: 0 0 20px rgba(16, 185, 129, 0.08);
}

/* ========================================
   Container - Fixed Position
   ======================================== */
#archiflow-chat-container {
    position: fixed;
    bottom: 60px;
    left: 24px;
    z-index: 10000;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

/* ========================================
   Toggle Button - Floating FAB (Professional)
   ======================================== */
.archiflow-chat-btn {
    width: 56px;
    height: 56px;
    border-radius: 16px;
    background: var(--af-green-primary);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--af-shadow-lg), var(--af-shadow-glow);
    transition: all 0.2s ease;
    color: white;
    position: relative;
    overflow: visible;
}

.archiflow-chat-btn:hover {
    transform: translateY(-2px);
    background: var(--af-green-dark);
    box-shadow: var(--af-shadow-lg), 0 0 30px rgba(16, 185, 129, 0.25);
}

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

.archiflow-chat-btn svg {
    width: 24px;
    height: 24px;
}

/* AI Badge on button - minimal */
.archiflow-chat-btn::after {
    content: 'AI';
    position: absolute;
    top: -6px;
    right: -6px;
    background: var(--af-bg-secondary);
    color: var(--af-green-primary);
    font-size: 9px;
    font-weight: 700;
    padding: 3px 6px;
    border-radius: 6px;
    border: 1px solid var(--af-border-default);
    letter-spacing: 0.5px;
}

/* ========================================
   Chat Panel - Main Container (Professional)
   ======================================== */
.archiflow-chat-panel {
    position: absolute;
    bottom: 72px;
    left: 0;
    width: 400px;
    height: 560px;
    background: var(--af-bg-primary);
    border-radius: 16px;
    box-shadow: var(--af-shadow-lg);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: opacity 0.15s ease, transform 0.15s ease, visibility 0.15s;
    border: 1px solid var(--af-border-subtle);
}

.archiflow-chat-panel.hidden {
    opacity: 0;
    transform: translateY(12px) scale(0.98);
    pointer-events: none;
    visibility: hidden;
}

/* ========================================
   Header - Clean Professional Style
   ======================================== */
.archiflow-chat-header {
    padding: 12px 16px;
    background: var(--af-bg-secondary);
    border-bottom: 1px solid var(--af-border-subtle);
    color: var(--af-text-primary);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.archiflow-chat-header-title {
    font-weight: 600;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--af-text-primary);
}

.archiflow-chat-header-title svg {
    width: 18px;
    height: 18px;
    color: var(--af-green-primary);
}

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

.archiflow-chat-header button {
    background: transparent;
    border: none;
    color: var(--af-text-muted);
    width: 28px;
    height: 28px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s ease;
}

.archiflow-chat-header button:hover {
    background: var(--af-bg-hover);
    color: var(--af-text-primary);
}

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

/* ========================================
   Tabs Container - Segmented Control Style
   ======================================== */
.archiflow-chat-tabs {
    display: flex;
    background: var(--af-bg-primary);
    padding: 8px 12px;
    gap: 4px;
}

.archiflow-tab {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 12px;
    background: transparent;
    border: none;
    border-radius: 8px;
    color: var(--af-text-muted);
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s ease;
    position: relative;
}

.archiflow-tab:hover {
    color: var(--af-text-secondary);
    background: var(--af-bg-hover);
}

.archiflow-tab.active {
    color: var(--af-text-primary);
    background: var(--af-bg-secondary);
    box-shadow: var(--af-shadow-sm);
}

.archiflow-tab svg {
    flex-shrink: 0;
    width: 14px;
    height: 14px;
}

/* ========================================
   Tab Content Areas
   ======================================== */
.archiflow-tab-content {
    display: none;
    flex: 1;
    overflow-y: auto;
    background: var(--af-bg-primary);
}

.archiflow-tab-content.active {
    display: flex;
    flex-direction: column;
}

/* ========================================
   Search Results Container
   ======================================== */
.archiflow-search-results {
    flex: 1;
    padding: 12px;
    overflow-y: auto;
}

.search-welcome {
    padding: 24px 16px;
    text-align: center;
    color: var(--af-text-secondary);
}

.search-welcome strong {
    color: var(--af-text-primary);
    font-size: 14px;
    font-weight: 600;
}

.search-welcome p {
    margin: 8px 0;
    font-size: 13px;
    line-height: 1.5;
}

.search-welcome code {
    background: var(--af-bg-secondary);
    padding: 2px 6px;
    border-radius: 4px;
    color: var(--af-green-light);
    font-size: 12px;
    font-family: 'SF Mono', Monaco, Consolas, monospace;
}

.search-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 32px;
    color: var(--af-text-muted);
    font-size: 13px;
}

.loading-spinner {
    width: 16px;
    height: 16px;
    border: 2px solid var(--af-border-default);
    border-top-color: var(--af-green-primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

.search-error {
    padding: 16px;
    text-align: center;
    color: var(--af-danger);
    background: rgba(239, 68, 68, 0.08);
    border-radius: 8px;
    margin: 8px;
    font-size: 13px;
    border: 1px solid rgba(239, 68, 68, 0.2);
}

.search-empty {
    padding: 24px 16px;
    text-align: center;
    color: var(--af-text-secondary);
    font-size: 13px;
}

.search-hint {
    font-size: 12px;
    color: var(--af-text-muted);
    margin-top: 8px;
}

.search-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 48px;
    padding: 0 4px 10px;
    border-bottom: 1px solid var(--af-border-subtle);
}

.search-query {
    color: var(--af-text-secondary);
    font-size: 12px;
    font-weight: 500;
}

.search-timing {
    font-size: 11px;
    color: var(--af-green-primary);
    font-family: 'SF Mono', Monaco, Consolas, monospace;
}

.search-section {
    margin-bottom: 16px;
}

.search-section h4 {
    color: var(--af-text-muted);
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    font-weight: 600;
    margin: 0 0 8px 4px;
}

/* ========================================
   Result Cards - Clean List Style
   ======================================== */
.result-card {
    background: var(--af-bg-secondary);
    border: 1px solid var(--af-border-subtle);
    border-radius: 10px;
    padding: 10px 12px;
    margin-bottom: 6px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    transition: all 0.12s ease;
}

.result-card:hover {
    background: var(--af-bg-tertiary);
    border-color: var(--af-border-default);
}

.result-main {
    display: flex;
    align-items: center;
    gap: 10px;
    flex: 1;
    min-width: 0;
}

.result-icon {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 9px;
    font-weight: 700;
    letter-spacing: 0.3px;
    background: var(--af-bg-tertiary);
    color: var(--af-green-primary);
}

.result-icon.diagram-icon {
    background: rgba(59, 130, 246, 0.1);
    color: var(--af-blue);
}

.result-icon.device-icon {
    background: rgba(245, 158, 11, 0.1);
    color: var(--af-warning);
}

.result-info {
    flex: 1;
    min-width: 0;
}

.result-info strong {
    display: block;
    color: var(--af-text-primary);
    font-size: 13px;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.result-detail {
    display: inline-block;
    font-size: 11px;
    color: var(--af-text-muted);
    margin-right: 6px;
}

.result-context {
    display: block;
    font-size: 11px;
    color: var(--af-text-muted);
    margin-top: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.result-ip {
    display: inline-block;
    font-size: 10px;
    font-family: 'SF Mono', Monaco, Consolas, monospace;
    background: var(--af-bg-tertiary);
    color: var(--af-green-light);
    padding: 2px 5px;
    border-radius: 4px;
    margin-top: 3px;
}

.result-status {
    font-size: 9px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 3px 6px;
    border-radius: 4px;
    font-weight: 600;
    flex-shrink: 0;
}

.result-status.status-active {
    background: rgba(16, 185, 129, 0.12);
    color: var(--af-green-primary);
}

.result-status.status-staging {
    background: rgba(245, 158, 11, 0.12);
    color: var(--af-warning);
}

.result-status.status-draft {
    background: var(--af-bg-tertiary);
    color: var(--af-text-muted);
}

/* Result Buttons */
.result-link {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 5px 10px;
    border-radius: 6px;
    font-size: 11px;
    font-weight: 500;
    text-decoration: none;
    transition: all 0.12s ease;
    flex-shrink: 0;
    border: none;
    cursor: pointer;
}

.result-link.source-link {
    background: transparent;
    color: var(--af-text-muted);
    border: 1px solid var(--af-border-default);
}

.result-link.source-link:hover {
    color: var(--af-text-secondary);
    border-color: var(--af-border-hover);
    background: var(--af-bg-hover);
}

.result-link.diagram-link {
    background: var(--af-green-primary);
    color: #0f172a;
    font-weight: 600;
}

.result-link.diagram-link:hover {
    background: var(--af-green-light);
}

/* Result Actions - Button Group */
.result-actions {
    display: flex;
    gap: 4px;
    flex-shrink: 0;
}

/* ========================================
   Messages Container
   ======================================== */
.archiflow-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--af-bg-primary);
}

/* ========================================
   Message Bubbles - Clean Style
   ======================================== */
.chat-message {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: 12px;
    line-height: 1.5;
    font-size: 13px;
    word-wrap: break-word;
    animation: message-appear 0.2s ease-out;
}

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

/* User Messages - Right aligned */
.chat-message.user {
    align-self: flex-end;
    background: var(--af-green-primary);
    color: #0f172a;
    border-bottom-right-radius: 4px;
}

/* Assistant Messages - Left aligned */
.chat-message.assistant {
    align-self: flex-start;
    background: var(--af-bg-secondary);
    color: var(--af-text-primary);
    border-bottom-left-radius: 4px;
    border: 1px solid var(--af-border-subtle);
}

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

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

/* Links in messages - ArchiFlow accent */
.chat-message a {
    color: var(--af-green-light);
    text-decoration: none;
    font-weight: 500;
    transition: all 0.2s;
    border-bottom: 1px solid transparent;
}

.chat-message a:hover {
    color: var(--af-green-primary);
    border-bottom-color: var(--af-green-primary);
}

/* User message links */
.chat-message.user a {
    color: #bfdbfe;
}

.chat-message.user a:hover {
    color: white;
    border-bottom-color: white;
}

/* Lists in messages */
.chat-message ul, .chat-message ol {
    margin: 10px 0;
    padding-left: 20px;
}

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

/* Code in messages */
.chat-message code {
    background: rgba(0, 0, 0, 0.25);
    padding: 3px 8px;
    border-radius: 6px;
    font-family: 'SF Mono', Monaco, Consolas, monospace;
    font-size: 13px;
    color: var(--af-teal-light);
}

.chat-message.user code {
    background: rgba(255, 255, 255, 0.15);
    color: #bfdbfe;
}

/* Strong/Bold text */
.chat-message strong {
    color: var(--af-text-brand);
    font-weight: 600;
}

.chat-message.user strong {
    color: white;
}

/* ========================================
   Typing Indicator
   ======================================== */
.chat-message.typing {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 18px 22px;
    background: var(--af-bg-tertiary);
    border: 1px solid var(--af-border-default);
}

.chat-message.typing .typing-text {
    color: var(--af-text-secondary);
    font-size: 13px;
    margin-right: 6px;
    font-weight: 500;
}

.chat-message.typing .typing-timer {
    color: var(--af-text-muted);
    font-size: 12px;
    margin-left: 10px;
    font-family: 'SF Mono', Monaco, Consolas, monospace;
    background: rgba(52, 211, 153, 0.1);
    padding: 2px 8px;
    border-radius: 4px;
}

.chat-message.typing .dot {
    width: 8px;
    height: 8px;
    background: var(--af-green-primary);
    border-radius: 50%;
    animation: typing-bounce 1.4s infinite ease-in-out;
    box-shadow: 0 0 8px rgba(52, 211, 153, 0.3);
}

.chat-message.typing .dot:nth-child(2) { animation-delay: 0s; }
.chat-message.typing .dot:nth-child(3) { animation-delay: 0.2s; }
.chat-message.typing .dot:nth-child(4) { animation-delay: 0.4s; }

@keyframes typing-bounce {
    0%, 60%, 100% {
        transform: translateY(0);
        background: var(--af-green-primary);
    }
    30% {
        transform: translateY(-8px);
        background: var(--af-teal-light);
    }
}

/* ========================================
   Error Message
   ======================================== */
.chat-message.error {
    background: linear-gradient(135deg, #7f1d1d 0%, #991b1b 100%);
    color: #fca5a5;
    border: 1px solid #dc2626;
    border-left: 4px solid var(--af-danger);
}

/* ========================================
   Input Container - Clean Professional Style
   ======================================== */
.archiflow-chat-input-container {
    padding: 12px;
    background: var(--af-bg-secondary);
    border-top: 1px solid var(--af-border-subtle);
    display: flex;
    gap: 8px;
    align-items: center;
}

#archiflow-chat-input {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid var(--af-border-default);
    border-radius: 10px;
    background: var(--af-bg-primary);
    color: var(--af-text-primary);
    font-size: 13px;
    outline: none;
    transition: all 0.15s ease;
}

#archiflow-chat-input:focus {
    border-color: var(--af-green-primary);
    box-shadow: 0 0 0 2px rgba(16, 185, 129, 0.1);
}

#archiflow-chat-input::placeholder {
    color: var(--af-text-muted);
    font-size: 12px;
}

/* ========================================
   Send Button - Minimal Style
   ======================================== */
#archiflow-chat-send {
    width: 38px;
    height: 38px;
    border-radius: 10px;
    background: var(--af-green-primary);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    transition: all 0.15s ease;
    flex-shrink: 0;
}

#archiflow-chat-send:hover {
    background: var(--af-green-dark);
}

#archiflow-chat-send:active {
    transform: scale(0.95);
}

#archiflow-chat-send:disabled {
    background: var(--af-bg-tertiary);
    cursor: not-allowed;
    color: var(--af-text-muted);
}

#archiflow-chat-send svg {
    width: 18px;
    height: 18px;
}

/* ========================================
   RTL Support for Hebrew
   ======================================== */
[dir="rtl"] .chat-message,
.chat-message[dir="rtl"] {
    text-align: right;
}

[dir="rtl"] .chat-message ul,
[dir="rtl"] .chat-message ol {
    padding-left: 0;
    padding-right: 20px;
}

/* Auto-detect RTL content */
.chat-message p[dir="auto"] {
    unicode-bidi: plaintext;
}

/* ========================================
   Scrollbar Styling
   ======================================== */
.archiflow-chat-messages::-webkit-scrollbar {
    width: 8px;
}

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

.archiflow-chat-messages::-webkit-scrollbar-thumb {
    background: var(--af-border-hover);
    border-radius: 4px;
}

.archiflow-chat-messages::-webkit-scrollbar-thumb:hover {
    background: var(--af-text-muted);
}

/* ========================================
   Status Indicator - Minimal
   ======================================== */
.archiflow-chat-status {
    padding: 6px 12px;
    background: var(--af-bg-primary);
    font-size: 10px;
    color: var(--af-text-muted);
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.archiflow-chat-status::before {
    content: '';
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--af-text-muted);
}

.archiflow-chat-status.error {
    color: var(--af-danger);
}

.archiflow-chat-status.error::before {
    background: var(--af-danger);
}

.archiflow-chat-status.connected {
    color: var(--af-green-primary);
}

.archiflow-chat-status.connected::before {
    background: var(--af-green-primary);
}

/* ========================================
   Responsive Adjustments
   ======================================== */
@media (max-width: 480px) {
    #archiflow-chat-container {
        bottom: 48px;
        left: 12px;
        right: 12px;
    }

    .archiflow-chat-panel {
        width: calc(100vw - 24px);
        height: 70vh;
        max-height: 520px;
        left: 0;
        right: 0;
        bottom: 64px;
    }

    .archiflow-chat-btn {
        width: 48px;
        height: 48px;
        border-radius: 12px;
    }

    .archiflow-chat-btn svg {
        width: 20px;
        height: 20px;
    }

    .archiflow-chat-header {
        padding: 10px 12px;
    }

    .archiflow-chat-messages {
        padding: 12px;
        gap: 10px;
    }

    .chat-message {
        max-width: 90%;
        padding: 8px 12px;
        font-size: 12px;
    }
}

/* ========================================
   Animation for Panel Appearance
   ======================================== */
@keyframes chat-panel-appear {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.archiflow-chat-panel:not(.hidden) {
    animation: chat-panel-appear 0.15s ease-out;
}

/* ========================================
   Top Bar Buttons - NetBox Header Integration
   Two buttons: Chat (blue) and AI Search (green)
   ======================================== */

/* Hide the floating FAB when top bar button is present */
#archiflow-chat-container .archiflow-chat-btn {
    display: none !important;
}

/* Make container static so it doesn't create a stacking context */
#archiflow-chat-container {
    position: static !important;
}

/* Shared topbar button base */
#archiflow-topbar-btn-aisearch,
#archiflow-topbar-btn-chat {
    color: white;
    border: none;
    border-radius: 8px;
    padding: 7px 16px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 7px;
    transition: all 0.2s ease;
    white-space: nowrap;
    line-height: 1;
    position: relative;
}

/* AI Search button - green */
#archiflow-topbar-btn-aisearch {
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    box-shadow: 0 2px 8px rgba(16, 185, 129, 0.3);
}
#archiflow-topbar-btn-aisearch:hover {
    background: linear-gradient(135deg, #059669 0%, #047857 100%);
    box-shadow: 0 4px 12px rgba(16, 185, 129, 0.45);
    transform: translateY(-1px);
}

/* Chat button - blue */
#archiflow-topbar-btn-chat {
    background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
    box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
}
#archiflow-topbar-btn-chat:hover {
    background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.45);
    transform: translateY(-1px);
}

#archiflow-topbar-btn-aisearch:active,
#archiflow-topbar-btn-chat:active {
    transform: translateY(0);
}

#archiflow-topbar-btn-aisearch svg,
#archiflow-topbar-btn-chat svg {
    width: 16px;
    height: 16px;
    flex-shrink: 0;
}

/* Unread badge on Chat topbar button */
.topbar-unread-badge {
    position: absolute;
    top: -6px;
    right: -6px;
    background: #ef4444;
    color: white;
    font-size: 10px;
    font-weight: 700;
    min-width: 18px;
    height: 18px;
    border-radius: 9px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 5px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

/* Reposition the AI Search panel as a dropdown from the top bar */
#archiflow-chat-container .archiflow-chat-panel {
    position: fixed !important;
    top: 56px !important;
    right: 16px !important;
    bottom: auto !important;
    left: auto !important;
    width: 420px !important;
    height: min(580px, calc(100vh - 80px)) !important;
    z-index: 10001 !important;
    border-radius: 12px !important;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5) !important;
}

/* Backdrop overlay for click-outside-to-close */
#archiflow-panel-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.15);
    z-index: 10000;
    display: none;
}

#archiflow-panel-backdrop.active {
    display: block;
}

[data-bs-theme="light"] #archiflow-panel-backdrop {
    background: rgba(0, 0, 0, 0.08);
}

/* Light mode panel box-shadow adjustments */
[data-bs-theme="light"] #archiflow-chat-container .archiflow-chat-panel,
#archiflow-chat-container.af-light-mode .archiflow-chat-panel {
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12) !important;
    border: 1px solid rgba(0, 0, 0, 0.1) !important;
}

[data-bs-theme="light"] .archiflow-teamchat-panel,
.archiflow-teamchat-panel.af-light-mode {
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
    border-color: rgba(0, 0, 0, 0.1);
}

/* ========================================
   Team Chat Panel - Modern Messenger
   Separate 600x700 panel with sidebar + conversation
   ======================================== */
.archiflow-teamchat-panel {
    /* CSS Variables (same as main container) */
    --af-green-light: #6ee7b7;
    --af-green-primary: #10b981;
    --af-green-dark: #059669;
    --af-bg-primary: #0f172a;
    --af-bg-secondary: #1e293b;
    --af-bg-tertiary: #334155;
    --af-bg-hover: rgba(255, 255, 255, 0.04);
    --af-text-primary: #f1f5f9;
    --af-text-secondary: #94a3b8;
    --af-text-muted: #64748b;
    --af-border-subtle: rgba(148, 163, 184, 0.1);
    --af-border-default: rgba(148, 163, 184, 0.15);
    --af-danger: #ef4444;
    --af-blue: #3b82f6;

    position: fixed !important;
    top: 56px !important;
    right: 16px !important;
    width: 600px !important;
    height: min(700px, calc(100vh - 80px)) !important;
    z-index: 10001 !important;
    background: var(--af-bg-primary);
    border-radius: 12px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid var(--af-border-subtle);
    transition: opacity 0.15s ease, transform 0.15s ease, visibility 0.15s;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.archiflow-teamchat-panel.hidden {
    opacity: 0;
    transform: translateY(12px) scale(0.98);
    pointer-events: none;
    visibility: hidden;
}

.archiflow-teamchat-panel:not(.hidden) {
    animation: chat-panel-appear 0.15s ease-out;
}

/* Chat panel header */
.teamchat-header {
    padding: 12px 16px;
    background: linear-gradient(135deg, #1e3a5f, #1e293b);
    border-bottom: 1px solid rgba(59, 130, 246, 0.2);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.teamchat-header-title {
    font-weight: 600;
    font-size: 15px;
    color: white;
    display: flex;
    align-items: center;
    gap: 8px;
}

.teamchat-header-title svg {
    width: 20px;
    height: 20px;
    color: #60a5fa;
}

.teamchat-header-close {
    background: transparent;
    border: none;
    color: rgba(255, 255, 255, 0.5);
    width: 28px;
    height: 28px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s ease;
}

.teamchat-header-close:hover {
    background: rgba(255, 255, 255, 0.1);
    color: white;
}

/* Two-column body */
.teamchat-body {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* Sidebar - 180px user list */
.teamchat-sidebar {
    width: 180px;
    min-width: 180px;
    background: var(--af-bg-secondary);
    border-right: 1px solid var(--af-border-subtle);
    display: flex;
    flex-direction: column;
}

.teamchat-sidebar-header {
    padding: 10px;
    border-bottom: 1px solid var(--af-border-subtle);
}

.teamchat-user-search {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid var(--af-border-default);
    border-radius: 8px;
    background: var(--af-bg-primary);
    color: var(--af-text-primary);
    font-size: 12px;
    outline: none;
    box-sizing: border-box;
}

.teamchat-user-search:focus {
    border-color: #3b82f6;
}

.teamchat-user-search::placeholder {
    color: var(--af-text-muted);
}

.teamchat-contacts-list {
    flex: 1;
    overflow-y: auto;
}

/* Contact item - horizontal with preview */
.teamchat-contact {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 12px;
    cursor: pointer;
    transition: background 0.12s ease;
    border-bottom: 1px solid var(--af-border-subtle);
    position: relative;
}

.teamchat-contact:hover {
    background: rgba(255, 255, 255, 0.06);
}

.teamchat-contact.active {
    background: rgba(59, 130, 246, 0.15);
}

.teamchat-contact.active::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 3px;
    background: #3b82f6;
    border-radius: 0 2px 2px 0;
}

/* Avatar with online dot */
.teamchat-contact-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    font-weight: 600;
    color: white;
    flex-shrink: 0;
    position: relative;
}

.teamchat-online-dot {
    position: absolute;
    bottom: 1px;
    right: 1px;
    width: 8px;
    height: 8px;
    background: #22c55e;
    border-radius: 50%;
    border: 2px solid var(--af-bg-secondary);
    box-shadow: 0 0 0 1px rgba(34, 197, 94, 0.3);
}

.teamchat-contact-info {
    flex: 1;
    min-width: 0;
}

.teamchat-contact-name {
    font-size: 13px;
    font-weight: 500;
    color: var(--af-text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.teamchat-contact-preview {
    font-size: 11px;
    color: var(--af-text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 2px;
}

.teamchat-contact-badge {
    background: #ef4444;
    color: white;
    font-size: 10px;
    font-weight: 700;
    min-width: 18px;
    height: 18px;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 5px;
    flex-shrink: 0;
}

.teamchat-no-users,
.teamchat-contacts-loading {
    padding: 20px 12px;
    text-align: center;
    font-size: 12px;
    color: var(--af-text-muted);
}

/* Conversation area */
.teamchat-conversation {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.teamchat-conv-header {
    padding: 10px 16px;
    background: var(--af-bg-secondary);
    border-bottom: 1px solid var(--af-border-subtle);
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 48px;
}

.teamchat-conv-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 600;
    color: white;
}

.teamchat-conv-name {
    font-weight: 500;
    color: var(--af-text-primary);
    font-size: 14px;
}

.teamchat-conv-placeholder {
    color: var(--af-text-muted);
    font-size: 13px;
}

/* Messages area */
.teamchat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 2px;
    background: var(--af-bg-primary);
    background-image: radial-gradient(circle at 20% 50%, rgba(59, 130, 246, 0.03) 0%, transparent 50%),
                      radial-gradient(circle at 80% 20%, rgba(59, 130, 246, 0.02) 0%, transparent 40%);
}

.teamchat-messages::-webkit-scrollbar {
    width: 6px;
}
.teamchat-messages::-webkit-scrollbar-track {
    background: transparent;
}
.teamchat-messages::-webkit-scrollbar-thumb {
    background: rgba(148, 163, 184, 0.2);
    border-radius: 3px;
}

/* Message bubbles - messenger style */
.teamchat-message {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    max-width: 75%;
    margin-bottom: 2px;
    animation: message-appear 0.2s ease-out;
}

.teamchat-message.sent {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.teamchat-message.received {
    align-self: flex-start;
}

.teamchat-msg-avatar {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 9px;
    font-weight: 700;
    color: white;
    flex-shrink: 0;
    margin-bottom: 2px;
}

.teamchat-msg-bubble {
    padding: 8px 12px;
    border-radius: 16px;
    font-size: 13px;
    line-height: 1.45;
    word-wrap: break-word;
    white-space: pre-wrap;
    display: inline-block;
    position: relative;
}

.teamchat-message.sent .teamchat-msg-bubble {
    background: linear-gradient(135deg, #3b82f6, #2563eb);
    color: white;
    border-bottom-right-radius: 4px;
}

.teamchat-message.received .teamchat-msg-bubble {
    background: var(--af-bg-tertiary);
    color: var(--af-text-primary);
    border-bottom-left-radius: 4px;
}

.teamchat-msg-text {
    display: inline;
}

.teamchat-msg-meta {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    margin-left: 8px;
    vertical-align: bottom;
    float: right;
    margin-top: 4px;
}

.teamchat-msg-time {
    font-size: 10px;
    opacity: 0.65;
    white-space: nowrap;
}

.teamchat-message.sent .teamchat-msg-time {
    color: rgba(255, 255, 255, 0.75);
}

.teamchat-message.received .teamchat-msg-time {
    color: var(--af-text-muted);
}

.teamchat-msg-status {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.7);
    display: inline-flex;
    align-items: center;
}

.teamchat-msg-status svg {
    width: 14px;
    height: 14px;
}

/* Date separator */
.teamchat-date-separator {
    text-align: center;
    margin: 12px 0;
    align-self: center;
}

.teamchat-date-separator span {
    background: var(--af-bg-tertiary);
    color: var(--af-text-muted);
    font-size: 10px;
    padding: 4px 12px;
    border-radius: 10px;
}

/* Typing indicator */
.teamchat-typing-indicator {
    padding: 4px 16px;
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: var(--af-text-muted);
}

.typing-dots {
    display: inline-flex;
    gap: 3px;
}

.typing-dots span {
    display: inline-block;
    width: 6px;
    height: 6px;
    background: var(--af-text-muted);
    border-radius: 50%;
    animation: typing-bounce 1.4s infinite ease-in-out;
}

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

/* Chat input */
.teamchat-input-container {
    padding: 10px 12px;
    background: var(--af-bg-secondary);
    border-top: 1px solid var(--af-border-subtle);
    display: flex;
    gap: 8px;
    align-items: center;
}

.teamchat-input {
    flex: 1;
    padding: 10px 16px;
    border: 1px solid var(--af-border-default);
    border-radius: 24px;
    background: var(--af-bg-primary);
    color: var(--af-text-primary);
    font-size: 13px;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.teamchat-input:focus {
    border-color: #3b82f6;
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.15);
}

.teamchat-input::placeholder {
    color: var(--af-text-muted);
}

.teamchat-send-btn {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: linear-gradient(135deg, #3b82f6, #2563eb);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    transition: all 0.15s;
    flex-shrink: 0;
    box-shadow: 0 2px 6px rgba(59, 130, 246, 0.3);
}

.teamchat-send-btn:hover {
    background: linear-gradient(135deg, #2563eb, #1d4ed8);
    transform: scale(1.05);
}

.teamchat-send-btn:disabled {
    background: var(--af-bg-tertiary);
    color: var(--af-text-muted);
    cursor: not-allowed;
    box-shadow: none;
    transform: none;
}

.teamchat-send-btn svg {
    width: 16px;
    height: 16px;
}

/* Empty/welcome state */
.teamchat-welcome {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--af-text-muted);
    gap: 12px;
    text-align: center;
    padding: 20px;
}

.teamchat-welcome svg {
    color: var(--af-text-muted);
    opacity: 0.5;
}

/* Mobile responsive */
@media (max-width: 700px) {
    .archiflow-teamchat-panel {
        width: calc(100vw - 16px) !important;
        height: 80vh !important;
        right: 8px !important;
    }
    .teamchat-sidebar {
        width: 60px;
        min-width: 60px;
    }
    .teamchat-contact-info,
    .teamchat-sidebar-header { display: none; }
}

@media (max-width: 480px) {
    #archiflow-chat-container .archiflow-chat-panel {
        width: calc(100vw - 16px) !important;
        right: 8px !important;
        height: 70vh !important;
    }

    #archiflow-topbar-btn-aisearch span,
    #archiflow-topbar-btn-chat span {
        display: none;
    }

    #archiflow-topbar-btn-aisearch,
    #archiflow-topbar-btn-chat {
        padding: 7px 10px;
    }
}
