/* AI Expert System - Стили */

:root {
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --secondary-color: #64748b;
    --success-color: #10b981;
    --danger-color: #ef4444;
    --bg-color: #f8fafc;
    --surface-color: #ffffff;
    --text-color: #1e293b;
    --text-muted: #64748b;
    --border-color: #e2e8f0;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    --radius-sm: 0.375rem;
    --radius-md: 0.5rem;
    --radius-lg: 0.75rem;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    touch-action: manipulation; /* Улучшает отзывчивость на touch */
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    height: 100vh;
    height: 100dvh; /* Динамическая высота для мобильных */
    display: flex;
    flex-direction: column;
    background: var(--surface-color);
    box-shadow: var(--shadow-lg);
    overflow: hidden; /* Предотвращает прокрутку всего контейнера */
    position: relative;
}

/* На мобильных используем visualViewport для правильной высоты */
@supports (height: 100dvh) {
    @media (max-width: 768px) {
        .container {
            height: 100dvh;
            max-height: 100dvh;
        }
    }
}

/* Шапка */
.header {
    background: linear-gradient(135deg, var(--primary-color), var(--primary-hover));
    color: white;
    padding: 1.5rem 2rem;
    box-shadow: var(--shadow-md);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
}

.header-content h1 {
    font-size: 1.75rem;
    font-weight: 700;
    margin-bottom: 0.25rem;
}

.subtitle {
    font-size: 0.95rem;
    opacity: 0.9;
}

.header-actions {
    display: flex;
    gap: 0.75rem;
}

/* Кнопки */
.btn {
    padding: 0.625rem 1.25rem;
    border: none;
    border-radius: var(--radius-md);
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    -webkit-tap-highlight-color: transparent; /* Убираем подсветку при тапе на мобильных */
    touch-action: manipulation; /* Улучшает отзывчивость */
    user-select: none;
}

/* По умолчанию показываем текст кнопки, скрываем иконку */
.send-btn-icon {
    display: none;
}

.send-btn-text {
    display: inline-block;
}

.btn:hover {
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

/* На touch-устройствах hover эффекты только при активном нажатии */
@media (hover: none) and (pointer: coarse) {
    .btn:active {
        transform: translateY(0);
        opacity: 0.8;
    }
}

.btn:active {
    transform: translateY(0);
}

.btn-primary {
    background-color: var(--primary-color);
    color: white;
}

.btn-primary:hover {
    background-color: var(--primary-hover);
}

.btn-secondary {
    background-color: white;
    color: var(--text-color);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover {
    background-color: var(--bg-color);
}

.btn-info {
    background-color: rgba(255, 255, 255, 0.2);
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.3);
}

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

/* Мобильные кнопки переключения режима - скрыты по умолчанию */
.mode-switcher-mobile {
    display: none;
}

/* Чат */
.chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 2rem;
    background: var(--bg-color);
}

.messages {
    max-width: 800px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.message {
    display: flex;
    gap: 1rem;
    animation: fadeIn 0.3s ease-in;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    flex-shrink: 0;
    background: var(--surface-color);
    box-shadow: var(--shadow-sm);
}

.message.user .message-avatar {
    order: 2;
}

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

.message-content {
    flex: 1;
    max-width: 70%;
}

.message.user .message-content {
    display: flex;
    justify-content: flex-end;
}

.message-text {
    background: var(--surface-color);
    padding: 1rem 1.25rem;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border-color);
    word-wrap: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
}

.message.user .message-text {
    background: var(--primary-color);
    color: white;
    border: none;
}

.message-text p {
    margin-bottom: 0.75rem;
}

.message-text p:last-child {
    margin-bottom: 0;
}

.message-text ul, .message-text ol {
    margin-left: 1.5rem;
    margin-bottom: 0.75rem;
}

.message-text li {
    margin-bottom: 0.25rem;
}

.message-text strong {
    font-weight: 600;
}

.message-text code {
    background: var(--bg-color);
    padding: 0.125rem 0.375rem;
    border-radius: var(--radius-sm);
    font-family: 'Courier New', monospace;
    font-size: 0.9em;
}

.message.user .message-text code {
    background: rgba(255, 255, 255, 0.2);
}

