/* ========== МОБИЛЬНАЯ АДАПТАЦИЯ (СОХРАНЕНЫ ВСЕ ПРАВИЛА, ИСПРАВЛЕНА СТРУКТУРА) ========== */
@media (max-width: 768px) {
    /* === ГЛОБАЛЬНЫЕ ФИКСЫ ДЛЯ МОБИЛЬНЫХ === */
    html, body {
        position: fixed;
        overflow: hidden;
        width: 100%;
        height: 100%;
        touch-action: manipulation;
    }

    .app {
        height: 100%;
        width: 100%;
        position: relative;
    }

    /* === ИСПРАВЛЕННЫЙ САЙДБАР === */
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        width: 85%;
        max-width: 300px;
        height: 100vh;
        max-height: 100vh;
        z-index: 150;
        box-shadow: 2px 0 20px rgba(0, 0, 0, 0.5);
        transform: translateX(-100%);
        transition: transform 0.3s ease-in-out;
        border-right: 1px solid var(--border-primary);
        background: var(--bg-secondary);
        visibility: visible;
        display: block !important;
        flex-direction: column;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
    }
    
    .sidebar.open {
        transform: translateX(0);
    }

    .sidebar-overlay {
        display: none;
        opacity: 0;
        transition: opacity 0.3s ease;
        pointer-events: none;
        z-index: 145;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.6);
        backdrop-filter: blur(4px);
        -webkit-backdrop-filter: blur(4px);
    }

    .sidebar-overlay.open {
        display: block;
        opacity: 1;
        pointer-events: auto;
    }

    .chats-list {
        flex: 1;
        overflow-y: visible;
        padding-bottom: 10px;
    }
    
    .sidebar-header {
        flex-shrink: 0;
        position: sticky;
        top: 0;
        z-index: 10;
        background: var(--bg-secondary);
        padding: 9px 10px 8px 60px !important;
    }

    .menu-toggle {
        display: flex;
        position: fixed;
        top: 9px;
        left: 8px;
        width: 42px;
        height: 42px;
        z-index: 160;
        border-radius: 50px;
        background: rgb(50, 70, 100);
        border: none;
        color: white;
        font-size: 24px;
        cursor: pointer;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
        align-items: center;
        justify-content: center;
        -webkit-tap-highlight-color: transparent !important;
        outline: none !important;
        user-select: none !important;
    }

    /* === ИСПРАВЛЕННАЯ ОБЛАСТЬ ЧАТА === */
    .chat-area {
        width: 100%;
        margin-left: 0;
        display: flex;
        flex-direction: column;
        height: 100%;
        position: relative;
    }

    .chat-header {
        padding: 12px 16px;
        padding-left: 60px;
        min-height: 60px;
        height: auto;
        flex-shrink: 0;
        position: relative;
        z-index: 100;
    }

    .back-btn {
        display: flex;
        position: absolute;
        left: 12px;
        top: 12px;
        width: 40px;
        height: 40px;
        background: var(--border-primary);
        border-radius: 50%;
        border: none;
        color: white;
        font-size: 20px;
        cursor: pointer;
        align-items: center;
        justify-content: center;
        z-index: 10;
    }

    /* Контейнер сообщений — динамический отступ снизу */
    .messages-container {
        padding: 12px 10px;
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: calc(114px + env(safe-area-inset-bottom, 0px)) !important;
        background: transparent !important;
        position: relative;
    }

    /* === ФИКСИРОВАННОЕ ПОЛЕ ВВОДА === */
    .input-area {
        position: fixed !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        width: 100% !important;
        z-index: 100 !important;
        background: var(--bg-secondary) !important;
        border-top: 1px solid var(--border-primary) !important;
        padding: 6px 8px !important;
        padding-bottom: max(6px, env(safe-area-inset-bottom)) !important;
        gap: 8px;
    }

    /* === СОХРАНЕНИЕ ВАШЕЙ СТИЛИЗАЦИИ ЭЛЕМЕНТОВ === */
    .channel-header {
        padding: 16px;
    }
    
    .channel-header-info h2 {
        font-size: 1.3rem;
    }
    
    .channel-posts {
        padding: 12px;
    }
    
    .post-item {
        border-radius: 12px;
        padding: 12px;
    }
    
    .create-post-area {
        padding: 12px;
    }
    
    .create-channel-modal .modal-content {
        padding: 20px;
    }
	
    .profile-sidebar {
        display: none;
    }

    .profile-content {
        padding: 20px;
    }

    .profile-card {
        padding: 30px 20px;
    }

    .profile-name {
        font-size: 24px;
    }

    .avatar-wrapper {
        width: 120px;
        height: 120px;
    }

    .avatar-placeholder-large {
        font-size: 48px;
    }

    .info-section {
        padding: 15px;
    }

    .btn-back {
        padding: 10px 20px;
        font-size: 14px;
    }
	
    .typing-indicator {
        font-size: 12px;
        margin-left: 6px;
    }
    
    .typing-dots {
        font-size: 12px;
    }
	
    .opponent-context-menu {
        min-width: 200px !important;
        max-width: 200px !important;
        width: 200px !important;
    }
		
    .opponent-context-menu-item {
        padding: 14px 18px;
        font-size: 15px;
    }
		
    .chat-item.muted::after {
        right: 40px;
    }
		
    .chat-item.blocked-user::after {
        right: 40px;
    }		
	
    .message-status {
        min-width: 20px;
        font-size: 10px;
    }
    
    .message-time {
        min-width: 40px;
        font-size: 11px;
    }
    
    .input-area button,
    .attach-btn,
    .voice-btn,
    .emoji-btn,
    #sendBtn {
        width: 40px !important;
        height: 40px !important;
        min-width: 40px !important;
        min-height: 40px !important;
        font-size: 18px;
    }
    
    #sendBtn {
        font-size: 20px;
    }
    
    .voice-btn svg {
        width: 18px;
        height: 18px;
    }
    
    .audio-player .play-btn {
        width: 48px;
        height: 48px;
    }
    
    .audio-player .play-btn svg {
        width: 22px;
        height: 22px;
    }
    
    .audio-player .waveform-container {
        min-height: 40px;
        padding: 8px 0;
        touch-action: pan-y pinch-zoom;
    }

    .admin-page {
        padding: 0;
        align-items: flex-start;
    }
    
    .admin-container {
        margin: 0;
        border-radius: 0;
        padding: 16px;
        min-height: 100vh;
    }
    
    .admin-stats {
        grid-template-columns: repeat(3, 1fr);
        gap: 8px;
    }
    
    .stat-card {
        padding: 12px;
        gap: 8px;
    }
    
    .stat-value {
        font-size: 20px;
    }
    
    .stat-label {
        font-size: 11px;
    }
    
    .user-actions {
        flex-direction: column;
        gap: 4px;
    }
    
    .admin-badge {
        font-size: 12px;
        padding: 6px 12px;
    }
    
    .admin-name {
        font-size: 16px;
    }
    
    .profile-settings-collapsible .profile-section,
    .admin-page .profile-section {
        padding: 16px;
    }

    .scroll-to-bottom-btn {
        width: 32px;
        height: 32px;
        bottom: 163px;
        right: 10px;
        background: rgba(41, 77, 136, 0.9);
        backdrop-filter: blur(4px);
    }
    
    .scroll-to-bottom-btn svg {
        width: 18px;
        height: 18px;
    }

    .emoji-picker {
        bottom: 80px;
        right: 10px;
        left: 10px;
        width: auto;
        max-width: none;
    }

    .profile-page {
        padding: 0;
        align-items: flex-start;
        height: 100vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .profile-container {
        padding: 15px;
        display: flex;
        align-items: flex-start;
    }
    
    .profile-card {
        padding: 24px 20px;
        width: 100%;
        border-radius: 24px;
    }
    
    .profile-header h1 { font-size: 22px; }
    .profile-avatar { width: 120px; height: 120px; font-size: 44px; }
    
    .avatar-actions { flex-direction: row; width: 100%; }
    .avatar-btn { flex: 1; justify-content: center; }
    
    .input-with-button { flex-direction: column; }
    .input-with-button button { padding: 12px; width: 100%; }
    
    .form-group input { padding: 14px 16px; font-size: 16px; }

    .favorites-modal-content { width: 95%; max-height: 90vh; }
    .favorite-content { padding-left: 0; }
    .favorite-meta { flex-wrap: wrap; }
    .favorite-goto-btn, .favorite-remove-btn { flex: 1; justify-content: center; }
    
    .call-avatar img,
    .call-avatar-placeholder {
        width: 80px;
        height: 80px;
        font-size: 36px;
    }
    
    .background-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }
    
    .background-name {
        padding: 6px 8px;
        font-size: 12px;
    }
    
    .news-header {
        padding: 12px 15px 8px 60px;
    }
    
    .favorite-message-content {
        padding-left: 0;
    }
    
    .favorite-message-header {
        flex-wrap: wrap;
    }
    
    .favorite-remove-btn-icon {
        margin-left: auto;
    }
    
    .favorite-image {
        max-width: 100%;
    }
    
    .favorite-audio-player {
        max-width: 100%;
    }

    .profile-container {
        padding: 8px 8px 63px 8px;
        min-height: 100%;
        align-items: flex-start;
    }
    
    .profile-header {
        padding: 15px 0;
        margin-bottom: 20px;
    }
    
    .profile-header h1 {
        font-size: 22px;
    }
    
    .profile-section {
        padding: 20px;
        margin-bottom: 20px;
        padding-bottom: 20px;
    }
    
    .profile-section h3 {
        font-size: 16px;
        margin-bottom: 15px;
    }

    .profile-avatar {
        width: 90px;
        height: 90px;
    }
    
    .avatar-section {
        flex-direction: column;
        align-items: flex-start;
        gap: 15px;
    }
    
    .auth-page {
        padding: 12px;
    }
    
    .auth-container {
        padding: 8px;
    }

    .create-post-area {
        padding: 8px 10px;
        gap: 6px;
    }
    
    .create-post-textarea {
        font-size: 13px;
        padding: 8px 12px;
        min-height: 38px;
        border-radius: 10px;
    }
    
    .create-post-image-btn,
    .create-post-submit-btn {
        width: 36px;
        height: 36px;
        min-width: 36px;
        border-radius: var(--radius-md);
    }
    
    .create-post-image-btn svg {
        width: 20px;
        height: 20px;
    }
    
    .create-post-submit-btn svg {
        width: 16px;
        height: 16px;
    }
}

