/* ============================================
   OPAL Chat Widget - Modern OpenLink Styling
   ============================================ */

:root {
    --opal-primary: #1f4e79;
    --opal-primary-light: #2d6a9f;
    --opal-primary-dark: #163a5c;
    --opal-accent: #00b4ff;
    --opal-success: #10b981;
    --opal-white: #ffffff;
    --opal-gray-50: #f9fafb;
    --opal-gray-100: #f3f4f6;
    --opal-gray-200: #e5e7eb;
    --opal-gray-300: #d1d5db;
    --opal-gray-500: #6b7280;
    --opal-gray-700: #374151;
    --opal-gray-900: #111827;
    --opal-shadow: 0 10px 40px rgba(31, 78, 121, 0.2);
    --opal-shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.08);
    --opal-radius: 16px;
    --opal-radius-sm: 8px;
    --opal-transition: all 0.2s ease;
}

.chat-popup *,
.open-btn * {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    box-sizing: border-box;
}

/* ============================================
   Floating Action Button (Open Button)
   ============================================ */
.open-btn {
    background: linear-gradient(135deg, var(--opal-primary) 0%, var(--opal-primary-light) 100%);
    color: var(--opal-white);
    border: none;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    cursor: pointer;
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 1999;
    display: none;
    align-items: center;
    justify-content: center;
    box-shadow: var(--opal-shadow);
    transition: var(--opal-transition);
}

.open-btn:hover {
    transform: scale(1.1);
    box-shadow: 0 12px 48px rgba(31, 78, 121, 0.35);
}

.open-btn-icon {
    width: 28px;
    height: 28px;
    stroke: var(--opal-white);
}

/* ============================================
   Chat Popup Container
   ============================================ */
.chat-popup {
    display: none;
    position: fixed;
    bottom: 24px;
    right: 24px;
    border-radius: var(--opal-radius);
    z-index: 2000;
    box-shadow: var(--opal-shadow);
    overflow: hidden;
    font-size: 14px;
}

.form-container {
    min-width: 380px;
    max-width: 420px;
    border-radius: var(--opal-radius);
    background-color: var(--opal-white);
    overflow: hidden;
}

/* ============================================
   Chat Header
   ============================================ */
