/**
 * Analyzer Chat Widget Styling
 * Bottom-right minimizable drawer for study-aware financial advisor
 */

:root {
    --chat-bg: #ffffff;
    --chat-border: #e5e7eb;
    --chat-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
    --chat-user-bg: #3b82f6;
    --chat-user-text: #ffffff;
    --chat-assistant-bg: #f3f4f6;
    --chat-assistant-text: #1f2937;
    --chat-accent: #3b82f6;
}

/* Dark mode (match app v2 dashboard — html[data-theme="dark"]) */
html[data-theme="dark"] .analyzer-chat-drawer {
    --chat-bg: #1e293b;
    --chat-border: #334155;
    --chat-shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
    --chat-user-bg: #2563eb;
    --chat-assistant-bg: #0f172a;
    --chat-assistant-text: #e2e8f0;
}

@media (prefers-color-scheme: dark) {
    :root {
        --chat-bg: #1f2937;
        --chat-border: #374151;
        --chat-user-bg: #2563eb;
        --chat-assistant-bg: #111827;
        --chat-assistant-text: #f3f4f6;
    }
}

/* Main drawer container */
.analyzer-chat-drawer {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: min(420px, calc(100vw - 32px));
    max-height: min(90vh, 780px);
    display: flex;
    flex-direction: column;
    background: var(--chat-bg);
    border: 1px solid var(--chat-border);
    border-radius: 12px;
    box-shadow: var(--chat-shadow);
    z-index: 10050;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    font-size: 14px;
    animation: slideIn 0.3s ease-out;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(20px) translateX(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0) translateX(0);
    }
}

/* Minimized state */
.analyzer-chat-drawer.minimized {
    max-height: 50px;
}

.analyzer-chat-drawer.minimized .analyzer-chat-messages,
.analyzer-chat-drawer.minimized .analyzer-chat-input-area {
    display: none;
}

/* Header */
.analyzer-chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
    border-bottom: 1px solid var(--chat-border);
    cursor: pointer;
}

.analyzer-chat-title {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.analyzer-chat-title > span {
    font-weight: 600;
    color: var(--chat-accent);
    font-size: 14px;
}

.analyzer-chat-title > small {
    font-size: 12px;
    color: #6b7280;
    font-weight: 400;
}

/* Controls (minimize/close buttons) */
.analyzer-chat-controls {
    display: flex;
    gap: 8px;
}

.analyzer-chat-btn {
    background: transparent;
    border: none;
    color: #6b7280;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 16px;
    transition: all 0.2s ease;
}

.analyzer-chat-btn:hover {
    background: #f3f4f6;
    color: #1f2937;
}

@media (prefers-color-scheme: dark) {
    .analyzer-chat-btn:hover {
        background: #374151;
        color: #f3f4f6;
    }
}

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

/* Messages container — min-height:0 so flex layout allows scrolling (full reply visible) */
.analyzer-chat-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--chat-bg);
}

/* Welcome message */
.analyzer-chat-welcome {
    text-align: center;
    padding: 8px 0;
    color: #6b7280;
    font-size: 13px;
}

.analyzer-chat-welcome p {
    margin: 0 0 8px 0;
}

.analyzer-chat-welcome ul {
    margin: 8px 0;
    padding-left: 20px;
    text-align: left;
}

.analyzer-chat-welcome li {
    margin: 4px 0;
}

.analyzer-chat-welcome .hint {
    font-size: 12px;
    margin-top: 12px;
    font-style: italic;
}

.analyzer-chat-welcome kbd {
    background: var(--chat-accent);
    color: white;
    padding: 2px 6px;
    border-radius: 3px;
    font-family: 'Monaco', 'Courier New', monospace;
    font-size: 11px;
    font-weight: 600;
}

/* Message bubbles */
.analyzer-chat-bubble {
    padding: 10px 12px;
    border-radius: 8px;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    white-space: pre-wrap;
    line-height: 1.45;
    max-width: 100%;
    animation: fadeIn 0.2s ease-out;
}

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

.analyzer-chat-bubble-user {
    background: var(--chat-user-bg);
    color: var(--chat-user-text);
    margin-left: auto;
    max-width: 85%;
    border-bottom-right-radius: 4px;
}

.analyzer-chat-bubble-assistant {
    background: var(--chat-assistant-bg);
    color: var(--chat-assistant-text);
    margin-right: auto;
    max-width: 85%;
    border-bottom-left-radius: 4px;
}

.analyzer-chat-bubble-with-tool {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    max-width: 92%;
}

.analyzer-chat-bubble-text {
    white-space: normal;
    overflow-wrap: anywhere;
}

.analyzer-chat-bubble-text .analyzer-chat-p {
    margin: 0 0 0.65em;
    line-height: 1.5;
}

