/* ============================================================
   SupportBot Design System — main stylesheet
   ============================================================ */

/* ---------- Design Tokens ---------------------------------- */
:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --primary-light: #eff6ff;
    --secondary: #64748b;
    --success: #16a34a;
    --warning: #d97706;
    --danger: #dc2626;
    --info: #0891b2;

    --bg: #f1f5f9;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --border: #e2e8f0;
    --border-hover: #cbd5e1;

    --text-primary: #0f172a;
    --text-secondary: #475569;
    --text-muted: #94a3b8;
    --text-inverse: #ffffff;

    --sidebar-width: 240px;
    --sidebar-collapsed-width: 64px;
    --topbar-height: 56px;

    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 16px;
    --radius-xl: 24px;

    --shadow-sm: 0 1px 3px rgba(0, 0, 0, .06), 0 1px 2px rgba(0, 0, 0, .04);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, .06), 0 2px 4px rgba(0, 0, 0, .04);
    --shadow-lg: 0 10px 15px rgba(0, 0, 0, .08), 0 4px 6px rgba(0, 0, 0, .04);

    --font-sans: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
    --font-mono: "SF Mono", "JetBrains Mono", Consolas, "Courier New", monospace;

    --transition: 150ms ease;
}

/* ---------- Reset & Base ----------------------------------- */
*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    height: 100%;
}

body {
    font-family: var(--font-sans);
    font-size: 14px;
    line-height: 1.6;
    color: var(--text-primary);
    background: var(--bg);
    height: 100%;
    margin: 0;
    -webkit-font-smoothing: antialiased;
}

a {
    color: var(--primary);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

/* ---------- Layout ---------------------------------------- */
#app-wrapper {
    display: flex;
    height: 100%;
}

/* ---- Немедленное состояние до загрузки sidebar.js (предотвращает мигание) ---- */
html.sb-pre-collapsed #sidebar {
    width: var(--sidebar-collapsed-width);
    transition: none !important;
}

html.sb-pre-collapsed #main {
    transition: none !important;
}

html.sb-pre-collapsed #sidebar .sidebar-brand-text,
html.sb-pre-collapsed #sidebar .sidebar-section-label,
html.sb-pre-collapsed #sidebar .sidebar-nav-item .nav-text,
html.sb-pre-collapsed #sidebar .sidebar-nav-item .nav-badge,
html.sb-pre-collapsed #sidebar .sidebar-user-name,
html.sb-pre-collapsed #sidebar .sidebar-user-chevron {
    display: none;
}

html.sb-pre-collapsed #sidebar~#main {
    margin-left: var(--sidebar-collapsed-width);
}

/* ---------- Sidebar --------------------------------------- */
#sidebar {
    width: var(--sidebar-width);
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    height: 100vh;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 200;
    overflow-y: auto;
    overflow-x: hidden;
    transition: width .25s ease, transform .25s ease;
}

/* ---- Свёрнутый сайдбар (только десктоп) ---- */
#sidebar.collapsed {
    width: var(--sidebar-collapsed-width);
}

#sidebar.collapsed .sidebar-brand-text {
    display: none;
}

#sidebar.collapsed .sidebar-section-label {
    display: none;
}

#sidebar.collapsed .sidebar-nav-item .nav-text {
    display: none;
}

#sidebar.collapsed .sidebar-nav-item .nav-badge {
    display: none;
}

#sidebar.collapsed .sidebar-user-name,
#sidebar.collapsed .sidebar-user-chevron {
    display: none;
}

#sidebar.collapsed .sidebar-brand {
    justify-content: center;
    padding: 18px 16px 16px;
}

#sidebar.collapsed .sidebar-nav-item {
    justify-content: center;
    padding: 10px;
    position: relative;
}

#sidebar.collapsed .sidebar-nav-item .nav-icon {
    width: auto;
    font-size: 18px;
}

#sidebar.collapsed .sidebar-user {
    justify-content: center;
    padding: 8px;
}

#sidebar.collapsed .sidebar-nav {
    padding: 8px 8px;
}

#sidebar.collapsed .sidebar-footer {
    padding: 12px 8px;
}

/* Тултип при свёрнутом сайдбаре */
#sidebar.collapsed .sidebar-nav-item::after {
    content: attr(data-label);
    position: absolute;
    left: calc(var(--sidebar-collapsed-width) - 4px);
    top: 50%;
    transform: translateY(-50%);
    background: var(--text-primary);
    color: #fff;
    font-size: 12px;
    font-weight: 500;
    padding: 5px 10px;
    border-radius: var(--radius-sm);
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    transition: opacity .15s ease;
    z-index: 300;
    box-shadow: var(--shadow-md);
}

#sidebar.collapsed .sidebar-nav-item:hover::after {
    opacity: 1;
}

