/* ===== دردشة الدعم بالذكاء الاصطناعي ===== */

/* 1) الزر العائم
   التدرّج هو هوية الموقع نفسها (--primary-color / --secondary-color في style.css)
   الحشو منطقي (padding-inline-start = يمين في RTL) ليبقى شعار الأيقونة ملاصقاً للحافة */
.ai-chat-fab {
    position: fixed;
    bottom: calc(18px + env(safe-area-inset-bottom, 0px));
    left: 18px;
    z-index: 1036;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    height: 56px;
    padding-inline-start: 7px;
    padding-inline-end: 20px;
    border: none;
    border-radius: 999px;
    background: linear-gradient(135deg, #7f8ff0 0%, #667eea 42%, #764ba2 100%);
    color: #fff;
    font-size: 1.15rem;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    box-shadow:
        0 14px 32px -8px rgba(102, 126, 234, 0.6),
        0 6px 14px -4px rgba(118, 75, 162, 0.4),
        inset 0 1px 0 rgba(255, 255, 255, 0.35),
        inset 0 -2px 6px rgba(76, 46, 116, 0.25);
    transition: transform 0.3s cubic-bezier(0.22, 1, 0.36, 1), box-shadow 0.3s ease, opacity 0.25s ease;
}

/* هالة نابضة خفيفة تلفت الانتباه دون إزعاج (تتوقف عند التفاعل) */
.ai-chat-fab::before {
    content: '';
    position: absolute;
    inset: -3px;
    border-radius: inherit;
    border: 2px solid rgba(102, 126, 234, 0.55);
    opacity: 0;
    pointer-events: none;
    animation: aiChatFabHalo 3.4s ease-out infinite;
}

/* لمعة زجاجية تمرّ على الزر كل ٦ ثوان — background-position لتبقى داخل الحدود */
.ai-chat-fab::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: inherit;
    background: linear-gradient(100deg, transparent 38%, rgba(255, 255, 255, 0.38) 50%, transparent 62%);
    background-size: 260% 100%;
    background-position: 210% 0;
    background-repeat: no-repeat;
    pointer-events: none;
    animation: aiChatFabSheen 6s ease-in-out infinite;
}

@keyframes aiChatFabHalo {
    0% { transform: scale(0.94); opacity: 0.5; }
    70%, 100% { transform: scale(1.14); opacity: 0; }
}

@keyframes aiChatFabSheen {
    0%, 62% { background-position: 210% 0; }
    100% { background-position: -70% 0; }
}

.ai-chat-fab:hover,
.ai-chat-fab:focus-visible {
    color: #fff;
    transform: translateY(-4px) scale(1.03);
    box-shadow:
        0 20px 40px -10px rgba(102, 126, 234, 0.7),
        0 8px 18px -4px rgba(118, 75, 162, 0.45),
        inset 0 1px 0 rgba(255, 255, 255, 0.45);
}

/* حلقة تركيز واضحة للوحة المفاتيح (لا outline افتراضي مع border-radius كبير) */
.ai-chat-fab:focus-visible {
    outline: none;
    box-shadow:
        0 0 0 3px #fff,
        0 0 0 6px rgba(102, 126, 234, 0.55),
        0 16px 34px -10px rgba(102, 126, 234, 0.6);
}

.ai-chat-fab:hover::before,
.ai-chat-fab:focus-visible::before { animation: none; opacity: 0; }

.ai-chat-fab:active { transform: translateY(-1px) scale(0.97); }

/* شارة الأيقونة: دائرة زجاجية داخل الزر */
.ai-chat-fab-icon {
    position: relative;
    z-index: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    box-shadow:
        inset 0 0 0 1px rgba(255, 255, 255, 0.3),
        inset 0 2px 6px rgba(255, 255, 255, 0.18);
    flex-shrink: 0;
    transition: transform 0.3s cubic-bezier(0.22, 1, 0.36, 1), background 0.3s ease;
}

