/* RushdCRM Chat UI — modern design system, layout, hierarchy, and overlap fixes */

:root {
    --chat-text-primary: #0f172a;
    --chat-text-secondary: #334155;
    --chat-text-muted: #64748b;
    --chat-text-meta: #64748b;
    --chat-active-bg: linear-gradient(135deg, rgba(99, 102, 241, 0.12) 0%, rgba(168, 85, 247, 0.08) 100%);
    --chat-active-border: #4f46e5;
    --chat-menu-shadow: 0 20px 40px -10px rgba(15, 23, 42, 0.18), 0 4px 12px -2px rgba(15, 23, 42, 0.08);
    --chat-composer-bg: #ffffff;
    --chat-composer-border: rgba(203, 213, 225, 0.8);
}

[data-theme="dark"] {
    --chat-text-primary: #f8fafc;
    --chat-text-secondary: #cbd5e1;
    --chat-text-muted: #94a3b8;
    --chat-text-meta: #94a3b8;
    --chat-active-bg: linear-gradient(135deg, rgba(99, 102, 241, 0.25) 0%, rgba(168, 85, 247, 0.15) 100%);
    --chat-active-border: #818cf8;
    --chat-menu-shadow: 0 24px 48px -12px rgba(0, 0, 0, 0.65), 0 4px 16px -4px rgba(0, 0, 0, 0.45);
    --chat-composer-bg: rgba(17, 28, 45, 0.95);
    --chat-composer-border: rgba(148, 163, 184, 0.25);
}

/* 1 & 2 — Header actions: spacing, grid alignment, dropdown layering */
.chat-header-bar {
    padding: 0.875rem 1.25rem;
    gap: 1rem;
    overflow: visible;
    position: relative;
    z-index: 30;
}

.chat-header-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding-right: 0.25rem;
    flex-shrink: 0;
}

.chat-header-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.375rem;
    height: 2.375rem;
    border-radius: 9999px;
    color: var(--chat-text-secondary);
    background-color: transparent;
    transition: background-color 0.15s ease, color 0.15s ease, transform 0.15s ease;
}

.chat-header-btn:hover {
    background-color: rgba(99, 102, 241, 0.1);
    color: #4f46e5;
    transform: translateY(-1px);
}

.chat-header-meta {
    color: var(--chat-text-secondary);
    font-size: 0.75rem;
    font-weight: 500;
}

.chat-more-wrap {
    position: relative;
    z-index: 50;
}

/* 1 & 3 — Dropdown menu: stable container, high z-index, crisp items */
.chat-dropdown-menu {
    position: absolute;
    right: 0;
    top: calc(100% + 0.5rem);
    min-width: 14.5rem;
    padding: 0.5rem;
    border-radius: 1rem;
    background: var(--bg-secondary, #ffffff);
    border: 1px solid var(--chat-composer-border);
    box-shadow: var(--chat-menu-shadow);
    z-index: 5000;
    overflow: hidden;
}

.chat-dropdown-menu .chat-dropdown-item {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    width: 100%;
    min-height: 2.5rem;
    padding: 0.5rem 0.875rem;
    font-size: 0.8125rem;
    font-weight: 500;
    line-height: 1.3;
    text-align: left;
    color: var(--chat-text-secondary);
    border-radius: 0.625rem;
    transition: background-color 0.15s ease, color 0.15s ease;
}

.chat-dropdown-menu .chat-dropdown-item:hover {
    background-color: rgba(99, 102, 241, 0.1);
    color: #4f46e5;
}

/* Toast positioning — bottom-right, clear of header menus */
#toast-container {
    top: auto !important;
    bottom: 1.5rem !important;
    right: 1.5rem !important;
    left: auto !important;
    z-index: 4000 !important;
}

body.chat-page #toast-container {
    bottom: 1.5rem !important;
    right: 1.5rem !important;
}

@media (max-width: 768px) {
    #toast-container {
        bottom: 1rem !important;
        right: 1rem !important;
        left: 1rem !important;
        max-width: none !important;
    }
}

/* 4 — Empty states */
.chat-empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 14rem;
    padding: 2.5rem 1.5rem;
    text-align: center;
}

.chat-empty-state-icon {
    width: 4.5rem;
    height: 4.5rem;
    margin-bottom: 1.25rem;
    border-radius: 9999px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, rgba(99, 102, 241, 0.15), rgba(168, 85, 247, 0.15));
    color: #6366f1;
    box-shadow: 0 10px 25px -5px rgba(99, 102, 241, 0.2);
}

.chat-empty-state-title {
    font-size: 1.0625rem;
    font-weight: 700;
    color: var(--chat-text-primary);
    margin-bottom: 0.5rem;
}

.chat-empty-state-hint {
    font-size: 0.875rem;
    color: var(--chat-text-secondary);
    max-width: 20rem;
    line-height: 1.5;
}

