:root {
    --cb-primary: #0066FF;
    --cb-primary-dark: #0044cc;
    --cb-navy: #0A1D37;
    --cb-bg: #f4f7fb;
    --cb-white: #ffffff;
    --cb-border: #e2e8f0;
    --cb-text: #1e293b;
    --cb-text-muted: #64748b;
    --cb-user-bg: #0066FF;
    --cb-bot-bg: #ffffff;
    --cb-shadow: 0 20px 60px rgba(0, 0, 0, 0.18);
    --cb-radius: 20px;
    --cb-radius-sm: 12px;
    --cb-ease: 0.35s cubic-bezier(0.4, 0, 0.2, 1);
    --cb-width: 380px;
    --cb-height: 450px;
}

/* Toggle Button */

#cb-toggle-btn {
    position: fixed;
    bottom: 30px;
    right: 30px;
    width: 62px;
    height: 62px;
    border-radius: 50%;
    background: linear-gradient(90deg, var(--cb-primary), #00B0F0);
    border: none;
    cursor: pointer;
    z-index: 9998;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 8px 30px rgba(1, 87, 152, 0.45);
    transition: transform var(--cb-user-bg), box-shadow var(--cb-ease);
    outline: none;
}

#cb-toggle-btn:hover {
    transform: scale(1.1) rotate(-5deg);
    box-shadow: 0 12px 40px rgba(0, 102, 255, 0.55);
}

#cb-toggle-btn:active {
    transform: scale(0.97);
}

#cb-toggle-btn .cb-icon-chat,
#cb-toggle-btn .cb-icon-close {
    position: absolute;
    transition: opacity var(--cb-ease), transform var(--cb-ease);
    color: #fff;
    font-size: 24px;
    line-height: 1;
}

#cb-toggle-btn .cb-icon-close {
    opacity: 0;
    transform: rotate(90deg) scale(0.6);
}

body.cb-open #cb-toggle-btn .cb-icon-chat {
    opacity: 0;
    transform: rotate(-90deg) scale(0.6);
}

body.cb-open #cb-toggle-btn .cb-icon-close {
    opacity: 1;
    transform: rotate(0deg) scale(1);
}

/* Notification Badge */

#cb-badge {
    position: absolute;
    top: 0;
    right: 0;
    width: 18px;
    height: 18px;
    background: #ff4757;
    border-radius: 50%;
    border: 2px solid #fff;
    font-size: 10px;
    font-weight: 700;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transform: scale(0);
    transition: opacity 0.3s, transform 0.3s;
}

#cb-badge.visible {
    opacity: 1;
    transform: scale(1);
}

/* Panel */

#cb-panel {
    position: fixed;
    bottom: 108px;
    right: 30px;
    width: var(--cb-width);
    height: var(--cb-height);
    background: var(--cb-white);
    border-radius: var(--cb-radius);
    box-shadow: var(--cb-shadow);
    display: flex;
    flex-direction: column;
    z-index: 9997;
    overflow: hidden;
    opacity: 0;
    transform: translateY(30px) scale(0.96);
    pointer-events: none;
    transition: opacity var(--cb-ease), transform var(--cb-ease);
    transform-origin: bottom right;
}

body.cb-open #cb-panel {
    opacity: 1;
    display: flex !important;
    visibility: visible !important;
    transform: translateY(0) scale(1);
    pointer-events: all;
}

/* Header */

#cb-header {
    background: linear-gradient(120deg, var(--cb-navy), #1a3a6e);
    padding: 18px 20px;
    display: flex;
    align-items: center;
    gap: 14px;
    flex-shrink: 0;
    position: relative;
    overflow: hidden;
}

#cb-header::before {
    content: '';
    position: absolute;
    top: -30px;
    right: -20px;
    width: 120px;
    height: 120px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 50%;
}

.cb-avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--cb-primary), #00aaff);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    color: #fff;
    flex-shrink: 0;
    box-shadow: 0 4px 12px rgba(0, 102, 255, 0.4);
    position: relative;
}

.cb-avatar::after {
    content: '';
    position: absolute;
    bottom: 2px;
    right: 2px;
    width: 10px;
    height: 10px;
    background: #22c55e;
    border-radius: 50%;
    border: 2px solid var(--cb-navy);
}

.cb-header-info {
    flex: 1;
}

.cb-header-info h4 {
    margin: 0 0 2px;
    font-size: 15px;
    font-weight: 700;
    color: #fff;
    letter-spacing: 0.2px;
}

.cb-header-info span {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.65);
    font-weight: 400;
}

#cb-clear-btn {
    background: rgba(255, 255, 255, 0.12);
    border: none;
    border-radius: 8px;
    color: rgba(255, 255, 255, 0.75);
    font-size: 13px;
    padding: 6px 10px;
    transition: background 0.2s, color 0.2s;
    display: flex;
    align-items: center;
    gap: 5px;
    flex-shrink: 0;
    pointer-events: auto !important;
    /* ← أهم حاجة */
    cursor: pointer !important;
    position: relative;
    z-index: 9999;
}

#cb-clear-btn:hover {
    background: rgba(255, 255, 255, 0.22);
    color: #fff;
}

/* Messages Area */

#cb-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--cb-bg);
    scroll-behavior: smooth;
}

#cb-messages::-webkit-scrollbar {
    width: 4px;
}

#cb-messages::-webkit-scrollbar-track {
    background: transparent;
}

#cb-messages::-webkit-scrollbar-thumb {
    background: var(--cb-border);
    border-radius: 4px;
}

#cb-messages::-webkit-scrollbar-thumb:hover {
    background: #cbd5e1;
}

