/* ===== CHAT SECTION ===== */
.chat-section {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
    background: var(--bg-light);
    min-height: 0;
}

.dark-mode .chat-section {
    background: var(--bg-dark);
}

.messages-container {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 10px 6px 8px 0px  ;
    display: flex;
    flex-direction: column;
    gap: 10px;
    min-height: 0;
    scroll-behavior: smooth;
}

/* ===== MESSAGES ===== */
.message {
    display: flex;
    animation: slideIn 0.3s ease-out;
    align-items: flex-start;
    gap: 8px;
    width: 100%;
}

/* BOT: avatar pinned LEFT in every language (direction:ltr anchors the flex axis) */
.message.bot {
    flex-direction: row !important;
    justify-content: flex-start;
    direction: ltr;
}

/* USER: bubble pinned RIGHT in every language */
.message.user {
    flex-direction: row !important;
    justify-content: flex-end;
    direction: ltr;
}

.message-icon {
    width: 40px;
    height: 40px;
    min-width: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    flex-shrink: 0;
    order: 0;
    transition: box-shadow 0.3s ease;
}



.message-icon img {
    width: 40px;
    height: 40px;
    object-fit: cover;
    border-radius: 50%;
}

.message-bot-name {
    font-size: 11px;
    font-weight: 700;
    color: var(--secondary);
    letter-spacing: 0.04em;
    margin-bottom: 4px;
    display: block;
    text-align: start;
}

.message-bubble {
    min-width: 76px;
    display: flex;
    flex-direction: column;
    padding: 10px 14px 8px;
    border-radius: 18px;
    word-wrap: break-word;
    line-height: 1.6;
    font-size: 13.5px;
    position: relative;
}

/* ── Bot bubble ─────────────────────────────────────────────── */
.message.bot .message-content { align-items: flex-start; }

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

.message.bot .message-bubble {
    background: linear-gradient(160deg, #f4f6ff 0%, #ffffff 100%);
    color: #1a1a2e;
    border-radius: 4px 18px 18px 18px;
    border: 1px solid rgba(0, 5, 133, 0.07);
    box-shadow: 0 2px 14px rgba(0, 5, 133, 0.07), 0 1px 3px rgba(0, 5, 133, 0.04);
    font-size: 14.5px;
    line-height: 1.72;
    letter-spacing: 0.01em;
    animation: msgBubbleIn 0.22s ease-out;
}

/* WhatsApp-style tail for the first bubble from the bot */
.message.bot:not(.grouped) .message-bubble::before {
    content: '';
    position: absolute;
    top: 4px;
    left: -8px;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 0 8px 8px 0;
    border-color: transparent #f4f6ff transparent transparent;
    filter: drop-shadow(-1px 0px 1px rgba(0,5,133,0.08));
}

/* ── Markdown elements inside bot bubbles ── */
.message.bot .message-bubble strong {
    font-weight: 700;
    color: var(--primary);
}
.message.bot .message-bubble em {
    font-style: italic;
    opacity: 0.88;
}
.message.bot .message-bubble ul,
.message.bot .message-bubble ol {
    padding-inline-start: 1.4em;
    margin: 6px 0 4px;
}
.message.bot .message-bubble li {
    margin-bottom: 5px;
    line-height: 1.6;
}

.dark-mode .message.bot .message-bubble {
    background: linear-gradient(160deg, #1a2038 0%, #1e2540 100%);
    color: #dde3f8;
    box-shadow: 0 2px 16px rgba(0, 0, 0, 0.32), 0 0 0 1px rgba(255, 255, 255, 0.04);
}

.dark-mode .message.bot .message-bubble strong {
    color: #a8bcff;
}

.dark-mode .message.bot:not(.grouped) .message-bubble::before {
    border-color: transparent #1a2038 transparent transparent;
}

/* ── User bubble ─────────────────────────────────────────────── */
.message.user .message-content {
    align-items: flex-end;
    max-width: 75%;
}

.message.user .message-bubble {
    background: var(--primary);
    color: #e8eeff;
    border-radius: 18px 4px 18px 18px;
    width: fit-content;
    max-width: 100%;
    word-break: break-word;
    white-space: normal;
    box-shadow: 0 1px 4px rgba(0,5,133,0.25);
}

/* WhatsApp-style tail for the first user bubble */
.message.user:not(.grouped) .message-bubble::before {
    content: '';
    position: absolute;
    top: 4px;
    right: -8px;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 8px 8px 0 0;
    border-color: var(--primary) transparent transparent transparent;
}

.dark-mode .message.user .message-bubble {
    background: #1a3a6e;
    box-shadow: 0 1px 6px rgba(0,0,0,0.4);
}

.dark-mode .message.user:not(.grouped) .message-bubble::before {
    border-color: #1a3a6e transparent transparent transparent;
}

.message-time {
    font-size: 10px;
    opacity: 0.5;
    direction: ltr;
    letter-spacing: 0.01em;
    line-height: 1;
    white-space: nowrap;
    margin-top: 3px;
    align-self: flex-end;
}

/* Bot bubble: restore page direction so align-self resolves correctly.
   In RTL: flex-end = left (Arabic end). In LTR: flex-end = right (English end).
   .message.bot forces direction:ltr for the ROW layout; we override it here
   just for the bubble's column layout so the time lands on the right side. */
[dir="rtl"] .message.bot .message-bubble { direction: rtl; }
[dir="ltr"] .message.bot .message-bubble { direction: ltr; }

.message-content {
    display: flex;
    flex-direction: column;
    max-width: 70%;
}

/* ── Message Grouping ──────────────────────────────────────── */

.message.grouped { margin-top: -7px; }
.message.grouped .message-icon { visibility: hidden; }

.message.bot .message-bubble.bubble--first { border-radius: 4px  18px 18px 4px  !important; }
.message.bot .message-bubble.bubble--mid   { border-radius: 4px  18px 18px 4px  !important; }
.message.bot .message-bubble.bubble--last  { border-radius: 4px  18px 18px 18px !important; }

.message.user .message-bubble.bubble--first { border-radius: 18px 4px  4px  18px !important; }
.message.user .message-bubble.bubble--mid   { border-radius: 18px 4px  4px  18px !important; }
.message.user .message-bubble.bubble--last  { border-radius: 18px 4px  18px 18px !important; }

/* No tail on grouped messages (tail only on first in sequence) */
.message.grouped .message-bubble::before { display: none !important; }

/* ── Emergency / sensitive message bubble ── */
.msg-emergency {
    background: linear-gradient(135deg, #fff1f0 0%, #ffe8e6 100%) !important;
    border: 1.5px solid #ff4d4f !important;
    color: #8b1a1a !important;
    border-radius: 14px !important;
    line-height: 1.7;
}
.dark-mode .msg-emergency {
    background: linear-gradient(135deg, rgba(255,77,79,0.15) 0%, rgba(255,77,79,0.08) 100%) !important;
    border-color: rgba(255,77,79,0.5) !important;
    color: #ffb3b3 !important;
}
.msg-emergency b { color: #c0392b; }
.dark-mode .msg-emergency b { color: #ff7875; }

/* ══════════════════════════════════════════════════════════════