/* GRID CONTAINER */
.cai-grid-menu-items {
    display: grid !important;
    grid-template-columns: repeat(3, 1fr) !important;
    gap: 10px !important;
    padding: 16px !important;
    box-sizing: border-box !important;
}

/* 1. Reset aggressive viewport locks */
html,
body {
    height: 100% !important;
    height: 100dvh !important;
    width: 100vw !important;
    overflow: hidden !important;
    position: fixed !important;
    /* Extremely important for iOS/Android WebView */
    top: 0;
    left: 0;
    margin: 0;
    padding: 0;
    -webkit-text-size-adjust: 100%;
    touch-action: none;
    /* Block default browser scroll/rubber-band */
}

body {
    background: #000;
}

.app-container {
    height: 100%;
    height: 100dvh;
    width: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: absolute;
    /* Relative to fixed body */
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    transform: translateZ(0);
    /* Force GPU */
}

/* Orientation Change Optimization */
@media screen and (orientation: landscape) {
    .app-container {
        height: 100dvh !important;
    }
}


/* 2. Scrollable areas must have touch enabled */
.main-content-area,
.chat-messages,
.tab-content,
.modal-scrollable-content,
#chatHistory {
    -webkit-overflow-scrolling: touch !important;
    overflow-y: auto !important;
    touch-action: pan-y !important;
    /* Allow vertical scroll but block horizontal/zoom */
    overscroll-behavior: contain !important;
}

/* Fix main tab contents */
.tab-content {
    height: 100%;
    overflow-y: auto !important;
    padding-bottom: 90px !important;
}

/* 3. Mobile native bottom sheet modals */
@media (max-width: 768px) {

    .modal-dialog-content,
    .auth-card,
    .modal-content-card {
        width: 100% !important;
        border-radius: 24px 24px 0 0 !important;
        margin: 0 !important;
        position: absolute !important;
        bottom: 0 !important;
        max-height: 90%;
        max-height: 90dvh !important;
        transform: translateZ(0);
    }

    .modal-overlay-container {
        align-items: flex-end !important;
        padding: 0 !important;
    }

    /* On mobile, auth card takes center stage — no bottom sheet for it */
    #firebase-auth-overlay .auth-card {
        border-radius: 24px !important;
        position: relative !important;
        bottom: auto !important;
        max-height: 92dvh !important;
        overflow-y: auto !important;
    }
}

/* 4. Touch targets — DO NOT override grid/icon buttons */
.button,
.icon-button,
.auth-input,
.search-input-wrapper input,
select {
    min-height: 48px;
    /* Safe touch target — NOT !important to allow grid overrides */
}

/* Fix chat input location for keyboard */
.chat-input-area {
    position: relative !important;
    z-index: 1000 !important;
    padding-bottom: env(safe-area-inset-bottom) !important;
    background: var(--bg-main, #000);
    margin-bottom: 0 !important;
    width: 100%;
}


/* Grid items must NOT be forced to 48px — they define their own height */
.cai-grid-item,
.nav-item,
.setting-item {
    min-height: unset;
    touch-action: manipulation;
}

/* 5. Chat input safe area */
.chat-input-container {
    padding-bottom: max(15px, env(safe-area-inset-bottom)) !important;
    z-index: 1000 !important;
    background: var(--bg-card, #141414);
}

/* 6. Z-Index Management */
#firebase-auth-overlay {
    z-index: 999999 !important;
}

.modal-overlay-container {
    z-index: 99999 !important;
}

#masterSettingsOverlay {
    z-index: 99998 !important;
}

#splashScreenContainer {
    z-index: 9999999 !important;
}

.app-header {
    z-index: 20000 !important;
}

#bottomNavigation {
    z-index: 1000 !important;
}

#global-background-layer {
    z-index: -2 !important;
    pointer-events: none;
}

#theme-effect-layer {
    z-index: -1 !important;
    pointer-events: none;
}

/* 7. AMOLED-FIRST Base Palette — CSS Variables (Non-breaking) */
:root {
    --amoled-black: #000000;
    --amoled-gray-1: #0A0A0A;
    --amoled-gray-2: #141414;
    --amoled-gray-3: #1C1C1E;
    --amoled-glass: rgba(12, 12, 12, 0.72);
    --amoled-border: rgba(255, 255, 255, 0.07);
}

/* 8. Glassmorphism Header */
.app-header {
    background: var(--amoled-glass) !important;
    backdrop-filter: blur(20px) saturate(180%) !important;
    -webkit-backdrop-filter: blur(20px) saturate(180%) !important;
    border-bottom: 1px solid var(--amoled-border) !important;
}

/* 9. Card Glassmorphism */
.explore-card-modern,
.personality-card-item {
    border: 1px solid var(--amoled-border) !important;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35) !important;
    transition: transform 0.2s cubic-bezier(0.2, 0.8, 0.2, 1), box-shadow 0.2s ease !important;
    border-radius: 18px !important;
    overflow: hidden !important;
}

