/**
 * ================================================================================
 * CO-READIT - AI Chatbox CSS
 * ================================================================================
 * 
 * AI Chat interface styles - Based on Komico implementation
 * Preserves all class names for consistency
 * 
 * @author      Edu Brazeal
 * @version     1.0.0
 * ================================================================================
 */

/* ===== Main Wrapper ===== */
.ai-chatbox-wrapper {
    width: 100%;
    height: calc(100vh - 150px);
    max-height: 85vh;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
}

.ai-chatbox {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    max-width: 100%;
    margin: 0 auto;
    background: var(--bg-primary);
    border-radius: 12px;
    overflow: hidden;
}

/* ===== Header ===== */
.ai-chatbox__header {
    display: none !important;
    padding: 1rem;
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.ai-chatbox__header-content {
    display: flex;
    align-items: center;
    gap: 12px;
}

.ai-chatbox__header-icon {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--primary-color);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #ffffff;
    flex-shrink: 0;
}

.ai-chatbox__header-text {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.ai-chatbox__header-text h3 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
    line-height: 1.2;
}

.ai-chatbox__header-text .ai-status {
    font-size: 13px;
    color: var(--text-secondary);
    font-weight: 400;
}

/* ===== Messages Container ===== */
.ai-chatbox__messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 16px;
    background: var(--bg-secondary);
    scroll-behavior: smooth;
}

/* Custom Scrollbar */
.ai-chatbox__messages::-webkit-scrollbar {
    width: 6px;
}

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

.ai-chatbox__messages::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 10px;
}

.ai-chatbox__messages::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

/* ===== Message Bubbles ===== */
.ai-message {
    display: flex;
    gap: 10px;
    max-width: 85%;
    animation: fadeInUp 0.3s ease-out;
}

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

/* AI Messages (Left) */
.ai-message--ai {
    align-self: flex-start;
}

/* Hide AI avatar completely */
.ai-message--ai .ai-message__avatar {
    display: none;
}

.ai-message--ai .ai-message__content {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.ai-message--ai .ai-message__text {
    background: var(--bg-primary);
    color: var(--text-primary);
    padding: 12px 16px;
    border-radius: 18px;
    border-top-left-radius: 4px;
    font-size: 15px;
    line-height: 1.5;
    word-wrap: break-word;
    white-space: pre-wrap;
    border: 1px solid var(--border-color);
}

/* User Messages (Right) */
.ai-message--user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.ai-message--user .ai-message__content {
    display: flex;
    flex-direction: column;
    gap: 4px;
    align-items: flex-end;
}

.ai-message--user .ai-message__text {
    background: var(--primary-color);
    color: #ffffff;
    padding: 12px 16px;
    border-radius: 18px;
    border-top-right-radius: 4px;
    font-size: 15px;
    line-height: 1.5;
    word-wrap: break-word;
    white-space: pre-wrap;
}

.ai-message__time {
    font-size: 11px;
    color: var(--text-secondary);
    padding: 0 8px;
    font-weight: 400;
}

/* ===== Loading Indicator ===== */
.ai-message--loading .ai-message__text {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 16px;
}

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

.ai-typing-indicator span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-secondary);
    animation: typing 1.4s infinite;
}

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

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

@keyframes typing {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.7;
    }
    30% {
        transform: translateY(-10px);
        opacity: 1;
    }
}

/* ===== Input Area ===== */
.ai-chatbox__input-wrapper {
    padding: 0 1rem 1rem;
    background: var(--bg-primary);
    flex-shrink: 0;
}

.ai-chatbox__input-container {
    display: flex;
    gap: 10px;
    align-items: flex-end;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 24px;
    padding: 8px 12px;
    transition: all 0.2s ease;
    position: relative;
}

.ai-chatbox__input-container:focus-within {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.ai-chatbox__input {
    flex: 1;
    border: none;
    outline: none;
    background: transparent;
    color: var(--text-primary);
    font-size: 15px;
    font-family: inherit;
    resize: none;
    max-height: 120px;
    min-height: 24px;
    line-height: 1.5;
    padding: 4px 0;
}

.ai-chatbox__input::placeholder {
    color: var(--text-secondary);
}

.ai-chatbox__send-btn {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--primary-color);
    border: none;
    color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    flex-shrink: 0;
    padding: 0;
}

.ai-chatbox__send-btn:hover:not(:disabled) {
    background: var(--primary-hover);
    transform: scale(1.05);
}

.ai-chatbox__send-btn:active:not(:disabled) {
    transform: scale(0.95);
}

.ai-chatbox__send-btn:disabled {
    background: var(--text-secondary);
    opacity: 0.5;
    cursor: not-allowed;
}

.ai-chatbox__stop-btn {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--error-color);
    border: none;
    color: #ffffff;
    display: none;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    flex-shrink: 0;
    padding: 0;
    position: absolute;
    right: 12px;
}

