/* ==========================================================================
   تكسي سوريا — محادثة الراكب ↔ السائق (4.6) — ridechat.js
   --------------------------------------------------------------------------
   ملف عادي (مو من بناء Tailwind) — بيعتمد على متغيّرات --tsx-* تبع الموقع،
   فبيمشي مع الوضع الفاتح والغامق لحاله. خصائص منطقية (inline-start/end)
   فالاتجاه RTL/LTR بيضبط لحاله. الموبايل أولاً: لوح من تحت، وعلى الشاشات
   الكبيرة نافذة عائمة بطرف الشاشة.
   ========================================================================== */

.ts-rchat[hidden],
.ts-rchat [hidden],
.ts-rchat-btn[hidden],
.ts-rchat-btn__badge[hidden] { display: none !important; }

/* --- زر «محادثة» + شارة غير المقروء ---------------------------------- */
.ts-rchat-btn {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    cursor: pointer;
    font: 700 .84rem/1.2 var(--tsx-body, system-ui, sans-serif);
    -webkit-tap-highlight-color: transparent;
}
.ts-rchat-ico { width: 18px; height: 18px; flex: none; }

/* بطاقة السائق عند الراكب: نفس شكل زر الاتصال بس بإطار */
.ts-rchat-btn--rider {
    min-height: 36px;
    padding: 6px 14px;
    border: 1.5px solid var(--tsx-amber, #c6952e);
    border-radius: 999px;
    background: var(--tsx-surface, #fff);
    color: var(--tsx-amber-deep, #9a6e1e);
}
.ts-rchat-btn--rider:hover { background: var(--tsx-amber-wash, #fdf3dc); }
.ts-rchat-btn--rider.has-unread {
    border-color: transparent;
    background: var(--tsx-night, #0c2233);
    color: #fff;
}

/* بطاقة الرحلة عند السائق: زر من أزرار اللوحة (ts-drv-btn) */
.ts-rchat-btn--drv { flex: none; }
.ts-rchat-btn--drv.has-unread { border-color: rgba(225, 181, 88, .7) !important; }

.ts-rchat-btn__badge {
    position: absolute;
    top: -7px;
    inset-inline-end: -7px;
    min-width: 20px;
    height: 20px;
    padding: 0 5px;
    border-radius: 999px;
    background: var(--tsx-danger, #dc2626);
    color: #fff;
    font: 800 .72rem/20px var(--tsx-body, system-ui, sans-serif);
    text-align: center;
    box-shadow: 0 0 0 2px var(--tsx-surface, #fff);
    animation: ts-rchat-pop .28s ease-out;
}
@keyframes ts-rchat-pop {
    0% { transform: scale(.4); }
    70% { transform: scale(1.15); }
    100% { transform: scale(1); }
}

/* --- النافذة ---------------------------------------------------------- */
html.ts-rchat-lock { overflow: hidden; }

.ts-rchat {
    position: fixed;
    inset: 0;
    z-index: 4500; /* فوق الخريطة وبوت المساعدة، وتحت نوافذ TS.modal (5000) */
    display: flex;
    align-items: flex-end;
    justify-content: center;
    font-family: var(--tsx-body, system-ui, sans-serif);
}
.ts-rchat__backdrop {
    position: absolute;
    inset: 0;
    background: rgba(8, 20, 28, .55);
    opacity: 0;
    transition: opacity .18s ease;
}
.ts-rchat__sheet {
    position: relative;
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 82vh;
    height: 82dvh;
    max-height: 720px;
    overflow: hidden;
    border-radius: 20px 20px 0 0;
    background: var(--tsx-bg, #ebeff3);
    color: var(--tsx-ink, #0c2233);
    box-shadow: 0 -12px 40px -12px rgba(0, 0, 0, .45);
    transform: translateY(24px);
    opacity: 0;
    transition: transform .2s ease, opacity .2s ease;
    padding-bottom: env(safe-area-inset-bottom, 0);
}
.ts-rchat.is-open .ts-rchat__backdrop { opacity: 1; }
.ts-rchat.is-open .ts-rchat__sheet { transform: none; opacity: 1; }

@media (min-width: 640px) {
    .ts-rchat { align-items: flex-end; justify-content: flex-end; padding: 20px; }
    .ts-rchat__sheet {
        width: 400px;
        height: min(620px, calc(100vh - 40px));
        border-radius: 20px;
        box-shadow: 0 24px 60px -20px rgba(0, 0, 0, .55);
    }
    .ts-rchat__backdrop { background: rgba(8, 20, 28, .3); }
}

/* الرأس: كحلي + لمسة ذهبية */
.ts-rchat__head {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    background: linear-gradient(160deg, #10304a 0%, var(--tsx-night, #0c2233) 70%);
    color: #fff;
    border-bottom: 2px solid var(--tsx-amber, #c6952e);
}
.ts-rchat__avatar {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: none;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: linear-gradient(150deg, #F6DF9A 0%, #C6952E 46%, #9A6E1E 100%);
    color: #2a1c05;
}
.ts-rchat__avatar .ts-rchat-ico { width: 20px; height: 20px; }
.ts-rchat__heading { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.ts-rchat__heading h3 {
    margin: 0;
    font: 800 1.02rem/1.3 var(--tsx-display, var(--tsx-body, system-ui, sans-serif));
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.ts-rchat__heading small { color: #e1b558; font-size: .78rem; }
.ts-rchat__x {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: none;
    width: 40px;
    height: 40px;
    border: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, .1);
    color: #fff;
    cursor: pointer;
}
.ts-rchat__x:hover { background: rgba(255, 255, 255, .2); }
.ts-rchat__x svg { width: 20px; height: 20px; }

/* الرسائل */
.ts-rchat__list {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 14px 12px;
    -webkit-overflow-scrolling: touch;
}
.ts-rchat__empty {
    margin: auto;
    padding: 24px 16px;
    text-align: center;
    color: var(--tsx-ink-soft, #5b6472);
    font-size: .9rem;
    position: absolute;
    inset: 40% 0 auto;
    pointer-events: none;
}
.ts-rchat__msg {
    display: flex;
    flex-direction: column;
    max-width: 82%;
    padding: 8px 12px 5px;
    border-radius: 16px;
    box-shadow: 0 1px 1px rgba(0, 0, 0, .06);
    animation: ts-rchat-in .18s ease-out;
}
@keyframes ts-rchat-in {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: none; }
}
/* عمود flex: محور العرض = اتجاه السطر → end/start بتتبع RTL/LTR لحالها */
.ts-rchat__msg.is-mine {
    align-self: flex-end;
    background: linear-gradient(150deg, #F6DF9A 0%, #E1B558 100%);
    color: #2a1c05;
    border-end-end-radius: 5px;
}
.ts-rchat__msg.is-theirs {
    align-self: flex-start;
    background: var(--tsx-surface, #fff);
    color: var(--tsx-ink, #0c2233);
    border: 1px solid var(--tsx-line, #dbe2e8);
    border-end-start-radius: 5px;
}
.ts-rchat__text {
    margin: 0;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    font-size: .95rem;
    line-height: 1.5;
}
.ts-rchat__time {
    align-self: flex-end;
    margin-top: 2px;
    font-size: .68rem;
    opacity: .65;
    direction: ltr;
    font-variant-numeric: tabular-nums;
}

/* ملاحظات: الإرسال مقفول / خطأ */
.ts-rchat__closed,
.ts-rchat__error {
    margin: 0 12px 8px;
    padding: 8px 12px;
    border-radius: 10px;
    font-size: .82rem;
    line-height: 1.5;
}
.ts-rchat__closed { background: var(--tsx-amber-wash, #fdf3dc); color: var(--tsx-amber-deep, #9a6e1e); }
.ts-rchat__error { background: rgba(220, 38, 38, .1); color: var(--tsx-danger, #dc2626); }

/* الكتابة */
.ts-rchat__form {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 12px 6px;
    border-top: 1px solid var(--tsx-line, #dbe2e8);
    background: var(--tsx-surface, #fff);
}
.ts-rchat__input {
    flex: 1 1 auto;
    min-width: 0;
    min-height: 44px;
    max-height: 140px;
    padding: 10px 14px;
    border: 1px solid var(--tsx-line, #dbe2e8);
    border-radius: 22px;
    background: var(--tsx-surface-alt, #f1f5f8);
    color: var(--tsx-ink, #0c2233);
    font: 400 16px/1.4 var(--tsx-body, system-ui, sans-serif); /* 16px: بلا زوم تلقائي على iOS */
    resize: none;
    outline: none;
}
.ts-rchat__input:focus { border-color: var(--tsx-amber, #c6952e); box-shadow: 0 0 0 3px rgba(198, 149, 46, .2); }
.ts-rchat__input:disabled { opacity: .55; cursor: not-allowed; }
.ts-rchat__send {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: none;
    width: 44px;
    height: 44px;
    border: 0;
    border-radius: 50%;
    background: linear-gradient(150deg, #F6DF9A 0%, #C6952E 46%, #9A6E1E 100%);
    color: #2a1c05;
    cursor: pointer;
    box-shadow: 0 6px 16px -8px rgba(150, 110, 30, .8);
}
.ts-rchat__send svg { width: 20px; height: 20px; }
[dir="rtl"] .ts-rchat__send svg { transform: scaleX(-1); }
.ts-rchat__send:disabled { opacity: .45; cursor: not-allowed; box-shadow: none; }
.ts-rchat__keys {
    margin: 0;
    padding: 0 14px 8px;
    background: var(--tsx-surface, #fff);
    color: var(--tsx-ink-soft, #5b6472);
    font-size: .7rem;
    text-align: center;
}
.ts-rchat.is-readonly .ts-rchat__keys { display: none; }
/* تلميح الكيبورد ما إله معنى على شاشات اللمس */
@media (hover: none) and (pointer: coarse) {
    .ts-rchat__keys { display: none; }
}

@media (prefers-reduced-motion: reduce) {
    .ts-rchat__sheet,
    .ts-rchat__backdrop,
    .ts-rchat__msg,
    .ts-rchat-btn__badge { transition: none; animation: none; }
}
