/* ═══════════════════════════════════════════════════════════════
   TAROT MOBILE OPTIMIZATION - Final Quality Pass
   Fixes: overflow, touch targets, performance, responsive layouts
   ═══════════════════════════════════════════════════════════════ */

/* --- GLOBAL MOBILE FOUNDATION --- */
@media (max-width: 768px) {
    html, body {
        overflow-x: hidden !important;
        -webkit-text-size-adjust: 100%;
    }

    * {
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation; /* Kill 300ms delay */
    }

    /* Safe area for notch devices */
    body {
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
        padding-bottom: env(safe-area-inset-bottom);
    }

    /* Reduce motion for performance */
    @media (prefers-reduced-motion: reduce) {
        *, *::before, *::after {
            animation-duration: 0.01ms !important;
            transition-duration: 0.01ms !important;
        }
    }
}

/* --- PHASE 1: CRITICAL OVERFLOW FIX --- */
@media (max-width: 600px) {
    #app {
        overflow-x: hidden;
        max-width: 100vw;
    }

    .reading-mat {
        overflow-x: hidden !important;
        overflow-y: visible;
        max-width: 100vw;
        width: 100% !important;
        position: relative;
        contain: layout style;
    }

    /* Prevent cards from causing horizontal scroll */
    .tarot-card.fan-card {
        max-width: calc(100vw - 20px);
    }
}

/* --- PHASE 1: TOUCH TARGETS (WCAG 44px minimum) --- */
@media (max-width: 768px) {
    .tarot-card.fan-card.selectable {
        min-width: 44px;
        min-height: 44px;
        /* Expand touch area without changing visual size */
        padding: 0;
        position: absolute;
    }

    /* Invisible touch expansion for small cards */
    .tarot-card.fan-card.selectable::before {
        content: '';
        position: absolute;
        top: -8px;
        left: -6px;
        right: -6px;
        bottom: -8px;
        z-index: 1;
    }

    .btn-celestial {
        min-height: 52px !important;
        min-width: 44px;
    }

    .btn-icon {
        min-width: 44px;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .spread-card {
        min-height: 44px;
        padding: 16px 12px !important;
    }

    /* AI Chat input touch targets */
    #ai-question {
        min-height: 44px !important;
        font-size: 16px !important; /* Prevent iOS zoom */
    }

    .btn-ask-master {
        min-height: 44px !important;
        min-width: 44px !important;
    }
}

/* --- PHASE 1: CELTIC CROSS MOBILE LAYOUT FIX --- */
@media (max-width: 400px) {
    .reading-mat {
        min-height: 900px !important;
    }

    /* Position labels need to be smaller and not overflow */
    .position-label {
        font-size: 8px !important;
        padding: 3px 7px !important;
        letter-spacing: 0.3px !important;
        max-width: 70px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .position-label-container {
        max-width: 80px;
    }
}

@media (min-width: 401px) and (max-width: 600px) {
    .position-label {
        font-size: 9px !important;
        padding: 3px 8px !important;
        max-width: 90px;
    }
}

/* --- PHASE 2: PERFORMANCE - Disable heavy effects on mobile --- */
@media (max-width: 768px) {
    /* Kill stardust particles on mobile */
    .stardust-particle {
        display: none !important;
    }

    /* Reduce particle effects */
    .particle {
        display: none !important;
    }

    /* Simplify background effects */
    .bg-fog {
        display: none !important;
    }

    .bg-glow {
        animation: none !important;
        opacity: 0.3;
    }

    /* Reduce nebula animation complexity */
    .bg-nebula {
        animation-duration: 30s !important;
        will-change: auto !important;
    }

    /* Disable card glow animation on mobile for perf */
    .card-glow {
        animation: none !important;
        opacity: 0.3;
    }

    /* Simplify button animations */
    .btn-celestial::before,
    .btn-celestial::after {
        animation: none !important;
    }

    /* Reduce box-shadow complexity */
    .fan-card.selectable:hover .card-wrap,
    .fan-card.selectable:active .card-wrap,
    .fan-card.selectable.touch-active .card-wrap {
        box-shadow: 0 15px 30px rgba(0, 0, 0, 0.8), 0 0 20px rgba(245, 158, 11, 0.6) !important;
        filter: brightness(1.1) !important;
    }

    /* Disable text flicker animation */
    #instruction-text {
        animation: none !important;
        opacity: 0.9 !important;
    }

    /* Simplify gold pulse */
    .position-label {
        animation: labelEntry 0.6s ease forwards !important;
    }

    /* Disable atmospheric panel animations */
    #ai-chat-panel.atmosphere-fire,
    #ai-chat-panel.atmosphere-water,
    #ai-chat-panel.atmosphere-earth,
    #ai-chat-panel.atmosphere-air,
    #ai-chat-panel.atmosphere-transformation {
        animation: none !important;
    }
}

