* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: #000000;
    min-height: 100vh;
    padding: 0;
    color: #ffffff;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    /* Исправление для мобильных - предотвращение блокировки touch событий */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    /* Убеждаемся что body не блокирует события */
    pointer-events: auto;
}

/* Глобальные исправления для всех интерактивных элементов на мобильных */
button, input, textarea, select, label, a {
    pointer-events: auto !important;
    touch-action: manipulation;
    -webkit-tap-highlight-color: rgba(255, 255, 255, 0.1);
}

/* Убеждаемся что все кнопки кликабельны */
button:not(:disabled) {
    cursor: pointer;
    -webkit-user-select: none;
    user-select: none;
}

.container {
    width: 100%;
    max-width: 1400px;
    margin: 0 auto;
    background: #0a0a0a;
    border-radius: 0;
    overflow: hidden;
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 0 0;
}

header {
    background: #000000;
    color: #ffffff;
    padding: 20px 20px 15px 20px;
    text-align: center;
    border-bottom: 1px solid #1a1a1a;
    padding-top: env(safe-area-inset-top, 20px);
}

header h1 {
    font-size: clamp(1.2em, 4vw, 1.8em);
    margin-bottom: 8px;
    font-weight: 600;
    letter-spacing: -0.5px;
}

.subtitle {
    font-size: clamp(0.75em, 2.5vw, 0.95em);
    opacity: 0.7;
    font-style: italic;
    letter-spacing: 0.5px;
}

main {
    flex: 1;
    display: flex;
    flex-direction: row;
    overflow: hidden;
    position: relative;
}

.sidebar-toggle {
    display: none;
    position: fixed;
    top: 10px;
    left: 10px;
    z-index: 1001;
    background: #1a1a1a;
    border: 1px solid #2a2a2a;
    color: #ffffff;
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 1.2em;
    transition: all 0.2s ease;
}

.sidebar-toggle:hover {
    background: #2a2a2a;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 998;
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none; /* Не блокирует клики когда не активен */
}

.sidebar-overlay.active {
    opacity: 1;
    pointer-events: auto; /* Блокирует клики только когда активен */
}

.chats-sidebar {
    width: clamp(200px, 20vw, 280px);
    min-width: 200px;
    background: #0a0a0a;
    border-right: 1px solid #1a1a1a;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: transform 0.3s ease;
    position: relative;
    z-index: 1000;
}

.new-chat-btn {
    padding: 15px 20px;
    background: #1a1a1a;
    color: #ffffff;
    border: none;
    border-bottom: 1px solid #2a2a2a;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1em;
    font-weight: 500;
    transition: background 0.2s ease;
}

.new-chat-btn:hover {
    background: #2a2a2a;
}

.new-chat-btn:active {
    background: #151515;
}

.new-chat-btn span:first-child {
    font-size: 1.5em;
    font-weight: 300;
    flex-shrink: 0;
}

.new-chat-btn .btn-text {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chats-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 5px;
    -webkit-overflow-scrolling: touch;
}

.chat-item {
    padding: 12px 15px;
    background: #1a1a1a;
    border: 1px solid #2a2a2a;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.chat-item:hover {
    background: #2a2a2a;
    border-color: #3a3a3a;
}

.chat-item.active {
    background: #2a2a2a;
    border-color: #ffffff;
}

.chat-item-name {
    flex: 1;
    color: #e0e0e0;
    font-size: clamp(0.8em, 2vw, 0.9em);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}

.chat-item-delete {
    background: transparent;
    border: none;
    color: #666666;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 1.2em;
    transition: all 0.2s ease;
    opacity: 0;
}

.chat-item:hover .chat-item-delete {
    opacity: 1;
}

.chat-item-delete:hover {
    color: #ff3333;
    background: rgba(255, 51, 51, 0.1);
}

.chats-list::-webkit-scrollbar {
    width: 6px;
}

