/**
 * MadBot - Responsive Styles
 * Media queries for all breakpoints
 */

/* ==========================================================================
   BREAKPOINTS
   - Mobile: < 768px
   - Tablet: 768px - 1024px
   - Desktop: > 1024px
   ========================================================================== */

/* ==========================================================================
   DEFAULT STATES (Hidden on desktop, shown on mobile via media queries)
   ========================================================================== */

/* Mobile-only elements - hidden by default */
.mobile-menu-btn,
.mobile-close-btn,
.mobile-sidebar-overlay {
    display: none;
}

/* ==========================================================================
   MOBILE (< 768px)
   ========================================================================== */

@media (max-width: 768px) {
    /* ========================================
       MOBILE SIDEBAR SYSTEM
       ======================================== */
    
    /* Sidebar: hidden by default, slides in as overlay when open */
    .sidebar {
        display: flex !important;
        position: fixed !important;
        left: 0;
        top: 0;
        height: 100vh !important;
        height: 100dvh !important; /* Dynamic viewport height for mobile browsers */
        width: 85vw !important;
        max-width: 320px !important;
        z-index: 2000;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        border-right: 1px solid var(--border-color);
        box-shadow: none;
    }
    
    /* Sidebar visible when open */
    .sidebar.mobile-open {
        transform: translateX(0);
        box-shadow: 4px 0 24px rgba(0, 0, 0, 0.3);
    }
    
    /* Hide desktop collapse/expand buttons on mobile */
    .sidebar-collapse-btn,
    .sidebar-expand-btn {
        display: none !important;
    }
    
    /* Mobile close button inside sidebar */
    .mobile-close-btn {
        display: flex !important;
        position: absolute;
        top: 12px;
        right: 12px;
        width: 32px;
        height: 32px;
        background: var(--card-bg, rgba(255,255,255,0.05));
        border: 1px solid var(--border-color, rgba(255,255,255,0.1));
        border-radius: 8px;
        cursor: pointer;
        color: var(--text-muted, #9ca3af);
        align-items: center;
        justify-content: center;
        z-index: 10;
        transition: all 0.2s;
    }
    
    .mobile-close-btn:hover {
        background: var(--card-hover, rgba(255,255,255,0.1));
        color: var(--text-color, #f3f4f6);
    }
    
    .mobile-close-btn svg {
        width: 18px;
        height: 18px;
    }
    
    /* Light mode close button */
    .light-mode .mobile-close-btn {
        background: rgba(0,0,0,0.05);
        border-color: rgba(0,0,0,0.1);
        color: #64748b;
    }
    
    .light-mode .mobile-close-btn:hover {
        background: rgba(0,0,0,0.1);
        color: #1e293b;
    }
    
    /* ========================================
       MOBILE HAMBURGER BUTTON
       ======================================== */
    
    .mobile-menu-btn {
        display: flex !important;
        position: fixed;
        left: 12px;
        top: 12px;
        width: 40px;
        height: 40px;
        background: var(--surface-color, #1a1a24);
        border: 1px solid var(--border-color, rgba(255,255,255,0.1));
        border-radius: 10px;
        cursor: pointer;
        color: var(--text-muted, #9ca3af);
        align-items: center;
        justify-content: center;
        z-index: 1000;
        transition: all 0.2s;
    }
    
    .mobile-menu-btn:hover {
        background: var(--card-hover, rgba(255,255,255,0.1));
        color: var(--text-color, #f3f4f6);
    }
    
    .mobile-menu-btn svg {
        width: 20px;
        height: 20px;
    }
    
    /* Light mode hamburger */
    .light-mode .mobile-menu-btn {
        background: #ffffff;
        border-color: #e2e8f0;
        color: #64748b;
        box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    }
    
    .light-mode .mobile-menu-btn:hover {
        background: #f1f5f9;
        color: #1e293b;
    }
    
    /* Hide hamburger when sidebar is open */
    .mobile-menu-btn.hidden {
        opacity: 0;
        pointer-events: none;
    }
    
    /* ========================================
       MOBILE OVERLAY BACKDROP
       ======================================== */
    
    .mobile-sidebar-overlay {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 1999;
        opacity: 0;
        transition: opacity 0.3s ease;
    }
    
    .mobile-sidebar-overlay.visible {
        display: block;
        opacity: 1;
    }
    
    /* ========================================
       LAYOUT ADJUSTMENTS
       ======================================== */
    
    /* Full width content - no margin needed since sidebar is overlay */
    .app-container {
        flex-direction: column;
        height: 100vh !important;
        height: 100dvh !important; /* Dynamic viewport height for mobile browsers */
        overflow: hidden;
    }
    
    .main-content {
        margin-left: 0 !important;
        width: 100% !important;
    }
    
    /* ========================================
       CHAT LAYOUT - CRITICAL FOR MOBILE
       ======================================== */
    
    .chat-container {
        flex: 1;
        display: flex !important;
        flex-direction: column !important;
        height: 100% !important;
        min-height: 0; /* Important for flex child scrolling */
        overflow: hidden;
    }
    
    #chatInterface {
        /* Don't override display - JS controls visibility via inline style */
        /* These properties apply when JS sets display: flex */
        flex-direction: column !important;
        flex: 1;
        min-height: 0; /* Critical for nested flex scrolling */
        overflow: hidden;
    }
    
    .messages-container {
        flex: 1;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
        min-height: 0; /* Critical for flex scrolling */
        padding: 16px;
        padding-top: 16px; /* Normal padding, first message handles the space */
    }
    
    /* First message needs extra top margin to avoid hamburger overlap */
    .messages-container .message:first-child {
        margin-top: 50px;
    }
    
    .input-container {
        flex-shrink: 0; /* Never shrink the input */
        position: relative;
        width: 100%;
        padding: 12px;
        background: var(--bg-color, #0f0f1a);
        border-top: 1px solid var(--border-color, rgba(255,255,255,0.1));
    }
    
    .light-mode .input-container {
        background: #ffffff;
        border-top-color: #e2e8f0;
    }
    
    /* Welcome screen adjustments */
    .welcome-screen {
        padding-top: 80px; /* Space for hamburger */
    }
    
    /* Chat header adjustments - leave space for hamburger */
    .chat-header {
        padding-left: 60px !important;
    }
    
    /* Profile menu button - move slightly for mobile */
    #profileMenuBtn {
        top: 12px;
        right: 12px;
    }
    
    #profileMenu {
        top: 50px;
        right: 12px;
        min-width: 180px;
    }
    
    /* ========================================
       PAGE CONTAINERS
       ======================================== */
    
    .login-container,
    .register-container,
    .contact-container,
    .legal-container,
    .profile-container,
    .account-container {
        padding: 20px 16px;
        padding-top: 60px; /* Space for hamburger */
        align-items: center;
    }
    
    /* Push page titles to avoid hamburger overlap */
    .legal-title,
    .account-title,
    .profile-title,
    .contact-title {
        padding-left: 45px;
    }
    
    .login-content,
    .register-content,
    .contact-content,
    .legal-content,
    .profile-content,
    .account-content {
        max-width: 100%;
    }
    
    /* ========================================
       FORMS
       ======================================== */
    
    .form-row {
        flex-direction: column;
        gap: 0;
    }
    
    .form-row .form-group {
        width: 100%;
        margin-bottom: 20px;
    }
    
    /* ========================================
       MODALS
       ======================================== */
    
    .modal-content {
        width: 95%;
        max-width: none;
        margin: 10px;
        padding: 20px;
    }
    
    /* ========================================
       CARDS & TYPOGRAPHY
       ======================================== */
    
    .login-card,
    .register-card,
    .account-card,
    .profile-card {
        padding: 20px;
    }
    
    .login-title,
    .register-title,
    .page-title {
        font-size: 24px;
    }
    
    /* ========================================
       GRIDS
       ======================================== */
    
    .characters-grid {
        grid-template-columns: 1fr;
        padding: 16px;
        gap: 16px;
    }
}

/* ==========================================================================
   TABLET (769px - 1023px)
   ========================================================================== */

@media (min-width: 769px) and (max-width: 1023px) {
    /* Expanded sidebar - slightly narrower on tablet */
    .sidebar:not(.compact):not(.hidden) {
        width: 280px;
        min-width: 280px;
    }
    
    /* Compact sidebar - same as desktop */
    .sidebar.compact {
        width: 72px;
        min-width: 72px;
        overflow: visible;
    }
    
    .sidebar.compact .sidebar-header {
        padding: 56px 12px 12px 12px;
    }
    
    /* Collapse button centered in compact */
    .sidebar.compact .sidebar-collapse-btn,
    .sidebar.compact #sidebarCollapse {
        left: 50%;
        transform: translateX(-50%);
        right: auto;
    }
    
    /* Hide collapse button when sidebar is hidden */
    .sidebar.hidden .sidebar-collapse-btn,
    .sidebar.hidden #sidebarCollapse {
        display: none !important;
    }
    
    /* Page containers */
    .login-container,
    .register-container,
    .contact-container {
        padding: 30px 20px;
    }
    
    /* Character grid */
    .characters-grid {
        grid-template-columns: repeat(2, 1fr);
    }
    
    /* Create button - smaller on tablet */
    .sidebar-create-btn {
        font-size: 15px;
        gap: 6px;
        padding: 8px 10px;
        margin: 8px 10px;
    }
    
    .sidebar-create-btn .create-label,
    .sidebar-create-btn .create-link {
        font-size: 15px;
    }
    
    .sidebar-create-btn .create-icon {
        width: 18px;
        height: 18px;
        font-size: 13px;
    }
    
    .sidebar-create-btn .create-links {
        gap: 6px;
    }
    
    .sidebar-create-btn .create-arrow {
        font-size: 14px;
    }
    
    .sidebar-create-btn .create-separator {
        font-size: 14px;
    }
}

/* ==========================================================================
   DESKTOP (>= 1024px)
   ========================================================================== */

@media (min-width: 1024px) {
    /* Full sidebar on desktop */
    .sidebar {
        width: var(--sidebar-width, 320px);
        min-width: var(--sidebar-width, 320px);
    }
    
    /* Show all expanded elements */
    .sidebar .logo {
        display: flex;
    }
    
    .sidebar .logo-compact {
        display: none;
    }
    
    .sidebar .credits-display {
        display: flex;
    }
    
    .sidebar .section-title,
    .sidebar .character-info,
    .sidebar .upgrade-btn {
        display: block;
    }
    
    /* Character avatars - normal size */
    .sidebar .character-avatar {
        width: 48px;
        height: 48px;
        filter: none;
    }
    
    .sidebar .character-avatar img {
        width: 48px;
        height: 48px;
    }
    
    /* Sidebar tabs - full display */
    .sidebar .sidebar-tabs {
        flex-direction: row;
        padding: 8px 12px;
    }
    
    .sidebar .sidebar-tab .tab-label {
        display: inline;
    }
    
    /* Character grid */
    .characters-grid {
        grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    }
}

/* ==========================================================================
   LARGE DESKTOP (> 1440px)
   ========================================================================== */

@media (min-width: 1440px) {
    /* Wider content areas */
    .login-content,
    .register-content {
        max-width: 480px;
    }
    
    .contact-content {
        max-width: 600px;
    }
    
    .legal-content {
        max-width: 800px;
    }
    
    .profile-content,
    .account-content {
        max-width: 900px;
    }
}

/* ==========================================================================
   PRINT
   ========================================================================== */

@media print {
    .sidebar,
    #profileMenuBtn,
    #profileMenu,
    .sidebar-expand-btn,
    .upgrade-btn,
    .btn-primary,
    .submit-btn {
        display: none !important;
    }
    
    .app-container {
        display: block;
    }
    
    body {
        background: white;
        color: black;
    }
    
    .modal-overlay {
        display: none !important;
    }
}

/* ==========================================================================
   REDUCED MOTION
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    
    .sidebar {
        transition: none;
    }
    
    .character-card:hover {
        transform: none;
    }
    
    .modal-content {
        animation: none;
    }
}

/* ==========================================================================
   HIGH CONTRAST
   ========================================================================== */

@media (prefers-contrast: high) {
    :root {
        --border-color: rgba(255,255,255,0.3);
    }
    
    .light-mode {
        --border-color: rgba(0,0,0,0.3);
    }
    
    .btn-primary,
    .btn-gradient,
    .submit-btn {
        border: 2px solid white;
    }
    
    .form-input,
    .form-select {
        border-width: 2px;
    }
}
