:root {
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --primary-light: #eef2ff;
    --sidebar-bg: #0f172a;
    --sidebar-width: 260px;
    --sidebar-active: #1e293b;
    --sidebar-text: #94a3b8;
    --sidebar-text-active: #ffffff;
    --topbar-height: 64px;
    --body-bg: #f1f5f9;
    --card-bg: #ffffff;
    --text-primary: #0f172a;
    --text-secondary: #64748b;
    --border-color: #e2e8f0;
    --success: #10b981;
    --success-light: #d1fae5;
    --warning: #f59e0b;
    --warning-light: #fef3c7;
    --danger: #ef4444;
    --danger-light: #fee2e2;
    --info: #3b82f6;
    --info-light: #dbeafe;
}

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

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--body-bg);
    color: var(--text-primary);
    font-size: 14px;
}

.fw-500 { font-weight: 500; }
.fw-600 { font-weight: 600; }
.x-small { font-size: 0.7rem; }
.text-muted-light { color: #64748b; }

/* ===== App Layout ===== */
.app-wrapper {
    display: flex;
    min-height: 100vh;
}

/* ===== Sidebar ===== */
.app-sidebar {
    width: var(--sidebar-width);
    background: var(--sidebar-bg);
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 1040;
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease;
}

.sidebar-brand {
    height: var(--topbar-height);
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0 20px;
    font-size: 1.2rem;
    font-weight: 700;
    color: #fff;
    border-bottom: 1px solid rgba(255,255,255,0.06);
}

.sidebar-brand i {
    font-size: 1.5rem;
    color: #25d366;
}

.sidebar-nav {
    flex: 1;
    overflow-y: auto;
    padding: 16px 0;
}

.nav-section {
    margin-bottom: 8px;
}

.nav-section-title {
    display: block;
    padding: 8px 20px 4px;
    font-size: 0.65rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: #475569;
}

.nav-link-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 20px;
    color: var(--sidebar-text);
    text-decoration: none;
    font-size: 0.875rem;
    font-weight: 500;
    transition: all 0.15s ease;
    border-left: 3px solid transparent;
}

.nav-link-item:hover {
    color: var(--sidebar-text-active);
    background: rgba(255,255,255,0.04);
}

.nav-link-item.active {
    color: var(--sidebar-text-active);
    background: var(--sidebar-active);
    border-left-color: var(--primary);
}

.nav-link-item i {
    font-size: 1.1rem;
    width: 20px;
    text-align: center;
}

.sidebar-footer {
    padding: 16px 20px;
    border-top: 1px solid rgba(255,255,255,0.06);
}

.sidebar-user-info {
    min-width: 0;
    overflow: hidden;
}

.sidebar-user-info div {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 1039;
}

/* ===== Main Content ===== */
.app-main {
    flex: 1;
    margin-left: var(--sidebar-width);
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.app-topbar {
    height: var(--topbar-height);
    background: var(--card-bg);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    position: sticky;
    top: 0;
    z-index: 1020;
}

.app-content {
    flex: 1;
    padding: 24px;
}

/* ===== User Avatar ===== */
.user-avatar-sm {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 0.8rem;
    flex-shrink: 0;
}

.user-avatar-lg {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 2rem;
    flex-shrink: 0;
}

.user-menu-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    text-decoration: none;
    color: var(--text-primary);
    font-weight: 500;
    font-size: 0.875rem;
}

.user-menu-btn::after {
    font-size: 0.7rem;
}

/* ===== Cards ===== */
.card {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}

.card-header {
    background: transparent;
    border-bottom: 1px solid var(--border-color);
    padding: 16px 20px;
    font-weight: 600;
}

.card-body {
    padding: 20px;
}

/* ===== Stat Cards ===== */
.stat-card {
    border-radius: 12px;
    padding: 20px;
    border: 1px solid var(--border-color);
    background: var(--card-bg);
    box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}

.stat-card .stat-icon {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.3rem;
}

.stat-card .stat-value {
    font-size: 1.75rem;
    font-weight: 700;
    line-height: 1.2;
}

.stat-card .stat-label {
    font-size: 0.8rem;
    color: var(--text-secondary);
    font-weight: 500;
}

/* ===== Status Badges ===== */
.badge-status {
    font-size: 0.7rem;
    font-weight: 600;
    padding: 4px 10px;
    border-radius: 20px;
}

.badge-connected {
    background: var(--success-light);
    color: #065f46;
}

.badge-disconnected,
.badge-logged_out {
    background: var(--danger-light);
    color: #991b1b;
}

