/* ===== Chat Widget ===== */

.chat-widget { position: fixed; bottom: 28px; right: 28px; z-index: 2000; }
[dir="rtl"] .chat-widget { right: auto; left: 28px; }

/* ---- Toggle button ---- */
.chat-toggle {
    display: inline-flex; align-items: center; gap: 10px;
    padding: 14px 22px; border-radius: 50px;
    background: var(--primary); border: none;
    cursor: grab; color: #fff;
    font-weight: 700; font-size: 14px; font-family: inherit;
    white-space: nowrap; flex-shrink: 0;
    box-shadow: 0 6px 24px rgba(4,64,104,.4), 0 0 0 0 rgba(79,219,120,.55);
    animation: chat-pulse 2.8s ease-in-out infinite;
    transition: transform .2s, box-shadow .2s, background .2s;
}
.chat-toggle:hover {
    transform: translateY(-3px);
    background: #033455;
    box-shadow: 0 10px 32px rgba(4,64,104,.5);
    animation: none;
}
[dir="rtl"] .chat-toggle { direction: rtl; flex-direction: row-reverse; font-family: 'Segoe UI', Arial, sans-serif; }

.chat-toggle__text { font-size: 14px; font-weight: 600; letter-spacing: .02em; }
.chat-toggle__dot {
    width: 8px; height: 8px; border-radius: 50%;
    background: var(--sec); flex-shrink: 0;
    animation: chat-dot-blink 1.6s ease-in-out infinite;
}

@keyframes chat-pulse {
    0%   { box-shadow: 0 6px 24px rgba(4,64,104,.4), 0 0 0 0    rgba(79,219,120,.55); }
    70%  { box-shadow: 0 6px 24px rgba(4,64,104,.4), 0 0 0 16px rgba(79,219,120,0);  }
    100% { box-shadow: 0 6px 24px rgba(4,64,104,.4), 0 0 0 0    rgba(79,219,120,0);  }
}
@keyframes chat-dot-blink {
    0%, 100% { opacity: 1; transform: scale(1); }
    50%      { opacity: .35; transform: scale(.6); }
}

/* ---- Panel ---- */
.chat-panel {
    position: absolute; bottom: calc(100% + 14px); right: 0;
    width: 368px; min-width: 300px;
    background: #fff; border-radius: 20px;
    box-shadow: 0 16px 56px rgba(0,0,0,.18), 0 2px 8px rgba(0,0,0,.07);
    display: flex; flex-direction: column; overflow: hidden;
    max-height: 540px;
    /* Hidden state — CSS-driven, no display:none flicker */
    visibility: hidden; opacity: 0;
    transform: translateY(14px) scale(0.97);
    pointer-events: none;
    transition: opacity .22s ease, transform .22s ease, visibility 0s linear .22s;
}
.chat-panel.is-open {
    visibility: visible; opacity: 1;
    transform: translateY(0) scale(1); pointer-events: auto;
    transition: opacity .22s ease, transform .22s ease, visibility 0s linear 0s;
}
[dir="rtl"] .chat-panel { right: auto; left: 0; }
.chat-panel--maximized { width: 580px; max-height: 680px; }

