/*
 * AlloTabibi chatbot (index.html). Scoped to #chatContainer / #chatToggle so it
 * outranks the older inline chat rules in index.html without editing them.
 * Palette: site blues (#2563eb / #1e40af) + hero gradient (#667eea → #764ba2).
 */

#chatContainer,
#chatToggle {
    --chat-grad: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    --chat-blue: linear-gradient(135deg, #2563eb 0%, #1e40af 100%);
    --chat-ease: cubic-bezier(0.22, 1, 0.36, 1);
    --chat-surface: #f8fafc;
    --chat-text: #1f2937;
    --chat-muted: #64748b;
    font-family: 'Inter', 'Segoe UI', Tahoma, sans-serif;
}

/* Neutralise decorations from the old inline styles */
#chatContainer::before,
#chatContainer .doctor-icon::after,
#chatToggle::before,
#chatContainer .message-dots {
    content: none;
    display: none;
}

/* ------------------------------------------------------------------ Panel */
#chatContainer {
    position: fixed;
    right: 24px;
    left: auto;
    bottom: 104px;
    width: 380px;
    max-width: calc(100vw - 32px);
    height: min(600px, calc(100vh - 140px));
    height: min(600px, calc(100dvh - 140px));
    margin: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: #ffffff;
    border: 1px solid rgba(37, 99, 235, 0.12);
    border-radius: 24px;
    box-shadow: 0 2px 6px rgba(15, 23, 42, 0.06), 0 30px 70px -20px rgba(37, 99, 235, 0.45);
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transform: translateY(18px) scale(0.94);
    transform-origin: bottom right;
    transition: opacity 0.3s ease, transform 0.45s var(--chat-ease), visibility 0s linear 0.45s;
}

#chatContainer.open {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transform: none;
    transition: opacity 0.3s ease, transform 0.45s var(--chat-ease), visibility 0s;
}

/* ----------------------------------------------------------------- Header */
#chatContainer .chat-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px 16px 16px 18px;
    background: var(--chat-grad);
    color: #fff;
    border: 0;
    position: relative;
    flex-shrink: 0;
}

#chatContainer .doctor-icon {
    position: relative;
    width: 44px;
    height: 44px;
    flex-shrink: 0;
    border-radius: 14px;
    display: grid;
    place-items: center;
    font-size: 1.15rem;
    color: #fff;
    background: rgba(255, 255, 255, 0.18);
    border: 1px solid rgba(255, 255, 255, 0.35);
    box-shadow: none;
    animation: none;
}

/* online dot */
#chatContainer .doctor-icon::before {
    content: "";
    position: absolute;
    right: -3px;
    bottom: -3px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: #22c55e;
    border: 2px solid #7a6fd8;
    animation: chat-online 2.4s ease-in-out infinite;
}

@keyframes chat-online {
    0%, 100% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.55); }
    50%      { box-shadow: 0 0 0 5px rgba(34, 197, 94, 0); }
}

#chatContainer .header-text {
    flex: 1;
    min-width: 0;
}

#chatContainer .header-text h3 {
    margin: 0;
    font-size: 1rem;
    font-weight: 700;
    line-height: 1.25;
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#chatContainer .header-text p {
    margin: 2px 0 0;
    font-size: 0.8rem;
    color: rgba(255, 255, 255, 0.85);
    opacity: 1;
}

#chatContainer .chat-close {
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    border: 0;
    border-radius: 50%;
    display: grid;
    place-items: center;
    color: #fff;
    background: rgba(255, 255, 255, 0.16);
    cursor: pointer;
    transition: background-color 0.2s ease, transform 0.3s var(--chat-ease);
}

#chatContainer .chat-close:hover {
    background: rgba(255, 255, 255, 0.28);
    transform: rotate(90deg);
}

#chatContainer .chat-close:focus-visible {
    outline: 3px solid #fff;
    outline-offset: 2px;
}

/* --------------------------------------------------------------- Messages */
#chatContainer .messages-section {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 18px 16px;
    background: var(--chat-surface);
    scroll-behavior: smooth;
}

#chatContainer .messages-section::-webkit-scrollbar {
    width: 6px;
}