.ai-chatbox__stop-btn:hover {
    background: #dc2626;
    transform: scale(1.05);
}

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

.ai-chatbox__disclaimer {
    margin-top: 8px;
    font-size: 12px;
    color: var(--text-secondary);
    text-align: center;
    line-height: 1.4;
    opacity: 0.8;
}

/* ===== Empty State ===== */
.ai-chatbox__empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    padding: 40px 20px;
    text-align: center;
    gap: 16px;
}

.ai-chatbox__empty-icon {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: var(--bg-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
}

.ai-chatbox__empty-text {
    color: var(--text-secondary);
    font-size: 15px;
    max-width: 300px;
}

/* ===== Prompt Suggestions Carousel ===== */
.ai-prompt-suggestions {
    margin: 0;
    padding: 15px 1rem 0;
    width: 100%;
    animation: fadeInUp 0.4s ease-out;
    display: block !important;
    visibility: visible;
    opacity: 1;
    background: var(--bg-primary);
    flex-shrink: 0;
    position: relative;
}

.ai-prompt-suggestions.hidden {
    display: none !important;
    visibility: hidden;
    opacity: 0;
}

/* Fade effect on right edge to indicate scrollability */
.ai-prompt-suggestions::after {
    content: '';
    position: absolute;
    top: 15px;
    right: 0;
    bottom: 0;
    width: 40px;
    background: linear-gradient(to right, transparent, var(--bg-primary));
    pointer-events: none;
    z-index: 2;
}

.ai-prompt-suggestions__grid {
    position: fixed;
    bottom: 15%;
    display: flex;
    gap: 5px;
    width: 100%;
    padding: 0;
    margin: 0;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.ai-prompt-suggestions__grid::-webkit-scrollbar {
    display: none;
}

.ai-prompt-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 16px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s ease;
    text-align: center;
    min-width: 120px;
    max-width: 140px;
    flex-shrink: 0;
    position: relative;
    overflow: hidden;
    outline: none;
}

.ai-prompt-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--primary-color);
    opacity: 0;
    transition: opacity 0.2s ease;
    z-index: 0;
}

.ai-prompt-card:hover::before {
    opacity: 0.05;
}