.explore-card-modern:active,
.personality-card-item:active {
    transform: scale(0.96) !important;
}

/* 10. Auth Card — Dark & Clean */
.auth-card {
    background: var(--amoled-gray-2, #141414) !important;
    border: 1px solid var(--amoled-border) !important;
}

/* Fix password field white background on iOS/WebView autofill */
.auth-card input.auth-input,
.auth-card input[type="password"],
.auth-card input[type="email"],
.auth-card input[type="text"] {
    background-color: #1C1C1E !important;
    color: #FFFFFF !important;
    -webkit-text-fill-color: #FFFFFF !important;
    border: 1px solid rgba(255, 255, 255, 0.12) !important;
    border-radius: 14px !important;
    padding: 14px 16px !important;
    width: 100% !important;
    box-sizing: border-box !important;
    /* Kills WebView/Chrome autofill yellow */
    box-shadow: inset 0 0 0 9999px #1C1C1E !important;
}

/* Autofill fix for Chrome/WebView */
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus {
    -webkit-box-shadow: 0 0 0px 1000px #1C1C1E inset !important;
    -webkit-text-fill-color: #FFFFFF !important;
    caret-color: #FFFFFF;
}

/* 11. Modal inner card */
.modal-content-card {
    background: var(--amoled-gray-2, #141414) !important;
    border: 1px solid var(--amoled-border) !important;
}

/* 12. Modern Scrollbars */
::-webkit-scrollbar {
    width: 4px;
    height: 4px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.15);
    border-radius: 10px;
}

/* 13. HITBOX & PROPORTION FIXES */

/* Modals have consistent safe padding */
.modal-dialog-content {
    box-sizing: border-box !important;
    padding: clamp(16px, 4vw, 24px) !important;
}

/* Inputs */
.input-modern,
textarea {
    width: 100% !important;
    box-sizing: border-box !important;
    border-radius: 14px !important;
}

/* Named action buttons (NOT grid items) */
.button,
.btn-modern {
    box-sizing: border-box !important;
    border-radius: 14px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    text-align: center !important;
    white-space: nowrap !important;
}

/* Icon-only buttons (e.g. close, menu, header icons) */
.icon-button,
.cai-action-btn,
.header-icon-btn {
    border-radius: 50% !important;
    width: 44px !important;
    height: 44px !important;
    min-height: 44px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    flex-shrink: 0 !important;
}

/* GRID MENU ITEMS — the 3×3 action drawer in chat */
.cai-grid-item {
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 10px !important;
    border-radius: 18px !important;
    padding: 18px 12px !important;
    min-height: unset !important;
    height: auto !important;
    cursor: pointer;
    background: rgba(30, 30, 30, 0.6);
    border: 1px solid rgba(255, 255, 255, 0.06);
    transition: transform 0.15s ease, background 0.15s ease;
}

.cai-grid-item:active {
    transform: scale(0.93) !important;
    background: rgba(50, 50, 50, 0.8) !important;
}

.cai-grid-item i {
    font-size: 1.5rem !important;
    line-height: 1 !important;
}

.cai-grid-item span {
    font-size: 0.75rem !important;
    font-weight: 600 !important;
    color: #BFC6D4 !important;
    text-align: center !important;
    white-space: nowrap !important;
}

/* GRID CONTAINER */
.cai-grid-bar {
    display: grid !important;
    grid-template-columns: repeat(3, 1fr) !important;
    gap: 10px !important;
    padding: 16px !important;
    box-sizing: border-box !important;
}

/* 14. CHAT SCREEN FIXES */
/* Chat area must always have a visible bg */
#chatSection,
.chat-section {
    background: var(--bg-main, #0A0A0A) !important;
}

#chatHistory {
    background: transparent !important;
    min-height: 100px;
}

/* The jump-to-bottom scroll button */
#scrollToBottomBtn,
.scroll-to-bottom-btn {
    position: fixed !important;
    bottom: 100px !important;
    right: 20px !important;
    z-index: 500 !important;
    width: 48px !important;
    height: 48px !important;
    border-radius: 50% !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
}

/* 15. SPLASH SCREEN */
#splashScreenContainer {
    position: fixed !important;
    inset: 0 !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    background-color: #000000 !important;
    transition: opacity 0.5s ease-in-out, transform 0.5s ease !important;
}

#splashScreenContainer.fade-out {
    opacity: 0 !important;
    transform: scale(1.04) !important;
    pointer-events: none !important;
}

.splash-minimal {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: min(90vw, 320px);
}

.splash-loader-bar {
    width: 100%;
    background: #1A1A1A;
    height: 5px;
    border-radius: 10px;
    overflow: hidden;
}

#splashProgressBar {
    height: 100% !important;
    border-radius: 10px !important;
    background: linear-gradient(90deg, #555, #FFFFFF) !important;
    transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
}

