.chat-toggle-btn {
    position: fixed;
    bottom: 20px;
    left: 20px;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: var(--chat-icone);
    color: white;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    box-shadow: var(--shadow-md);
    z-index: 1000;
    transition: all 0.3s;
    opacity: 0.87;
    /* position: fixed est suffisant pour le badge absolu */
}

.chat-badge {
    position: absolute;
    top: 5px;
    right: 5px;
    width: 12px;
    height: 12px;
    background-color: #ff3b30;
    /* Rouge notification */
    border: 2px solid white;
    border-radius: 50%;
    z-index: 1001;
}

.chat-badge.hidden {
    display: none;
}

.chat-toggle-btn.hidden {
    transform: scale(0);
    opacity: 0;
    pointer-events: none;
}

.chat-toggle-btn:hover {
    transform: scale(1.1);
    opacity: 1;
}

.chat-widget {
    position: fixed;
    bottom: 20px;
    left: 20px;
    width: 350px;
    height: 75vh;
    min-height: 450px;
    max-height: 800px;
    background: var(--card-bg);
    border-radius: var(--radius-md);
    box-shadow: 0 5px 25px rgba(0, 0, 0, 0.2);
    display: flex;
    flex-direction: column;
    z-index: 99999;
    overflow: hidden;
    /* Restore for rounding and layout stability */
    transition: all 0.3s ease;
    border: 1px solid rgba(0, 0, 0, 0.1);
}

@media (max-width: 480px) {
    .chat-widget {
        width: 100% !important;
        height: 100% !important;
        bottom: 0 !important;
        left: 0 !important;
        border-radius: 0 !important;
        z-index: 99999;
    }
}

.chat-widget.hidden {
    display: none;
}

.chat-widget.minimized {
    transform: translateY(100%);
    opacity: 0;
    pointer-events: none;
}

.chat-body {
    position: relative;
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* Prevent body overflow, keep scroll in messages */
}

.chat-scroll-bottom {
    position: absolute;
    bottom: 15px;
    right: 15px;
    background: var(--chat-icone);
    color: white;
    border: none;
    width: 35px;
    height: 35px;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    z-index: 1005;
    transition: all 0.3s ease;
    opacity: 0.4;
    border: 1px solid rgba(255, 255, 255, 0.4);
}

.chat-scroll-bottom:hover {
    opacity: 0.8;
    transform: translateY(-3px);
    background: var(--chat-frame);
    box-shadow: 0 6px 15px rgba(0, 0, 0, 0.4);
}

.chat-scroll-bottom.hidden {
    display: none;
}

.chat-header {
    background: var(--chat-frame);
    color: white;
    padding: 0.8rem 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: bold;
}

.chat-messages {
    flex: 1;
    padding: 0.3rem;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    background: #f9f9f9;
}

[data-theme="dark"] .chat-messages {
    background: #252525;
}

.chat-empty {
    text-align: center;
    color: var(--text-secondary);
    margin-top: 2rem;
    font-size: 0.9rem;
}

.message-item {
    max-width: 85%;
    padding: 0.6rem 0.8rem;
    border-radius: 12px;
    font-size: 0.9rem;
    position: relative;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);

}

.message-item.received {
    align-self: flex-start;
    background: white;
    color: var(--text-primary);
    border-bottom-right-radius: 2px;
}

[data-theme="dark"] .message-item.received {
    background: #333;
}

.message-item.sent {
    align-self: flex-end;
    background: var(--chat-icone);
    color: white;
    border-bottom-left-radius: 2px;
}

.msg-author {
    font-size: 0.7rem;
    font-weight: bold;
    display: block;
    margin-bottom: 2px;
    opacity: 0.8;
}

.message-item.system-message {
    align-self: center !important;
    max-width: 95% !important;
    width: 92% !important;
    background: rgba(243, 225, 107, 0.1);
    border: 1px dashed var(--accent-color);
    color: var(--text-primary);
    padding: 1.2rem 1rem !important;
    border-radius: 12px;
    font-size: 0.9rem !important;
    text-align: center;
    margin: 12px auto !important;
    display: block !important;
    min-height: fit-content !important;
    box-shadow: none;
    font-weight: bold !important;
    line-height: 1.5 !important;
}

.message-item.system-message.khatma-congrats {
    background: linear-gradient(135deg, rgba(255, 215, 0, 0.12) 0%, rgba(16, 185, 129, 0.12) 100%) !important;
    border: 2px solid var(--accent-color) !important;
    border-style: double !important;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05) !important;
    position: relative;
    overflow: visible !important;
}

.message-item.system-message.khatma-congrats::before {
    content: '⭐';
    position: absolute;
    top: -5px;
    right: -5px;
    font-size: 1.2rem;
    opacity: 0.3;
}

.message-item.system-message.khatma-congrats .system-msg-content {
    font-weight: 600;
    line-height: 1.6;
}

.alert-message {
    background: rgba(230, 126, 34, 0.1) !important;
    border: 1px solid var(--warning-color) !important;
}

[data-theme="dark"] .message-item.system-message {
    background: rgba(243, 225, 107, 0.05);
    color: #eee;
}

.msg-wird-link {
    text-decoration: underline dashed rgba(255, 255, 255, 0.5);
    transition: all 0.2s ease;
    display: inline-block;
}

.message-item.sent .msg-wird-link:hover {
    text-decoration: underline solid white;
    transform: scale(1.02);
}