.ai-prompt-card:hover {
    border-color: var(--primary-color);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.ai-prompt-card:active {
    transform: translateY(0);
}

.ai-prompt-card__icon {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    background: var(--bg-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--primary-color);
    flex-shrink: 0;
    position: relative;
    z-index: 1;
    transition: all 0.2s ease;
}

.ai-prompt-card:hover .ai-prompt-card__icon {
    background: var(--primary-color);
    color: #ffffff;
    transform: scale(1.05);
}

.ai-prompt-card__icon svg {
    width: 20px;
    height: 20px;
}

.ai-prompt-card__label {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-primary);
    line-height: 1.3;
    position: relative;
    z-index: 1;
    max-width: 100%;
    word-wrap: break-word;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ===== Web Search Results Styling ===== */

/* Web Search Badge */
.ai-message--web-search .ai-message__content::before {
    content: "🌐 Web Search";
    display: inline-block;
    padding: 4px 10px;
    background: var(--primary-color);
    color: #ffffff;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 600;
    margin-bottom: 8px;
    letter-spacing: 0.3px;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ai-message--web-search .ai-message__content {
    max-width: 100%;
    overflow: hidden;
}

.ai-message--web-search .ai-message__text {
    max-width: 100%;
    overflow-wrap: break-word;
    word-wrap: break-word;
    word-break: break-word;
}

/* Source Images Container - Overlapping thumbnails */
.ai-message__source-images {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
    padding-left: 4px;
}

.ai-message__source-images-list {
    display: flex;
    position: relative;
    height: 30px;
}

.ai-message__source-image {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    border: 2px solid var(--bg-primary);
    object-fit: cover;
    position: relative;
    transition: transform 0.2s ease, z-index 0.2s ease;
    background: var(--bg-secondary);
}

.ai-message__source-image:not(:first-child) {
    margin-left: -10px;
}

.ai-message__source-image:hover {
    transform: scale(1.2) translateY(-2px);
    z-index: 10;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.ai-message__source-image-count {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    border: 2px solid var(--bg-primary);
    background: var(--primary-color);
    color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    font-weight: 700;
    margin-left: -10px;
    position: relative;
    z-index: 5;
}

/* Sources Carousel Container */
.ai-message__sources {
    margin-top: 12px;
    width: 100%;
}

.ai-message__sources-title {
    font-size: 12px;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 0 4px;
}

.ai-message__sources-title svg {
    width: 14px;
    height: 14px;
    color: var(--primary-color);
}

/* Carousel Wrapper */
.ai-message__sources-carousel {
    position: relative;
    width: 100%;
    overflow: hidden;
}

/* Scrollable Container */
.ai-message__sources-list {
    display: flex;
    gap: 12px;
    overflow-x: auto;
    scroll-behavior: smooth;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    padding: 4px;
    margin: -4px;
}

.ai-message__sources-list::-webkit-scrollbar {
    display: none;
}

.ai-message__sources-list {
    -ms-overflow-style: none;
    scrollbar-width: none;
}

/* Individual Source Card */
.ai-message__source-item {
    flex: 0 0 calc(80% - 60px);
    scroll-snap-align: start;
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 8px;
    background: var(--bg-primary);
    border-radius: 12px;
    border: 1px solid var(--border-color);
    transition: all 0.3s ease;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    cursor: pointer;
    text-decoration: none;
    min-height: 160px;
}

.ai-message__source-item:hover {
    border-color: var(--primary-color);
    box-shadow: 0 4px 16px rgba(59, 130, 246, 0.15);
    transform: translateY(-2px);
}

/* Card Image */
.ai-message__source-card-image {
    width: 100%;
    height: 120px;
    border-radius: 8px;
    object-fit: cover;
    background: var(--bg-secondary);
    flex-shrink: 0;
}

.ai-message__source-card-image-placeholder {
    width: 100%;
    height: 120px;
    border-radius: 8px;
    background: linear-gradient(135deg, var(--bg-secondary) 0%, var(--border-color) 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    flex-shrink: 0;
}

.ai-message__source-card-image-placeholder svg {
    width: 40px;
    height: 40px;
    opacity: 0.5;
}

/* Card Content */
.ai-message__source-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-height: 0;
}

.ai-message__source-link {
    color: inherit;
    text-decoration: none;
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex: 1;
}

.ai-message__source-title {
    font-weight: 600;
    line-height: 1.4;
    color: var(--text-primary);
    font-size: 14px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.ai-message__source-snippet {
    color: var(--text-secondary);
    font-size: 12px;
    line-height: 1.5;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    flex: 1;
}

.ai-message__source-url {
    color: var(--primary-color);
    font-size: 11px;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 4px;
    margin-top: auto;
}

.ai-message__source-url svg {
    width: 12px;
    height: 12px;
    flex-shrink: 0;
}

/* Loading state for web search */
.ai-typing-indicator--searching {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.ai-typing-indicator--searching::before {
    content: "🔍 Searching the web...";
    display: block;
    font-size: 12px;
    color: var(--primary-color);
    font-weight: 500;
}

/* ===== Markdown Formatting in AI Messages ===== */
.ai-message--ai .ai-message__text h1,
.ai-message--ai .ai-message__text h2,
.ai-message--ai .ai-message__text h3 {
    margin: 12px 0 8px 0;
    font-weight: 600;
    color: var(--text-primary);
}

.ai-message--ai .ai-message__text h1 {
    font-size: 20px;
}

.ai-message--ai .ai-message__text h2 {
    font-size: 18px;
}

.ai-message--ai .ai-message__text h3 {
    font-size: 16px;
}

.ai-message--ai .ai-message__text h1:first-child,
.ai-message--ai .ai-message__text h2:first-child,
.ai-message--ai .ai-message__text h3:first-child {
    margin-top: 0;
}

.ai-message--ai .ai-message__text p {
    margin: 8px 0;
}

.ai-message--ai .ai-message__text p:first-child {
    margin-top: 0;
}

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

.ai-message--ai .ai-message__text strong {
    font-weight: 600;
    color: var(--text-primary);
}

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

.ai-message--ai .ai-message__text code {
    background: rgba(59, 130, 246, 0.1);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Courier New', Courier, monospace;
    font-size: 13px;
    color: var(--primary-color);
}

.ai-message--ai .ai-message__text pre {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 12px;
    margin: 12px 0;
    overflow-x: auto;
}

.ai-message--ai .ai-message__text pre code {
    background: transparent;
    padding: 0;
    border-radius: 0;
    display: block;
    color: var(--text-primary);
    font-size: 13px;
    line-height: 1.6;
}

.ai-message--ai .ai-message__text ul,
.ai-message--ai .ai-message__text ol {
    margin: 8px 0;
    padding-left: 24px;
}

.ai-message--ai .ai-message__text li {
    margin: 4px 0;
    line-height: 1.6;
}

.ai-message--ai .ai-message__text a {
    color: var(--primary-color);
    text-decoration: underline;
}

.ai-message--ai .ai-message__text a:hover {
    text-decoration: none;
}

/* Typing cursor animation */
.typing-cursor::after {
    content: '▋';
    animation: blink 1s step-end infinite;
    margin-left: 2px;
    color: var(--primary-color);
}

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

/* Error Message Style */
.ai-message--error .ai-message__text {
    background: var(--error-color);
    color: #ffffff;
}

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

.ai-error {
    color: var(--error-color);
}

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

/* Tablet (768px - 1199px) */
@media (min-width: 768px) and (max-width: 1199px) {
    .ai-chatbox {
        max-width: 720px;
    }

    .ai-message {
        max-width: 75%;
    }

    .ai-chatbox__header-text h3 {
        font-size: 17px;
    }

    .ai-message__text {
        font-size: 14px;
    }
    
    .ai-message__source-item {
        flex: 0 0 calc(50% - 40px);
    }
}

/* Desktop (1200px and above) */
@media (min-width: 1200px) {
    .ai-chatbox {
        max-width: 800px;
    }

    .ai-message {
        max-width: 99%;
    }

    .ai-chatbox__header {
        padding: 20px;
    }

    .ai-chatbox__messages {
        padding: 20px;
    }

    .ai-prompt-suggestions {
        padding: 15px 20px 0;
    }

    .ai-chatbox__input-wrapper {
        padding: 15px 20px 20px;
    }
    
    .ai-message__source-item {
        flex: 0 0 calc(50% - 40px);
    }
}

/* Mobile (below 768px) */
@media (max-width: 767px) {
    .ai-chatbox-wrapper {
        height: calc(100vh - 120px);
    }

    .ai-chatbox {
        border-radius: 0;
    }

    .ai-chatbox__header-icon {
        width: 38px;
        height: 38px;
    }

    .ai-chatbox__header-icon svg {
        width: 22px;
        height: 22px;
    }

    .ai-chatbox__header-text h3 {
        font-size: 16px;
    }

    .ai-chatbox__header-text .ai-status {
        font-size: 12px;
    }

    .ai-message {
        max-width: 90%;
    }

    .ai-message__text {
        font-size: 14px;
        padding: 10px 14px;
    }

    .ai-message__time {
        font-size: 10px;
    }

    .ai-chatbox__input {
        font-size: 14px;
    }

    .ai-chatbox__send-btn {
        width: 32px;
        height: 32px;
    }

    .ai-chatbox__send-btn svg {
        width: 18px;
        height: 18px;
    }

    .ai-prompt-card {
        min-width: 110px;
        max-width: 130px;
        padding: 10px 14px;
        gap: 6px;
    }

    .ai-prompt-card__icon {
        width: 36px;
        height: 36px;
    }

    .ai-prompt-card__icon svg {
        width: 18px;
        height: 18px;
    }

    .ai-prompt-card__label {
        font-size: 11px;
    }
}

/* Extra small mobile (below 375px) */
@media (max-width: 374px) {
    .ai-chatbox__header {
        padding: 10px;
    }

    .ai-chatbox__messages {
        padding: 10px;
        gap: 12px;
    }

    .ai-chatbox__input-wrapper {
        padding: 10px;
    }

    .ai-prompt-suggestions {
        padding: 10px 10px 0;
    }

    .ai-prompt-card {
        min-width: 100px;
        max-width: 120px;
    }

    .ai-message {
        max-width: 99%;
    }

    .ai-message__text {
        font-size: 13px;
        padding: 8px 12px;
    }
    
    .ai-message__source-item {
        flex-direction: column;
        gap: 6px;
    }
    
    .ai-message__source-snippet {
        -webkit-line-clamp: 1;
    }
}

/* ===== Dark Mode Support ===== */
[data-theme="dark"] .ai-chatbox__send-btn:disabled {
    background: var(--text-secondary);
    opacity: 0.4;
}

[data-theme="dark"] .ai-message--ai .ai-message__text {
    background: var(--bg-secondary);
}

[data-theme="dark"] .ai-chatbox__messages {
    background: var(--bg-primary);
}

[data-theme="dark"] .ai-message--ai .ai-message__text code {
    background: rgba(59, 130, 246, 0.15);
}

[data-theme="dark"] .ai-message--ai .ai-message__text pre {
    background: rgba(0, 0, 0, 0.2);
    border-color: rgba(255, 255, 255, 0.1);
}

[data-theme="dark"] .ai-message__source-item {
    background: var(--bg-secondary);
    border-color: rgba(255, 255, 255, 0.1);
}

[data-theme="dark"] .ai-message__source-item:hover {
    border-color: var(--primary-color);
    background: rgba(59, 130, 246, 0.05);
}

[data-theme="dark"] .ai-message__source-image {
    border-color: var(--bg-secondary);
}

[data-theme="dark"] .ai-message__source-card-image-placeholder {
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.05) 0%, rgba(255, 255, 255, 0.1) 100%);
}

/* ===== Accessibility ===== */
.ai-chatbox__input:focus,
.ai-chatbox__send-btn:focus {
    outline: none;
}

.ai-chatbox__send-btn:focus-visible {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}
