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

.chatbot-widget {
    position: fixed;
    bottom: 20px;
    left: 20px;
    z-index: 9999;
    direction: rtl;
    /* Enhanced safe area support for notched iPhones */
    bottom: max(20px, env(safe-area-inset-bottom, 20px));
    left: max(20px, env(safe-area-inset-left, 20px));
    /* Ensure proper positioning on devices with notches */
    /* Remove padding that's causing positioning issues */
}

/* ── Chat Toggle Button ─────────────────────── */
.chatbot-toggle {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, #2C3F83, #EC2024);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 20px rgba(44, 63, 131, 0.3);
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
    touch-action: manipulation;
    /* Prevent tap highlight on mobile */
    -webkit-tap-highlight-color: transparent;
}

.chatbot-toggle:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 25px rgba(44, 63, 131, 0.4);
}

.chatbot-toggle i {
    color: white;
    font-size: 24px;
    transition: transform 0.3s ease;
}

.chatbot-toggle:hover i {
    transform: scale(1.2);
}

/* Pulse animation */
.chatbot-toggle::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: inherit;
    border-radius: 50%;
    animation: chatbot-pulse 2s infinite;
    z-index: -1;
}

@keyframes chatbot-pulse {
    0%   { transform: scale(1);   opacity: 1;   }
    50%  { transform: scale(1.2); opacity: 0.5; }
    100% { transform: scale(1);   opacity: 1;   }
}

/* ── Chat Container ─────────────────────────── */
.chatbot-container {
    position: absolute;
    bottom: 80px;
    left: 0;
    width: 350px;
    /* Use flex column so messages area fills remaining space */
    height: 500px;
    max-height: calc(100dvh - 120px);
    background: white;
    border-radius: 20px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
    display: flex;
    flex-direction: column;
    opacity: 0;
    visibility: hidden;
    transform: translateY(20px) scale(0.9);
    transition: all 0.3s ease;
    overflow: hidden;
}

.chatbot-container.active {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1);
}

/* ── Chat Header ────────────────────────────── */
.chatbot-header {
    background: linear-gradient(135deg, #2C3F83, #1E2F5F);
    color: white;
    padding: 20px;
    border-radius: 20px 20px 0 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 2px 10px rgba(44, 63, 131, 0.2);
    flex-shrink: 0;
}

.chatbot-header h3 {
    margin: 0;
    font-size: 18px;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 10px;
}

.chatbot-header .status-indicator {
    width: 10px;
    height: 10px;
    background: #10B981;
    border-radius: 50%;
    animation: chatbot-blink 2s infinite;
    flex-shrink: 0;
}

@keyframes chatbot-blink {
    0%, 100% { opacity: 1;   }
    50%       { opacity: 0.5; }
}

.chatbot-close {
    background: none;
    border: none;
    color: white;
    font-size: 20px;
    cursor: pointer;
    transition: transform 0.3s ease;
    padding: 5px;
    min-width: 40px;
    min-height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    border-radius: 50%;
    flex-shrink: 0;
}

.chatbot-close:hover {
    transform: rotate(90deg);
    background: rgba(255, 255, 255, 0.15);
}

/* ── Messages Area ──────────────────────────── */
.chatbot-messages {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    background: #f8f9fa;
    display: flex;
    flex-direction: column;
    gap: 15px;
    min-height: 0; /* critical for flex overflow on mobile */
    overscroll-behavior: contain; /* prevent page scroll bleed */
}

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

.chatbot-messages::-webkit-scrollbar-track {
    background: transparent;
}

.chatbot-messages::-webkit-scrollbar-thumb {
    background: #2C3F83;
    border-radius: 10px;
}

.chatbot-messages::-webkit-scrollbar-thumb:hover {
    background: #1E2F5F;
}

/* ── Message Styles ─────────────────────────── */
.message {
    max-width: 82%;
    padding: 12px 16px;
    border-radius: 18px;
    font-size: 14px;
    line-height: 1.5;
    animation: chatbot-slide-in 0.3s ease;
    word-break: break-word;
    overflow-wrap: break-word;
}

@keyframes chatbot-slide-in {
    from { opacity: 0; transform: translateY(10px); }
    to   { opacity: 1; transform: translateY(0);    }
}

.message.user {
    background: linear-gradient(135deg, #2C3F83, #1E2F5F);
    color: white;
    align-self: flex-end;
    border-bottom-left-radius: 4px;
}

.message.bot {
    background: white;
    color: #333;
    align-self: flex-start;
    border: 1px solid #e1e5e9;
    border-bottom-right-radius: 4px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.08);
}

.message-time {
    font-size: 11px;
    opacity: 0.65;
    margin-top: 5px;
}

/* Markdown content inside bot messages */
.message-content-wrapper p { margin: 0 0 8px; }
.message-content-wrapper p:last-child { margin-bottom: 0; }
.message-content-wrapper ul,
.message-content-wrapper ol { padding-right: 20px; margin: 6px 0; }

/* ── Typing Indicator ───────────────────────── */
.typing-indicator {
    display: none;
    align-self: flex-start;
    background: white;
    border: 1px solid #e1e5e9;
    border-radius: 18px;
    padding: 12px 16px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.08);
    flex-shrink: 0;
    /* sits inside the messages flex column */
}