#sidebar.collapsed .sidebar-user::after {
    content: attr(data-label);
    position: absolute;
    left: calc(var(--sidebar-collapsed-width) - 4px);
    top: 50%;
    transform: translateY(-50%);
    background: var(--text-primary);
    color: #fff;
    font-size: 12px;
    font-weight: 500;
    padding: 5px 10px;
    border-radius: var(--radius-sm);
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    transition: opacity .15s ease;
    z-index: 300;
    box-shadow: var(--shadow-md);
}

#sidebar.collapsed .sidebar-user:hover::after {
    opacity: 1;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 18px 20px 16px;
    border-bottom: 1px solid var(--border);
    font-weight: 700;
    font-size: 16px;
    color: var(--text-primary);
    text-decoration: none;
    letter-spacing: -.3px;
}

.sidebar-brand:hover {
    text-decoration: none;
    color: var(--text-primary);
}

.sidebar-brand-icon {
    width: 32px;
    height: 32px;
    background: var(--primary);
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 16px;
    flex-shrink: 0;
}

.sidebar-section-label {
    font-size: 10px;
    font-weight: 700;
    letter-spacing: .08em;
    text-transform: uppercase;
    color: var(--text-muted);
    padding: 18px 20px 6px;
}

.sidebar-nav {
    padding: 8px 12px;
    flex: 1;
}

.sidebar-nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-weight: 500;
    font-size: 13.5px;
    transition: background var(--transition), color var(--transition);
    cursor: pointer;
    text-decoration: none;
    position: relative;
}

.sidebar-nav-item:hover {
    background: var(--bg);
    color: var(--text-primary);
    text-decoration: none;
}

.sidebar-nav-item.active {
    background: var(--primary-light);
    color: var(--primary);
}

.sidebar-nav-item .nav-icon {
    font-size: 16px;
    width: 20px;
    text-align: center;
    flex-shrink: 0;
}

.sidebar-nav-item .nav-badge {
    margin-left: auto;
    background: var(--danger);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    padding: 1px 6px;
    border-radius: 10px;
    min-width: 18px;
    text-align: center;
}

.sidebar-footer {
    border-top: 1px solid var(--border);
    padding: 12px;
}

.sidebar-user {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 13px;
    font-weight: 500;
    text-decoration: none;
    transition: background var(--transition);
}

.sidebar-user:hover {
    background: var(--bg);
    color: var(--text-primary);
    text-decoration: none;
}

.sidebar-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.sidebar-avatar-placeholder {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    font-weight: 700;
    font-size: 13px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.sidebar-user-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ---------- Main Content ---------------------------------- */
#main {
    margin-left: var(--sidebar-width);
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 100vh;
    min-width: 0;
    transition: margin-left .25s ease;
}

/* Когда сайдбар свёрнут — сдвигаем контент */
#sidebar.collapsed~#main {
    margin-left: var(--sidebar-collapsed-width);
}

/* ---------- Top Bar --------------------------------------- */
#topbar {
    height: var(--topbar-height);
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 24px;
    gap: 12px;
    flex-shrink: 0;
    position: sticky;
    top: 0;
    z-index: 100;
}

.topbar-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
    flex: 1;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* ---------- Page Content ---------------------------------- */
.page-content {
    padding: 24px;
    flex: 1;
}

.page-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 24px;
}

.page-header-title {
    font-size: 20px;
    font-weight: 700;
    color: var(--text-primary);
    margin: 0;
    line-height: 1.3;
}

.page-header-subtitle {
    font-size: 13px;
    color: var(--text-secondary);
    margin: 4px 0 0;
}

/* ---------- Cards ---------------------------------------- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
}

.card-header {
    padding: 14px 18px;
    border-bottom: 1px solid var(--border);
    font-weight: 600;
    font-size: 13.5px;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: transparent;
    border-radius: var(--radius-md) var(--radius-md) 0 0;
}

.card-body {
    padding: 18px;
}

.card-footer {
    padding: 14px 18px;
    border-top: 1px solid var(--border);
    background: var(--surface-2);
    border-radius: 0 0 var(--radius-md) var(--radius-md);
}

/* ---------- Stat Cards ------------------------------------ */
.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 20px;
    box-shadow: var(--shadow-sm);
}

.stat-card-label {
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .05em;
    color: var(--text-muted);
    margin-bottom: 8px;
}

.stat-card-value {
    font-size: 28px;
    font-weight: 700;
    color: var(--text-primary);
    line-height: 1;
}

.stat-card-sub {
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 6px;
}

.stat-card-icon {
    font-size: 22px;
    opacity: .7;
}

/* ---------- Buttons --------------------------------------- */
.btn {
    font-size: 13px;
    font-weight: 500;
    border-radius: var(--radius-sm);
    padding: 7px 14px;
    transition: all var(--transition);
    display: inline-flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    border: 1px solid transparent;
    white-space: nowrap;
    line-height: 1.4;
    text-decoration: none;
}

.btn:hover {
    text-decoration: none;
}

.btn:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

