/* ==========================================================================
   AI Chat Widget - Search Modal Inspired Design
   ========================================================================== */

/* Floating Chat Button - Centered on desktop/tablet, bottom-right on mobile */
.ai-chat-trigger {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    height: 40px;
    border-radius: 20px;
    background: var(--mlad-chat-bg);
    color: var(--mlad-chat-fg);
    border: 1px solid var(--mlad-button-border);
    box-shadow: var(--md-shadow-z2);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 0 20px;
    transition: all 0.2s ease;
    z-index: 999;
    font-size: 0.7rem;
    font-weight: 500;
    font-family: var(--md-text-font-family);
}

.ai-chat-trigger:hover {
    background: var(--mlad-button-bg-hover);
    color: var(--mlad-chat-fg);
    box-shadow: var(--md-shadow-z3);
}

.ai-chat-trigger:active {
    transform: translateX(-50%) scale(0.98);
}

.ai-chat-trigger svg {
    flex-shrink: 0;
}

.ai-chat-trigger-text {
    white-space: nowrap;
}

/* Modal Overlay */
.ai-chat-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    -webkit-backdrop-filter: blur(4px);
    backdrop-filter: blur(4px);
    display: none;
    align-items: flex-start;
    justify-content: center;
    z-index: 1000;
    padding: 48px 16px 16px;
    overflow-y: auto;
    /* Prevent scroll chaining to body */
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

.ai-chat-modal.active {
    display: flex;
}

/* Chat Container - Search modal inspired */
.ai-chat-container {
    background: var(--md-default-bg-color);
    border-radius: 8px;
    box-shadow: var(--md-shadow-z3);
    width: 100%;
    max-width: 560px;
    max-height: calc(100vh - 96px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    margin: 0 auto;
}

/* Header - Minimal like search */
.ai-chat-header {
    padding: 12px 16px;
    border-bottom: 1px solid var(--md-default-fg-color--lightest);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--md-default-bg-color);
    flex-shrink: 0;
}

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

.ai-chat-header-icon {
    color: var(--md-default-fg-color--light);
    flex-shrink: 0;
}

.ai-chat-header-title {
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--md-default-fg-color);
    letter-spacing: -0.01em;
}

/* Header Actions Container */
.ai-chat-header-actions {
    display: flex;
    align-items: center;
    gap: 4px;
}

/* Action Buttons (Clear, Copy, Share) */
.ai-chat-action-btn {
    background: none;
    border: none;
    padding: 6px;
    cursor: pointer;
    color: var(--md-default-fg-color--light);
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: all 0.15s ease;
    opacity: 0.7;
}

.ai-chat-action-btn:hover {
    background: var(--md-default-fg-color--lightest);
    color: var(--md-default-fg-color);
    opacity: 1;
}

.ai-chat-action-btn:active {
    transform: scale(0.95);
}

.ai-chat-close {
    background: none;
    border: none;
    padding: 6px;
    cursor: pointer;
    color: var(--md-default-fg-color--light);
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: all 0.15s ease;
}

.ai-chat-close:hover {
    background: var(--md-default-fg-color--lightest);
    color: var(--md-default-fg-color);
}

/* Input Area - At bottom of modal */
.ai-chat-input-area {
    padding: 16px 20px;
    border-top: 1px solid var(--md-default-fg-color--lightest);
    display: flex;
    gap: 10px;
    background: var(--md-default-bg-color);
    flex-shrink: 0;
}

#ai-chat-input {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid var(--md-default-fg-color--lighter);
    border-radius: 6px;
    font-size: 0.8rem;
    font-family: inherit;
    background: var(--md-default-bg-color);
    color: var(--md-default-fg-color);
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

#ai-chat-input:focus {
    outline: none;
    border-color: var(--md-primary-fg-color);
    box-shadow: 0 0 0 3px var(--md-accent-fg-color--transparent);
}

#ai-chat-input::placeholder {
    color: var(--md-default-fg-color--light);
}

.ai-chat-send-btn {
    background: var(--mlad-button-bg);
    color: var(--mlad-button-fg);
    border: 1px solid var(--mlad-button-border);
    border-radius: 6px;
    width: 36px;
    height: 36px;
    padding: 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s ease;
    flex-shrink: 0;
}

.ai-chat-send-btn svg {
    width: 16px;
    height: 16px;
}

.ai-chat-send-btn:hover {
    opacity: 0.9;
}

.ai-chat-send-btn:active {
    transform: scale(0.98);
}

.ai-chat-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Menu Button (Vertical Ellipsis) */
.ai-chat-menu-wrapper {
    position: relative;
}

.ai-chat-menu-btn {
    background: var(--md-default-fg-color--lightest);
    color: var(--md-default-fg-color--light);
    border: none;
    border-radius: 6px;
    width: 36px;
    height: 36px;
    padding: 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s ease;
    flex-shrink: 0;
}

.ai-chat-menu-btn:hover {
    background: var(--md-default-fg-color--lighter);
    color: var(--md-default-fg-color);
}

.ai-chat-menu-btn:active {
    transform: scale(0.95);
}