.chat-header {
    background: linear-gradient(135deg, var(--opal-primary) 0%, var(--opal-primary-light) 100%);
    padding: 16px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.chat-header-content {
    display: flex;
    align-items: center;
    gap: 12px;
}

.chat-header-icon {
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.15);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-header-icon svg {
    width: 22px;
    height: 22px;
    stroke: var(--opal-white);
}

.chat-header-text {
    display: flex;
    flex-direction: column;
}

.chat-header-title {
    color: var(--opal-white);
    font-weight: 600;
    font-size: 16px;
    line-height: 1.3;
}

.chat-header-status {
    color: rgba(255, 255, 255, 0.85);
    font-size: 12px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    display: inline-block;
}

/* Idle state - gray, no animation */
.status-dot.status-idle {
    background: var(--opal-gray-300);
    animation: none;
}

/* Connecting state - yellow/orange, pulsing */
.status-dot.status-connecting {
    background: #f59e0b;
    animation: pulse 1s infinite;
}

/* Online state - green, gentle pulse */
.status-dot.status-online {
    background: var(--opal-success);
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.chat-header-actions {
    display: flex;
    align-items: center;
    gap: 4px;
}

.header-action-btn {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    cursor: pointer;
    transition: var(--opal-transition);
    background: transparent;
    border: none;
}

.header-action-btn:hover {
    background: rgba(255, 255, 255, 0.15);
}

.header-action-btn svg {
    width: 18px;
    height: 18px;
    stroke: var(--opal-white);
}

.header-action-btn a {
    display: flex;
    align-items: center;
    justify-content: center;
}

.header-action-btn a svg {
    stroke: var(--opal-white);
}

/* ============================================
   Chat Body & Messages
   ============================================ */
.chat-body {
    background: var(--opal-gray-50);
}

.messages {
    overflow-y: auto;
    overflow-x: hidden;
    min-height: 350px;
    max-height: 400px;
    width: 100%;
    padding: 16px;
    display: block;
    background: var(--opal-gray-50);
}

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

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

.messages::-webkit-scrollbar-thumb {
    background: var(--opal-gray-300);
    border-radius: 3px;
}

/* User Messages */
.user-message {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    width: 100%;
    padding: 8px 0;
}

.user-message pre {
    margin: 0;
}

.user-message p {
    display: inline-block;
    max-width: 85%;
    font-size: 14px;
    background: linear-gradient(135deg, var(--opal-primary) 0%, var(--opal-primary-light) 100%);
    color: var(--opal-white);
    padding: 12px 16px;
    border-radius: 18px 18px 4px 18px;
    position: relative;
    line-height: 1.5;
    word-wrap: break-word;
}

.user-message p::after {
    display: none;
}

/* Agent Messages */
.agent-message {
    background: var(--opal-white);
    display: block;
    max-width: 85%;
    margin: 8px 0;
    font-size: 14px;
    border-radius: 18px 18px 18px 4px;
    padding: 12px 16px;
    position: relative;
    line-height: 1.6;
    color: var(--opal-gray-700);
    box-shadow: var(--opal-shadow-sm);
    border: 1px solid var(--opal-gray-200);
}

.agent-message::before {
    display: none;
}

.agent-message table {
    margin: 12px 0;
    border-collapse: collapse;
    width: 100%;
    font-size: 13px;
}

.agent-message table thead tr th {
    border: 1px solid var(--opal-gray-200);
    padding: 8px 12px;
    background: var(--opal-gray-100);
    font-weight: 600;
}

.agent-message table tbody tr td {
    border: 1px solid var(--opal-gray-200);
    padding: 8px 12px;
}

/* Typing Indicator */
.typing-indicator {
    background: var(--opal-white);
    width: fit-content;
    padding: 16px 20px;
    border-radius: 18px 18px 18px 4px;
    box-shadow: var(--opal-shadow-sm);
    border: 1px solid var(--opal-gray-200);
    display: block;
}

.typing-indicator.d-none {
    display: none !important;
}

#progress:not(.d-none) {
    display: block !important;
}

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

.typing-dots span {
    width: 8px;
    height: 8px;
    background: var(--opal-primary);
    border-radius: 50%;
    animation: typing-wave 1.4s infinite ease-in-out;
}

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

@keyframes typing-wave {
    0%, 60%, 100% {
        transform: translateY(0);
    }
    30% {
        transform: translateY(-8px);
    }
}

/* ============================================
   Chat Input Area
   ============================================ */
.chat-input-area {
    background: var(--opal-white);
    padding: 16px;
    border-top: 1px solid var(--opal-gray-200);
    display: flex;
    align-items: center;
    gap: 12px;
    position: relative;
}

.chat-input-area textarea {
    flex: 1;
    padding: 12px 16px;
    border-radius: 24px;
    border: 1px solid var(--opal-gray-300);
    resize: none;
    min-height: 48px;
    max-height: 120px;
    font-size: 14px;
    line-height: 1.5;
    transition: var(--opal-transition);
    background: var(--opal-gray-50);
}

.chat-input-area textarea:focus {
    outline: none;
    border-color: var(--opal-primary);
    background: var(--opal-white);
    box-shadow: 0 0 0 3px rgba(31, 78, 121, 0.1);
}

.chat-input-area textarea::placeholder {
    color: var(--opal-gray-500);
}

.send-btn,
.stop-btn {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--opal-transition);
    flex-shrink: 0;
    position: relative;
}

.send-btn.d-none,
.stop-btn.d-none {
    display: none !important;
}

#send:not(.d-none),
#stop:not(.d-none) {
    display: flex !important;
}

.send-btn {
    background: linear-gradient(135deg, var(--opal-primary) 0%, var(--opal-primary-light) 100%);
    color: var(--opal-white);
}

.send-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(31, 78, 121, 0.3);
}

.send-btn svg {
    width: 20px;
    height: 20px;
    stroke: var(--opal-white);
}

.stop-btn {
    background: #ef4444;
    color: var(--opal-white);
}

.stop-btn:hover {
    background: #dc2626;
}

