/**
 * Chat Widget Styles - CoCreate AI
 * Fully responsive, theme-aware, corporate styling
 *
 * Features:
 * - Resizable sidebar (300px - 600px)
 * - Voice input with Web Speech API
 * - Screenshot capture with html2canvas
 * - Token usage tracking
 * - Markdown rendering
 * - Full responsive design (mobile, tablet, desktop)
 * - Theme persistence support
 */

/* ========== Corporate Font Import ========== */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap');

/* ========== Theme Variables (defaults) ========== */
:root {
    /* Chat widget specific */
    --chat-primary: #6366f1;
    --chat-secondary: #8b5cf6;
    --chat-bg: #1e293b;
    --chat-messages-bg: #0f172a;
    --chat-input-bg: #0f172a;
    --chat-border: #334155;
    --chat-text-primary: #f1f5f9;
    --chat-text-secondary: #cbd5e1;
    --chat-text-muted: #94a3b8;
    --chat-msg-assistant-bg: #1e293b;
    --chat-msg-assistant-text: #e2e8f0;

    /* Font family */
    --font-primary: 'Plus Jakarta Sans', 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --font-mono: 'SF Mono', 'Monaco', 'Menlo', 'Consolas', monospace;
}

/* Light theme overrides */
[data-theme="light"] {
    --chat-bg: #ffffff;
    --chat-messages-bg: #f8fafc;
    --chat-input-bg: #f1f5f9;
    --chat-border: #e2e8f0;
    --chat-text-primary: #1e293b;
    --chat-text-secondary: #475569;
    --chat-text-muted: #64748b;
    --chat-msg-assistant-bg: #ffffff;
    --chat-msg-assistant-text: #334155;
}

[data-theme="light"] .chat-status {
    color: rgba(0, 0, 0, 0.7);
}

[data-theme="light"] .chat-session-info {
    color: rgba(0, 0, 0, 0.5);
}

[data-theme="light"] .info-tooltip {
    color: rgba(0, 0, 0, 0.4);
}

[data-theme="light"] .info-tooltip:hover {
    color: rgba(0, 0, 0, 0.8);
}

