/**
 * AIDA Chat Widget Styles
 * Version: 1.3.1
 * Based on MVP design by 4Care Service srl
 */

:root {
    --aida-chat-primary: #0073aa;
    --aida-chat-primary-hover: #005d8c;
    --aida-chat-primary-light: #e5f3ff;
    --aida-chat-text: #333;
    --aida-chat-text-light: #666;
    --aida-chat-bg: #fff;
    --aida-chat-bg-light: #f7f7f7;
    --aida-chat-border: #e0e0e0;
    --aida-chat-shadow: 0 5px 40px rgba(0, 0, 0, 0.16);
    --aida-chat-radius: 16px;
    --aida-chat-z-index: 999999;
}

/* Widget Container */
.aida-chat-container {
    position: fixed;
    z-index: var(--aida-chat-z-index);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    font-size: 14px;
    line-height: 1.5;
    box-sizing: border-box;
    pointer-events: none;
}

.aida-chat-container > * {
    pointer-events: auto;
}

.aida-chat-container *,
.aida-chat-container *::before,
.aida-chat-container *::after {
    box-sizing: border-box;
}

.aida-chat-bottom-right {
    bottom: 20px;
    right: 20px;
}

.aida-chat-bottom-left {
    bottom: 20px;
    left: 20px;
}

/* Chat Bubble Button - Elegant floating design */
.aida-chat-button {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 24px;
    background: var(--aida-chat-primary);
    border: none;
    border-radius: 28px;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(0, 115, 170, 0.35), 0 2px 4px rgba(0, 0, 0, 0.1);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    z-index: 999999;
    pointer-events: auto !important;
}

.aida-chat-button:hover {
    background: var(--aida-chat-primary-hover);
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(0, 115, 170, 0.45), 0 3px 6px rgba(0, 0, 0, 0.12);
}

.aida-chat-button:active {
    transform: translateY(0);
    box-shadow: 0 2px 8px rgba(0, 115, 170, 0.3);
}

.aida-chat-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
}

.aida-chat-icon svg {
    width: 22px;
    height: 22px;
}

.aida-chat-button-text {
    color: #fff;
    font-size: 14px;
    font-weight: 500;
    white-space: nowrap;
    letter-spacing: 0.2px;
}

/* Hide bubble when chat is open */
.aida-chat-open .aida-chat-button {
    display: none;
}

/* Chat Window */
.aida-chat-window {
    position: absolute;
    bottom: 80px;
    width: 380px;
    max-width: calc(100vw - 40px);
    height: 550px;
    max-height: calc(100vh - 120px);
    background: var(--aida-chat-bg);
    border-radius: var(--aida-chat-radius);
    box-shadow: var(--aida-chat-shadow);
    display: none;
    flex-direction: column;
    overflow: hidden;
    animation: aida-chat-slideUp 0.3s ease;
}

.aida-chat-open .aida-chat-window {
    display: flex;
}

.aida-chat-bottom-right .aida-chat-window {
    right: 0;
}

.aida-chat-bottom-left .aida-chat-window {
    left: 0;
}

@keyframes aida-chat-slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Header */
.aida-chat-header {
    background: var(--aida-chat-primary);
    color: #fff;
    padding: 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

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

.aida-chat-avatar {
    width: 40px;
    height: 40px;
    background: #fff;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    flex-shrink: 0;
}

.aida-chat-avatar img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    padding: 4px;
}

.aida-chat-avatar svg {
    width: 24px;
    height: 24px;
    color: var(--aida-chat-primary);
}

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

.aida-chat-agent-name {
    font-weight: 600;
    font-size: 16px;
}

.aida-chat-status {
    font-size: 12px;
    opacity: 0.9;
}

/* X close button - elegant round design */
.aida-chat-minimize {
    background: rgba(255, 255, 255, 0.2) !important;
    border: none !important;
    width: 32px !important;
    height: 32px !important;
    min-width: 32px !important;
    min-height: 32px !important;
    border-radius: 50% !important;
    cursor: pointer;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    transition: all 0.25s ease;
    color: #fff !important;
    font-size: 14px !important;
    font-weight: 400;
    line-height: 1;
    padding: 0 !important;
    margin: 0;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}

