/* ==========================================
   KUSOYA AI - RESPONSIVE STYLES
   All Media Queries for All Breakpoints
   ========================================== */

/* ==========================================
   DESKTOP (1024px+)
   ========================================== */
@media (min-width: 1024px) {
    .login-form-wrapper {
        max-width: 480px;
    }

    .login-card {
        padding: 3rem 2.5rem;
    }
}

/* ==========================================
   TABLET (768px - 1024px)
   ========================================== */
@media (min-width: 768px) and (max-width: 1024px) {
    .login-page-split {
        padding: 3rem 2rem;
    }

    .login-card {
        padding: 3rem 2.5rem;
    }

    .login-form-wrapper {
        max-width: 460px;
    }
    
    .example-prompts-grid {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .message-content.user-content,
    .message-content.ai-content {
        max-width: 80%;
    }
    
    .mode-toggle-btn {
        font-size: 12px;
        padding: 5px 14px;
    }
    
    .mode-hint {
        font-size: 11px;
    }
}

/* ==========================================
   SIDEBAR RESPONSIVE (Desktop)
   ========================================== */
@media (min-width: 769px) {
    .ai-sidebar, #chatSidebar, .sidebar-chat {
        transform: translateX(0);
    }
    
    .ai-sidebar:not(.open):not(.sidebar-open),
    #chatSidebar:not(.open):not(.sidebar-open),
    .sidebar-chat:not(.open):not(.sidebar-open) {
        transform: translateX(-260px);
    }
    
    .ai-sidebar.open, .ai-sidebar.sidebar-open,
    #chatSidebar.open, #chatSidebar.sidebar-open,
    .sidebar-chat.open, .sidebar-chat.sidebar-open {
        transform: translateX(0);
    }
    
    .ai-sidebar:not(.open):not(.sidebar-open) ~ .ai-main-content,
    #chatSidebar:not(.open):not(.sidebar-open) ~ .main-chat-area {
        margin-left: 0;
    }
}

/* ==========================================
   MOBILE (max 768px) - CRITICAL FIXES
   ========================================== */