/* ========== Chat Sidebar ========== */
.chat-sidebar {
    position: fixed;
    right: -420px;
    top: var(--nav-height, 72px); /* Below navigation bar */
    width: 380px;
    min-width: 300px;
    max-width: 600px;
    height: calc(100vh - var(--nav-height, 72px));
    height: calc(100dvh - var(--nav-height, 72px)); /* Dynamic viewport height for mobile */
    background: var(--chat-bg, #1e293b);
    border-left: 1px solid var(--chat-border, #334155);
    display: flex;
    flex-direction: column;
    z-index: 10020; /* Above navigation for clean overlap */
    transition: right 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: -10px 0 40px rgba(0, 0, 0, 0.3);
    font-family: var(--font-primary);
    border-top-left-radius: 16px;
}

.chat-sidebar.open {
    right: 0;
}

/* Minimized state - collapse to header only */
.chat-sidebar.minimized {
    height: auto !important;
    min-height: auto !important;
}

.chat-sidebar.minimized .chat-messages,
.chat-sidebar.minimized .chat-input-area,
.chat-sidebar.minimized .chat-screenshot-preview,
.chat-sidebar.minimized .chat-token-display,
.chat-sidebar.minimized .chat-resize-handle {
    display: none !important;
}

.chat-sidebar.minimized .chat-header {
    border-radius: 16px 0 0 16px;
}

/* Minimize button styling */
.chat-minimize {
    background: transparent;
    border: none;
    color: var(--chat-text-secondary);
    cursor: pointer;
    padding: 6px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.chat-minimize:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--chat-text-primary);
}

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

/* ========== Resize Handle ========== */
.chat-resize-handle {
    position: absolute;
    left: 0;
    top: 0;
    width: 6px;
    height: 100%;
    cursor: ew-resize;
    background: transparent;
    z-index: 10;
    transition: background 0.2s;
}

.chat-resize-handle:hover,
.chat-resize-handle.dragging {
    background: var(--chat-primary, #6366f1);
}

.chat-resize-handle::before {
    content: '';
    position: absolute;
    left: 1px;
    top: 50%;
    transform: translateY(-50%);
    width: 4px;
    height: 40px;
    background: var(--chat-border, #334155);
    border-radius: 2px;
    opacity: 0.5;
}

.chat-resize-handle:hover::before {
    opacity: 1;
    background: white;
}

/* ========== Chat Header ========== */
.chat-header {
    background: linear-gradient(135deg, var(--chat-primary, #6366f1) 0%, var(--chat-secondary, #8b5cf6) 100%);
    padding: 16px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    min-height: 76px;
    position: relative;
    z-index: 2;
}

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

.chat-avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    position: relative;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f857a6 100%);
    background-size: 200% 200%;
    animation: avatarGradient 8s ease infinite, avatarBreathe 3s ease-in-out infinite;
    box-shadow:
        0 0 20px rgba(99, 102, 241, 0.4),
        0 0 40px rgba(139, 92, 246, 0.2),
        inset 0 0 20px rgba(255, 255, 255, 0.1);
}

/* Gradient animation */
@keyframes avatarGradient {
    0%, 100% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
}

/* Breathing animation (idle state) */
@keyframes avatarBreathe {
    0%, 100% {
        transform: scale(1);
        box-shadow:
            0 0 20px rgba(99, 102, 241, 0.4),
            0 0 40px rgba(139, 92, 246, 0.2),
            inset 0 0 20px rgba(255, 255, 255, 0.1);
    }
    50% {
        transform: scale(1.08);
        box-shadow:
            0 0 25px rgba(99, 102, 241, 0.6),
            0 0 50px rgba(139, 92, 246, 0.3),
            inset 0 0 25px rgba(255, 255, 255, 0.15);
    }
}

/* Thinking state (active pulsing when bot is typing) */
.chat-avatar.thinking {
    animation: avatarGradient 4s ease infinite, avatarThinking 1s ease-in-out infinite;
}

@keyframes avatarThinking {
    0%, 100% {
        transform: scale(1);
        box-shadow:
            0 0 30px rgba(99, 102, 241, 0.7),
            0 0 60px rgba(139, 92, 246, 0.4),
            inset 0 0 30px rgba(255, 255, 255, 0.2);
    }
    50% {
        transform: scale(1.15);
        box-shadow:
            0 0 40px rgba(99, 102, 241, 0.9),
            0 0 80px rgba(139, 92, 246, 0.6),
            inset 0 0 40px rgba(255, 255, 255, 0.3);
    }
}

/* Inner orb glow */
.chat-avatar::before {
    content: '';
    position: absolute;
    width: 60%;
    height: 60%;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(255, 255, 255, 0.8) 0%, rgba(255, 255, 255, 0) 70%);
    animation: innerGlow 2s ease-in-out infinite;
}

@keyframes innerGlow {
    0%, 100% { opacity: 0.3; transform: scale(1); }
    50% { opacity: 0.6; transform: scale(1.1); }
}

/* Sparkle effect */
.chat-avatar::after {
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: linear-gradient(45deg, transparent 30%, rgba(255, 255, 255, 0.3) 50%, transparent 70%);
    background-size: 200% 200%;
    animation: sparkle 3s linear infinite;
    pointer-events: none;
}

@keyframes sparkle {
    0% { background-position: -100% -100%; opacity: 0; }
    10% { opacity: 1; }
    90% { opacity: 1; }
    100% { background-position: 200% 200%; opacity: 0; }
}

.chat-header h3 {
    color: white;
    font-size: 16px;
    font-weight: 700;
    margin: 0;
    letter-spacing: -0.02em;
}

.chat-status {
    color: rgba(255, 255, 255, 0.8);
    font-size: 12px;
    margin: 2px 0 0 0;
    font-weight: 500;
}

.chat-session-info {
    color: rgba(255, 255, 255, 0.6);
    font-size: 11px;
    margin: 4px 0 0 0;
    font-weight: 400;
    display: flex;
    align-items: center;
    gap: 4px;
}

.info-tooltip {
    display: inline-flex;
    align-items: center;
    cursor: help;
    position: relative;
    color: rgba(255, 255, 255, 0.5);
    transition: color 0.2s ease;
}

.info-tooltip:hover {
    color: rgba(255, 255, 255, 0.9);
}

.info-tooltip svg {
    vertical-align: middle;
}

/* Custom tooltip */
.info-tooltip::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: calc(100% + 8px);
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.95);
    color: #ffffff;
    padding: 10px 14px;
    border-radius: 8px;
    font-size: 12px;
    font-weight: 400;
    line-height: 1.5;
    white-space: normal;
    width: max-content;
    max-width: 280px;
    text-align: left;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease, visibility 0.2s ease;
    z-index: 10000;
    pointer-events: none;
}

