/**
 * QuoteGPT Chat Sidebar Styles
 * PBI-38 Task 38-8: Chat Component Template
 *
 * Provides styling for the AI chat sidebar with:
 * - Collapsed/expanded states
 * - Message bubbles (user/assistant)
 * - Typing indicator animation
 * - Mobile responsive design
 */

/* ==========================================================================
   Chat Sidebar Container
   ========================================================================== */

.chat-sidebar {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 1040; /* Below Bootstrap modals (1055) but above most content */
    font-family: inherit;
}

/* Bug fix 038: Smart scroll lock - lock body scroll on mobile when chat expanded.
 * Only applies to floating chat (not embedded).
 * JS handles scroll position save/restore.
 * See: specs/038-mobile-style-refresh/spec.md FR-002, FR-003 */
@media (max-width: 767.98px) {
    body:has(.chat-sidebar.chat-expanded:not([data-embedded="true"])) {
        overflow: hidden;
        position: fixed;
        width: 100%;
        height: 100%;
    }
}

/* Collapsed state - just the toggle button */
.chat-sidebar.chat-collapsed .chat-container {
    display: none;
}

.chat-sidebar.chat-collapsed .chat-toggle-btn {
    display: flex;
}

/* Expanded state - full chat interface */
.chat-sidebar.chat-expanded .chat-container {
    display: flex;
}

.chat-sidebar.chat-expanded .chat-toggle-btn {
    display: none;
}

/* ==========================================================================
   Toggle Button (Collapsed State)
   ========================================================================== */

.chat-toggle-btn {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--bs-primary) 0%, #6a3a0a 100%);
    border: none;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-toggle-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
}

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

/* FAB icon stays larger (1.5rem) - primary action intentionally prominent */
.chat-toggle-btn .bi {
    font-size: 1.5rem;
}

/* Hide FAB on mobile - mini-console provides chat button (Spec 038 FR-008) */
@media (max-width: 767.98px) {
    .chat-sidebar.chat-collapsed .chat-toggle-btn {
        display: none;
    }
}

/* ==========================================================================
   Chat Container (Expanded State)
   ========================================================================== */

.chat-container {
    position: relative; /* For loading overlay positioning */
    /* Bug fix 038: Use min() for width calculation.
     * 100% (not 100vw) avoids scrollbar-width overflow issue.
     * Prevents chat from being wider than viewport on iPhone 16 Pro (393px).
     * See: specs/038-mobile-style-refresh/spec.md FR-004 */
    width: min(380px, calc(100% - 40px));
    max-width: calc(100% - 40px);
    /* Fill available height: viewport minus top margin (20px) and bottom position (20px) */
    height: calc(100vh - 40px);
    height: calc(100dvh - 40px); /* Modern browsers: accounts for mobile chrome */
    background: white;
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* Prevent dark corner artifacts from shadow/background sub-pixel rendering */
    isolation: isolate;
    -webkit-mask-image: -webkit-radial-gradient(white, black);
}

/* ==========================================================================
   Chat Header
   ========================================================================== */

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 12px;  /* Reduced to accommodate 44px buttons */
    background: linear-gradient(135deg, var(--bs-primary) 0%, #6a3a0a 100%);
    color: white;
    flex-shrink: 0;
}

.chat-title {
    font-weight: 600;
    font-size: 1.25rem;
    display: flex;
    align-items: center;
    color: inherit;
    text-decoration: none;
}

.chat-title:hover {
    color: inherit;
    opacity: 0.9;
}

/* Owl logo in header - sized to match header action buttons (44px) */
.chat-logo-icon {
    width: 44px;
    height: 44px;
    object-fit: contain;
}

.chat-header-actions {
    display: flex;
    gap: 8px;  /* Increased for 44px buttons */
}

.btn-header-action {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    width: 44px;         /* WCAG 2.5.5 touch target minimum */
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    border-radius: 8px;  /* Slightly larger to match bigger button */
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s ease;
    line-height: 1;
}

