/* =============================================
   IJP Chatbot Widget Styles
   ============================================= */

:root {
    --ijp-fab-bottom: max(20px, env(safe-area-inset-bottom, 0px));
    --ijp-fab-right: max(20px, env(safe-area-inset-right, 0px));
    --ijp-chat-toggle-height: 50px;
    --ijp-fab-stack-gap: 12px;
}

/* Lift Back to Top above the chatbot so they never overlap */
#backToTop {
    bottom: calc(var(--ijp-fab-bottom) + var(--ijp-chat-toggle-height) + var(--ijp-fab-stack-gap));
    right: var(--ijp-fab-right);
    z-index: 9998;
}

#ijp-chat-toggle {
    position: fixed;
    bottom: var(--ijp-fab-bottom);
    right: var(--ijp-fab-right);
    height: var(--ijp-chat-toggle-height);
    padding: 0 18px;
    border-radius: 25px;
    background: linear-gradient(145deg, #ffffff 0%, #d8d8d8 30%, #f5f5f5 60%, #c4c4c4 100%);
    border: 1px solid #b0b0b0;
    cursor: pointer;
    box-shadow: 0 4px 18px rgba(0, 0, 0, 0.18),
                0 1px 0 rgba(255, 255, 255, 0.9) inset,
                0 -1px 0 rgba(0, 0, 0, 0.08) inset;
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease, opacity 0.2s ease;
    color: #2c3e50;
    font-size: 18px;
    max-width: calc(100vw - 40px);
}

#ijp-chat-toggle:hover {
    transform: scale(1.04);
    background: linear-gradient(145deg, #ffffff 0%, #e8e8e8 30%, #fafafa 60%, #d0d0d0 100%);
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.22),
                0 1px 0 rgba(255, 255, 255, 0.95) inset;
}

.chat-toggle-label {
    font-size: 13px;
    font-weight: 700;
    letter-spacing: 0.4px;
    color: #2c3e50;
    line-height: 1;
}

#ijp-chat-toggle .bi-chat-dots-fill {
    color: #1a6fc4;
}

#ijp-chat-toggle .chat-badge {
    position: absolute;
    top: -3px;
    right: -3px;
    width: 14px;
    height: 14px;
    background: #e74c3c;
    border-radius: 50%;
    border: 2px solid #fff;
    display: none;
}

#ijp-chat-panel {
    --ijp-chat-gap: 16px;
    --ijp-chat-toggle-space: 76px;
    position: fixed;
    bottom: calc(var(--ijp-chat-toggle-space) + env(safe-area-inset-bottom, 0px));
    right: max(var(--ijp-chat-gap), env(safe-area-inset-right, 0px));
    width: min(380px, calc(100vw - (var(--ijp-chat-gap) * 2)));
    height: min(560px, calc(100dvh - var(--ijp-chat-toggle-space) - var(--ijp-chat-gap) - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)));
    max-width: calc(100vw - (var(--ijp-chat-gap) * 2));
    max-height: calc(100dvh - var(--ijp-chat-toggle-space) - var(--ijp-chat-gap) - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.18);
    z-index: 9998;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transform: scale(0.85) translateY(20px);
    transform-origin: bottom right;
    opacity: 0;
    pointer-events: none;
    transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.2s ease;
}

#ijp-chat-panel.ijp-chat-open {
    transform: scale(1) translateY(0);
    opacity: 1;
    pointer-events: all;
}

/* Header */
.ijp-chat-header {
    background: linear-gradient(135deg, #1a6fc4, #0d4f8c);
    color: #fff;
    padding: 12px 14px;
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
    min-height: 56px;
}

.ijp-chat-header .ijp-chat-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255,255,255,0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    flex-shrink: 0;
}

.ijp-chat-header .ijp-chat-title {
    flex: 1;
    min-width: 0;
}