.btn-primary {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}

.btn-primary:hover {
    background: var(--primary-dark);
    border-color: var(--primary-dark);
    color: #fff;
}

.btn-secondary {
    background: #fff;
    border-color: var(--border);
    color: var(--text-primary);
}

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

.btn-danger {
    background: var(--danger);
    border-color: var(--danger);
    color: #fff;
}

.btn-danger:hover {
    background: #b91c1c;
    border-color: #b91c1c;
    color: #fff;
}

.btn-success {
    background: var(--success);
    border-color: var(--success);
    color: #fff;
}

.btn-success:hover {
    background: #15803d;
    color: #fff;
}

.btn-warning {
    background: var(--warning);
    border-color: var(--warning);
    color: #fff;
}

.btn-warning:hover {
    background: #b45309;
    color: #fff;
}

.btn-ghost {
    background: transparent;
    border-color: transparent;
    color: var(--text-secondary);
}

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

.btn-outline-primary {
    background: transparent;
    border-color: var(--primary);
    color: var(--primary);
}

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

.btn-outline-danger {
    background: transparent;
    border-color: var(--danger);
    color: var(--danger);
}

.btn-outline-danger:hover {
    background: var(--danger);
    color: #fff;
}

.btn-outline-secondary {
    background: transparent;
    border-color: var(--border);
    color: var(--text-secondary);
}

.btn-outline-secondary:hover {
    background: var(--bg);
    border-color: var(--border-hover);
    color: var(--text-primary);
}

.btn-sm {
    padding: 4px 10px;
    font-size: 12px;
}

.btn-lg {
    padding: 10px 20px;
    font-size: 15px;
}

.btn-icon {
    padding: 7px;
    border-radius: var(--radius-sm);
}

.btn-icon.btn-sm {
    padding: 5px;
}

/* ---------- Forms ---------------------------------------- */
.form-label {
    font-size: 13px;
    font-weight: 500;
    color: var(--text-primary);
    margin-bottom: 5px;
    display: block;
}

.form-control,
.form-select {
    display: block;
    width: 100%;
    padding: 8px 12px;
    font-size: 13.5px;
    font-family: var(--font-sans);
    color: var(--text-primary);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    transition: border-color var(--transition), box-shadow var(--transition);
    line-height: 1.5;
    appearance: none;
}

.form-control:focus,
.form-select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}

.form-control::placeholder {
    color: var(--text-muted);
}

.form-control.is-invalid {
    border-color: var(--danger);
}

.invalid-feedback {
    font-size: 12px;
    color: var(--danger);
    margin-top: 4px;
}

.form-text {
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 4px;
}

textarea.form-control {
    resize: vertical;
    min-height: 80px;
}

.form-check {
    display: flex;
    align-items: center;
    gap: 8px;
}

.form-check-input {
    width: 16px;
    height: 16px;
    border: 1.5px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
    flex-shrink: 0;
    accent-color: var(--primary);
}

.form-check-label {
    font-size: 13px;
    color: var(--text-secondary);
    cursor: pointer;
}

.input-group {
    display: flex;
}

.input-group .form-control,
.input-group .form-select {
    border-radius: 0;
}

.input-group> :first-child {
    border-radius: var(--radius-sm) 0 0 var(--radius-sm);
}

.input-group> :last-child {
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.input-group> :only-child {
    border-radius: var(--radius-sm);
}

/* ---------- Tables ---------------------------------------- */
.table-wrapper {
    overflow-x: auto;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
}

table.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13.5px;
}

table.data-table thead th {
    padding: 10px 14px;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--text-muted);
    background: var(--surface-2);
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
    text-align: left;
}

table.data-table tbody td {
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
    color: var(--text-primary);
    vertical-align: middle;
}

table.data-table tbody tr:last-child td {
    border-bottom: none;
}

table.data-table tbody tr:hover td {
    background: var(--surface-2);
}

table.data-table .col-actions {
    text-align: right;
    white-space: nowrap;
}

/* ---------- Badges ---------------------------------------- */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    font-size: 11px;
    font-weight: 600;
    border-radius: 20px;
    line-height: 1.6;
    white-space: nowrap;
}

.badge-primary {
    background: #dbeafe;
    color: #1d4ed8;
}

.badge-success {
    background: #dcfce7;
    color: #15803d;
}

.badge-warning {
    background: #fef3c7;
    color: #92400e;
}

.badge-danger {
    background: #fee2e2;
    color: #b91c1c;
}

.badge-secondary {
    background: #f1f5f9;
    color: #475569;
}

.badge-info {
    background: #cffafe;
    color: #0e7490;
}

.badge-dot::before {
    content: '';
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
}

/* ---------- Alerts ---------------------------------------- */
.alert {
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    font-size: 13.5px;
    display: flex;
    align-items: flex-start;
    gap: 10px;
    border: 1px solid transparent;
}

