/* 📱 Оптимизация для iPhone 16 Pro Max и других мобильных устройств */

/* Базовые настройки для мобильных */
@media screen and (max-width: 932px) {
    /* Убираем горизонтальный скролл */
    * {
        -webkit-overflow-scrolling: touch;
    }

    html {
        /* Запрещаем зум при двойном тапе */
        touch-action: manipulation;
        /* Оптимизация рендеринга текста */
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }

    body {
        /* Минимальный размер шрифта для читаемости */
        font-size: 16px;
        line-height: 1.5;
        /* Отступы для Safe Area (iPhone) */
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
    }
}

/* Специально для iPhone 16 Pro Max (430px viewport) */
@media screen and (max-width: 430px) {
    /* Dashboard оптимизация */
    .dashboard-container {
        flex-direction: column;
        height: auto;
        min-height: 100vh;
    }

    .sidebar {
        width: 100%;
        height: auto;
        max-height: none;
        position: sticky;
        top: 0;
        z-index: 100;
        padding: 10px 15px;
        border-right: none;
        border-bottom: 2px solid rgba(0, 247, 255, 0.2);
        background: linear-gradient(135deg, #16213e 0%, #0a0e27 100%);
        transition: all 0.3s ease;
    }

    /* Кнопка меню (гамбургер) */
    .menu-toggle {
        display: block;
        position: absolute;
        top: 10px;
        left: 15px;
        background: linear-gradient(135deg, #00f7ff 0%, #00bcd4 100%);
        border: none;
        color: #000;
        font-size: 24px;
        width: 44px;
        height: 44px;
        border-radius: 8px;
        cursor: pointer;
        box-shadow: 0 4px 12px rgba(0, 247, 255, 0.3);
        z-index: 101;
        transition: all 0.3s ease;
        line-height: 1;
        padding: 0;
    }

    .menu-toggle:active {
        transform: scale(0.95);
    }

    /* Контент меню */
    .menu-content {
        display: block;
        transition: all 0.3s ease;
        overflow: hidden;
    }

    /* Когда меню развернуто */
    .sidebar.expanded {
        max-height: 80vh;
        overflow-y: auto;
    }

    /* Скрываем кнопку меню на десктопе */
    @media screen and (min-width: 431px) {
        .menu-toggle {
            display: none;
        }
    }

    .sidebar h2 {
        font-size: 16px;
        margin-bottom: 8px;
        text-align: center;
    }

    /* Компактное горизонтальное меню */
    .sidebar ul {
        display: flex;
        flex-wrap: nowrap;
        gap: 6px;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 5px;
        /* Скрываем скроллбар */
        scrollbar-width: none;
        -ms-overflow-style: none;
    }

    .sidebar ul::-webkit-scrollbar {
        display: none;
    }

    .sidebar ul li {
        flex: 0 0 auto;
        margin-bottom: 0;
        min-width: 80px;
    }

    .sidebar ul li a {
        display: block;
        text-align: center;
        padding: 8px 12px;
        font-size: 13px;
        white-space: nowrap;
        border-radius: 8px;
        min-height: 40px;
        line-height: 1.3;
    }

    /* Submenu - компактно под основным меню */
    .submenu {
        display: flex;
        flex-direction: row;
        padding-left: 0;
        gap: 4px;
        margin-top: 6px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }

    .submenu::-webkit-scrollbar {
        display: none;
    }

    .submenu li {
        flex: 0 0 auto;
        min-width: 120px;
    }

    .submenu li a {
        font-size: 12px;
        padding: 6px 10px;
        min-height: 36px;
    }

    /* Logout кнопка - маленькая иконка в правом верхнем углу */
    .logout-button {
        position: absolute;
        top: 10px;
        right: 10px;
        width: auto;
        margin: 0;
        left: auto;
        z-index: 102;
    }

    .logout-button button {
        width: 36px;
        height: 36px;
        padding: 0;
        font-size: 18px;
        line-height: 1;
        min-height: 36px;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: linear-gradient(135deg, #c62828 0%, #b71c1c 100%);
        opacity: 0.85;
        box-shadow: 0 2px 8px rgba(198, 40, 40, 0.3);
    }

    .logout-button button:active {
        opacity: 1;
        transform: scale(0.95);
    }

    /* Когда меню свернуто - компактный вид */
    .sidebar:not(.expanded) .menu-content {
        max-height: 0;
        opacity: 0;
        padding: 0;
        margin: 0;
        overflow: hidden;
    }

    .sidebar:not(.expanded) {
        padding: 10px 15px;
        min-height: 60px;
    }

    .content {
        padding: 15px;
        height: auto;
        overflow-x: hidden;
        overflow-y: visible;
    }

    /* Контейнеры */
    .container {
        margin: 10px;
        padding: 15px;
        border-radius: 12px;
    }

    /* Заголовки */
    h2 {
        font-size: 22px;
        margin-bottom: 15px;
    }

    h3 {
        font-size: 18px;
        margin-bottom: 12px;
    }

    /* Таблицы - без вложенного скролла на мобильных */
    .ip-table-container {
        max-height: none;
        overflow-y: visible;
        margin-top: 15px;
        border-radius: 12px;
    }

    table {
        font-size: 14px;
    }

    th, td {
        padding: 12px 8px;
        font-size: 13px;
    }

    th {
        font-size: 12px;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    /* Адаптивные таблицы для узких экранов */
    table.mobile-friendly {
        display: block;
        overflow-x: auto;
        white-space: nowrap;
    }

    /* Формы */
    select, input[type="text"], input[type="password"], input[type="email"] {
        font-size: 16px; /* Предотвращаем зум при фокусе на iOS */
        padding: 14px 16px;
        margin: 8px 0;
        border-radius: 12px;
        min-height: 44px;
    }

    .search-box {
        font-size: 16px;
        padding: 14px 18px;
        margin-bottom: 12px;
        border-radius: 12px;
        min-height: 44px;
    }

    /* Кнопки - touch-friendly */
    .btn, button {
        min-height: 44px;
        min-width: 44px;
        padding: 12px 20px;
        font-size: 16px;
        border-radius: 12px;
        /* Убираем highlight при тапе */
        -webkit-tap-highlight-color: transparent;
        /* Плавная анимация */
        transition: all 0.2s ease;
    }

    .btn:active, button:active {
        transform: scale(0.98);
    }

    .btn.delete {
        padding: 12px 16px;
    }

    .btn.add {
        padding: 14px 20px;
        font-size: 17px;
    }

    /* Форма добавления IP */
    .add-form {
        padding: 20px 15px;
        margin-top: 20px;
        border-radius: 12px;
    }

    .add-form h3 {
        font-size: 19px;
        margin-bottom: 15px;
    }

    .add-form input[type="text"] {
        padding: 16px 18px;
        font-size: 16px;
        margin: 8px 0;
    }

    /* Чекбоксы - увеличиваем для touch */
    input[type="checkbox"] {
        width: 22px;
        height: 22px;
        min-width: 22px;
        min-height: 22px;
    }

    /* Server info */
    .server-info-wrapper {
        padding: 12px 0;
    }

    .server-info {
        font-size: 14px;
        flex-direction: column;
        gap: 8px;
        text-align: center;
    }

    .server-info-item {
        padding: 10px 15px;
        margin: 4px 0;
        width: 100%;
        justify-content: center;
    }

    .server-info button {
        margin: 8px 0 0 0;
        width: 100%;
        min-height: 44px;
    }

    /* DC Monitor оптимизация */
    .dc-container {
        padding: 15px;
    }

    .dc-header {
        padding: 15px 20px;
        border-radius: 12px;
        margin-bottom: 20px;
    }

    .dc-header h2 {
        font-size: 20px;
        flex-direction: column;
        gap: 10px;
        text-align: center;
    }

    .status-indicator {
        margin-top: 8px;
    }

    .server-info {
        flex-direction: column;
    }

    .action-panel {
        padding: 18px;
        margin-bottom: 20px;
        border-radius: 12px;
    }

    .ou-grid {
        grid-template-columns: 1fr;
        gap: 15px;
    }

    .ou-block {
        padding: 15px;
        border-radius: 12px;
    }

    .user-item {
        padding: 12px;
        margin: 8px 0;
        border-radius: 10px;
    }

    .user-actions {
        flex-direction: column;
        gap: 8px;
    }

    .user-actions button,
    .user-actions form {
        width: 100%;
    }

    .user-actions button {
        width: 100%;
        min-height: 44px;
    }

    /* Modal оптимизация */
    .modal-content {
        margin: 5% auto;
        padding: 20px;
        width: 95%;
        max-width: 100%;
        border-radius: 16px;
    }

    /* Login page */
    .login-container {
        margin: 20px 15px;
        padding: 30px 20px;
        border-radius: 16px;
        max-width: 100%;
    }

    .login-container h2 {
        font-size: 24px;
    }

    .login-container input {
        padding: 16px 18px;
        font-size: 16px;
        margin-bottom: 15px;
        min-height: 44px;
    }

    .login-container button {
        padding: 16px;
        font-size: 17px;
        min-height: 48px;
    }

    /* Logout page */
    .logout-box {
        padding: 25px 20px;
        margin: 20px 15px;
        border-radius: 16px;
    }

    .logout-box h2 {
        font-size: 24px;
    }

    .logout-box a {
        padding: 14px 24px;
        font-size: 17px;
        min-height: 48px;
        display: block;
        text-align: center;
    }

    /* Оптимизация графиков */
    #graph-section {
        padding: 15px;
        margin-top: 20px;
        border-radius: 12px;
    }

    #graph-section h3 {
        font-size: 18px;
    }

    #download-csv {
        width: 100%;
        margin-bottom: 15px;
        min-height: 44px;
    }

    canvas {
        max-width: 100%;
        height: auto !important;
        border-radius: 12px;
    }

    /* Скрытие элементов на мобильных */
    .desktop-only {
        display: none !important;
    }

    /* Sticky элементы с учетом Safe Area */
    thead {
        position: sticky;
        top: env(safe-area-inset-top, 0);
        z-index: 10;
    }

    /* Оптимизация скроллбаров для iOS */
    ::-webkit-scrollbar {
        display: none; /* Скрываем на iOS */
    }

    /* Message boxes */
    .message-box, .error-box, .success-box, .warning-box {
        padding: 12px 16px;
        margin: 12px 0;
        border-radius: 12px;
        font-size: 15px;
    }

    /* Улучшение читаемости на маленьких экранах */
    .ping-good, .ping-ok, .ping-bad {
        font-size: 13px;
        display: inline-block;
        padding: 4px 8px;
        border-radius: 6px;
    }

    /* Header оптимизация */
    .header {
        margin-bottom: 20px;
    }

    .header form {
        margin: 10px 0;
    }

    .header select {
        width: 100%;
        margin: 10px 0;
    }

    .header button {
        width: 100%;
        min-height: 44px;
        margin: 8px 0;
    }

    /* Button center */
    .button-center {
        margin: 15px 0;
    }

    .button-center button {
        width: 100%;
        padding: 14px 20px;
        font-size: 16px;
    }

    /* Horizontal scroll для широких таблиц */
    .table-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: 15px -15px;
        padding: 0 15px;
    }
}