/* Индикатор печати */
.typing-indicator {
    max-width: 800px;
    margin: 0 auto;
    display: flex;
    gap: 1rem;
    padding: 0 2rem;
}

.typing-dots {
    background: var(--surface-color);
    padding: 1rem 1.5rem;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border-color);
    display: flex;
    gap: 0.5rem;
}

.typing-dots span {
    width: 8px;
    height: 8px;
    background: var(--text-muted);
    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% {
        opacity: 0.3;
        transform: scale(0.8);
    }
    30% {
        opacity: 1;
        transform: scale(1);
    }
}

/* Форма ввода */
.input-container {
    background: var(--surface-color);
    padding: 1.5rem 2rem;
    border-top: 1px solid var(--border-color);
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
    flex-shrink: 0;
    position: relative;
    z-index: 10;
}

.input-wrapper,
form.input-wrapper,
#chatForm {
    max-width: 800px;
    margin: 0 auto;
    display: flex;
    gap: 1rem;
    align-items: flex-end;
    width: 100%;
}

#userInput {
    flex: 1;
    padding: 0.875rem 1.25rem;
    border: 2px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: 1rem;
    font-family: inherit;
    resize: none; /* Отключаем ручное изменение размера */
    min-height: 44px; /* Высота одной строки */
    max-height: 120px; /* Максимум ~3 строки, дальше скролл */
    height: 44px; /* Начальная высота одной строки */
    transition: border-color 0.2s, height 0.1s ease-out;
    line-height: 1.5;
    overflow-y: auto; /* Вертикальный скролл при превышении max-height */
    -webkit-appearance: none; /* Убираем стили iOS */
    appearance: none;
    touch-action: manipulation;
}

#userInput:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

/* Улучшение для мобильных клавиатур */
@media (max-width: 768px) {
    #userInput:focus {
        box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.2);
    }
}

#sendBtn {
    padding: 1rem 2rem;
    white-space: nowrap;
    height: fit-content;
    align-self: flex-end;
    font-size: 1rem;
}

.input-hint {
    max-width: 800px;
    margin: 0.75rem auto 0;
    font-size: 0.85rem;
    color: var(--text-muted);
    text-align: center;
}

/* Модальное окно */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    animation: fadeIn 0.2s;
}

.modal-content {
    background-color: var(--surface-color);
    margin: 10% auto;
    padding: 2rem;
    border-radius: var(--radius-lg);
    max-width: 500px;
    box-shadow: var(--shadow-lg);
    position: relative;
    animation: slideDown 0.3s;
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-50px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.modal-close {
    position: absolute;
    right: 1.5rem;
    top: 1.5rem;
    font-size: 2rem;
    font-weight: 300;
    color: var(--text-muted);
    cursor: pointer;
    line-height: 1;
    transition: color 0.2s;
}

.modal-close:hover {
    color: var(--text-color);
}

.modal-content h2 {
    margin-bottom: 1.5rem;
    color: var(--text-color);
}

.stats-item {
    display: flex;
    justify-content: space-between;
    padding: 1rem;
    border-bottom: 1px solid var(--border-color);
}

.stats-item:last-child {
    border-bottom: none;
}

.stats-label {
    font-weight: 600;
    color: var(--text-color);
}

.stats-value {
    color: var(--primary-color);
    font-weight: 500;
}

/* Скроллбар */
.chat-container::-webkit-scrollbar {
    width: 8px;
}

.chat-container::-webkit-scrollbar-track {
    background: var(--bg-color);
}

.chat-container::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: var(--radius-sm);
}

.chat-container::-webkit-scrollbar-thumb:hover {
    background: var(--secondary-color);
}

/* На мобильных скроллбар тоньше или скрыт */
@media (max-width: 768px) {
    .chat-container::-webkit-scrollbar {
        width: 4px;
    }
}