@media (max-width: 768px) {
    /* CRITICAL: Force proper viewport height management */
    html {
        height: 100%;
        overflow: hidden;
    }
    
    body {
        height: 100%;
        overflow: hidden;
        position: fixed;
        width: 100%;
    }
    
    /* CRITICAL: Main wrapper must be 100vh */
    .ai-chat-wrapper {
        height: 100vh;
        height: 100dvh; /* Dynamic viewport height for mobile */
        display: flex;
        flex-direction: row;
        overflow: hidden;
        position: relative;
    }
    
    /* Sidebar */
    .ai-sidebar, #chatSidebar, .sidebar-chat {
        transform: translateX(-100%);
        z-index: 2000;
        position: fixed;
        height: 100vh;
        left: 0;
        top: 0;
    }
    
    .ai-sidebar.open, .ai-sidebar.sidebar-open,
    #chatSidebar.open, #chatSidebar.sidebar-open,
    .sidebar-chat.open, .sidebar-chat.sidebar-open {
        transform: translateX(0);
        box-shadow: 4px 0 20px rgba(0, 0, 0, 0.3);
    }
    
    /* CRITICAL: Main content area layout */
    .ai-main-content, .main-chat-area {
        margin-left: 0;
        width: 100%;
        height: 100vh;
        height: 100dvh;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        position: relative;
    }
    
    /* Topbar - Fixed height */
    .sidebar-toggle, #sidebarToggle {
        display: flex !important;
        visibility: visible !important;
        opacity: 1 !important;
        z-index: 2002 !important;
    }
    
    .ai-topbar, .navbar {
        position: relative;
        flex-shrink: 0;
        height: 60px;
        z-index: 10;
        padding: 0 16px;
    }
    
    /* CRITICAL FIX: Messages container - Add bottom padding to prevent hidden messages */
    .messages-container, .chat-messages-area {
        flex: 1 1 auto;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        padding: 16px 16px 100px; /* ADDED bottom padding to prevent hidden messages */
        min-height: 0;
        height: auto;
    }
    
    /* CRITICAL: Welcome screen - Must not force height */
    .welcome-screen {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: flex-start;
        padding: 20px 16px 100px; /* ADDED bottom padding */
        text-align: center;
        min-height: auto;
        height: auto;
    }
    
    .ai-avatar-large {
        width: 60px;
        height: 60px;
        font-size: 28px;
        margin-bottom: 16px;
    }
    
    .welcome-title {
        font-size: 22px;
        margin-bottom: 8px;
    }
    
    .welcome-subtitle {
        font-size: 15px;
        margin-bottom: 20px;
    }
    
    /* Example prompts - Compact on mobile */
    .example-prompts-grid {
        grid-template-columns: 1fr;
        gap: 10px;
        margin-top: 16px;
        width: 100%;
    }
    
    .prompt-card {
        padding: 14px;
        font-size: 14px;
    }
    
    .prompt-card i {
        font-size: 18px;
    }
    
    /* Chat messages */
    .message-content.user-content,
    .message-content.ai-content {
        max-width: 85%;
        font-size: 14px;
    }
    
    /* CRITICAL: Input Area - Fixed at bottom with known height */
    .input-area, .chat-input-area {
        flex-shrink: 0;
        padding: 10px 16px 16px;
        background: #ffffff;
        border-top: 1px solid #e5e7eb;
        position: relative;
        z-index: 100;
        height: auto;
        min-height: 80px; /* Consistent height */
    }
    
    /* Mode toggle - More compact */
    .mode-selector-bar {
        margin-bottom: 8px;
        gap: 6px;
    }
    
    .mode-toggle-group {
        width: 100%;
        justify-content: center;
    }
    
    .mode-toggle-btn {
        font-size: 11px;
        padding: 6px 12px;
    }
    
    .mode-hint {
        font-size: 10px;
        text-align: center;
        margin-top: 4px;
        display: block;
        width: 100%;
    }
    
    .input-wrapper {
        padding: 8px 10px;
        border-radius: 20px;
    }
    
    .input-wrapper textarea {
        font-size: 14px;
        max-height: 120px;
    }
    
    .send-button {
        width: 30px;
        height: 30px;
    }
    
    .send-button i {
        font-size: 14px;
    }
    
    .input-hint {
        font-size: 10px;
        margin-top: 6px;
    }
    
    /* Login pages */
    .login-page-split {
        padding: 1.5rem 1rem;
    }

    .login-card {
        padding: 2.5rem 1.75rem;
        border-radius: 16px;
    }

    .logo-container-mobile {
        width: 64px;
        height: 64px;
        font-size: 1.75rem;
        margin-bottom: 1.25rem;
    }

    .login-card h3 {
        font-size: 1.75rem;
    }

    .login-card > p {
        font-size: 0.9375rem;
    }

    .login-input {
        padding: 0.8rem 1rem;
        font-size: 0.9375rem;
    }

    .login-card .btn-kusoya-blue {
        padding: 0.8rem 1.25rem;
        font-size: 0.9375rem;
    }
    
    .login-left-panel {
        display: none !important;
    }
    
    .login-right-panel {
        padding: 2rem 1.5rem !important;
    }
    
    /* === KEYBOARD FIX === */
    body.keyboard-open {
        position: fixed;
        width: 100%;
        overflow: hidden;
    }
    
    body.keyboard-open .ai-chat-wrapper {
        height: 100svh;
    }
    
    body.keyboard-open .ai-main-content {
        height: 100svh;
    }
    
    /* CRITICAL FIX: When keyboard is open, add more bottom padding to messages */
    body.keyboard-open .messages-container,
    body.keyboard-open .chat-messages-area {
        padding-bottom: 140px !important; /* Extra space when keyboard pushes input up */
    }
    
    body.keyboard-open .welcome-screen {
        padding-bottom: 140px !important;
    }
    
    body.keyboard-open .input-area {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 9999;
        background: #ffffff;
        box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
        padding: 8px 12px 12px !important;
    }
    
    /* Compact mode selector when keyboard open */
    body.keyboard-open .mode-selector-bar {
        margin-bottom: 6px;
    }
    
    body.keyboard-open .mode-toggle-btn {
        padding: 4px 10px;
        font-size: 10px;
    }
    
    body.keyboard-open .mode-hint {
        display: none;
    }
    
    /* Compact input wrapper */
    body.keyboard-open .input-wrapper {
        padding: 6px 8px;
    }
    
    body.keyboard-open .input-wrapper textarea {
        max-height: 60px;
        font-size: 13px;
    }
    
    /* Keep send button visible and accessible */
    body.keyboard-open .send-button {
        width: 28px;
        height: 28px;
        min-width: 28px;
        min-height: 28px;
        flex-shrink: 0;
    }
    
    body.keyboard-open .send-button i {
        font-size: 13px;
    }
    
    /* Hide input hint to save space */
    body.keyboard-open .input-hint {
        display: none;
    }
    
    /* iOS Safari */
    @supports (-webkit-touch-callout: none) {
        .ai-chat-wrapper {
            height: -webkit-fill-available;
        }
        
        body.keyboard-open .input-area {
            padding-bottom: max(12px, env(safe-area-inset-bottom));
        }
    }
}