/* ========== МАЛЕНЬКИЕ ЭКРАНЫ (<= 480px) ========== */
@media (max-width: 480px) {
    .search-box-messages {
        gap: 2px;
        margin-left: 2px;
    }
    
    .search-box-messages input {
        padding: 4px 6px;
        font-size: 12px;
        min-width: 60px;
    }
    
    .search-box-messages button {
        padding: 2px 4px;
        font-size: 12px;
    }
    
    #searchResultsCount {
        font-size: 10px;
    }

    .profile-modal {
        width: 100vw;
        height: 100vh;
        max-width: 100vw;
        max-height: 100vh;
        border-radius: 0;
    }
    
    .profile-cover {
        padding: 30px 20px 25px;
    }
    
    .profile-avatar {
        width: 80px;
        height: 80px;
    }
    
    .profile-display-name {
        font-size: 20px;
    }
    
    .profile-details {
        padding: 16px;
    }

    .profile-content {
        padding: 10px;
    }

    .profile-card {
        padding: 20px 15px;
    }

    .avatar-wrapper {
        width: 100px;
        height: 100px;
    }

    .avatar-placeholder-large {
        font-size: 40px;
    }

    .info-row {
        flex-direction: column;
        align-items: flex-start;
        gap: 4px;
    }

    .toggle-name-btn {
        width: 32px;
        height: 32px;
        font-size: 14px;
    }
    
    .toggle-pin-btn {
        padding: 8px 14px;
        font-size: 12px;
    }
    
    .user-name-hidden {
        letter-spacing: 3px;
    }

    .profile-container {
        padding: 10px;
    }
    
    .profile-header {
        padding: 10px 0;
        margin-bottom: 15px;
    }
    
    .profile-header h1 {
        font-size: 20px;
    }
    
    .back-btn-profile {
        font-size: 14px;
        padding: 8px 12px;
    }
    
    .profile-section {
        padding: 15px;
        margin-bottom: 15px;
    }
    
    .profile-section h3 {
        font-size: 16px;
        margin-bottom: 15px;
    }
    
    .profile-avatar {
        width: 80px;
        height: 80px;
    }
    
    .avatar-placeholder {
        font-size: 32px;
    }
    
    .form-input {
        padding: 10px;
        font-size: 14px;
    }
    
    .save-btn {
        padding: 12px;
        font-size: 14px;
    }
    
    .remove-btn {
        padding: 12px;
        font-size: 14px;
    }
    
    .btn-upload,
    .btn-remove-avatar {
        padding: 8px 16px;
        font-size: 13px;
    }
    
    .pin-code-input {
        padding: 12px;
        font-size: 18px;
        letter-spacing: 8px;
    }
    
    .success-message,
    .error-message {
        padding: 12px;
        font-size: 13px;
    }
    
    .pin-login-container {
        padding: 8px;
    }
    
    .pin-login-card {
        padding: 24px 20px;
        border-radius: 24px;
    }
    
    .pin-logo {
        width: 60px;
        height: 60px;
        font-size: 28px;
        margin: 16px auto 12px;
    }
    
    h2 {
        font-size: 20px;
    }
    
    .pin-description {
        font-size: 14px;
        margin-bottom: 20px;
    }
    
    .pin-digit {
        width: 55px;
        height: 65px;
        font-size: 24px;
    }
    
    .pin-inputs-container {
        gap: 10px;
    }
    
    .input-group input[type="email"] {
        padding: 14px 18px;
        font-size: 15px;
    }
    
    .pin-btn {
        padding: 14px;
        font-size: 15px;
    }
    
    .user-info-display {
        padding: 14px;
    }
    
    .user-avatar-small {
        width: 45px;
        height: 45px;
        font-size: 18px;
    }
}

