/* ==========================================================================
   NestroHub AI - Enterprise Chat UI Stylesheet (Mobile Optimized)
   ========================================================================== */

/* --- 1. Variables & Theming --- */
:root {
    /* Brand Colors */
    --chat-primary: #0f62fe;
    --chat-primary-hover: #0353e9;
    --chat-success: #198038;
    --chat-danger: #da1e28;
    --chat-warning: #f1c21b;
    --chat-info: #0043ce;

    /* Light Theme (Default) */
    --chat-bg: #ffffff;
    --chat-surface: #f4f4f4;
    --chat-border: #e0e0e0;
    --chat-text-main: #161616;
    --chat-text-muted: #525252;
    --chat-bubble-ai: #f4f4f4;
    --chat-bubble-user: #0f62fe;
    --chat-text-user: #ffffff;
    --chat-shadow: 0 12px 24px rgba(0, 0, 0, 0.1);

    /* Dimensions & Radius */
    --chat-radius-lg: 16px;
    --chat-radius-md: 12px;
    --chat-radius-sm: 8px;
    --chat-spacing: 16px;
    
    /* Typography */
    --chat-font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    
    /* Mobile Safe Areas */
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* Dark Theme Overrides */
.dark {
    --chat-bg: #161616;
    --chat-surface: #262626;
    --chat-border: #393939;
    --chat-text-main: #f4f4f4;
    --chat-text-muted: #a8a8a8;
    --chat-bubble-ai: #262626;
    --chat-bubble-user: #0f62fe;
    --chat-shadow: 0 12px 24px rgba(0, 0, 0, 0.4);
}

/* --- 2. Reset & Wrapper --- */
#ai-chat-wrapper {
    position: fixed;
    bottom: max(24px, var(--safe-bottom));
    right: 24px;
    z-index: 9999;
    font-family: var(--chat-font);
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    color: var(--chat-text-main);
}

#ai-chat-wrapper * {
    box-sizing: border-box;
}

/* --- 3. Floating Action Button (FAB) & Notifications --- */
.chat-fab {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: var(--chat-primary);
    color: white;
    border: none;
    font-size: 28px;
    cursor: pointer;
    box-shadow: var(--chat-shadow);
    transition: transform 0.2s ease, background 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    /* Touch target padding for mobile */
    touch-action: manipulation;
}

.chat-fab:hover {
    transform: scale(1.05);
    background: var(--chat-primary-hover);
}

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

.chat-fab .badge {
    position: absolute;
    top: -2px;
    right: -2px;
    background: var(--chat-danger);
    color: white;
    font-size: 12px;
    font-weight: bold;
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 6px;
    border: 2px solid var(--chat-bg);
    animation: pulse 2s infinite;
}

/* --- 4. Chat Window & Layout --- */
#ai-chat-window {
    width: 400px;
    height: 650px;
    max-height: calc(100vh - max(120px, var(--safe-bottom) + 90px));
    background: var(--chat-bg);
    border-radius: var(--chat-radius-lg);
    box-shadow: var(--chat-shadow);
    border: 1px solid var(--chat-border);
    display: flex;
    overflow: hidden;
    margin-bottom: 20px;
    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    transform-origin: bottom right;
    opacity: 1;
    transform: scale(1) translateY(0);
}

#ai-chat-window.minimized {
    opacity: 0;
    transform: scale(0.9) translateY(20px);
    pointer-events: none;
}

#ai-chat-window.maximized {
    width: 90vw;
    height: 90vh;
    max-height: 90vh;
    right: 5vw;
    bottom: 5vh;
}

#ai-main-chat {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    position: relative;
}

/* --- 5. Sidebar (History) --- */
#ai-sidebar {
    width: 250px;
    background: var(--chat-surface);
    border-right: 1px solid var(--chat-border);
    display: flex;
    flex-direction: column;
    transition: width 0.3s ease, transform 0.3s ease;
    z-index: 100;
}

#ai-sidebar.hidden {
    width: 0;
    border: none;
    overflow: hidden;
}

.sidebar-header {
    padding: 16px;
    border-bottom: 1px solid var(--chat-border);
}

#ai-btn-new-chat {
    width: 100%;
    padding: 12px;
    background: var(--chat-primary);
    color: white;
    border: none;
    border-radius: var(--chat-radius-sm);
    cursor: pointer;
    font-weight: bold;
}

