/* ========================================
   MOBILE ENHANCEMENT STYLES
   Advanced Mobile Optimizations & Animations
   ======================================== */

/* ========================================
   VIEWPORT & SAFE AREA
   ======================================== */

:root {
    --safe-area-top: max(20px, env(safe-area-inset-top));
    --safe-area-right: max(20px, env(safe-area-inset-right));
    --safe-area-bottom: max(20px, env(safe-area-inset-bottom));
    --safe-area-left: max(20px, env(safe-area-inset-left));
    --mobile-transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Fix viewport issues */
html {
    -webkit-font-smoothing: antialiased;
    -webkit-text-size-adjust: 100%;
    -moz-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    -webkit-touch-callout: none;
    -webkit-user-select: text;
    user-select: text;
}

/* Prevent accidental horizontal scrolling on mobile (often caused by wide nav rows) */
@media (max-width: 767px) {
    html,
    body {
        overflow-x: hidden;
    }

    /* Force hamburger menu dropdown to be vertical and viewport-safe */
    .nav-menu {
        display: none;
        flex-direction: column;
        align-items: stretch;
        position: absolute;
        top: 100%;
        left: 0;
        right: 0;
        width: 100%;
        max-width: 100vw;
        box-sizing: border-box;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
    }

    .nav-menu.active {
        display: flex;
    }

    .nav-menu li {
        width: 100%;
    }

    .nav-menu a {
        width: 100%;
        justify-content: flex-start;
        white-space: normal;
    }

    /* Hero 4D canvas: keep gestures on the scene */
    #hero4DScene {
        touch-action: none;
    }
    .hero-4d-canvas-wrapper {
        overscroll-behavior: contain;
    }
}

/* Allow text selection on inputs */
input, textarea, [contenteditable] {
    -webkit-user-select: text;
    user-select: text;
}

/* ========================================
   MOBILE BOTTOM NAVIGATION
   ======================================== */

.mobile-bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 1000;
    background: rgba(15, 23, 42, 0.95);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    border-top: 1px solid rgba(139, 92, 246, 0.2);
    padding-bottom: var(--safe-area-bottom);
    animation: slideInUp 0.3s ease-out forwards;
}

.mobile-nav-container {
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: 60px;
}

.mobile-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    height: 100%;
    color: rgba(255, 255, 255, 0.6);
    text-decoration: none;
    transition: var(--mobile-transition);
    border: none;
    background: none;
    cursor: pointer;
    padding: 8px;
    font-size: 12px;
}

.mobile-nav-item i {
    font-size: 24px;
    margin-bottom: 4px;
}

.mobile-nav-item:active,
.mobile-nav-item.active {
    color: #8b5cf6;
    background: rgba(139, 92, 246, 0.1);
}

.mobile-nav-item.touch-active {
    transform: scale(0.9);
}

/* ========================================
   MOBILE MENU TOGGLE
   ======================================== */

.mobile-menu-toggle {
    position: relative;
    padding: 8px;
}

.mobile-menu-toggle::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: rgba(139, 92, 246, 0.1);
    opacity: 0;
    transition: opacity 0.3s ease;
}

.mobile-menu-toggle:active::after {
    opacity: 1;
    animation: ripple 0.6s ease-out;
}

/* ========================================
   MOBILE FORMS
   ======================================== */

input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
textarea,
select {
    font-size: 16px;
    min-height: 44px;
    padding: 12px;
    border-radius: 8px;
    border: 2px solid rgba(139, 92, 246, 0.2);
    background: rgba(139, 92, 246, 0.05);
    color: white;
    transition: var(--mobile-transition);
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
}

input[type="text"]:focus,
input[type="email"]:focus,
input[type="password"]:focus,
input[type="number"]:focus,
textarea:focus,
select:focus {
    border-color: #8b5cf6;
    background: rgba(139, 92, 246, 0.1);
    outline: none;
    box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.2);
}

.mobile-focused {
    animation: popIn 0.3s ease-out forwards;
}

/* Mobile checkbox improvements */
input[type="checkbox"] {
    width: 24px;
    height: 24px;
    margin-right: 8px;
    cursor: pointer;
    accent-color: #8b5cf6;
}

/* Mobile select improvements */
select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%238b5cf6' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 36px;
}

/* ========================================
   TOUCH FEEDBACK
   ======================================== */

button,
a[role="button"],
.btn,
.floating-action-button,
.mobile-nav-item {
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    position: relative;
}

button:active,
a[role="button"]:active,
.btn:active,
.floating-action-button:active {
    transform: scale(0.95);
}