.badge-qr_required {
    background: var(--warning-light);
    color: #92400e;
}

.badge-reconnecting {
    background: var(--info-light);
    color: #1e40af;
}

/* ===== Tables ===== */
.table-card {
    border-radius: 12px;
    overflow: hidden;
}

.table-card .table {
    margin-bottom: 0;
}

.table-card .table th {
    background: #f8fafc;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-secondary);
    border-bottom: 1px solid var(--border-color);
    padding: 12px 16px;
}

.table-card .table td {
    padding: 14px 16px;
    vertical-align: middle;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.875rem;
}

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

/* ===== Auth Pages ===== */
.auth-body {
    background: linear-gradient(135deg, #0f172a 0%, #1e293b 50%, #0f172a 100%);
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.auth-card {
    width: 100%;
    max-width: 440px;
    background: #fff;
    border-radius: 16px;
    padding: 40px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}

.auth-brand {
    text-align: center;
    margin-bottom: 32px;
}

.auth-brand i {
    font-size: 2.5rem;
    color: #25d366;
}

.auth-brand h2 {
    font-size: 1.5rem;
    font-weight: 700;
    margin-top: 8px;
    color: var(--text-primary);
}

.auth-brand p {
    color: var(--text-secondary);
    font-size: 0.875rem;
    margin-top: 4px;
}

/* ===== Form Styles ===== */
.form-control,
.form-select {
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 10px 14px;
    font-size: 0.875rem;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.form-control:focus,
.form-select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.form-label {
    font-weight: 500;
    font-size: 0.875rem;
    margin-bottom: 6px;
    color: var(--text-primary);
}

.btn-primary {
    background: var(--primary);
    border-color: var(--primary);
    border-radius: 8px;
    padding: 10px 20px;
    font-weight: 600;
    font-size: 0.875rem;
    transition: all 0.15s ease;
}

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

.btn-outline-primary {
    border-color: var(--primary);
    color: var(--primary);
    border-radius: 8px;
    padding: 10px 20px;
    font-weight: 600;
    font-size: 0.875rem;
}

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

.btn-sm {
    padding: 6px 12px;
    font-size: 0.8rem;
}

/* ===== QR Code ===== */
.qr-container {
    background: #fff;
    border: 2px dashed var(--border-color);
    border-radius: 16px;
    padding: 32px;
    text-align: center;
    min-height: 320px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.qr-container img {
    max-width: 256px;
    border-radius: 8px;
}

.qr-container .qr-status {
    margin-top: 16px;
    font-size: 0.875rem;
    font-weight: 500;
}

/* ===== Sync Status Bar ===== */
.sync-status-bar {
    background: #e8f5e9;
    border: 1px solid #c8e6c9;
    border-radius: 8px;
    padding: 8px 16px;
    margin-bottom: 12px;
    font-size: 0.82rem;
    color: #2e7d32;
}

.sync-status-bar.sync-done {
    background: #e3f2fd;
    border-color: #bbdefb;
    color: #1565c0;
}

.sync-status-inner {
    display: flex;
    align-items: center;
}

.sync-status-inner .spinner-border {
    width: 14px;
    height: 14px;
    border-width: 2px;
    color: #2e7d32;
}

.sync-done .sync-status-inner .spinner-border {
    display: none;
}

/* ===== Chat UI ===== */
.chat-wrapper {
    display: flex;
    height: calc(100vh - var(--topbar-height) - 48px);
    background: var(--card-bg);
    border-radius: 12px;
    border: 1px solid var(--border-color);
    overflow: hidden;
}

.chat-sidebar {
    width: 340px;
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.chat-sidebar-header {
    padding: 8px 12px;
    border-bottom: 1px solid var(--border-color);
    background: #fafbfc;
}

.chat-search-wrap {
    display: flex;
    align-items: center;
    gap: 8px;
    background: #f0f2f5;
    border-radius: 8px;
    padding: 6px 12px;
}

.chat-search-wrap i {
    color: var(--text-secondary);
    font-size: 0.85rem;
    flex-shrink: 0;
}

.chat-search-wrap input {
    border: none;
    background: transparent;
    outline: none;
    font-size: 0.85rem;
    color: var(--text-primary);
    width: 100%;
}

.chat-search-wrap input::placeholder {
    color: #8696a0;
}

.chat-list {
    flex: 1;
    overflow-y: auto;
}

.chat-list-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 16px;
    cursor: pointer;
    border-bottom: 1px solid #f1f3f5;
    transition: background 0.1s ease;
}

.chat-list-item:hover {
    background: #f8f9fa;
}

.chat-list-item.active {
    background: var(--primary-light);
}

.chat-list-item .chat-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 1rem;
    flex-shrink: 0;
}

.chat-list-item .chat-avatar.chat-avatar-group {
    background: #25d366;
}

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

.chat-list-item .chat-info-top {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 2px;
}

.chat-list-item .chat-name {
    font-weight: 600;
    font-size: 0.875rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--text-primary);
    flex: 1;
    min-width: 0;
}

.chat-list-item .chat-time {
    font-size: 0.7rem;
    color: var(--text-secondary);
    flex-shrink: 0;
    white-space: nowrap;
}

.chat-list-item.active .chat-time {
    color: var(--primary);
}

.chat-list-item .chat-preview {
    font-size: 0.8rem;
    color: var(--text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 1.4;
}

.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.chat-main-header {
    padding: 12px 20px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 12px;
    background: #fafbfc;
}

.chat-header-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 0.9rem;
    flex-shrink: 0;
}

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

.chat-header-name {
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

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

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px 60px;
    background: #efeae2;
    background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23d5d0c8' fill-opacity='0.15'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}

.chat-loading {
    text-align: center;
    padding: 40px;
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.chat-no-messages {
    text-align: center;
    padding: 40px;
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.date-separator {
    text-align: center;
    margin: 16px 0;
}

.date-separator span {
    background: #fff;
    color: var(--text-secondary);
    font-size: 0.75rem;
    font-weight: 500;
    padding: 5px 14px;
    border-radius: 8px;
    box-shadow: 0 1px 2px rgba(0,0,0,0.08);
    display: inline-block;
}

.msg-row {
    display: flex;
    margin-bottom: 2px;
}

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

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

.chat-bubble {
    max-width: 65%;
    padding: 6px 8px 4px;
    border-radius: 8px;
    font-size: 0.875rem;
    line-height: 1.45;
    position: relative;
    word-wrap: break-word;
}

.chat-bubble.incoming {
    background: #fff;
    color: var(--text-primary);
    border-top-left-radius: 0;
    box-shadow: 0 1px 1px rgba(0,0,0,0.06);
}

.chat-bubble.outgoing {
    background: #d9fdd3;
    color: var(--text-primary);
    border-top-right-radius: 0;
    box-shadow: 0 1px 1px rgba(0,0,0,0.06);
}

.bubble-sender {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 2px;
}

.bubble-content {
    padding: 0 4px;
}

.bubble-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 3px;
    margin-top: 1px;
    padding: 0 2px;
}

.bubble-time {
    font-size: 0.65rem;
    color: #667781;
}

.tick-mark {
    font-size: 0.75rem;
    color: #53bdeb;
}

.media-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 0;
    color: #667781;
}

.media-indicator i {
    font-size: 1rem;
}

.chat-bubble.outgoing .media-indicator {
    color: #667781;
}

.media-label {
    font-size: 0.82rem;
    font-style: italic;
}

.media-caption {
    padding: 2px 0 0;
    font-size: 0.875rem;
}

.chat-input-area {
    padding: 10px 16px;
    border-top: 1px solid var(--border-color);
    display: flex;
    gap: 10px;
    align-items: center;
    background: #f0f2f5;
}

.chat-input-area .form-control {
    flex: 1;
    border-radius: 20px;
    padding: 8px 16px;
    font-size: 0.875rem;
    border: 1px solid var(--border-color);
    background: #fff;
}

.btn-send {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    cursor: pointer;
    transition: background 0.15s ease;
    flex-shrink: 0;
}

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

.chat-empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    background: #f0f2f5;
}

.chat-empty i {
    font-size: 3rem;
    margin-bottom: 16px;
    opacity: 0.3;
}

/* ===== Contacts Toolbar ===== */
.contacts-toolbar {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 12px 16px;
}

.contacts-toolbar-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}

.contacts-toolbar-left {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    flex: 1;
    min-width: 0;
}

.contacts-toolbar-right {
    display: flex;
    align-items: center;
    flex-shrink: 0;
}

.contacts-search-wrap {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--body-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0 10px;
    min-width: 200px;
    max-width: 280px;
}

.contacts-search-wrap i {
    color: var(--text-secondary);
    font-size: 0.85rem;
    flex-shrink: 0;
}

.contacts-search-wrap .form-control {
    border: none;
    background: transparent;
    padding: 6px 0;
    box-shadow: none;
}

.contacts-search-wrap .form-control:focus {
    box-shadow: none;
}

.contacts-toolbar-left .form-select {
    max-width: 180px;
}

.sort-header {
    text-decoration: none;
    color: inherit;
    white-space: nowrap;
    display: inline-flex;
    align-items: center;
}

.sort-header:hover {
    color: var(--primary);
}

.sort-header i {
    font-size: 0.7rem;
}

@media (max-width: 767.98px) {
    .contacts-toolbar-inner {
        flex-direction: column;
        align-items: stretch;
    }
    .contacts-toolbar-right {
        justify-content: space-between;
    }
    .contacts-search-wrap {
        max-width: 100%;
        min-width: 0;
    }
    .contacts-toolbar-left .form-select {
        max-width: 100%;
    }
}

/* ===== Media in Chat Bubbles ===== */
.media-image { max-width: 300px; border-radius: 8px; overflow: hidden; margin: -4px -6px 4px; }
.media-image img { width: 100%; display: block; cursor: pointer; border-radius: 8px; }
.media-sticker { max-width: 180px; margin: -4px -6px 4px; }
.media-sticker img { width: 100%; display: block; }
.media-video { max-width: 320px; border-radius: 8px; overflow: hidden; margin: -4px -6px 4px; }
.media-video video { width: 100%; display: block; border-radius: 8px; max-height: 280px; }
.media-audio { display: flex; align-items: center; min-width: 240px; }
.media-audio audio { flex: 1; height: 36px; }
.media-audio i { font-size: 1.3rem; color: #128c7e; }
.media-voice { background: rgba(0,0,0,0.03); border-radius: 20px; padding: 6px 12px; }

.media-document {
    display: flex; align-items: center; gap: 10px;
    background: rgba(0,0,0,0.04); border-radius: 8px; padding: 10px 14px;
    text-decoration: none; color: inherit; margin: -2px -4px 4px;
    transition: background 0.15s;
}
.media-document:hover { background: rgba(0,0,0,0.08); color: inherit; }
.media-document > i { font-size: 1.6rem; color: #128c7e; flex-shrink: 0; }
.media-doc-info { display: flex; flex-direction: column; min-width: 0; }
.media-doc-name { font-size: 0.85rem; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 200px; }
.media-doc-type { font-size: 0.7rem; color: #888; text-transform: uppercase; }

.media-contacts { display: flex; flex-direction: column; gap: 6px; }
.media-contact-card {
    display: flex; align-items: center; gap: 10px;
    background: rgba(0,0,0,0.04); border-radius: 8px; padding: 10px 12px;
}
.media-contact-card > i { font-size: 1.8rem; color: #128c7e; }
.media-contact-card strong { font-size: 0.85rem; }
.media-contact-card small { color: #666; font-size: 0.78rem; }

.media-location {
    display: block; text-decoration: none; color: inherit;
    background: rgba(0,0,0,0.04); border-radius: 8px; overflow: hidden;
    margin: -2px -4px 4px;
}
.media-location:hover { color: inherit; }
.media-location > img { width: 100%; max-width: 300px; display: block; }
.media-loc-map { background: #e8f5e9; padding: 24px; text-align: center; font-size: 2.5rem; color: #e74c3c; }
.media-loc-info { padding: 8px 12px; font-size: 0.85rem; }
.media-loc-info i { color: #e74c3c; }
.media-loc-info small { color: #666; }

.media-caption { margin-top: 4px; font-size: 0.875rem; }

.media-lightbox {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.85); z-index: 9999;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer;
}
.media-lightbox img {
    max-width: 90vw; max-height: 90vh; object-fit: contain;
    border-radius: 4px; cursor: default;
}

/* ===== Alerts ===== */
.alert {
    border-radius: 10px;
    font-size: 0.875rem;
    border: none;
}

/* ===== Empty State ===== */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-secondary);
}

.empty-state i {
    font-size: 3rem;
    opacity: 0.3;
    margin-bottom: 16px;
}

.empty-state h5 {
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 8px;
}

.empty-state p {
    font-size: 0.875rem;
    max-width: 400px;
    margin: 0 auto 20px;
}

/* ===== Responsive ===== */
@media (max-width: 991.98px) {
    .app-sidebar {
        transform: translateX(-100%);
    }
    .app-sidebar.show {
        transform: translateX(0);
    }
    .sidebar-overlay.show {
        display: block;
    }
    .app-main {
        margin-left: 0;
    }
    .chat-sidebar {
        width: 100%;
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        z-index: 10;
        background: #fff;
    }
}

@media (max-width: 767.98px) {
    .app-content {
        padding: 16px;
    }
    .auth-card {
        margin: 16px;
        padding: 24px;
    }
    .chat-wrapper {
        height: calc(100vh - var(--topbar-height) - 32px);
    }
}
