/* ═══════════════════════════════════════════════════════════════════════
   Axis Puls Messenger — Rebuilt UI (v2)
   Modern, calm, delightful chat interface with full Persian RTL support.
   Contract: every id / class / data-attribute touched by utils.js, api.js,
   ui.js and app.js is preserved — restyle freely, never rename hooks.
   ═══════════════════════════════════════════════════════════════════════ */

/* ── Design tokens ─────────────────────────────────────────────────── */
:root {
    --chat-sidebar-w: 375px;
    --chat-header-h: 74px;
    --chat-radius-xl: 24px;
    --chat-radius-lg: 18px;
    --chat-radius-md: 14px;
    --chat-radius-sm: 10px;

    /* Dark (default) */
    --bg-base: #060e18;
    --bg-surface: #0a1626;
    --bg-surface-elevated: #0f1f35;
    --bg-glass: rgba(10, 22, 38, 0.78);
    --bg-glass-strong: rgba(7, 16, 28, 0.92);
    --bg-glass-subtle: rgba(255, 255, 255, 0.04);

    --text-main: #f3f6fb;
    --text-muted: #8a9bb5;
    --text-subtle: #5a6e8b;

    --border-subtle: rgba(255, 255, 255, 0.07);
    --border-medium: rgba(255, 255, 255, 0.12);
    --border-focus: rgba(139, 92, 246, 0.65);

    --primary: #8b5cf6;
    --primary-light: #a78bfa;
    --primary-dark: #6d28d9;
    --primary-glow: rgba(139, 92, 246, 0.35);
    --primary-dim: rgba(139, 92, 246, 0.12);

    --accent-cyan: #38bdf8;
    --accent-cyan-glow: rgba(56, 189, 248, 0.35);
    --success: #10b981;
    --success-glow: rgba(16, 185, 129, 0.35);
    --danger: #f43f5e;
    --warning: #f59e0b;

    --msg-out-bg: linear-gradient(135deg, #7c3aed 0%, #6366f1 100%);
    --msg-out-border: rgba(196, 181, 253, 0.22);
    --msg-out-text: #ffffff;
    --msg-in-bg: rgba(15, 31, 53, 0.85);
    --msg-in-border: rgba(255, 255, 255, 0.08);
    --msg-in-text: #f3f6fb;

    --shadow-app: 0 25px 70px rgba(0, 0, 0, 0.45);
    --shadow-card: 0 10px 30px rgba(0, 0, 0, 0.25);
    --shadow-glow: 0 0 25px var(--primary-glow);
    --transition-fast: 0.18s cubic-bezier(0.4, 0, 0.2, 1);
    --transition-smooth: 0.28s cubic-bezier(0.4, 0, 0.2, 1);

    /* Local extras (chat-only, always with fallback) */
    --chat-danger-dim: rgba(244, 63, 94, 0.12);
    --chat-composer-h: 66px;
}

[data-theme="light"] {
    --bg-base: #f1f5f9;
    --bg-surface: #ffffff;
    --bg-surface-elevated: #f8fafc;
    --bg-glass: rgba(255, 255, 255, 0.85);
    --bg-glass-strong: rgba(255, 255, 255, 0.95);
    --bg-glass-subtle: rgba(15, 23, 42, 0.035);

    --text-main: #0f172a;
    --text-muted: #64748b;
    --text-subtle: #94a3b8;

    --border-subtle: rgba(15, 23, 42, 0.08);
    --border-medium: rgba(15, 23, 42, 0.14);
    --border-focus: rgba(124, 58, 237, 0.6);

    --primary: #7c3aed;
    --primary-light: #8b5cf6;
    --primary-dark: #5b21b6;
    --primary-glow: rgba(124, 58, 237, 0.22);
    --primary-dim: rgba(124, 58, 237, 0.08);

    --msg-out-bg: linear-gradient(135deg, #7c3aed 0%, #6366f1 100%);
    --msg-out-border: rgba(124, 58, 237, 0.2);
    --msg-out-text: #ffffff;
    --msg-in-bg: #ffffff;
    --msg-in-border: rgba(15, 23, 42, 0.08);
    --msg-in-text: #0f172a;

    --shadow-app: 0 20px 60px rgba(15, 23, 42, 0.12);
    --shadow-card: 0 8px 24px rgba(15, 23, 42, 0.06);
    --chat-danger-dim: rgba(244, 63, 94, 0.08);
}

/* ── Reset & base ──────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { width: 100%; height: 100%; overflow: hidden; }

body {
    font-family: 'Vazirmatn', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    color: var(--text-main);
    background: var(--bg-base);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    /* Pinned so the OS keyboard can never scroll the page behind the app;
       the shell is resized to the visual viewport (--app-vh) by JS. */
    position: fixed;
    inset: 0;
    overscroll-behavior: none;
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: transparent; -webkit-tap-highlight-color: transparent; }
button:focus-visible, a:focus-visible, input:focus-visible, textarea:focus-visible {
    outline: 2px solid var(--primary-light);
    outline-offset: 2px;
}
img { -webkit-user-drag: none; }

/* ── Shell ─────────────────────────────────────────────────────────── */
.chat-app {
    display: flex;
    width: 100%;
    /* --app-vh mirrors the visual viewport (setupViewportFit in app.js) so
       the composer stays visible above the mobile keyboard. */
    height: var(--app-vh, 100dvh);
    max-height: 100dvh;
    /* The top offset follows the keyboard ONLY while open (rule below).
       Default 0: a stale --app-vh-top (missed visualViewport event, common
       in iOS standalone) must never bury the composer below the fold. */
    top: 0;
    max-width: 1600px;
    margin: 0 auto;
    overflow: hidden;
    position: relative;
    background: var(--bg-surface);
    box-shadow: var(--shadow-app);
}

html.kb-open .chat-app {
    top: var(--app-vh-top, 0px);
}

@media (min-width: 860px) {
    .chat-app {
        height: calc(var(--app-vh, 100dvh) - 24px);
        max-height: calc(100dvh - 24px);
        margin-top: 12px;
        margin-bottom: 12px;
        border-radius: var(--chat-radius-xl);
        border: 1px solid var(--border-subtle);
    }
}

/* ── Generic controls (chat-local refinements) ─────────────────────── */
.btn-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 38px;
    min-height: 38px;
    border-radius: var(--chat-radius-sm);
    color: var(--text-muted);
    transition: var(--transition-fast);
    touch-action: manipulation;
    flex: none;
}
.btn-icon:hover { color: var(--text-main); background: var(--bg-glass-subtle); }
.btn-icon:active { transform: scale(0.92); }

.btn-small { padding: 7px 14px; font-size: 12px; border-radius: 10px; }

.input-field {
    width: 100%;
    padding: 11px 14px;
    border-radius: var(--chat-radius-md);
    border: 1px solid var(--border-subtle);
    background: var(--bg-surface-elevated);
    color: var(--text-main);
    font-size: 14px;
    outline: 0;
    transition: var(--transition-fast);
}
.input-field:focus { border-color: var(--border-focus); box-shadow: 0 0 0 3px var(--primary-dim); }
.input-field::placeholder { color: var(--text-subtle); }

.form-group { margin-bottom: 14px; }
.form-label { display: block; font-size: 12.5px; font-weight: 700; color: var(--text-muted); margin-bottom: 7px; }

/* ── Sidebar ───────────────────────────────────────────────────────── */
.sidebar {
    width: var(--chat-sidebar-w);
    flex: 0 0 var(--chat-sidebar-w);
    min-width: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg-glass);
    border-left: 1px solid var(--border-subtle);
    position: relative;
    z-index: 20;
    backdrop-filter: blur(28px);
    -webkit-backdrop-filter: blur(28px);
}

[dir="ltr"] .sidebar { border-left: none; border-right: 1px solid var(--border-subtle); }

