/* === Chat Page Layout === */
.chat-layout {
    display: flex;
    height: calc(100vh - 120px);
    min-height: 400px;
    gap: 0;
    margin: 0 -16px;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid var(--color-border, #e5e7eb);
    background: #fff;
    position: relative;
}

/* === Sidebar === */
.chat-sidebar {
    width: 260px;
    flex-shrink: 0;
    border-right: 1px solid var(--color-border, #e5e7eb);
    display: flex;
    flex-direction: column;
    background: #f9fafb;
    overflow: hidden;
}

.chat-sidebar-header {
    padding: 14px 16px;
    border-bottom: 1px solid var(--color-border, #e5e7eb);
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    flex-shrink: 0;
}

.chat-sidebar-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
}

.chat-sidebar-empty {
    padding: 12px 16px;
    font-size: 13px;
    color: #9ca3af;
}

.chat-conv-item {
    display: flex;
    flex-direction: column;
    padding: 10px 16px;
    font-size: 13px;
    color: #374151;
    text-decoration: none;
    cursor: pointer;
    border-left: 3px solid transparent;
    transition: background 0.1s;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    min-height: 48px;
    justify-content: center;
}
.chat-conv-item:hover { background: #f3f4f6; text-decoration: none; }
.chat-conv-item.active {
    background: #eff6ff;
    border-left-color: var(--color-primary, #2563eb);
    color: #1e40af;
}

.chat-conv-date {
    font-size: 11px;
    color: #9ca3af;
    margin-top: 2px;
}

/* === Mobile sidebar toggle === */
.chat-sidebar-toggle {
    display: none;
    position: absolute;
    top: 8px;
    left: 8px;
    z-index: 10;
    background: #fff;
    border: 1px solid var(--color-border, #e5e7eb);
    border-radius: 6px;
    padding: 6px 10px;
    font-size: 13px;
    cursor: pointer;
    min-height: 48px;
    align-items: center;
}

/* === Main chat area === */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 24px 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

/* === Welcome screen === */
.chat-welcome {
    text-align: center;
    margin: auto;
    padding: 32px;
    color: #6b7280;
}
.chat-welcome-icon { font-size: 48px; margin-bottom: 12px; }
.chat-welcome h2   { margin: 0 0 8px; font-size: 22px; color: #111827; }
.chat-welcome p    { margin: 0; font-size: 15px; }

/* === Messages === */
.chat-message {
    display: flex;
    flex-direction: column;
    max-width: 82%;
}
.chat-message-user      { align-self: flex-end;   align-items: flex-end; }
.chat-message-assistant { align-self: flex-start; align-items: flex-start; flex-direction: row; }

.chat-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    flex-shrink: 0;
    margin-right: 8px;
    margin-top: 4px;
}
.chat-message-error     { align-self: center; }

.chat-bubble {
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 15px;
    line-height: 1.6;
    white-space: pre-wrap;
    word-break: break-word;
}
.chat-message-user .chat-bubble {
    background: var(--color-primary, #2563eb);
    color: #fff;
    border-bottom-right-radius: 4px;
}
.chat-message-assistant .chat-bubble {
    background: #f3f4f6;
    color: #111827;
    border-bottom-left-radius: 4px;
}
.chat-bubble-error {
    background: #fef2f2;
    color: #b91c1c;
    border-radius: 8px;
}

/* === Sources === */
.chat-sources {
    margin-top: 6px;
    font-size: 12px;
    color: #6b7280;
    padding: 0 4px;
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    align-items: center;
}
.chat-source-link {
    display: inline-block;
    padding: 2px 8px;
    background: #e0e7ff;
    color: var(--color-primary, #2563eb);
    border-radius: 999px;
    font-size: 11px;
    font-weight: 500;
    text-decoration: none;
}
.chat-source-link:hover { background: #c7d2fe; text-decoration: none; }

/* === Thinking animation === */
.chat-dots {
    display: inline-flex;
    gap: 4px;
    align-items: center;
}
.chat-dots span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #9ca3af;
    animation: chatDotBounce 1.2s infinite;
}
.chat-dots span:nth-child(2) { animation-delay: 0.2s; }
.chat-dots span:nth-child(3) { animation-delay: 0.4s; }

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

/* === Input area === */
.chat-input-area {
    border-top: 1px solid var(--color-border, #e5e7eb);
    padding: 12px 16px;
    background: #fff;
    flex-shrink: 0;
}
.chat-input-bar {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}
.chat-textarea {
    flex: 1;
    resize: none;
    border: 1px solid var(--color-border, #e5e7eb);
    border-radius: 8px;
    padding: 10px 14px;
    font-size: 16px;
    font-family: inherit;
    line-height: 1.5;
    max-height: 120px;
    overflow-y: auto;
    outline: none;
    transition: border-color 0.15s;
}
.chat-textarea:focus {
    border-color: var(--color-primary, #2563eb);
    box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}
.chat-send-btn {
    flex-shrink: 0;
    width: 48px;
    height: 48px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    padding: 0;
    min-height: 48px;
    min-width: 48px;
}
.chat-input-actions {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-top: 8px;
    font-size: 13px;
}
.chat-status {
    color: #6b7280;
    font-style: italic;
}
.chat-loading, .chat-error-msg {
    text-align: center;
    padding: 24px;
    font-size: 14px;
    color: #6b7280;
}
.chat-error-msg { color: #b91c1c; }

/* === Chat Widget: floating button === */
.chat-widget-button {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--color-primary, #2563eb);
    color: #fff;
    border: none;
    box-shadow: 0 4px 16px rgba(37,99,235,0.4);
    font-size: 24px;
    cursor: pointer;
    z-index: 150;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, transform 0.15s;
}
.chat-widget-button:hover {
    background: var(--color-primary-dark, #1d4ed8);
    transform: scale(1.05);
}

/* === Chat Widget: popup window === */
.chat-widget-window {
    position: fixed;
    bottom: 92px;
    right: 24px;
    width: 380px;
    height: 500px;
    background: var(--color-surface, #fff);
    border: 1px solid var(--color-border, #e5e7eb);
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.18);
    z-index: 150;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chat-widget-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--color-border, #e5e7eb);
    background: #f9fafb;
    flex-shrink: 0;
    font-size: 14px;
}

.chat-widget-close {
    background: none;
    border: none;
    font-size: 1.4rem;
    cursor: pointer;
    color: var(--color-text-light, #64748b);
    line-height: 1;
    padding: 4px;
    min-width: 32px;
    min-height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
}
.chat-widget-close:hover { background: #f3f4f6; color: var(--color-text, #1e293b); }

.chat-widget-messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* Widget messages use the same chat-message classes, with smaller sizing */
.chat-widget-messages .chat-message  { max-width: 90%; }
.chat-widget-messages .chat-bubble   { font-size: 14px; padding: 8px 12px; }
.chat-widget-messages .chat-sources  { font-size: 11px; }

.chat-widget-input {
    display: flex;
    gap: 8px;
    align-items: flex-end;
    padding: 10px 12px;
    border-top: 1px solid var(--color-border, #e5e7eb);
    background: #fff;
    flex-shrink: 0;
}
.chat-widget-input textarea {
    flex: 1;
    resize: none;
    border: 1px solid var(--color-border, #e5e7eb);
    border-radius: 6px;
    padding: 8px 10px;
    font-size: 14px;
    font-family: inherit;
    line-height: 1.5;
    max-height: 80px;
    overflow-y: auto;
    outline: none;
    transition: border-color 0.15s;
}
.chat-widget-input textarea:focus {
    border-color: var(--color-primary, #2563eb);
    box-shadow: 0 0 0 2px rgba(37,99,235,0.1);
}
.chat-widget-input .btn {
    flex-shrink: 0;
    min-height: 36px;
    min-width: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 10px;
}

/* === Mobile === */
@media (max-width: 767px) {
    .chat-layout {
        margin: 0 -8px;
        height: calc(100vh - 100px);
    }

    /* Sidebar as drawer */
    .chat-sidebar {
        position: absolute;
        top: 0; left: 0; bottom: 0;
        z-index: 20;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        box-shadow: 2px 0 8px rgba(0,0,0,0.1);
    }
    .chat-sidebar.chat-sidebar-open { transform: translateX(0); }

    .chat-sidebar-toggle { display: flex; }
    .chat-messages { padding: 52px 12px 16px; }
    .chat-message  { max-width: 94%; }
    .chat-bubble   { font-size: 16px; }
    .chat-input-area { padding: 8px 10px; }
    .chat-textarea { font-size: 16px; }

    /* Widget: fullscreen overlay on mobile */
    .chat-widget-window {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 100%;
        border-radius: 0;
        border: none;
    }
    .chat-widget-button {
        bottom: 16px;
        right: 16px;
        width: 52px;
        height: 52px;
        font-size: 22px;
    }
    .chat-widget-messages .chat-bubble { font-size: 16px; }
    .chat-widget-input textarea { font-size: 16px; }
}

/* === Safe area insets for mobile browsers (notch/home indicator) === */
@supports (padding: env(safe-area-inset-bottom)) {
    .chat-input-area {
        padding-bottom: calc(12px + env(safe-area-inset-bottom));
    }
    .chat-widget-input {
        padding-bottom: calc(10px + env(safe-area-inset-bottom));
    }
    @media (max-width: 767px) {
        .chat-widget-button {
            bottom: calc(16px + env(safe-area-inset-bottom));
        }
    }
}