.ai-chat-menu-btn svg {
    width: 16px;
    height: 16px;
}

.ai-chat-menu-btn svg circle {
    fill: currentColor;
}

/* Dropdown Menu */
.ai-chat-menu {
    position: absolute;
    bottom: 100%;
    right: 0;
    margin-bottom: 8px;
    background: var(--md-default-bg-color);
    border: 1px solid var(--md-default-fg-color--lightest);
    border-radius: 8px;
    box-shadow: var(--md-shadow-z3);
    min-width: 120px;
    padding: 4px;
    opacity: 0;
    visibility: hidden;
    transform: translateY(8px);
    transition: opacity 0.15s ease, transform 0.15s ease, visibility 0.15s;
    z-index: 10;
}

.ai-chat-menu.active {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.ai-chat-menu-item {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 8px 10px;
    background: none;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    color: var(--md-default-fg-color);
    font-size: 0.75rem;
    font-family: inherit;
    text-align: left;
    transition: background 0.15s ease;
}

.ai-chat-menu-item:hover {
    background: var(--md-default-fg-color--lightest);
}

.ai-chat-menu-item:active {
    transform: scale(0.98);
}

.ai-chat-menu-item svg {
    color: var(--md-default-fg-color--light);
    flex-shrink: 0;
}

.ai-chat-menu-item span {
    flex: 1;
}

/* Maximized State */
.ai-chat-container.maximized {
    max-width: 96vw;
    max-height: 90vh;
    width: 96vw;
    height: 90vh;
}

.ai-chat-container.maximized .ai-chat-messages {
    max-height: none;
    flex: 1;
}

/* Messages Area */
.ai-chat-messages {
    flex: 1;
    padding: 16px;
    overflow-y: auto;
    background: var(--md-default-bg-color);
    display: flex;
    flex-direction: column;
    gap: 12px;
    min-height: 200px;
    max-height: 400px;
    /* Prevent scroll chaining to parent/body */
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

.ai-message {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    max-width: 100%;
}

.ai-message-content {
    padding: 10px 14px;
    border-radius: 8px;
    font-size: 0.8rem;
    line-height: 1.6;
    word-wrap: break-word;
}

.ai-message-bot .ai-message-content {
    background: var(--md-default-fg-color--lightest);
    color: var(--md-default-fg-color);
    border-bottom-left-radius: 2px;
}

.ai-message-user {
    flex-direction: row-reverse;
}

.ai-message-user .ai-message-content {
    background: var(--md-primary-fg-color);
    color: var(--md-primary-bg-color);
    border-bottom-right-radius: 2px;
}

.ai-message-loading .ai-message-content {
    background: var(--md-default-fg-color--lightest);
    color: var(--md-default-fg-color--light);
}

/* Animated loading indicator */
.ai-loading-content {
    display: flex;
    align-items: center;
    gap: 4px;
}

.ai-loading-text {
    font-weight: 500;
}

.ai-loading-dots {
    display: inline-flex;
    gap: 3px;
    margin-left: 2px;
}

.ai-dot {
    width: 4px;
    height: 4px;
    background: var(--md-default-fg-color--light);
    border-radius: 50%;
    animation: dotPulse 1.4s ease-in-out infinite;
}

.ai-dot:nth-child(1) {
    animation-delay: 0s;
}

.ai-dot:nth-child(2) {
    animation-delay: 0.2s;
}

.ai-dot:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes dotPulse {

    0%,
    60%,
    100% {
        opacity: 0.3;
        transform: scale(1);
    }

    30% {
        opacity: 1;
        transform: scale(1.2);
    }
}

/* Link styles in messages */
.ai-chat-link {
    color: var(--md-typeset-a-color);
    text-decoration: underline;
    font-weight: 500;
    transition: color 0.15s ease;
    word-break: break-word;
}

.ai-chat-link:hover {
    color: var(--md-accent-fg-color);
}

/* List styles in messages */
.ai-chat-list {
    margin: 8px 0 4px 0;
    padding-left: 0;
    list-style: none;
}

.ai-chat-list li {
    position: relative;
    padding-left: 16px;
    margin-bottom: 6px;
    line-height: 1.5;
}

.ai-chat-list li:last-child {
    margin-bottom: 0;
}

.ai-chat-list li::before {
    content: '';
    position: absolute;
    left: 0;
    top: 8px;
    width: 4px;
    height: 4px;
    background: var(--md-primary-fg-color);
    border-radius: 50%;
}

/* Paragraph styles in messages */
.ai-message-content p {
    margin: 0 0 8px 0;
}

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

/* Bold and italic text */
.ai-message-content strong {
    font-weight: 600;
}

.ai-message-content em {
    font-style: italic;
}

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

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

.ai-chat-messages::-webkit-scrollbar-thumb {
    background: var(--md-default-fg-color--lighter);
    border-radius: 3px;
}

.ai-chat-messages::-webkit-scrollbar-thumb:hover {
    background: var(--md-default-fg-color--light);
}

/* ==========================================================================
   Responsive Design
   ========================================================================== */

/* Mobile - Button stays bottom right for thumb access */
@media (max-width: 768px) {
    .ai-chat-trigger {
        bottom: 20px;
        left: auto;
        right: 20px;
        transform: none;
        width: 48px;
        height: 48px;
        border-radius: 50%;
        padding: 0;
    }

    .ai-chat-trigger:active {
        transform: scale(0.95);
    }

    .ai-chat-trigger-text {
        display: none;
    }

    .ai-chat-modal {
        padding: 0;
        align-items: stretch;
        /* Mobile: prevent any scrolling on overlay itself */
        overflow: hidden;
    }

    .ai-chat-container {
        max-width: 100%;
        max-height: 100%;
        height: 100%;
        border-radius: 0;
        /* Isolate touch actions within container */
        touch-action: pan-y;
        overflow: hidden;
    }

    /* Hide maximize button on mobile - already fullscreen */
    #ai-chat-maximize {
        display: none;
    }

    .ai-chat-messages {
        min-height: 0;
        max-height: none;
        flex: 1;
        /* Ensure messages area handles all scrolling on mobile */
        touch-action: pan-y;
        overscroll-behavior: contain;
    }

    /* Menu adjustments for mobile */
    .ai-chat-menu {
        right: 0;
        min-width: 160px;
    }

    /* Handle virtual keyboard - use dvh for dynamic viewport height */
    @supports (height: 100dvh) {
        .ai-chat-container {
            height: 100dvh;
            max-height: 100dvh;
        }
    }
}

/* Tablet - Centered like desktop */
@media (min-width: 769px) and (max-width: 1024px) {
    .ai-chat-trigger {
        bottom: 24px;
    }
}

/* Dark mode adjustments */
@media (prefers-color-scheme: dark) {
    .ai-chat-trigger {
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3), 0 0 0 1px rgba(255, 255, 255, 0.1);
    }

    .ai-message-bot .ai-message-content {
        background: rgba(255, 255, 255, 0.05);
    }

    .ai-chat-link {
        color: var(--md-typeset-a-color);
    }

    .ai-chat-link:hover {
        color: var(--md-accent-fg-color);
    }
}