.sidebar::before {
    content: "";
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 190px;
    background: radial-gradient(circle at 50% -20%, var(--primary-glow), transparent 70%);
    pointer-events: none;
    z-index: 0;
}
.sidebar > * { position: relative; z-index: 1; }

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: calc(14px + env(safe-area-inset-top, 0px)) 14px 10px;
}

.sidebar-title { display: flex; align-items: center; gap: 10px; min-width: 0; }

.sidebar-brand { display: flex; align-items: baseline; gap: 7px; min-width: 0; }
.brand-title { font-size: 17px; font-weight: 800; letter-spacing: -0.2px; white-space: nowrap; }
.brand-badge {
    font-size: 10px;
    font-weight: 800;
    padding: 2px 9px;
    border-radius: 999px;
    color: #fff;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    box-shadow: 0 3px 10px var(--primary-glow);
    white-space: nowrap;
}

.sidebar-header-actions { display: flex; align-items: center; gap: 6px; }

.header-action-btn { width: 38px; height: 38px; border-radius: 12px; }
.header-action-btn.primary-glow {
    color: var(--primary-light);
    background: var(--primary-dim);
    border: 1px solid rgba(139, 92, 246, 0.28);
}
.header-action-btn.primary-glow:hover { box-shadow: var(--shadow-glow); color: var(--text-main); }

.theme-toggle {
    width: 38px; height: 38px;
    border-radius: 12px;
    display: inline-flex; align-items: center; justify-content: center;
    font-size: 17px;
    color: var(--text-main);
    background: var(--bg-glass-subtle);
    border: 1px solid var(--border-subtle);
    transition: var(--transition-fast);
    flex: none;
}
.theme-toggle:hover { border-color: var(--border-medium); transform: rotate(12deg); }

/* Sidebar search */
.sidebar-search { padding: 2px 14px 8px; }

.search-field-wrapper { position: relative; }

.search-icon {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    inset-inline-start: 13px;
    color: var(--text-subtle);
    pointer-events: none;
}

.search-field {
    width: 100%;
    height: 42px;
    padding: 0 40px 0 36px;
    outline: 0;
    border: 1px solid var(--border-subtle);
    border-radius: var(--chat-radius-md);
    background: var(--bg-surface-elevated);
    color: var(--text-main);
    font-size: 13px;
    transition: var(--transition-fast);
}
[dir="ltr"] .search-field { padding: 0 36px 0 40px; }
.search-field::placeholder { color: var(--text-subtle); }
.search-field:focus { border-color: var(--border-focus); box-shadow: 0 0 0 3px var(--primary-dim); }

.btn-clear-search {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    inset-inline-end: 8px;
    width: 26px; height: 26px;
    border-radius: 8px;
    display: flex; align-items: center; justify-content: center;
    font-size: 12px;
    color: var(--text-muted);
}
.btn-clear-search:hover { background: var(--bg-glass-subtle); color: var(--text-main); }

/* Filter chips */
.conv-filters {
    display: flex;
    gap: 7px;
    padding: 2px 14px 10px;
    overflow-x: auto;
    scrollbar-width: none;
}
.conv-filters::-webkit-scrollbar { display: none; }

.filter-chip {
    flex: none;
    padding: 7px 15px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 700;
    color: var(--text-muted);
    background: transparent;
    border: 1px solid var(--border-subtle);
    transition: var(--transition-fast);
}
.filter-chip:hover { color: var(--text-main); border-color: var(--border-medium); }
.filter-chip.active {
    color: #fff;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    border-color: transparent;
    box-shadow: 0 4px 14px var(--primary-glow);
}