.alert-success {
    background: #f0fdf4;
    border-color: #bbf7d0;
    color: #15803d;
}

.alert-warning {
    background: #fffbeb;
    border-color: #fde68a;
    color: #92400e;
}

.alert-danger {
    background: #fef2f2;
    border-color: #fecaca;
    color: #b91c1c;
}

.alert-info {
    background: #f0f9ff;
    border-color: #bae6fd;
    color: #0369a1;
}

/* ---------- Tabs ------------------------------------------ */
.nav-tabs-custom {
    display: flex;
    gap: 4px;
    border-bottom: 1px solid var(--border);
    padding: 0;
    list-style: none;
    margin: 0 0 24px;
}

.nav-tabs-custom .tab-link {
    padding: 8px 16px;
    font-size: 13.5px;
    font-weight: 500;
    color: var(--text-secondary);
    border-radius: var(--radius-sm) var(--radius-sm) 0 0;
    border: 1px solid transparent;
    border-bottom: none;
    cursor: pointer;
    text-decoration: none;
    transition: color var(--transition), background var(--transition);
    margin-bottom: -1px;
    display: flex;
    align-items: center;
    gap: 6px;
    background: transparent;
}

.nav-tabs-custom .tab-link:hover {
    color: var(--text-primary);
    background: var(--surface-2);
    text-decoration: none;
}

.nav-tabs-custom .tab-link.active {
    color: var(--primary);
    background: var(--surface);
    border-color: var(--border);
}

/* ---------- Chat Layout ----------------------------------- */
.chat-layout {
    display: flex;
    height: calc(100vh - var(--topbar-height));
    overflow: hidden;
}

.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--bg);
}

.chat-sidebar {
    width: 300px;
    flex-shrink: 0;
    background: var(--surface);
    border-left: 1px solid var(--border);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

/* Chat header */
.chat-topbar {
    height: var(--topbar-height);
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 16px;
    gap: 12px;
    flex-shrink: 0;
}

.chat-topbar-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.chat-topbar-avatar-placeholder {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 14px;
    flex-shrink: 0;
}

.chat-topbar-info {
    flex: 1;
    min-width: 0;
}

.chat-topbar-name {
    font-weight: 600;
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-topbar-sub {
    font-size: 12px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Messages area */
.chat-messages-wrap {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    scroll-behavior: smooth;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

/* Date separator */
.msg-date-sep {
    text-align: center;
    margin: 16px 0 8px;
}

.msg-date-sep span {
    background: rgba(0, 0, 0, .06);
    color: var(--text-secondary);
    font-size: 11px;
    font-weight: 500;
    padding: 3px 12px;
    border-radius: 10px;
}

/* Bubble */
.msg-row {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    margin-bottom: 2px;
}

.msg-row.out {
    flex-direction: row-reverse;
}

.msg-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
    align-self: flex-end;
}

.msg-avatar-placeholder {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--secondary);
    color: #fff;
    font-size: 11px;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    align-self: flex-end;
}

.msg-bubble-wrap {
    max-width: 70%;
    display: flex;
    flex-direction: column;
}

.msg-row.in .msg-bubble-wrap {
    align-items: flex-start;
}

.msg-row.out .msg-bubble-wrap {
    align-items: flex-end;
}

.msg-sender-name {
    font-size: 11px;
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 2px;
    padding: 0 12px;
}

.msg-bubble {
    position: relative;
    padding: 8px 12px;
    border-radius: var(--radius-lg);
    font-size: 13.5px;
    line-height: 1.55;
    word-break: break-word;
    white-space: pre-wrap;
}

.msg-row.in .msg-bubble {
    background: var(--surface);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.msg-row.out .msg-bubble {
    background: var(--primary);
    color: #fff;
    border-bottom-right-radius: 4px;
}

/* group: first/last of sequence */
.msg-row.in.first .msg-bubble {
    border-top-left-radius: var(--radius-lg);
}

.msg-row.in.last .msg-bubble {
    border-bottom-left-radius: 4px;
}

.msg-row.in.mid .msg-bubble {
    border-top-left-radius: 4px;
    border-bottom-left-radius: 4px;
}

.msg-row.out.first .msg-bubble {
    border-top-right-radius: var(--radius-lg);
}

.msg-row.out.last .msg-bubble {
    border-bottom-right-radius: 4px;
}

.msg-row.out.mid .msg-bubble {
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
}

.msg-time {
    font-size: 10.5px;
    opacity: .65;
    margin-top: 4px;
    padding: 0 4px;
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 3px;
}

.msg-row.in .msg-time {
    justify-content: flex-start;
}

.msg-row.out .msg-time {
    justify-content: flex-end;
}

/* Reply preview inside bubble */
.msg-reply-preview {
    background: rgba(0, 0, 0, .08);
    border-left: 3px solid rgba(255, 255, 255, .5);
    border-radius: 4px;
    padding: 4px 8px;
    margin-bottom: 6px;
    font-size: 12px;
    opacity: .85;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.msg-row.in .msg-reply-preview {
    border-color: var(--primary);
    background: var(--bg);
    color: var(--text-secondary);
}

/* Image inside bubble */
.msg-bubble img.msg-img {
    max-width: 240px;
    max-height: 200px;
    border-radius: 8px;
    cursor: pointer;
    display: block;
    margin-bottom: 4px;
    object-fit: cover;
}

/* File attachment */
.msg-file {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    background: rgba(0, 0, 0, .07);
    border-radius: 8px;
    font-size: 12.5px;
    text-decoration: none;
    color: inherit;
}

.msg-file:hover {
    opacity: .8;
}

.msg-row.out .msg-file {
    color: #fff;
}

/* Message options (reply btn) */
.msg-options {
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition);
    display: flex;
    align-items: center;
    gap: 2px;
    align-self: center;
}

.msg-row:hover .msg-options {
    opacity: 1;
    pointer-events: auto;
}

.msg-option-btn {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 3px 6px;
    font-size: 12px;
    cursor: pointer;
    color: var(--text-secondary);
    transition: background var(--transition);
}

.msg-option-btn:hover,
.msg-option-btn:focus-visible {
    background: var(--bg);
    color: var(--text-primary);
    outline: none;
}

/* Сбрасываем hover-эффект когда кнопка скрыта (поддержка touch-устройств) */
.msg-options:not(:hover) .msg-option-btn {
    background: var(--surface);
    color: var(--text-secondary);
}

/* ---------- Chat Input Area ------------------------------- */
.chat-input-wrap {
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 12px 16px;
    flex-shrink: 0;
}

/* Reply banner */
.reply-banner {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--primary-light);
    border-left: 3px solid var(--primary);
    border-radius: 4px;
    padding: 6px 10px;
    margin-bottom: 8px;
    font-size: 12.5px;
    color: var(--text-secondary);
}

.reply-banner-text {
    flex: 1;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.reply-cancel {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 16px;
    padding: 0 2px;
    line-height: 1;
}

.reply-cancel:hover {
    color: var(--text-primary);
}

/* File preview */
.file-preview-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 6px 10px;
    margin-bottom: 8px;
    font-size: 12.5px;
}

.file-preview-bar .file-name {
    flex: 1;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    color: var(--text-secondary);
}

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

.chat-input-row .btn {
    height: 38px;
}

#msgInput {
    flex: 1;
    min-height: 38px;
    max-height: 130px;
    /* ~5 lines */
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    font-size: 13.5px;
    font-family: var(--font-sans);
    resize: none;
    overflow-y: auto;
    line-height: 1.5;
    transition: border-color var(--transition), box-shadow var(--transition);
    background: var(--surface);
    color: var(--text-primary);
}

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

#msgInput::placeholder {
    color: var(--text-muted);
}