.aida-chat-minimize:hover {
    background: rgba(255, 255, 255, 0.35) !important;
    transform: scale(1.1);
    box-shadow: 0 3px 12px rgba(0, 0, 0, 0.2);
}

.aida-chat-minimize:active {
    transform: scale(0.95);
}

.aida-chat-minimize svg {
    width: 18px;
    height: 18px;
    color: white;
}

/* Messages Container */
.aida-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--aida-chat-bg-light);
}

/* Welcome Message */
.aida-chat-welcome {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: 24px 16px;
    color: var(--aida-chat-text-light);
}

.aida-welcome-avatar {
    width: 64px;
    height: 64px;
    background: var(--aida-chat-primary-light);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 16px;
}

.aida-welcome-avatar svg {
    width: 36px;
    height: 36px;
    color: var(--aida-chat-primary);
}

.aida-chat-welcome p {
    margin: 0;
    font-size: 15px;
    line-height: 1.6;
    color: var(--aida-chat-text);
}

/* Message Bubbles */
.aida-message {
    max-width: 80%;
    animation: aida-chat-fadeIn 0.3s ease;
}

@keyframes aida-chat-fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.aida-message-bubble {
    padding: 8px 12px;
    border-radius: 14px;
    word-wrap: break-word;
    white-space: pre-wrap;
    line-height: 1.35;
    display: inline-block;
    font-size: 14px;
    max-width: 280px;
}

.aida-message-assistant {
    align-self: flex-start;
}

.aida-message-assistant .aida-message-bubble {
    background: var(--aida-chat-bg);
    color: var(--aida-chat-text);
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.aida-message-user {
    align-self: flex-end;
}

.aida-message-user .aida-message-bubble {
    background: var(--aida-chat-primary);
    color: #fff;
    border-bottom-right-radius: 4px;
}

/* Operator message (human takeover) - styled like assistant but with different color */
.aida-message-operator {
    align-self: flex-start;
}

.aida-message-operator .aida-message-bubble {
    background: #e8f5e9;
    color: var(--aida-chat-text);
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
    border-left: 3px solid #4caf50;
}

/* Error message */
.aida-message-error .aida-message-bubble {
    background: #fee;
    color: #c00;
    border: 1px solid #fcc;
}

/* Typing Indicator */
.aida-chat-typing {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    color: var(--aida-chat-text-light);
    font-size: 13px;
    background: var(--aida-chat-bg-light);
}

.aida-typing-indicator {
    display: flex;
    gap: 4px;
}

.aida-typing-indicator span {
    width: 8px;
    height: 8px;
    background: var(--aida-chat-primary);
    border-radius: 50%;
    animation: aida-chat-bounce 1.4s infinite ease-in-out both;
}

.aida-typing-indicator span:nth-child(1) {
    animation-delay: -0.32s;
}

.aida-typing-indicator span:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes aida-chat-bounce {
    0%, 80%, 100% {
        transform: scale(0);
    }
    40% {
        transform: scale(1);
    }
}

/* Input Area */
.aida-chat-input-form {
    padding: 12px 16px;
    background: var(--aida-chat-bg);
    border-top: 1px solid var(--aida-chat-border);
    flex-shrink: 0;
    display: flex;
    gap: 8px;
    align-items: center;
}

.aida-chat-input {
    flex: 1;
    padding: 12px 16px;
    border: 1px solid var(--aida-chat-border);
    border-radius: 24px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s;
    background: var(--aida-chat-bg);
    color: var(--aida-chat-text);
}

.aida-chat-input:focus {
    border-color: var(--aida-chat-primary);
}

.aida-chat-input::placeholder {
    color: #999;
}

.aida-chat-input:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* Send button */
.aida-chat-send {
    width: 46px;
    height: 46px;
    border-radius: 50%;
    background: var(--aida-chat-primary);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    flex-shrink: 0;
    color: #fff;
    font-size: 24px;
    padding: 0;
}

.aida-chat-send:hover:not(:disabled) {
    background: var(--aida-chat-primary-hover);
    transform: scale(1.08);
}

.aida-chat-send:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.aida-chat-send svg {
    width: 18px;
    height: 18px;
    color: white;
}

/* Footer */
.aida-chat-footer {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 8px 16px;
    font-size: 11px;
    color: var(--aida-chat-text-light);
    border-top: 1px solid var(--aida-chat-border);
    background: var(--aida-chat-bg);
    flex-shrink: 0;
}

.aida-chat-footer a {
    color: var(--aida-chat-primary);
    text-decoration: none;
}

.aida-chat-footer a:hover {
    text-decoration: underline;
}

/* Streaming cursor animation */
.aida-cursor {
    display: inline-block;
    width: 2px;
    height: 1em;
    background: currentColor;
    margin-left: 2px;
    animation: aida-chat-blink 1s infinite;
    vertical-align: text-bottom;
}

@keyframes aida-chat-blink {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0; }
}