/* Conversation list */
.conv-list {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 2px 10px calc(14px + env(safe-area-inset-bottom, 0px));
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.conv-item {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    text-align: start;
    padding: 11px 12px;
    border-radius: var(--chat-radius-md);
    border: 1px solid transparent;
    transition: var(--transition-fast);
    position: relative;
}
.conv-item:hover { background: var(--bg-glass-subtle); }
.conv-item.active {
    background: var(--primary-dim);
    border-color: rgba(139, 92, 246, 0.3);
}

.conv-avatar {
    width: 52px; height: 52px;
    flex: none;
    border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 19px; font-weight: 800;
    color: #fff;
    background: linear-gradient(135deg, var(--primary), var(--accent-cyan));
    position: relative;
    overflow: hidden;
}

.conv-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.conv-top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.conv-name {
    font-size: 14px; font-weight: 750;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-time { font-size: 10.5px; color: var(--text-subtle); flex: none; }
.conv-bottom { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.conv-last-msg {
    font-size: 12px; color: var(--text-muted);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    flex: 1; min-width: 0;
}
.conv-unread {
    flex: none;
    min-width: 21px; height: 21px;
    padding: 0 6px;
    border-radius: 999px;
    display: flex; align-items: center; justify-content: center;
    font-size: 11px; font-weight: 800;
    color: #fff;
    background: linear-gradient(135deg, var(--success), #0d9488);
    box-shadow: 0 3px 10px var(--success-glow);
}

/* Avatars (list, header, members, search) */
.avatar-img { width: 100%; height: 100%; object-fit: cover; border-radius: inherit; }
.avatar-initials {
    width: 100%; height: 100%;
    display: flex; align-items: center; justify-content: center;
    font-weight: 800;
    border-radius: inherit;
    background: linear-gradient(135deg, var(--primary), var(--accent-cyan));
    color: #fff;
}
.online-dot {
    position: absolute;
    bottom: 1px; inset-inline-end: 1px;
    width: 13px; height: 13px;
    border-radius: 50%;
    background: var(--success);
    border: 2.5px solid var(--bg-surface);
}

/* Empty states */
.empty-state {
    display: flex; flex-direction: column; align-items: center;
    text-align: center;
    gap: 8px;
    padding: 48px 24px;
    color: var(--text-muted);
}
.empty-icon-wrap {
    width: 76px; height: 76px;
    border-radius: 26px;
    display: grid; place-items: center;
    color: var(--primary-light);
    background: var(--primary-dim);
    border: 1px solid rgba(139, 92, 246, 0.25);
    margin-bottom: 8px;
}
.empty-title { font-size: 15px; font-weight: 800; color: var(--text-main); }
.empty-desc { font-size: 12.5px; line-height: 1.7; max-width: 260px; }

/* Skeleton loaders */
.skeleton-msg { display: flex; gap: 10px; padding: 10px 14px; align-items: flex-end; }
.skeleton-msg.out { flex-direction: row-reverse; }
.skeleton-avatar { width: 34px; height: 34px; border-radius: 50%; flex: none; }
.skeleton-bubble { display: flex; flex-direction: column; gap: 8px; flex: 1; max-width: 70%; }
.skeleton-avatar, .skeleton-line {
    background: linear-gradient(90deg, var(--bg-glass-subtle) 25%, var(--border-medium) 50%, var(--bg-glass-subtle) 75%);
    background-size: 200% 100%;
    animation: skelShimmer 1.3s ease-in-out infinite;
}
.skeleton-line { height: 13px; border-radius: 7px; }
.skeleton-bubble { padding: 14px; border-radius: var(--chat-radius-lg); background: var(--bg-glass-subtle); border: 1px solid var(--border-subtle); }
@keyframes skelShimmer {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* ── Notification banner ───────────────────────────────────────────── */
.notif-banner {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 12px 18px;
    margin: 0;
    position: absolute;
    top: calc(10px + env(safe-area-inset-top, 0px));
    left: 12px; right: 12px;
    z-index: 60;
    border-radius: var(--chat-radius-lg);
    background: linear-gradient(135deg, rgba(139, 92, 246, 0.16), rgba(56, 189, 248, 0.12)), var(--bg-glass-strong);
    border: 1px solid rgba(139, 92, 246, 0.35);
    color: var(--text-main);
    box-shadow: 0 12px 36px rgba(0, 0, 0, 0.35);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    animation: bannerPop 0.3s ease;
}
@keyframes bannerPop {
    from { opacity: 0; transform: translateY(-10px); }
    to { opacity: 1; transform: translateY(0); }
}
.notif-banner-icon { flex: none; }
.notif-icon-pulse { font-size: 24px; animation: notifPulse 2s infinite; }
@keyframes notifPulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.15); }
}
.notif-banner-text { flex: 1; min-width: 0; font-size: 12.5px; line-height: 1.6; }
.notif-banner-text strong { display: block; font-size: 13.5px; }
.notif-banner-actions { display: flex; align-items: center; gap: 8px; flex: none; }
.notif-banner-close {
    flex: none;
    width: 30px; height: 30px;
    border-radius: 9px;
    display: grid; place-items: center;
    font-size: 13px;
    color: var(--text-muted);
    background: var(--bg-glass-subtle);
    border: 1px solid var(--border-subtle);
}
.notif-banner-close:hover { color: var(--text-main); background: var(--border-medium); }

/* ── Main / view ───────────────────────────────────────────────────── */
.chat-main {
    flex: 1;
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
    position: relative;
    overflow: hidden;
    background: var(--bg-surface);
}

.chat-empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background:
        radial-gradient(circle at 50% 38%, var(--primary-dim) 0%, transparent 62%),
        linear-gradient(135deg, var(--bg-base) 0%, var(--bg-surface) 100%);
}
.chat-empty-card {
    max-width: 440px;
    text-align: center;
    padding: 36px 28px;
    border-radius: var(--chat-radius-xl);
    background: var(--bg-glass-strong);
    border: 1px solid var(--border-subtle);
    box-shadow: var(--shadow-card);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    animation: cardIn 0.4s ease both;
}
@keyframes cardIn {
    from { opacity: 0; transform: translateY(14px) scale(0.98); }
    to { opacity: 1; transform: none; }
}
.empty-icon-glow {
    width: 80px; height: 80px;
    margin: 0 auto 20px;
    border-radius: 26px;
    display: grid; place-items: center;
    background: linear-gradient(135deg, var(--primary-dim), rgba(56, 189, 248, 0.08));
    border: 1px solid rgba(139, 92, 246, 0.25);
    color: var(--primary-light);
    box-shadow: 0 12px 36px var(--primary-glow);
}
.chat-empty-card h2 { font-size: 19px; font-weight: 800; margin-bottom: 8px; }
.chat-empty-card p { font-size: 13px; color: var(--text-muted); line-height: 1.7; margin-bottom: 22px; }
.empty-features { display: flex; flex-direction: column; gap: 9px; text-align: right; padding-top: 14px; border-top: 1px solid var(--border-subtle); }
[dir="ltr"] .empty-features { text-align: left; }
.feature-item { font-size: 12px; color: var(--text-muted); display: flex; align-items: center; gap: 8px; }
.feat-icon { font-size: 14px; }

.chat-view {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
    flex: 1 1 auto;
    width: 100%;
    position: relative;
    /* The composer must never be pushed out of the flex column when the
       keyboard shrinks --app-vh: the message canvas shrinks instead. */
    overflow: hidden;
}

/* ── Chat header ───────────────────────────────────────────────────── */
.chat-header {
    min-height: var(--chat-header-h);
    height: var(--chat-header-h);
    flex: 0 0 var(--chat-header-h);
    display: flex;
    align-items: center;
    gap: 12px;
    padding: calc(8px + env(safe-area-inset-top, 0px) / 3) 16px 8px;
    border-bottom: 1px solid var(--border-subtle);
    background: var(--bg-glass-strong);
    backdrop-filter: blur(28px);
    -webkit-backdrop-filter: blur(28px);
    position: relative;
    z-index: 15;
}

.btn-back-chat {
    display: none;
    width: 38px; height: 38px;
    border-radius: var(--chat-radius-sm);
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    background: var(--bg-glass-subtle);
    border: 1px solid var(--border-subtle);
}

.chat-header-avatar {
    width: 48px; height: 48px;
    flex: none;
    border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 18px; font-weight: 800;
    color: #fff;
    background: linear-gradient(135deg, var(--primary), var(--accent-cyan));
    position: relative;
    overflow: visible;
}
#chat-header-avatar-text {
    width: 100%; height: 100%;
    display: flex; align-items: center; justify-content: center;
    border-radius: 50%;
    overflow: hidden;
}
.header-online-dot {
    position: absolute;
    bottom: 0; inset-inline-end: 0;
    width: 13px; height: 13px;
    border-radius: 50%;
    background: var(--text-subtle);
    border: 2.5px solid var(--bg-surface);
    transition: var(--transition-fast);
}
.chat-header-avatar.is-online .header-online-dot,
.header-online-dot.is-online,
.header-online-dot.online {
    background: var(--success);
    box-shadow: 0 0 8px var(--success-glow);
}

