/* TocaYAvanza v2 — Chat Module (Asistente IA) */

/* ===================== FAB BUTTON ===================== */
#chat-fab {
    position: fixed;
    bottom: var(--space-6);
    right: var(--space-6);
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--primary);
    color: white;
    border: none;
    cursor: pointer;
    font-size: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 16px rgba(109,101,173,0.4);
    z-index: 1000;
    transition: transform 0.2s, box-shadow 0.2s;
}
#chat-fab:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 24px rgba(109,101,173,0.5);
}

/* ===================== CHAT PANEL ===================== */
#chat-panel {
    position: fixed;
    right: 0;
    bottom: 0;
    width: 380px;
    height: 70vh;
    max-height: 600px;
    background: white;
    border-radius: var(--radius-xl) 0 0 0;
    box-shadow: -4px 0 32px rgba(0,0,0,0.15);
    z-index: 1001;
    display: flex;
    flex-direction: column;
    transform: translateX(100%);
    transition: transform 0.3s cubic-bezier(.4,0,.2,1);
    overflow: hidden;
}
#chat-panel.open {
    transform: translateX(0);
}

/* ===================== HEADER ===================== */
.chat-header {
    background: var(--primary);
    color: white;
    padding: var(--space-3) var(--space-4);
    display: flex;
    align-items: center;
    gap: var(--space-2);
    flex-shrink: 0;
}
.chat-header-title {
    flex: 1;
    font-size: var(--text-md);
    font-weight: var(--fw-bold);
}
.chat-header-btn {
    background: none;
    border: none;
    color: rgba(255,255,255,0.85);
    cursor: pointer;
    font-size: 18px;
    padding: var(--space-1);
    border-radius: var(--radius-sm);
    line-height: 1;
}
.chat-header-btn:hover {
    color: white;
    background: rgba(255,255,255,0.15);
}

/* ===================== MESSAGES AREA ===================== */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-4);
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
}

/* ===================== MESSAGE BUBBLES ===================== */
.chat-msg {
    max-width: 85%;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-xl);
    font-size: var(--text-base);
    line-height: 1.5;
    word-wrap: break-word;
    overflow-wrap: break-word;
}
.chat-msg-user {
    background: #EDE9FE;
    color: #3B1F8E;
    align-self: flex-end;
    border-bottom-right-radius: var(--radius-sm);
    white-space: pre-wrap;
}
.chat-msg-assistant {
    background: #F0F0F0;
    color: var(--text);
    align-self: flex-start;
    border-bottom-left-radius: var(--radius-sm);
}

/* Basic markdown in assistant messages */
.chat-msg-assistant strong { font-weight: var(--fw-bold); }
.chat-msg-assistant ul, .chat-msg-assistant ol {
    margin: var(--space-1) 0 var(--space-1) var(--space-5);
    padding: 0;
}
.chat-msg-assistant li { margin: 2px 0; }
.chat-msg-assistant p { margin: var(--space-1) 0; }
.chat-msg-assistant p:first-child { margin-top: 0; }
.chat-msg-assistant p:last-child { margin-bottom: 0; }

/* ===================== FEEDBACK ===================== */
.chat-feedback {
    display: flex;
    gap: 6px;
    margin-top: 6px;
    opacity: 0;
    transition: opacity 0.2s;
}
.chat-msg-assistant:hover .chat-feedback {
    opacity: 1;
}
.chat-feedback-btn {
    background: none;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 2px var(--space-2);
    cursor: pointer;
    font-size: var(--text-base);
    line-height: 1;
    transition: background 0.15s;
}
.chat-feedback-btn:hover {
    background: var(--bg);
}
.chat-feedback-done {
    font-size: var(--text-xs);
    color: var(--text-subtle);
}

/* ===================== ERROR MESSAGE ===================== */
.chat-msg-error {
    background: var(--danger-subtle);
    color: #991B1B;
    align-self: flex-start;
    border-bottom-left-radius: var(--radius-sm);
    font-size: var(--text-sm);
}

/* ===================== TYPING INDICATOR ===================== */
.chat-typing {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    padding: var(--space-2) var(--space-3);
    align-self: flex-start;
}
.chat-typing-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-subtle);
    animation: chatBounce 1.4s ease-in-out infinite;
}
.chat-typing-dot:nth-child(2) { animation-delay: 0.2s; }
.chat-typing-dot:nth-child(3) { animation-delay: 0.4s; }

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

/* ===================== INPUT AREA ===================== */
.chat-input-area {
    padding: var(--space-3);
    border-top: 1px solid var(--border);
    display: flex;
    gap: var(--space-2);
    align-items: flex-end;
    flex-shrink: 0;
    background: white;
}
.chat-input-area textarea {
    flex: 1;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-lg);
    padding: var(--space-2) var(--space-3);
    font-size: var(--text-base);
    font-family: inherit;
    resize: none;
    max-height: 100px;
    line-height: 1.4;
    overflow-y: auto;
}
.chat-input-area textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(109,101,173,0.15);
}
.chat-send-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--primary);
    color: white;
    border: none;
    cursor: pointer;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s;
}
.chat-send-btn:hover { background: var(--primary-dark); }
.chat-send-btn:disabled { background: var(--border-strong); cursor: not-allowed; }

/* ===================== EMPTY STATE ===================== */
.chat-empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-subtle);
    text-align: center;
    padding: var(--space-6);
    gap: var(--space-2);
}
.chat-empty-icon { font-size: 40px; }
.chat-empty-text { font-size: var(--text-base); line-height: 1.5; }

/* ===================== STANDALONE MODE ===================== */
#chat-panel.standalone {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    max-height: 100%;
    border-radius: 0;
    transform: none !important;
    box-shadow: none;
}

/* ===================== KB SAVE OVERLAY ===================== */
.chat-kb-btn { margin-left: var(--space-1); }
.chat-kb-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
    border-radius: inherit;
}
.chat-kb-modal {
    background: white;
    border-radius: var(--radius-lg);
    padding: var(--space-4);
    margin: var(--space-3);
    width: calc(100% - 24px);
    max-height: 80%;
    overflow-y: auto;
}
.chat-kb-input {
    width: 100%;
    padding: 6px var(--space-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: var(--text-sm);
    font-family: inherit;
    box-sizing: border-box;
}
.chat-kb-input:focus {
    outline: none;
    border-color: var(--primary);
}
.chat-kb-cancel {
    background: var(--bg);
    border: none;
    padding: 6px var(--space-3);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: var(--text-sm);
}
.chat-kb-cancel:hover { background: var(--border); }
.chat-kb-save {
    background: var(--primary);
    color: white;
    border: none;
    padding: 6px var(--space-3);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: var(--text-sm);
}
.chat-kb-save:hover { background: var(--primary-dark); }

/* ===================== RESPONSIVE ===================== */
@media (max-width: 768px) {
    #chat-panel {
        width: 100%;
        height: 100%;
        max-height: 100%;
        border-radius: 0;
    }
    #chat-fab {
        bottom: 140px; /* Above mobile #fab and bottom-nav */
        right: var(--space-4);
        width: 48px;
        height: 48px;
        font-size: 20px;
    }
}
