/* Botão flutuante */
#chatButton {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 60px;
    height: 60px;
    background: #007bff;
    border-radius: 50%;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    transition: all 0.3s ease;
}

#chatButton:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 16px rgba(0,0,0,0.4);
}

#chatButton i {
    color: white;
    font-size: 24px;
}

/* Badge de notificação */
#chatButton .badge {
    position: absolute;
    top: -5px;
    right: -5px;
    background: #dc3545;
}

/* Container do chat */
#chatContainer {
    position: fixed;
    bottom: 90px;
    right: 20px;
    width: 380px;
    max-width: calc(100vw - 40px);
    height: 550px;
    max-height: calc(100vh - 120px);
    background: white;
    border-radius: 15px;
    box-shadow: 0 5px 40px rgba(0,0,0,0.3);
    display: none;
    flex-direction: column;
    z-index: 999;
    overflow: hidden;
}

#chatContainer.active {
    display: flex;
    animation: slideUp 0.3s ease;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Header do chat */
.chat-header {
    background: linear-gradient(135deg, #007bff, #0056b3);
    color: white;
    padding: 15px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
}

.chat-header h5 {
    margin: 0;
    font-size: 16px;
}

.chat-header .btn-close {
    background: transparent;
    border: none;
    color: white;
    font-size: 20px;
    cursor: pointer;
    padding: 0;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: background 0.3s;
}

.chat-header .btn-close:hover {
    background: rgba(255,255,255,0.2);
}

/* Área de seleção */
#selectionArea {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    min-height: 0;
}

.selection-title {
    text-align: center;
    margin-bottom: 20px;
    color: #333;
}

.subject-card {
    border: 2px solid #e0e0e0;
    border-radius: 10px;
    padding: 15px;
    margin-bottom: 10px;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
}

.subject-card:hover {
    border-color: #007bff;
    background: #f8f9fa;
    transform: translateX(5px);
}

/* Foto do professor */
.teacher-photo {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    object-fit: cover;
    margin-right: 15px;
    border: 2px solid #e0e0e0;
    flex-shrink: 0;
}

.subject-card:hover .teacher-photo {
    border-color: #007bff;
}

.subject-info {
    flex: 1;
}

.subject-info h6 {
    margin: 0 0 5px 0;
    color: #333;
    font-weight: 600;
}

.subject-info small {
    color: #6c757d;
    display: flex;
    align-items: center;
}

.subject-info small i {
    font-size: 12px;
    margin-right: 5px;
}

/* Área de chat - IMPORTANTE: estrutura flex correta */
#chatArea {
    display: none;
    flex-direction: column;
    height: 100%;
    min-height: 0;
}

#chatArea.active {
    display: flex;
}

.chat-subheader {
    background: #f8f9fa;
    padding: 12px 20px;
    border-bottom: 1px solid #dee2e6;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.chat-subheader .selected-info {
    display: flex;
    align-items: center;
    flex: 1;
}

.chat-subheader .teacher-photo-small {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
    margin-right: 12px;
    border: 2px solid #007bff;
}

.chat-subheader .info-text {
    flex: 1;
}

.chat-subheader .info-text .subject-name {
    font-weight: 600;
    color: #333;
    margin: 0;
    font-size: 14px;
}

.chat-subheader small {
    color: #6c757d;
    font-size: 12px;
}

.btn-back {
    background: transparent;
    border: none;
    color: #007bff;
    cursor: pointer;
    padding: 5px 10px;
    border-radius: 5px;
    transition: background 0.3s;
    flex-shrink: 0;
}

.btn-back:hover {
    background: rgba(0, 123, 255, 0.1);
}

/* Área de mensagens - CRÍTICO: flex: 1 e min-height: 0 */
#messagesArea {
    flex: 1 1 auto;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 20px;
    background: #f5f5f5;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

/* Estrutura das mensagens */
.message {
    margin-bottom: 15px;
    display: flex;
    width: 100%;
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Mensagens do usuário - DIREITA */
.message.user {
    justify-content: flex-end;
    align-items: flex-end;
}

.message.user > div {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    max-width: 75%;
}

/* Mensagens do bot - ESQUERDA */
.message.bot {
    justify-content: flex-start;
    align-items: flex-start;
}

.message.bot > img {
    margin-right: 10px;
    flex-shrink: 0;
}

.message.bot > div {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    max-width: 75%;
}

.message.bot .teacher-photo-msg {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.message-content {
    padding: 10px 15px;
    border-radius: 15px;
    word-wrap: break-word;
    word-break: break-word;
    white-space: pre-wrap;
}

.message.user .message-content {
    background: #007bff;
    color: white;
    border-bottom-right-radius: 5px;
}

.message.bot .message-content {
    background: white;
    color: #333;
    border-bottom-left-radius: 5px;
    box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

.message-time {
    font-size: 11px;
    color: #999;
    margin-top: 5px;
}

.message.user .message-time {
    text-align: right;
}

.message.bot .message-time {
    text-align: left;
}

/* Typing indicator */
.typing-indicator {
    display: none;
    align-items: center;
    margin-bottom: 15px;
}

.typing-indicator.active {
    display: flex;
}

.typing-indicator .teacher-photo-msg {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    margin-right: 10px;
    flex-shrink: 0;
}

.typing-dots {
    padding: 10px 15px;
    background: white;
    border-radius: 15px;
    border-bottom-left-radius: 5px;
    box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

.typing-dots span {
    height: 8px;
    width: 8px;
    background: #999;
    border-radius: 50%;
    display: inline-block;
    margin: 0 2px;
    animation: typing 1.4s infinite;
}

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

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

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

/* Input area - CRÍTICO: flex-shrink: 0 para nunca sumir */
.chat-input {
    padding: 15px 20px;
    background: white;
    border-top: 1px solid #dee2e6;
    flex-shrink: 0;
    flex-grow: 0;
    position: relative;
    z-index: 10;
}

.chat-input .input-group {
    display: flex;
    gap: 10px;
    align-items: flex-end;
}

#messageInput {
    flex: 1;
    border: 1px solid #dee2e6;
    border-radius: 25px;
    padding: 10px 15px;
    outline: none;
    resize: none;
    max-height: 100px;
    min-height: 40px;
    font-family: inherit;
    font-size: 14px;
    line-height: 1.4;
}

#messageInput:focus {
    border-color: #007bff;
}

#sendButton {
    background: #007bff;
    border: none;
    color: white;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.3s;
    flex-shrink: 0;
}

#sendButton:hover:not(:disabled) {
    background: #0056b3;
}

#sendButton:disabled {
    background: #ccc;
    cursor: not-allowed;
}

#sendButton i {
    font-size: 16px;
}

/* Scrollbar customizada */
#messagesArea::-webkit-scrollbar,
#selectionArea::-webkit-scrollbar {
    width: 6px;
}

#messagesArea::-webkit-scrollbar-track,
#selectionArea::-webkit-scrollbar-track {
    background: #f1f1f1;
}

#messagesArea::-webkit-scrollbar-thumb,
#selectionArea::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 3px;
}

#messagesArea::-webkit-scrollbar-thumb:hover,
#selectionArea::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* Responsivo */
@media (max-width: 576px) {
    #chatContainer {
        width: calc(100vw - 20px);
        right: 10px;
        bottom: 80px;
        height: calc(100vh - 100px);
    }
    
    #chatButton {
        right: 10px;
        bottom: 10px;
    }
    
    .message.user > div,
    .message.bot > div {
        max-width: 85%;
    }
}