.chat-header-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; cursor: pointer; }
.chat-header-name {
    font-size: 15px; font-weight: 800;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-header-status { font-size: 11.5px; color: var(--text-muted); }
.chat-header-status.online { color: var(--success); }

.chat-header-tools { display: flex; align-items: center; gap: 6px; }
.header-tool-btn { width: 38px; height: 38px; border-radius: 12px; }

/* ── In-chat search panel ──────────────────────────────────────────── */
.chat-search-panel {
    flex: none;
    border-bottom: 1px solid var(--border-subtle);
    background: var(--bg-glass);
    padding: 10px 14px;
    z-index: 14;
}
.chat-search-row { display: flex; align-items: center; gap: 8px; }
.chat-search-row .search-field { flex: 1; }
.search-panel-icon { color: var(--text-subtle); flex: none; }
.btn-search-close { width: 34px; height: 34px; }
.chat-search-results {
    display: flex; flex-direction: column; gap: 4px;
    max-height: 220px;
    overflow-y: auto;
    margin-top: 8px;
}
.chat-search-results:empty { display: none; }
.search-msg-item {
    display: flex; gap: 10px; align-items: center;
    width: 100%; text-align: start;
    padding: 9px 10px;
    border-radius: var(--chat-radius-sm);
    transition: var(--transition-fast);
}
.search-msg-item:hover { background: var(--bg-glass-subtle); }
.search-msg-avatar {
    width: 34px; height: 34px; flex: none;
    border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 13px; font-weight: 800; color: #fff;
    background: linear-gradient(135deg, var(--primary), var(--accent-cyan));
    overflow: hidden;
}
.search-msg-info { flex: 1; min-width: 0; }
.search-msg-top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.search-msg-name { font-size: 12.5px; font-weight: 750; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.search-msg-time { font-size: 10px; color: var(--text-subtle); flex: none; }
.search-msg-text { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.search-no-results { text-align: center; padding: 14px; font-size: 12px; color: var(--text-muted); }

/* ── Pinned banner ─────────────────────────────────────────────────── */
.pinned-banner {
    flex: none;
    display: flex; align-items: center; gap: 10px;
    margin: 10px 14px 0;
    padding: 9px 12px;
    border-radius: var(--chat-radius-md);
    background: var(--primary-dim);
    border: 1px solid rgba(139, 92, 246, 0.3);
    cursor: pointer;
    transition: var(--transition-fast);
}
.pinned-banner:hover { border-color: rgba(139, 92, 246, 0.5); }
.pinned-icon-box {
    width: 30px; height: 30px; flex: none;
    border-radius: 10px;
    display: grid; place-items: center;
    color: var(--primary-light);
    background: rgba(139, 92, 246, 0.15);
}
.pinned-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.pinned-label { font-size: 11px; font-weight: 800; color: var(--primary-light); }
.pinned-text {
    font-size: 12px; color: var(--text-muted);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.btn-unpin-action { width: 28px; height: 28px; font-size: 12px; color: var(--text-muted); }
.btn-unpin-action:hover { color: var(--danger); }

/* ── Messages canvas ───────────────────────────────────────────────── */
.chat-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 18px 14px 12px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
}
.chat-messages::-webkit-scrollbar { width: 5px; }
.chat-messages::-webkit-scrollbar-track { background: transparent; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--border-medium); border-radius: 99px; }

/* Date / unread dividers */
.date-divider, .unread-divider {
    align-self: center;
    font-size: 11px;
    font-weight: 700;
    padding: 5px 14px;
    margin: 10px 0;
    border-radius: 999px;
    max-width: 90%;
}
.date-divider {
    color: var(--text-muted);
    background: var(--bg-glass-subtle);
    border: 1px solid var(--border-subtle);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}
.unread-divider {
    color: #fff;
    background: linear-gradient(135deg, var(--success), #0d9488);
    box-shadow: 0 4px 14px var(--success-glow);
}

/* Message rows & bubbles */
.msg-row {
    max-width: min(78%, 680px);
    display: flex;
    flex-direction: column;
    margin: 2px 0;
    position: relative;
    animation: msgEntry 0.22s cubic-bezier(0.34, 1.56, 0.64, 1) both;
    /* Long-press opens our own message menu: suppress the native iOS
       callout/selection loupe so it doesn't fight the popup. */
    -webkit-touch-callout: none;
}
@keyframes msgEntry {
    from { opacity: 0; transform: translateY(8px) scale(0.98); }
    to { opacity: 1; transform: none; }
}
.msg-row.out { align-self: flex-end; }
.msg-row.in { align-self: flex-start; }

.msg-sender {
    margin: 0 0 3px;
    padding: 0 12px;
    color: var(--primary-light);
    font-size: 11px;
    font-weight: 750;
}

.msg-wrap { display: flex; align-items: flex-end; gap: 7px; max-width: 100%; }
.msg-row.out .msg-wrap { flex-direction: row-reverse; }
.msg-avatar {
    width: 30px; height: 30px; flex: none;
    border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    align-self: flex-end;
    margin-bottom: 2px;
}

.msg-bubble {
    max-width: 100%;
    min-width: 0;
    padding: 10px 14px 7px;
    border-radius: var(--chat-radius-lg);
    position: relative;
    word-break: break-word;
    line-height: 1.75;
    /* User-controlled message text size (Settings → Appearance). */
    font-size: calc(13.5px * var(--chat-msg-scale, 1));
    box-shadow: 0 4px 18px rgba(0, 0, 0, 0.1);
    transition: transform var(--transition-fast), box-shadow var(--transition-fast);
}
/* NOTE: the 16px iOS-zoom floor applies to INPUTS only (.chat-textarea,
   .search-field) — bubbles keep the user's chosen scale. */
.msg-row.out .msg-bubble {
    background: var(--msg-out-bg);
    color: var(--msg-out-text);
    border: 1px solid var(--msg-out-border);
    border-bottom-right-radius: 5px;
}
[dir="ltr"] .msg-row.out .msg-bubble { border-bottom-right-radius: var(--chat-radius-lg); border-bottom-left-radius: 5px; }
.msg-row.in .msg-bubble {
    background: var(--msg-in-bg);
    color: var(--msg-in-text);
    border: 1px solid var(--msg-in-border);
    border-bottom-left-radius: 5px;
}
[dir="ltr"] .msg-row.in .msg-bubble { border-bottom-left-radius: var(--chat-radius-lg); border-bottom-right-radius: 5px; }
.msg-bubble.dangerous-file {
    outline: 1.5px dashed rgba(244, 63, 94, 0.55);
    outline-offset: 2px;
}
.msg-highlight .msg-bubble { box-shadow: 0 0 0 3px var(--primary-glow), 0 4px 18px rgba(0, 0, 0, 0.1); }

.msg-text { overflow-wrap: anywhere; }
.msg-text p { margin: 0 0 6px; }
.msg-text p:last-child { margin-bottom: 0; }
.msg-bubble strong { font-weight: 800; }
.msg-code {
    font-family: ui-monospace, 'Cascadia Code', Consolas, monospace;
    font-size: 0.86em;
    direction: ltr;
    unicode-bidi: embed;
    background: rgba(0, 0, 0, 0.28);
    border: 1px solid var(--border-subtle);
    padding: 1px 7px;
    border-radius: 7px;
}
[data-theme="light"] .msg-code { background: rgba(15, 23, 42, 0.06); }
.msg-link { color: inherit; text-decoration: underline; text-decoration-color: currentColor; text-underline-offset: 3px; overflow-wrap: anywhere; }
.msg-row.out .msg-link { color: #fff; font-weight: 600; }
.msg-row.in .msg-link { color: var(--accent-cyan); }

.msg-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 5px;
    margin-top: 3px;
}
.msg-time { font-size: 10px; opacity: 0.75; }
.msg-edited { font-size: 10px; opacity: 0.7; font-style: italic; }
.msg-tick { font-size: 11px; line-height: 1; letter-spacing: -1px; opacity: 0.8; }
.msg-tick.read { color: #7dd3fc; opacity: 1; }
.msg-tick.delivered { opacity: 0.9; }
.msg-tick.sent { opacity: 0.65; }
.msg-tick.pending { opacity: 0.55; }

/* Quoted reply inside a bubble */
.msg-reply-bar {
    display: block;
    margin-bottom: 7px;
    padding: 7px 11px;
    border-radius: 10px;
    background: rgba(0, 0, 0, 0.18);
    border-inline-start: 3px solid currentColor;
    cursor: pointer;
    opacity: 0.92;
}
[data-theme="light"] .msg-reply-bar { background: rgba(15, 23, 42, 0.05); }
.msg-reply-name { display: block; font-size: 11px; font-weight: 800; margin-bottom: 1px; }
.msg-reply-text {
    display: block;
    font-size: 12px;
    opacity: 0.85;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    max-width: 100%;
}

/* Attachments */
.msg-image {
    max-width: min(66vw, 340px);
    max-height: 320px;
    border-radius: var(--chat-radius-md);
    cursor: zoom-in;
    display: block;
    background: rgba(0, 0, 0, 0.2);
}
.msg-file {
    min-width: 220px;
    max-width: 340px;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    border-radius: var(--chat-radius-md);
    background: rgba(0, 0, 0, 0.15);
    border: 1px solid var(--border-subtle);
    text-decoration: none;
    color: inherit;
    transition: var(--transition-fast);
}
[data-theme="light"] .msg-file { background: rgba(15, 23, 42, 0.04); }
.msg-file:hover { border-color: var(--border-medium); }
.msg-file-icon { font-size: 26px; flex: none; }
.msg-file-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.msg-file-name {
    font-size: 12.5px; font-weight: 750;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.msg-file-size { font-size: 10.5px; color: var(--text-muted); direction: ltr; text-align: end; }
[dir="ltr"] .msg-file-size { text-align: start; }

/* Audio / voice player */
.msg-audio-player {
    min-width: 230px;
    max-width: 300px;
    padding: 6px 4px;
    display: flex;
    align-items: center;
    gap: 8px;
}
.chat-audio-elem { flex: 1; min-width: 0; height: 38px; border-radius: 99px; outline: none; }
.voice-speed-btn {
    flex: none;
    min-width: 44px;
    height: 30px;
    padding: 0 10px;
    border-radius: 99px;
    font-size: 11px;
    font-weight: 800;
    font-family: ui-monospace, Consolas, monospace;
    color: var(--text-muted);
    background: var(--bg-glass-subtle);
    border: 1px solid var(--border-subtle);
    cursor: pointer;
    transition: var(--transition-fast);
    touch-action: manipulation;
}
.voice-speed-btn.fast {
    color: var(--primary-light);
    border-color: rgba(139, 92, 246, 0.45);
    background: var(--primary-dim);
}

.msg-quarantine-note {
    font-size: 11px;
    color: var(--danger);
    background: var(--chat-danger-dim);
    border: 1px solid rgba(244, 63, 94, 0.3);
    border-radius: 8px;
    padding: 6px 10px;
    margin-top: 6px;
}

/* Typing indicator */
.typing-bar { flex: none; padding: 0 16px 6px; }
.typing-bubble {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 14px;
    border-radius: 999px;
    background: var(--bg-glass-subtle);
    border: 1px solid var(--border-subtle);
    font-size: 11.5px;
    color: var(--text-muted);
}
.typing-dots { display: inline-flex; gap: 3px; }
.typing-dots span {
    width: 6px; height: 6px;
    border-radius: 50%;
    background: var(--primary-light);
    animation: typingPulse 1.4s ease-in-out infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typingPulse {
    0%, 80%, 100% { opacity: 0.3; transform: scale(0.75); }
    40% { opacity: 1; transform: scale(1.2); }
}
.typing-text strong { color: var(--text-main); }

/* Scroll-to-bottom */
.btn-scroll-bottom {
    position: absolute;
    bottom: 96px;
    inset-inline-start: 16px;
    width: 42px; height: 42px;
    border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    color: var(--text-main);
    background: var(--bg-glass-strong);
    border: 1px solid var(--border-medium);
    box-shadow: var(--shadow-card);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    z-index: 12;
    transition: var(--transition-fast);
}
.btn-scroll-bottom:hover { border-color: var(--primary-light); transform: translateY(2px); }
.scroll-unread-badge {
    position: absolute;
    top: -5px; inset-inline-end: -5px;
    min-width: 20px; height: 20px;
    padding: 0 5px;
    border-radius: 999px;
    display: flex; align-items: center; justify-content: center;
    font-size: 10.5px; font-weight: 800;
    color: #fff;
    background: linear-gradient(135deg, var(--success), #0d9488);
}
html.kb-open .btn-scroll-bottom { bottom: 88px; }

/* ── Composer dock ───────────────────────────────────────────────────
   The dock is flex:0 0 auto inside .chat-view (column) so the message
   canvas — never the composer — absorbs every keyboard resize. Sticky
   bottom + transform guard keep it above the OS keyboard even if the
   --app-vh variable lags one frame behind the visual viewport. */
.chat-input-area {
    flex: none;
    padding: 10px 16px max(14px, env(safe-area-inset-bottom));
    background: var(--bg-glass-strong);
    border-top: 1px solid var(--border-subtle);
    backdrop-filter: blur(28px);
    -webkit-backdrop-filter: blur(28px);
    position: sticky;
    bottom: 0;
    z-index: 15;
    /* Never collapse under the keyboard: the area always keeps its row. */
    min-height: var(--chat-composer-h);
}

.chat-input-row {
    display: flex;
    align-items: flex-end;
    flex-wrap: nowrap;
    gap: 8px;
    max-width: 1100px;
    margin: 0 auto;
    min-width: 0;
}

.chat-textarea {
    flex: 1;
    min-width: 0;
    min-height: 46px;
    max-height: 140px;
    resize: none;
    outline: 0;
    padding: 12px 16px;
    border-radius: var(--chat-radius-lg);
    border: 1px solid var(--border-subtle);
    background: var(--bg-surface-elevated);
    color: var(--text-main);
    line-height: 1.55;
    /* Never below 16px: iOS auto-zooms on focus for smaller fields, which
       breaks the visual viewport and buries the composer/voice button. The
       max() keeps the user's Settings → Appearance text scaling intact. */
    font-size: max(16px, calc(13.5px * var(--chat-msg-scale, 1)));
    transition: var(--transition-fast);
    touch-action: manipulation;
}
.chat-textarea::placeholder { color: var(--text-subtle); }
.chat-textarea:focus {
    border-color: var(--border-focus);
    background: var(--bg-surface);
    box-shadow: 0 0 0 3px var(--primary-dim);
}

.composer-tool-btn {
    width: 42px;
    height: 42px;
    flex: 0 0 42px;
    border-radius: var(--chat-radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    background: var(--bg-glass-subtle);
    border: 1px solid var(--border-subtle);
    transition: var(--transition-fast);
    touch-action: manipulation;
}
.composer-tool-btn:hover {
    color: var(--text-main);
    background: var(--primary-dim);
    border-color: rgba(139, 92, 246, 0.3);
    transform: translateY(-1px);
}
.composer-tool-btn:active { transform: scale(0.94); }

.btn-voice.recording-active, .btn-voice:hover { color: var(--danger); }

.btn-send {
    width: 46px;
    height: 46px;
    flex: 0 0 46px;
    border-radius: var(--chat-radius-lg);
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 6px 20px var(--primary-glow);
    transition: var(--transition-fast);
    touch-action: manipulation;
}
.btn-send:hover { transform: translateY(-2px) scale(1.04); box-shadow: 0 10px 28px var(--primary-glow); }
.btn-send:active { transform: scale(0.94); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; transform: none; box-shadow: none; }

/* Previews (reply / file / recording / voice-preview) */
.reply-preview, .file-preview, .recording-bar, .preview-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    border-radius: var(--chat-radius-md);
    margin: 0 auto 10px;
    padding: 9px 14px;
    max-width: 1100px;
    background: var(--primary-dim);
    border: 1px solid rgba(139, 92, 246, 0.2);
    font-size: 12px;
    animation: previewSlide 0.2s ease;
}
@keyframes previewSlide {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: none; }
}
.reply-preview { border-inline-start: 4px solid var(--primary); }
.reply-info { flex: 1; min-width: 0; }
.reply-name { display: block; color: var(--primary-light); font-size: 11px; font-weight: 800; }
.reply-text {
    display: block;
    color: var(--text-muted);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    max-width: 500px;
    font-size: 11.5px;
}

/* Sandbox file box */
.file-preview-icon { font-size: 18px; flex: none; }
.file-safe-box {
    background: linear-gradient(135deg, rgba(16, 185, 129, 0.10), rgba(139, 92, 246, 0.10));
    border: 1px solid rgba(16, 185, 129, 0.35);
}
.file-safe-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.file-safe-name {
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    font-size: 12.5px; font-weight: 750;
    color: var(--text-main);
    max-width: 100%;
}
.file-safe-meta { font-size: 10.5px; color: var(--text-muted); direction: ltr; text-align: right; }
[dir="ltr"] .file-safe-meta { text-align: left; }
.file-safe-badge {
    flex: none;
    font-size: 10px; font-weight: 800;
    padding: 3px 9px;
    border-radius: 99px;
    color: var(--success);
    background: rgba(16, 185, 129, 0.12);
    border: 1px solid rgba(16, 185, 129, 0.30);
    white-space: nowrap;
}
.btn-remove-file { color: var(--danger); width: 28px; height: 28px; border-radius: 6px; display: grid; place-items: center; }
.btn-remove-file:hover { background: var(--chat-danger-dim); }

/* Recording bar */
.recording-bar {
    background: rgba(244, 63, 94, 0.08);
    border-color: rgba(244, 63, 94, 0.25);
}
.rec-dot {
    width: 10px; height: 10px;
    border-radius: 50%;
    background: var(--danger);
    box-shadow: 0 0 10px rgba(244, 63, 94, 0.6);
    animation: recBlink 1s infinite;
    flex: none;
}
@keyframes recBlink {
    50% { opacity: 0.3; transform: scale(0.85); }
}
.rec-waveform { display: flex; align-items: center; gap: 3px; height: 20px; }
.wave-bar {
    width: 3px;
    height: 10px;
    background: var(--danger);
    border-radius: 99px;
    animation: waveAnim 0.8s ease-in-out infinite alternate;
}
.wave-bar:nth-child(2) { animation-delay: 0.15s; height: 16px; }
.wave-bar:nth-child(3) { animation-delay: 0.3s; height: 12px; }
.wave-bar:nth-child(4) { animation-delay: 0.45s; height: 18px; }
.wave-bar:nth-child(5) { animation-delay: 0.6s; height: 8px; }
.wave-bar:nth-child(6) { animation-delay: 0.75s; height: 14px; }
@keyframes waveAnim {
    0% { transform: scaleY(0.4); }
    100% { transform: scaleY(1.2); }
}
/* Live level meter drives inline heights — freeze the decoration. */
.rec-waveform.live .wave-bar { animation: none; }
/* Sustained near-silence (dead/blocked mic): gray frozen bars. */
.rec-waveform.nosig .wave-bar {
    background: var(--text-muted);
    opacity: 0.45;
    animation: none;
}
.rec-timer { font-weight: 800; color: var(--danger); font-family: ui-monospace, Consolas, monospace; font-size: 13px; }
.rec-text { flex: 1; color: var(--text-muted); font-size: 12px; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rec-actions { display: flex; align-items: center; gap: 6px; flex: none; }
.btn-stop-rec {
    width: 34px; height: 34px;
    border-radius: var(--chat-radius-sm);
    background: var(--primary);
    color: #ffffff;
    display: grid; place-items: center;
    box-shadow: 0 4px 12px var(--primary-glow);
}
.btn-cancel-rec, .btn-pause-rec, .btn-preview-play, .btn-send-rec, .btn-preview-del {
    width: 34px; height: 34px;
    border-radius: var(--chat-radius-sm);
    display: grid; place-items: center;
    font-size: 15px;
    touch-action: manipulation;
}
.btn-cancel-rec, .btn-preview-del { color: var(--danger); }
.btn-cancel-rec:hover, .btn-preview-del:hover { background: var(--chat-danger-dim); }
.btn-preview-play { color: var(--accent-cyan); background: var(--bg-glass-subtle); border: 1px solid var(--border-subtle); }
.btn-send-rec { background: var(--primary); color: #ffffff; box-shadow: 0 4px 12px var(--primary-glow); }

/* Voice preview bar (listen before send) */
.preview-bar {
    background: rgba(56, 189, 248, 0.08);
    border-color: rgba(56, 189, 248, 0.3);
}
.preview-timer { color: var(--accent-cyan); }
.preview-seek {
    flex: 1;
    min-width: 0;
    accent-color: var(--primary);
    cursor: pointer;
    touch-action: manipulation;
}

/* While recording/previewing, only the active bar is shown. Hiding (not
   removing) keeps layout stable; the bars live INSIDE .chat-input-area so
   the area itself must stay visible. */
.chat-input-area.recording .chat-input-row,
.chat-input-area.recording .reply-preview,
.chat-input-area.recording .file-preview,
.chat-input-area.recording .emoji-picker,
.chat-input-area.previewing .chat-input-row,
.chat-input-area.previewing .reply-preview,
.chat-input-area.previewing .file-preview,
.chat-input-area.previewing .emoji-picker,
.chat-input-area.previewing .recording-bar {
    display: none !important;
}

/* The on-screen keyboard already covers the home indicator, so the
   safe-area padding would only add a dead band above it. */
html.kb-open .chat-input-area { padding-bottom: 10px; }

/* ── Emoji picker ──────────────────────────────────────────────────── */
.emoji-picker {
    display: flex;
    flex-direction: column;
    /* Never taller than the shell: with the keyboard open the picker must
       not push the composer and the send button out of view. */
    max-height: min(290px, calc(var(--app-vh, 100dvh) * 0.45));
    background: var(--bg-glass-strong);
    border: 1px solid var(--border-subtle);
    border-radius: var(--chat-radius-lg);
    margin-top: 10px;
    overflow: hidden;
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    box-shadow: var(--shadow-card);
    animation: drawerIn 0.2s ease;
}
@keyframes drawerIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: none; }
}
.emoji-tabs {
    display: flex; gap: 6px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border-subtle);
    overflow-x: auto;
    scrollbar-width: none;
}
.emoji-tabs::-webkit-scrollbar { display: none; }
.emoji-tab {
    flex: none;
    padding: 6px 12px;
    border-radius: var(--chat-radius-sm);
    font-size: 12px; font-weight: 700;
    color: var(--text-muted);
    transition: var(--transition-fast);
}
.emoji-tab:hover { color: var(--text-main); }
.emoji-tab.active, .emoji-tab.sel {
    color: #fff;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
}
.emoji-body { overflow-y: auto; padding: 10px 12px 14px; }
.emoji-group-pane { margin-bottom: 10px; }
.emoji-item {
    display: inline-flex;
    align-items: center; justify-content: center;
    width: 38px; height: 38px;
    font-size: 21px;
    border-radius: 10px;
    transition: var(--transition-fast);
}
.emoji-item:hover { background: var(--bg-glass-subtle); transform: scale(1.18); }

/* ── Message context menu ──────────────────────────────────────────── */
.msg-context-menu {
    position: fixed;
    z-index: 1200;
    min-width: 178px;
    max-width: min(230px, 70vw);
    padding: 6px;
    border-radius: var(--chat-radius-md);
    background: var(--bg-glass-strong);
    border: 1px solid var(--border-medium);
    box-shadow: var(--shadow-app);
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    animation: menuPop 0.16s ease;
}
@keyframes menuPop {
    from { opacity: 0; transform: scale(0.94) translateY(-4px); }
    to { opacity: 1; transform: none; }
}
.msg-menu-item {
    display: flex; align-items: center; gap: 8px;
    padding: 9px 12px;
    border-radius: 9px;
    font-size: 13px; font-weight: 600;
    color: var(--text-main);
    cursor: pointer;
    transition: var(--transition-fast);
    white-space: nowrap;
}
.msg-menu-item:hover { background: var(--bg-glass-subtle); }
.msg-menu-item.msg-menu-danger, .msg-menu-item.danger { color: var(--danger); }
.msg-menu-item.msg-menu-danger:hover { background: var(--chat-danger-dim); }

/* ── Modals & lightbox ─────────────────────────────────────────────── */
.modal-overlay {
    display: none;
    position: fixed;
    inset: auto 0 auto 0;
    top: 0;
    height: var(--app-vh, 100dvh);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: rgba(2, 8, 16, 0.72);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
}
/* Same stale-offset guard as .chat-app: follow the keyboard only while open. */
html.kb-open .modal-overlay {
    top: var(--app-vh-top, 0px);
}
.modal-overlay.open {
    display: flex;
    animation: modalOverlayFade 0.2s ease;
}
@keyframes modalOverlayFade {
    from { opacity: 0; }
    to { opacity: 1; }
}
.modal-box {
    width: min(92vw, 460px);
    max-height: min(85vh, calc(var(--app-vh, 100dvh) - 24px));
    overflow-y: auto;
    position: relative;
    padding: 26px 24px;
    border-radius: var(--chat-radius-xl);
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    box-shadow: var(--shadow-app);
    animation: modalPopIn 0.24s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes modalPopIn {
    from { opacity: 0; transform: scale(0.94) translateY(10px); }
    to { opacity: 1; transform: none; }
}
.modal-close {
    position: absolute;
    top: 14px; inset-inline-end: 14px;
    width: 32px; height: 32px;
    border-radius: 10px;
    display: grid; place-items: center;
    color: var(--text-muted);
    background: var(--bg-glass-subtle);
    border: 1px solid var(--border-subtle);
}
.modal-close:hover { color: var(--text-main); }
.modal-title { font-size: 16px; font-weight: 800; margin-bottom: 16px; padding-inline-end: 36px; }
.modal-tabs { display: flex; gap: 6px; margin-bottom: 16px; padding: 4px; border-radius: 12px; background: var(--bg-glass-subtle); border: 1px solid var(--border-subtle); }
.tab {
    flex: 1;
    padding: 9px 6px;
    border-radius: 9px;
    font-size: 12.5px; font-weight: 750;
    color: var(--text-muted);
    transition: var(--transition-fast);
    white-space: nowrap;
}
.tab:hover { color: var(--text-main); }
.tab.active { color: #fff; background: linear-gradient(135deg, var(--primary), var(--primary-dark)); box-shadow: 0 4px 12px var(--primary-glow); }
.tab-content { display: none; }
.tab-content.active { display: block; }

.lightbox-overlay { background: rgba(2, 6, 12, 0.9); }
.lightbox-content {
    display: flex; flex-direction: column; align-items: center; gap: 14px;
    max-width: min(94vw, 860px);
    max-height: calc(var(--app-vh, 100dvh) - 48px);
}
.lightbox-img {
    max-width: 100%;
    max-height: calc(var(--app-vh, 100dvh) - 160px);
    border-radius: var(--chat-radius-lg);
    box-shadow: var(--shadow-app);
    object-fit: contain;
    background: rgba(0, 0, 0, 0.4);
}
.lightbox-close {
    align-self: flex-end;
    width: 36px; height: 36px;
    border-radius: 12px;
    display: grid; place-items: center;
    color: #fff;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.15);
    font-size: 15px;
}
.lightbox-actions { display: flex; gap: 10px; }

/* Search results (new-chat modal) */
.search-results { display: flex; flex-direction: column; gap: 4px; margin: 10px 0; max-height: 220px; overflow-y: auto; }
.search-results:empty { display: none; }
.search-result-item {
    display: flex; align-items: center; gap: 10px;
    width: 100%; text-align: start;
    padding: 9px 10px;
    border-radius: var(--chat-radius-sm);
    transition: var(--transition-fast);
}
.search-result-item:hover { background: var(--bg-glass-subtle); }

/* ── Settings sheet ────────────────────────────────────────────────── */
.settings-overlay { align-items: flex-end; padding: 0; }
.modal-box.settings-sheet {
    width: min(96vw, 520px);
    max-height: min(92vh, var(--app-vh, 100dvh));
    border-radius: 26px 26px 0 0;
    padding: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}
@media (min-width: 640px) {
    .settings-overlay { align-items: center; padding: 20px; }
    .modal-box.settings-sheet { border-radius: var(--chat-radius-xl); max-height: min(88vh, var(--app-vh, 100dvh)); }
}
.settings-head {
    display: flex; align-items: center; gap: 10px;
    padding: 16px 18px 12px;
    border-bottom: 1px solid var(--border-subtle);
    flex: none;
}
.settings-title { flex: 1; font-size: 15.5px; font-weight: 800; }
.settings-close { width: 32px; height: 32px; }
.settings-conn { font-size: 13px; color: var(--text-subtle); transition: var(--transition-fast); }
.settings-conn.online { color: var(--success); text-shadow: 0 0 8px var(--success-glow); }
.settings-conn.offline { color: var(--danger); }

.settings-body { overflow-y: auto; padding: 6px 18px 20px; }
.settings-account {
    display: flex; align-items: center; gap: 12px;
    padding: 14px 2px;
}
.settings-account-avatar {
    width: 54px; height: 54px; flex: none;
    border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 20px; font-weight: 800; color: #fff;
    background: linear-gradient(135deg, var(--primary), var(--accent-cyan));
    overflow: hidden;
}
.settings-account-info { flex: 1; min-width: 0; }
.settings-account-name { font-size: 14.5px; font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.settings-account-phone { font-size: 12px; color: var(--text-muted); }
.settings-account-edit { font-size: 12px; font-weight: 700; color: var(--primary-light); text-decoration: none; white-space: nowrap; }
.settings-account-edit:hover { text-decoration: underline; }

.settings-section { margin-top: 14px; }
.settings-section-title { font-size: 12px; font-weight: 800; color: var(--text-muted); margin-bottom: 8px; padding: 0 2px; }
.settings-card {
    border-radius: var(--chat-radius-lg);
    background: var(--bg-glass-subtle);
    border: 1px solid var(--border-subtle);
    overflow: hidden;
}
.settings-row {
    display: flex; align-items: center; justify-content: space-between; gap: 10px;
    padding: 12px 14px;
    cursor: pointer;
}
.settings-row + .settings-row,
.settings-row + .settings-sessions { border-top: 1px solid var(--border-subtle); }
a.settings-row { text-decoration: none; color: inherit; }
a.settings-row:hover { background: rgba(255, 255, 255, 0.02); }
.settings-row-main { flex: 1; min-width: 0; }
.settings-row-label { font-size: 13px; font-weight: 750; }
.settings-row-desc { font-size: 11.5px; color: var(--text-muted); margin-top: 2px; line-height: 1.6; }
.settings-row-stack { flex-direction: column; align-items: stretch; }
.settings-chevron { color: var(--text-subtle); font-size: 18px; flex: none; }
.settings-btn {
    flex: none;
    padding: 8px 16px;
    border-radius: 10px;
    font-size: 12px; font-weight: 800;
    color: var(--primary-light);
    background: var(--primary-dim);
    border: 1px solid rgba(139, 92, 246, 0.35);
    transition: var(--transition-fast);
}
.settings-btn:hover { box-shadow: var(--shadow-glow); }
.settings-btn.danger { color: var(--danger); background: var(--chat-danger-dim); border-color: rgba(244, 63, 94, 0.35); }
.settings-btn.ghost { color: var(--text-muted); background: transparent; }
.settings-badge {
    flex: none;
    font-size: 10.5px; font-weight: 800;
    padding: 4px 11px;
    border-radius: 999px;
    color: var(--accent-cyan);
    background: rgba(56, 189, 248, 0.12);
    border: 1px solid rgba(56, 189, 248, 0.3);
    white-space: nowrap;
}
.settings-switch {
    appearance: none;
    -webkit-appearance: none;
    flex: none;
    width: 42px; height: 24px;
    border-radius: 999px;
    background: var(--border-medium);
    border: 1px solid var(--border-subtle);
    position: relative;
    cursor: pointer;
    transition: var(--transition-fast);
}
.settings-switch::after {
    content: "";
    position: absolute;
    top: 2px; inset-inline-start: 2px;
    width: 18px; height: 18px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
    transition: var(--transition-fast);
}
.settings-switch:checked { background: var(--success); border-color: transparent; }
.settings-switch:checked::after { transform: translateX(-18px); }
[dir="ltr"] .settings-switch:checked::after { transform: translateX(18px); }
.settings-segmented {
    display: flex; gap: 4px;
    padding: 4px;
    border-radius: 12px;
    background: rgba(0, 0, 0, 0.18);
    border: 1px solid var(--border-subtle);
}
[data-theme="light"] .settings-segmented { background: rgba(15, 23, 42, 0.05); }
.settings-segmented button {
    flex: 1;
    padding: 8px 4px;
    border-radius: 9px;
    font-size: 12px; font-weight: 750;
    color: var(--text-muted);
    transition: var(--transition-fast);
    white-space: nowrap;
}
.settings-segmented button.active, .settings-segmented button.sel {
    color: #fff;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    box-shadow: 0 3px 10px var(--primary-glow);
}
.settings-logout {
    width: 100%;
    margin-top: 16px;
    padding: 13px;
    border-radius: var(--chat-radius-md);
    font-size: 13.5px; font-weight: 800;
    color: var(--danger);
    background: var(--chat-danger-dim);
    border: 1px solid rgba(244, 63, 94, 0.3);
    transition: var(--transition-fast);
}
.settings-logout:hover { background: rgba(244, 63, 94, 0.2); }
.settings-footnote { margin-top: 12px; font-size: 11px; color: var(--text-subtle); text-align: center; line-height: 1.7; }
.settings-footnote a { color: var(--primary-light); }

/* Sessions */
.settings-sessions { display: flex; flex-direction: column; gap: 8px; padding: 12px 14px; }
.settings-session {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 12px;
    border-radius: var(--chat-radius-md);
    background: rgba(0, 0, 0, 0.15);
    border: 1px solid var(--border-subtle);
}
[data-theme="light"] .settings-session { background: rgba(15, 23, 42, 0.03); }
.settings-session-icon { font-size: 20px; flex: none; }
.settings-session-info { flex: 1; min-width: 0; }
.settings-session-title { font-size: 12.5px; font-weight: 800; display: flex; align-items: center; gap: 7px; }
.settings-session-badge {
    font-size: 9.5px; font-weight: 800;
    padding: 2px 8px;
    border-radius: 999px;
    color: var(--success);
    background: rgba(16, 185, 129, 0.12);
    border: 1px solid rgba(16, 185, 129, 0.3);
    white-space: nowrap;
}
.settings-session-meta { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.settings-session-revoke {
    flex: none;
    width: 28px; height: 28px;
    border-radius: 8px;
    display: grid; place-items: center;
    font-size: 12px;
    color: var(--danger);
}
.settings-session-revoke:hover { background: var(--chat-danger-dim); }
.settings-sessions-actions { display: flex; justify-content: center; padding: 2px 14px 12px; }
.settings-session-empty { padding: 16px; text-align: center; font-size: 12px; color: var(--text-muted); }

/* Info members */
.info-members { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; }

/* ── Group-plan & shift-leave cards in bubbles ───────────────────────── */
.gp-card, .slr-card {
    min-width: 220px;
    max-width: 320px;
    border-radius: var(--chat-radius-md);
    background: rgba(0, 0, 0, 0.16);
    border: 1px solid var(--border-subtle);
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
[data-theme="light"] .gp-card, [data-theme="light"] .slr-card { background: rgba(15, 23, 42, 0.04); }
.gp-card.gp-loading, .slr-card.slr-loading { opacity: 0.65; }
.gp-head { display: flex; align-items: center; gap: 9px; }
.gp-ico { font-size: 22px; flex: none; }
.gp-title { font-size: 13px; font-weight: 800; }
.gp-sub { font-size: 11.5px; color: var(--text-muted); line-height: 1.7; }
.gp-chips { display: flex; flex-wrap: wrap; gap: 5px; }
.gp-chip {
    font-size: 10.5px; font-weight: 700;
    padding: 3px 10px;
    border-radius: 999px;
    background: var(--primary-dim);
    border: 1px solid rgba(139, 92, 246, 0.3);
    color: var(--primary-light);
}
.gp-status, .gp-from { font-size: 11.5px; color: var(--text-muted); }
.gp-actions { display: flex; gap: 8px; }
.gp-btn {
    flex: 1;
    padding: 9px;
    border-radius: 10px;
    font-size: 12.5px; font-weight: 800;
    transition: var(--transition-fast);
}
.gp-btn.ok { color: #fff; background: linear-gradient(135deg, var(--success), #0d9488); }
.gp-btn.no { color: var(--danger); background: var(--chat-danger-dim); border: 1px solid rgba(244, 63, 94, 0.3); }
.gp-recs { display: flex; flex-direction: column; gap: 4px; }
.gp-rec {
    display: inline-flex; align-items: center; gap: 6px;
    font-size: 11.5px;
    padding: 4px 9px;
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.15);
    width: fit-content;
    max-width: 100%;
}
.slr-hint { font-size: 11.5px; color: var(--text-muted); line-height: 1.7; }
.slr-sugs { display: flex; flex-direction: column; gap: 6px; }
.slr-sugs-title { font-size: 11.5px; font-weight: 800; color: var(--primary-light); }
.slr-sug {
    text-align: start;
    padding: 8px 11px;
    border-radius: 9px;
    font-size: 12px;
    background: var(--bg-glass-subtle);
    border: 1px solid var(--border-subtle);
    transition: var(--transition-fast);
}
.slr-sug:hover { border-color: var(--primary-light); }

/* ── Toast ─────────────────────────────────────────────────────────── */
#toast {
    position: fixed;
    bottom: calc(26px + env(safe-area-inset-bottom, 0px));
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    z-index: 2000;
    display: none;
    max-width: min(92vw, 420px);
    padding: 12px 20px;
    border-radius: 14px;
    font-size: 13px; font-weight: 700;
    text-align: center;
    color: var(--text-main);
    background: var(--bg-glass-strong);
    border: 1px solid var(--border-medium);
    box-shadow: var(--shadow-app);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    opacity: 0;
    transition: opacity 0.25s ease, transform 0.25s ease;
}
#toast[style*="block"] { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── Scrollbars ────────────────────────────────────────────────────── */
.conv-list::-webkit-scrollbar, .chat-search-results::-webkit-scrollbar,
.settings-body::-webkit-scrollbar, .modal-box::-webkit-scrollbar,
.emoji-body::-webkit-scrollbar, .search-results::-webkit-scrollbar { width: 5px; }
.conv-list::-webkit-scrollbar-thumb, .chat-search-results::-webkit-scrollbar-thumb,
.settings-body::-webkit-scrollbar-thumb, .modal-box::-webkit-scrollbar-thumb,
.emoji-body::-webkit-scrollbar-thumb, .search-results::-webkit-scrollbar-thumb {
    background: var(--border-medium);
    border-radius: 99px;
}

/* ── Responsive: master-detail ─────────────────────────────────────── */
@media (max-width: 860px) {
    .sidebar { width: 100%; flex-basis: 100%; border: none; }
    .sidebar.hidden-mobile { display: none; }
    .btn-back-chat { display: inline-flex; }

    .chat-app { max-width: 100%; }
    .chat-header { padding: calc(6px + env(safe-area-inset-top, 0px) / 3) 12px 6px; }
    .chat-messages { padding: 14px 10px 10px; }
    .msg-row { max-width: 88%; }
    .msg-image { max-width: 78vw; max-height: 300px; }
    .msg-file { min-width: 0; max-width: 80vw; }
    .msg-audio-player { min-width: 0; }

    .chat-input-area { padding: 8px 10px max(10px, env(safe-area-inset-bottom)); }
    /* iOS home-indicator + Android gesture bar both sit BELOW the keyboard;
       when the keyboard is open the extra safe-area padding becomes a dead
       band that pushes the composer down again. */
    html.kb-open .chat-input-area { padding-bottom: 8px; }

    /* Narrow phones: the dock must keep textarea + voice + send on one row. */
    .chat-input-row { gap: 6px; }
    .composer-tool-btn { width: 38px; flex-basis: 38px; height: 42px; }
    .btn-send { width: 42px; flex-basis: 42px; height: 42px; }
    .chat-textarea { min-height: 42px; padding: 10px 12px; }
    .file-safe-badge { display: none; }

    .btn-scroll-bottom { bottom: 88px; inset-inline-start: 14px; }

    .notif-banner { left: 10px; right: 10px; }
}

@media (max-width: 480px) {
    .notif-banner { flex-direction: column; align-items: stretch; }
    .notif-action-btn { width: 100%; }
    .msg-audio-player { max-width: 76vw; }
    .rec-text { display: none; }
    .modal-box { padding: 22px 18px; }
}

/* Short landscape phones: fill the keyboard-aware viewport. */
@media (max-height: 500px) and (max-width: 860px) {
    .chat-header { min-height: 56px; height: 56px; flex-basis: 56px; }
    .chat-empty-card { padding: 20px; }
    .empty-icon-glow { width: 52px; height: 52px; margin-bottom: 10px; }
}

/* ── Reduced motion ────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    .chat-messages { scroll-behavior: auto; }
}

/* ── Slow theme transition on manual toggle ────────────────────────── */
.theme-transitioning .chat-app,
.theme-transitioning .sidebar,
.theme-transitioning .chat-main,
.theme-transitioning .chat-header,
.theme-transitioning .chat-input-area,
.theme-transitioning .msg-bubble,
.theme-transitioning .menu-card {
    transition: background-color 0.4s ease, border-color 0.4s ease, color 0.4s ease;
}

/* ── Chat-local control refinements (override shared main.css) ─────── */
.btn-primary {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 12px;
    border-radius: var(--chat-radius-md);
    font-size: 13.5px;
    font-weight: 800;
    color: #fff;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    box-shadow: 0 6px 20px var(--primary-glow);
    transition: var(--transition-fast);
}
.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 10px 26px var(--primary-glow); }
.btn-primary:active { transform: scale(0.98); }
.btn-small { width: auto; padding: 8px 16px; font-size: 12px; border-radius: 10px; }

.settings-row-static { cursor: default; }
.settings-row-link:hover .settings-chevron { transform: translateX(-3px); }
[dir="ltr"] .settings-row-link:hover .settings-chevron { transform: translateX(3px); }
.settings-chevron { transition: var(--transition-fast); }

