/* 
 * Mobile-First Optimized CSS for TrilingoApp
 * Targets 100% mobile friendliness with proper touch targets, font sizes, and responsive design
 */

:root {
    /* Enhanced mobile-friendly CSS variables */
    --mobile-min-touch-target: 44px;
    --mobile-min-font-size: 16px;
    --mobile-padding: 1rem;
    --mobile-safe-area-top: env(safe-area-inset-top);
    --mobile-safe-area-bottom: env(safe-area-inset-bottom);
    --mobile-safe-area-left: env(safe-area-inset-left);
    --mobile-safe-area-right: env(safe-area-inset-right);
}

/* Advanced Chat Features - Mobile Optimized */

/* Emoji suggestions - positioned under input field */
.emoji-suggestions {
    position: fixed;
    bottom: 90px; /* Just above input field */
    left: 20px;
    right: 20px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 16px 16px 8px 8px;
    padding: 8px 12px;
    display: flex;
    gap: 8px;
    box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.15);
    opacity: 0;
    pointer-events: none;
    transition: all 0.3s ease;
    z-index: 1001;
    flex-wrap: wrap;
    justify-content: center;
    transform: translateY(10px);
}

.emoji-suggestions.show {
    opacity: 1;
    pointer-events: all;
    transform: translateY(0);
}

.emoji-btn {
    background: none;
    border: none;
    font-size: 20px;
    padding: 4px;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    min-width: 32px;
    min-height: 32px;
}

.emoji-btn:hover {
    background: var(--bg-hover);
    transform: scale(1.1);
}

/* Input tooltip - mobile positioned */
.input-tooltip {
    position: fixed;
    bottom: 220px;
    left: 20px;
    right: 20px;
    background: var(--bg-secondary);
    color: var(--text-color);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 8px 12px;
    font-size: 13px;
    opacity: 0;
    pointer-events: none;
    transition: all 0.3s ease;
    z-index: 1002;
    text-align: center;
}

.input-tooltip.show {
    opacity: 1;
    transform: translateY(-5px);
}

/* Right Sidebar - Desktop Only */
.right-sidebar {
    position: fixed !important;
    top: 80px !important;
    right: 20px !important;
    width: 200px !important;
    background: #ffffff !important;
    border: 2px solid #3498db !important;
    border-radius: 12px !important;
    padding: 16px !important;
    z-index: 9999 !important;
    opacity: 1 !important;
    pointer-events: all !important;
    transition: all 0.4s ease !important;
    box-shadow: 0 8px 24px rgba(52, 152, 219, 0.3) !important;
    display: block !important;
}

.sidebar-section {
    margin-bottom: 20px;
}

.sidebar-title {
    font-size: 14px !important;
    font-weight: 600 !important;
    color: #333333 !important;
    margin-bottom: 12px !important;
    padding-bottom: 8px !important;
    border-bottom: 1px solid #e5e7eb !important;
}

/* Ambient sound controls - now inside sidebar */
.ambient-controls {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
    width: 100%;
}

/* Hide sidebar on mobile completely */
@media (max-width: 768px) {
    .right-sidebar {
        display: none !important;
    }
}

.ambient-controls.show {
    opacity: 1;
    pointer-events: all;
}