/* 16. PREMIUM MODAL PLAN CARD FIX */
/* Ensure Premium plan cards are proportional and not squished */
.premium-plan-card {
    border-radius: 18px !important;
    padding: 18px !important;
    cursor: pointer !important;
}

/* Ensure plan selection visually works */
.premium-plan-card.selected {
    transform: scale(1.01) !important;
}

.premium-plan-card.ultra-card.selected {
    border-color: #FFD700 !important;
    background: rgba(255, 215, 0, 0.06) !important;
}

/* 17. THEME PREVIEW CONTAINER in Settings */
#msThemeList,
.theme-previews-container {
    display: flex !important;
    flex-direction: row !important;
    gap: 12px !important;
    overflow-x: auto !important;
    padding: 8px 16px !important;
    -webkit-overflow-scrolling: touch !important;
}

/* Individual theme preview boxes */
.theme-preview-box {
    flex-shrink: 0 !important;
    border-radius: 14px !important;
    border: 2px solid transparent !important;
    cursor: pointer !important;
    transition: border-color 0.2s, transform 0.2s !important;
    overflow: hidden !important;
}

.theme-preview-box.active {
    border-color: var(--accent-primary, #FFFFFF) !important;
    transform: scale(1.05) !important;
}

/* 14. CHAT SCREEN FIXES */
#chatSection,
.chat-section {
    background: var(--bg-main, #0B0D14) !important;
}

body.has-custom-background #chatSection,
body.has-custom-background .chat-section {
    background: transparent !important;
}

#chatHistory {
    background: transparent !important;
    padding-bottom: 120px !important;
}

/* 15. SPLASH SCREEN - CLEAN LOGO */
#splashScreenContainer {
    position: fixed !important;
    inset: 0 !important;
    background: #000000 !important;
    z-index: 9999999 !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    transition: opacity 0.6s cubic-bezier(0.23, 1, 0.32, 1), transform 0.6s cubic-bezier(0.23, 1, 0.32, 1) !important;
}

#splashScreenContainer.fade-out {
    opacity: 0 !important;
    transform: scale(1.08) !important;
    pointer-events: none !important;
}

.splash-avatar-wrapper {
    width: 200px !important;
    height: auto !important;
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
    border-radius: 0 !important;
}

.splash-img {
    width: 100% !important;
    height: auto !important;
    object-fit: contain !important;
    border-radius: 20px !important;
}

/* 16. INSTAGRAM-STYLE PREMIUM CHAT INPUT */
.chat-input-wrapper.gemini-style-v3 {
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    background: rgba(10, 10, 10, 0.85) !important;
    backdrop-filter: blur(25px) saturate(160%) !important;
    -webkit-backdrop-filter: blur(25px) saturate(160%) !important;
    border-top: 1px solid rgba(255, 255, 255, 0.08) !important;
    padding: 10px 14px max(10px, env(safe-area-inset-bottom)) 14px !important;
    display: flex !important;
    align-items: flex-end !important;
    gap: 10px !important;
    z-index: 1010 !important;
}

#userMessageInput {
    background: rgba(255, 255, 255, 0.05) !important;
    border: 1px solid rgba(255, 255, 255, 0.12) !important;
    border-radius: 24px !important;
    color: #FFFFFF !important;
    padding: 10px 16px !important;
    font-size: 1rem !important;
    line-height: 1.4 !important;
    min-height: 48px !important;
    max-height: 140px !important;
    flex: 1 !important;
    resize: none !important;
    margin: 0 !important;
}

#userMessageInput::placeholder {
    color: rgba(255, 255, 255, 0.4) !important;
}

.right-controls {
    display: flex !important;
    align-items: center !important;
    height: 48px !important;
    gap: 4px !important;
}

.icon-button.footer-action-btn,
.icon-button.send-btn,
.icon-button.continue-btn-input {
    width: 44px !important;
    height: 44px !important;
    background: transparent !important;
    border: none !important;
    color: var(--accent-primary, #FFFFFF) !important;
}

#rpFormatBtn {
    font-size: 1.8rem !important;
    opacity: 0.8;
}

#sendMessageBtn i {
    font-size: 1.3rem !important;
}

/* FIX: Chat bubble character rendering */
.message-bubble {
    font-family: 'Lexend', sans-serif !important;
}

/* Mobile viewport adjustments */
@media (max-width: 480px) {
    #chatInputProfileBtn {
        width: 34px !important;
        height: 34px !important;
    }
}

/* 17. DISABLED MODELS (STYLING) */
.model-item.model-disabled {
    opacity: 0.4 !important;
    filter: grayscale(0.8) !important;
    cursor: default !important;
    pointer-events: auto !important;
    /* Allow click for toast */
}

.model-item.model-disabled:active {
    transform: none !important;
}

/* FIX: Ensure chat-messages container is scrollable but stays in flow */
.chat-messages {
    flex: 1 !important;
    overflow-y: auto !important;
    padding-bottom: 20px !important;
}