/* ========== ОЧЕНЬ МАЛЕНЬКИЕ ЭКРАНЫ (<= 400px) ========== */
@media (max-width: 400px) {
    .auth-card { 
        padding: 20px 16px; 
    }
    
    .message { 
        max-width: 90%; 
        -webkit-touch-callout: none !important;
        -webkit-user-select: none !important;
        -khtml-user-select: none !important;
        -moz-user-select: none !important;
        -ms-user-select: none !important;
        user-select: none !important;
    }
    
    .input-area { 
        gap: 4px; 
        padding: 6px 8px 6px 8px !important;
    }
    
    .input-area button,
    .attach-btn,
    .voice-btn,
    .emoji-btn,
    #sendBtn {
        width: 36px !important;
        height: 36px !important;
        min-width: 36px !important;
        min-height: 36px !important;
        font-size: 16px;
    }
    
    #sendBtn i {
        font-size: 16px;
    }
    
    .reply-bar {
        background: var(--bg-tertiary);
        border-bottom: 1px solid var(--border-primary);
        padding: 8px 12px;
        animation: slideDown 0.2s ease;
        margin-bottom: 0 !important;
        margin-right: 0 !important;
        position: relative;
        z-index: 101;
        width: 100%;
        top: -104px;
    }
    
    .message-image {
        max-width: 200px;
        max-height: 200px;
        border-radius: 10px;
        cursor: pointer;
        margin: 5px 0;
        transition: transform 0.2s;
        -webkit-touch-callout: none !important;
        -webkit-user-select: none !important;
        -khtml-user-select: none !important;
        -moz-user-select: none !important;
        -ms-user-select: none !important;
        user-select: none !important;
        pointer-events: auto;
    }
    
    .message-input-wrapper {
        flex: 1;
        min-width: 0;
        max-height: 300px;
        overflow-y: auto;
        background: var(--bg-primary);
        border: 1px solid var(--border-primary);
        border-radius: 10px;
        padding: 3px 4px;
        align-self: center;
        scrollbar-width: none !important;
        -ms-overflow-style: none !important;
    }
    
    .message .message-text {
        -webkit-user-select: text;
        -moz-user-select: text;
        -ms-user-select: text;
        user-select: text;
    }
    
    .background-grid {
        grid-template-columns: 1fr;
    }
    
    .admin-card {
        padding: 20px 16px;
    }
}