#chatContainer .messages-section::-webkit-scrollbar-track {
    background: transparent;
}

#chatContainer .messages-section::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 6px;
}

#chatContainer .message {
    max-width: 82%;
    width: fit-content;
    margin: 0;
    padding: 10px 14px;
    font-size: 0.92rem;
    line-height: 1.55;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    border-radius: 18px;
    box-shadow: 0 1px 2px rgba(15, 23, 42, 0.05);
    animation: chat-pop 0.45s var(--chat-ease) both;
    position: static;
}

#chatContainer .message.doctor {
    align-self: flex-start;
    color: var(--chat-text);
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-bottom-left-radius: 6px;
}

#chatContainer .message.user {
    align-self: flex-end;
    color: #ffffff;
    background: var(--chat-blue);
    border: 0;
    border-bottom-right-radius: 6px;
    box-shadow: 0 8px 18px -10px rgba(37, 99, 235, 0.7);
}

#chatContainer .message.is-error {
    color: #991b1b;
    background: #fef2f2;
    border-color: #fecaca;
}

#chatContainer .message strong {
    font-weight: 700;
}

@keyframes chat-pop {
    from { opacity: 0; transform: translateY(8px) scale(0.96); }
    to   { opacity: 1; transform: none; }
}

/* Typing indicator: looks like an assistant bubble */
#chatContainer .typing-indicator {
    align-self: flex-start;
    padding: 12px 16px;
    margin: 0;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-radius: 18px;
    border-bottom-left-radius: 6px;
    animation: chat-pop 0.35s var(--chat-ease) both;
}

#chatContainer .typing-dots {
    display: flex;
    gap: 5px;
}

#chatContainer .typing-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #94a3b8;
    animation: chat-typing 1.2s ease-in-out infinite;
}

#chatContainer .typing-dot:nth-child(2) { animation-delay: 0.15s; }
#chatContainer .typing-dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes chat-typing {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
    30%           { transform: translateY(-5px); opacity: 1; background: #667eea; }
}

/* ------------------------------------------------------------------ Input */
#chatContainer .input-section {
    flex-shrink: 0;
    padding: 12px 14px 10px;
    background: #ffffff;
    border-top: 1px solid #eef2f7;
}

#chatContainer .input-container {
    position: relative;
    display: flex;
    align-items: center;
    gap: 8px;
}

#chatContainer .message-input {
    flex: 1;
    min-width: 0;
    height: 46px;
    padding: 0 16px;
    border: 1.5px solid #e2e8f0;
    border-radius: 999px;
    background: #f8fafc;
    color: var(--chat-text);
    font: inherit;
    font-size: 0.92rem;
    box-shadow: none;
    transition: border-color 0.2s ease, box-shadow 0.2s ease, background-color 0.2s ease;
}

#chatContainer .message-input::placeholder {
    color: #94a3b8;
}

#chatContainer .message-input:focus {
    outline: none;
    background: #ffffff;
    border-color: #2563eb;
    box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.14);
}

#chatContainer .send-button {
    position: static;
    flex-shrink: 0;
    width: 46px;
    height: 46px;
    border: 0;
    border-radius: 50%;
    display: grid;
    place-items: center;
    color: #fff;
    font-size: 1rem;
    background: var(--chat-blue);
    box-shadow: 0 10px 20px -10px rgba(37, 99, 235, 0.8);
    cursor: pointer;
    transform: none;
    transition: transform 0.25s var(--chat-ease), opacity 0.2s ease, box-shadow 0.25s ease;
}

#chatContainer .send-button:hover {
    transform: translateY(-2px) rotate(-8deg);
}

#chatContainer .send-button:active {
    transform: scale(0.94);
}

#chatContainer .send-button:focus-visible {
    outline: 3px solid #2563eb;
    outline-offset: 3px;
}

/* Send looks inactive until something is typed */
#chatContainer .message-input:placeholder-shown + .send-button {
    opacity: 0.45;
    box-shadow: none;
}

#chatContainer .chat-disclaimer {
    margin: 8px 4px 0;
    font-size: 0.72rem;
    line-height: 1.4;
    text-align: center;
    color: var(--chat-muted);
}