/* Markdown formatting in messages */
.aida-message-content {
    white-space: normal;
}

.aida-message-content strong,
.aida-message-content b {
    font-weight: 600;
}

.aida-message-content em,
.aida-message-content i {
    font-style: italic;
}

.aida-message-content ul.aida-list {
    margin: 8px 0;
    padding-left: 20px;
    list-style-type: disc;
    list-style-position: outside;
}

.aida-message-content li.aida-list-item {
    margin: 4px 0;
    padding-left: 4px;
    display: list-item;
}

.aida-message-content p {
    margin: 0 0 8px;
}

.aida-message-content p:last-child {
    margin-bottom: 0;
}

.aida-message-content .aida-inline-code,
.aida-message-content code {
    background: rgba(0, 0, 0, 0.08);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Courier New', Consolas, monospace;
    font-size: 13px;
    word-break: break-word;
}

.aida-message-content .aida-code-block,
.aida-message-content pre {
    background: rgba(0, 0, 0, 0.08);
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 8px 0;
    white-space: pre-wrap;
    word-wrap: break-word;
}

.aida-message-content .aida-code-block code,
.aida-message-content pre code {
    background: none;
    padding: 0;
}

.aida-message-content a {
    color: var(--aida-chat-primary);
    text-decoration: underline;
    word-break: break-word;
}

.aida-message-content a:hover {
    text-decoration: none;
}

/* Keep user message content as plain text */
.aida-message-user .aida-message-content {
    white-space: pre-wrap;
}

/* Scrollbar */
.aida-chat-messages::-webkit-scrollbar {
    width: 6px;
}

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

.aida-chat-messages::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 3px;
}

.aida-chat-messages::-webkit-scrollbar-thumb:hover {
    background: #aaa;
}

/* Tablet Responsive */
@media (max-width: 768px) {
    .aida-chat-container.aida-chat-bottom-right {
        right: 15px;
        bottom: 15px;
    }

    .aida-chat-container.aida-chat-bottom-left {
        left: 15px;
        bottom: 15px;
    }

    .aida-chat-window {
        width: 340px;
        height: 500px;
    }
}

