* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    overflow-x: hidden;
    width: 100%;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: linear-gradient(135deg, #0a0a0a 0%, #1a1a1a 50%, #2d2d2d 100%);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    line-height: 1.5;
    overflow-x: hidden;
    width: 100%;
    position: relative;
}

/* Приветственный экран */
.welcome-screen {
    width: 100%;
    max-width: 500px;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(20px);
    border-radius: 24px;
    padding: 40px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.3);
    animation: slideUp 0.5s ease-out;
    border: 1px solid #e0e0e0;
}

.welcome-container {
    text-align: center;
}

.welcome-image {
    width: 150px;
    height: 150px;
    border-radius: 50%;
    object-fit: cover;
    border: 5px solid #FFD700;
    box-shadow: 0 10px 30px rgba(255, 215, 0, 0.3);
    margin-bottom: 30px;
    animation: pulse 2s infinite;
    /* Предотвращаем мерцание при загрузке */
    will-change: transform;
    backface-visibility: hidden;
}

@keyframes pulse {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.05);
    }
    100% {
        transform: scale(1);
    }
}

.welcome-title {
    font-size: 32px;
    font-weight: 700;
    color: #000000;
    margin-bottom: 15px;
}

.welcome-text {
    font-size: 18px;
    color: #333333;
    line-height: 1.6;
    margin-bottom: 30px;
}

.start-chat-btn {
    background: linear-gradient(135deg, #000000 0%, #333333 100%);
    color: #ffffff;
    border: none;
    padding: 15px 35px;
    border-radius: 50px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
}

.start-chat-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.6);
    background: linear-gradient(135deg, #1a1a1a 0%, #444444 100%);
}

/* Основной контейнер чата */
.chat-container {
    width: 100%;
    max-width: 900px;
    height: 85vh;
    max-height: 800px;
    background: #ffffff;
    border-radius: 24px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: fadeIn 0.5s ease-out;
    border: 1px solid #e0e0e0;
}

.hidden {
    display: none !important;
}

/* Заголовок чата */
.chat-header {
    background: linear-gradient(135deg, #000000 0%, #333333 100%);
    color: white;
    padding: 20px 25px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.friend-info {
    display: flex;
    align-items: center;
    gap: 15px;
}

.friend-avatar-small {
    width: 45px;
    height: 45px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid rgba(255, 255, 255, 0.3);
    /* Предотвращаем мерцание при загрузке */
    will-change: transform;
    backface-visibility: hidden;
}

.friend-name {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 3px;
}

.status-indicator {
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 13px;
    opacity: 0.9;
}

.status-dot {
    width: 8px;
    height: 8px;
    background: #4ade80;
    border-radius: 50%;
    animation: pulse 2s infinite;
    box-shadow: 0 0 10px rgba(74, 222, 128, 0.5);
}

.settings-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    color: white;
}

.settings-btn:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* Область сообщений */
.messages-area {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    padding-bottom: 30px;
    background: #f5f5f5;
    display: flex;
    flex-direction: column;
    gap: 8px; /* Уменьшил отступ между сообщениями */
}

.messages-area::-webkit-scrollbar {
    width: 8px;
}

.messages-area::-webkit-scrollbar-track {
    background: #e0e0e0;
    border-radius: 4px;
}

.messages-area::-webkit-scrollbar-thumb {
    background: #999999;
    border-radius: 4px;
}

.messages-area::-webkit-scrollbar-thumb:hover {
    background: #666666;
}

/* Сообщения */
.message {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    animation: slideUp 0.3s ease-out;
    margin-bottom: 8px;
}

.message.user {
    flex-direction: row-reverse;
}

.message-content {
    max-width: 70%;
    display: flex;
    flex-direction: column;
}

.message-avatar {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
    border: 1px solid #e0e0e0;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    /* Предотвращаем мерцание при загрузке */
    will-change: transform;
    backface-visibility: hidden;
}

.message-bubble {
    padding: 12px 18px;
    border-radius: 20px;
    /* Правильные переносы для всех браузеров */
    word-wrap: break-word; /* Для старых браузеров */
    overflow-wrap: break-word; /* Современный стандарт */
    word-break: break-word; /* Разбиваем длинные слова */
    -webkit-hyphens: manual;
    -moz-hyphens: manual;
    -ms-hyphens: manual;
    hyphens: manual;
    line-height: 1.6;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    display: inline-block;
    max-width: 100%;
    min-width: 60px;
    white-space: normal; /* Разрешаем переносы */
    overflow: hidden; /* Скрываем содержимое, выходящее за границы */
}

.message.friend .message-bubble {
    background: #ffffff;
    color: #000000;
    border-bottom-left-radius: 5px;
    border: 1px solid #e0e0e0;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.message.user .message-bubble {
    background: linear-gradient(135deg, #000000 0%, #333333 100%);
    color: #ffffff;
    border-bottom-right-radius: 5px;
}

.message.user .message-content {
    align-items: flex-end;
}

.message-time {
    font-size: 11px;
    color: #999999;
    margin-top: 5px;
}

.message.user .message-time {
    text-align: right;
    color: #666666;
}

/* Изображения в сообщениях */
.message-image {
    max-width: 100%;
    width: auto;
    height: auto;
    max-height: 300px;
    border-radius: 12px;
    margin-top: 8px;
    cursor: pointer;
    transition: transform 0.3s ease;
    object-fit: contain;
    display: block;
}

.message-image:hover {
    transform: scale(1.02);
    box-shadow: 0 5px 20px rgba(0, 0, 0, 0.2);
}

/* Индикатор набора */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 10px;
}

.typing-dots {
    background: #ffffff;
    padding: 12px 18px;
    border-radius: 20px;
    border-bottom-left-radius: 5px;
    display: flex;
    gap: 4px;
    align-items: center;
    border: 1px solid #e0e0e0;
}

.typing-dots span {
    width: 8px;
    height: 8px;
    background: #999999;
    border-radius: 50%;
    animation: typing 1.4s infinite;
}

.typing-dots span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-dots span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typing {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.5;
    }
    30% {
        transform: translateY(-10px);
        opacity: 1;
    }
}