.typing-indicator.active {
    display: flex;
    align-items: center;
    gap: 4px;
}

.typing-dot {
    width: 8px;
    height: 8px;
    background: #2C3F83;
    border-radius: 50%;
    animation: chatbot-typing 1.4s infinite ease-in-out;
}

.typing-dot:nth-child(1) { animation-delay: -0.32s; }
.typing-dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes chatbot-typing {
    0%, 80%, 100% { transform: scale(0.8); opacity: 0.5; }
    40%            { transform: scale(1);   opacity: 1;   }
}

/* ── Input Area ─────────────────────────────── */
.chatbot-input-area {
    padding: 12px 16px;
    background: white;
    border-top: 1px solid #e1e5e9;
    border-radius: 0 0 20px 20px;
    flex-shrink: 0;
    /* Keep input above virtual keyboard on iOS/Android */
    padding-bottom: max(12px, env(safe-area-inset-bottom, 12px));
}

.chatbot-input-form {
    display: flex;
    gap: 10px;
    align-items: center;
}

.chatbot-input {
    flex: 1;
    padding: 11px 16px;
    border: 2px solid #e1e5e9;
    border-radius: 25px;
    outline: none;
    /* ≥16px prevents iOS auto-zoom on focus */
    font-size: 16px;
    transition: border-color 0.3s ease;
    font-family: 'Cairo', sans-serif;
    min-width: 0; /* allow shrinking inside flex */
    background: #fafafa;
}

.chatbot-input:focus {
    border-color: #2C3F83;
    background: white;
}