/* --- 6. Header & Status --- */
.chat-header {
    padding: 16px;
    background: var(--chat-surface);
    border-bottom: 1px solid var(--chat-border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    min-height: 70px;
}

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

.ai-avatar {
    font-size: 24px;
}

.ai-title-group {
    display: flex;
    flex-direction: column;
}

#ai-assistant-selector {
    background: transparent;
    border: none;
    color: var(--chat-text-main);
    font-weight: 600;
    font-size: 15px;
    cursor: pointer;
    outline: none;
    padding: 0;
}

.ai-status {
    font-size: 12px;
    color: var(--chat-text-muted);
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 2px;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--chat-success);
    display: inline-block;
}

.chat-controls {
    display: flex; 
    align-items: center;
}

.chat-controls button {
    background: transparent;
    border: none;
    color: var(--chat-text-muted);
    font-size: 16px;
    cursor: pointer;
    padding: 8px;
    border-radius: var(--chat-radius-sm);
    min-width: 40px; /* Touch target minimum */
    min-height: 40px; /* Touch target minimum */
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, color 0.2s;
}

.chat-controls button:hover {
    background: var(--chat-border);
    color: var(--chat-text-main);
}

/* --- 7. Messages Area --- */
#ai-chat-messages {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 20px;
    scroll-behavior: smooth;
}

.message-wrapper {
    display: flex;
    gap: 12px;
    max-width: 90%;
}

.message-wrapper.user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--chat-surface);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
}

.message-content-area {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.message-wrapper.user .message-content-area {
    align-items: flex-end;
}

.message-meta {
    font-size: 12px;
    color: var(--chat-text-muted);
    display: flex;
    gap: 8px;
}

.message-bubble {
    padding: 12px 16px;
    border-radius: var(--chat-radius-md);
    line-height: 1.5;
    font-size: 15px; /* Better mobile readability */
    word-break: break-word;
}

.ai .message-bubble {
    background: var(--chat-bubble-ai);
    border-top-left-radius: 2px;
}

.user .message-bubble {
    background: var(--chat-bubble-user);
    color: var(--chat-text-user);
    border-top-right-radius: 2px;
}

/* --- 8. Markdown & Rich Content --- */
.message-bubble pre {
    background: #282c34;
    color: #abb2bf;
    padding: 12px;
    border-radius: var(--chat-radius-sm);
    overflow-x: auto;
    margin: 8px 0;
}

.message-bubble code {
    font-family: monospace;
    background: rgba(127, 127, 127, 0.2);
    padding: 2px 4px;
    border-radius: 4px;
}

.message-bubble table {
    width: 100%;
    border-collapse: collapse;
    margin: 8px 0;
}

.message-bubble th, .message-bubble td {
    border: 1px solid var(--chat-border);
    padding: 8px;
    text-align: left;
}

.message-bubble img {
    max-width: 100%;
    border-radius: var(--chat-radius-sm);
    margin: 8px 0;
}

/* --- 9. Message Actions --- */
.message-actions {
    display: flex;
    gap: 8px;
    opacity: 0;
    transition: opacity 0.2s ease;
    margin-top: 4px;
}

@media (hover: none) {
    /* Always show actions on mobile since there is no hover */
    .message-actions { opacity: 1; }
}

.message-wrapper:hover .message-actions {
    opacity: 1;
}

.message-actions button {
    background: transparent;
    border: none;
    color: var(--chat-text-muted);
    cursor: pointer;
    font-size: 14px;
    border-radius: 4px;
    padding: 4px 8px;
}

.message-actions button:hover {
    background: var(--chat-border);
    color: var(--chat-text-main);
}

/* --- 10. System, Errors & Suggestions --- */
.message-wrapper.system {
    align-self: center;
    max-width: 100%;
}

.error-card {
    background: rgba(218, 30, 40, 0.1);
    border-left: 4px solid var(--chat-danger);
    padding: 12px 16px;
    border-radius: 0 var(--chat-radius-sm) var(--chat-radius-sm) 0;
}

.error-card strong { color: var(--chat-danger); }

.suggestions-container {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 10px;
}

.suggestion-chip {
    background: var(--chat-surface);
    border: 1px solid var(--chat-border);
    color: var(--chat-text-main);
    padding: 8px 12px;
    border-radius: 20px;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.2s;
}

.suggestion-chip:hover {
    background: var(--chat-primary);
    color: white;
    border-color: var(--chat-primary);
}

/* --- 11. Typing Indicator --- */
#ai-typing-indicator {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 10px;
}