/* Превью изображения */
.image-preview {
    padding: 10px 20px;
    background: #ffffff;
    border-top: 1px solid #e0e0e0;
    display: flex;
    align-items: center;
    gap: 10px;
}

#previewImage {
    width: 60px;
    height: 60px;
    object-fit: cover;
    border-radius: 8px;
    border: 1px solid #e0e0e0;
}

.remove-image-btn {
    background: #e53e3e;
    color: white;
    border: none;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-left: auto;
    font-size: 16px;
    transition: background 0.3s ease;
}

.remove-image-btn:hover {
    background: #c53030;
}

/* Панель ввода */
.input-panel {
    padding: 15px;
    background: #ffffff;
    border-top: 1px solid #e0e0e0;
    height: 90px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.input-container {
    display: flex;
    align-items: center;
    gap: 10px;
    background: #f5f5f5;
    border-radius: 50px;
    padding: 8px 8px 8px 15px;
    border: 1px solid #e0e0e0;
    width: 100%;
    box-sizing: border-box;
}

.message-input {
    flex: 1;
    border: none;
    background: transparent;
    outline: none;
    font-size: 15px;
    padding: 10px;
    color: #000000;
    width: 100%;
    min-width: 0; /* Позволяет flex сжиматься */
}

.message-input::placeholder {
    color: #999999;
}

.attach-btn,
.voice-btn,
.send-btn {
    width: 40px;
    height: 40px;
    min-width: 40px; /* Фиксированная минимальная ширина */
    border-radius: 50%;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    background: transparent;
    color: #666666;
    flex-shrink: 0; /* Не позволяем кнопкам сжиматься */
}

.attach-btn:hover,
.voice-btn:hover {
    background: rgba(0, 0, 0, 0.1);
    color: #000000;
}

.send-btn {
    background: linear-gradient(135deg, #000000 0%, #333333 100%);
    color: #ffffff;
}

.send-btn:hover {
    transform: scale(1.1);
    background: linear-gradient(135deg, #1a1a1a 0%, #444444 100%);
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.3);
}

.voice-btn.recording {
    background: #e53e3e;
    color: white;
    animation: pulse 1s infinite;
    box-shadow: 0 0 20px rgba(229, 62, 62, 0.5);
}


/* Markdown стили */
.markdown-content {
    line-height: 1.6;
}

.markdown-content h1,
.markdown-content h2,
.markdown-content h3,
.markdown-content h4,
.markdown-content h5,
.markdown-content h6 {
    margin-top: 1em;
    margin-bottom: 0.5em;
    font-weight: 600;
}

.markdown-content h1 { font-size: 1.5em; }
.markdown-content h2 { font-size: 1.3em; }
.markdown-content h3 { font-size: 1.1em; }

.markdown-content p {
    margin-bottom: 0.8em;
}

.markdown-content ul,
.markdown-content ol {
    margin-left: 1.5em;
    margin-bottom: 0.8em;
}

.markdown-content li {
    margin-bottom: 0.3em;
}

.markdown-content code {
    background: rgba(0, 0, 0, 0.05);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    font-size: 0.9em;
}

.markdown-content pre {
    background: rgba(0, 0, 0, 0.05);
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin-bottom: 0.8em;
    max-width: 100%;
    word-wrap: break-word;
    white-space: pre-wrap;
}

.markdown-content pre code {
    background: transparent;
    padding: 0;
}

.markdown-content blockquote {
    border-left: 3px solid #FFD700;
    padding-left: 1em;
    margin-left: 0;
    color: #666;
    font-style: italic;
    margin-bottom: 0.8em;
}

.markdown-content strong {
    font-weight: 600;
}

.markdown-content em {
    font-style: italic;
}

.markdown-content a {
    color: #FFD700;
    text-decoration: none;
    border-bottom: 1px solid #FFD700;
}

.markdown-content a:hover {
    opacity: 0.8;
}

/* Markdown стили для сообщений пользователя */
.message.user .markdown-content code {
    background: rgba(255, 255, 255, 0.1);
    color: #FFD700;
}

.message.user .markdown-content pre {
    background: rgba(255, 255, 255, 0.1);
}

.message.user .markdown-content blockquote {
    border-left-color: #FFD700;
    color: rgba(255, 255, 255, 0.8);
}

.markdown-content hr {
    border: none;
    border-top: 1px solid #e0e0e0;
    margin: 1em 0;
}

/* Стриминг анимация */
.message.streaming .message-bubble::after {
    content: '▋';
    animation: blink 1s infinite;
    color: #666;
    margin-left: 2px;
}

@keyframes blink {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0; }
}

/* Анимации */
@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

/* CSS переменные для viewport height */
:root {
    --vh: 1vh;
}

/* Адаптивность */
@media (max-width: 768px) {
    body {
        padding: 0;
        background: linear-gradient(135deg, #0a0a0a 0%, #1a1a1a 50%, #2d2d2d 100%);
        overflow: hidden;
        position: fixed;
        width: 100%;
        height: 100%;
    }
    
    .chat-container {
        height: 100vh;
        height: calc(var(--vh, 1vh) * 100); /* Используем CSS переменную */
        max-height: none;
        border-radius: 0;
        border: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        display: flex;
        flex-direction: column;
    }
    
    .chat-header {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        z-index: 100;
        flex-shrink: 0;
    }
    
    .messages-area {
        position: fixed;
        top: 85px; /* Высота шапки */
        bottom: 90px; /* Высота панели ввода */
        left: 0;
        right: 0;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch; /* Smooth scrolling на iOS */
        padding: 15px;
        padding-bottom: 30px; /* Дополнительный отступ снизу */
    }
    
    .input-panel {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 100;
        padding: 10px;
        padding-bottom: env(safe-area-inset-bottom, 10px); /* Для iPhone с notch */
        background: #ffffff;
        height: 90px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .image-preview {
        position: fixed;
        bottom: 90px;
        left: 0;
        right: 0;
        z-index: 99;
    }
    
    .welcome-screen {
        border-radius: 0;
        height: 100vh;
        height: calc(var(--vh, 1vh) * 100);
        display: flex;
        align-items: center;
        justify-content: center;
        border: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        padding: 20px;
        overflow-y: auto;
        max-width: 100vw;
    }
    
    .message-content {
        max-width: 85%;
    }
    
    /* Исправляем проблему с input на мобильных */
    .message-input {
        font-size: 16px; /* Предотвращаем zoom на iOS при фокусе */
    }
    
    /* Убираем horizontal scroll */
    .chat-container,
    .welcome-screen,
    .messages-area,
    .input-panel {
        max-width: 100vw;
        overflow-x: hidden;
    }
}

/* iOS Safari специфичные стили */
@supports (-webkit-touch-callout: none) {
    .chat-container {
        height: -webkit-fill-available;
    }
    
    .messages-area {
        padding-bottom: 20px;
    }
    
    /* Предотвращаем bounce эффект на iOS */
    .messages-area {
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }
    
    /* Фиксим input на iOS */
    input, textarea {
        -webkit-appearance: none;
        appearance: none;
        -webkit-border-radius: 0;
        border-radius: 0;
    }
}

/* Маленькие экраны (iPhone SE и подобные) */
@media (max-width: 375px) {
    .chat-header {
        padding: 15px 20px;
    }
    
    .friend-name {
        font-size: 16px;
    }
    
    .message-bubble {
        font-size: 14px;
        padding: 10px 14px;
    }
    
    .input-container {
        padding: 6px 6px 6px 12px;
        width: 100%;
    }
    
    .input-panel {
        padding: 8px;
    }
    
    .message-input {
        font-size: 16px;
        padding: 8px;
    }
}