/* ----------------------------------------------------------- Toggle button */
#chatToggle {
    position: fixed;
    right: 24px;
    left: auto;
    bottom: 24px;
    width: 62px;
    height: 62px;
    border: 0;
    border-radius: 50%;
    display: grid;
    place-items: center;
    font-size: 1.5rem;
    color: #fff;
    background: var(--chat-grad);
    box-shadow: 0 16px 34px -12px rgba(118, 75, 162, 0.75);
    cursor: pointer;
    z-index: 1001;
    transition: transform 0.35s var(--chat-ease), box-shadow 0.3s ease;
}

#chatToggle:hover {
    transform: translateY(-3px) scale(1.04);
    box-shadow: 0 22px 40px -12px rgba(118, 75, 162, 0.85);
}

#chatToggle i {
    transition: transform 0.4s var(--chat-ease);
}

#chatToggle.open {
    background: var(--chat-blue);
}

#chatToggle.open i {
    transform: rotate(90deg);
}

#chatToggle:focus-visible {
    outline: 3px solid #2563eb;
    outline-offset: 4px;
}

/* back-to-top sits in the panel's spot; hide it while the chat is open */
html.chat-is-open .home-to-top {
    opacity: 0 !important;
    visibility: hidden !important;
}

/* -------------------------------------------------------------------- RTL */
[dir="rtl"] #chatContainer {
    right: auto;
    left: 24px;
    transform-origin: bottom left;
}

[dir="rtl"] #chatToggle {
    right: auto;
    left: 24px;
}

[dir="rtl"] #chatContainer .chat-header {
    padding: 16px 18px 16px 16px;
}

[dir="rtl"] #chatContainer .doctor-icon::before {
    right: auto;
    left: -3px;
}

[dir="rtl"] #chatContainer .message.doctor {
    border-bottom-left-radius: 18px;
    border-bottom-right-radius: 6px;
}

[dir="rtl"] #chatContainer .message.user {
    border-bottom-right-radius: 18px;
    border-bottom-left-radius: 6px;
}

[dir="rtl"] #chatContainer .typing-indicator {
    border-bottom-left-radius: 18px;
    border-bottom-right-radius: 6px;
}

[dir="rtl"] #chatContainer .send-button i {
    transform: scaleX(-1);
}

/* ------------------------------------------------------ Phones: full screen */
@media (max-width: 560px) {
    #chatContainer,
    [dir="rtl"] #chatContainer {
        inset: 0;
        width: 100%;
        max-width: none;
        height: 100vh;
        height: 100dvh;
        border-radius: 0;
        border: 0;
        transform: translateY(24px);
        transform-origin: bottom center;
    }

    #chatContainer.open {
        transform: none;
    }

    #chatContainer .chat-header {
        padding-top: calc(14px + env(safe-area-inset-top, 0px));
    }

    #chatContainer .input-section {
        padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    }

    #chatContainer .message {
        max-width: 88%;
    }

    /* 16px avoids iOS zooming into the field */
    #chatContainer .message-input {
        font-size: 16px;
    }

    #chatToggle,
    [dir="rtl"] #chatToggle {
        bottom: 18px;
        width: 56px;
        height: 56px;
        font-size: 1.35rem;
    }

    #chatToggle { right: 16px; }
    [dir="rtl"] #chatToggle { left: 16px; right: auto; }

    /* the header has its own close button; the toggle would cover the input */
    html.chat-is-open #chatToggle {
        opacity: 0;
        visibility: hidden;
        transform: scale(0.6);
    }

    html.chat-is-open,
    html.chat-is-open body {
        overflow: hidden;
    }
}

/* --------------------------------------------------------- Reduced motion */
@media (prefers-reduced-motion: reduce) {
    #chatContainer,
    #chatContainer.open,
    #chatToggle,
    #chatToggle i,
    #chatContainer .send-button,
    #chatContainer .chat-close {
        transition: opacity 0.2s ease, visibility 0s !important;
        transform: none !important;
    }

    #chatContainer .message,
    #chatContainer .typing-indicator,
    #chatContainer .doctor-icon::before {
        animation: none !important;
    }

    #chatContainer .typing-dot {
        animation-duration: 2.4s !important;
    }
}