.chats-list::-webkit-scrollbar-track {
    background: #0a0a0a;
}

.chats-list::-webkit-scrollbar-thumb {
    background: #2a2a2a;
    border-radius: 10px;
}

.chats-list::-webkit-scrollbar-thumb:hover {
    background: #3a3a3a;
}

.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    padding: clamp(12px, 2vw, 15px);
    padding-bottom: calc(clamp(12px, 2vw, 15px) + env(safe-area-inset-bottom, 0px));
    overflow: hidden;
    min-width: 0; /* Prevents flex overflow */
    position: relative;
    z-index: 1; /* Выше overlay когда не активен */
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: clamp(12px, 2vw, 15px);
    background: #0f0f0f;
    border-radius: 12px;
    margin-bottom: clamp(12px, 2vw, 15px);
    display: flex;
    flex-direction: column;
    gap: clamp(10px, 1.5vw, 12px);
    -webkit-overflow-scrolling: touch;
    min-height: 0; /* Allows flex shrinking */
}

.welcome-message {
    background: #1a1a1a;
    padding: clamp(15px, 3vw, 20px);
    border-radius: 12px;
    border: 1px solid #2a2a2a;
    line-height: 1.7;
    color: #e0e0e0;
    font-size: clamp(0.9em, 2.5vw, 1em);
}

.welcome-message ul {
    margin: 15px 0;
    padding-left: 25px;
}

.welcome-message li {
    margin: 8px 0;
}