/* ==========================================
   SMALL MOBILE (max 480px)
   ========================================== */
@media (max-width: 480px) {
    /* Chat */
    .welcome-title {
        font-size: 20px;
    }
    
    .welcome-subtitle {
        font-size: 14px;
    }
    
    .ai-avatar-large {
        width: 56px;
        height: 56px;
        font-size: 26px;
        margin-bottom: 12px;
    }
    
    .message-content.user-content,
    .message-content.ai-content {
        max-width: 90%;
        font-size: 13px;
        padding: 10px 14px;
    }
    
    .table {
        font-size: 0.8em;
    }
    
    /* Input */
    .input-area {
        padding: 8px 12px 12px;
    }
    
    .mode-selector-bar {
        margin-bottom: 6px;
    }
    
    .mode-toggle-btn {
        font-size: 10px;
        padding: 5px 10px;
    }
    
    .mode-hint {
        font-size: 9px;
    }
    
    .input-wrapper {
        padding: 7px 9px;
    }
    
    .input-wrapper textarea {
        font-size: 13px;
        max-height: 100px;
    }
    
    .send-button {
        width: 28px;
        height: 28px;
        border-radius: 7px;
    }
    
    .send-button i {
        font-size: 13px;
    }
    
    .input-hint {
        font-size: 9px;
        margin-top: 4px;
    }
    
    .prompt-card {
        padding: 12px;
        font-size: 13px;
    }
    
    /* Login */
    .login-page-split {
        padding: 1rem 0.75rem;
    }

    .login-card {
        padding: 2rem 1.5rem;
        border-radius: 12px;
    }

    .logo-container-mobile {
        width: 56px;
        height: 56px;
        font-size: 1.5rem;
    }

    .login-card h3 {
        font-size: 1.5rem;
    }

    .login-card > p {
        font-size: 0.875rem;
    }

    .login-input {
        padding: 0.75rem 0.875rem;
        font-size: 0.875rem;
    }

    .login-card .btn-kusoya-blue {
        padding: 0.75rem 1rem;
        font-size: 0.875rem;
    }

    .login-card .form-label {
        font-size: 0.875rem;
    }
    
    .login-right-panel {
        padding: 1.75rem 1.25rem !important;
    }
    
    .login-input {
        font-size: 0.9rem;
        padding: 0.75rem 0.9rem;
    }
}

/* ==========================================
   EXTRA SMALL (max 360px)
   ========================================== */
@media (max-width: 360px) {
    .login-card {
        padding: 1.75rem 1.25rem;
    }

    .logo-container-mobile {
        width: 52px;
        height: 52px;
        font-size: 1.375rem;
    }

    .login-card h3 {
        font-size: 1.375rem;
    }
    
    .mode-toggle-btn {
        font-size: 10px;
        padding: 4px 8px;
    }
    
    .welcome-title {
        font-size: 18px;
    }
    
    .prompt-card {
        padding: 10px;
    }
}

/* ==========================================
   PRINT
   ========================================== */
@media print {
    .ai-sidebar, .sidebar-chat, .input-area, 
    .navbar, .sidebar-toggle, .login-left-panel,
    .mode-selector-bar {
        display: none !important;
    }
    
    .ai-main-content, .main-chat-area, .login-right-panel {
        margin-left: 0;
        width: 100%;
    }
    
    .login-page-split {
        background: #ffffff;
    }

    .login-card {
        box-shadow: none;
        border: 1px solid #e5e7eb;
    }
}

/* ==========================================
   ACCESSIBILITY - REDUCED MOTION
   ========================================== */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
    
    .login-input,
    .login-card .btn-kusoya-blue,
    .password-toggle,
    .mode-toggle-btn,
    .send-button,
    .input-wrapper {
        transition: none !important;
    }
}

/* ==========================================
   DARK MODE (Optional)
   ========================================== */
@media (prefers-color-scheme: dark) {
    .mode-toggle-group {
        background: #1f2937;
    }
    
    .mode-toggle-btn {
        color: #e5e7eb;
    }
    
    .mode-toggle-btn.active {
        background: #374151;
        color: #ffffff;
    }
}

/* END OF RESPONSIVE STYLES */