/* ---------- Chat Sidebar Sections ------------------------- */
.cs-section {
    border-bottom: 1px solid var(--border);
}

.cs-section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--text-muted);
    cursor: pointer;
    user-select: none;
}

.cs-section-header:hover {
    background: var(--surface-2);
}

.cs-section-body {
    padding: 0 16px 12px;
}

.cs-meta-row {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 5px 0;
    font-size: 13px;
    gap: 8px;
}

.cs-meta-label {
    color: var(--text-muted);
    flex-shrink: 0;
}

.cs-meta-value {
    color: var(--text-primary);
    font-weight: 500;
    text-align: right;
    word-break: break-word;
}

/* ---------- Dashboard Chat List --------------------------- */
.chat-list-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    text-decoration: none;
    color: inherit;
    transition: background var(--transition);
    position: relative;
}

.chat-list-item:hover {
    background: var(--surface-2);
    text-decoration: none;
    color: inherit;
}

.chat-list-item.unread {
    background: #f0f9ff;
}

.chat-list-item.blocked {
    opacity: .55;
}

.chat-list-avatar {
    position: relative;
    flex-shrink: 0;
}

.chat-list-avatar img,
.chat-list-avatar .avatar-placeholder {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    object-fit: cover;
}

.chat-list-avatar .avatar-placeholder {
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 16px;
}

.chat-list-avatar .unread-dot {
    position: absolute;
    top: 0;
    right: 0;
    width: 14px;
    height: 14px;
    background: var(--danger);
    border-radius: 50%;
    border: 2px solid var(--surface);
}

.chat-list-body {
    flex: 1;
    min-width: 0;
}

.chat-list-name {
    font-weight: 600;
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-bottom: 2px;
}