/* Slate theme specific */
[data-md-color-scheme="slate"] .ai-chat-trigger {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3), 0 0 0 1px rgba(255, 255, 255, 0.1);
}

[data-md-color-scheme="slate"] .ai-message-bot .ai-message-content {
    background: rgba(255, 255, 255, 0.05);
}

[data-md-color-scheme="slate"] .ai-chat-menu {
    background: var(--md-default-bg-color);
    border-color: rgba(255, 255, 255, 0.1);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

[data-md-color-scheme="slate"] .ai-chat-menu-btn {
    background: rgba(255, 255, 255, 0.08);
}

[data-md-color-scheme="slate"] .ai-chat-menu-btn:hover {
    background: rgba(255, 255, 255, 0.15);
}

/* Toast Notification */
.ai-chat-toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%) translateY(10px);
    background: var(--md-default-fg-color);
    color: var(--md-default-bg-color);
    padding: 8px 16px;
    border-radius: 6px;
    font-size: 0.8rem;
    font-weight: 500;
    box-shadow: var(--md-shadow-z2);
    opacity: 0;
    transition: opacity 0.2s ease, transform 0.2s ease;
    z-index: 1001;
    pointer-events: none;
}

.ai-chat-toast-visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ==========================================================================
   Suggested Prompts - Chip-style buttons above input
   ========================================================================== */

.ai-chat-prompts {
    display: none;
    flex-wrap: wrap;
    gap: 8px;
    padding: 12px 16px 16px;
    justify-content: center;
}

.ai-chat-prompts.visible {
    display: flex;
}

.ai-chat-prompt-btn {
    background: var(--md-default-fg-color--lightest);
    border: 1px solid var(--md-default-fg-color--lighter);
    border-radius: 16px;
    padding: 8px 14px;
    font-size: 0.75rem;
    font-family: inherit;
    color: var(--md-default-fg-color);
    cursor: pointer;
    transition: all 0.15s ease;
    max-width: calc(50% - 4px);
    text-align: center;
    line-height: 1.4;
    /* Truncate long prompts */
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ai-chat-prompt-btn:hover {
    background: var(--md-primary-fg-color);
    border-color: var(--md-primary-fg-color);
    color: var(--md-primary-bg-color);
    transform: translateY(-1px);
}

.ai-chat-prompt-btn:active {
    transform: translateY(0) scale(0.98);
}

/* Dark mode / Slate theme for prompts */
[data-md-color-scheme="slate"] .ai-chat-prompt-btn {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.1);
}

[data-md-color-scheme="slate"] .ai-chat-prompt-btn:hover {
    background: var(--md-primary-fg-color);
    border-color: var(--md-primary-fg-color);
}

/* Mobile adjustments for prompts */
@media (max-width: 768px) {
    .ai-chat-prompts {
        padding: 12px 12px 12px;
    }

    .ai-chat-prompt-btn {
        font-size: 0.7rem;
        padding: 6px 12px;
        /* Allow full width on very small screens */
        max-width: 100%;
        flex: 1 1 auto;
        min-width: 45%;
    }
}