.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    animation: fadeIn 0.2s ease;
}

.modal {
    background: var(--color-surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    max-width: 480px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    animation: slideUp 0.2s ease;
}

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

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 24px;
    border-bottom: 1px solid var(--color-border);
}

.modal-header h2 {
    font-size: 18px;
    font-weight: 600;
}

.modal-close {
    width: 36px;
    height: 36px;
    border-radius: var(--radius-full);
    border: none;
    background: transparent;
    color: var(--color-text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-close:hover {
    background: var(--color-surface-hover);
    color: var(--color-text);
}

.modal-body {
    padding: 24px;
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    padding: 16px 24px;
    border-top: 1px solid var(--color-border);
}

.user-search-result {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: background var(--transition-fast);
}

.user-search-result:hover {
    background: var(--color-surface-hover);
}

.user-search-avatar {
    flex-shrink: 0;
}

.user-search-info {
    flex: 1;
    min-width: 0;
}

.user-search-name {
    font-size: 14px;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-search-status {
    flex-shrink: 0;
}

.settings-section {
    margin-bottom: 24px;
}

.settings-section-title {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--color-text-muted);
    margin-bottom: 12px;
}

.setting-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 0;
    border-bottom: 1px solid var(--color-border);
}

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

.setting-info {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.setting-label {
    font-size: 14px;
    font-weight: 500;
}

.setting-description {
    font-size: 12px;
    color: var(--color-text-secondary);
}

.toggle {
    position: relative;
    width: 48px;
    height: 28px;
}

.toggle input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: var(--color-border);
    transition: .3s;
    border-radius: 28px;
}

.toggle-slider:before {
    position: absolute;
    content: "";
    height: 22px;
    width: 22px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .3s;
    border-radius: 50%;
    box-shadow: var(--shadow-sm);
}

.toggle input:checked + .toggle-slider {
    background-color: var(--color-primary);
}

.toggle input:checked + .toggle-slider:before {
    transform: translateX(20px);
}

.profile-form {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.profile-avatar-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
}

.profile-avatar-preview {
    width: 100px;
    height: 100px;
    border-radius: var(--radius-full);
    object-fit: cover;
    border: 3px solid var(--color-border);
}

.profile-avatar-upload {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    background: var(--color-surface);
    color: var(--color-text);
    font-size: 13px;
    cursor: pointer;
    transition: border-color var(--transition-fast);
}

.profile-avatar-upload:hover {
    border-color: var(--color-primary);
}

.profile-avatar-upload input {
    display: none;
}

.notification-item {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding: 12px;
    border-radius: var(--radius-md);
    transition: background var(--transition-fast);
}

.notification-item:hover {
    background: var(--color-surface-hover);
}

.notification-item.unread {
    background: var(--color-primary-light);
}

.notification-icon {
    width: 36px;
    height: 36px;
    border-radius: var(--radius-full);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.notification-icon.message {
    background: var(--color-primary-light);
    color: var(--color-primary);
}

.notification-icon.friend {
    background: var(--color-success);
    color: white;
}

.notification-icon.system {
    background: var(--color-warning);
    color: white;
}

.notification-content {
    flex: 1;
    min-width: 0;
}

.notification-title {
    font-size: 14px;
    font-weight: 500;
    margin-bottom: 2px;
}

.notification-body {
    font-size: 13px;
    color: var(--color-text-secondary);
    margin-bottom: 4px;
}

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

.file-upload-progress {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    margin-bottom: 12px;
}

.file-upload-info {
    flex: 1;
    min-width: 0;
}

.file-upload-name {
    font-size: 13px;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.file-upload-bar {
    height: 4px;
    background: var(--color-border);
    border-radius: 2px;
    overflow: hidden;
    flex: 1;
    max-width: 200px;
}

.file-upload-fill {
    height: 100%;
    background: var(--color-primary);
    border-radius: 2px;
    transition: width 0.3s ease;
}

.file-upload-cancel {
    width: 28px;
    height: 28px;
    border-radius: var(--radius-full);
    border: none;
    background: transparent;
    color: var(--color-text-secondary);
    cursor: pointer;
}

.file-upload-cancel:hover {
    background: var(--color-surface-hover);
    color: var(--color-danger);
}

.connection-status {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px;
    border-radius: var(--radius-full);
    font-size: 11px;
    font-weight: 500;
}

.status-connected {
    background: #dcfce7;
    color: #166534;
}

.status-connecting {
    background: #fef9c3;
    color: #854d0e;
}

.status-reconnecting {
    background: #ffedd5;
    color: #9a3412;
}

.status-offline {
    background: #fef2f2;
    color: #991b1b;
}

.status-relay {
    background: #e0e7ff;
    color: #3730a3;
}

.toast {
    position: fixed;
    bottom: 24px;
    right: 24px;
    padding: 12px 20px;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    z-index: 2000;
    animation: slideIn 0.3s ease;
    display: flex;
    align-items: center;
    gap: 12px;
    max-width: 360px;
}

.toast.success {
    background: var(--color-success);
    color: white;
}

.toast.error {
    background: var(--color-danger);
    color: white;
}

.toast.info {
    background: var(--color-text);
    color: white;
}

@keyframes slideIn {
    from { opacity: 0; transform: translateX(100%); }
    to { opacity: 1; transform: translateX(0); }
}

.toast-dismiss {
    background: none;
    border: none;
    color: inherit;
    opacity: 0.7;
    cursor: pointer;
    padding: 4px;
    margin-left: 8px;
}

.toast-dismiss:hover {
    opacity: 1;
}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-menu {
    position: absolute;
    top: 100%;
    right: 0;
    margin-top: 8px;
    min-width: 180px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    padding: 8px 0;
    z-index: 100;
    opacity: 0;
    visibility: hidden;
    transform: translateY(-10px);
    transition: opacity var(--transition-fast), transform var(--transition-fast), visibility var(--transition-fast);
}

.dropdown.open .dropdown-menu {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.dropdown-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 10px 16px;
    border: none;
    background: transparent;
    color: var(--color-text);
    font-size: 13px;
    text-align: left;
    cursor: pointer;
    transition: background var(--transition-fast);
}

.dropdown-item:hover {
    background: var(--color-surface-hover);
}

.dropdown-item.danger {
    color: var(--color-danger);
}

.dropdown-item svg {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

.dropdown-divider {
    height: 1px;
    background: var(--color-border);
    margin: 8px 0;
}

.emoji-picker {
    position: absolute;
    bottom: 100%;
    left: 0;
    margin-bottom: 8px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    padding: 12px;
    z-index: 100;
    width: 320px;
}

.emoji-picker.hidden {
    display: none;
}

.emoji-categories {
    display: flex;
    gap: 4px;
    margin-bottom: 12px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--color-border);
    overflow-x: auto;
}

.emoji-category {
    padding: 6px 10px;
    border: none;
    background: transparent;
    border-radius: var(--radius-sm);
    cursor: pointer;
    color: var(--color-text-secondary);
    font-size: 18px;
    transition: background var(--transition-fast), color var(--transition-fast);
}

.emoji-category.active {
    background: var(--color-primary-light);
    color: var(--color-primary);
}

.emoji-grid {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    gap: 4px;
    max-height: 200px;
    overflow-y: auto;
}

.emoji-btn {
    aspect-ratio: 1;
    border: none;
    background: transparent;
    font-size: 20px;
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: background var(--transition-fast);
}

.emoji-btn:hover {
    background: var(--color-surface-hover);
}

.context-menu {
    position: fixed;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    padding: 8px 0;
    z-index: 2000;
    min-width: 160px;
}

.context-menu.hidden {
    display: none;
}

.context-menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 10px 16px;
    border: none;
    background: transparent;
    color: var(--color-text);
    font-size: 13px;
    text-align: left;
    cursor: pointer;
}

.context-menu-item:hover {
    background: var(--color-surface-hover);
}

.context-menu-item.danger {
    color: var(--color-danger);
}

.context-menu-divider {
    height: 1px;
    background: var(--color-border);
    margin: 8px 0;
}
/* ---- Rich messaging: context menu, reply, select, search, emoji, voice ---- */
.context-menu {
    position: fixed;
    z-index: 1000;
    min-width: 200px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
    padding: 6px;
}
.ctx-reactions {
    display: flex;
    gap: 2px;
    padding: 4px 4px 8px;
    border-bottom: 1px solid var(--color-border);
    margin-bottom: 4px;
    flex-wrap: wrap;
}
.ctx-react {
    background: transparent;
    border: none;
    font-size: 20px;
    cursor: pointer;
    padding: 4px;
    border-radius: var(--radius-sm);
}
.ctx-react:hover { background: var(--color-surface-hover); }
.ctx-item {
    display: block;
    width: 100%;
    background: transparent;
    border: none;
    color: var(--color-text);
    font-size: 14px;
    text-align: left;
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    cursor: pointer;
}
.ctx-item:hover { background: var(--color-surface-hover); }
.ctx-item.danger { color: var(--color-danger); }

.reply-quote {
    border-left: 3px solid var(--color-primary);
    padding: 4px 8px;
    margin-bottom: 6px;
    background: rgba(0, 0, 0, 0.06);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    cursor: pointer;
    font-size: 12px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.reply-quote-sender { font-weight: 600; color: var(--color-primary); }
.reply-quote-text { color: var(--color-text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 260px; }

.reply-preview {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-top: 1px solid var(--color-border);
    background: var(--color-surface);
}
.reply-preview-body { flex: 1; border-left: 3px solid var(--color-primary); padding-left: 8px; min-width: 0; }
.reply-preview-sender { font-size: 12px; font-weight: 600; color: var(--color-primary); }
.reply-preview-text { font-size: 13px; color: var(--color-text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.message.flash .message-bubble { outline: 2px solid var(--color-primary); transition: outline 0.3s; }
.message.selected .message-bubble { outline: 2px solid var(--color-primary); }

.select-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 8px 12px;
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
    font-size: 14px;
}
.select-actions { display: flex; gap: 6px; align-items: center; }

.message-search-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
}
.message-search-bar input {
    flex: 1;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    padding: 6px 10px;
    background: var(--color-background);
    color: var(--color-text);
}
.search-count { font-size: 12px; color: var(--color-text-muted); white-space: nowrap; }

.emoji-picker {
    position: absolute;
    bottom: 64px;
    left: 12px;
    z-index: 500;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
    padding: 10px;
    max-width: 300px;
}
.emoji-group-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--color-text-muted); margin: 4px 2px; }
.emoji-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; }
.emoji-btn-item { background: transparent; border: none; font-size: 22px; cursor: pointer; padding: 4px; border-radius: var(--radius-sm); }
.emoji-btn-item:hover { background: var(--color-surface-hover); }

.voice-preview, .recording-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-top: 1px solid var(--color-border);
    background: var(--color-surface);
}
.voice-preview audio { flex: 1; min-width: 0; height: 36px; }
.voice-duration { font-size: 13px; color: var(--color-text-muted); }
.rec-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--color-danger); animation: recpulse 1s infinite; }
@keyframes recpulse { 50% { opacity: 0.3; } }
.rec-hint { font-size: 13px; color: var(--color-text-muted); flex: 1; }
@media (prefers-reduced-motion: reduce) {
    .rec-dot { animation: none; }
    .message { scroll-behavior: auto; }
}