.ijp-chat-header .ijp-chat-title strong {
    display: block;
    font-size: 14px;
    font-weight: 600;
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ijp-chat-header .ijp-chat-title span {
    display: block;
    font-size: 11px;
    opacity: 0.85;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ijp-chat-header .ijp-chat-actions {
    display: flex;
    gap: 4px;
    flex-shrink: 0;
}

.ijp-chat-header .ijp-chat-actions button {
    background: none;
    border: none;
    color: rgba(255,255,255,0.8);
    cursor: pointer;
    width: 32px;
    height: 32px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    transition: background 0.15s;
}

.ijp-chat-header .ijp-chat-actions button:hover {
    background: rgba(255,255,255,0.15);
    color: #fff;
}

/* Messages area */
.ijp-chat-messages {
    flex: 1 1 auto;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: 14px 14px 8px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    scroll-behavior: smooth;
}

.ijp-chat-messages::-webkit-scrollbar {
    width: 4px;
}

.ijp-chat-messages::-webkit-scrollbar-track {
    background: #f1f1f1;
}

.ijp-chat-messages::-webkit-scrollbar-thumb {
    background: #c8d8ec;
    border-radius: 4px;
}

/* Message bubbles */
.ijp-msg {
    display: flex;
    align-items: flex-end;
    gap: 7px;
    max-width: min(88%, 320px);
    animation: ijpMsgIn 0.2s ease;
}

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

.ijp-msg.ijp-msg-user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.ijp-msg.ijp-msg-bot {
    align-self: flex-start;
}

.ijp-msg-bubble {
    padding: 9px 13px;
    border-radius: 14px;
    font-size: 13.5px;
    line-height: 1.5;
    word-break: break-word;
    overflow-wrap: anywhere;
    white-space: pre-wrap;
    min-width: 0;
}

.ijp-msg-user .ijp-msg-bubble {
    background: #1a6fc4;
    color: #fff;
    border-bottom-right-radius: 4px;
}

.ijp-msg-bot .ijp-msg-bubble {
    background: #f0f4f9;
    color: #2c3e50;
    border-bottom-left-radius: 4px;
    border: 1px solid #e1eaf4;
}

.ijp-msg-icon {
    width: 26px;
    height: 26px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    flex-shrink: 0;
}

.ijp-msg-user .ijp-msg-icon {
    background: #d0e4f7;
    color: #1a6fc4;
}

.ijp-msg-bot .ijp-msg-icon {
    background: #e8f0fa;
    color: #1a6fc4;
}

/* Typing indicator */
.ijp-typing {
    display: flex;
    align-items: center;
    gap: 7px;
    align-self: flex-start;
}

.ijp-typing-dots {
    background: #f0f4f9;
    border: 1px solid #e1eaf4;
    border-radius: 14px;
    border-bottom-left-radius: 4px;
    padding: 10px 14px;
    display: flex;
    gap: 4px;
    align-items: center;
}

.ijp-typing-dots span {
    width: 7px;
    height: 7px;
    background: #90a8c3;
    border-radius: 50%;
    animation: ijpDot 1.2s infinite;
    display: inline-block;
}

.ijp-typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.ijp-typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes ijpDot {
    0%, 80%, 100% { transform: scale(0.7); opacity: 0.5; }
    40%            { transform: scale(1);   opacity: 1; }
}

/* Quick suggestions */
.ijp-suggestions {
    padding: 6px 14px 8px;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    flex-shrink: 0;
    max-width: 100%;
}

.ijp-suggestion-btn {
    background: #eef4fc;
    border: 1px solid #c5d9f0;
    border-radius: 20px;
    padding: 5px 11px;
    font-size: 12px;
    color: #1a6fc4;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
    white-space: normal;
    text-align: left;
    max-width: 100%;
}

.ijp-suggestion-btn:hover {
    background: #d6e8f9;
    border-color: #1a6fc4;
}

/* Input area */
.ijp-chat-footer {
    padding: 10px 12px calc(12px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid #e8edf3;
    flex-shrink: 0;
    background: #fff;
}

.ijp-chat-input-row {
    display: flex;
    gap: 8px;
    align-items: flex-end;
    width: 100%;
}

#ijp-chat-input {
    flex: 1;
    min-width: 0;
    border: 1.5px solid #c8d8ec;
    border-radius: 22px;
    padding: 9px 14px;
    font-size: 16px; /* prevents iOS zoom on focus */
    resize: none;
    outline: none;
    max-height: 90px;
    min-height: 40px;
    line-height: 1.4;
    transition: border-color 0.15s;
    font-family: inherit;
}

#ijp-chat-input:focus {
    border-color: #1a6fc4;
}

#ijp-chat-send {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: #1a6fc4;
    border: none;
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
    transition: background 0.15s, transform 0.1s;
}

