/* =============================================================================
   پیام‌رسان داخلی — سبک چت (parity با Flutter / واتساپ)
   ============================================================================= */

.messaging-page,
.messaging-shell {
    --msg-accent: var(--theme-primary, #1976d2);
    --msg-accent-rgb: var(--theme-primary-rgb, 25, 118, 210);
    --msg-bg: var(--theme-background, #f5f7fa);
    --msg-surface: var(--theme-surface, #ffffff);
    --msg-text: var(--theme-text-primary, #1a1a2e);
    --msg-text-muted: var(--theme-text-secondary, rgba(0, 0, 0, 0.55));
    --msg-border: rgba(var(--msg-accent-rgb), 0.12);
    --msg-hover: rgba(var(--msg-accent-rgb), 0.06);
    --msg-unread-bg: rgba(var(--msg-accent-rgb), 0.1);
    --msg-shadow: 0 1px 3px rgba(0, 0, 0, 0.06), 0 4px 12px rgba(0, 0, 0, 0.04);
    --msg-radius: 14px;
    --msg-radius-sm: 10px;
    --msg-chat-grad-top: #eff4fb;
    --msg-chat-grad-mid: #f7f9fc;
    --msg-chat-grad-bot: #e8f0fa;
    --msg-bubble-mine: var(--msg-accent);
    --msg-bubble-theirs: #ffffff;
    --msg-composer-bg: rgba(255, 255, 255, 0.94);

    max-width: 820px;
    margin: 0 auto;
    padding: 12px 14px 16px;
    padding-bottom: max(16px, env(safe-area-inset-bottom));
    font-family: 'IRANSans', Tahoma, Arial, sans-serif;
    color: var(--msg-text);
    -webkit-font-smoothing: antialiased;
}

[data-theme="dark"] .messaging-page,
[data-theme="dark"] .messaging-shell {
    --msg-bg: var(--theme-background, #121212);
    --msg-surface: var(--theme-surface, #1e1e1e);
    --msg-text: var(--theme-text-primary, #f0f0f0);
    --msg-text-muted: var(--theme-text-secondary, rgba(255, 255, 255, 0.65));
    --msg-border: rgba(255, 255, 255, 0.08);
    --msg-hover: rgba(255, 255, 255, 0.05);
    --msg-unread-bg: rgba(var(--msg-accent-rgb), 0.18);
    --msg-shadow: 0 1px 3px rgba(0, 0, 0, 0.25), 0 4px 16px rgba(0, 0, 0, 0.2);
    --msg-chat-grad-top: #0f172a;
    --msg-chat-grad-mid: #111827;
    --msg-chat-grad-bot: #0b1220;
    --msg-bubble-theirs: #1e293b;
    --msg-composer-bg: rgba(15, 23, 42, 0.94);
}

.messaging-page .mud-typography,
.messaging-page .mud-button,
.messaging-page .mud-button-label,
.messaging-page .mud-input,
.messaging-page .mud-input input,
.messaging-page .mud-input textarea,
.messaging-page .mud-input-label,
.messaging-page .mud-chip,
.messaging-page .mud-tab,
.messaging-page label {
    font-family: 'IRANSans', Tahoma, Arial, sans-serif !important;
}

/* ---------- Hero / list header ---------- */
.msg-hero {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
    padding: 14px 16px;
    border-radius: var(--msg-radius);
    background: linear-gradient(135deg, rgba(var(--msg-accent-rgb), 0.12), rgba(var(--msg-accent-rgb), 0.03));
    border: 1px solid var(--msg-border);
}

.msg-hero-title {
    margin: 0;
    font-size: 1.25rem;
    font-weight: 800;
    letter-spacing: -0.02em;
}

.msg-hero-sub {
    margin: 4px 0 0;
    font-size: 0.82rem;
    color: var(--msg-text-muted);
}

.msg-hero-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.msg-btn-compose {
    border-radius: 12px !important;
    text-transform: none !important;
    font-weight: 700 !important;
    box-shadow: 0 2px 10px rgba(var(--msg-accent-rgb), 0.28);
}

.msg-round-btn {
    background: var(--msg-surface) !important;
    border: 1px solid var(--msg-border) !important;
    border-radius: 50% !important;
}

.msg-search-wrap {
    margin-bottom: 10px;
}

.msg-search .mud-input-outlined {
    border-radius: 16px !important;
}

.msg-search .mud-input-slot,
.msg-search .mud-input-control {
    background: var(--msg-surface);
}

/* ---------- Segment tabs ---------- */
.messaging-tabs .mud-tabs-toolbar,
.msg-seg-tabs .mud-tabs-toolbar {
    background: rgba(var(--msg-accent-rgb), 0.08);
    border-radius: 14px;
    border: 1px solid rgba(var(--msg-accent-rgb), 0.16);
    padding: 5px;
    min-height: 46px;
    gap: 4px;
}

.messaging-tabs .mud-tab {
    border-radius: 12px;
    font-weight: 700;
    min-height: 38px;
    flex: 1;
    color: rgba(var(--msg-accent-rgb), 0.72) !important;
    text-transform: none !important;
}

.messaging-tabs .mud-tab.mud-tab-active,
.messaging-tabs .mud-tab[aria-selected="true"] {
    background: var(--msg-accent) !important;
    color: #fff !important;
    box-shadow: 0 3px 10px rgba(var(--msg-accent-rgb), 0.28);
}

.messaging-tabs .mud-tabs-panels {
    padding-top: 10px;
}

/* ---------- Conversation list ---------- */
.msg-conv-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.msg-conv-item {
    display: flex;
    align-items: stretch;
    gap: 4px;
    width: 100%;
    border: 1px solid transparent;
    border-radius: var(--msg-radius);
    background: var(--msg-surface);
    box-shadow: var(--msg-shadow);
    padding: 4px 4px 4px 0;
    transition: background 0.15s, transform 0.15s, border-color 0.15s, box-shadow 0.15s;
    color: inherit;
    position: relative;
    overflow: hidden;
}

.msg-conv-item:hover {
    border-color: rgba(var(--msg-accent-rgb), 0.18);
    transform: translateY(-1px);
}

.msg-conv-unread {
    background: linear-gradient(
        90deg,
        rgba(var(--msg-accent-rgb), 0.14) 0%,
        rgba(var(--msg-accent-rgb), 0.05) 42%,
        var(--msg-surface) 100%
    );
    border-color: rgba(var(--msg-accent-rgb), 0.28);
    box-shadow: 0 2px 14px rgba(var(--msg-accent-rgb), 0.12);
}

.msg-conv-unread-bar {
    position: absolute;
    top: 8px;
    bottom: 8px;
    right: 0;
    width: 4px;
    border-radius: 4px 0 0 4px;
    background: var(--msg-accent);
    box-shadow: 0 0 10px rgba(var(--msg-accent-rgb), 0.45);
}

.msg-conv-main {
    flex: 1;
    display: flex;
    align-items: flex-start;
    gap: 12px;
    min-width: 0;
    padding: 10px 8px 10px 12px;
    cursor: pointer;
    border-radius: calc(var(--msg-radius) - 4px);
}

.msg-conv-main:hover {
    background: var(--msg-hover);
}

.msg-conv-delete {
    align-self: center;
    margin-left: 2px;
    color: var(--msg-text-muted) !important;
    opacity: 0.72;
    transition: opacity 0.15s, color 0.15s, background 0.15s;
}

.msg-conv-item:hover .msg-conv-delete {
    opacity: 1;
}

.msg-conv-delete:hover {
    color: #d32f2f !important;
    background: rgba(211, 47, 47, 0.08) !important;
}

.msg-avatar-unread {
    box-shadow: 0 0 0 3px rgba(var(--msg-accent-rgb), 0.22), 0 2px 10px rgba(var(--msg-accent-rgb), 0.35);
}

.msg-conv-unread .msg-conv-name {
    color: var(--msg-accent);
}

.msg-conv-unread .msg-conv-preview {
    color: var(--msg-text);
    font-weight: 600;
}

.msg-conv-unread .msg-conv-time {
    color: var(--msg-accent);
    font-weight: 800;
}

.msg-avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    font-size: 0.85rem;
    color: #fff;
    background: linear-gradient(145deg, var(--msg-accent), color-mix(in srgb, var(--msg-accent) 65%, #0f172a));
    box-shadow: 0 2px 8px rgba(var(--msg-accent-rgb), 0.3);
}

.msg-avatar-sent {
    background: linear-gradient(145deg, #64748b, #334155);
}

.msg-conv-body {
    flex: 1;
    min-width: 0;
}

.msg-conv-top {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    align-items: baseline;
}

.msg-conv-name {
    font-weight: 800;
    font-size: 0.92rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.msg-conv-time {
    flex-shrink: 0;
    font-size: 0.72rem;
    font-weight: 700;
    color: var(--msg-text-muted);
}

.msg-conv-subject {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 2px;
    font-size: 0.84rem;
    font-weight: 700;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.msg-conv-preview {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-top: 2px;
    font-size: 0.78rem;
    color: var(--msg-text-muted);
    overflow: hidden;
}

.msg-conv-preview span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.msg-unread-badge {
    flex-shrink: 0;
    min-width: 22px;
    height: 22px;
    padding: 0 6px;
    border-radius: 999px;
    background: var(--msg-accent);
    color: #fff;
    font-size: 0.72rem;
    font-weight: 800;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 6px rgba(var(--msg-accent-rgb), 0.35);
    margin-inline-start: 6px;
}

.messaging-unread-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--msg-accent);
    flex-shrink: 0;
    box-shadow: 0 0 0 3px rgba(var(--msg-accent-rgb), 0.2);
}

.msg-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 48px 16px;
    color: var(--msg-text-muted);
    text-align: center;
}

.messaging-loading {
    padding: 24px 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
}

/* ---------- Chat thread layout ---------- */
.messaging-chat {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 120px);
    min-height: 420px;
    max-height: 900px;
    padding: 0;
    max-width: 860px;
    overflow: hidden;
    border-radius: var(--msg-radius);
    border: 1px solid var(--msg-border);
    background: var(--msg-surface);
    box-shadow: var(--msg-shadow);
}

.msg-thread-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--msg-border);
    background: var(--msg-surface);
    flex-shrink: 0;
}

.msg-thread-meta {
    min-width: 0;
    flex: 1;
}

.msg-thread-peer {
    font-weight: 800;
    font-size: 0.98rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.msg-thread-subject {
    font-size: 0.75rem;
    color: var(--msg-text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.msg-thread-body {
    flex: 1;
    overflow-y: auto;
    background: linear-gradient(180deg, var(--msg-chat-grad-top), var(--msg-chat-grad-mid) 55%, var(--msg-chat-grad-bot));
    -webkit-overflow-scrolling: touch;
}

.msg-thread-loading {
    height: 100%;
    justify-content: center;
    color: var(--msg-text-muted);
    font-size: 0.85rem;
}

.msg-thread-messages {
    direction: ltr;
    padding: 14px 12px 12px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.msg-day-chip {
    align-self: center;
    margin: 10px 0 8px;
    padding: 4px 12px;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 700;
    color: var(--msg-text-muted);
    background: rgba(0, 0, 0, 0.06);
    direction: rtl;
}

[data-theme="dark"] .msg-day-chip {
    background: rgba(255, 255, 255, 0.08);
}

.msg-bubble-row {
    display: flex;
    width: 100%;
    margin: 5px 0;
}

.msg-bubble-row.mine {
    justify-content: flex-start;
}

.msg-bubble-row.theirs {
    justify-content: flex-end;
}

.msg-bubble {
    direction: rtl;
    text-align: start;
    max-width: min(78%, 520px);
    padding: 10px 12px 8px;
    border-radius: 18px;
    box-shadow: 0 3px 10px rgba(0, 0, 0, 0.06);
    word-break: break-word;
}

.msg-bubble.mine {
    background: var(--msg-bubble-mine);
    color: #fff;
    border-bottom-left-radius: 6px;
}

.msg-bubble.theirs {
    background: var(--msg-bubble-theirs);
    color: var(--msg-text);
    border: 1px solid var(--msg-border);
    border-bottom-right-radius: 6px;
}

.msg-bubble-sender {
    font-size: 0.75rem;
    font-weight: 800;
    color: var(--msg-accent);
    margin-bottom: 4px;
}

.msg-bubble-text {
    font-size: 0.92rem;
    font-weight: 500;
    line-height: 1.45;
    white-space: pre-wrap;
}

.msg-bubble-attach {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
}

.msg-attach-chip {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    max-width: 100%;
    border: none;
    border-radius: 10px;
    padding: 7px 10px;
    cursor: pointer;
    font: inherit;
    font-size: 0.72rem;
    font-weight: 700;
    transition: opacity 0.15s;
}

.msg-bubble.mine .msg-attach-chip {
    background: rgba(255, 255, 255, 0.18);
    color: #fff;
}

.msg-bubble.theirs .msg-attach-chip {
    background: rgba(var(--msg-accent-rgb), 0.1);
    color: var(--msg-accent);
}

.msg-attach-chip:hover {
    opacity: 0.88;
}

.msg-attach-name {
    max-width: 140px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.msg-attach-size {
    opacity: 0.75;
    font-size: 0.65rem;
}

.msg-bubble-meta {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-top: 6px;
    font-size: 0.68rem;
    font-weight: 700;
    opacity: 0.78;
}

.msg-bubble.mine .msg-bubble-meta {
    color: rgba(255, 255, 255, 0.85);
}

.msg-bubble.theirs .msg-bubble-meta {
    color: var(--msg-text-muted);
}

/* ---------- Composer ---------- */
.msg-composer {
    flex-shrink: 0;
    border-top: 1px solid var(--msg-border);
    background: var(--msg-composer-bg);
    backdrop-filter: blur(10px);
    padding: 8px 10px 10px;
}

.msg-pending-strip {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 8px;
}

.msg-composer-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}

.msg-composer-attach {
    background: rgba(var(--msg-accent-rgb), 0.08) !important;
    border-radius: 50% !important;
    color: var(--msg-accent) !important;
}

.msg-composer-input {
    flex: 1;
}

.msg-composer-input .mud-input-outlined {
    border-radius: 22px !important;
}

.msg-composer-input .mud-input-slot {
    background: rgba(var(--msg-accent-rgb), 0.06);
}

.msg-composer-send {
    border-radius: 50% !important;
    width: 48px !important;
    height: 48px !important;
    min-width: 48px !important;
    box-shadow: 0 2px 10px rgba(var(--msg-accent-rgb), 0.35);
}

/* ---------- Compose page ---------- */
.messaging-compose-form {
    background: var(--msg-surface);
    border: 1px solid var(--msg-border);
    border-radius: var(--msg-radius);
    padding: 16px;
    box-shadow: var(--msg-shadow);
}

.messaging-compose-actions .mud-button {
    border-radius: 12px !important;
    text-transform: none !important;
    font-weight: 700 !important;
}

.messaging-header {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-bottom: 14px;
    padding-bottom: 10px;
    border-bottom: 1px solid var(--msg-border);
}

/* ---------- Image preview dialog ---------- */
.messaging-image-dialog .mud-dialog-content {
    padding-top: 8px;
}

.messaging-image-preview {
    display: flex;
    align-items: center;
    justify-content: center;
    max-height: min(70vh, 640px);
    overflow: auto;
    background: rgba(0, 0, 0, 0.04);
    border-radius: 8px;
    padding: 8px;
}

[data-theme="dark"] .messaging-image-preview {
    background: rgba(255, 255, 255, 0.06);
}

.messaging-image-preview img {
    max-width: 100%;
    max-height: min(68vh, 620px);
    object-fit: contain;
    border-radius: 4px;
}

@media (max-width: 600px) {
    .messaging-page,
    .messaging-shell {
        padding: 8px 8px 12px;
    }

    .messaging-chat {
        height: calc(100vh - 88px);
        border-radius: 0;
        border-left: none;
        border-right: none;
        max-width: none;
    }

    .msg-hero {
        flex-direction: column;
        align-items: stretch;
    }

    .msg-hero-actions {
        justify-content: flex-end;
    }

    .msg-bubble {
        max-width: 86%;
    }
}