.chat-list-preview {
    font-size: 12.5px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-list-meta {
    flex-shrink: 0;
    text-align: right;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
}

.chat-list-time {
    font-size: 11px;
    color: var(--text-muted);
}

/* ---------- Pagination ------------------------------------ */
.pagination {
    display: flex;
    align-items: center;
    gap: 4px;
    list-style: none;
    padding: 0;
    margin: 0;
}

.page-item .page-link {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 32px;
    height: 32px;
    padding: 0 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 13px;
    color: var(--text-secondary);
    background: var(--surface);
    text-decoration: none;
    transition: all var(--transition);
}

.page-item .page-link:hover {
    background: var(--bg);
    color: var(--text-primary);
}

.page-item.active .page-link {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}

.page-item.disabled .page-link {
    opacity: .45;
    pointer-events: none;
}

/* ---------- Modals ---------------------------------------- */
.modal-content {
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
}

.modal-header {
    padding: 18px 20px;
    border-bottom: 1px solid var(--border);
}

.modal-title {
    font-size: 15px;
    font-weight: 600;
}

.modal-body {
    padding: 20px;
}

.modal-footer {
    padding: 14px 20px;
    border-top: 1px solid var(--border);
    gap: 8px;
}

/* ---------- Toasts ---------------------------------------- */
.toast {
    min-width: 280px;
    max-width: 340px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
}

.toast-header {
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    font-weight: 600;
    font-size: 13px;
}

.toast-body {
    padding: 12px 14px;
    font-size: 13px;
}

/* ---------- Timeline (Ticket History) --------------------- */
.timeline {
    position: relative;
    padding-left: 28px;
}

.timeline::before {
    content: '';
    position: absolute;
    left: 10px;
    top: 0;
    bottom: 0;
    width: 1px;
    background: var(--border);
}

.timeline-item {
    position: relative;
    margin-bottom: 16px;
}

.timeline-item::before {
    content: '';
    position: absolute;
    left: -22px;
    top: 6px;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--border);
    border: 2px solid var(--surface);
}

.timeline-item.comment::before {
    background: var(--primary);
}

.timeline-item.log::before {
    background: var(--text-muted);
}

.timeline-log {
    font-size: 12.5px;
    color: var(--text-secondary);
    padding: 6px 12px;
    background: var(--surface-2);
    border-radius: var(--radius-sm);
}

.timeline-comment {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
}

.timeline-comment-header {
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12.5px;
}

.timeline-comment-body {
    padding: 12px 14px;
    font-size: 13.5px;
}

/* ---------- Settings Layout ------------------------------- */
.settings-layout {
    display: grid;
    grid-template-columns: 200px 1fr;
    gap: 24px;
    align-items: start;
}

.settings-nav {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 8px 0;
    position: sticky;
    top: calc(var(--topbar-height) + 24px);
}

.settings-nav-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 9px 16px;
    font-size: 13.5px;
    font-weight: 500;
    color: var(--text-secondary);
    text-decoration: none;
    transition: background var(--transition), color var(--transition);
}

.settings-nav-item:hover {
    background: var(--bg);
    color: var(--text-primary);
    text-decoration: none;
}

.settings-nav-item.active {
    background: var(--primary-light);
    color: var(--primary);
}

.settings-section {
    scroll-margin-top: calc(var(--topbar-height) + 24px);
}

.settings-section-title {
    font-size: 15px;
    font-weight: 700;
    color: var(--text-primary);
    margin: 0 0 4px;
}

.settings-section-desc {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 16px;
}

/* ---------- Connection indicator -------------------------- */
#connectionStatus {
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 10px;
    font-weight: 600;
}

#connectionStatus.ok {
    background: #dcfce7;
    color: #15803d;
}

#connectionStatus.offline {
    background: #fee2e2;
    color: #b91c1c;
}

/* ---------- Lightbox -------------------------------------- */
#lightboxModal .modal-dialog {
    max-width: 90vw;
}

#lightboxModal img {
    max-width: 100%;
    max-height: 80vh;
    object-fit: contain;
    border-radius: var(--radius-sm);
}

/* ---------- Snippet/Template Picker ----------------------- */
.template-picker-dropdown {
    width: 320px;
    max-height: 280px;
    overflow-y: auto;
}

.template-item {
    padding: 10px 14px;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
    transition: background var(--transition);
}

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

.template-item:hover {
    background: var(--primary-light);
}

.template-item-title {
    font-weight: 600;
    margin-bottom: 2px;
}