.typing-bubble {
    background: var(--chat-bubble-ai);
    padding: 12px 16px;
    border-radius: var(--chat-radius-md);
    border-top-left-radius: 2px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.typing-text { font-size: 13px; color: var(--chat-text-muted); }

.dots span {
    display: inline-block;
    width: 6px; height: 6px;
    background: var(--chat-text-muted);
    border-radius: 50%;
    animation: typingBounce 1.4s infinite ease-in-out both;
}
.dots span:nth-child(1) { animation-delay: -0.32s; }
.dots span:nth-child(2) { animation-delay: -0.16s; }

/* --- 12. Input Area & Media --- */
.chat-input-wrapper {
    border-top: 1px solid var(--chat-border);
    background: var(--chat-bg);
    padding: 12px 16px;
    /* Respect iPhone bottom bar */
    padding-bottom: max(12px, var(--safe-bottom)); 
}

.chat-input-area {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--chat-surface);
    padding: 8px;
    border-radius: var(--chat-radius-lg);
    border: 1px solid var(--chat-border);
}

.chat-input-area:focus-within {
    border-color: var(--chat-primary);
}

#ai-chat-input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--chat-text-main);
    resize: none;
    max-height: 96px;
    padding: 8px 4px;
    outline: none;
    font-family: inherit;
    font-size: 16px; /* 16px is mandatory to prevent iOS Safari auto-zoom */
}

.chat-input-area button {
    background: transparent;
    border: none;
    color: var(--chat-text-muted);
    font-size: 20px;
    cursor: pointer;
    padding: 10px; /* Touch friendly size */
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: 0.2s;
}

.chat-input-area button:hover {
    background: var(--chat-border);
    color: var(--chat-text-main);
}

#ai-btn-send {
    color: var(--chat-primary);
}

#ai-btn-send:disabled {
    color: var(--chat-text-muted);
    opacity: 0.5;
    cursor: not-allowed;
}

#ai-btn-voice.recording {
    color: var(--chat-danger);
    animation: pulse 1.5s infinite;
}

#ai-upload-preview {
    padding-bottom: 12px;
    display: flex;
    gap: 8px;
}

.preview-item {
    background: var(--chat-surface);
    padding: 4px 8px;
    border-radius: var(--chat-radius-sm);
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
    border: 1px solid var(--chat-border);
}

/* --- 13. Scrollbar & Animations --- */
#ai-chat-messages::-webkit-scrollbar,
#ai-sidebar::-webkit-scrollbar {
    width: 6px;
}
#ai-chat-messages::-webkit-scrollbar-track,
#ai-sidebar::-webkit-scrollbar-track {
    background: transparent;
}
#ai-chat-messages::-webkit-scrollbar-thumb,
#ai-sidebar::-webkit-scrollbar-thumb {
    background: var(--chat-border);
    border-radius: 10px;
}

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(218, 30, 40, 0.4); }
    70% { box-shadow: 0 0 0 10px rgba(218, 30, 40, 0); }
    100% { box-shadow: 0 0 0 0 rgba(218, 30, 40, 0); }
}
@keyframes typingBounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}

/* --- 14. Responsive Layouts (Mobile Optimized) --- */
@media (max-width: 768px) {
    #ai-chat-wrapper {
        bottom: 0;
        right: 0;
        width: 100%;
        /* Ensure the wrapper doesn't block clicks when closed on mobile */
        pointer-events: none;
    }
    
    #ai-chat-wrapper > * { 
        pointer-events: auto; 
    }
    
    #ai-chat-window, #ai-chat-window.maximized {
        width: 100%;
        /* Use dynamic viewport height to prevent hiding under mobile URL bars */
        height: 100dvh; 
        max-height: 100dvh;
        border-radius: 0;
        margin-bottom: 0;
        border: none;
    }
    
    #ai-sidebar {
        position: absolute;
        height: 100%;
        z-index: 100;
        transform: translateX(0);
        width: 280px; 
        max-width: 85vw;
        box-shadow: 4px 0 24px rgba(0,0,0,0.1);
    }
    
    #ai-sidebar.hidden {
        transform: translateX(-100%);
        width: 280px;
        box-shadow: none;
    }
    
    .chat-fab {
        margin: 20px max(20px, var(--safe-bottom));
    }
    
    .chat-header {
        min-height: 60px; 
        padding: 12px;
    }
    
    .chat-input-wrapper {
        padding: 10px; 
        padding-bottom: max(10px, var(--safe-bottom)); 
    }
}

/* --- 15. Accessibility --- */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

button:focus-visible, 
#ai-chat-input:focus-visible,
select:focus-visible {
    outline: 2px solid var(--chat-primary);
    outline-offset: 2px;
}