.ambient-toggle {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    border: 1px solid var(--border-color, #e5e7eb);
    background: var(--bg-primary, #ffffff);
    color: var(--text-muted, #6b7280);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 14px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.ambient-toggle:hover,
.ambient-toggle.active {
    background: var(--primary-color);
    color: white;
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(52, 152, 219, 0.3);
}

/* User message action icons - Fixed positioning */
.user-actions {
    justify-content: flex-end !important;
    margin-top: 8px;
    display: flex !important;
    gap: 6px !important;
    position: relative !important;
    z-index: 10 !important;
}

.user-actions .action-icon-btn {
    background: rgba(255, 255, 255, 0.9) !important;
    border: 1px solid rgba(0, 0, 0, 0.2) !important;
    color: #333 !important;
    transition: all 0.3s ease;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1) !important;
    min-width: 32px !important;
    min-height: 32px !important;
    border-radius: 50% !important;
    padding: 6px !important;
    cursor: pointer !important;
    flex-shrink: 0 !important;
}

/* Ensure icons don't get cut off for long messages */
.message.user {
    overflow: visible !important;
}

.message.user .message-content {
    overflow: visible !important;
    word-wrap: break-word !important;
    max-width: 100% !important;
}

/* User action dropdown positioning */
.user-action-container {
    position: relative !important;
}

/* User dropdown menu styling - Dark mode compatible */
.user-dropdown-menu {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
    z-index: 99999;
    min-width: 220px;
    max-width: 300px;
    padding: 8px 0;
    font-family: inherit;
}

.user-dropdown-menu .dropdown-item {
    padding: 14px 18px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 12px;
    color: var(--text-color);
    font-size: 15px;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
    transition: all 0.2s ease;
    min-height: 44px; /* Mobile touch target */
}

.user-dropdown-menu .dropdown-item:hover {
    background: var(--bg-hover);
    color: var(--primary-color);
    transform: translateX(2px);
}

.user-dropdown-menu .dropdown-item i {
    width: 18px;
    color: inherit;
    font-size: 14px;
}

/* Dark mode specific overrides */
[data-theme="dark"] .user-dropdown-menu {
    background: #2d2d2d;
    border-color: rgba(255, 255, 255, 0.2);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
}

[data-theme="dark"] .user-dropdown-menu .dropdown-item {
    color: #e0e0e0;
}

[data-theme="dark"] .user-dropdown-menu .dropdown-item:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #3498db;
}

/* Mobile responsiveness for dropdown */
/* Better mobile layout for long text */
@media (max-width: 768px) {
    .user-dropdown-menu {
        min-width: 240px;
        max-width: calc(100vw - 20px);
        font-size: 16px; /* Prevent zoom on iOS */
    }
    
    .user-dropdown-menu .dropdown-item {
        padding: 16px 20px;
        min-height: 48px;
        font-size: 16px;
    }
    
    .user-dropdown-menu .dropdown-item i {
        width: 20px;
        font-size: 16px;
    }
    
    /* Prevent icon cropping on mobile */
    .message.user {
        padding-bottom: 50px !important; /* Extra space for icons */
    }
    
    .user-actions {
        position: absolute !important;
        bottom: 10px !important;
        right: 10px !important;
        background: rgba(255, 255, 255, 0.95) !important;
        padding: 4px 8px !important;
        border-radius: 20px !important;
        box-shadow: 0 2px 8px rgba(0,0,0,0.1) !important;
    }
    
    [data-theme="dark"] .user-actions {
        background: rgba(45, 45, 45, 0.95) !important;
    }
}

.user-action-container .action-dropdown-item {
    padding: 10px 16px !important;
    cursor: pointer !important;
    display: flex !important;
    align-items: center !important;
    gap: 8px !important;
    color: #333 !important;
    text-decoration: none !important;
    border: none !important;
    background: none !important;
    width: 100% !important;
    text-align: left !important;
    font-size: 14px !important;
}

.user-action-container .action-dropdown-item:hover {
    background: #f8f9fa !important;
    color: #007bff !important;
}

.user-action-container .action-dropdown-item i {
    width: 16px !important;
    color: inherit !important;
}

.user-actions .action-icon-btn:hover {
    background: var(--primary-color, #3498db);
    color: white;
    transform: translateY(-1px) scale(1.05);
    box-shadow: 0 2px 8px rgba(52, 152, 219, 0.3);
}

.user-message-editor {
    width: 100% !important;
    min-height: 60px !important;
    padding: 10px !important;
    border: 2px solid var(--primary-color, #3498db) !important;
    border-radius: 8px !important;
    font-family: inherit !important;
    font-size: inherit !important;
    background: var(--bg-primary, #ffffff) !important;
    color: var(--text-color, #333) !important;
    resize: vertical !important;
}

/* Mobile-first base styles */
body {
    /* Ensure proper text scaling on mobile */
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    
    /* Improve touch scrolling on iOS */
    -webkit-overflow-scrolling: touch;
    
    /* Safe area support for modern phones */
    padding-top: var(--mobile-safe-area-top);
    padding-bottom: var(--mobile-safe-area-bottom);
    padding-left: var(--mobile-safe-area-left);
    padding-right: var(--mobile-safe-area-right);
    
    /* Minimum font size for readability */
    font-size: max(var(--mobile-min-font-size), 1rem);
    line-height: 1.5; /* Better readability */
}

/* Touch-friendly interactive elements */
button,
.btn,
.menu-btn,
.action-btn,
.show-more-btn,
.file-menu-btn,
input[type="submit"],
input[type="button"],
.clickable {
    min-height: var(--mobile-min-touch-target);
    min-width: var(--mobile-min-touch-target);
    padding: 0.75rem 1rem; /* Ensure adequate padding */
    
    /* Touch feedback */
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
    touch-action: manipulation; /* Prevent double-tap zoom */
    
    /* Improve touch interaction */
    cursor: pointer;
    user-select: none;
    
    /* Ensure text is readable */
    font-size: max(var(--mobile-min-font-size), 0.9rem);
}

/* Enhanced file menu button for mobile - the three dots (...) menu */
.file-menu-btn {
    /* Override base styles for better mobile visibility */
    min-height: 48px !important;
    min-width: 48px !important;
    padding: 12px !important;
    
    /* Make the dots more visible */
    font-size: 20px !important;
    font-weight: bold !important;
    line-height: 1 !important;
    
    /* Better visual feedback */
    background: rgba(0, 0, 0, 0.05) !important;
    border: 1px solid rgba(0, 0, 0, 0.1) !important;
    border-radius: 8px !important;
    color: #333 !important;
    
    /* Enhanced touch target */
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    
    /* Smooth transitions */
    transition: all 0.2s ease;
}

.file-menu-btn:hover,
.file-menu-btn:focus,
.file-menu-btn:active {
    background: rgba(0, 0, 0, 0.1) !important;
    border-color: rgba(0, 0, 0, 0.2) !important;
    color: #000 !important;
    transform: scale(1.05);
}

/* Dark theme support for file menu button */
[data-theme="dark"] .file-menu-btn {
    background: rgba(255, 255, 255, 0.1) !important;
    border-color: rgba(255, 255, 255, 0.2) !important;
    color: #e0e0e0 !important;
}

[data-theme="dark"] .file-menu-btn:hover,
[data-theme="dark"] .file-menu-btn:focus,
[data-theme="dark"] .file-menu-btn:active {
    background: rgba(255, 255, 255, 0.2) !important;
    border-color: rgba(255, 255, 255, 0.3) !important;
    color: #fff !important;
}

/* Input fields optimization */
input,
textarea,
select {
    min-height: var(--mobile-min-touch-target);
    font-size: max(var(--mobile-min-font-size), 1rem); /* Prevents zoom on iOS */
    padding: 0.75rem;
    
    /* Better mobile keyboard support */
    -webkit-appearance: none;
    border-radius: 8px;
    
    /* Touch improvements */
    touch-action: manipulation;
}

/* Textarea specific optimizations */
textarea {
    resize: vertical; /* Prevent horizontal resize on mobile */
    min-height: 120px; /* Adequate space for typing */
}

/* Chat input area mobile optimization */
.chat-input-container {
    padding: var(--mobile-padding);
    background: var(--surface);
    
    /* Safe area support */
    padding-bottom: max(var(--mobile-padding), var(--mobile-safe-area-bottom));
    
    /* Sticky positioning for better UX */
    position: sticky;
    bottom: 0;
    z-index: 100;
}

.chat-input {
    min-height: var(--mobile-min-touch-target);
    font-size: var(--mobile-min-font-size);
    border-radius: 24px; /* More mobile-friendly rounded corners */
    padding: 1rem 1.5rem;
    
    /* Auto-resize with content */
    resize: none;
    overflow-y: auto;
    max-height: 120px;
}

/* Message bubbles mobile optimization */
.message {
    margin-bottom: 1rem;
    max-width: 90%; /* Better mobile width */
    word-wrap: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
}

.message-content {
    font-size: var(--mobile-min-font-size);
    line-height: 1.5;
    padding: 1rem;
    border-radius: 16px;
    
    /* Ensure readability on all devices */
    min-height: 2.5rem;
}

/* Action buttons mobile optimization */
.action-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 1rem;
    justify-content: flex-start;
}

.action-buttons .btn {
    min-height: var(--mobile-min-touch-target);
    min-width: var(--mobile-min-touch-target);
    padding: 0.75rem 1rem;
    font-size: 0.9rem;
    
    /* Better touch targets */
    flex: 0 0 auto;
    white-space: nowrap;
}

/* Header mobile optimization */
.header {
    min-height: var(--mobile-min-touch-target);
    padding: 0.75rem var(--mobile-padding);
    
    /* Safe area support */
    padding-top: max(0.75rem, var(--mobile-safe-area-top));
}

.menu-btn,
.theme-toggle {
    min-height: var(--mobile-min-touch-target);
    min-width: var(--mobile-min-touch-target);
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Sidebar mobile optimization */
.sidebar {
    /* Better mobile sidebar behavior */
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    width: 280px;
    z-index: 1000;
    
    /* Safe area support */
    padding-top: var(--mobile-safe-area-top);
}

.sidebar.open {
    transform: translateX(0);
}

.sidebar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 999;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

.sidebar-overlay.active {
    opacity: 1;
    visibility: visible;
}

/* File attachment mobile optimization */
.file-attachment {
    padding: 1rem;
    border-radius: 12px;
    
    /* Better mobile interaction */
    min-height: calc(var(--mobile-min-touch-target) * 1.5);
}

.file-icon {
    min-width: 48px;
    min-height: 48px;
    font-size: 1.25rem;
}

/* Enhanced file dropdown positioning and visibility */
.file-actions {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

.file-menu {
    /* Better mobile dropdown positioning */
    position: absolute;
    top: 100%;
    right: 0;
    z-index: 1000;
    min-width: 180px;
    max-width: calc(100vw - 2rem);
    
    /* Enhanced mobile visibility */
    background: white;
    border: 1px solid rgba(0, 0, 0, 0.15);
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
    padding: 8px 0;
    margin-top: 4px;
    
    /* Ensure it's above other elements */
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.file-menu.show {
    display: block;
    animation: fadeInDown 0.2s ease;
}

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

.file-menu-item {
    /* Enhanced mobile menu items */
    display: block;
    width: 100%;
    padding: 12px 16px;
    border: none;
    background: none;
    text-align: left;
    color: #333;
    font-size: 16px;
    line-height: 1.4;
    
    /* Touch-friendly */
    min-height: 44px;
    cursor: pointer;
    transition: background-color 0.2s ease;
}

.file-menu-item:hover,
.file-menu-item:focus {
    background: rgba(0, 0, 0, 0.05);
    color: #000;
}

/* Dark theme support for file menu */
[data-theme="dark"] .file-menu {
    background: #2d2d2d;
    border-color: rgba(255, 255, 255, 0.2);
}

[data-theme="dark"] .file-menu-item {
    color: #e0e0e0;
}

[data-theme="dark"] .file-menu-item:hover,
[data-theme="dark"] .file-menu-item:focus {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
}

/* Dropdown menu mobile optimization */
.dropdown-menu,
.file-menu,
.action-dropdown {
    /* Better mobile dropdown behavior */
    min-width: 200px;
    max-width: calc(100vw - 2rem);
    border-radius: 12px;
    
    /* Better touch interaction */
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
}

.dropdown-item,
.file-menu-item,
.action-item {
    min-height: var(--mobile-min-touch-target);
    padding: 1rem;
    font-size: var(--mobile-min-font-size);
    
    /* Touch feedback */
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
}

/* Form elements mobile optimization */
select {
    /* Better mobile select styling */
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
    background-position: right 0.5rem center;
    background-repeat: no-repeat;
    background-size: 1.5em 1.5em;
    padding-right: 2.5rem;
}

/* Loading states mobile optimization */
.loading,
.typing-indicator {
    /* Better mobile loading indicators */
    min-height: 3rem;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--mobile-min-font-size);
}

/* Error states mobile optimization */
.error-message,
.alert {
    padding: 1rem;
    border-radius: 8px;
    font-size: var(--mobile-min-font-size);
    margin: 1rem 0;
    
    /* Better mobile error visibility */
    min-height: 3rem;
    display: flex;
    align-items: center;
}

/* Performance optimizations */
* {
    /* Optimize repaints and reflows */
    will-change: auto;
    
    /* Better box-sizing for mobile */
    box-sizing: border-box;
}

/* Mobile keyboard optimization */
body.keyboard-open {
    height: 100vh;
    overflow: visible; /* Allow dropdowns to show */
}

/* Dynamic viewport height support */
.full-height {
    height: 100vh;
    height: calc(var(--vh, 1vh) * 100);
}

/* Mobile device optimization */
body.mobile-device {
    /* Disable text selection where inappropriate */
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

body.mobile-device input,
body.mobile-device textarea {
    /* Re-enable text selection for inputs */
    -webkit-user-select: text;
    -moz-user-select: text;
    -ms-user-select: text;
    user-select: text;
}

.animated {
    /* Only animate when needed */
    will-change: transform, opacity;
}

/* Smooth scrolling optimization */
.chat-messages,
.scrollable {
    /* Better mobile scrolling */
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
    overscroll-behavior: contain;
}

/* Mobile-specific rules for advanced features */
@media (max-width: 768px) {
    /* Hide Replit domain branding on mobile */
    body::after {
        content: '';
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 40px;
        background: var(--bg-primary);
        z-index: 10000;
        pointer-events: none;
    }
    
    /* Adjust chat container to account for hidden bar */
    .chat-container {
        padding-bottom: 150px !important;
    }
    
    /* Hide ambient controls completely on mobile */
    .ambient-controls {
        display: none !important;
    }
    
    /* Emoji suggestions - mobile optimized under input */
    .emoji-suggestions {
        bottom: 85px; /* Closer to input on mobile */
        left: 15px;
        right: 15px;
        padding: 6px 10px;
        gap: 6px;
        border-radius: 12px 12px 6px 6px;
    }
    
    .emoji-btn {
        font-size: 18px;
        min-width: 32px;
        min-height: 32px;
        padding: 4px;
    }
    
    /* Input tooltip - more compact */
    .input-tooltip {
        bottom: 200px;
        left: 15px;
        right: 15px;
        padding: 6px 10px;
        font-size: 12px;
    }
}

/* Hide features when keyboard is likely open */
@media (max-height: 600px) {
    .emoji-suggestions,
    .ambient-controls,
    .input-tooltip {
        display: none !important;
    }
}

/* Media queries for progressive enhancement */
@media (max-width: 480px) {
    /* Extra small devices */
    :root {
        --mobile-padding: 0.75rem;
    }
    
    .message {
        max-width: 95%;
    }
    
    .action-buttons {
        justify-content: center;
    }
    
    .action-buttons .btn {
        flex: 1 0 45%; /* Two buttons per row */
        text-align: center;
    }
    
    /* Smaller typography for very small screens */
    .logo {
        font-size: 1rem;
    }
    
    .header {
        padding: 0.5rem;
    }
    
    /* Keep ambient controls hidden on very small screens */
    .ambient-controls {
        display: none !important;
    }
    
    .emoji-suggestions {
        bottom: 80px;
        left: 10px;
        right: 10px;
        padding: 4px 8px;
        gap: 4px;
    }
    
    .emoji-btn {
        font-size: 16px;
        min-width: 28px;
        min-height: 28px;
        padding: 2px;
    }
    
    /* Improve input field spacing */
    .chat-input-container {
        padding: 12px 16px !important;
        margin-bottom: 0px !important;
    }
    
    #chatInput {
        padding-left: 84px !important; /* Adjusted for small screens */
        padding-right: 50px !important;
        font-size: 16px !important; /* Prevent zoom on iOS */
    }
}

@media (max-width: 320px) {
    /* Very small devices */
    :root {
        --mobile-padding: 0.5rem;
    }
    
    .action-buttons .btn {
        flex: 1 0 100%; /* One button per row */
        margin-bottom: 0.5rem;
    }
    
    .message-content {
        padding: 0.75rem;
    }
}

/* Landscape mobile optimization */
@media (orientation: landscape) and (max-height: 500px) {
    .header {
        padding: 0.5rem var(--mobile-padding);
    }
    
    .chat-input-container {
        padding: 0.5rem var(--mobile-padding);
    }
    
    .sidebar {
        width: 240px; /* Narrower sidebar in landscape */
    }
}

/* High DPI display optimization */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    /* Optimize for high DPI displays */
    .file-icon,
    .logo i {
        image-rendering: -webkit-optimize-contrast;
        image-rendering: crisp-edges;
    }
}

/* Dark mode mobile enhancements */
@media (prefers-color-scheme: dark) {
    /* Better dark mode defaults for mobile */
    body {
        background-color: #000000; /* True black for OLED power saving */
    }
    
    .header,
    .chat-input-container {
        background-color: #1a1a1a;
    }
}

/* Reduced motion preference */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Print optimization */
@media print {
    .header,
    .sidebar,
    .chat-input-container,
    .action-buttons {
        display: none !important;
    }
    
    .message {
        max-width: 100% !important;
        page-break-inside: avoid;
    }
}