/* ---- Call overlay — WhatsApp-style full-screen ---- */
.call-overlay {
    position: fixed;
    inset: 0;
    z-index: 900;
    background: #111b2f;
    display: flex;
    flex-direction: column;
}
.call-overlay.hidden { display: none !important; }
.call-wa-top {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    color: #fff;
    background: linear-gradient(to bottom, rgba(0,0,0,0.35), transparent);
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 2;
}
.call-wa-icon {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: none;
    background: rgba(255,255,255,0.14);
    color: #fff;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}
.call-wa-icon svg { width: 20px; height: 20px; }
.call-wa-top-info { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#call-name { font-size: 16px; font-weight: 600; color: #fff; line-height: 1.2; }
#call-status { font-size: 13px; color: #cbd5e1; }
#call-timer { font-variant-numeric: tabular-nums; color: #e2e8f0; font-size: 13px; }
.call-wa-stage {
    flex: 1;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #0b1220;
    overflow: hidden;
}
#call-remote-video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    background: #000;
}
.call-remote-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    color: #94a3b8;
}
.call-remote-placeholder.hidden { display: none !important; }
.call-remote-placeholder svg { width: 42px; height: 42px; }
.call-avatar {
    width: 110px;
    height: 110px;
    border-radius: 50%;
    object-fit: cover;
    border: 3px solid rgba(255,255,255,0.18);
    background: #1e293b;
}
#call-local-video {
    position: absolute;
    bottom: 96px;
    right: 16px;
    width: 108px;
    height: 144px;
    object-fit: cover;
    border-radius: 12px;
    border: 2px solid rgba(255,255,255,0.9);
    box-shadow: 0 8px 24px rgba(0,0,0,0.45);
    background: #000;
}
#call-local-video.hidden { display: none !important; }
#call-remote-audio { display: none; }
.call-wa-controls {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
    display: flex;
    justify-content: center;
    background: linear-gradient(to top, rgba(0,0,0,0.45), transparent);
}
.call-wa-actions { display: flex; gap: 14px; align-items: center; justify-content: center; position: relative; z-index: 1; }
.call-wa-actions.hidden { display: none !important; }
.call-wa-btn {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    border: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    background: rgba(255,255,255,0.14);
    color: #fff;
    backdrop-filter: blur(8px);
    transition: transform 120ms ease, background 120ms ease;
}
.call-wa-btn:active { transform: scale(0.96); }
.call-wa-btn svg { width: 24px; height: 24px; }
.call-wa-btn.accept { background: #22c55e; color: #fff; }
.call-wa-btn.reject { background: #ef4444; color: #fff; }
.call-wa-btn.muted { background: #ef4444; color: #fff; }
.call-wa-secure {
    position: absolute;
    bottom: 2px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 11px;
    color: rgba(255,255,255,0.7);
    letter-spacing: 0.02em;
    display: flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
    pointer-events: none;
}

/* Username under the peer name in the call header. */
.call-username {
    font-size: 11px;
    font-weight: 400;
    color: rgba(255,255,255,0.65);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Minimized-call pill: hides the overlay without ending the call. */
.call-pill {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    bottom: calc(18px + env(safe-area-inset-bottom));
    z-index: 950;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    border: none;
    border-radius: 9999px;
    background: #111b2f;
    color: #fff;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 8px 24px rgba(0,0,0,0.35);
}

.call-pill.hidden {
    display: none !important;
}

.call-pill-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #22c55e;
    box-shadow: 0 0 0 4px rgba(34,197,94,0.18);
}

.call-pill-name {
    max-width: 140px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.call-pill-timer {
    font-variant-numeric: tabular-nums;
    color: #cbd5e1;
    font-weight: 500;
}
@media (max-width: 480px) {
    #call-local-video { width: 96px; height: 128px; bottom: 88px; right: 12px; }
    .call-wa-btn { width: 52px; height: 52px; }
}

/* ---- Chat prefs: font size + compact ---- */
body[data-font-size="small"] .message-bubble { font-size: 12.5px; }
body[data-font-size="medium"] .message-bubble { font-size: 14px; }
body[data-font-size="large"] .message-bubble { font-size: 16px; }
body.compact .message { margin-bottom: 2px; }
body.compact .message-bubble { padding: 6px 10px; }
body.compact .date-divider { margin: 8px 0; }

/* ---- Unread divider + skeletons ---- */
.unread-divider {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 10px 0;
    color: var(--color-primary);
    font-size: 12px;
    font-weight: 600;
}
.unread-divider::before, .unread-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--color-primary);
    opacity: 0.4;
}
.unread-divider span { white-space: nowrap; }
.skeleton {
    border-radius: var(--radius-md);
    background: linear-gradient(90deg, var(--color-surface-hover) 25%, var(--color-border) 50%, var(--color-surface-hover) 75%);
    background-size: 200% 100%;
    animation: skshimmer 1.2s infinite;
}
@keyframes skshimmer { to { background-position: -200% 0; } }
.skeleton-row { display: flex; gap: 10px; padding: 10px 12px; align-items: center; }
.skeleton-avatar { width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0; }
.skeleton-lines { flex: 1; display: flex; flex-direction: column; gap: 6px; }
.skeleton-line { height: 12px; }
@media (prefers-reduced-motion: reduce) {
    .skeleton { animation: none; }
}
/* ---- Shared media grid ---- */
.shared-media-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 8px; }
.shared-media-grid img, .shared-media-grid video { width: 100%; height: 90px; object-fit: cover; border-radius: var(--radius-sm); }
.shared-media-file { font-size: 12px; padding: 8px; border: 1px solid var(--color-border); border-radius: var(--radius-sm); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.filter-chips { display: flex; gap: 6px; padding: 0 12px 8px; }
.filter-chip { border: 1px solid var(--color-border); background: var(--color-surface); color: var(--color-text-secondary); border-radius: var(--radius-full); padding: 4px 12px; font-size: 12px; cursor: pointer; }
.filter-chip.active { background: var(--color-primary); border-color: var(--color-primary); color: #fff; }