.analyzer-chat-bubble-text .analyzer-chat-p:last-child {
    margin-bottom: 0;
}

.analyzer-chat-bubble-text .analyzer-chat-ul,
.analyzer-chat-bubble-text .analyzer-chat-ol {
    margin: 0.35em 0 0.65em 1.1em;
    padding: 0;
    line-height: 1.45;
}

.analyzer-chat-bubble-text .analyzer-chat-li {
    margin: 0.25em 0;
}

.analyzer-chat-bubble-text strong {
    font-weight: 700;
    color: inherit;
}

.analyzer-chat-bubble-text .analyzer-chat-code {
    font-family: ui-monospace, 'Cascadia Code', 'Segoe UI Mono', monospace;
    font-size: 0.88em;
    padding: 0.1em 0.35em;
    border-radius: 4px;
    background: rgba(0, 0, 0, 0.12);
}

html[data-theme="dark"] .analyzer-chat-bubble-text .analyzer-chat-code {
    background: rgba(255, 255, 255, 0.1);
}

.analyzer-chat-tool-row {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding-top: 8px;
    border-top: 1px solid var(--chat-border);
}

.analyzer-chat-tool-preview {
    font-size: 11px;
    color: var(--chat-assistant-text);
    opacity: 0.85;
    line-height: 1.35;
}

.analyzer-chat-apply-btn {
    align-self: flex-start;
    padding: 8px 14px;
    border-radius: 8px;
    border: none;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    background: var(--chat-accent);
    color: #fff;
}

.analyzer-chat-apply-btn:hover:not(:disabled) {
    filter: brightness(1.08);
}

.analyzer-chat-apply-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* Input area */
.analyzer-chat-input-area {
    padding: 12px 16px;
    border-top: 1px solid var(--chat-border);
    background: var(--chat-bg);
}

.analyzer-chat-input-wrapper {
    display: flex;
    gap: 8px;
    margin-bottom: 8px;
}

.analyzer-chat-input {
    flex: 1;
    padding: 10px 12px;
    border: 1px solid var(--chat-border);
    border-radius: 6px;
    font-size: 13px;
    font-family: inherit;
    background: var(--chat-bg);
    color: var(--chat-assistant-text);
    transition: border-color 0.2s ease;
}

.analyzer-chat-input:focus {
    outline: none;
    border-color: var(--chat-accent);
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1);
}

@media (prefers-color-scheme: dark) {
    .analyzer-chat-input {
        background: #111827;
        border-color: #374151;
    }
    .analyzer-chat-input:focus {
        border-color: #2563eb;
        box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.15);
    }
}

.analyzer-chat-send {
    padding: 10px 14px;
    background: var(--chat-accent);
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
    font-size: 14px;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
}

.analyzer-chat-send:hover:not(:disabled) {
    background: #2563eb;
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
}

.analyzer-chat-send:active:not(:disabled) {
    transform: translateY(0);
}

.analyzer-chat-send:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.analyzer-chat-send.loading {
    animation: spin 1s linear infinite;
}

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

/* Hint text */
.analyzer-chat-hint {
    font-size: 11px;
    color: #9ca3af;
    text-align: center;
    font-style: italic;
}

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

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

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

.analyzer-chat-messages::-webkit-scrollbar-thumb:hover {
    background: #9ca3af;
}

@media (prefers-color-scheme: dark) {
    .analyzer-chat-messages::-webkit-scrollbar-thumb {
        background: #4b5563;
    }
    .analyzer-chat-messages::-webkit-scrollbar-thumb:hover {
        background: #6b7280;
    }
}

/* Mobile responsiveness */
@media (max-width: 768px) {
    .analyzer-chat-drawer {
        position: fixed;
        bottom: 0;
        right: 0;
        left: 0;
        width: 100%;
        max-height: 92vh;
        border-radius: 12px 12px 0 0;
        box-shadow: 0 -5px 20px rgba(0, 0, 0, 0.15);
    }

    .analyzer-chat-drawer.minimized {
        max-height: 50px;
        bottom: auto;
        top: auto;
        right: 0;
        left: auto;
        width: auto;
        min-width: 200px;
    }

    /* Let flex + min-height:0 fill the drawer so long replies scroll inside the panel */
    .analyzer-chat-messages {
        max-height: none;
    }
}

/* Very small screens */
@media (max-width: 480px) {
    .analyzer-chat-drawer {
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        max-height: 90vh;
        border-radius: 12px 12px 0 0;
    }

    .analyzer-chat-bubble-user,
    .analyzer-chat-bubble-assistant {
        max-width: 90%;
    }
}

/* Print styles */
@media print {
    .analyzer-chat-drawer {
        display: none !important;
    }
}