/* Tooltip arrow */
.info-tooltip::before {
    content: '';
    position: absolute;
    bottom: calc(100% + 2px);
    left: 50%;
    transform: translateX(-50%);
    border: 6px solid transparent;
    border-top-color: rgba(0, 0, 0, 0.95);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease, visibility 0.2s ease;
    z-index: 10000;
    pointer-events: none;
}

.info-tooltip:hover::after,
.info-tooltip:hover::before {
    opacity: 1;
    visibility: visible;
}

.chat-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* Header action buttons */
.chat-header-actions button {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: #fff;
    cursor: pointer;
    transition: all 0.2s ease;
}

.chat-header-actions button:hover {
    background: rgba(255, 255, 255, 0.25);
    border-color: rgba(255, 255, 255, 0.4);
    transform: translateY(-1px);
}

.chat-header-actions button svg {
    width: 18px;
    height: 18px;
    stroke: currentColor;
}

/* ========== Close Button (Highly Visible) ========== */
.chat-close {
    flex-shrink: 0;
}

.chat-close:hover {
    transform: translateY(-1px);
}

.chat-close svg {
    stroke-width: 2.5;
}

/* ========== Messages Area ========== */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    background: var(--chat-messages-bg, #0f172a);
    -webkit-overflow-scrolling: touch; /* Smooth scroll on iOS */
}

/* Message Row + Avatars */
.chat-message-row {
    display: flex;
    gap: 10px;
    align-items: flex-end;
}

.chat-message-row.user {
    justify-content: flex-end;
}

.chat-bubble-avatar {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.04em;
    color: #fff;
    flex-shrink: 0;
}

.chat-bubble-avatar.assistant {
    background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
    box-shadow: 0 6px 16px rgba(99, 102, 241, 0.35);
}