.ai-chat-fab:hover .ai-chat-fab-icon {
    background: rgba(255, 255, 255, 0.3);
    transform: rotate(-8deg) scale(1.06);
}

/* نقطة «متاح الآن» */
.ai-chat-fab-icon::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 11px;
    height: 11px;
    border-radius: 50%;
    background: #34d399;
    box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.85), 0 0 8px rgba(52, 211, 153, 0.9);
}

.ai-chat-fab-label {
    position: relative;
    z-index: 1;
    font-size: 0.95rem;
    font-weight: 700;
    line-height: 1;
    letter-spacing: 0.2px;
    white-space: nowrap;
    text-shadow: 0 1px 2px rgba(49, 27, 82, 0.25);
}

@media (max-width: 576px) {
    .ai-chat-fab {
        bottom: calc(14px + env(safe-area-inset-bottom, 0px));
        left: 14px;
        height: 50px;
        gap: 8px;
        padding-inline-start: 6px;
        padding-inline-end: 16px;
        font-size: 1.05rem;
    }
    .ai-chat-fab-icon { width: 38px; height: 38px; }
    .ai-chat-fab-icon::after { width: 10px; height: 10px; }
    .ai-chat-fab-label { font-size: 0.85rem; }
}

/* على الشاشات الضيقة جداً: زر دائري بالأيقونة فقط لئلا يزحم المحتوى */
@media (max-width: 359px) {
    .ai-chat-fab { padding-inline-end: 6px; }
    .ai-chat-fab-label { display: none; }
}

/* احترام تفضيل تقليل الحركة */
@media (prefers-reduced-motion: reduce) {
    .ai-chat-fab { transition: none; }
    .ai-chat-fab::before { animation: none; opacity: 0; }
    .ai-chat-fab::after { animation: none; }
}

body.ai-chat-open .ai-chat-fab {
    opacity: 0;
    transform: translateY(8px) scale(0.9);
    pointer-events: none;
}

/* قفل تمرير الصفحة الخلفية والنافذة مفتوحة على الجوال: بدونه يمرّر المتصفح
   الصفحة لإظهار حقل الكتابة عند فتح لوحة المفاتيح، فتدخل النافذة في حلقة
   تغذية راجعة مع support-chat.js (اهتزاز وتجميد) — لا تحذفه */
@media (max-width: 576px) {
    body.ai-chat-open { overflow: hidden; }
}

@media print { .ai-chat-fab, .ai-chat-panel { display: none !important; } }

/* 2) نافذة الدردشة */
.ai-chat-panel[hidden] { display: none !important; }

.ai-chat-panel {
    position: fixed;
    bottom: calc(18px + env(safe-area-inset-bottom, 0px));
    left: 18px;
    z-index: 10500;
    display: flex;
    flex-direction: column;
    width: min(380px, calc(100vw - 36px));
    height: min(580px, calc(100vh - 90px));
    background: #fff;
    border-radius: 22px;
    border: 1px solid rgba(102, 126, 234, 0.14);
    box-shadow:
        0 24px 60px -12px rgba(49, 27, 82, 0.35),
        0 10px 24px -8px rgba(102, 126, 234, 0.25);
    overflow: hidden;
    direction: rtl;
    opacity: 0;
    transform: translateY(18px) scale(0.96);
    transform-origin: bottom left;
    transition: transform 0.3s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.25s ease;
}

.ai-chat-panel.is-open {
    opacity: 1;
    transform: translateY(0) scale(1);
}

@media (max-width: 576px) {
    .ai-chat-panel {
        left: 0;
        right: 0;
        bottom: 0;
        width: 100vw;
        border-radius: 22px 22px 0 0;
        border-bottom: none;
        /* dvh إلزامي هنا: support-chat.js يضبط interactive-widget=resizes-content
           أثناء فتح المحادثة، فيتقلّص الـ layout viewport عند ظهور لوحة المفاتيح
           وتتبعه dvh تلقائياً ⇒ النافذة تجلس فوق اللوحة بلا حساب في JS.
           سطر vh قبله احتياط للمتصفحات القديمة (لا تعرف dvh فتتجاهل السطر) */
        height: min(88vh, 640px);
        height: min(88dvh, 640px);
    }
}