.chatbot-send {
    width: 44px;
    height: 44px;
    min-width: 44px; /* prevent shrinking */
    border-radius: 50%;
    background: linear-gradient(135deg, #2C3F83, #EC2024);
    border: none;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
    flex-shrink: 0;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.chatbot-send:hover {
    transform: scale(1.1);
    box-shadow: 0 4px 15px rgba(44, 63, 131, 0.3);
}

.chatbot-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* ── Welcome Message ────────────────────────── */
.welcome-message {
    background: linear-gradient(135deg, #f8f9fa, #e9ecef);
    border: 1px solid #dee2e6;
    text-align: center;
    margin-bottom: 5px;
}

.welcome-message h4 {
    color: #2C3F83;
    margin: 0 0 6px;
    font-size: 15px;
}

.welcome-message p {
    color: #666;
    margin: 0;
    font-size: 13px;
}

/* ── Error Message ──────────────────────────── */
.error-message {
    background: #fff5f5;
    color: #c0392b;
    border: 1px solid #fcc;
}

/* ============================================
   Responsive – Tablet  (481px – 768px)
   ============================================ */
@media (min-width: 481px) and (max-width: 768px) {
    .chatbot-widget {
        bottom: max(16px, env(safe-area-inset-bottom, 16px));
        left:   max(16px, env(safe-area-inset-left, 16px));
    }

    .chatbot-container {
        /* Show as a centered popup instead of anchored to corner */
        position: fixed;
        bottom: max(86px, calc(env(safe-area-inset-bottom, 0px) + 86px));
        left: 50%;
        transform: translateX(-50%) translateY(20px) scale(0.9);
        width: min(420px, calc(100vw - 32px));
        height: calc(100dvh - 130px);
        max-height: 560px;
        border-radius: 18px;
    }

    .chatbot-container.active {
        transform: translateX(-50%) translateY(0) scale(1);
    }
}

/* ============================================
   Responsive – Mobile  (≤ 480px)
   ============================================ */
@media (max-width: 480px) {
    .chatbot-widget {
        bottom: max(10px, env(safe-area-inset-bottom, 10px));
        left:   max(10px, env(safe-area-inset-left, 10px));
        /* Ensure widget doesn't go off-screen on RTL */
        right: auto;
    }

    .chatbot-toggle {
        width: 54px;
        height: 54px;
        /* Remove margin that might affect positioning */
    }

    .chatbot-toggle i {
        font-size: 22px;
    }

    /*
     * On mobile, detach from the parent widget and cover most of the viewport.
     * Use dynamic height calculation to handle virtual keyboard properly.
     */
    .chatbot-container {
        position: fixed;
        bottom: max(74px, calc(env(safe-area-inset-bottom, 0px) + 74px));
        left:   max(5px, env(safe-area-inset-left, 5px));
        right:  max(5px, env(safe-area-inset-right, 5px));
        width: auto;
        /* Use dynamic height that adapts to virtual keyboard - fill most of screen */
        height: var(--vh, 100vh);
        max-height: calc(var(--vh, 100vh) - 85px);
        min-height: 400px;
        border-radius: 12px;
        /* Reset the absolute transform origin */
        transform-origin: bottom left;
        /* Prevent content from being obscured by keyboard */
        transition: all 0.3s ease, height 0.25s ease;
        /* Remove padding that might cause positioning issues */
    }

    .chatbot-container.active {
        transform: translateY(0) scale(1);
    }

    /* When virtual keyboard is open, adjust container height */
    .chatbot-container.keyboard-open {
        max-height: calc(var(--vh, 100vh) - 250px);
        min-height: 350px;
    }

    .chatbot-header {
        padding: 13px 14px;
        border-radius: 16px 16px 0 0;
    }

    .chatbot-header h3 {
        font-size: 14px;
        gap: 8px;
    }

    .chatbot-messages {
        padding: 12px;
        gap: 10px;
        /* Improve scroll performance on mobile */
        -webkit-overflow-scrolling: touch;
        scroll-behavior: smooth;
        /* Prevent overscroll bounce */
        overscroll-behavior-y: contain;
    }

    .chatbot-input-area {
        padding: 10px 12px;
        padding-bottom: max(10px, env(safe-area-inset-bottom, 10px));
        border-radius: 0 0 16px 16px;
    }

    .chatbot-input {
        padding: 9px 13px;
        font-size: 16px; /* keep ≥16px to prevent iOS zoom */
        /* Prevent zoom on iOS */
        transform-origin: left center;
        transform: scale(0.95);
    }

    /* Fix input zoom issue on iOS */
    @supports (-webkit-touch-callout: none) {
        .chatbot-input {
            font-size: 16px;
            transform: none;
        }
    }

    .message {
        font-size: 13px;
        max-width: 90%;
        padding: 10px 13px;
    }

    .chatbot-send {
        width: 42px;
        height: 42px;
        min-width: 42px;
    }
}

/* ============================================
   Responsive – Very small  (≤ 360px, eg iPhone SE)
   ============================================ */
@media (max-width: 360px) {
    .chatbot-container {
        left:   max(3px, env(safe-area-inset-left, 3px));
        right:  max(3px, env(safe-area-inset-right, 3px));
        bottom: max(70px, calc(env(safe-area-inset-bottom, 0px) + 70px));
        max-height: calc(var(--vh, 100vh) - 80px);
        min-height: 380px;
        border-radius: 10px;
    }

    .chatbot-header h3 {
        font-size: 13px;
    }
}

/* ============================================
   Responsive – Landscape mobile  (short screens)
   ============================================ */
@media (max-height: 500px) and (max-width: 900px) {
    .chatbot-container {
        position: fixed;
        bottom: max(70px, calc(env(safe-area-inset-bottom, 0px) + 70px));
        left:   max(10px, env(safe-area-inset-left, 10px));
        right:  max(10px, env(safe-area-inset-right, 10px));
        width: auto;
        /* In landscape use most of the short screen height */
        height: calc(100dvh - 90px);
        max-height: 340px;
        border-radius: 14px;
    }

    .chatbot-container.active {
        transform: translateY(0) scale(1);
    }

    .chatbot-header {
        padding: 10px 14px;
    }

    .chatbot-messages {
        padding: 8px 12px;
        gap: 8px;
    }

    .chatbot-input-area {
        padding: 8px 12px;
    }
}

/* ============================================
   Dark Mode – JS class  (.dark-theme on body)
   ============================================ */
body.dark-theme .chatbot-container {
    background: #1a202c;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
}

body.dark-theme .chatbot-messages {
    background: #2d3748;
}

body.dark-theme .message.bot {
    background: #4a5568;
    color: #e2e8f0;
    border-color: #718096;
}

body.dark-theme .chatbot-input-area {
    background: #1a202c;
    border-top-color: #4a5568;
}

body.dark-theme .chatbot-input {
    background: #2d3748;
    border-color: #4a5568;
    color: #e2e8f0;
}

body.dark-theme .chatbot-input::placeholder {
    color: #a0aec0;
}

body.dark-theme .chatbot-input:focus {
    border-color: #667eea;
    background: #374151;
}

body.dark-theme .typing-indicator {
    background: #4a5568;
    border-color: #718096;
}

body.dark-theme .welcome-message {
    background: linear-gradient(135deg, #2d3748, #374151);
    border-color: #4a5568;
}

body.dark-theme .welcome-message h4 {
    color: #90cdf4;
}

body.dark-theme .welcome-message p {
    color: #a0aec0;
}

body.dark-theme .error-message {
    background: #3d1515;
    color: #fc8181;
    border-color: #9b2c2c;
}

/* ============================================
   Dark Mode – System preference fallback
   (for pages that don't have the JS toggle)
   ============================================ */
@media (prefers-color-scheme: dark) {
    body:not(.dark-theme):not(.light-theme) .chatbot-container {
        background: #1a202c;
    }

    body:not(.dark-theme):not(.light-theme) .chatbot-messages {
        background: #2d3748;
    }

    body:not(.dark-theme):not(.light-theme) .message.bot {
        background: #4a5568;
        color: #e2e8f0;
        border-color: #718096;
    }

    body:not(.dark-theme):not(.light-theme) .typing-indicator {
        background: #4a5568;
        border-color: #718096;
    }

    body:not(.dark-theme):not(.light-theme) .chatbot-input-area {
        background: #1a202c;
    }

    body:not(.dark-theme):not(.light-theme) .chatbot-input {
        background: #2d3748;
        border-color: #4a5568;
        color: #e2e8f0;
    }
}