.message-item.received .msg-wird-link {
    text-decoration-color: rgba(0, 0, 0, 0.2);
    color: var(--primary-color);
    font-weight: 500;
}

.message-item.received .msg-wird-link:hover {
    text-decoration-color: var(--primary-color);
    filter: brightness(0.8);
}

.chat-input-area {
    padding: 0.8rem;
    background: var(--card-bg);
    border-top: 1px solid rgba(0, 0, 0, 0.05);
    position: relative;
    /* Essential for z-index stacking */
    z-index: 100;
    /* Ensure this area (and its dropdowns) is ABOVE chat-body */
    overflow: visible;
    /* Allow categories to pop out and overlap chat-body */
}

.chat-predefined-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    /* Un peu plus d'espace entre les éléments */
    width: 100%;
    margin-bottom: 15px;
    /* Espace avant la zone de texte du chat */
}

.chat-cat-dropdown {
    position: relative;
    width: 100%;
}

/* --- CHAT TYPOGRAPHY FIX --- */
.chat-widget,
.chat-widget button,
.chat-widget input,
.chat-widget .message-item,
.chat-cat-trigger,
.chat-cat-item,
.chat-cat-menu,
.msg-content,
.system-msg-content {
    font-family: 'Tajawal', sans-serif !important;
}

.chat-cat-trigger {
    background: var(--chat-icone);
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.2);
    padding: 10px 15px;
    border-radius: 20px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    width: 100%;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.chat-cat-trigger:after {
    content: '\f107';
    /* FontAwesome down arrow */
    font-family: 'Font Awesome 6 Free';
    font-weight: 900;
    font-size: 0.8rem;
    transition: transform 0.3s ease;
}

.chat-cat-dropdown.active .chat-cat-trigger:after {
    transform: rotate(180deg);
}

.chat-cat-trigger.disabled {
    opacity: 0.5;
    cursor: wait;
    filter: grayscale(1);
}

.chat-cat-trigger:hover:not(.disabled) {
    background-color: var(--chat-frame);
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.chat-cat-menu {
    position: absolute;
    bottom: calc(100% + 5px);
    left: 0;
    width: 100%;
    /* Match button width */
    min-width: 180px;
    background: var(--card-bg);
    border-radius: 12px;
    box-shadow: 0 15px 40px rgba(0, 0, 0, 0.3);
    padding: 8px;
    list-style: none;
    margin: 0;
    z-index: 100000;
    opacity: 0;
    visibility: hidden;
    transform: translateY(10px);
    transition: all 0.25s cubic-bezier(0.165, 0.84, 0.44, 1);
    border: 1px solid rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(10px);
    /* Premium feel */
}

.chat-cat-dropdown.active .chat-cat-menu {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

[data-theme="dark"] .chat-cat-menu {
    border-color: rgba(255, 255, 255, 0.1);
    background: rgba(45, 45, 45, 0.95);
}

.chat-cat-item {
    padding: 12px 14px;
    border-radius: 8px;
    font-size: 0.9rem;
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.2s;
    white-space: normal;
    text-align: right;
    line-height: 1.4;
    border-bottom: 1px solid rgba(0, 0, 0, 0.03);
}

.chat-cat-item:last-child {
    border-bottom: none;
}

.chat-cat-item:hover {
    background: var(--primary-color);
    color: white;
    transform: translateX(-5px);
}

.btn-close {
    background: none;
    border: none;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
    line-height: 1;
}

/* --- CHAT SYSTEM MESSAGES & REACTIONS --- */
/* --- CHAT SYSTEM MESSAGES & REACTIONS REMOVED DUPLICATE --- */
.message-item.system-message {
    /* Merged into main chat-message section above */
}

/* Cleaned up older conflicting styles */

.reaction-btn {
    background: var(--card-bg);
    border: 1px solid #ddd;
    border-radius: 20px;
    padding: 4px 12px;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    display: flex;
    align-items: center;
    gap: 5px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
}

.reaction-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.reaction-btn.reacted {
    background: #fff3cd;
    border-color: var(--accent-color);
    color: #856404;
}

.reaction-btn.pop-anim {
    animation: pop 0.3s ease;
}

@keyframes pop {
    0% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.3);
    }

    100% {
        transform: scale(1);
    }
}

[data-theme='dark'] .reaction-btn {
    background: #444;
    border-color: #555;
    color: #eee;
}

[data-theme='dark'] .reaction-btn.reacted {
    background: rgba(212, 175, 55, 0.2);
    border-color: var(--accent-color);
    color: var(--accent-color);
}


.msg-time {
    font-size: 0.65rem;
    opacity: 0.5;
    margin-top: 0rem;
    text-align: right;
    display: block;
}


.reaction-display {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 20px;
    padding: 4px 12px;
    font-size: 0.85rem;
    display: flex;
    align-items: center;
    gap: 5px;
    opacity: 0.8;
}

.flying-crown {
    position: fixed;
    font-size: 2rem;
    pointer-events: none;
    z-index: 999999;
    animation: float-crown 2s ease-out forwards;
}

@keyframes float-crown {
    0% {
        transform: translate(0, 0) scale(0.5) rotate(0deg);
        opacity: 0;
    }

    10% {
        opacity: 1;
        transform: translate(0, -20px) scale(1.2) rotate(10deg);
    }

    100% {
        transform: var(--target-transform);
        opacity: 0;
    }
}