/* АДАПТИВНЫЕ СТИЛИ ДЛЯ ВСЕХ УСТРОЙСТВ И TELEGRAM MINIAPP v1.4 - ИСПРАВЛЕНО */

/* Сброс стилей для мобильных браузеров */
* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

html {
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

/* Специальные стили для Telegram WebApp */
.telegram-webapp {
    --tg-theme-bg-color: var(--primary-bg);
    --tg-theme-text-color: var(--text-primary);
    --tg-theme-hint-color: var(--text-secondary);
    --tg-theme-link-color: var(--accent-primary);
    --tg-theme-button-color: var(--accent-primary);
    --tg-theme-button-text-color: white;
}

/* Дополнительные исправления для Telegram WebApp */
@supports (height: 100dvh) {
    body {
        min-height: 100dvh !important;
    }
}

/* Специальные правила для WebKit в Telegram */
@supports (-webkit-appearance: none) {
    body {
        -webkit-text-size-adjust: 100%;
        -webkit-tap-highlight-color: transparent;
    }
    
    .main-content {
        -webkit-overflow-scrolling: touch;
    }
}

/* Исправление для Telegram Mobile App */
body {
    min-height: 100vh;
    min-height: 100dvh;
    min-height: -webkit-fill-available;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    position: relative;
}

/* Исправление статуса */
.status-badge {
    min-width: 120px;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    white-space: nowrap;
}

#status-text {
    font-size: 0.875rem;
    font-weight: 600;
}

/* Исправления для Safari на iOS (часто используется в Telegram) */
@supports (-webkit-touch-callout: none) {
    .form-input,
    .form-textarea,
    .form-select {
        font-size: 16px !important; /* Предотвращает зум на iOS */
        -webkit-appearance: none;
        border-radius: 12px;
    }
    
    body {
        -webkit-overflow-scrolling: touch;
        -webkit-tap-highlight-color: transparent;
    }
    
    .btn {
        -webkit-tap-highlight-color: transparent;
        cursor: pointer;
    }
}

/* Фиксация для height в Telegram WebApp */
html {
    height: 100%;
}

body {
    min-height: 100vh;
    min-height: 100dvh;
    min-height: -webkit-fill-available;
    height: auto;
}

/* Улучшения для Telegram темной темы */
@media (prefers-color-scheme: dark) {
    .telegram-webapp {
        --primary-bg: #0A0A12;
        --secondary-bg: #151525;
        --text-primary: #FFFFFF;
        --text-secondary: #E4E4E7;
    }
}

/* Базовые настройки для мобильных */
@media (max-width: 480px) {
    body {
        position: relative;
        touch-action: manipulation;
    }
    
    .header-content {
        flex-direction: column;
        gap: 0.75rem;
        padding: 0.75rem;
    }

    .logo {
        font-size: 1.25rem;
    }

    .status-badge {
        padding: 0.375rem 0.75rem;
        font-size: 0.75rem;
    }

    .nav-tabs {
        margin: 1rem 0.5rem !important;
        padding: 0.25rem !important;
        overflow-x: auto !important;
        overflow-y: hidden !important;
        scrollbar-width: none !important;
        -ms-overflow-style: none !important;
        gap: 0.5rem !important;
        -webkit-overflow-scrolling: touch !important;
        justify-content: flex-start !important;
        white-space: nowrap !important;
        touch-action: pan-x;
    }

    .nav-tabs::-webkit-scrollbar {
        display: none !important;
    }
    
    .nav-tab {
        padding: 0.5rem 0.75rem !important;
        font-size: 0.7rem !important;
        white-space: nowrap !important;
        flex: 0 0 auto !important;
        min-width: 70px !important;
        max-width: 90px !important;
        margin: 0 !important;
        text-align: center !important;
        display: flex !important;
        flex-direction: column !important;
        align-items: center !important;
        justify-content: center !important;
        gap: 0.25rem !important;
        border-radius: 12px !important;
    }

    .nav-tab span:first-child {
        font-size: 1rem;
    }

    /* Правильный размер активного элемента в мобильном меню */
    .nav-tab.active {
        width: 100%;
        min-width: auto;
    }

    /* Убираем перенос текста в маленьких пунктах меню */
    .nav-tab span:last-child {
        font-size: 0.65rem;
        line-height: 1;
        text-overflow: ellipsis;
        overflow: hidden;
        max-width: 100%;
    }

    .main-content {
        padding: 0 0.75rem 1.5rem;
        position: relative;
        overflow-x: hidden;
        overflow-y: auto;
    }

    .card {
        padding: 1.25rem;
        border-radius: 16px;
    }

    .dashboard-grid {
        /* Для мобильных - одна колонка */
        grid-template-columns: 1fr !important;
        grid-gap: 1rem !important;
        gap: 1rem !important;
        
        /* Flex fallback для мобильных */
        flex-direction: column;
    }
    
    .dashboard-grid > * {
        min-width: 100% !important;
        flex: 1 1 auto !important;
    }

    .kpi-value {
        font-size: 2.25rem;
    }

    .funnel-card {
        grid-column: span 1;
    }

    .funnel-stage {
        flex-direction: column;
        gap: 0.75rem;
        text-align: center;
        padding: 1.25rem;
    }

    .stage-metrics {
        justify-content: center;
        flex-direction: row;
        gap: 1rem;
    }

    .action-buttons {
        grid-template-columns: 1fr !important;
        grid-gap: 1rem !important;
        gap: 1rem !important;
        
        /* Flex fallback для мобильных */
        flex-direction: column;
    }
    
    .action-buttons > * {
        min-width: 100% !important;
        flex: 1 1 auto !important;
    }

    .action-btn {
        padding: 1.5rem;
    }
}

