/* ── Базовые стили ─────────────────────────── */

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #f0f2f5;
    color: #333;
    height: 100dvh;  /* dvh учитывает навбар мобильного браузера */
    overflow: hidden;
}

a { color: #2563eb; text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Кнопки ──────────────────────────────── */

.btn {
    display: inline-block;
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    text-decoration: none;
    transition: background 0.2s;
}
.btn-primary { background: #2563eb; color: white; }
.btn-primary:hover { background: #1d4ed8; }
.btn-primary:disabled { background: #93b4f5; cursor: not-allowed; }
.btn-secondary { background: #e5e7eb; color: #374151; }
.btn-secondary:hover { background: #d1d5db; }
.btn-danger { background: #fee2e2; color: #991b1b; }
.btn-danger:hover { background: #fecaca; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-full { width: 100%; }

/* ── Формы авторизации ───────────────────── */

.auth-container {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 20px;
}

.auth-card {
    background: white;
    border-radius: 12px;
    padding: 40px;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.08);
}

.auth-logo {
    font-size: 24px;
    font-weight: 700;
    text-align: center;
    margin-bottom: 8px;
}

.auth-card h2 {
    text-align: center;
    margin-bottom: 24px;
    color: #374151;
    font-size: 18px;
}

.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    margin-bottom: 4px;
    font-size: 13px;
    font-weight: 500;
    color: #6b7280;
}

.form-group input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s;
}
.form-group input:focus { border-color: #2563eb; }

.auth-footer {
    margin-top: 20px;
    text-align: center;
    font-size: 13px;
    color: #6b7280;
}
.auth-footer a { margin-left: 4px; }

.error-msg {
    background: #fee2e2;
    color: #991b1b;
    padding: 10px;
    border-radius: 8px;
    font-size: 13px;
    margin-bottom: 16px;
}

.success-msg {
    background: #d1fae5;
    color: #065f46;
    padding: 10px;
    border-radius: 8px;
    font-size: 13px;
    margin-bottom: 16px;
}

.pending-text {
    text-align: center;
    color: #6b7280;
    line-height: 1.6;
    margin-bottom: 20px;
}

/* ── Layout приложения ───────────────────── */

.app-layout {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    height: 100vh; /* fallback */
    max-height: -webkit-fill-available;
}

.app-header {
    display: flex;
    align-items: center;
    padding: 10px 16px;
    background: #1e293b;
    color: white;
    gap: 12px;
    flex-shrink: 0;
}

.app-title { font-weight: 600; font-size: 16px; }

.header-right {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 8px;
}

.user-badge {
    font-size: 12px;
    background: rgba(255,255,255,0.15);
    padding: 4px 10px;
    border-radius: 12px;
}

.sidebar-toggle {
    display: none;
    background: none;
    border: none;
    color: white;
    font-size: 20px;
    cursor: pointer;
}

.app-body {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* ── Sidebar ─────────────────────────────── */

.sidebar {
    width: 220px;
    background: white;
    border-right: 1px solid #e5e7eb;
    overflow-y: auto;
    flex-shrink: 0;
    padding: 12px 0;
}

.sidebar-section {
    padding: 8px 12px;
    border-bottom: 1px solid #f3f4f6;
}

.sidebar-title {
    font-size: 11px;
    font-weight: 600;
    color: #9ca3af;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 6px;
}

.sidebar-btn {
    display: block;
    width: 100%;
    text-align: left;
    padding: 7px 10px;
    border: none;
    background: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    color: #374151;
    transition: background 0.15s;
}
.sidebar-btn:hover { background: #f3f4f6; }
.sidebar-btn.active { background: #dbeafe; color: #1d4ed8; font-weight: 500; }

/* ── Чат ─────────────────────────────────── */

.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.message {
    max-width: 85%;
    animation: fadeIn 0.2s ease;
}

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

.user-message {
    align-self: flex-end;
}

.user-message .message-content {
    background: #2563eb;
    color: white;
    padding: 10px 14px;
    border-radius: 16px 16px 4px 16px;
    font-size: 14px;
    line-height: 1.5;
    white-space: pre-wrap;
}

.bot-message {
    align-self: flex-start;
}

.bot-message .message-content {
    background: white;
    padding: 12px 16px;
    border-radius: 16px 16px 16px 4px;
    font-size: 14px;
    line-height: 1.6;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
    overflow-x: auto;
    max-height: 300px;
    overflow-y: hidden;
    position: relative;
}

.bot-message .message-content.expanded {
    max-height: none;
    overflow-y: visible;
}

.bot-message .message-wrapper {
    position: relative;
}

.bot-message .message-wrapper .fade-overlay {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 60px;
    background: linear-gradient(transparent, white);
    pointer-events: none;
    border-radius: 0 0 16px 4px;
}

.bot-message .message-wrapper .fade-overlay.hidden {
    display: none;
}

.toggle-expand-btn {
    display: block;
    margin-top: 6px;
    background: none;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    padding: 4px 12px;
    font-size: 12px;
    color: #2563eb;
    cursor: pointer;
    transition: background 0.15s;
}

.toggle-expand-btn:hover {
    background: #eff6ff;
}

/* HTML-контент в ответах бота */
.bot-message .message-content h3 { margin: 12px 0 6px; font-size: 15px; }
.bot-message .message-content b { font-weight: 600; }
.bot-message .message-content table {
    border-collapse: collapse;
    width: 100%;
    margin: 8px 0;
    font-size: 13px;
}
.bot-message .message-content th,
.bot-message .message-content td {
    border: 1px solid #d1d5db;
    padding: 6px 8px;
    text-align: left;
}
.bot-message .message-content th {
    background: #343a40;
    color: white;
    font-weight: 500;
}
.bot-message .message-content ul,
.bot-message .message-content ol {
    padding-left: 20px;
    margin: 6px 0;
}
.bot-message .message-content pre {
    background: #f3f4f6;
    padding: 8px;
    border-radius: 6px;
    overflow-x: auto;
    font-size: 12px;
}
.bot-message .message-content code {
    background: #f3f4f6;
    padding: 1px 4px;
    border-radius: 3px;
    font-size: 13px;
}

.download-btn {
    display: inline-block;
    margin-top: 8px;
}

.prompt-buttons {
    display: flex;
    gap: 10px;
    margin-top: 10px;
}
.btn-rec {
    padding: 8px 20px;
    border-radius: 8px;
    font-size: 14px;
    cursor: pointer;
    border: none;
}

.typing-indicator {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    font-size: 13px;
    color: #6b7280;
}

.typing-dots {
    animation: pulse 1.5s infinite;
}
@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

/* ── Ввод сообщения ──────────────────────── */

.input-area {
    padding: 12px 16px;
    background: white;
    border-top: 1px solid #e5e7eb;
}

.input-row {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

#msg-input {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid #d1d5db;
    border-radius: 12px;
    font-size: 14px;
    outline: none;
    resize: none;
    max-height: 120px;
    font-family: inherit;
    line-height: 1.4;
}
#msg-input:focus { border-color: #2563eb; }

.send-btn {
    height: 40px;
    border-radius: 12px;
    white-space: nowrap;
}

.quick-actions-mobile {
    display: none;
    gap: 6px;
    margin-top: 8px;
    flex-wrap: wrap;
}

.quick-actions-mobile button {
    padding: 5px 10px;
    border: 1px solid #d1d5db;
    border-radius: 16px;
    background: white;
    font-size: 12px;
    cursor: pointer;
}
.quick-actions-mobile button:hover { background: #f3f4f6; }

/* ── Админ-панель ────────────────────────── */

.admin-container {
    max-width: 900px;
    margin: 0 auto;
    padding: 20px;
    overflow-y: auto;
    height: calc(100vh - 52px);
}

.admin-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 20px;
    border-bottom: 2px solid #e5e7eb;
}

.admin-tab {
    padding: 8px 16px;
    border: none;
    background: none;
    cursor: pointer;
    font-size: 14px;
    color: #6b7280;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
}
.admin-tab.active { color: #2563eb; border-bottom-color: #2563eb; font-weight: 500; }

.admin-panel { display: none; }
.admin-panel.active { display: block; }

.admin-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
    background: white;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.admin-table th,
.admin-table td {
    padding: 10px 12px;
    text-align: left;
    border-bottom: 1px solid #f3f4f6;
}

.admin-table th {
    background: #f9fafb;
    font-weight: 600;
    color: #6b7280;
    font-size: 11px;
    text-transform: uppercase;
}

.invite-code {
    font-family: monospace;
    background: #f3f4f6;
    padding: 2px 8px;
    border-radius: 4px;
    font-weight: 600;
    user-select: all;
}

.admin-actions { display: flex; gap: 4px; }

/* ── Чат регистрации ────────────────────── */

.register-chat-card {
    max-width: 480px;
    padding: 24px;
}

.register-chat {
    max-height: 350px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 12px 0;
    margin-bottom: 12px;
}

.reg-message {
    animation: fadeIn 0.2s ease;
}

.user-msg {
    align-self: flex-end;
    max-width: 80%;
}

.user-msg .reg-msg-content {
    background: #2563eb;
    color: white;
    padding: 8px 14px;
    border-radius: 14px 14px 4px 14px;
    font-size: 14px;
    line-height: 1.5;
}

.bot-msg {
    align-self: flex-start;
    max-width: 90%;
}

.bot-msg .reg-msg-content {
    background: #f3f4f6;
    padding: 10px 14px;
    border-radius: 14px 14px 14px 4px;
    font-size: 14px;
    line-height: 1.6;
}

.bot-msg .reg-msg-content code {
    background: #e5e7eb;
    padding: 1px 6px;
    border-radius: 4px;
    font-family: monospace;
    font-weight: 600;
    user-select: all;
}

.register-input-area {
    margin-bottom: 12px;
}

.register-input-row {
    display: flex;
    gap: 8px;
}

.register-input-row input {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid #d1d5db;
    border-radius: 12px;
    font-size: 14px;
    outline: none;
}
.register-input-row input:focus { border-color: #2563eb; }

.register-input-row .btn {
    height: 40px;
    border-radius: 12px;
    white-space: nowrap;
}

/* ── Мобильная адаптация ─────────────────── */

@media (max-width: 768px) {
    body { font-size: 15px; }

    /* Шапка */
    .app-header {
        padding: 8px 10px;
        gap: 8px;
        min-height: 48px;
    }
    .app-title { font-size: 14px; }
    .sidebar-toggle {
        display: block;
        font-size: 22px;
        padding: 4px 8px;
    }
    .header-right { gap: 6px; }
    .header-right .btn-sm {
        font-size: 11px;
        padding: 4px 8px;
        border-radius: 4px;
    }
    .user-badge { display: none; }

    /* Сайдбар — выезжает слева */
    .sidebar {
        position: fixed;
        left: -280px;
        top: 48px;
        bottom: 0;
        width: 270px;
        z-index: 100;
        transition: left 0.25s ease;
        box-shadow: 2px 0 16px rgba(0,0,0,0.2);
        background: white;
    }
    .sidebar.open { left: 0; }

    /* Затемнение фона при открытом сайдбаре */
    .sidebar-overlay {
        display: none;
        position: fixed;
        top: 48px;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0,0,0,0.3);
        z-index: 99;
    }
    .sidebar-overlay.active { display: block; }

    /* Чат — занимает весь экран */
    .chat-area { width: 100%; }

    .messages {
        padding: 10px;
        gap: 10px;
    }

    .message { max-width: 92%; }

    .user-message .message-content {
        padding: 9px 12px;
        font-size: 15px;
        border-radius: 14px 14px 4px 14px;
    }

    .bot-message .message-content {
        padding: 10px 12px;
        font-size: 15px;
        line-height: 1.5;
        border-radius: 14px 14px 14px 4px;
    }

    .bot-message .message-content table { font-size: 12px; }
    .bot-message .message-content th,
    .bot-message .message-content td { padding: 4px 6px; }
    .bot-message .message-content pre { font-size: 11px; padding: 6px; }

    /* Поле ввода — гарантированно видно над навбаром браузера */
    .input-area {
        padding: 6px 10px;
        padding-bottom: max(12px, env(safe-area-inset-bottom, 12px));
        flex-shrink: 0;
    }

    .input-row { gap: 6px; }

    #msg-input {
        padding: 10px 12px;
        font-size: 16px;  /* 16px чтобы iOS не зумил */
        border-radius: 20px;
    }

    .send-btn {
        height: 42px;
        padding: 0 14px;
        border-radius: 20px;
        font-size: 13px;
    }

    /* Быстрые кнопки — всегда видны на мобильном */
    .quick-actions-mobile {
        display: flex;
        gap: 6px;
        margin-top: 6px;
        overflow-x: auto;
        flex-wrap: nowrap;
        padding-bottom: 2px;
        -webkit-overflow-scrolling: touch;
    }

    .quick-actions-mobile button {
        padding: 6px 12px;
        border: 1px solid #d1d5db;
        border-radius: 20px;
        background: white;
        font-size: 13px;
        cursor: pointer;
        white-space: nowrap;
        flex-shrink: 0;
    }
    .quick-actions-mobile button:active { background: #dbeafe; }

    /* Индикатор загрузки */
    .typing-indicator {
        padding: 6px 10px;
        font-size: 14px;
    }

    /* Приветственное сообщение */
    .welcome-msg .message-content {
        font-size: 15px;
        line-height: 1.5;
    }

    /* Админ-панель */
    .admin-container { padding: 10px; }
    .admin-tabs { gap: 2px; overflow-x: auto; flex-wrap: nowrap; }
    .admin-tab { padding: 8px 10px; font-size: 13px; white-space: nowrap; }
    .admin-table { font-size: 12px; }
    .admin-table th, .admin-table td { padding: 6px 8px; }
}

/* Очень маленькие экраны (< 380px) */
@media (max-width: 380px) {
    .app-title { font-size: 13px; }
    .header-right .btn-sm { font-size: 10px; padding: 3px 6px; }
    .quick-actions-mobile button { font-size: 12px; padding: 5px 10px; }
}