/* لوحة المفاتيح مفتوحة ⇒ الـ layout viewport قصير: استخدم كل المساحة المتاحة
   فوقها ليبقى الرأس والإدخال ظاهرين وتتقلّص منطقة الرسائل وحدها */
@media (max-width: 576px) and (max-height: 520px) {
    .ai-chat-panel { height: 100dvh; }
}

@media (prefers-reduced-motion: reduce) {
    .ai-chat-panel { transition: opacity 0.2s ease; transform: none; }
    .ai-chat-panel.is-open { transform: none; }
}

/* 3) الرأس — تدرّج الهوية + توهّج ناعم أعلى النافذة */
.ai-chat-head {
    position: relative;
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 14px 16px;
    background: linear-gradient(135deg, #7f8ff0 0%, #667eea 45%, #764ba2 100%);
    color: #fff;
    flex-shrink: 0;
}

/* توهّج زجاجي خفيف يمنح الرأس عمقاً */
.ai-chat-head::before {
    content: '';
    position: absolute;
    inset: 0;
    background:
        radial-gradient(120% 160% at 85% -40%, rgba(255, 255, 255, 0.28) 0%, transparent 55%),
        linear-gradient(180deg, rgba(255, 255, 255, 0.08) 0%, transparent 60%);
    pointer-events: none;
}

.ai-chat-head > * { position: relative; }

/* شارة المساعد: حلقة زجاجية + نقطة «متاح» */
.ai-chat-head-icon {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    border-radius: 14px;
    background: rgba(255, 255, 255, 0.18);
    box-shadow:
        inset 0 0 0 1px rgba(255, 255, 255, 0.32),
        inset 0 2px 6px rgba(255, 255, 255, 0.15),
        0 4px 10px -4px rgba(49, 27, 82, 0.4);
    font-size: 1.15rem;
    flex-shrink: 0;
}

.ai-chat-head-icon::after {
    content: '';
    position: absolute;
    bottom: -2px;
    left: -2px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: #34d399;
    box-shadow: 0 0 0 2px rgba(118, 75, 162, 0.9), 0 0 8px rgba(52, 211, 153, 0.9);
}

.ai-chat-heading {
    display: flex;
    flex-direction: column;
    gap: 3px;
    flex: 1;
    min-width: 0;
}

.ai-chat-title {
    font-size: 1rem;
    font-weight: 800;
    line-height: 1.2;
    letter-spacing: 0.2px;
    text-shadow: 0 1px 2px rgba(49, 27, 82, 0.25);
}

.ai-chat-status {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 0.72rem;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.92);
}

.ai-chat-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #4ade80;
    box-shadow: 0 0 6px rgba(74, 222, 128, 0.9);
    animation: aiChatDotPulse 2.4s ease-in-out infinite;
}

@keyframes aiChatDotPulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(74, 222, 128, 0.7); }
    50% { box-shadow: 0 0 0 4px rgba(74, 222, 128, 0); }
}

@media (prefers-reduced-motion: reduce) {
    .ai-chat-dot { animation: none; }
}

.ai-chat-tool {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border: 1px solid rgba(255, 255, 255, 0.18);
    border-radius: 11px;
    background: rgba(255, 255, 255, 0.12);
    color: #fff;
    font-size: 0.85rem;
    cursor: pointer;
    transition: background 0.2s ease, transform 0.2s ease;
    flex-shrink: 0;
}

.ai-chat-tool:hover { background: rgba(255, 255, 255, 0.26); transform: translateY(-1px); }
.ai-chat-tool:active { transform: translateY(0); }

/* 4) الرسائل — خلفية فاتحة بلمسة بنفسجية تربطها بهوية الرأس */
.ai-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px 14px;
    background:
        radial-gradient(140% 60% at 50% 0%, rgba(102, 126, 234, 0.07) 0%, transparent 65%),
        #f4f5fb;
    display: flex;
    flex-direction: column;
    gap: 10px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    scrollbar-color: rgba(102, 126, 234, 0.35) transparent;
}

