/* ============================================================
 * کارباطناب AI — ویجت چت هوش مصنوعی — نسخه ۱۸.۸.۱
 * ظاهر: دکمه‌ی شناور ربات + پنل چت با هدر کهربایی، آواتار،
 * انیمیشن ورود پیام‌ها، ساعت پیام، چیپ‌های پیگیری، RTL
 * جدید در ۱۸.۸: ورودی چندخطی خودبلند، دکمه کپی پاسخ، دکمه اسکرول،
 * بافت نقطه‌ای بدنه، الگوی هدر، رنگ‌آمیزی لبه حباب ربات، متن تایپ
 * جدید در ۱۸.۸.۱: بلوک دفاعی انتها فایل — استایل‌های بحرانی با
 *   !important تا CSS قالب یا کش قدیمی مرورگر نتواند چیدمان پنل را بشکند
 * ============================================================ */

/* ---------- دکمه‌ی شناور ربات ---------- */
#ktAiLauncher {
    position: fixed;
    bottom: 22px;
    right: 22px;
    width: 62px;
    height: 62px;
    border: 0;
    border-radius: 50%;
    background: linear-gradient(135deg, #fbbf24, #f59e0b 55%, #d97706);
    background-size: 170% 170%;
    animation: ktai_grad 6s ease infinite;
    box-shadow: 0 8px 26px rgba(217, 119, 6, .45);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 99999;
    transition: transform .2s ease, box-shadow .2s ease;
    padding: 0;
}
@keyframes ktai_grad {
    0%, 100% { background-position: 0% 30%; }
    50%      { background-position: 100% 70%; }
}
#ktAiLauncher:hover {
    transform: scale(1.08);
    box-shadow: 0 10px 32px rgba(217, 119, 6, .6);
}
#ktAiLauncher:active { transform: scale(.96); }
#ktAiLauncher svg { width: 36px; height: 36px; display: block; transition: transform .25s ease; }
#ktAiLauncher:hover svg { transform: rotate(-8deg) scale(1.08); }

/* حلقه‌ی پالس دور دکمه */
#ktAiLauncher::after {
    content: '';
    position: absolute;
    inset: -5px;
    border-radius: 50%;
    border: 2px solid rgba(245, 158, 11, .55);
    animation: ktai_pulse 2.2s ease-out infinite;
    pointer-events: none;
}
@keyframes ktai_pulse {
    0%   { transform: scale(.9); opacity: 1; }
    70%  { transform: scale(1.25); opacity: 0; }
    100% { transform: scale(1.25); opacity: 0; }
}

/* نشان اطلاع «۱» تا اولین باز شدن */
.ktai-badge {
    position: absolute;
    top: -4px;
    left: -4px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #dc2626;
    color: #fff;
    font-size: 11px;
    font-weight: 800;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid #fff;
    animation: ktai_badge_in .35s ease;
}
@keyframes ktai_badge_in {
    from { transform: scale(0); }
    to   { transform: scale(1); }
}

/* ---------- حباب راهنمای کنار دکمه ---------- */
.ktai-tip {
    position: fixed;
    bottom: 34px;
    right: 96px;
    max-width: 240px;
    background: #111827;
    color: #fff;
    font-size: 12.5px;
    line-height: 1.8;
    padding: 10px 12px;
    border-radius: 12px 12px 2px 12px;
    box-shadow: 0 10px 30px rgba(17, 24, 39, .35);
    display: flex;
    align-items: center;
    gap: 8px;
    z-index: 99999;
    cursor: pointer;
    animation: ktai_tip_in .3s ease;
}
.ktai-tip[hidden] { display: none; }
@keyframes ktai_tip_in {
    from { transform: translateY(8px); opacity: 0; }
    to   { transform: translateY(0); opacity: 1; }
}
.ktai-tip::after {
    content: '';
    position: absolute;
    bottom: -6px;
    right: 26px;
    width: 12px;
    height: 12px;
    background: #111827;
    transform: rotate(45deg);
    border-radius: 2px;
}
.ktai-tip-close {
    border: 0;
    background: rgba(255, 255, 255, .16);
    color: #fff;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    font-size: 13px;
    line-height: 1;
    cursor: pointer;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}
.ktai-tip-close:hover { background: rgba(255, 255, 255, .3); }

/* ---------- پنل چت ---------- */
#ktAiPanel {
    position: fixed;
    bottom: 98px;
    right: 22px;
    width: 376px;
    max-width: calc(100vw - 24px);
    height: 556px;
    max-height: calc(100vh - 122px);
    background: #fff;
    border-radius: 20px;
    box-shadow: 0 18px 60px rgba(15, 23, 42, .30);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    z-index: 99998;
    transform-origin: bottom right;
    animation: ktai_pop .22s ease;
}
#ktAiPanel[hidden] { display: none; }
@keyframes ktai_pop {
    from { transform: translateY(14px) scale(.96); opacity: 0; }
    to   { transform: translateY(0) scale(1); opacity: 1; }
}

