/* Chat Widget Styles */

#chat-widget-container {
    position: fixed;
    z-index: 9999;
    font-family: 'Vazirmatn', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* مودال باز: بالاتر از دکمه نصب PWA در base (z-index: 10000) */
#chat-widget-container.chat-widget--open {
    z-index: 10050;
}

/* موقعیت ویجت — دسکتاپ */
#chat-widget-container[data-position="bottom_right"] {
    bottom: 20px;
    right: 20px;
}

#chat-widget-container[data-position="bottom_left"] {
    bottom: 20px;
    left: 20px;
}

/* دکمه شناور */
.chat-widget-button {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: var(--widget-color, #007bff);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    transition: all 0.3s ease;
    position: relative;
}

.chat-widget-button:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}

.chat-widget-badge {
    position: absolute;
    top: -5px;
    right: -5px;
    background: #dc3545;
    color: white;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: bold;
    border: 2px solid white;
}

/* پاپاپ ویجت */
.chat-widget-popup {
    position: absolute;
    bottom: 80px;
    width: 380px;
    max-width: calc(100vw - 40px);
    height: 600px;
    max-height: calc(100vh - 100px);
    background: white;
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: slideUp 0.3s ease;
    min-width: 0;
    box-sizing: border-box;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* موقعیت پاپاپ */
#chat-widget-container[data-position="bottom_right"] .chat-widget-popup {
    right: 0;
}

#chat-widget-container[data-position="bottom_left"] .chat-widget-popup {
    left: 0;
}