.template-item-text {
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ---------- Loading spinner ------------------------------ */
.spinner {
    width: 18px;
    height: 18px;
    border: 2px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin .6s linear infinite;
    display: inline-block;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* ---------- Empty state ----------------------------------- */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 64px 24px;
    color: var(--text-muted);
    text-align: center;
}

.empty-state-icon {
    font-size: 48px;
    margin-bottom: 16px;
    opacity: .4;
}

.empty-state-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.empty-state-text {
    font-size: 13px;
    max-width: 320px;
}

/* ---------- Status badge helpers -------------------------- */
.status-open {
    background: #dcfce7;
    color: #15803d;
}

.status-in_progress {
    background: #fef3c7;
    color: #92400e;
}

.status-resolved {
    background: #f1f5f9;
    color: #475569;
}

.status-closed {
    background: #f1f5f9;
    color: #374151;
}

/* ---------- Avatar helpers -------------------------------- */
.avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.avatar-sm {
    width: 24px;
    height: 24px;
    font-size: 10px;
}

.avatar-lg {
    width: 64px;
    height: 64px;
    font-size: 24px;
}

.avatar-xl {
    width: 96px;
    height: 96px;
    font-size: 36px;
}

.avatar-placeholder {
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    flex-shrink: 0;
}

/* ---------- Utilities ------------------------------------- */
.d-flex {
    display: flex;
}

.d-none {
    display: none;
}

.align-items-center {
    align-items: center;
}

.align-items-start {
    align-items: flex-start;
}

.justify-content-between {
    justify-content: space-between;
}

.justify-content-center {
    justify-content: center;
}

.flex-1 {
    flex: 1;
}

.gap-1 {
    gap: 4px;
}

.gap-2 {
    gap: 8px;
}

.gap-3 {
    gap: 12px;
}

.text-muted {
    color: var(--text-muted) !important;
}

.text-secondary {
    color: var(--text-secondary) !important;
}

.text-danger {
    color: var(--danger) !important;
}

.text-success {
    color: var(--success) !important;
}

.text-primary {
    color: var(--primary) !important;
}

.text-truncate {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.fw-600 {
    font-weight: 600;
}

.fw-700 {
    font-weight: 700;
}

.fs-12 {
    font-size: 12px;
}

.fs-13 {
    font-size: 13px;
}

.w-100 {
    width: 100%;
}

.mb-0 {
    margin-bottom: 0;
}

.mb-2 {
    margin-bottom: 8px;
}

.mb-3 {
    margin-bottom: 12px;
}

.mb-4 {
    margin-bottom: 16px;
}

.mt-0 {
    margin-top: 0;
}

.mt-2 {
    margin-top: 8px;
}

.mt-3 {
    margin-top: 12px;
}

.mt-4 {
    margin-top: 16px;
}

.me-1 {
    margin-right: 4px;
}

.me-2 {
    margin-right: 8px;
}

.ms-auto {
    margin-left: auto;
}

.p-0 {
    padding: 0;
}

/* ---------- Markdown content in bubbles / tickets --------- */
.md-content {
    font-size: 13.5px;
    line-height: 1.6;
}

.md-content p {
    margin: 0 0 .5em;
}

.md-content p:last-child {
    margin: 0;
}

.md-content code {
    background: rgba(0, 0, 0, .08);
    border-radius: 3px;
    padding: 1px 4px;
    font-family: var(--font-mono);
    font-size: .88em;
}

.msg-row.out .md-content code {
    background: rgba(255, 255, 255, .15);
}

.md-content pre {
    background: rgba(0, 0, 0, .06);
    border-radius: 6px;
    padding: 10px 12px;
    overflow-x: auto;
    font-family: var(--font-mono);
    font-size: .85em;
    margin: 6px 0;
}

.md-content ul,
.md-content ol {
    padding-left: 1.4em;
    margin: .4em 0;
}

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

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

/* ---------- Login page ------------------------------------ */
.login-wrap {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    padding: 24px;
}

.login-card {
    width: 100%;
    max-width: 380px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
}

.login-header {
    padding: 28px 28px 20px;
    text-align: center;
    border-bottom: 1px solid var(--border);
}

.login-logo {
    width: 48px;
    height: 48px;
    background: var(--primary);
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    color: #fff;
    margin: 0 auto 12px;
}

.login-title {
    font-size: 18px;
    font-weight: 700;
    margin: 0 0 4px;
}

.login-sub {
    font-size: 13px;
    color: var(--text-secondary);
    margin: 0;
}

.login-body {
    padding: 24px 28px 28px;
}

/* ---------- Responsive ------------------------------------ */
/* ============================================================
   Адаптивность — мобильные устройства
   ============================================================ */

@media (max-width: 768px) {
    :root {
        --sidebar-width: 0px;
        --topbar-height: 52px;
    }

    #sidebar {
        width: 260px !important;
        transform: translateX(-100%);
        transition: transform .25s ease, width .25s ease;
        box-shadow: none;
    }

    #sidebar.open {
        transform: translateX(0);
        box-shadow: 4px 0 24px rgba(0, 0, 0, .18);
    }

    /* Сброс свёрнутого состояния на мобильном */
    #sidebar.collapsed {
        width: 260px !important;
    }

    #sidebar.collapsed .sidebar-brand-text,
    #sidebar.collapsed .sidebar-section-label,
    #sidebar.collapsed .sidebar-nav-item .nav-text,
    #sidebar.collapsed .sidebar-nav-item .nav-badge,
    #sidebar.collapsed .sidebar-user-name,
    #sidebar.collapsed .sidebar-user-chevron {
        display: unset;
    }

    #sidebar.collapsed .sidebar-brand {
        justify-content: flex-start;
        padding: 18px 20px 16px;
    }

    #sidebar.collapsed .sidebar-nav-item {
        justify-content: flex-start;
        padding: 8px 10px;
    }

    #sidebar.collapsed .sidebar-nav-item .nav-icon {
        font-size: 16px;
        width: 20px;
    }

    #sidebar.collapsed .sidebar-user {
        justify-content: flex-start;
        padding: 8px 10px;
    }

    #sidebar.collapsed .sidebar-nav {
        padding: 8px 12px;
    }

    #sidebar.collapsed .sidebar-footer {
        padding: 12px;
    }

    /* Тултипы скрываем на мобильном */
    #sidebar.collapsed .sidebar-nav-item::after,
    #sidebar.collapsed .sidebar-user::after {
        display: none;
    }

    #main {
        margin-left: 0 !important;
    }

    #sidebar.collapsed~#main {
        margin-left: 0 !important;
    }

    /* Правая панель чата — скрыта, открывается по кнопке */
    .chat-sidebar {
        display: none;
        position: fixed;
        inset: 0;
        z-index: 250;
        background: var(--surface);
        overflow-y: auto;
        animation: slideInRight .25s ease;
    }

    .chat-sidebar.open {
        display: flex;
        flex-direction: column;
    }

    @keyframes slideInRight {
        from {
            transform: translateX(100%);
        }

        to {
            transform: translateX(0);
        }
    }

    .settings-layout {
        grid-template-columns: 1fr;
        gap: 0;
    }

    .settings-nav {
        flex-direction: row;
        overflow-x: auto;
        border-right: none;
        border-bottom: 1px solid var(--border);
        padding: 0;
        gap: 0;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }

    .settings-nav::-webkit-scrollbar {
        display: none;
    }

    .settings-nav-item {
        flex-shrink: 0;
        border-radius: 0;
        border-bottom: 2px solid transparent;
        padding: 10px 14px;
        white-space: nowrap;
    }

    .settings-nav-item.active {
        border-bottom-color: var(--primary);
        background: transparent;
        color: var(--primary);
    }

    /* Чат-layout на мобильном */
    .chat-layout {
        flex-direction: column;
        height: auto;
    }

    .chat-main {
        height: calc(100svh - var(--topbar-height));
    }

    .chat-messages-wrap {
        -webkit-overflow-scrolling: touch;
    }

    /* Область ввода сообщения */
    .chat-input-wrap {
        padding-bottom: env(safe-area-inset-bottom, 8px);
    }

    #msgInput {
        font-size: 16px;
        /* предотвращает зум при фокусе на iOS */
    }

    /* Контент страниц */
    .page-content {
        padding: 12px;
    }

    /* Topbar */
    #topbar {
        padding: 0 12px;
        gap: 8px;
    }

    .topbar-title {
        font-size: 14px;
    }

    /* Таблицы — горизонтальный скролл */
    .table-wrapper,
    .table-responsive-mobile {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .data-table {
        min-width: 600px;
    }

    /* Карточки статистики — 2 колонки */
    .stat-card {
        padding: 14px;
    }

    .stat-card-value {
        font-size: 24px;
    }

    /* Кнопки — крупнее для касания */
    .btn {
        min-height: 38px;
    }

    .btn-sm {
        min-height: 32px;
    }

    /* Формы */
    .form-control,
    .form-select {
        font-size: 16px;
        /* предотвращает зум на iOS */
    }

    /* Пагинация */
    .pagination {
        flex-wrap: wrap;
        justify-content: center;
        gap: 4px;
    }

    /* Модальные окна */
    .modal-dialog {
        margin: 8px;
    }

    /* Чат-список */
    .chat-list-item {
        padding: 12px 14px;
    }

    /* Шапка чата */
    .chat-topbar {
        padding: 0 12px;
    }

    /* Топбар-экшны */
    .topbar-actions .btn-sm span {
        display: none;
    }
}

/* Очень маленькие экраны */
@media (max-width: 380px) {
    .page-content {
        padding: 8px;
    }

    .stat-card-value {
        font-size: 20px;
    }

    .topbar-title {
        font-size: 13px;
        max-width: 160px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
}

/* Планшеты (768px – 1024px) */
@media (min-width: 769px) and (max-width: 1024px) {
    :root {
        --sidebar-width: 200px;
    }

    .page-content {
        padding: 16px 20px;
    }
}

/* Поддержка safe-area (iPhone с чёлкой) */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
    #sidebar {
        padding-bottom: env(safe-area-inset-bottom);
    }

    .chat-input-wrap {
        padding-bottom: calc(8px + env(safe-area-inset-bottom));
    }
}

/* Кнопка-гамбургер — всегда видна */
#sidebarToggle {
    display: flex;
    flex-shrink: 0;
}

/* scrollbar polish */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border-hover);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}