/* هدر کهربایی */
.ktai-head {
    position: relative;
    background: linear-gradient(135deg, #fbbf24, #f59e0b 55%, #d97706);
    color: #fff;
    padding: 13px 14px;
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
    overflow: hidden;
}
.ktai-head::before {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(120px 60px at 85% -10%, rgba(255, 255, 255, .35), transparent 70%);
    pointer-events: none;
}
/* الگوی نقطه‌ای ظریف روی هدر (۱۸.۸) */
.ktai-head::after {
    content: '';
    position: absolute;
    inset: 0;
    background-image: radial-gradient(rgba(255, 255, 255, .16) 1px, transparent 1.4px);
    background-size: 14px 14px;
    pointer-events: none;
}
.ktai-head-avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: rgba(255, 255, 255, .92);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .12);
}
.ktai-head-avatar svg { width: 30px; height: 30px; }
.ktai-head-info { flex: 1; min-width: 0; }
.ktai-head-title {
    font-size: 14.5px;
    font-weight: 800;
    margin: 0;
    line-height: 1.5;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.ktai-head-sub {
    font-size: 11px;
    opacity: .95;
    margin: 1px 0 0;
    display: flex;
    align-items: center;
    gap: 5px;
}
.ktai-online-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #22c55e;
    box-shadow: 0 0 0 3px rgba(34, 197, 94, .25);
    flex-shrink: 0;
    animation: ktai_dot 2s ease infinite;
}
@keyframes ktai_dot {
    0%, 100% { box-shadow: 0 0 0 3px rgba(34, 197, 94, .25); }
    50%      { box-shadow: 0 0 0 5px rgba(34, 197, 94, .12); }
}
.ktai-clear,
.ktai-close {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    border: 0;
    background: rgba(255, 255, 255, .22);
    color: #fff;
    font-size: 17px;
    line-height: 1;
    cursor: pointer;
    flex-shrink: 0;
    transition: background .15s;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}
.ktai-clear svg { width: 16px; height: 16px; }
.ktai-clear:hover,
.ktai-close:hover { background: rgba(255, 255, 255, .38); }