/* هدر */
.chat-widget-header {
    background: var(--widget-color, #007bff);
    color: white;
    padding: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.chat-widget-header-info h3 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
}

.chat-widget-status {
    font-size: 12px;
    opacity: 0.9;
}

.chat-widget-close {
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    padding: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0.8;
    transition: opacity 0.2s;
}

.chat-widget-close:hover {
    opacity: 1;
}

/* بدنه چت — LTR برای محور افقی حباب‌ها؛ متن داخل حباب در .chat-message-content با rtl می‌ماند */
.chat-widget-body {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    background: #f8f9fa;
    display: flex;
    flex-direction: column;
    gap: 12px;
    direction: ltr;
}

.chat-widget-welcome {
    text-align: center;
    padding: 20px;
    color: #6c757d;
    direction: rtl;
}

.chat-widget-welcome p {
    margin: 8px 0;
}

/* پیام‌ها — ارسالی راست، دریافتی چپ (فیزیکی) */
.chat-message {
    display: flex;
    width: 100%;
    gap: 8px;
    margin-bottom: 12px;
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

.chat-message.customer {
    justify-content: flex-end;
    flex-direction: row;
}

.chat-message.admin {
    justify-content: flex-start;
    flex-direction: row;
}

.chat-message-content {
    max-width: 75%;
    padding: 10px 14px;
    border-radius: 12px;
    font-size: var(--widget-font-size, 14px);
    word-wrap: break-word;
    direction: rtl;
    text-align: right;
}

.chat-message.customer .chat-message-content {
    /* متغیرها توسط JS از روی亮度 رنگ ویجت تنظیم می‌شوند؛ متن همیشه خوانا */
    background: var(--chat-outgoing-bg, var(--widget-color, #007bff));
    color: var(--chat-outgoing-text, #ffffff);
    border: 1px solid var(--chat-outgoing-border, transparent);
    border-bottom-right-radius: 4px;
}

.chat-message.admin .chat-message-content {
    background: white;
    color: #333;
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.chat-message-time {
    font-size: 11px;
    color: #6c757d;
    margin-top: 4px;
    text-align: left;
}

.chat-message.customer .chat-message-time {
    text-align: right;
}

.chat-message-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 6px;
    margin-top: 4px;
    flex-wrap: wrap;
}

.chat-message.customer .chat-message-meta {
    justify-content: flex-end;
}

.chat-message.customer .chat-message-meta .chat-message-time {
    color: var(--chat-outgoing-meta, rgba(255, 255, 255, 0.9));
}

.chat-message-send-state {
    display: inline-flex;
    align-items: center;
    color: var(--chat-outgoing-meta, rgba(255, 255, 255, 0.85));
    flex-shrink: 0;
}

.chat-message--pending .chat-message-content {
    opacity: 0.94;
    position: relative;
}

.chat-widget-clock-ic {
    animation: chat-widget-clock-tick 0.9s ease-in-out infinite;
}

@keyframes chat-widget-clock-tick {
    0%,
    100% {
        opacity: 0.65;
        transform: rotate(-6deg);
    }
    50% {
        opacity: 1;
        transform: rotate(6deg);
    }
}

.chat-message--failed .chat-message-content {
    outline: 2px dashed rgba(220, 53, 69, 0.6);
    opacity: 0.95;
}

.chat-widget-inline-alert {
    font-size: 12px;
    line-height: 1.4;
    padding: 8px 10px;
    border-radius: 8px;
    background: #fff3cd;
    color: #664d03;
    border: 1px solid #ffecb5;
    margin: 0;
}

.chat-widget-inline-alert[hidden] {
    display: none !important;
}

.chat-message-read {
    font-size: 10px;
    color: #6c757d;
    margin-top: 2px;
    text-align: left;
}

.chat-message.customer .chat-message-read {
    text-align: right;
    color: var(--chat-outgoing-meta, rgba(255, 255, 255, 0.88));
    font-size: 11px;
}

/* ضمیمه */
.chat-message-attachment {
    margin-top: 8px;
    max-width: 200px;
}

.chat-message-attachment img {
    width: 100%;
    border-radius: 8px;
    cursor: pointer;
}

/* نشانگر تایپ — سمت چپ (طرف مقابل در حال نوشتن) */
.chat-widget-typing {
    padding: 12px 16px;
    display: flex;
    gap: 4px;
    align-items: center;
    direction: ltr;
    justify-content: flex-start;
}

.chat-widget-typing span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #6c757d;
    animation: typing 1.4s infinite;
}

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

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

@keyframes typing {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.7;
    }
    30% {
        transform: translateY(-10px);
        opacity: 1;
    }
}

/* فوتر */
.chat-widget-footer {
    border-top: 1px solid #e9ecef;
    background: white;
    padding: 12px;
    min-width: 0;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* فرم مهمان — داخل کارت، تمام‌عرض، بدون سرریز از مودال */
.chat-widget-guest-info {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    box-sizing: border-box;
    margin: 0;
    padding: 10px 12px;
    background: linear-gradient(180deg, #f8fafb 0%, #f1f5f9 100%);
    border: 1px solid rgba(0, 0, 0, 0.06);
    border-radius: 12px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
    max-height: 320px;
    opacity: 1;
    overflow: hidden;
    transition:
        max-height 0.35s cubic-bezier(0.4, 0, 0.2, 1),
        opacity 0.25s ease,
        margin 0.25s ease,
        padding 0.25s ease,
        border-width 0.2s ease;
}

.chat-widget-guest-info--dismissed {
    max-height: 0 !important;
    opacity: 0 !important;
    margin-top: 0 !important;
    margin-bottom: 0 !important;
    padding-top: 0 !important;
    padding-bottom: 0 !important;
    border-width: 0 !important;
    pointer-events: none;
    visibility: hidden;
}

.chat-widget-guest-hint {
    margin: 0 0 10px;
    font-size: 12px;
    line-height: 1.45;
    color: #64748b;
    font-weight: 500;
}

.chat-widget-guest-fields {
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
    min-width: 0;
}

.chat-widget-guest-field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
    width: 100%;
}

.chat-widget-guest-field label {
    font-size: 11px;
    font-weight: 600;
    color: #475569;
    letter-spacing: 0.02em;
}

.chat-widget-guest-field input {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    box-sizing: border-box;
    padding: 10px 12px;
    border: 1px solid #e2e8f0;
    border-radius: 10px;
    font-size: 14px;
    font-family: inherit;
    background: #fff;
    color: #0f172a;
    transition:
        border-color 0.2s,
        box-shadow 0.2s;
}

.chat-widget-guest-field input::placeholder {
    color: #94a3b8;
}

.chat-widget-guest-field input:focus {
    outline: none;
    border-color: var(--widget-color, #0d6efd);
    box-shadow: 0 0 0 2px rgba(15, 23, 42, 0.06);
}

.chat-widget-guest-field--warn input {
    border-color: #f97316;
    background: #fffbeb;
}

.chat-widget-guest-info--attention {
    animation: chat-widget-guest-nudge 0.55s ease;
}

@keyframes chat-widget-guest-nudge {
    0%,
    100% {
        transform: translateX(0);
    }
    20% {
        transform: translateX(-4px);
    }
    40% {
        transform: translateX(4px);
    }
    60% {
        transform: translateX(-3px);
    }
    80% {
        transform: translateX(3px);
    }
}

.chat-widget-input-container {
    display: flex;
    gap: 8px;
    align-items: center;
    min-width: 0;
    width: 100%;
}

.chat-widget-attach,
.chat-widget-send {
    background: none;
    border: none;
    color: var(--widget-color, #007bff);
    cursor: pointer;
    padding: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition:
        color 0.2s,
        opacity 0.2s,
        transform 0.15s ease;
}

.chat-widget-attach:hover,
.chat-widget-send:hover:not(:disabled) {
    color: var(--widget-color, #007bff);
    opacity: 0.85;
}

.chat-widget-send:disabled {
    opacity: 0.35;
    cursor: not-allowed;
    pointer-events: none;
    color: #94a3b8;
}

.chat-widget-send--busy {
    animation: chat-widget-send-pulse 0.85s ease-in-out infinite;
}

@keyframes chat-widget-send-pulse {
    0%,
    100% {
        opacity: 0.45;
    }
    50% {
        opacity: 0.95;
    }
}

#chat-widget-input {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid #dee2e6;
    border-radius: 20px;
    font-size: var(--widget-font-size, 14px);
    font-family: inherit;
    outline: none;
    transition: border-color 0.2s;
}

#chat-widget-input:focus {
    border-color: var(--widget-color, #007bff);
}

/* قفل اسکرول پس‌زمینه هنگام باز بودن چت (موبایل) */
body.chat-widget-body-lock {
    overflow: hidden !important;
    touch-action: none;
}

@media (min-width: 1024px) {
    body.chat-widget-body-lock {
        overflow: auto !important;
        touch-action: auto;
    }
}

/* پس‌زمینه تیره موبایل */
.chat-widget-backdrop {
    display: none;
}

/* موبایل / تبلت: مودال نزدیک تمام‌صفحه با حاشیهٔ یکنواخت + بک‌دراپ */
@media (max-width: 1023px) {
    .chat-widget-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 10000;
        margin: 0;
        padding: 0;
        border: none;
        background: rgba(15, 23, 42, 0.5);
        -webkit-backdrop-filter: blur(10px);
        backdrop-filter: blur(10px);
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s ease, visibility 0.3s ease;
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }

    .chat-widget-backdrop:not([hidden]) {
        opacity: 1;
        visibility: visible;
    }

    .chat-widget-backdrop[hidden] {
        display: none !important;
        pointer-events: none;
    }

    #chat-widget-container[data-position="bottom_right"] {
        bottom: var(--mobile-chat-fab-bottom, calc(4.85rem + env(safe-area-inset-bottom, 0px)));
        right: max(12px, env(safe-area-inset-right, 0px));
        left: auto;
    }

    #chat-widget-container[data-position="bottom_left"] {
        bottom: var(--mobile-chat-fab-bottom, calc(4.85rem + env(safe-area-inset-bottom, 0px)));
        left: max(12px, env(safe-area-inset-left, 0px));
        right: auto;
    }

    .chat-widget-button {
        width: 56px;
        height: 56px;
        transition: opacity 0.22s ease, transform 0.24s cubic-bezier(0.34, 1.06, 0.64, 1);
    }

    .chat-widget--open .chat-widget-button {
        opacity: 0;
        pointer-events: none;
        transform: scale(0.88);
    }

    /*
     * بسته: مودال در DOM است اما display:none کامل — بدون این، فرزندان با pointer-events:auto
     * روی کل صفحه تاچ می‌دزدند در حالی که opacity والد 0 است (TV موبایل / WebView).
     * باز: فقط با .chat-widget--open نمایش داده می‌شود.
     */
    #chat-widget-container:not(.chat-widget--open) .chat-widget-popup {
        display: none !important;
        pointer-events: none !important;
    }

    #chat-widget-container.chat-widget--open .chat-widget-popup {
        position: fixed !important;
        z-index: 10002;
        pointer-events: auto;
        touch-action: auto;
        top: max(28px, calc(env(safe-area-inset-top, 0px) + 10px)) !important;
        bottom: max(28px, calc(env(safe-area-inset-bottom, 0px) + 10px)) !important;
        /*
         * RTL: start = راست (فاصله کمتر)، end = چپ (فاصله بیشتر — مودال کوچک‌تر).
         */
        left: max(56px, env(safe-area-inset-left, 0px)) !important;
        right: max(18px, env(safe-area-inset-right, 0px)) !important;
        width: auto !important;
        max-width: none !important;
        height: auto !important;
        max-height: none !important;
        min-height: 0 !important;
        border-radius: 22px;
        box-shadow:
            0 0 0 1px rgba(255, 255, 255, 0.06) inset,
            0 28px 64px -16px rgba(0, 0, 0, 0.42);
        display: flex !important;
        flex-direction: column;
        overflow: hidden;
        opacity: 1;
        /* transform روی position:fixed گاهی جعبهٔ قابل مشاهده را به گوشه والد (آیکون) می‌چسباند */
        transform: none !important;
        transition: opacity 0.3s cubic-bezier(0.32, 0.72, 0, 1);
        will-change: opacity;
        -webkit-overflow-scrolling: touch;
    }

    html[dir="ltr"] #chat-widget-container.chat-widget--open .chat-widget-popup {
        left: max(18px, env(safe-area-inset-left, 0px)) !important;
        right: max(56px, env(safe-area-inset-right, 0px)) !important;
    }

    .chat-widget-header {
        flex-shrink: 0;
        padding: 14px 16px;
    }

    .chat-widget-body {
        flex: 1 1 auto;
        min-height: 0;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        overflow-y: auto;
        padding: 12px 14px;
        touch-action: pan-y pinch-zoom;
    }

    .chat-widget-footer {
        flex-shrink: 0;
        padding: 12px;
        padding-bottom: max(12px, env(safe-area-inset-bottom, 0px));
    }

    .chat-widget-welcome {
        padding: 14px 10px;
    }

    .chat-widget-guest-info {
        padding: 10px;
    }

    .chat-widget-guest-hint {
        font-size: max(13px, 12px);
    }
    .chat-widget-input-container {
        flex-wrap: nowrap;
        align-items: center;
        gap: 6px;
    }

    .chat-message-content {
        max-width: 90%;
    }

    /* جلوگیری از زوم ناخواسته iOS روی فوکوس */
    #chat-widget-input,
    .chat-widget-guest-info input {
        font-size: max(16px, var(--widget-font-size, 16px));
    }
}