.message {
    padding: clamp(12px, 2vw, 15px) clamp(16px, 3vw, 20px);
    border-radius: 15px;
    max-width: min(85%, 600px);
    word-wrap: break-word;
    word-break: break-word;
    line-height: 1.6;
    animation: fadeIn 0.3s ease-in;
    hyphens: auto;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message.user {
    background: #1a1a1a;
    color: #ffffff;
    align-self: flex-end;
    margin-left: auto;
    border: 1px solid #2a2a2a;
}

.message.assistant {
    background: #151515;
    border: 1px solid #2a2a2a;
    color: #e0e0e0;
    align-self: flex-start;
}

.message-image {
    max-width: 100%;
    border-radius: 10px;
    margin-bottom: 10px;
    max-height: 300px;
    object-fit: contain;
}

.message-content {
    white-space: pre-wrap;
    line-height: 1.8;
}

.message-content p {
    margin: 12px 0;
}

.message-content code {
    background: #2a2a2a;
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    font-size: 0.9em;
    color: #ffffff;
}

.message-content pre {
    background: #1a1a1a;
    padding: 15px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 10px 0;
    border: 1px solid #2a2a2a;
    color: #e0e0e0;
}

/* Стили для пошаговых решений - простое и чистое */
.message-content ol,
.message-content ul {
    margin: 12px 0;
    padding-left: 20px;
}

.message-content li {
    margin: 8px 0;
    line-height: 1.7;
}

.message-content strong {
    color: #ffffff;
    font-weight: 600;
}

.message-content em {
    font-style: italic;
    color: #d0d0d0;
}

/* Блоки с решениями уравнений - минималистичный стиль */
.message-content .solution-step {
    margin: 10px 0;
    padding: 8px 0;
    line-height: 1.8;
}

.message-content .solution-step strong {
    color: #ffffff;
    font-weight: 600;
    display: block;
    margin-bottom: 4px;
}

/* Блоки с ответами - простое выделение */
.message-content .answer-simple {
    margin: 15px 0;
    padding: 10px 0;
    border-top: 1px solid #3a3a3a;
    border-bottom: 1px solid #3a3a3a;
}

.message-content .answer-simple strong {
    color: #ffffff;
    font-weight: 600;
    margin-right: 8px;
}

/* Блоки с уравнениями - центрированные и чистые */
.message-content .equation-simple {
    margin: 12px 0;
    padding: 10px 0;
    text-align: center;
    font-size: 1.1em;
}

/* Улучшение отображения формул в простых блоках */
.message-content .equation-simple mjx-container {
    display: inline-block !important;
    margin: 0 4px !important;
    vertical-align: middle !important;
}

/* Выделение важных частей */
.message-content mark {
    background: #3a3a3a;
    color: #ffffff;
    padding: 2px 6px;
    border-radius: 4px;
}

.loading {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 3px solid rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    border-top-color: #ffffff;
    animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.input-area {
    display: flex;
    flex-direction: column;
    gap: clamp(10px, 2vw, 15px);
    flex-shrink: 0;
    position: relative;
    z-index: 100; /* Высокий z-index для интерактивных элементов */
}

.image-upload-section {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.upload-btn {
    display: inline-flex;
    align-items: center;
    gap: clamp(8px, 1.5vw, 10px);
    padding: clamp(10px, 2vw, 12px) clamp(15px, 3vw, 20px);
    background: #1a1a1a;
    border: 2px dashed #3a3a3a;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: clamp(0.85em, 2.5vw, 0.95em);
    color: #ffffff;
    font-weight: 500;
    white-space: nowrap;
    position: relative;
    z-index: 101;
    pointer-events: auto !important;
    touch-action: manipulation;
    -webkit-tap-highlight-color: rgba(255, 255, 255, 0.1);
}

.upload-btn:hover {
    background: #2a2a2a;
    border-color: #4a4a4a;
}

.upload-btn:active {
    background: #151515;
}

.image-preview {
    display: none;
    margin-top: 10px;
}

.image-preview.active {
    display: block;
}

.image-preview img {
    max-width: 200px;
    max-height: 200px;
    border-radius: 10px;
    border: 2px solid #2a2a2a;
    object-fit: cover;
}

.image-preview .remove-image {
    margin-left: 10px;
    padding: 5px 10px;
    background: #ff3333;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 0.9em;
}

.input-group {
    display: flex;
    gap: 10px;
    align-items: flex-end;
    position: relative;
    z-index: 101; /* Еще выше для группы ввода */
}

.input-group textarea {
    flex: 1;
    min-width: 0;
}

#messageInput {
    flex: 1;
    padding: clamp(12px, 2vw, 15px);
    border: 1px solid #2a2a2a;
    border-radius: 12px;
    font-size: clamp(14px, 2.5vw, 16px);
    font-family: inherit;
    resize: none;
    transition: border-color 0.3s ease;
    background: #1a1a1a;
    color: #ffffff;
    min-width: 0;
    width: 100%;
    /* Исправление для мобильных устройств */
    -webkit-user-select: text;
    user-select: text;
    pointer-events: auto !important;
    touch-action: manipulation;
    -webkit-tap-highlight-color: rgba(255, 255, 255, 0.1);
    z-index: 102;
    position: relative;
}

#messageInput::placeholder {
    color: #666666;
}

#messageInput:focus {
    outline: none;
    border-color: #4a4a4a;
    background: #151515;
}

.send-btn {
    padding: clamp(12px, 2vw, 15px) clamp(20px, 4vw, 30px);
    background: #ffffff;
    color: #000000;
    border: none;
    border-radius: 12px;
    font-size: clamp(0.9em, 2.5vw, 1em);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: clamp(6px, 1.5vw, 8px);
    white-space: nowrap;
    flex-shrink: 0;
    position: relative;
    z-index: 103;
    pointer-events: auto !important;
    touch-action: manipulation;
    -webkit-tap-highlight-color: rgba(255, 255, 255, 0.2);
}

.send-btn:hover:not(:disabled) {
    background: #e0e0e0;
    transform: translateY(-1px);
}

.send-btn:active:not(:disabled) {
    background: #cccccc;
    transform: translateY(0);
}

.send-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    background: #2a2a2a;
    color: #666666;
}