/* بدنه‌ی پیام‌ها — با بافت نقطه‌ای ظریف (۱۸.۸) */
.ktai-body {
    flex: 1;
    overflow-y: auto;
    background: #f6f7f9;
    background-image: radial-gradient(rgba(17, 24, 39, .05) 1px, transparent 1.3px);
    background-size: 16px 16px;
    padding: 14px 12px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    scroll-behavior: smooth;
    position: relative;
}
.ktai-body::-webkit-scrollbar { width: 6px; }
.ktai-body::-webkit-scrollbar-track { background: transparent; }
.ktai-body::-webkit-scrollbar-thumb { background: #d7dade; border-radius: 3px; }
.ktai-body::-webkit-scrollbar-thumb:hover { background: #c2c7cd; }

.ktai-row {
    display: flex;
    gap: 8px;
    align-items: flex-end;
    animation: ktai_msg_in .25s ease;
}
.ktai-row.user { justify-content: flex-end; }
@keyframes ktai_msg_in {
    from { transform: translateY(8px); opacity: 0; }
    to   { transform: translateY(0); opacity: 1; }
}

/* ستون حباب + ساعت */
.ktai-col {
    display: flex;
    flex-direction: column;
    gap: 3px;
    max-width: 80%;
    min-width: 0;
}
.ktai-row.bot .ktai-col  { align-items: flex-start; }
.ktai-row.user .ktai-col { align-items: flex-end; }

.ktai-msg {
    max-width: 100%;
    padding: 10px 13px;
    border-radius: 14px;
    font-size: 13.5px;
    line-height: 1.95;
    white-space: pre-wrap;
    word-break: break-word;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .04);
}
.ktai-row.bot .ktai-msg {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-right: 3px solid #fbbf24;
    color: #1f2937;
    border-top-right-radius: 4px;
}
.ktai-row.user .ktai-msg {
    background: linear-gradient(135deg, #f59e0b, #d97706);
    color: #fff;
    border-top-left-radius: 4px;
}
.ktai-msg a {
    color: #b45309;
    font-weight: 700;
    text-decoration: underline;
    text-underline-offset: 2px;
}
.ktai-row.user .ktai-msg a { color: #fff; }

/* ساعت پیام + دکمه کپی (۱۸.۸) */
.ktai-meta {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 0 4px;
}
.ktai-time {
    font-size: 9.5px;
    color: #9ca3af;
    user-select: none;
}
.ktai-copy {
    width: 22px;
    height: 22px;
    border-radius: 6px;
    border: 1px solid #e5e7eb;
    background: #fff;
    color: #9ca3af;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    opacity: 0;
    transition: opacity .15s, color .15s, border-color .15s, background .15s;
}
.ktai-row:hover .ktai-copy { opacity: 1; }
.ktai-copy:hover { color: #b45309; border-color: #f59e0b; background: #fffbeb; }
.ktai-copy.ok { color: #16a34a; border-color: #86efac; opacity: 1; }
.ktai-copy svg { width: 12px; height: 12px; }
@media (hover: none) {
    .ktai-copy { opacity: .75; }
}

/* آواتار کوچک کنار پیام ربات */
.ktai-av-mini {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: #fef3c7;
    border: 1px solid #fde68a;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.ktai-av-mini svg { width: 19px; height: 19px; }

/* نشانگر تایپ + متن «در حال نوشتن…» (۱۸.۸) */
.ktai-typing-row { display: flex; align-items: center; gap: 9px; }
.ktai-typing { display: flex; gap: 4px; padding: 4px 0; }
.ktai-typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #d97706;
    animation: ktai_bounce 1.2s infinite;
}
.ktai-typing span:nth-child(2) { animation-delay: .15s; }
.ktai-typing span:nth-child(3) { animation-delay: .3s; }
.ktai-typing-txt {
    font-size: 11.5px;
    color: #9ca3af;
    user-select: none;
}
@keyframes ktai_bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: .55; }
    30%           { transform: translateY(-5px); opacity: 1; }
}

/* چیپ‌های پیشنهادی و پیگیری */
.ktai-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 0 2px;
    animation: ktai_msg_in .3s ease;
}
.ktai-chip {
    border: 1px solid #f59e0b;
    color: #b45309;
    background: #fffbeb;
    border-radius: 999px;
    padding: 5px 12px;
    font-size: 12px;
    font-family: inherit;
    cursor: pointer;
    transition: background .15s, transform .15s, box-shadow .15s;
}
.ktai-chip:hover {
    background: #fef3c7;
    transform: translateY(-1px);
    box-shadow: 0 3px 10px rgba(245, 158, 11, .25);
}
.ktai-chip:active { transform: translateY(0); }
.ktai-chip.ktai-retry { background: #fef2f2; border-color: #fca5a5; color: #b91c1c; }
.ktai-chip.ktai-retry:hover { background: #fee2e2; box-shadow: 0 3px 10px rgba(220, 38, 38, .2); }

/* دکمه‌ی «رفتن به آخر پیام» (۱۸.۸) */
.ktai-godown {
    position: absolute;
    bottom: 88px;
    right: 16px;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: 1px solid #e5e7eb;
    background: #fff;
    color: #b45309;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    box-shadow: 0 4px 14px rgba(15, 23, 42, .16);
    z-index: 5;
    transition: background .15s, transform .15s;
}
.ktai-godown:hover { background: #fffbeb; transform: translateY(-1px); }
.ktai-godown[hidden] { display: none; }
.ktai-godown svg { width: 16px; height: 16px; }

/* نوار ورودی */
.ktai-inputbar {
    display: flex;
    gap: 8px;
    padding: 10px;
    border-top: 1px solid #e5e7eb;
    background: #fff;
    align-items: center;
    flex-shrink: 0;
}
.ktai-input {
    flex: 1;
    border: 1px solid #e5e7eb;
    border-radius: 999px;
    padding: 10px 15px;
    font-size: 13.5px;
    font-family: inherit;
    background: #f9fafb;
    color: #1f2937;
    min-width: 0;
    transition: border-color .15s, background .15s, box-shadow .15s;
    resize: none;
    max-height: 110px;
    min-height: 41px;
    line-height: 1.8;
    overflow-y: auto;
}
.ktai-input:focus {
    outline: none;
    border-color: #f59e0b;
    background: #fff;
    box-shadow: 0 0 0 3px rgba(245, 158, 11, .15);
}
.ktai-count {
    font-size: 9.5px;
    color: #9ca3af;
    white-space: nowrap;
    opacity: 0;
    transition: opacity .15s;
    user-select: none;
}
.ktai-count.on { opacity: 1; }
.ktai-send {
    width: 41px;
    height: 41px;
    border-radius: 50%;
    border: 0;
    background: linear-gradient(135deg, #f59e0b, #d97706);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: transform .15s, opacity .15s, box-shadow .15s;
}
.ktai-send:hover {
    transform: scale(1.06);
    box-shadow: 0 4px 14px rgba(217, 119, 6, .4);
}
.ktai-send:disabled { opacity: .5; cursor: default; transform: none; box-shadow: none; }
.ktai-send svg { width: 19px; height: 19px; transform: scaleX(-1); }

/* یادداشت پایین پنل */
.ktai-note {
    font-size: 10px;
    color: #9ca3af;
    text-align: center;
    padding: 0 10px 7px;
    background: #fff;
    flex-shrink: 0;
    line-height: 1.7;
}

/* فوکوس قابل‌مشاهده برای دسترس‌پذیری */
#ktAiLauncher:focus-visible,
.ktai-chip:focus-visible,
.ktai-send:focus-visible,
.ktai-clear:focus-visible,
.ktai-close:focus-visible,
.ktai-tip-close:focus-visible {
    outline: 2px solid #b45309;
    outline-offset: 2px;
}

/* ---------- موبایل ---------- */
@media (max-width: 480px) {
    #ktAiLauncher { bottom: 14px; right: 14px; width: 58px; height: 58px; }
    #ktAiLauncher svg { width: 33px; height: 33px; }
    #ktAiPanel {
        right: 6px;
        left: 6px;
        width: auto;
        bottom: 82px;
        height: min(72vh, 620px);
        border-radius: 16px;
    }
    .ktai-tip { right: 84px; bottom: 26px; max-width: 200px; }
}

/* صفحه‌های خیلی کوتاه (موبایل افقی) */
@media (max-height: 560px) {
    #ktAiPanel { height: calc(100vh - 100px); }
}

/* احترام به تنظیم کاهش انیمیشن کاربر */
@media (prefers-reduced-motion: reduce) {
    #ktAiLauncher::after,
    .ktai-badge,
    .ktai-online-dot,
    .ktai-typing span { animation: none !important; }
    #ktAiLauncher { animation: none !important; }
    #ktAiPanel,
    .ktai-row,
    .ktai-chips,
    .ktai-tip { animation: none !important; }
    .ktai-body { scroll-behavior: auto; }
}

/* ============================================================
 * بلوک دفاعی ۱۸.۸.۱ — ضد تداخل با CSS قالب و ضد کش قدیمی
 * ------------------------------------------------------------
 * مشکل واقعی سایت: مرورگر بعضی بازدیدکننده‌ها نسخه‌ی قدیمیِ این فایل
 * را از کش خواند (بدون کلاس‌های جدید مثل ktai-clear) و ویجت به‌هم ریخت:
 * کادر سفید کنار × و بدنه‌ی جمع‌شده. این بلوک چیدمان بحرانی را قفل می‌کند
 * تا هیچ CSS بیرونی (یا ناقص) نتواند آن را بشکند. در footer.php هم
 * URL فایل‌ها با ?v= تغییر نسخه می‌شود تا کش همیشه تازه شود.
 * ============================================================ */
#ktAiPanel,
#ktAiPanel * { box-sizing: border-box !important; }

#ktAiPanel:not([hidden]) {
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
    /* ارتفاع عمداً بدون !important گذاشته شد تا media query موبایل (۴۸۰px)
       و حالت صفحه‌ی کوتاه (۵۶۰px) بتوانند ارتفاع را عوض کنند */
}

#ktAiPanel button {
    appearance: none;
    -webkit-appearance: none;
    font-family: inherit;
    box-shadow: none;
    text-transform: none;
}

.ktai-head {
    flex-shrink: 0 !important;
    display: flex !important;
    align-items: center !important;
}

.ktai-clear,
.ktai-close {
    border: 0 !important;
    padding: 0 !important;
    width: 30px !important;
    height: 30px !important;
    border-radius: 50% !important;
    background: rgba(255, 255, 255, .22) !important;
    color: #fff !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
}
.ktai-clear svg { width: 16px !important; height: 16px !important; display: block !important; }

.ktai-body {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    max-height: none !important;
    overflow-y: auto !important;
    display: flex !important;
    flex-direction: column !important;
}

.ktai-inputbar {
    display: flex !important;
    flex-shrink: 0 !important;
    align-items: center !important;
}

.ktai-input {
    appearance: none;
    -webkit-appearance: none;
    flex: 1 1 auto !important;
    min-width: 0 !important;
}

.ktai-send {
    width: 41px !important;
    height: 41px !important;
    border-radius: 50% !important;
    border: 0 !important;
    padding: 0 !important;
    flex-shrink: 0 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
}

.ktai-note { flex-shrink: 0 !important; }