@media (min-width: 1024px) {
    .chat-widget-backdrop {
        display: none !important;
    }

    .chat-widget--open .chat-widget-button {
        opacity: 1 !important;
        pointer-events: auto !important;
        transform: none !important;
    }
}

@media (max-width: 380px) {
    .chat-widget-header-info h3 {
        font-size: 16px;
    }

    #chat-widget-input {
        font-size: 13px;
    }
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
    .chat-widget-popup {
        background: #1a1a1a;
        color: #e0e0e0;
    }
    
    .chat-widget-body {
        background: #2a2a2a;
    }
    
    .chat-message.admin .chat-message-content {
        background: #333;
        color: #e0e0e0;
    }
    
    .chat-widget-footer {
        background: #1a1a1a;
        border-top-color: #333;
    }

    .chat-widget-guest-info {
        background: linear-gradient(180deg, #252525 0%, #1f1f1f 100%);
        border-color: #333;
    }

    .chat-widget-guest-hint {
        color: #94a3b8;
    }

    .chat-widget-guest-field label {
        color: #cbd5e1;
    }

    .chat-widget-guest-field input {
        background: #2a2a2a;
        border-color: #444;
        color: #e0e0e0;
    }

    .chat-widget-guest-field input::placeholder {
        color: #64748b;
    }

    .chat-widget-guest-field--warn input {
        border-color: #fb923c;
        background: #292524;
    }

    .chat-widget-send:disabled {
        color: #64748b;
    }
    
    #chat-widget-input {
        background: #2a2a2a;
        border-color: #444;
        color: #e0e0e0;
    }
}