.touch-active {
    background: rgba(139, 92, 246, 0.2);
    transform: scale(0.95);
}

/* ========================================
   FLOATING ACTION BUTTON
   ======================================== */

.floating-action-button {
    position: fixed;
    bottom: 90px;
    right: 20px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: linear-gradient(135deg, #8b5cf6 0%, #6366f1 100%);
    color: white;
    border: none;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(139, 92, 246, 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    z-index: 999;
    transition: var(--mobile-transition);
}

.floating-action-button:hover,
.floating-action-button:focus {
    transform: scale(1.1);
    box-shadow: 0 6px 20px rgba(139, 92, 246, 0.6);
}

.floating-action-button:active {
    transform: scale(0.95);
}

/* ========================================
   MOBILE SCROLL INDICATOR
   ======================================== */

.mobile-scroll-indicator {
    position: fixed;
    top: 0;
    left: 0;
    height: 3px;
    background: linear-gradient(90deg, #8b5cf6 0%, #6366f1 100%);
    z-index: 10000;
    width: 0;
    transition: width 0.1s ease-out;
}

/* ========================================
   PULL TO REFRESH
   ======================================== */

.pull-to-refresh {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: #8b5cf6;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 20px;
    opacity: 0;
    pointer-events: none;
    z-index: 1001;
}

.pull-to-refresh.visible {
    opacity: 1;
    animation: popIn 0.3s ease-out forwards;
}

/* ========================================
   HEADER MOBILE OPTIMIZATION
   ======================================== */

header {
    transition: transform 0.3s ease-out;
    position: sticky;
    top: 0;
    z-index: 100;
}

header.header-hidden {
    transform: translateY(-100%);
}

/* Mobile header padding */
@media (max-width: 768px) {
    header {
        padding-top: var(--safe-area-top);
    }

    nav {
        max-width: 100%;
    }
}

/* ========================================
   MOBILE KEYBOARD HANDLING
   ======================================== */

body[data-keyboard-visible="true"] {
    padding-bottom: 20px;
}

body[data-keyboard-visible="true"] .floating-action-button {
    bottom: calc(90px + var(--safe-area-bottom));
}

body[data-keyboard-visible="true"] .mobile-bottom-nav {
    transform: translateY(-100%);
}

/* ========================================
   MOBILE IMAGES
   ======================================== */

img {
    max-width: 100%;
    height: auto;
    display: block;
    -webkit-user-drag: none;
}

img.image-loaded {
    animation: fadeIn 0.5s ease-out forwards;
}

/* Lazy loading placeholder */
img[loading="lazy"] {
    background: rgba(139, 92, 246, 0.1);
    min-height: 200px;
    opacity: 0.8;
}

img[loading="lazy"].loaded {
    opacity: 1;
}

/* ========================================
   MOBILE TEXT OPTIMIZATION
   ======================================== */

body {
    font-size: 16px;
}

h1 { font-size: 28px; }
h2 { font-size: 24px; }
h3 { font-size: 20px; }
h4 { font-size: 18px; }
h5 { font-size: 16px; }
h6 { font-size: 14px; }

@media (max-width: 480px) {
    h1 { font-size: 24px; }
    h2 { font-size: 20px; }
    h3 { font-size: 18px; }
    h4 { font-size: 16px; }

    body {
        font-size: 14px;
    }
}

/* ========================================
   MOBILE SPACING
   ======================================== */

@media (max-width: 768px) {
    * {
        margin: 0;
        padding: 0;
    }

    body {
        padding-bottom: 80px;
    }

    .container,
    .container-fluid {
        padding: 0 16px;
    }

    section {
        padding: 24px 0;
    }

    header {
        padding: var(--safe-area-top) 16px 16px;
    }
}

/* ========================================
   MOBILE CARDS & CONTAINERS
   ======================================== */

.card {
    border-radius: 12px;
    overflow: hidden;
    margin-bottom: 16px;
}

.card-body {
    padding: 16px;
}

/* Touch-friendly spacing */
a, button {
    min-height: 44px;
    min-width: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* ========================================
   MOBILE DIALOG & MODALS
   ======================================== */

.mobile-modal {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(15, 23, 42, 0.95);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    border-radius: 16px 16px 0 0;
    padding: 20px;
    padding-top: 24px;
    padding-bottom: calc(20px + var(--safe-area-bottom));
    max-height: 90vh;
    overflow-y: auto;
    animation: slideInUp 0.3s ease-out forwards;
    z-index: 1002;
}

.mobile-modal::before {
    content: '';
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 4px;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 2px;
}

/* Standard site modals (used by Integrations + Contact) */
@media (max-width: 768px) {
    .modal {
        padding: 16px;
        padding-bottom: calc(16px + var(--safe-area-bottom));
    }

    .modal .modal-content {
        width: 100%;
        max-width: 560px;
        max-height: calc(90vh - var(--safe-area-top));
        overflow: auto;
        -webkit-overflow-scrolling: touch;
    }

    .modal-actions {
        justify-content: stretch;
    }

    .modal-actions .btn {
        width: 100%;
    }
}

/* Chat widget box sizing on small screens */
@media (max-width: 480px) {
    .chat-box {
        left: 12px;
        right: 12px;
        width: auto;
        height: min(520px, 70vh);
        bottom: calc(86px + var(--safe-area-bottom));
    }

    .chat-widget {
        right: 16px;
        bottom: calc(16px + var(--safe-area-bottom));
    }

    .chat-header-actions {
        gap: 8px;
    }
}

/* ========================================
   MOBILE ANIMATIONS
   ======================================== */

@media (max-width: 768px) {
    /* Reduce animation duration on mobile */
    .fadeIn,
    .fadeInUp,
    .fadeInDown,
    .slideInLeft,
    .slideInRight,
    .scaleIn,
    .popIn,
    .bounceIn {
        animation-duration: 0.3s;
    }

    /* Smooth scroll */
    html {
        scroll-behavior: smooth;
    }

    /* Mobile-first transitions */
    * {
        transition-duration: 0.2s;
    }

    /* Disable parallax */
    [data-parallax] {
        transform: none !important;
    }

    /* Reduce motion support */
    @media (prefers-reduced-motion: reduce) {
        * {
            animation-duration: 0.01ms !important;
            animation-iteration-count: 1 !important;
            transition-duration: 0.01ms !important;
        }
    }
}

/* ========================================
   MOBILE ORIENTATION
   ======================================== */

@media (orientation: landscape) {
    body {
        padding-bottom: 60px;
    }

    h1 { font-size: 20px; }
    h2 { font-size: 18px; }

    .floating-action-button {
        bottom: 70px;
    }
}

@media (orientation: portrait) {
    .mobile-bottom-nav {
        padding-bottom: calc(var(--safe-area-bottom) + 10px);
    }
}

/* ========================================
   MOBILE NOTCH HANDLING
   ======================================== */

@supports (padding: max(0px)) {
    body {
        padding-left: max(20px, var(--safe-area-left));
        padding-right: max(20px, var(--safe-area-right));
    }

    .mobile-bottom-nav {
        padding-left: max(0px, var(--safe-area-left));
        padding-right: max(0px, var(--safe-area-right));
    }
}

/* ========================================
   MOBILE PERFORMANCE
   ======================================== */

/* GPU acceleration */
.mobile-nav-item,
.floating-action-button,
.mobile-modal,
.card {
    will-change: transform, opacity;
    transform: translateZ(0);
    backface-visibility: hidden;
}

/* Optimize scrolling */
body {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

/* ========================================
   MOBILE RESPONSIVE GRID
   ======================================== */

@media (max-width: 768px) {
    .grid {
        grid-template-columns: 1fr;
    }

    .col {
        grid-column: span 1;
    }

    .col-2 {
        grid-column: span 1;
    }
}

/* ========================================
   MOBILE MENU ANIMATIONS
   ======================================== */

@keyframes slideOutLeft {
    from {
        transform: translateX(0);
        opacity: 1;
    }
    to {
        transform: translateX(-100%);
        opacity: 0;
    }
}

@keyframes mobileSwipeIn {
    from {
        transform: translateY(100%);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

/* ========================================
   MOBILE DARK MODE
   ======================================== */

@media (prefers-color-scheme: dark) {
    .mobile-bottom-nav {
        background: rgba(15, 23, 42, 0.98);
        border-top-color: rgba(139, 92, 246, 0.15);
    }

    .mobile-modal {
        background: rgba(15, 23, 42, 0.98);
    }

    input,
    textarea,
    select {
        background: rgba(139, 92, 246, 0.05);
        color: white;
    }
}

/* ========================================
   MOBILE DEBUGGING
   ======================================== */

@media (max-width: 768px) {
    /* Safe area visualization (dev only) */
    body[data-show-safe-area="true"]::before {
        content: '';
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        pointer-events: none;
        border: 2px dashed red;
        border-top-width: var(--safe-area-top);
        border-right-width: var(--safe-area-right);
        border-bottom-width: var(--safe-area-bottom);
        border-left-width: var(--safe-area-left);
        z-index: 99999;
    }
}