/* Message Bubbles */

.cb-msg {
    display: flex;
    gap: 8px;
    align-items: flex-end;
    animation: cb-bubble-in 0.28s ease both;
}

@keyframes cb-bubble-in {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.cb-msg.bot {
    flex-direction: row;
}

.cb-msg.user {
    flex-direction: row-reverse;
}

.cb-msg-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--cb-primary), #00aaff);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    color: #fff;
    flex-shrink: 0;
    margin-bottom: 2px;
}

.cb-msg.user .cb-msg-avatar {
    display: none;
}

.cb-bubble-wrap {
    display: flex;
    flex-direction: column;
    max-width: 80%;
}

.cb-msg.user .cb-bubble-wrap {
    align-items: flex-end;
}

.cb-msg.bot .cb-bubble-wrap {
    align-items: flex-start;
}

.cb-bubble {
    padding: 11px 15px;
    border-radius: var(--cb-radius-sm);
    font-size: 14px;
    line-height: 1.6;
    word-break: break-word;
    white-space: pre-wrap;
}

.cb-msg.bot .cb-bubble {
    background: var(--cb-bot-bg);
    color: var(--cb-text);
    border: 1px solid var(--cb-border);
    border-bottom-left-radius: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.cb-msg.user .cb-bubble {
    background: linear-gradient(135deg, var(--cb-primary), #0088ff);
    color: #fff;
    border-bottom-right-radius: 4px;
    box-shadow: 0 4px 16px rgba(0, 102, 255, 0.3);
}

.cb-time {
    font-size: 10px;
    color: var(--cb-text-muted);
    margin-top: 4px;
    padding: 0 2px;
}

.cb-bubble.cb-error {
    background: #fff5f5;
    border: 1px solid #fecaca;
    color: #dc2626;
}

/* Typing Indicator */

#cb-typing {
    display: none;
    align-items: flex-end;
    gap: 8px;
}

.cb-typing-dots {
    background: var(--cb-bot-bg);
    border: 1px solid var(--cb-border);
    border-radius: var(--cb-radius-sm);
    border-bottom-left-radius: 4px;
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 5px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.cb-typing-dots span {
    width: 7px;
    height: 7px;
    background: var(--cb-primary);
    border-radius: 50%;
    display: inline-block;
    animation: cb-bounce 1.2s infinite ease-in-out;
}

.cb-typing-dots span:nth-child(1) {
    animation-delay: 0s;
}

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

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

@keyframes cb-bounce {

    0%,
    60%,
    100% {
        transform: translateY(0);
    }

    30% {
        transform: translateY(-7px);
    }
}

/* Input Bar */

#cb-input-bar {
    padding: 14px 16px;
    background: var(--cb-white);
    border-top: 1px solid var(--cb-border);
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

#cb-input {
    flex: 1;
    border: 1.5px solid var(--cb-border);
    border-radius: 25px;
    padding: 11px 18px;
    font-size: 14px;
    font-family: 'Poppins', sans-serif;
    color: var(--cb-text);
    background: var(--cb-bg);
    outline: none;
    resize: none;
    height: 44px;
    max-height: 110px;
    overflow-y: auto;
    line-height: 1.5;
    transition: border-color 0.25s, box-shadow 0.25s;
    direction: auto;
    /* يحدد الاتجاه تلقائياً بناءً على أول حرف يكتبه المستخدم */
    text-align: start;
}

#cb-input:focus {
    border-color: var(--cb-primary);
    box-shadow: 0 0 0 3px rgba(0, 102, 255, 0.12);
    background: #fff;
}

#cb-input::placeholder {
    color: #a0aec0;
}

#cb-send-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--cb-primary), #00aaff);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 16px;
    flex-shrink: 0;
    transition: transform var(--cb-ease), box-shadow var(--cb-ease), opacity 0.2s;
    box-shadow: 0 4px 12px rgba(0, 102, 255, 0.35);
}

#cb-send-btn:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 20px rgba(0, 102, 255, 0.45);
}

#cb-send-btn:active {
    transform: scale(0.95);
}

#cb-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

/* Welcome State */

#cb-welcome {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: 30px 20px 10px;
    gap: 10px;
    animation: cb-bubble-in 0.4s ease both;
}

.cb-welcome-icon {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--cb-primary), #00aaff);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    color: #fff;
    box-shadow: 0 8px 24px rgba(0, 102, 255, 0.35);
    margin-bottom: 6px;
}

#cb-welcome h5 {
    margin: 0;
    font-size: 16px;
    font-weight: 700;
    color: var(--cb-navy);
}

#cb-welcome p {
    margin: 0;
    font-size: 13px;
    color: var(--cb-text-muted);
    line-height: 1.6;
    max-width: 260px;
}

/* Suggestion Chips */

.cb-suggestions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
    margin-top: 8px;
}

.cb-chip {
    background: #fff;
    border: 1.5px solid var(--cb-primary);
    color: var(--cb-primary);
    border-radius: 20px;
    padding: 6px 14px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s, color 0.2s, transform 0.2s;
    font-family: 'Poppins', sans-serif;
}

.cb-chip:hover {
    background: var(--cb-primary);
    color: #fff;
    transform: translateY(-2px);
}

/* Responsive */

@media (max-width: 480px) {
    #cb-panel {
        width: calc(100vw - 20px);
        height: calc(100dvh - 120px);
        right: 10px;
        bottom: 100px;
        border-radius: 16px;
    }

    #cb-toggle-btn {
        right: 16px;
        bottom: 20px;
    }
}

body.cb-open #cb-panel {
    display: flex !important;
}