/* --- PHASE 2: IMAGE OPTIMIZATION --- */
@media (max-width: 768px) {
    .card-face.card-front {
        background-size: cover;
        background-position: center;
        image-rendering: -webkit-optimize-contrast;
    }

    /* Smaller card back for fan display */
    .card-face.card-back {
        background-size: cover;
    }
}

/* --- PHASE 3: RESPONSIVE SPREAD LAYOUTS --- */

/* 1-Card Spread Mobile */
@media (max-width: 600px) {
    .reading-mat .tarot-card.selected[data-spread="1"] {
        transform: translate(-50%, -50%) scale(0.9) !important;
    }
}

/* 3-Card Spread Mobile */
@media (max-width: 380px) {
    .reading-mat {
        min-height: 500px !important;
    }
}

/* 5-Card Cross Mobile */
@media (max-width: 500px) {
    .reading-mat[data-spread="5"] {
        min-height: 700px;
    }
}

/* Celtic Cross - Extra Small */
@media (max-width: 380px) {
    .reading-mat[data-spread="10"] {
        min-height: 850px !important;
    }
}

/* --- PHASE 3: AI CHAT PANEL MOBILE --- */
@media (max-width: 600px) {
    .ai-chat-panel {
        margin: 15px 8px 80px 8px !important;
        padding: 16px 12px !important;
        border-radius: 20px !important;
        width: calc(100% - 16px) !important;
        max-width: none !important;
    }

    .ai-chat-history {
        max-height: 50vh !important;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }

    .ai-msg {
        font-size: 0.9rem !important;
        line-height: 1.7 !important;
        padding: 14px 16px !important;
        word-break: break-word;
        overflow-wrap: break-word;
    }

    /* Sticky input at bottom */
    .ai-chat-input-box {
        position: sticky;
        bottom: 0;
        z-index: 100;
        background: rgba(10, 14, 26, 0.98) !important;
        border-radius: 20px;
        margin-top: 12px;
    }
}

/* --- PHASE 3: MODAL MOBILE (Bottom Sheet Pattern) --- */
@media (max-width: 768px) {
    .card-modal {
        align-items: flex-end !important;
        padding: 0 !important;
    }

    .card-modal .modal-content {
        max-height: 90vh !important;
        max-width: 100% !important;
        width: 100% !important;
        border-radius: 24px 24px 0 0 !important;
        margin: 0 !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }

    .modal-body {
        flex-direction: column !important;
        align-items: center !important;
        padding: 20px 16px !important;
        gap: 20px !important;
    }

    .modal-card-view {
        flex: 0 0 auto !important;
        width: 150px !important;
    }

    .modal-card-img {
        width: 150px !important;
        height: 240px !important;
    }

    .modal-card-details {
        width: 100% !important;
    }

    .modal-card-details h2 {
        font-size: 1.5rem !important;
        text-align: center;
    }

    .keywords {
        font-size: 0.95rem !important;
        text-align: center;
    }

    .detail-section h3 {
        font-size: 1.1rem !important;
    }

    .detail-section p,
    .card-meaning-text {
        font-size: 0.95rem !important;
        line-height: 1.7 !important;
    }

    /* Close button - larger touch target */
    .close-modal {
        min-width: 44px !important;
        min-height: 44px !important;
        top: 12px !important;
        right: 12px !important;
        font-size: 1.5rem !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
    }
}