.model-selector {
    display: flex;
    align-items: center;
    gap: clamp(8px, 2vw, 10px);
    font-size: clamp(0.8em, 2.5vw, 0.9em);
}

.model-selector label {
    font-weight: 500;
    color: #999999;
    white-space: nowrap;
    flex-shrink: 0;
}

.model-selector select {
    padding: clamp(6px, 1.5vw, 8px) clamp(10px, 2vw, 12px);
    border: 1px solid #2a2a2a;
    border-radius: 8px;
    font-size: clamp(0.8em, 2.5vw, 0.9em);
    cursor: pointer;
    background: #1a1a1a;
    color: #ffffff;
    transition: border-color 0.3s ease;
    flex: 1;
    min-width: 0;
    position: relative;
    z-index: 101;
    pointer-events: auto !important;
    touch-action: manipulation;
}

.model-selector select:focus {
    outline: none;
    border-color: #4a4a4a;
    background: #151515;
}

/* Math equation styling - простое и чистое отображение */
.MathJax {
    font-size: 1.1em !important;
    color: #ffffff !important;
}

.MathJax_SVG,
.MathJax_SVG_Display {
    color: #ffffff !important;
    fill: #ffffff !important;
}

/* Inline math - простые формулы в тексте */
mjx-container:not([display="true"]) {
    color: #ffffff !important;
    display: inline !important;
    margin: 0 2px !important;
    vertical-align: middle !important;
}

/* Display math - центрированные формулы */
mjx-container[display="true"] {
    color: #ffffff !important;
    display: block !important;
    margin: 12px auto !important;
    text-align: center !important;
}

/* Улучшение читаемости формул */
.MathJax .mo,
.MathJax .mi,
.MathJax .mn {
    color: #ffffff !important;
}

.MathJax .mfrac {
    border-color: #ffffff !important;
}

/* Простые уравнения в блоках */
.message-content .equation-simple mjx-container {
    color: #ffffff !important;
    display: inline-block !important;
    margin: 0 3px !important;
}

/* Scrollbar styling */
.chat-messages::-webkit-scrollbar {
    width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
    background: #1a1a1a;
    border-radius: 10px;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: #3a3a3a;
    border-radius: 10px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: #4a4a4a;
}

/* Responsive design - Tablets and smaller */
@media (max-width: 768px) {
    .sidebar-toggle {
        display: block;
    }

    .chats-sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: 280px;
        max-width: 85vw;
        transform: translateX(-100%);
        box-shadow: 2px 0 10px rgba(0, 0, 0, 0.5);
    }

    .chats-sidebar.open {
        transform: translateX(0);
    }

    .sidebar-overlay {
        display: block;
        pointer-events: none; /* Не блокирует когда не активен */
    }

    .sidebar-overlay.active {
        display: block;
        pointer-events: auto; /* Блокирует только когда активен */
    }

    .new-chat-btn .btn-text {
        display: block;
    }

    header {
        padding-left: 50px;
    }
}