/* ========== МИКРО-ЭКРАНЫ И ЛАНДШАФТ ========== */
@media (max-width: 360px) {
    .pin-digit {
        width: 50px;
        height: 60px;
        font-size: 22px;
    }
    
    .pin-inputs-container {
        gap: 8px;
    }
    
    .pin-login-card {
        padding: 20px 16px;
    }
}

@media (max-width: 320px) {
    .profile-container {
        padding: 5px;
    }
    
    .profile-header h1 {
        font-size: 18px;
    }
    
    .profile-section {
        padding: 12px;
    }
    
    .profile-avatar {
        width: 70px;
        height: 70px;
    }
    
    .avatar-placeholder {
        font-size: 28px;
    }
}

@media (max-height: 700px) {
    .profile-avatar {
        width: 100px;
        height: 100px;
        font-size: 38px;
    }
    
    .profile-section {
        margin-bottom: 20px;
        padding-bottom: 20px;
    }
    
    .section-title {
        margin-bottom: 12px;
    }
    
    .form-group {
        margin-bottom: 12px;
    }
}

@media (max-width: 767px) {
    h3 {
        width: 200px;
    }
}

@media (max-width: 520px) {
    .email-body {
        padding: 28px 22px;
    }
    
    .email-header {
        padding: 24px 20px 18px;
    }
    
    .brand-name {
        font-size: 26px;
    }
    
    .btn-reset {
        padding: 14px 24px;
        font-size: 16px;
        width: 100%;
    }
}