.chat-bubble-avatar.user {
    background: linear-gradient(135deg, #0f172a 0%, #334155 100%);
    border: 1px solid rgba(148, 163, 184, 0.4);
}

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

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

.chat-messages::-webkit-scrollbar-thumb {
    background: var(--chat-border, #334155);
    border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: var(--chat-text-muted, #94a3b8);
}

.chat-message {
    max-width: 85%;
    padding: 16px 18px;
    border-radius: 18px;
    font-size: 15px;
    line-height: 1.6;
    word-wrap: break-word;
    animation: messageSlide 0.3s ease;
    font-weight: 400;
}

.chat-message.assistant {
    background: var(--chat-msg-assistant-bg, #1e293b);
    color: var(--chat-msg-assistant-text, #e2e8f0);
    border: 1px solid rgba(148, 163, 184, 0.14);
    box-shadow: 0 12px 30px rgba(15, 23, 42, 0.22);
}

.chat-message.user {
    background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
    color: #fff;
    border: none;
    box-shadow: 0 10px 24px rgba(99, 102, 241, 0.35);
}

/* Clean markdown rendering - no italics, proper formatting */
.chat-message.assistant p {
    margin: 0 0 12px 0;
}

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

.chat-message.assistant strong {
    font-weight: 700;
    color: inherit;
}

.chat-message.assistant em {
    font-style: normal;
    font-weight: 500;
}

.chat-message.assistant ul,
.chat-message.assistant ol {
    margin: 12px 0;
    padding-left: 24px;
    list-style: disc;
}

.chat-message.assistant ol {
    list-style: decimal;
}

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

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

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

.chat-message.assistant {
    background: var(--chat-msg-assistant-bg, #1e293b);
    color: var(--chat-msg-assistant-text, #e2e8f0);
    border: 1px solid var(--chat-border, #334155);
    border-bottom-left-radius: 4px;
    align-self: flex-start;
}

.chat-message.user {
    background: linear-gradient(135deg, var(--chat-primary, #6366f1) 0%, var(--chat-secondary, #8b5cf6) 100%);
    color: white;
    border-bottom-right-radius: 4px;
    align-self: flex-end;
    font-weight: 500;
}

/* ========== Typing Indicator ========== */
.chat-typing {
    display: flex;
    gap: 4px;
    padding: 12px 16px;
    background: var(--chat-msg-assistant-bg, #1e293b);
    border: 1px solid var(--chat-border, #334155);
    border-radius: 16px;
    border-bottom-left-radius: 4px;
    width: fit-content;
}

.chat-typing span {
    width: 8px;
    height: 8px;
    background: var(--chat-primary, #6366f1);
    border-radius: 50%;
    animation: typingDot 1.4s infinite ease-in-out;
}

.chat-typing span:nth-child(2) { animation-delay: 0.2s; }
.chat-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingDot {
    0%, 80%, 100% { transform: scale(0.6); opacity: 0.5; }
    40% { transform: scale(1); opacity: 1; }
}

/* ========== Token Usage Display ========== */
.chat-token-display {
    padding: 8px 20px;
    border-top: 1px solid var(--chat-border, #334155);
    background: var(--chat-bg, #1e293b);
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 11px;
    color: var(--chat-text-muted, #94a3b8);
    font-weight: 500;
}

.chat-token-display .token-stats {
    display: flex;
    gap: 12px;
}

.chat-token-display .token-cost {
    font-weight: 600;
    color: var(--chat-text-secondary, #cbd5e1);
}

/* ========== Input Area ========== */
.chat-input-area {
    padding: 8px 12px;
    padding-bottom: max(8px, env(safe-area-inset-bottom)); /* iOS safe area */
    border-top: 1px solid var(--chat-border, #334155);
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--chat-bg, #1e293b);
    flex-shrink: 0;
}

.chat-input-row {
    display: flex;
    gap: 6px;
    align-items: flex-end;
    width: 100%;
}

.chat-input-area textarea {
    flex: 1;
    background: var(--chat-input-bg, #0f172a);
    border: 1px solid var(--chat-border, #334155);
    border-radius: 16px;
    padding: 8px 14px;
    font-size: 14px;
    font-family: var(--font-primary);
    font-weight: 400;
    color: var(--chat-text-primary, #f1f5f9);
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
    resize: none;
    min-height: 40px;
    max-height: 120px;
    line-height: 1.4;
    overflow-y: auto;
}

.chat-input-area textarea:focus {
    border-color: var(--chat-primary, #6366f1);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.chat-input-area textarea::placeholder {
    color: var(--chat-text-muted, #64748b);
    font-weight: 400;
}

.chat-input-area textarea.listening {
    border-color: #ef4444;
    background: rgba(239, 68, 68, 0.1);
}

/* ========== Attach Button ========== */
.chat-attach-btn {
    width: 42px;
    height: 42px;
    background: var(--chat-input-bg, #0f172a);
    border: 1px solid var(--chat-border, #334155);
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.chat-attach-btn:hover {
    background: var(--chat-bg, #1e293b);
    border-color: var(--chat-primary, #6366f1);
}

.chat-attach-btn svg {
    width: 18px;
    height: 18px;
    stroke: var(--chat-text-muted, #94a3b8);
}

/* Header screenshot button active state */
.chat-screenshot.active {
    background: rgba(99, 102, 241, 0.35);
    border-color: rgba(99, 102, 241, 0.6);
    color: #fff;
}

/* ========== Send Button ========== */
.chat-send {
    width: 42px;
    height: 42px;
    background: linear-gradient(135deg, var(--chat-primary, #6366f1) 0%, var(--chat-secondary, #8b5cf6) 100%);
    border: none;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, box-shadow 0.2s;
    flex-shrink: 0;
}

.chat-send:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 20px rgba(99, 102, 241, 0.4);
}

.chat-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

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

/* ========== Voice Input Button ========== */
.chat-voice {
    width: 42px;
    height: 42px;
    background: var(--chat-input-bg, #0f172a);
    border: 1px solid var(--chat-border, #334155);
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.chat-voice:hover {
    background: var(--chat-bg, #1e293b);
    border-color: var(--chat-primary, #6366f1);
}

.chat-voice.listening {
    background: #ef4444;
    border-color: #ef4444;
    animation: voicePulse 1.5s infinite;
}

.chat-voice.listening svg {
    stroke: white;
}

.chat-voice svg {
    width: 18px;
    height: 18px;
    stroke: var(--chat-text-muted, #94a3b8);
}

.chat-voice.hidden {
    display: none;
}

@keyframes voicePulse {
    0%, 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4); }
    50% { transform: scale(1.05); box-shadow: 0 0 0 10px rgba(239, 68, 68, 0); }
}

/* ========== Attach / Screenshot Buttons ========== */
.chat-attach,
.chat-screenshot {
    width: 36px;
    height: 36px;
    background: transparent;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    opacity: 0.6;
    color: white;
}

.chat-attach:hover,
.chat-screenshot:hover {
    opacity: 1;
    background: rgba(255, 255, 255, 0.1);
}

.chat-screenshot.active {
    opacity: 1;
    color: var(--chat-primary, #6366f1);
}

.chat-attach svg,
.chat-screenshot svg {
    width: 18px;
    height: 18px;
    stroke: currentColor;
}

input.chat-file-input {
    display: none !important;
}

/* ========== Screenshot Preview ========== */
.chat-screenshot-preview {
    background: var(--chat-input-bg, #0f172a);
    border: 1px solid var(--chat-border, #334155);
    border-radius: 8px;
    padding: 8px;
    display: none;
}

.chat-screenshot-preview.visible {
    display: block;
}

.chat-screenshot-preview img {
    width: 100%;
    border-radius: 4px;
    max-height: 100px;
    object-fit: cover;
}

.chat-screenshot-preview .preview-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 6px;
    font-size: 11px;
    color: var(--chat-text-muted, #94a3b8);
}

.chat-screenshot-preview .remove-screenshot {
    background: none;
    border: none;
    color: #ef4444;
    cursor: pointer;
    font-size: 11px;
    font-family: var(--font-primary);
}

.chat-screenshot-preview .remove-screenshot:hover {
    text-decoration: underline;
}

/* ========== Clear Chat Button ========== */
.chat-clear {
    background: transparent;
    border: none;
    color: rgba(255, 255, 255, 0.6);
    font-size: 12px;
    font-weight: 500;
    font-family: var(--font-primary);
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    transition: all 0.2s;
}

.chat-clear:hover {
    color: #ef4444;
    background: rgba(239, 68, 68, 0.1);
}

/* ========== Markdown Styles ========== */
.chat-message.assistant code {
    background: rgba(99, 102, 241, 0.2);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: var(--font-mono);
    font-size: 13px;
}

.chat-message.assistant pre {
    background: var(--chat-input-bg, #0f172a);
    border: 1px solid var(--chat-border, #334155);
    border-radius: 8px;
    padding: 12px;
    margin: 8px 0;
    overflow-x: auto;
    font-size: 12px;
}

.chat-message.assistant pre code {
    background: none;
    padding: 0;
}

/* Links in messages */

.chat-message.assistant a {
    color: var(--chat-primary, #6366f1);
    text-decoration: underline;
    font-weight: 500;
}

.chat-message.assistant a:hover {
    opacity: 0.8;
}

/* ========== Voice Error Message ========== */
.voice-error {
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: #ef4444;
    color: white;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 500;
    white-space: nowrap;
    margin-bottom: 8px;
    animation: fadeInOut 3s forwards;
    z-index: 10;
}

@keyframes fadeInOut {
    0% { opacity: 0; transform: translateX(-50%) translateY(10px); }
    10% { opacity: 1; transform: translateX(-50%) translateY(0); }
    80% { opacity: 1; }
    100% { opacity: 0; }
}

/* ========== Chat Toggle Button ========== */
.chat-toggle {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 60px;
    height: 60px;
    background: linear-gradient(135deg, var(--chat-primary, #6366f1) 0%, var(--chat-secondary, #8b5cf6) 100%);
    border: none;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9998; /* Below sidebar (9999) */
    box-shadow: 0 4px 20px rgba(99, 102, 241, 0.4);
    transition: transform 0.3s, box-shadow 0.3s;
}

.chat-toggle:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 30px rgba(99, 102, 241, 0.5);
}

.chat-toggle.hidden {
    transform: scale(0);
    opacity: 0;
    pointer-events: none;
}

.chat-toggle svg {
    width: 28px;
    height: 28px;
    stroke: white;
}

.chat-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    width: 22px;
    height: 22px;
    background: #ef4444;
    border-radius: 50%;
    color: white;
    font-size: 12px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: var(--font-primary);
}

/* Dot indicator for existing conversation */
.chat-badge.dot-indicator {
    width: 12px;
    height: 12px;
    min-width: 12px;
    padding: 0;
    font-size: 0;
    background: #22c55e;
    border: 2px solid var(--chat-bg, #1a1a2e);
    box-shadow: 0 0 8px rgba(34, 197, 94, 0.5);
}

/* ========== RESPONSIVE DESIGN ========== */

/* Large Desktop (1400px+) */
@media (min-width: 1400px) {
    .chat-sidebar {
        width: 420px;
    }
}

/* Desktop (1024px - 1399px) */
@media (min-width: 1024px) and (max-width: 1399px) {
    .chat-sidebar {
        width: 380px;
    }
}

/* Tablet Landscape (768px - 1023px) */
@media (min-width: 768px) and (max-width: 1023px) {
    .chat-sidebar {
        width: 360px;
    }

    .chat-toggle {
        width: 56px;
        height: 56px;
    }

    .chat-toggle svg {
        width: 26px;
        height: 26px;
    }
}

/* Tablet Portrait (600px - 767px) */
@media (min-width: 600px) and (max-width: 767px) {
    .chat-sidebar {
        width: 340px;
        right: -340px;
        top: 68px;
        height: calc(100vh - 68px);
        height: calc(100dvh - 68px);
    }

    .chat-header {
        padding: 14px 16px;
    }

    .chat-messages {
        padding: 16px;
        gap: 14px;
    }

    .chat-input-area {
        padding: 14px 16px;
    }

    .chat-toggle {
        bottom: 20px;
        right: 20px;
        width: 54px;
        height: 54px;
    }
}

/* Mobile Large (480px - 599px) */
@media (min-width: 480px) and (max-width: 599px) {
    .chat-sidebar {
        width: 90vw;
        max-width: 320px;
        right: -90vw;
        top: 66px;
        height: calc(100vh - 66px);
        height: calc(100dvh - 66px);
    }

    .chat-header {
        padding: 12px 16px;
        min-height: 68px;
    }

    .chat-avatar {
        width: 40px;
        height: 40px;
    }

    .chat-header h3 {
        font-size: 15px;
    }

    .chat-close {
        width: 38px;
        height: 38px;
    }

    .chat-messages {
        padding: 14px;
        gap: 12px;
    }

    .chat-message {
        max-width: 90%;
        padding: 10px 14px;
        font-size: 14px;
    }

    .chat-input-area {
        padding: 12px 14px;
    }

    .chat-input-area textarea {
        padding: 10px 16px;
        font-size: 14px;
    }

    .chat-voice, .chat-send {
        width: 44px;
        height: 44px;
    }

    .chat-toggle {
        bottom: 16px;
        right: 16px;
        width: 52px;
        height: 52px;
    }

    .chat-resize-handle {
        display: none;
    }
}

/* Mobile Small (< 480px) */
@media (max-width: 479px) {
    .chat-sidebar {
        width: 100%;
        max-width: 100%;
        right: -100%;
        top: 64px; /* Smaller nav on mobile */
        height: calc(100vh - 64px);
        height: calc(100dvh - 64px);
        border-left: none;
        border-radius: 0;
        border-top-left-radius: 0;
    }

    .chat-sidebar.open {
        right: 0;
    }

    .chat-header {
        padding: 12px 16px;
        min-height: 64px;
    }

    .chat-avatar {
        width: 36px;
        height: 36px;
        /* Reduce animations on mobile for performance */
        animation: avatarGradient 12s ease infinite;
    }

    .chat-avatar::before {
        animation: innerGlow 4s ease-in-out infinite;
    }

    .chat-avatar::after {
        animation: none; /* Disable sparkle on mobile */
    }

    .chat-avatar.thinking {
        animation: avatarGradient 8s ease infinite, avatarThinking 1.5s ease-in-out infinite;
    }

    .chat-header h3 {
        font-size: 14px;
    }

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

    .chat-session-info {
        font-size: 10px;
    }

    .info-tooltip::after {
        max-width: 240px;
        font-size: 11px;
        padding: 8px 12px;
    }

    .chat-close {
        width: 36px;
        height: 36px;
        border-radius: 10px;
    }

    .chat-close svg {
        width: 20px;
        height: 20px;
    }

    .chat-messages {
        padding: 12px;
        gap: 10px;
    }

    .chat-message {
        max-width: 92%;
        padding: 10px 12px;
        font-size: 13px;
        border-radius: 14px;
    }

    .chat-message.assistant {
        border-bottom-left-radius: 4px;
    }

    .chat-message.user {
        border-bottom-right-radius: 4px;
    }

    .chat-input-area {
        padding: 10px 12px;
        gap: 10px;
    }

    .chat-input-row {
        gap: 8px;
    }

    .chat-input-area textarea {
        padding: 10px 14px;
        font-size: 14px;
        min-height: 44px;
        border-radius: 18px;
    }

    .chat-voice {
        width: 40px;
        height: 40px;
    }

    .chat-send {
        width: 40px;
        height: 40px;
    }

    .chat-voice svg, .chat-send svg {
        width: 18px;
        height: 18px;
    }

    .chat-toggle {
        bottom: 16px;
        right: 16px;
        width: 52px;
        height: 52px;
    }

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

    .chat-resize-handle {
        display: none;
    }

    .chat-token-display {
        padding: 6px 12px;
        font-size: 10px;
    }

    .chat-screenshot-preview {
        padding: 6px;
    }

    .chat-screenshot-preview img {
        max-height: 80px;
    }
}

/* Extra Small Mobile (< 360px) */
@media (max-width: 359px) {
    .chat-header {
        padding: 10px 12px;
    }

    .chat-header-info {
        gap: 8px;
    }

    .chat-avatar {
        width: 32px;
        height: 32px;
    }

    .chat-header h3 {
        font-size: 13px;
    }

    .chat-close {
        width: 32px;
        height: 32px;
    }

    .chat-messages {
        padding: 10px;
    }

    .chat-message {
        padding: 8px 10px;
        font-size: 12px;
    }

    .chat-input-area {
        padding: 8px 10px;
    }

    .chat-input-area textarea {
        padding: 8px 12px;
        font-size: 13px;
    }

    .chat-voice, .chat-send {
        width: 36px;
        height: 36px;
    }

    .chat-toggle {
        width: 48px;
        height: 48px;
        bottom: 12px;
        right: 12px;
    }
}

/* ========== iOS Safe Area Support ========== */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
    .chat-sidebar {
        padding-bottom: env(safe-area-inset-bottom);
    }

    .chat-input-area {
        padding-bottom: max(16px, env(safe-area-inset-bottom));
    }

    .chat-toggle {
        bottom: max(24px, calc(env(safe-area-inset-bottom) + 16px));
    }

    @media (max-width: 479px) {
        .chat-toggle {
            bottom: max(16px, calc(env(safe-area-inset-bottom) + 12px));
        }
    }
}

/* ========== Landscape Orientation ========== */
@media (max-height: 500px) and (orientation: landscape) {
    .chat-sidebar {
        width: 50vw;
        max-width: 400px;
    }

    .chat-header {
        padding: 10px 16px;
        min-height: auto;
    }

    .chat-messages {
        padding: 10px 16px;
    }

    .chat-input-area {
        padding: 10px 16px;
    }
}

/* ========== Print Styles ========== */
@media print {
    .chat-sidebar,
    .chat-toggle {
        display: none !important;
    }
}

/* ========== Reduced Motion ========== */
@media (prefers-reduced-motion: reduce) {
    .chat-sidebar,
    .chat-message,
    .chat-toggle,
    .voice-error {
        animation: none;
        transition: none;
    }

    .chat-typing span {
        animation: none;
    }

    .chat-voice.listening {
        animation: none;
    }

    .chat-avatar,
    .chat-avatar::before,
    .chat-avatar::after {
        animation: none !important;
    }

    .chat-avatar.thinking {
        animation: none !important;
    }
}

/* ========== High Contrast Mode ========== */
@media (prefers-contrast: high) {
    .chat-sidebar {
        border-left-width: 2px;
    }

    .chat-message {
        border-width: 2px;
    }

    .chat-input-area textarea {
        border-width: 2px;
    }

    .chat-close {
        border-width: 3px;
    }
}