.chat-panel-empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2.5rem 1.5rem;
    background: linear-gradient(180deg, rgba(248, 250, 252, 0.6), rgba(255, 255, 255, 0.4));
    border-radius: 0 0 0.875rem 0.875rem;
}

[data-theme="dark"] .chat-panel-empty {
    background: linear-gradient(180deg, rgba(8, 17, 31, 0.5), rgba(17, 28, 45, 0.3));
}

/* 5, 7, 8 — Left chat list hierarchy & active state */
.chat-list-item {
    display: block;
    padding: 0.75rem 1rem;
    border-left: 4px solid transparent;
    transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.15s ease;
}

.chat-list-item:hover {
    background-color: rgba(148, 163, 184, 0.08);
}

.chat-list-item-active {
    background: var(--chat-active-bg) !important;
    border-left-color: var(--chat-active-border) !important;
    box-shadow: 0 2px 8px rgba(99, 102, 241, 0.08);
}

.chat-list-title {
    font-size: 0.875rem;
    font-weight: 700;
    color: var(--chat-text-primary);
    line-height: 1.3;
}

.chat-list-preview {
    margin-top: 0.25rem;
    font-size: 0.8125rem;
    color: var(--chat-text-secondary);
    line-height: 1.4;
}

.chat-list-preview-you {
    color: #4f46e5;
    font-weight: 600;
}

[data-theme="dark"] .chat-list-preview-you {
    color: #818cf8;
}

.chat-list-time {
    flex-shrink: 0;
    font-size: 0.6875rem;
    font-weight: 500;
    color: var(--chat-text-meta);
    letter-spacing: 0.01em;
}

.chat-list-item-active .chat-list-title {
    color: #4338ca;
}

[data-theme="dark"] .chat-list-item-active .chat-list-title {
    color: #c7d2fe;
}

.chat-section-label {
    font-size: 0.6875rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--chat-text-meta);
}

/* 6 — Unified message composer block */
.chat-composer-shell {
    padding: 0.75rem 1rem 0.625rem;
}

.chat-composer-block {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.375rem 0.5rem;
    border-radius: 1.25rem;
    border: 1px solid var(--chat-composer-border);
    background: var(--chat-composer-bg);
    box-shadow: 0 2px 8px rgba(15, 23, 42, 0.05);
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.chat-composer-block:focus-within {
    border-color: rgba(99, 102, 241, 0.6);
    box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.12);
}

.chat-composer-attach-btn {
    width: 2.5rem;
    height: 2.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 9999px;
    color: var(--chat-text-secondary);
    transition: all 0.15s ease;
    flex-shrink: 0;
}

.chat-composer-attach-btn:hover {
    background-color: rgba(99, 102, 241, 0.1);
    color: #4f46e5;
}

.chat-composer-input-wrap {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
}

.chat-composer-input-wrap textarea {
    width: 100%;
    min-height: 2.625rem;
    max-height: 8rem;
    padding: 0.5rem 0.625rem;
    border: 0;
    background: transparent;
    resize: none;
    font-size: 0.875rem;
    line-height: 1.45;
    color: var(--chat-text-primary);
}

.chat-composer-input-wrap textarea:focus {
    outline: none;
    box-shadow: none;
}

.chat-composer-actions {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    flex-shrink: 0;
}

.chat-composer-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    min-height: 1.25rem;
    margin-top: 0.375rem;
    padding: 0 0.5rem;
}

.chat-composer-status {
    font-size: 0.6875rem;
    color: var(--chat-text-secondary);
}

.chat-composer-meta {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.6875rem;
    color: var(--chat-text-secondary);
}

.chat-char-counter {
    font-variant-numeric: tabular-nums;
    color: var(--chat-text-meta);
    font-weight: 500;
}

.chat-char-counter.chat-char-counter-warn {
    color: #d97706;
    font-weight: 600;
}

.chat-char-counter.chat-char-counter-over {
    color: #dc2626;
    font-weight: 600;
}

/* Message history empty inline state */
#chat-empty.chat-empty-state {
    min-height: 60%;
}

/* Mobile responsive refinements */
@media (max-width: 767.98px) {
    .chat-header-bar {
        padding: 0.5rem 0.625rem;
        gap: 0.375rem;
    }

    .chat-header-actions {
        gap: 0.125rem;
        padding-right: 0;
    }

    .chat-header-btn {
        width: 1.875rem;
        height: 1.875rem;
    }

    .chat-header-btn svg {
        width: 1rem;
        height: 1rem;
    }

    /* Предотвращение авто-зума на iOS Safari при фокусе на поле ввода */
    #client-chat-message {
        font-size: 16px !important;
    }

    .chat-dropdown-menu {
        right: -0.5rem;
        min-width: 13rem;
    }

    #client-chat-history {
        padding: 0.5rem;
    }
}