.stop-btn svg {
    width: 18px;
    height: 18px;
    fill: var(--opal-white);
}

/* ============================================
   Intro Screen
   ============================================ */
.intro-container {
    background: var(--opal-white);
}

.intro-header {
    background: linear-gradient(135deg, var(--opal-primary) 0%, var(--opal-primary-light) 100%);
    padding: 24px 20px;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
}

.intro-header-content {
    display: flex;
    align-items: center;
    gap: 14px;
}

.intro-avatar {
    width: 52px;
    height: 52px;
    background: rgba(255, 255, 255, 0.15);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.intro-avatar svg {
    width: 28px;
    height: 28px;
    stroke: var(--opal-white);
}

.intro-title-text {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.intro-title {
    color: var(--opal-white);
    font-weight: 700;
    font-size: 18px;
}

.intro-subtitle {
    color: rgba(255, 255, 255, 0.85);
    font-size: 14px;
}

.intro-body {
    padding: 20px;
}

.intro-section {
    margin-bottom: 20px;
}

.intro-section:last-child {
    margin-bottom: 0;
}

.intro-section-title {
    font-size: 12px;
    font-weight: 600;
    color: var(--opal-gray-500);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 12px;
    padding: 0;
}

.intro-options-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.intro-option-btn {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    background: var(--opal-gray-50);
    border: 1px solid var(--opal-gray-200);
    border-radius: var(--opal-radius-sm);
    cursor: pointer;
    transition: var(--opal-transition);
    text-decoration: none;
    color: var(--opal-gray-700);
    font-size: 14px;
    font-weight: 500;
}

.intro-option-btn:hover {
    background: var(--opal-gray-100);
    border-color: var(--opal-primary);
    color: var(--opal-primary);
}

.intro-option-btn svg {
    width: 20px;
    height: 20px;
    stroke: var(--opal-primary);
    flex-shrink: 0;
}

/* ============================================
   Quick Questions / Prompts
   ============================================ */
.questions {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    padding: 16px 0;
    gap: 8px;
}

.questions .prompt {
    width: 90%;
    background: var(--opal-white);
    border: 1px solid var(--opal-gray-200);
    border-radius: var(--opal-radius-sm);
    padding: 12px 16px;
    text-align: center;
    color: var(--opal-gray-700);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: var(--opal-transition);
    text-decoration: none;
    display: block;
}

.questions .prompt:hover {
    background: var(--opal-gray-50);
    border-color: var(--opal-primary);
    color: var(--opal-primary);
    transform: translateY(-1px);
}

/* ============================================
   Cursor Animation
   ============================================ */
span.cursor::after {
    content: "";
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--opal-primary);
    display: inline-block;
    margin-left: 4px;
    animation: cursor-blink 1s steps(2) infinite;
}

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

/* ============================================
   Utility Classes
   ============================================ */
.d-none {
    display: none !important;
}

#clipboard-text {
    height: 0;
    opacity: 0;
    position: absolute;
    z-index: -9999;
}

/* ============================================
   Responsive Styles
   ============================================ */

/* Tablet and smaller */
@media only screen and (max-width: 768px) {
    .chat-popup {
        width: 100%;
        max-width: 400px;
    }
    
    .form-container {
        min-width: 100%;
    }
}