#ijp-chat-send:hover {
    background: #0d4f8c;
    transform: scale(1.05);
}

#ijp-chat-send:disabled {
    background: #b0c8e4;
    cursor: not-allowed;
    transform: none;
}

/* Welcome card */
.ijp-welcome {
    background: linear-gradient(135deg, #eef4fc, #ddeaf8);
    border-radius: 12px;
    padding: 14px 16px;
    font-size: 13px;
    color: #2c3e50;
    line-height: 1.55;
    border: 1px solid #c8d8f0;
}

.ijp-welcome strong {
    display: block;
    color: #1a6fc4;
    font-size: 14px;
    margin-bottom: 4px;
}

/* Support prompt buttons (low confidence) */
.ijp-support-prompt {
    margin: 8px 0;
    max-width: 100%;
    align-self: stretch;
    padding-left: 33px;
    box-sizing: border-box;
}

.ijp-support-btns {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.ijp-support-btn {
    padding: 8px 18px;
    border-radius: 20px;
    border: none;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s ease, transform 0.15s ease;
}

.ijp-support-yes {
    background: #1a6fc4;
    color: #fff;
}

.ijp-support-yes:hover {
    background: #0d4f8c;
}

.ijp-support-no {
    background: #e8eef4;
    color: #2c3e50;
}

.ijp-support-no:hover {
    background: #d0dbe8;
}

.ijp-support-btn:disabled {
    opacity: 0.65;
    cursor: not-allowed;
    transform: none;
}

.ijp-support-loading {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 10px;
    padding: 8px 12px;
    background: #f0f4f9;
    border: 1px solid #e1eaf4;
    border-radius: 12px;
    color: #2c3e50;
    font-size: 12.5px;
}

.ijp-support-spinner {
    width: 14px;
    height: 14px;
    border: 2px solid #c8d8ec;
    border-top-color: #1a6fc4;
    border-radius: 50%;
    animation: ijpSupportSpin 0.7s linear infinite;
    flex-shrink: 0;
}

@keyframes ijpSupportSpin {
    to { transform: rotate(360deg); }
}

.ijp-support-loading-text {
    line-height: 1.3;
}

.ijp-guest-support-form {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 8px;
    width: 100%;
    max-width: 100%;
}

.ijp-guest-support-form label {
    font-size: 12px;
    color: #2c3e50;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.ijp-guest-support-form input {
    padding: 8px 10px;
    border: 1px solid #c8d8f0;
    border-radius: 8px;
    font-size: 16px; /* prevents iOS zoom */
    width: 100%;
    box-sizing: border-box;
}

.ijp-guest-support-error {
    margin-top: 4px;
    padding: 8px 10px;
    background: #fdebec;
    border: 1px solid #f5c2c7;
    border-radius: 8px;
    color: #842029;
    font-size: 12.5px;
    line-height: 1.4;
}

/* Prevent page scroll behind open chat on small screens */
body.ijp-chat-open-mobile {
    overflow: hidden;
    touch-action: none;
}

body.ijp-chat-open-mobile #backToTop {
    opacity: 0;
    pointer-events: none;
}

/* ---------- Responsive breakpoints ---------- */

/* Large tablets / small desktops */
@media (max-width: 992px) {
    #ijp-chat-panel {
        width: min(360px, calc(100vw - 24px));
        height: min(520px, calc(100dvh - 100px));
    }
}