/* Responsive design - iPhone 13 and similar */
@media (max-width: 428px) {
    body {
        padding: 0;
    }

    .container {
        height: 100vh;
        height: 100dvh; /* Dynamic viewport height for mobile */
        border-radius: 0;
    }

    .sidebar-toggle {
        top: calc(env(safe-area-inset-top, 0px) + 10px);
        padding: 8px 10px;
        font-size: 1em;
    }

    .chats-sidebar {
        width: 100%;
        max-width: 100vw;
    }

    .new-chat-btn .btn-text {
        display: inline;
    }

    header {
        padding: 15px 20px 12px 50px;
        padding-top: calc(env(safe-area-inset-top, 0px) + 15px);
    }

    header {
        padding: 15px 20px 12px 20px;
        padding-top: calc(env(safe-area-inset-top, 0px) + 15px);
    }

    header h1 {
        font-size: clamp(1.2em, 5vw, 1.5em);
        margin-bottom: 6px;
    }

    .subtitle {
        font-size: clamp(0.7em, 3vw, 0.85em);
    }

    .chat-container {
        padding: 12px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    }

    .chat-messages {
        padding: 12px;
        margin-bottom: 12px;
        gap: 10px;
    }

    .welcome-message {
        padding: 15px;
        font-size: 0.95em;
    }

    .message {
        max-width: 90%;
        padding: 12px 16px;
        font-size: 0.95em;
    }

    .input-area {
        gap: 12px;
    }

    .upload-btn {
        padding: 10px 16px;
        font-size: 0.9em;
        z-index: 200;
        position: relative;
        pointer-events: auto !important;
        touch-action: manipulation;
    }

    .input-group {
        flex-direction: row;
        gap: 10px;
    }

    #messageInput {
        flex: 1;
        padding: 12px;
        font-size: 16px; /* Prevents zoom on iOS */
        min-height: 44px; /* iOS touch target size */
        min-width: 0; /* Allows flex to work properly */
        width: auto; /* Auto width for proper input */
        -webkit-appearance: none;
        appearance: none;
        -webkit-user-select: text;
        user-select: text;
        pointer-events: auto !important;
        touch-action: manipulation;
        -webkit-tap-highlight-color: rgba(255, 255, 255, 0.1);
        z-index: 200; /* Очень высокий z-index на мобильных */
        position: relative;
    }

    .send-btn {
        flex-shrink: 0;
        padding: 12px 20px;
        font-size: 0.95em;
        min-width: 80px;
        z-index: 201;
        position: relative;
        pointer-events: auto !important;
        touch-action: manipulation;
    }

    .model-selector {
        font-size: clamp(0.75em, 3vw, 0.85em);
    }

    .model-selector label {
        display: none; /* Hide label on very small screens */
    }

    .model-selector select {
        padding: 8px 10px;
        font-size: clamp(0.75em, 3vw, 0.85em);
        flex: 1;
        width: 100%;
    }

    .image-preview img {
        max-width: 150px;
        max-height: 150px;
    }

    .send-btn span:first-child {
        display: none; /* Hide "Отправить" text, show only arrow */
    }
}

/* iPhone 13 specific (390px width) */
@media (max-width: 390px) {
    header h1 {
        font-size: 1.4em;
    }

    .subtitle {
        font-size: 0.8em;
    }

    .message {
        max-width: 88%;
        padding: 10px 14px;
    }
}

/* Landscape orientation for mobile */
@media (max-height: 500px) and (orientation: landscape) {
    header {
        padding: 8px 20px;
        padding-top: calc(env(safe-area-inset-top, 0px) + 8px);
        padding-left: 50px;
    }

    header h1 {
        font-size: clamp(1em, 4vw, 1.3em);
        margin-bottom: 2px;
    }

    .subtitle {
        font-size: clamp(0.65em, 2.5vw, 0.75em);
    }

    .chat-container {
        padding: 8px;
    }

    .chat-messages {
        padding: 8px;
        margin-bottom: 8px;
    }

    .input-area {
        gap: 8px;
    }

    .upload-btn {
        padding: 8px 12px;
        font-size: 0.8em;
    }
}

/* Performance optimizations */
.chat-messages,
.chats-list {
    will-change: scroll-position;
    contain: layout style paint;
    transform: translateZ(0); /* GPU acceleration */
}

.message {
    contain: layout style;
    transform: translateZ(0);
}

.chat-container {
    contain: layout style;
}

/* Optimize image rendering */
.message-image {
    image-rendering: -webkit-optimize-contrast;
    image-rendering: crisp-edges;
    max-width: 100%;
    height: auto;
}

/* Hide scrollbars when not needed */
.chat-messages:not(:hover)::-webkit-scrollbar,
.chats-list:not(:hover)::-webkit-scrollbar {
    width: 4px;
}

/* Smooth scrolling */
.chat-messages,
.chats-list {
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
}