/* Mobile phones */
@media only screen and (max-width: 480px) {
    .chat-popup {
        position: fixed;
        bottom: 0;
        right: 0;
        left: 0;
        top: auto;
        width: 100%;
        max-width: 100%;
        border-radius: var(--opal-radius) var(--opal-radius) 0 0;
        max-height: 85vh;
        max-height: 85dvh; /* Dynamic viewport height for mobile browsers */
    }

    .form-container {
        min-width: 100%;
        max-width: 100%;
        border-radius: var(--opal-radius) var(--opal-radius) 0 0;
        height: 100%;
        display: flex;
        flex-direction: column;
    }

    .chat-header {
        border-radius: var(--opal-radius) var(--opal-radius) 0 0;
        padding: 12px 16px;
        flex-shrink: 0;
    }

    .chat-header-title {
        font-size: 15px;
    }

    .chat-body {
        flex: 1;
        min-height: 0;
        overflow: hidden;
    }

    .messages {
        max-height: none;
        min-height: 200px;
        height: 100%;
        padding: 12px;
    }

    .chat-input-area {
        padding: 12px;
        flex-shrink: 0;
        /* Prevent iOS keyboard from pushing content weirdly */
        position: relative;
    }

    /* CRITICAL: Prevent iOS zoom on input focus - must be 16px or larger */
    .chat-input-area textarea {
        font-size: 16px;
        padding: 10px 14px;
        min-height: 44px;
        max-height: 100px;
        /* Prevent iOS text size adjustment */
        -webkit-text-size-adjust: 100%;
    }

    .send-btn,
    .stop-btn {
        width: 44px;
        height: 44px;
        flex-shrink: 0;
    }

    .send-btn svg,
    .stop-btn svg {
        width: 20px;
        height: 20px;
    }

    .open-btn {
        bottom: 16px;
        right: 16px;
        width: 56px;
        height: 56px;
    }

    .user-message,
    .agent-message {
        max-width: 90%;
        padding: 10px 14px;
        font-size: 14px;
    }

    /* Intro screen mobile adjustments */
    #opal-chat-intro .chat-body {
        padding: 16px;
    }

    .intro-title {
        font-size: 20px;
    }

    .intro-subtitle {
        font-size: 13px;
    }

    .intro-options {
        gap: 12px;
    }

    .intro-option {
        padding: 14px;
    }

    .intro-option-title {
        font-size: 14px;
    }

    .intro-option-desc {
        font-size: 12px;
    }

    .questions {
        padding: 12px;
        gap: 8px;
    }

    .questions .prompt {
        padding: 8px 12px;
        font-size: 12px;
    }
}

/* Very small screens and landscape phones */
@media only screen and (max-width: 380px) {
    .chat-header-actions {
        gap: 2px;
    }

    .header-action-btn {
        width: 32px;
        height: 32px;
    }

    .header-action-btn svg {
        width: 16px;
        height: 16px;
    }
}

/* Handle short screens (landscape or when keyboard is open) */
@media only screen and (max-height: 600px) {
    .chat-popup {
        max-height: 100vh;
        max-height: 100dvh;
    }

    .messages {
        min-height: 120px;
        max-height: 200px;
    }

    #opal-chat-intro .chat-body {
        padding: 12px;
    }

    .intro-title {
        font-size: 18px;
        margin-bottom: 8px;
    }

    .intro-subtitle {
        margin-bottom: 12px;
    }
}

/* iOS-specific fixes for keyboard handling */
@supports (-webkit-touch-callout: none) {
    .chat-popup {
        /* Use safe area insets for notched devices */
        padding-bottom: env(safe-area-inset-bottom, 0);
    }

    .chat-input-area {
        padding-bottom: calc(12px + env(safe-area-inset-bottom, 0));
    }
}

/* Legacy support - keep old class names working */
.input-area {
    background: var(--opal-white);
    padding: 16px;
    border-top: 1px solid var(--opal-gray-200);
}

.chat-top {
    background: linear-gradient(135deg, var(--opal-primary) 0%, var(--opal-primary-light) 100%);
    padding: 16px 20px;
    border-radius: var(--opal-radius) var(--opal-radius) 0 0;
}

.chat-top-text {
    color: var(--opal-white);
    margin: 0;
    padding: 0;
}

.chat-top-connected {
    color: var(--opal-success);
    font-size: 12px;
}

.close-btn,
.share-btn,
.call-btn {
    cursor: pointer;
}

#opal-chat-intro,
#opal-chat-minimized {
    display: block;
}

#opal-chat-main {
    min-width: 0;
}

/* Legacy send/stop button support */
.form-container .send,
.form-container .stop {
    background-color: var(--opal-white);
    border: none;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    cursor: pointer;
}

.send img,
.stop img {
    width: 24px;
    transition: var(--opal-transition);
}

.send img:hover,
.stop img:hover {
    transform: scale(1.1);
}

/* Main CSS Overrides */
.chat-popup p {
    margin: 0;
}