/* Tablets / large phones */
@media (max-width: 768px) {
    :root {
        --ijp-fab-bottom: max(16px, env(safe-area-inset-bottom, 0px));
        --ijp-fab-right: max(16px, env(safe-area-inset-right, 0px));
        --ijp-chat-toggle-height: 46px;
        --ijp-fab-stack-gap: 10px;
    }

    #ijp-chat-toggle {
        padding: 0 14px;
    }

    #ijp-chat-panel {
        --ijp-chat-gap: 12px;
        --ijp-chat-toggle-space: 70px;
        width: min(400px, calc(100vw - 24px));
        right: max(12px, env(safe-area-inset-right, 0px));
        bottom: calc(var(--ijp-chat-toggle-space) + env(safe-area-inset-bottom, 0px));
        height: min(560px, calc(100dvh - 90px));
        max-height: calc(100dvh - 90px);
    }

    .ijp-msg {
        max-width: 92%;
    }
}

/* Phones — full-screen sheet */
@media (max-width: 576px) {
    :root {
        --ijp-fab-bottom: max(14px, env(safe-area-inset-bottom, 0px));
        --ijp-fab-right: max(14px, env(safe-area-inset-right, 0px));
        --ijp-chat-toggle-height: 46px;
        --ijp-fab-stack-gap: 10px;
    }

    #ijp-chat-toggle {
        padding: 0 14px;
        font-size: 16px;
    }

    .chat-toggle-label {
        font-size: 12px;
    }

    #ijp-chat-panel {
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        width: 100%;
        max-width: 100%;
        height: 100dvh;
        max-height: 100dvh;
        border-radius: 0;
        transform-origin: bottom center;
        box-shadow: none;
    }

    body.ijp-chat-open-mobile #ijp-chat-toggle {
        opacity: 0;
        pointer-events: none;
        transform: scale(0.9);
    }

    .ijp-chat-header {
        padding: 12px 14px;
        padding-top: max(12px, env(safe-area-inset-top, 0px));
        border-radius: 0;
    }

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

    .ijp-msg {
        max-width: 94%;
    }

    .ijp-support-prompt {
        padding-left: 0;
        margin-left: 0;
    }

    .ijp-suggestions {
        padding-left: 12px;
        padding-right: 12px;
    }
}

/* Very small phones */
@media (max-width: 360px) {
    .ijp-chat-header .ijp-chat-avatar {
        width: 32px;
        height: 32px;
        font-size: 15px;
    }

    .ijp-msg-bubble {
        font-size: 13px;
        padding: 8px 11px;
    }

    .ijp-suggestion-btn {
        font-size: 11px;
        padding: 5px 9px;
    }
}

/* Short viewports (landscape phones, short laptop heights) */
@media (max-height: 620px) {
    #ijp-chat-panel {
        height: calc(100dvh - 88px);
        max-height: calc(100dvh - 88px);
    }

    .ijp-welcome {
        padding: 10px 12px;
        font-size: 12.5px;
    }

    .ijp-suggestions {
        max-height: 72px;
        overflow-y: auto;
    }
}

@media (max-height: 480px) {
    #ijp-chat-panel {
        top: 0;
        right: 0;
        bottom: 0;
        left: auto;
        width: min(420px, 100vw);
        max-width: 100vw;
        height: 100dvh;
        max-height: 100dvh;
        border-radius: 0;
        transform-origin: center right;
    }

    :root {
        --ijp-fab-bottom: max(10px, env(safe-area-inset-bottom, 0px));
        --ijp-fab-right: max(10px, env(safe-area-inset-right, 0px));
    }

    body.ijp-chat-open-mobile #ijp-chat-toggle {
        opacity: 0;
        pointer-events: none;
    }

    .ijp-chat-header {
        min-height: 48px;
        padding: 8px 12px;
    }

    .ijp-suggestions {
        display: none;
    }
}

/* Fallback when dvh is unsupported */
@supports not (height: 100dvh) {
    #ijp-chat-panel {
        height: min(560px, calc(100vh - 110px));
        max-height: calc(100vh - 110px);
    }

    @media (max-width: 576px) {
        #ijp-chat-panel {
            height: 100vh;
            max-height: 100vh;
        }
    }
}