/* Landscape режим для iPhone */
@media screen and (max-height: 430px) and (orientation: landscape) {
    .sidebar {
        max-height: 200px;
        overflow-y: auto;
    }

    .content {
        max-height: calc(100vh - 220px);
    }

    .ip-table-container {
        max-height: 50vh;
    }
}

/* Dark mode оптимизация для iOS */
@media (prefers-color-scheme: dark) {
    /* Уже темная тема, дополнительных изменений не требуется */
    body {
        color-scheme: dark;
    }
}

/* Анимации для touch устройств */
@media (hover: none) and (pointer: coarse) {
    /* Убираем hover эффекты, оставляем только active */
    .btn:hover,
    button:hover,
    a:hover {
        transform: none;
    }

    .btn:active,
    button:active,
    a:active {
        transform: scale(0.96);
        opacity: 0.9;
    }

    /* Увеличиваем область тапа */
    a, button, .btn, input[type="checkbox"], select {
        position: relative;
    }

    a::after, button::after, .btn::after {
        content: '';
        position: absolute;
        top: -10px;
        left: -10px;
        right: -10px;
        bottom: -10px;
    }
}

/* Улучшение производительности на мобильных */
@media screen and (max-width: 430px) {
    /* GPU acceleration для плавных анимаций */
    .btn, button, a, .sidebar ul li a {
        will-change: transform;
        transform: translateZ(0);
        backface-visibility: hidden;
    }

    /* Оптимизация рендеринга */
    * {
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }
}