/* ---- Header ---- */
.chat-panel__header {
    background: linear-gradient(135deg, #044068 0%, #065c94 100%);
    color: #fff; padding: 14px 16px;
    display: flex; align-items: center; gap: 11px;
    flex-shrink: 0; cursor: move; user-select: none;
}
.chat-panel__avatar {
    width: 42px; height: 42px; border-radius: 50%;
    background: rgba(255,255,255,.12);
    border: 2px solid rgba(79,219,120,.55);
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}
.chat-panel__header-info { flex: 1; min-width: 0; }
.chat-panel__header-name {
    display: block; font-weight: 700; font-size: 15px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    letter-spacing: .01em;
}
.chat-panel__header-sub {
    display: flex; align-items: center; gap: 5px;
    font-size: 11px; opacity: .72; margin-top: 2px;
}
.chat-panel__header-dot {
    width: 7px; height: 7px; border-radius: 50%;
    background: var(--sec); flex-shrink: 0;
    animation: chat-dot-blink 1.6s ease-in-out infinite;
}

/* Window controls */
.chat-panel__controls { display: flex; align-items: center; gap: 2px; flex-shrink: 0; }
.chat-ctrl-btn {
    background: none; border: none; color: rgba(255,255,255,.65);
    cursor: pointer; width: 28px; height: 28px;
    display: flex; align-items: center; justify-content: center;
    border-radius: 6px; transition: background .15s, color .15s;
}
.chat-ctrl-btn:hover { background: rgba(255,255,255,.18); color: #fff; }

/* ---- Messages area ---- */
.chat-messages {
    flex: 1; overflow-y: auto; padding: 16px 14px 10px;
    display: flex; flex-direction: column; gap: 10px;
    min-height: 200px; scroll-behavior: smooth;
}

/* Message wrappers (for avatar alignment) */
.chat-msg-wrapper { display: flex; align-items: flex-end; gap: 8px; }
.chat-msg-wrapper--user  { flex-direction: row-reverse; }
[dir="rtl"] .chat-msg-wrapper--user      { flex-direction: row; }
[dir="rtl"] .chat-msg-wrapper--assistant { flex-direction: row-reverse; }

/* Fahman avatar bubble */
.chat-msg-avatar {
    width: 28px; height: 28px; border-radius: 50%;
    background: var(--primary);
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0; align-self: flex-end;
}

/* Message bubbles */
.chat-msg {
    max-width: 78%; padding: 10px 14px;
    border-radius: 16px; font-size: 14px; line-height: 1.58;
    word-wrap: break-word; white-space: pre-wrap;
}
.chat-msg--user {
    background: var(--primary); color: #fff;
    border-bottom-right-radius: 4px;
}
[dir="rtl"] .chat-msg--user { border-bottom-right-radius: 16px; border-bottom-left-radius: 4px; }
.chat-msg--assistant {
    background: #f0f4f8; color: var(--black);
    border-bottom-left-radius: 4px;
}
[dir="rtl"] .chat-msg--assistant { border-bottom-left-radius: 16px; border-bottom-right-radius: 4px; }

/* Markdown in assistant messages */
.chat-msg--assistant p { margin: 4px 0; }
.chat-msg--assistant p:first-child { margin-top: 0; }
.chat-msg--assistant p:last-child  { margin-bottom: 0; }
.chat-msg--assistant ul,
.chat-msg--assistant ol  { margin: 5px 0 5px 18px; padding: 0; }
[dir="rtl"] .chat-msg--assistant ul,
[dir="rtl"] .chat-msg--assistant ol { margin: 5px 18px 5px 0; }
.chat-msg--assistant li  { margin-bottom: 3px; }
.chat-msg--assistant strong { color: #033455; }

/* Order card */
.chat-msg--order {
    background: linear-gradient(135deg, #eaf9ef 0%, #f0fdf4 100%);
    border: 1px solid #a7e8bc;
    border-radius: 14px; padding: 12px 14px;
    font-size: 13px; max-width: 100%; width: 100%;
    border-bottom-left-radius: 4px;
}
.chat-msg--order strong { color: var(--primary); display: block; margin-bottom: 5px; font-size: 14px; }

/* Prescription image thumbnail */
.chat-msg--image {
    background: none; padding: 0; border-bottom-right-radius: 16px;
}
[dir="rtl"] .chat-msg--image { border-bottom-left-radius: 16px; border-bottom-right-radius: 16px; }
.chat-msg--image img {
    max-width: 220px; max-height: 220px; width: 100%;
    border-radius: 14px; display: block; object-fit: cover;
    border: 1px solid rgba(0,0,0,.08);
}

/* ---- Suggestion chips (injected into messages area after greeting) ---- */
.chat-suggestions-inline {
    display: flex; flex-wrap: wrap; gap: 7px;
    padding: 4px 0 6px;
}
.chat-chip {
    background: #fff; border: 1.5px solid #d0dce8;
    border-radius: 20px; padding: 7px 13px;
    font-size: 13px; font-family: inherit; font-weight: 500;
    color: var(--primary); cursor: pointer;
    transition: background .15s, border-color .15s, transform .12s, box-shadow .12s;
    white-space: nowrap;
}
.chat-chip:hover {
    background: #eaf3fb; border-color: var(--primary);
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(4,64,104,.12);
}
.chat-chip:active { transform: translateY(0); }

/* ---- Typing indicator (3 animated dots) ---- */
.chat-typing {
    display: inline-flex; align-items: center; gap: 4px;
    padding: 11px 15px; background: #f0f4f8;
    border-radius: 16px; border-bottom-left-radius: 4px;
}
[dir="rtl"] .chat-typing { border-bottom-left-radius: 16px; border-bottom-right-radius: 4px; }
.chat-typing span {
    width: 7px; height: 7px; border-radius: 50%; background: #93a8be;
    display: inline-block; animation: chat-bounce 1.3s ease-in-out infinite;
}
.chat-typing span:nth-child(2) { animation-delay: .18s; }
.chat-typing span:nth-child(3) { animation-delay: .36s; }
@keyframes chat-bounce {
    0%, 80%, 100% { transform: translateY(0); opacity: .5; }
    40%           { transform: translateY(-6px); opacity: 1; }
}

/* ---- Input row ---- */
.chat-input-row {
    display: flex; align-items: center; gap: 8px;
    padding: 12px 14px;
    border-top: 1px solid #eef0f2; flex-shrink: 0; background: #fff;
}
[dir="rtl"] .chat-input-row { font-family: 'Segoe UI', Arial, sans-serif; }
.chat-input {
    flex: 1; border: 1.5px solid #dde3ea;
    border-radius: 22px; padding: 10px 16px;
    font-size: 14px; font-family: inherit; outline: none; min-width: 0;
    background: #f8f9fb; color: var(--black);
    transition: border-color .18s, background .18s;
}
.chat-input:focus { border-color: var(--primary); background: #fff; }
[dir="rtl"] .chat-input { font-family: 'Segoe UI', Arial, sans-serif; }

.chat-attach-btn {
    width: 34px; height: 34px; border-radius: 50%;
    background: none; color: #93a8be;
    border: none; cursor: pointer; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    transition: color .18s, background .18s;
}
.chat-attach-btn:hover { color: var(--primary); background: #eaf3fb; }
.chat-attach-btn:disabled { opacity: .4; cursor: default; }

.chat-send-btn {
    width: 40px; height: 40px; border-radius: 50%;
    background: var(--primary); color: #fff;
    border: none; cursor: pointer; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    transition: background .18s, transform .15s, box-shadow .18s;
    box-shadow: 0 2px 8px rgba(4,64,104,.25);
}
.chat-send-btn:hover { background: #033455; transform: scale(1.08); box-shadow: 0 4px 14px rgba(4,64,104,.35); }
.chat-send-btn:active { transform: scale(0.96); }
.chat-send-btn:disabled { opacity: .5; cursor: default; transform: none; box-shadow: none; }

/* ---- Resize grip ---- */
.chat-resize-handle {
    position: absolute; bottom: 0; right: 0;
    width: 20px; height: 20px; cursor: se-resize;
    opacity: .22; display: flex;
    align-items: flex-end; justify-content: flex-end; padding: 4px;
    transition: opacity .15s;
}
.chat-resize-handle:hover { opacity: .55; }
[dir="rtl"] .chat-resize-handle { right: auto; left: 0; cursor: sw-resize; }

/* ---- Mobile ---- */
@media (max-width: 640px) {
    .chat-widget { bottom: 20px; right: 20px; }
    [dir="rtl"] .chat-widget { right: auto; left: 20px; }

    /* Round FAB */
    .chat-toggle {
        width: 60px; height: 60px; padding: 0;
        border-radius: 50%; justify-content: center;
        box-shadow: 0 6px 22px rgba(4,64,104,.45), 0 0 0 0 rgba(79,219,120,.55);
    }
    .chat-toggle__text { display: none; }
    .chat-toggle__dot  { display: none; }

    /* Speech bubble hint — fades in then out once */
    .chat-toggle::before {
        content: attr(data-hint);
        position: absolute; bottom: calc(100% + 10px); right: 0;
        background: var(--primary); color: #fff;
        font-size: 12px; font-weight: 600;
        padding: 7px 12px; border-radius: 14px 14px 0 14px;
        white-space: nowrap; pointer-events: none;
        animation: chat-hint 5s ease forwards;
    }
    [dir="rtl"] .chat-toggle::before {
        right: auto; left: 0;
        border-radius: 14px 14px 14px 0;
    }

    /* Full-screen panel on mobile */
    .chat-panel,
    .chat-panel--maximized {
        position: fixed; top: 0; left: 0; right: 0; bottom: 0;
        width: 100% !important; max-height: 100% !important;
        border-radius: 0; min-height: unset;
    }
    [dir="rtl"] .chat-panel { left: 0; right: 0; }

    .chat-resize-handle { display: none; }
    #chat-btn-maxrestore { display: none; }
    .chat-panel__avatar { width: 36px; height: 36px; }
}

@keyframes chat-hint {
    0%   { opacity: 0; transform: translateY(6px) scale(.9); }
    15%  { opacity: 1; transform: translateY(0) scale(1); }
    75%  { opacity: 1; }
    100% { opacity: 0; }
}