/* Адаптивность для мобильных */
@media (max-width: 768px) {
    .container {
        height: 100vh;
        height: 100dvh; /* Динамическая высота для мобильных */
    }

    .header {
        padding: 0.5rem 1rem;
        padding-top: max(0.5rem, env(safe-area-inset-top));
        padding-left: max(1rem, env(safe-area-inset-left));
        padding-right: max(1rem, env(safe-area-inset-right));
        flex-direction: column;
        align-items: flex-start;
        gap: 0.25rem;
        min-height: auto;
    }

    .header-content {
        width: 100%;
    }

    .header-content h1 {
        font-size: 1.1rem;
        margin-bottom: 0;
        line-height: 1.2;
    }

    .subtitle {
        font-size: 0.85rem;
        display: none !important; /* Скрываем подзаголовок на мобильных */
    }

    .header-actions {
        width: 100%;
        flex-wrap: wrap;
        gap: 0.5rem;
        display: none !important; /* Полностью скрываем все кнопки в хедере на мобильных */
    }

    .mode-switcher {
        display: none !important; /* Скрываем кнопки переключения режима в хедере на мобильных */
    }
    
    /* Мобильные кнопки переключения режима (внутри chat-container, прокручиваются вместе с контентом) */
    .mode-switcher-mobile {
        display: flex;
        gap: 0.5rem;
        padding: 0.75rem 1rem;
        background: var(--surface-color);
        border-bottom: 1px solid var(--border-color);
        margin-bottom: 0.5rem;
        margin-top: -2rem; /* Компенсируем padding контейнера чата */
    }
    
    .mode-switcher-mobile .btn-mode {
        flex: 1;
        padding: 0.625rem 0.875rem;
        font-size: 0.875rem;
        text-align: center;
    }

    .btn-mode {
        flex: 1;
        padding: 0.625rem 0.875rem;
        font-size: 0.875rem;
        text-align: center;
    }

    .btn {
        padding: 0.625rem 1rem;
        font-size: 0.875rem;
        flex: 1;
        min-width: auto;
    }

    /* Скрываем кнопки Статистика и Очистить на мобильных */
    #statsBtn,
    #clearBtn {
        display: none !important;
    }

    .chat-container {
        padding: 1rem 0.75rem;
        padding-bottom: 0.5rem;
        padding-left: max(0.75rem, env(safe-area-inset-left));
        padding-right: max(0.75rem, env(safe-area-inset-right));
        -webkit-overflow-scrolling: touch; /* Плавная прокрутка на iOS */
        /* Предотвращаем прокрутку вверх при фокусе на поле ввода */
        overscroll-behavior: contain;
    }

    .messages {
        gap: 1rem;
    }

    .message {
        gap: 0.75rem;
    }

    .message-avatar {
        width: 32px;
        height: 32px;
        font-size: 1.25rem;
    }

    .message-content {
        max-width: 85%;
    }

    .message-text {
        padding: 0.875rem 1rem;
        font-size: 0.95rem;
        line-height: 1.5;
    }

    .message-text h2, .message-text h3, .message-text h4 {
        font-size: 1.1em;
        margin-top: 0.75rem;
        margin-bottom: 0.5rem;
    }

    .message-text ul, .message-text ol {
        margin-left: 1.25rem;
    }

    .typing-indicator {
        padding: 0 0.75rem;
    }

    .input-container {
        padding: 1rem 0.75rem;
        padding-bottom: max(1rem, env(safe-area-inset-bottom));
        padding-left: max(0.75rem, env(safe-area-inset-left));
        padding-right: max(0.75rem, env(safe-area-inset-right));
        position: sticky;
        bottom: 0;
        background: var(--surface-color);
        z-index: 10;
        box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
    }

    .input-wrapper,
    form.input-wrapper {
        flex-direction: row !important; /* Горизонтальное расположение на мобильных */
        gap: 0.5rem;
        align-items: flex-end; /* Выравнивание по нижнему краю */
        width: 100%;
    }

    #userInput {
        flex: 1; /* Занимает доступное пространство */
        min-height: 44px; /* Высота одной строки */
        max-height: 120px; /* Максимум ~3 строки */
        height: 44px; /* Начальная высота */
        font-size: 16px; /* Предотвращает зум на iOS */
        padding: 0.875rem 1rem;
        border-radius: 12px;
        overflow-y: auto; /* Скролл при превышении max-height */
    }

    #sendBtn {
        width: auto !important;
        min-width: 44px; /* Квадратная кнопка */
        height: 44px; /* Такая же высота как поле ввода */
        padding: 0.5rem;
        font-size: 1.25rem;
        justify-content: center;
        align-items: center;
        flex: none !important;
        border-radius: 12px;
    }
    
    /* Скрываем текст кнопки на мобильных, показываем только иконку */
    #sendBtn .send-btn-text {
        display: none;
    }
    
    #sendBtn .send-btn-icon {
        display: inline-block;
    }

    .modal-content {
        margin: 5% auto;
        padding: 1.5rem;
        max-width: 90%;
        max-height: 90vh;
        overflow-y: auto;
    }

    .modal-close {
        right: 1rem;
        top: 1rem;
        font-size: 1.75rem;
    }
}