/* Bootstrap icon sizing in chat (vertical-align fixed globally in base.css) */
.chat-sidebar .bi {
    font-size: 1.25rem;  /* 20px - matches mini-console standard (spec 044) */
    line-height: 1;
}

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

/* ==========================================================================
   Chat Messages Area
   ========================================================================== */

.chat-messages {
    flex: 1;
    min-height: 0; /* Allow flex item to shrink below content size for scroll */
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: #f8f9fa;
}

/* Welcome message */
.chat-welcome {
    text-align: center;
    padding: 24px 16px;
    color: #495057;
}

.welcome-icon {
    width: 56px;
    height: 56px;
    background: linear-gradient(135deg, var(--bs-primary) 0%, #6a3a0a 100%);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 16px;
    color: white;
    font-size: 1.5rem;
    padding: 10px;
}

/* Owl logo in welcome message */
.welcome-logo {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

/* Message bubbles */
.chat-message {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: 16px;
    line-height: 1.5;
    font-size: 0.9375rem;
    word-wrap: break-word;
}

.chat-message.user {
    align-self: flex-end;
    background: var(--bs-primary);
    color: white;
    border-bottom-right-radius: 4px;
}

.chat-message.assistant {
    align-self: flex-start;
    background: white;
    color: #212529;
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

/* Results pane indicator (embedded mode) */
.chat-message.assistant.has-pane-results {
    background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 100%);
    color: #2e7d32;
    font-weight: 500;
    font-size: 0.875rem;
}

/* Tool call indicators */
.chat-tool-call {
    align-self: flex-start;
    background: rgba(var(--bs-primary-rgb), 0.1);
    color: var(--bs-primary);
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 0.8125rem;
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
}

.chat-tool-call i {
    font-size: 1rem;
}

.chat-tool-call i.spinning {
    animation: spin 1s linear infinite;
}

.chat-tool-call.completed {
    background: rgba(var(--bs-primary-rgb), 0.15);
    color: var(--bs-primary-dark, #704010);
}

.chat-tool-call.completed i {
    color: var(--bs-primary);
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

/* ==========================================================================
   Typing Indicator
   ========================================================================== */

.chat-typing {
    padding: 8px 16px;
    display: flex;
    align-items: center;
    gap: 8px;
    color: #6c757d;
    font-size: 0.875rem;
    background: #f8f9fa;
    border-top: 1px solid #e9ecef;
}

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

.typing-dot {
    width: 6px;
    height: 6px;
    background: var(--bs-primary);
    border-radius: 50%;
    animation: typing-bounce 1.4s ease-in-out infinite;
}

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

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

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

/* ==========================================================================
   Chat Input Form
   ========================================================================== */

.chat-input-form {
    padding: 12px 16px;
    background: white;
    border-top: 1px solid #e9ecef;
    border-radius: 0 0 16px 16px; /* Match parent container's bottom corners */
    flex-shrink: 0;
}

.chat-input-wrapper {
    display: flex;
    gap: 8px;
    align-items: center;
    max-width: 100%; /* Prevent overflow on iOS keyboard focus */
}

.chat-input {
    flex: 1;
    min-width: 0; /* Allow flex shrink below content size */
    padding: 10px 14px;
    border: 1px solid #dee2e6;
    border-radius: 24px;
    /* 16px minimum prevents iOS Safari auto-zoom on focus */
    font-size: 1rem;
    outline: none;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.chat-input:focus {
    border-color: var(--bs-primary);
    box-shadow: 0 0 0 3px rgba(139, 69, 19, 0.1);
}

.chat-input::placeholder {
    color: #adb5bd;
}

.btn-send {
    width: 44px;         /* WCAG 2.5.5 touch target minimum */
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    border-radius: 50%;
    background: var(--bs-primary);
    border: none;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s ease, transform 0.2s ease;
    flex-shrink: 0;
}

.btn-send:hover {
    background: #6a3a0a;
}

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

.btn-send:disabled {
    background: #adb5bd;
    cursor: not-allowed;
}

/* ==========================================================================
   Mobile Responsive
   ========================================================================== */

@media (max-width: 575.98px) {
    .chat-sidebar {
        bottom: 0;
        right: 0;
        left: 0;
    }

    .chat-sidebar.chat-collapsed {
        bottom: 20px;
        right: 20px;
        left: auto;
    }

    .chat-sidebar.chat-expanded {
        /* Position above mini-console (44px + safe area) so both are accessible */
        bottom: calc(44px + env(safe-area-inset-bottom, 0));
        right: 0;
        left: 0;
    }

    .chat-container {
        /* Bug fix 038: Full width on mobile.
         * Uses 100% not 100vw - 100vw includes scrollbar width causing overflow.
         * Prevents chat from being wider than iPhone 16 Pro viewport (393px). */
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        /* Fill viewport minus mini-console height (44px + safe area) */
        height: calc(100vh - 44px - env(safe-area-inset-bottom, 0));
        height: calc(100dvh - 44px - env(safe-area-inset-bottom, 0));
        max-height: calc(100vh - 44px - env(safe-area-inset-bottom, 0));
        max-height: calc(100dvh - 44px - env(safe-area-inset-bottom, 0));
        border-radius: 0; /* Full-screen on mobile - no rounded corners */
    }

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

    /* Chat sits above mini-console (which handles safe-area) so no extra safe-area needed */
    .chat-input-form {
        padding-bottom: 12px;
        border-radius: 0; /* No rounded corners on mobile - sits flush at bottom */
    }
}

/* Spec 040: Desktop chat width - 1/3 of viewport, clamped 380-520px.
 * Fills the margin-right space reserved by .chat-results-partial. */
@media (min-width: 768px) {
    .chat-container {
        width: clamp(380px, calc(33.33vw - 20px), 520px);
        max-width: 520px;
    }
}

/* ==========================================================================
   Partial Viewport Swap Layout (Spec 040)
   Swapped into main element alongside floating chat sidebar.
   Desktop: 2/3 results, 1/3 chat (chat capped at 520px)
   ========================================================================== */

/* Base partial styles */
.chat-results-partial {
    display: flex;
    padding: 24px;
    padding-top: calc(var(--navbar-height, 56px) + 24px); /* Account for fixed navbar */
    min-height: calc(100vh - var(--navbar-height, 56px));
}

.chat-results-partial .chat-results-main {
    flex: 1;
    max-width: 900px;
    overflow-y: auto;
}

.chat-results-partial .results-header {
    margin-bottom: 24px;
    padding-bottom: 16px;
    border-bottom: 1px solid #e9ecef;
}

.chat-results-partial #quote-results-container {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

/* 576-767px: Too small for side-by-side, stack or hide results */
@media (min-width: 576px) and (max-width: 767.98px) {
    .chat-results-partial {
        /* On small tablets, results go full width above chat */
        margin-right: 0;
        padding-bottom: 450px; /* Space for chat at bottom */
    }
}

/* 768px+: Side-by-side layout with 2/3:1/3 ratio.
 * margin-right must account for chat's 360px minimum width. */
@media (min-width: 768px) {
    .chat-results-partial {
        /* min: 360px chat + 40px buffer = 400px
         * preferred: 1/3 viewport + gap
         * max: 520px chat + 40px buffer = 560px */
        margin-right: clamp(400px, calc(33.33vw + 20px), 560px);
    }
}

/* ==========================================================================
   Dark Mode Support (future)
   ========================================================================== */

@media (prefers-color-scheme: dark) {
    /* Placeholder for dark mode styles */
}

/* ==========================================================================
   Accessibility
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
    .chat-toggle-btn,
    .btn-minimize,
    .btn-send,
    .chat-input {
        transition: none;
    }

    .typing-dot {
        animation: none;
        opacity: 0.6;
    }

    .chat-tool-call i.spinning {
        animation: none;
    }
}

/* Focus visible for keyboard navigation */
.chat-toggle-btn:focus-visible,
.btn-minimize:focus-visible,
.btn-send:focus-visible,
.chat-input:focus-visible {
    outline: 2px solid var(--bs-primary);
    outline-offset: 2px;
}

/* ==========================================================================
   Markdown Content Styling (for assistant responses)
   ========================================================================== */

.chat-message.assistant p {
    margin: 0 0 0.5em;
}

.chat-message.assistant p:last-child {
    margin-bottom: 0;
}

.chat-message.assistant code {
    background: #f1f3f4;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.875em;
}

.chat-message.assistant pre {
    background: #f1f3f4;
    padding: 8px 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 8px 0;
}

.chat-message.assistant ul,
.chat-message.assistant ol {
    margin: 8px 0;
    padding-left: 20px;
}

.chat-message.assistant li {
    margin-bottom: 4px;
}

.chat-message.assistant blockquote {
    border-left: 3px solid var(--bs-primary);
    padding-left: 12px;
    margin: 8px 0;
    color: #495057;
    font-style: italic;
}

/* ==========================================================================
   Inline Quote Cards (Mobile Chat)
   ========================================================================== */

.chat-inline-quotes {
    margin-top: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.chat-quote-card {
    background: #f8f9fa;
    border: 1px solid #e9ecef;
    border-radius: 12px;
    padding: 12px;
    font-size: 0.875rem;
}

.chat-quote-text {
    font-style: italic;
    color: #495057;
    line-height: 1.5;
    margin-bottom: 8px;
}

.chat-quote-attribution {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.chat-quote-author {
    color: #6c757d;
    font-size: 0.8125rem;
    display: flex;
    align-items: center;
    gap: 4px;
}

.chat-quote-author a {
    color: inherit;
    text-decoration: none;
}

.chat-quote-author a:hover {
    text-decoration: underline;
}

.chat-quote-actions {
    display: flex;
    gap: 8px;
}

.chat-quote-action-btn {
    background: transparent;
    border: 1px solid #dee2e6;
    border-radius: 6px;
    padding: 4px 8px;
    font-size: 0.75rem;
    color: #6c757d;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 4px;
    transition: all 0.2s ease;
    text-decoration: none;
}

.chat-quote-action-btn:hover {
    background: #e9ecef;
    border-color: #adb5bd;
    color: #495057;
}

.chat-quote-action-btn.collect-btn:hover {
    background: transparent;
    border-color: var(--qs-collect-fill-hover, #7B5229);
    color: var(--qs-collect-fill-hover, #7B5229);
}

.chat-quote-action-btn.collected {
    background: transparent;
    border-color: var(--qs-collect-fill, #654321);
    color: var(--qs-collect-fill, #654321);
}

@media (max-width: 767.98px) {
    /* Inline quotes always show on mobile */
    .chat-inline-quotes {
        display: flex;
    }
}

/* ==========================================================================
   Quote Action Bar (Spec 045 - inline buttons after quotes in chat)
   ========================================================================== */

.quote-action-bar {
    display: inline-flex;
    gap: 8px;
    margin: 0;
    padding: 4px 0;
}

.quote-action-btn {
    background: transparent;
    border: 1px solid var(--bs-border-color, #dee2e6);
    border-radius: 6px;
    padding: 6px 10px;
    font-size: 0.875rem;
    color: var(--bs-secondary, #6c757d);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    min-width: 44px;
    min-height: 44px;
    transition: all 0.2s ease;
}

.quote-action-btn:hover {
    background: var(--bs-light, #f8f9fa);
    border-color: var(--bs-secondary, #6c757d);
    color: var(--bs-body-color, #212529);
}

.quote-action-btn.copy-btn:hover {
    border-color: var(--bs-primary, #0d6efd);
    color: var(--bs-primary, #0d6efd);
}

/* T032: Share button hover state - matches copy button */
.quote-action-btn.share-btn:hover {
    border-color: var(--bs-primary, #0d6efd);
    color: var(--bs-primary, #0d6efd);
}

.quote-action-btn.collect-btn:hover {
    border-color: var(--qs-collect-fill-hover, #7B5229);
    color: var(--qs-collect-fill-hover, #7B5229);
}

.quote-action-btn.collect-btn.collected {
    border-color: var(--qs-collect-fill, #654321);
    color: var(--qs-collect-fill, #654321);
}

/* Quote action dropdown (copy format selector) */
.quote-action-dropdown {
    display: inline-flex;
}

.quote-action-dropdown .quote-action-btn {
    border-radius: 6px 0 0 6px;
}

.quote-action-dropdown .dropdown-toggle-split {
    background: transparent;
    border: 1px solid var(--bs-border-color, #dee2e6);
    border-left: 0;
    border-radius: 0 6px 6px 0;
    padding: 6px 8px;
    min-width: auto;
    min-height: 44px;
    color: var(--bs-secondary, #6c757d);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
}

.quote-action-dropdown .dropdown-toggle-split:hover {
    background: var(--bs-light, #f8f9fa);
    border-color: var(--bs-primary, #0d6efd);
    color: var(--bs-primary, #0d6efd);
}

.quote-action-dropdown .dropdown-menu {
    font-size: 0.875rem;
    min-width: 10rem;
}

.quote-action-dropdown .dropdown-item {
    display: flex;
    align-items: center;
    padding: 0.5rem 0.75rem;
}

.quote-action-dropdown .dropdown-item i {
    width: 1rem;
    text-align: center;
}

@media (prefers-reduced-motion: reduce) {
    .quote-action-btn {
        transition: none;
    }
}

/* ==========================================================================
   Navigation Loading Overlay
   ========================================================================== */

.chat-nav-loading {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.95);
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 10;
    border-radius: 16px;
}

@media (prefers-reduced-motion: reduce) {
    .chat-quote-card {
        transition: none;
        animation: none;
    }
}

/* ==========================================================================
   Theme Chips (Clickable theme suggestions in AI responses)
   ========================================================================== */

.theme-chip {
    display: inline-block;
    padding: 4px 12px;
    margin: 2px 4px;
    background: rgba(var(--bs-primary-rgb), 0.1);
    color: var(--bs-primary);
    border: 1px solid rgba(var(--bs-primary-rgb), 0.3);
    border-radius: 16px;
    font-size: 0.875rem;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.15s ease;
    text-decoration: none;
    line-height: 1.4;
}

.theme-chip:hover {
    background: var(--bs-primary);
    color: white;
    border-color: var(--bs-primary);
    transform: translateY(-1px);
}

.theme-chip:active {
    transform: translateY(0);
}

.theme-chip:focus-visible {
    outline: 2px solid var(--bs-primary);
    outline-offset: 2px;
}

/* Mobile: larger touch targets */
@media (max-width: 768px) {
    .theme-chip {
        padding: 6px 14px;
        font-size: 0.9375rem;
        margin: 3px 5px;
    }
}

/* Reduced motion preference */
@media (prefers-reduced-motion: reduce) {
    .theme-chip {
        transition: none;
        transform: none !important;
    }
}

/* ==========================================================================
   Error Handling States (T057-T059)
   ========================================================================== */

.chat-message.error {
    background: #fef2f2;
    color: #991b1b;
    border: 1px solid #fecaca;
}

.chat-message.error-recoverable {
    background: #fffbeb;
    color: #92400e;
    border: 1px solid #fde68a;
}

.chat-message.error-terminal {
    background: #fef2f2;
    color: #991b1b;
    border: 1px solid #fecaca;
}

.chat-message.error .error-message {
    display: block;
    margin-bottom: 0;
}

.chat-message.error .retry-btn {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 0.8125rem;
    padding: 4px 12px;
    border-radius: 16px;
}

.chat-message.error .retry-btn:hover {
    background: var(--bs-primary);
    color: white;
    border-color: var(--bs-primary);
}