/* يمنع تسلسل التمرير إلى الصفحة خلف النافذة (سبب ارتجاج على الجوال) */
.ai-chat-messages { overscroll-behavior: contain; }

.ai-chat-messages::-webkit-scrollbar { width: 5px; }
.ai-chat-messages::-webkit-scrollbar-track { background: transparent; }
.ai-chat-messages::-webkit-scrollbar-thumb {
    background: rgba(102, 126, 234, 0.3);
    border-radius: 999px;
}

.ai-chat-msg {
    max-width: 84%;
    padding: 10px 14px;
    border-radius: 16px;
    font-size: 0.87rem;
    line-height: 1.65;
    white-space: pre-wrap;
    word-wrap: break-word;
    animation: aiChatMsgIn 0.28s cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes aiChatMsgIn {
    from { opacity: 0; transform: translateY(8px) scale(0.98); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

@media (prefers-reduced-motion: reduce) {
    .ai-chat-msg { animation: none; }
}

.ai-chat-msg--bot {
    align-self: flex-start;
    background: #fff;
    color: #1e293b;
    border: 1px solid rgba(102, 126, 234, 0.12);
    border-bottom-right-radius: 5px;
    box-shadow: 0 2px 8px -2px rgba(49, 27, 82, 0.08);
}

.ai-chat-msg--user {
    align-self: flex-end;
    background: linear-gradient(135deg, #7f8ff0 0%, #667eea 45%, #764ba2 100%);
    color: #fff;
    border-bottom-left-radius: 5px;
    box-shadow:
        0 6px 14px -6px rgba(102, 126, 234, 0.55),
        inset 0 1px 0 rgba(255, 255, 255, 0.22);
}

.ai-chat-msg--error {
    align-self: flex-start;
    background: #fef2f2;
    color: #b91c1c;
    border: 1px solid #fecaca;
    border-bottom-right-radius: 5px;
}

/* مؤشر الكتابة */
.ai-chat-typing {
    align-self: flex-start;
    display: inline-flex;
    gap: 5px;
    padding: 13px 17px;
    background: #fff;
    border: 1px solid rgba(102, 126, 234, 0.12);
    border-radius: 16px;
    border-bottom-right-radius: 5px;
    box-shadow: 0 2px 8px -2px rgba(49, 27, 82, 0.08);
}

.ai-chat-typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #94a3b8;
    animation: aiChatBounce 1.2s infinite ease-in-out;
}

.ai-chat-typing span:nth-child(2) { animation-delay: 0.15s; }
.ai-chat-typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes aiChatBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
    30% { transform: translateY(-5px); opacity: 1; }
}

/* 4.5) أزرار الموافقة على التحويل للدعم المباشر (لا تحويل تلقائي) */
.ai-chat-consent {
    align-self: stretch;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 2px 2px 4px;
}

.ai-chat-consent-btn {
    flex: 1 1 45%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 12px;
    border-radius: 12px;
    border: 1px solid #cbd5e1;
    background: #fff;
    color: #334155;
    font-size: 0.8rem;
    font-weight: 700;
    font-family: inherit;
    cursor: pointer;
    box-shadow: 0 1px 3px rgba(15, 23, 42, 0.06);
    transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.ai-chat-consent-btn:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(15, 23, 42, 0.12); }
.ai-chat-consent-btn:active { transform: translateY(0); }

.ai-chat-consent-btn--yes {
    border-color: #86efac;
    background: #f0fdf4;
    color: #15803d;
}

.ai-chat-consent-btn--no { color: #475569; }

/* 5) كتلة التصعيد لواتساب */
.ai-chat-wa-block {
    align-self: stretch;
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 4px 2px;
}

/* سطر توضيحي فوق الأزرار — نصه في support-chat.js (appendWaBlock) */
.ai-chat-wa-hint {
    font-size: 0.74rem;
    line-height: 1.5;
    color: #0f766e;
    background: #f0fdfa;
    border: 1px dashed #99f6e4;
    border-radius: 10px;
    padding: 6px 10px;
}

.ai-chat-wa {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 11px 13px;
    background: #fff;
    border: 1px solid #d1fae5;
    border-radius: 14px;
    text-decoration: none;
    color: #1e293b;
    box-shadow: 0 1px 3px rgba(15, 23, 42, 0.05);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.ai-chat-wa:hover {
    color: #1e293b;
    text-decoration: none;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(18, 140, 126, 0.18);
}

.ai-chat-wa-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: linear-gradient(135deg, #25d366 0%, #128c7e 100%);
    color: #fff;
    font-size: 1.1rem;
    flex-shrink: 0;
}

.ai-chat-wa-info {
    display: flex;
    flex-direction: column;
    gap: 1px;
    flex: 1;
    min-width: 0;
}

.ai-chat-wa-name {
    font-size: 0.85rem;
    font-weight: 700;
}

.ai-chat-wa-note {
    font-size: 0.72rem;
    color: #64748b;
}

.ai-chat-wa-go {
    color: #94a3b8;
    font-size: 0.8rem;
    flex-shrink: 0;
}

/* 6) التذييل والإدخال */
.ai-chat-foot {
    flex-shrink: 0;
    padding: 12px 14px calc(12px + env(safe-area-inset-bottom, 0px));
    background: #fff;
    border-top: 1px solid rgba(102, 126, 234, 0.12);
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* لافتة التسليم للدعم المباشر (بعد التصعيد) */
.ai-chat-handover[hidden] { display: none !important; }

.ai-chat-handover {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 11px;
    border: 1px solid #86efac;
    border-radius: 10px;
    background: #f0fdf4;
    color: #15803d;
    font-size: 0.78rem;
    font-weight: 600;
    line-height: 1.5;
}

.ai-chat-handover i { font-size: 0.95rem; flex-shrink: 0; }

.ai-chat-ticket {
    display: inline-block;
    margin-inline-start: 4px;
    padding: 1px 7px;
    border-radius: 6px;
    background: #dcfce7;
    font-family: monospace;
    font-size: 0.76rem;
}

/* حقل الإدخال والزر داخل كبسولة واحدة أنيقة */
.ai-chat-form {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    padding: 5px;
    border: 1.5px solid #e2e8f0;
    border-radius: 24px;
    background: #f8fafc;
    transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
}

.ai-chat-form:focus-within {
    border-color: #667eea;
    background: #fff;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.14);
}

.ai-chat-input {
    flex: 1;
    min-height: 38px;
    max-height: 110px;
    padding: 9px 14px;
    border: none;
    border-radius: 999px;
    background: transparent;
    font-size: 0.87rem;
    font-family: inherit;
    line-height: 1.5;
    resize: none;
    outline: none;
}

.ai-chat-input::placeholder { color: #94a3b8; }

.ai-chat-send {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border: none;
    border-radius: 50%;
    background: linear-gradient(135deg, #7f8ff0 0%, #667eea 45%, #764ba2 100%);
    color: #fff;
    font-size: 0.9rem;
    cursor: pointer;
    flex-shrink: 0;
    box-shadow:
        0 4px 10px -3px rgba(102, 126, 234, 0.55),
        inset 0 1px 0 rgba(255, 255, 255, 0.3);
    transition: opacity 0.2s ease, transform 0.2s ease, box-shadow 0.2s ease;
}

.ai-chat-send:hover {
    transform: scale(1.08);
    box-shadow:
        0 6px 14px -3px rgba(102, 126, 234, 0.65),
        inset 0 1px 0 rgba(255, 255, 255, 0.35);
}
.ai-chat-send:active { transform: scale(0.96); }
.ai-chat-send:disabled { opacity: 0.5; cursor: not-allowed; transform: none; box-shadow: none; }

/* أيقونة الطائرة تشير لليسار في RTL */
.ai-chat-send i { transform: scaleX(-1); }