/* Планшеты в портретной ориентации */
@media (min-width: 481px) and (max-width: 768px) {
    .header {
        flex-direction: row;
        align-items: center;
    }

    .mode-switcher {
        width: auto;
        margin-bottom: 0;
        display: flex; /* Показываем кнопки в хедере на планшетах */
    }
    
    .mode-switcher-mobile {
        display: none; /* Скрываем мобильные кнопки на планшетах */
    }

    .input-wrapper {
        flex-direction: row;
        align-items: flex-end;
    }

    #sendBtn {
        width: auto;
        min-width: 44px;
        height: 44px;
        padding: 0.5rem;
        flex: 0 0 auto;
    }
    
    /* На планшетах тоже показываем только иконку */
    #sendBtn .send-btn-text {
        display: none;
    }
    
    #sendBtn .send-btn-icon {
        display: inline-block;
    }
}

/* Очень маленькие экраны */
@media (max-width: 480px) {
    .header {
        padding: 0.5rem 0.875rem;
        padding-top: max(0.5rem, env(safe-area-inset-top));
        padding-left: max(0.875rem, env(safe-area-inset-left));
        padding-right: max(0.875rem, env(safe-area-inset-right));
        gap: 0.25rem;
    }
    
    .header-content h1 {
        font-size: 1rem;
        margin-bottom: 0;
        line-height: 1.2;
    }
    
    .header-actions {
        display: none !important; /* Скрываем все кнопки в хедере на очень маленьких экранах */
    }

    .header-content h1 {
        font-size: 1.125rem;
    }

    .subtitle {
        font-size: 0.8rem;
        display: none !important; /* Скрываем подзаголовок на очень маленьких экранах */
    }

    .btn-mode {
        padding: 0.5rem 0.75rem;
        font-size: 0.8rem;
    }

    .btn {
        padding: 0.5rem 0.875rem;
        font-size: 0.8rem;
    }

    .chat-container {
        padding: 0.75rem 0.5rem;
        padding-left: max(0.5rem, env(safe-area-inset-left));
        padding-right: max(0.5rem, env(safe-area-inset-right));
    }

    .message-text {
        padding: 0.75rem 0.875rem;
        font-size: 0.9rem;
    }

    .input-container {
        padding: 0.875rem 0.5rem;
        padding-bottom: max(0.875rem, env(safe-area-inset-bottom));
    }

    #userInput {
        min-height: 44px;
        max-height: 100px;
        height: 44px;
        padding: 0.875rem;
        overflow-y: auto;
    }

    #sendBtn {
        min-width: 44px;
        height: 44px;
        padding: 0.5rem;
    }
    
    #sendBtn .send-btn-text {
        display: none;
    }
    
    #sendBtn .send-btn-icon {
        display: inline-block;
    }
}

/* Ландшафтная ориентация на мобильных */
@media (max-width: 768px) and (orientation: landscape) {
    .header {
        padding: 0.75rem 1rem;
        flex-direction: row;
        align-items: center;
    }

    .header-content h1 {
        font-size: 1.125rem;
    }

    .mode-switcher {
        width: auto;
        margin-bottom: 0;
    }

    .chat-container {
        padding: 0.75rem;
    }

    #userInput {
        min-height: 60px;
        max-height: 120px;
    }
}

/* Улучшения для touch-устройств */
* {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
}

textarea, input {
    -webkit-tap-highlight-color: transparent;
}

/* Предотвращение выделения текста при двойном тапе на мобильных */
@media (max-width: 768px) {
    .message-text {
        -webkit-user-select: text;
        user-select: text;
    }

    .btn, .message-avatar {
        -webkit-user-select: none;
        user-select: none;
    }
}

/* Утилиты */
.hidden {
    display: none !important;
}

.loading {
    opacity: 0.6;
    pointer-events: none;
}



