/* ============================================================
   خوارزميات — مساعد الموقع الذكي (أيقونة عائمة + نافذة محادثة)
   بهوية «نسيج المعرفة»: أخضر مهيمن · كحلي · برتقالي
   ============================================================ */

.khw-bot {
    --kb-green: #2eb97e;
    --kb-green-d: #157f52;
    --kb-navy: #16324f;
    --kb-deep: #06231a;
    position: fixed;
    bottom: 26px;
    left: 26px;   /* الجهة اليسرى — وزر العودة للأعلى يبقى يميناً بلا ازدحام */
    z-index: 9998;
    direction: rtl;
    font-family: "Tajawal", sans-serif;
}

/* ---------- الأيقونة العائمة ---------- */
.khw-bot__fab {
    position: relative;
    width: 62px;
    height: 62px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 24px;
    background: linear-gradient(145deg, #35d18f 0%, var(--kb-green) 45%, var(--kb-green-d) 100%);
    box-shadow: 0 10px 22px -6px rgba(46, 185, 126, 0.65), 0 4px 10px rgba(6, 35, 26, 0.25);
    transition: transform 0.35s cubic-bezier(0.22, 1, 0.36, 1), box-shadow 0.35s ease;
}

.khw-bot__fab:hover { transform: translateY(-3px) scale(1.05); box-shadow: 0 18px 34px -8px rgba(46, 185, 126, 0.8); }
.khw-bot__fab:active { transform: scale(0.97); }

.khw-bot__fab-icon {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.3s ease, transform 0.35s cubic-bezier(0.22, 1, 0.36, 1);
}

/* أيقونة البوت المرسومة — نفرض اللون صراحةً لأن وراثة currentColor مقطوعة عالمياً */
.khw-bot__svg { width: 30px; height: 30px; display: block; }

.khw-bot .khw-bot__fab .khw-bot__svg { color: #fff; stroke: #fff; }
.khw-bot .khw-bot__fab .khw-bot__svg circle[fill],
.khw-bot .khw-bot__fab .khw-bot__svg [fill="currentColor"] { fill: #fff; }

.khw-bot .khw-bot__avatar .khw-bot__svg {
    width: 24px;
    height: 24px;
    color: var(--kb-deep);
    stroke: var(--kb-deep);
}

.khw-bot .khw-bot__avatar .khw-bot__svg circle[fill],
.khw-bot .khw-bot__avatar .khw-bot__svg [fill="currentColor"] { fill: var(--kb-deep); }

/* شارة الاسم بجانب الأيقونة */
.khw-bot__tag {
    position: absolute;
    left: calc(100% + 10px);   /* تظهر لليمين لأن البوت صار على اليسار */
    top: 50%;
    transform: translateY(-50%) translateX(-6px);
    font-family: "Tajawal", sans-serif;
    font-size: 13px;
    font-weight: 800;
    color: var(--kb-deep);
    background: #fff;
    border: 1px solid rgba(46, 185, 126, 0.35);
    border-radius: 999px;
    padding: 7px 14px;
    white-space: nowrap;
    box-shadow: 0 8px 20px -10px rgba(6, 35, 26, 0.5);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease, transform 0.35s cubic-bezier(0.22, 1, 0.36, 1);
}

.khw-bot__fab:hover .khw-bot__tag { opacity: 1; transform: translateY(-50%) translateX(0); }
.khw-bot.is-open .khw-bot__tag { display: none; }

.khw-bot__fab-icon--close { opacity: 0; transform: rotate(-90deg) scale(0.6); }
.khw-bot.is-open .khw-bot__fab-icon--open { opacity: 0; transform: rotate(90deg) scale(0.6); }
.khw-bot.is-open .khw-bot__fab-icon--close { opacity: 1; transform: rotate(0) scale(1); }

/* نبضة لفت الانتباه */
.khw-bot__pulse {
    position: absolute;
    inset: 0;
    border-radius: 50%;
    border: 2px solid rgba(46, 185, 126, 0.6);
    animation: kbPulse 2.6s ease-out infinite;
    pointer-events: none;
}

.khw-bot.is-open .khw-bot__pulse { display: none; }

@keyframes kbPulse {
    0%   { transform: scale(1); opacity: 0.75; }
    70%  { transform: scale(1.55); opacity: 0; }
    100% { transform: scale(1.55); opacity: 0; }
}

/* ---------- نافذة المحادثة ---------- */
.khw-bot__panel {
    position: absolute;
    bottom: 78px;
    left: 0;
    width: 370px;
    max-width: calc(100vw - 36px);
    background: #fff;
    border-radius: 22px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-shadow: 0 4px 12px rgba(22, 50, 79, 0.1), 0 30px 70px -18px rgba(6, 35, 26, 0.55);
    border: 1px solid rgba(46, 185, 126, 0.2);
    opacity: 0;
    visibility: hidden;
    transform: translateY(16px) scale(0.97);
    transform-origin: bottom left;
    transition: opacity 0.32s ease, transform 0.38s cubic-bezier(0.22, 1, 0.36, 1), visibility 0.32s;
}

.khw-bot.is-open .khw-bot__panel { opacity: 1; visibility: visible; transform: translateY(0) scale(1); }

/* ---------- الترويسة ---------- */
.khw-bot__header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 15px 18px;
    background: linear-gradient(135deg, #0a2e1f 0%, #0f4d33 55%, var(--kb-navy) 140%);
    color: #fff;
}

.khw-bot__avatar {
    width: 40px;
    height: 40px;
    flex: none;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 17px;
    color: #06231a;
    background: linear-gradient(145deg, #7fe3b0, var(--kb-green));
    box-shadow: 0 6px 14px -4px rgba(46, 185, 126, 0.7);
}

.khw-bot__title { flex: 1; min-width: 0; }
.khw-bot__title b { display: block; font-size: 15.5px; font-weight: 800; }
.khw-bot__title span { display: flex; align-items: center; gap: 6px; font-size: 12px; color: rgba(234, 247, 240, 0.85); margin-top: 2px; }
.khw-bot__title span i { font-size: 7px; color: #45e39b; }

.khw-bot__min {
    flex: none;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    color: #fff;
    background: rgba(255, 255, 255, 0.12);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    transition: background 0.3s ease;
}

.khw-bot__min:hover { background: rgba(255, 255, 255, 0.22); }

/* ---------- جسم المحادثة ---------- */
.khw-bot__body {
    flex: 1;
    padding: 18px 16px;
    overflow-y: auto;
    max-height: 380px;
    min-height: 220px;
    background:
        radial-gradient(ellipse 60% 40% at 90% 0%, rgba(46, 185, 126, 0.06), transparent 65%),
        #f7fbf9;
    scroll-behavior: smooth;
}

.khw-bot__body::-webkit-scrollbar { width: 6px; }
.khw-bot__body::-webkit-scrollbar-thumb { background: rgba(46, 185, 126, 0.35); border-radius: 6px; }

.khw-bot__msg { display: flex; margin-bottom: 12px; }
.khw-bot__msg--bot { justify-content: flex-start; }
.khw-bot__msg--user { justify-content: flex-end; }

.khw-bot__bubble {
    max-width: 84%;
    font-size: 14.5px;
    line-height: 1.85;
    padding: 12px 16px;
    border-radius: 16px;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    animation: kbIn 0.35s cubic-bezier(0.22, 1, 0.36, 1) both;
}

@keyframes kbIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.khw-bot__msg--bot .khw-bot__bubble {
    color: #22343c;
    background: #fff;
    border: 1px solid rgba(22, 50, 79, 0.09);
    border-bottom-right-radius: 5px;
    box-shadow: 0 6px 16px -10px rgba(22, 50, 79, 0.35);
}

.khw-bot__msg--user .khw-bot__bubble {
    color: #fff;
    background: linear-gradient(135deg, var(--kb-green), var(--kb-green-d));
    border-bottom-left-radius: 5px;
    box-shadow: 0 8px 18px -8px rgba(46, 185, 126, 0.7);
}

/* روابط داخل الردود */
.khw-bot__bubble a {
    color: var(--kb-green-d);
    font-weight: 700;
    text-decoration: underline;
    text-underline-offset: 3px;
    word-break: break-all;
}

.khw-bot__msg--user .khw-bot__bubble a { color: #eaf7f0; }

/* ---------- اقتراحات سريعة ---------- */
.khw-bot__chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px; }

.khw-bot__chip {
    font-family: "Tajawal", sans-serif;
    font-size: 12.5px;
    font-weight: 700;
    color: var(--kb-green-d);
    background: rgba(46, 185, 126, 0.1);
    border: 1px solid rgba(46, 185, 126, 0.3);
    border-radius: 999px;
    padding: 8px 14px;
    cursor: pointer;
    transition: background 0.25s ease, color 0.25s ease, transform 0.25s ease;
}

.khw-bot__chip:hover { color: #fff; background: var(--kb-green-d); transform: translateY(-2px); }

/* ---------- مؤشر الكتابة ---------- */
.khw-bot__typing { display: flex; align-items: center; gap: 5px; padding: 14px 18px; }
.khw-bot__typing span {
    width: 7px; height: 7px; border-radius: 50%;
    background: rgba(46, 185, 126, 0.7);
    animation: kbDot 1.3s infinite ease-in-out;
}
.khw-bot__typing span:nth-child(2) { animation-delay: 0.18s; }
.khw-bot__typing span:nth-child(3) { animation-delay: 0.36s; }

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

/* ---------- حقل الإرسال ---------- */
.khw-bot__form {
    display: flex;
    align-items: center;
    gap: 9px;
    padding: 12px 14px;
    background: #fff;
    border-top: 1px solid rgba(22, 50, 79, 0.08);
}

.khw-bot__form input {
    flex: 1;
    min-width: 0;
    font-family: "Tajawal", sans-serif;
    font-size: 14.5px;
    color: #22343c;
    background: #f4f8f6;
    border: 1.5px solid rgba(22, 50, 79, 0.1);
    border-radius: 999px;
    padding: 11px 16px;
    outline: none;
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.khw-bot__form input:focus { border-color: var(--kb-green); box-shadow: 0 0 0 3px rgba(46, 185, 126, 0.14); }

.khw-bot__form button {
    flex: none;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    color: #fff;
    font-size: 15px;
    background: linear-gradient(135deg, var(--kb-green), var(--kb-green-d));
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 8px 18px -8px rgba(46, 185, 126, 0.75);
    transition: transform 0.3s cubic-bezier(0.22, 1, 0.36, 1);
}

.khw-bot__form button:hover { transform: scale(1.07); }
.khw-bot__form button:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

/* ---------- زر واتساب ---------- */
.khw-bot__wa {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 9px;
    font-size: 13.5px;
    font-weight: 700;
    color: #0d6b3f;
    background: rgba(37, 211, 102, 0.12);
    border-top: 1px solid rgba(37, 211, 102, 0.25);
    padding: 12px;
    text-decoration: none;
    transition: background 0.3s ease, color 0.3s ease;
}

.khw-bot__wa i { font-size: 16px; color: #25d366; }
.khw-bot__wa:hover { color: #fff; background: #25d366; }
.khw-bot__wa:hover i { color: #fff; }

/* ---------- التجاوب ---------- */
@media (max-width: 575px) {
    .khw-bot { bottom: 18px; left: 16px; }
    .khw-bot__fab { width: 54px; height: 54px; font-size: 21px; }
    .khw-bot__panel { width: calc(100vw - 32px); bottom: 68px; left: -4px; }
    .khw-bot__body { max-height: 54vh; min-height: 190px; }
}

/* لا يتعارض مع زر العودة للأعلى */
@media (max-width: 991px) {
    .khw-bot { bottom: 84px; }
}

/* ---------- تقليل الحركة ---------- */
@media (prefers-reduced-motion: reduce) {
    .khw-bot__pulse { animation: none; display: none; }
    .khw-bot__fab, .khw-bot__panel, .khw-bot__bubble, .khw-bot__chip, .khw-bot__form button { transition: none !important; animation: none !important; }
    .khw-bot__typing span { animation: none; }
}