/* Планшеты в портретной ориентации */
@media (min-width: 481px) and (max-width: 768px) {
    .dashboard-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 1.25rem;
    }

    .funnel-card {
        grid-column: span 2;
    }

    .action-buttons {
        grid-template-columns: repeat(2, 1fr);
    }

    .nav-tab {
        padding: 0.75rem 1rem;
        font-size: 0.8rem;
    }
}

/* Планшеты в альбомной ориентации */
@media (min-width: 769px) and (max-width: 1024px) {
    .dashboard-grid {
        grid-template-columns: repeat(3, 1fr);
    }

    .funnel-card {
        grid-column: span 3;
    }

    .action-buttons {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* Десктопы */
@media (min-width: 1025px) {
    .dashboard-grid {
        grid-template-columns: repeat(4, 1fr);
    }

    .funnel-card {
        grid-column: span 4;
    }

    .action-buttons {
        grid-template-columns: repeat(4, 1fr);
    }
}

/* Специальные стили для очень маленьких экранов */
@media (max-width: 320px) {
    .logo {
        font-size: 1rem;
    }

    .nav-tab {
        padding: 0.5rem;
        font-size: 0.7rem;
    }

    .nav-tab span:last-child {
        display: none; /* Скрываем текст, оставляем только иконки */
    }

    .kpi-value {
        font-size: 2rem;
    }

    .card {
        padding: 1rem;
    }

    /* Analytics for very small screens */
    .overview-cards {
        grid-template-columns: 1fr;
        gap: 0.5rem;
    }

    .card-value {
        font-size: 1.5rem;
    }

    .card-icon {
        font-size: 1.5rem;
    }

    .chart-container {
        height: 220px;
    }

    .chart-card canvas {
        height: min(75vw, 220px) !important;
        max-height: 240px !important;
        min-height: 200px !important;
    }

    /* График активности для очень маленьких экранов */
    #chart-activity {
        height: min(80vw, 250px) !important;
        max-height: 270px !important;
        min-height: 220px !important;
    }

    .chart-card {
        padding: 20px 16px 16px 16px;
    }

    .insights-grid {
        gap: 0.75rem;
    }

    .insight-card {
        padding: 0.75rem;
    }
}

/* Высота экрана для мобильных */
@media (max-height: 600px) {
    .header {
        padding: 0.5rem 1rem;
    }

    .nav-tabs {
        margin: 0.75rem;
    }

    .card {
        padding: 1rem;
    }

    .kpi-card {
        padding: 1rem;
    }

    .kpi-value {
        font-size: 2rem;
        margin-bottom: 0.5rem;
    }
}

/* Исправления для форм на мобильных */
@media (max-width: 768px) {
    .form-input, .form-textarea, .form-select {
        font-size: 16px; /* Предотвращает зум на iOS */
        padding: 1rem;
    }

    .form-actions {
        flex-direction: column;
        gap: 0.75rem;
    }

    .form-actions .btn {
        width: 100%;
        min-height: 48px;
    }

    .option-grid {
        grid-template-columns: 1fr;
        gap: 0.75rem;
    }

    .delivery-grid {
        grid-template-columns: 1fr;
    }

    .publish-grid {
        grid-template-columns: 1fr;
    }

    .leadmagnets-list {
        grid-template-columns: 1fr;
    }

    .leadmagnet-actions {
        flex-direction: column;
        gap: 0.5rem;
    }

    .leadmagnet-actions .btn {
        width: 100%;
        min-height: 44px;
    }

    .leadmagnet-item {
        padding: 1.5rem;
        border-radius: 20px;
    }

    .leadmagnet-stats {
        gap: 1rem;
        padding: 0.75rem;
    }

    .stat-value {
        font-size: 1.25rem;
    }

    .leadmagnet-material {
        padding: 0.75rem;
        margin: 1rem 0;
    }

    /* Analytics responsive fixes */
    .analytics-filters {
        flex-direction: column;
        align-items: stretch;
        gap: 1rem;
        margin-bottom: 1.5rem;
    }

    .analytics-content {
        gap: 1.5rem;
    }

    .analytics-chart {
        padding: 1.5rem;
        min-height: 250px;
        border-radius: 20px;
    }

    .chart-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 1rem;
        margin-bottom: 1rem;
        padding-bottom: 0.75rem;
    }

    .chart-title {
        font-size: 1rem;
        margin-bottom: 0;
    }

    .chart-icon {
        font-size: 1.25rem;
    }

    .chart-wrapper {
        padding: 0.75rem;
    }

    .chart-select {
        width: 100%;
        min-width: unset;
    }

    .info-badge {
        font-size: 0.7rem;
        padding: 0.25rem 0.5rem;
    }

    /* Overview cards mobile optimization */
    .overview-cards {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.75rem;
    }

    .overview-card {
        padding: 1.5rem;
        border-radius: 20px;
    }

    .card-icon {
        font-size: 1.75rem;
        margin-bottom: 0.5rem;
    }

    .card-value {
        font-size: 1.75rem;
        margin-bottom: 0.25rem;
    }

    .card-label {
        font-size: 0.875rem;
        margin-bottom: 0.25rem;
    }

    .card-sublabel {
        font-size: 0.75rem;
    }

    /* Charts mobile optimization - УВЕЛИЧЕННЫЕ размеры */
    .chart-container {
        height: 280px;
        /* Улучшенные touch области для мобильных */
        padding: 10px;
        margin: -10px;
    }

    .chart-card canvas {
        height: min(65vw, 280px) !important;
        max-height: 300px !important;
        min-height: 240px !important;
        /* Увеличиваем область клика для мобильных */
        cursor: pointer;
        touch-action: manipulation;
    }

    /* Специальная оптимизация графика активности для мобильных */
    #chart-activity {
        height: min(70vw, 320px) !important;
        max-height: 350px !important;
        min-height: 280px !important;
    }

    .chart-card {
        padding: 24px 20px 20px 20px;
    }

    .chart-legend {
        gap: 0.5rem;
        justify-content: center;
        flex-wrap: wrap;
    }

    .legend-item {
        font-size: 0.75rem;
    }

    /* Insights mobile optimization */
    .insights-grid {
        grid-template-columns: 1fr;
        gap: 1rem;
    }

    .insight-card {
        padding: 1.5rem;
        border-radius: 20px;
    }

    .insight-icon {
        font-size: 1.5rem;
    }

    .insight-title {
        font-size: 0.875rem;
    }

    .insight-text {
        font-size: 0.8rem;
    }
}