/* Mobile Responsive */
@media (max-width: 480px) {
    /* CRITICAL: Prevent iOS zoom on input focus - must be 16px minimum */
    .aida-chat-container,
    .aida-chat-container * {
        -webkit-text-size-adjust: 100% !important;
        -ms-text-size-adjust: 100% !important;
        text-size-adjust: 100% !important;
    }

    .aida-chat-container input,
    .aida-chat-container textarea,
    .aida-chat-container select {
        font-size: 16px !important;
        transform: scale(1) !important;
        -webkit-transform: scale(1) !important;
    }

    /* Widget container on mobile - not affecting page */
    .aida-chat-container {
        position: fixed !important;
        bottom: 0 !important;
        right: 0 !important;
        left: auto !important;
        width: auto !important;
        max-width: none !important;
        overflow: visible !important;
        z-index: 9999999 !important;
    }

    .aida-chat-container.aida-chat-bottom-right,
    .aida-chat-container.aida-chat-bottom-left {
        bottom: 0;
        right: 0;
        left: auto;
    }

    /* Bubble on mobile - small round icon to avoid cookie banner overlap */
    .aida-chat-button {
        position: fixed !important;
        bottom: 16px !important;
        right: 16px !important;
        left: auto !important;
        padding: 0 !important;
        border-radius: 50% !important;
        width: 56px !important;
        height: 56px !important;
        min-width: 56px !important;
        min-height: 56px !important;
        justify-content: center;
        z-index: 9999999;
    }

    .aida-chat-button-text {
        display: none !important;
    }

    .aida-chat-icon svg {
        width: 24px;
        height: 24px;
    }

    /* Full screen chat window on mobile - FIXED position */
    .aida-chat-open .aida-chat-window {
        display: flex !important;
    }

    .aida-chat-window {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100vw !important;
        height: 100vh !important;
        height: 100dvh !important;
        max-width: 100vw !important;
        max-height: 100vh !important;
        max-height: 100dvh !important;
        border-radius: 0 !important;
        z-index: 99999999 !important;
        flex-direction: column !important;
        overflow: hidden !important;
        transform: none !important;
        -webkit-transform: none !important;
    }

    /* Header on mobile */
    .aida-chat-header {
        padding: 12px 16px;
        padding-top: max(12px, env(safe-area-inset-top));
        flex-shrink: 0;
        overflow: hidden;
        width: 100%;
    }

    .aida-chat-avatar {
        width: 36px;
        height: 36px;
    }

    .aida-chat-agent-name {
        font-size: 15px;
    }

    .aida-chat-status {
        font-size: 11px;
    }

    /* Messages area */
    .aida-chat-messages {
        flex: 1 1 auto !important;
        min-height: 0 !important;
        padding: 12px;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        -webkit-overflow-scrolling: touch;
        width: 100%;
        overscroll-behavior: contain;
    }

    .aida-message {
        max-width: 82%;
        word-wrap: break-word;
        overflow-wrap: break-word;
    }

    .aida-message-bubble {
        padding: 7px 11px;
        font-size: 14px;
        word-wrap: break-word;
        overflow-wrap: break-word;
        max-width: 260px;
        line-height: 1.35;
    }

    /* Input area - CRITICAL for preventing zoom */
    .aida-chat-input-form {
        flex-shrink: 0 !important;
        padding: 10px 12px;
        padding-bottom: max(10px, env(safe-area-inset-bottom));
        background: var(--aida-chat-bg);
        border-top: 1px solid var(--aida-chat-border);
        width: 100%;
        overflow: hidden;
    }

    .aida-chat-input {
        flex: 1 1 auto !important;
        min-width: 0 !important;
        padding: 12px 16px;
        font-size: 16px !important; /* MUST be 16px to prevent iOS zoom */
        line-height: 1.4 !important;
        border-radius: 24px;
        border: 1px solid var(--aida-chat-border);
        background: #fff;
        -webkit-appearance: none !important;
        -moz-appearance: none !important;
        appearance: none !important;
        touch-action: manipulation !important;
    }

    .aida-chat-input:focus {
        outline: none !important;
        border-color: var(--aida-chat-primary);
        font-size: 16px !important;
        transform: none !important;
        -webkit-transform: none !important;
    }

    .aida-chat-send {
        flex-shrink: 0 !important;
        width: 46px;
        height: 46px;
        min-width: 46px;
        font-size: 20px;
        border-radius: 50%;
    }

    /* Typing indicator */
    .aida-chat-typing {
        flex-shrink: 0;
    }

    /* Footer */
    .aida-chat-footer {
        font-size: 10px;
    }
}

/* Very small screens - icon only bubble */
@media (max-width: 360px) {
    .aida-chat-button {
        width: 52px !important;
        height: 52px !important;
    }

    .aida-chat-icon svg {
        width: 22px;
        height: 22px;
    }
}

/* Landscape mobile */
@media (max-height: 500px) and (orientation: landscape) {
    .aida-chat-window {
        height: 100vh;
        max-height: 100vh;
    }

    .aida-chat-messages {
        padding: 8px 12px;
    }

    .aida-chat-header {
        padding: 10px 16px;
    }

    .aida-chat-input-form {
        padding: 8px 12px;
    }
}

/* Accessibility */
.aida-chat-button:focus-visible,
.aida-chat-minimize:focus-visible,
.aida-chat-send:focus-visible,
.aida-chat-input:focus-visible {
    outline: 2px solid var(--aida-chat-primary);
    outline-offset: 2px;
}

/* Print: hide widget */
@media print {
    .aida-chat-container {
        display: none !important;
    }
}