/* --- PHASE 3: SETUP SCREEN MOBILE --- */
@media (max-width: 600px) {
    .spread-options {
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        gap: 12px !important;
        padding: 0 12px !important;
        overflow-x: visible !important;
    }

    .spread-card {
        min-width: 0 !important;
        width: 100% !important;
        padding: 16px 12px !important;
    }

    .spread-card h3 {
        font-size: 1.1rem !important;
    }

    .spread-card p {
        font-size: 0.8rem !important;
        line-height: 1.4;
    }

    /* Full-width CTA */
    .btn-celestial {
        width: 100% !important;
        max-width: none !important;
    }

    /* Question textarea */
    .initial-question-box {
        width: 100% !important;
        padding: 0 12px;
    }

    #setup-question {
        font-size: 16px !important; /* Prevent iOS zoom */
        min-height: 90px !important;
        border-radius: 14px !important;
    }
}

/* --- PHASE 3: HEADER MOBILE --- */
@media (max-width: 600px) {
    .header {
        padding: 12px 16px !important;
        height: auto !important;
    }

    .header .logo h1 {
        font-size: 1.1rem !important;
    }

    .logo-circle-header {
        width: 36px !important;
        height: 36px !important;
    }

    .logo-circle-header img {
        width: 28px !important;
        height: 28px !important;
    }
}

/* --- PHASE 3: DECK PILE MOBILE --- */
@media (max-width: 600px) {
    #deck-pile {
        transform: scale(0.75);
        transform-origin: center center;
    }
}

/* --- PHASE 3: ACTION BAR MOBILE --- */
@media (max-width: 600px) {
    .action-bar {
        margin: 30px 0 !important;
        padding: 0 12px !important;
    }

    .btn-group {
        gap: 12px !important;
    }

    .user-question-display {
        font-size: 1rem !important;
        padding: 10px 16px !important;
        border-radius: 14px !important;
        max-width: 100%;
        word-break: break-word;
    }
}

/* --- PHASE 3: KNOWLEDGE LIBRARY MOBILE --- */
@media (max-width: 600px) {
    .knowledge-grid {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
        padding: 0 12px !important;
    }

    .knowledge-detail {
        width: 100% !important;
        padding: 30px 16px !important;
        border-radius: 20px 20px 0 0 !important;
    }
}

/* --- PHASE 3: BIRTH CARD CALCULATOR MOBILE --- */
@media (max-width: 480px) {
    .birth-card-calculator {
        margin: 0 8px !important;
        padding: 16px 12px !important;
    }
}

/* --- UTILITY: Body scroll lock when modal open --- */
body.modal-open {
    overflow: hidden !important;
    position: fixed;
    width: 100%;
    height: 100%;
}

/* --- UTILITY: Smooth scroll container --- */
.ai-chat-history {
    scroll-behavior: smooth;
}

/* --- FIX: Prevent horizontal overflow from absolute positioned elements --- */
@media (max-width: 768px) {
    .position-label-container {
        max-width: calc(100vw - 20px);
    }

    /* Ensure mandala doesn't cause overflow */
    .mandala-overlay {
        overflow: hidden;
    }

    .mandala-container {
        max-width: 100vw;
        overflow: hidden;
    }
}

/* --- FIX: iOS input zoom prevention --- */
@media (max-width: 768px) {
    input, textarea, select {
        font-size: 16px !important;
    }
}

/* --- FIX: Keyboard push fix for AI chat --- */
@media (max-width: 768px) {
    .ai-chat-panel {
        /* Prevent layout shift when keyboard opens */
        transform: translateZ(0);
    }
}

/* --- VOICE BUTTON MOBILE --- */
@media (max-width: 600px) {
    .btn-voice-master {
        min-height: 44px;
        min-width: 44px;
        padding: 10px 14px !important;
    }
}