/* Улучшенные стили для touch-устройств */
@media (hover: none) and (pointer: coarse) {
    .btn {
        min-height: 48px; /* Минимальная высота для удобного тапа */
    }

    .nav-tab {
        min-height: 44px;
    }

    .kpi-card {
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }

    .leadmagnet-item {
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }

    /* Увеличиваем области тапа для мелких элементов */
    .funnel-stage {
        min-height: 80px;
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }

    /* Touch-friendly analytics elements */
    .overview-card {
        min-height: 120px;
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }

    .insight-card {
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }

    .chart-controls select {
        min-height: 44px;
        padding: 0.75rem;
        font-size: 16px; /* Prevent zoom on iOS */
        touch-action: manipulation;
    }

    .analytics-filters select {
        min-height: 44px;
        padding: 0.75rem;
        font-size: 16px;
        touch-action: manipulation;
    }


    
    /* Улучшаем видимость элементов графиков */
    .analytics-charts .chart-card:hover {
        transform: none; /* Отключаем transform на мобильных */
    }
}

/* Темная тема для устройств с поддержкой */
@media (prefers-color-scheme: dark) {
    :root {
        --primary-bg: #0A0A12;
        --text-primary: #FFFFFF;
    }
}

/* Анимации только для устройств, которые их поддерживают */
@media (prefers-reduced-motion: no-preference) {
    .card {
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

    .btn {
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

    .nav-tab {
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }
}

/* Отключаем анимации для пользователей с настройкой reduce motion */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }

    .logo-icon {
        animation: none;
    }

    .status-badge {
        animation: none;
    }
}

/* Дополнительные исправления viewport для мобильных */
@media screen and (max-width: 480px) {
    html {
        -webkit-text-size-adjust: 100%;
        -ms-text-size-adjust: 100%;
    }
    
    body {
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
        user-select: none;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
    }
    
    * {
        -webkit-touch-callout: none;
        -webkit-tap-highlight-color: transparent;
    }
}

/* Ландшафтная ориентация на мобильных */
@media (orientation: landscape) and (max-height: 500px) {
    body {
        overflow-y: auto !important;
    }
    
    .header {
        padding: 0.375rem 1rem;
    }

    .nav-tabs {
        margin: 0.5rem;
        padding: 0.25rem;
    }

    .nav-tab {
        padding: 0.5rem 0.75rem;
        font-size: 0.75rem;
    }

    .main-content {
        padding: 0 1rem 1rem;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .dashboard-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.75rem;
    }

    .funnel-card {
        grid-column: span 2;
    }

    .card {
        padding: 1rem;
    }

    .kpi-value {
        font-size: 1.75rem;
    }
}