/**
 * Chat messages, inbox toasts and the live-updates bar (chat-render.js, inbox-state.js,
 * socket-owner.js). Loaded on every page by base.html.
 */

:root {
    --message-outgoing-bg: var(--bs-primary);
    --message-incoming-bg: color-mix(in srgb, var(--bs-dark) 80%, var(--bs-light) 20%);
    --message-text: var(--bs-white);
    --message-text-secondary: var(--bs-gray-400);
    --message-translation-bg: rgba(0, 0, 0, 0.2);
}

/* Message styles */
.message-wrapper {
    display: flex;
    flex-direction: column;
    max-width: 80%;
}

.message-wrapper.incoming {
    align-self: flex-start;
}

.message-wrapper.outgoing {
    align-self: flex-end;
}

/* Family conversation sender info */
.message-wrapper .message-sender-info {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 3px;
    padding-left: 2px;
}

.message-wrapper .message-sender-avatar {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.message-wrapper .message-sender-name {
    font-size: 0.7rem;
    color: var(--bs-gray-400);
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.message-wrapper .message {
    display: flex;
    position: relative;
}

.message-wrapper .message-content {
    border-radius: 18px;
    padding: 0.75rem;
    position: relative;
    max-width: 100%;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.message-wrapper .message.incoming .message-content {
    background-color: var(--message-incoming-bg);
    border-bottom-left-radius: 4px;
}

.message-wrapper .message.outgoing .message-content {
    background-color: var(--message-outgoing-bg);
    border-bottom-right-radius: 4px;
}

.message-wrapper .message-text {
    color: var(--message-text);
    word-break: break-word;
}

.message-wrapper .message-translation {
    margin-top: 0.5rem;
    padding: 0.5rem;
    background-color: var(--message-translation-bg);
    border-radius: 8px;
    font-style: italic;
    color: var(--message-text-secondary);
}

.message-wrapper .message-media {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.message-wrapper .message-image {
    max-width: 25vw;
    max-height: 400px;
    border-radius: 12px;
    object-fit: cover;
}

.message-wrapper .message-video {
    max-width: 25vw;
    max-height: 400px;
    border-radius: 12px;
}

.message-wrapper .message-voice {
    min-width: 200px;
}

.message-wrapper .message-audio {
    width: 100%;
    height: 40px;
}

/* Premium content styles */
.message-wrapper .message-premium {
    position: relative;
    margin-bottom: 0.5rem;
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.message-wrapper .message-premium img,
.message-wrapper .message-premium video {
    border-radius: 12px;
    max-width: 25vw;
    max-height: 400px;
    overflow: hidden;
    object-fit: cover;
}

.message-wrapper .message-premium-cost {
    display: inline-block;
    background-color: rgba(0, 0, 0, 0.6);
    color: var(--bs-warning);
    padding: 0.25rem 0.5rem;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 600;
    align-self: flex-start;
    margin-top: 0.25rem;
}

.message-wrapper .message-time {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    font-size: 0.7rem;
    color: var(--message-text-secondary);
    margin-top: 0.25rem;
    justify-content: flex-end;
}

.message-wrapper .message-sent,
.message-wrapper .message-read {
    font-size: 0.7rem;
}

/* Gift message styles */
.message-wrapper .message-gift {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0.75rem;
    background-color: color-mix(in srgb, var(--bs-dark) 85%, var(--bs-light) 15%);
    border-radius: 12px;
    text-align: center;
}

.message-wrapper .message-gift-icon {
    width: 64px;
    height: 64px;
    margin-bottom: 0.5rem;
    object-fit: contain;
}

.message-wrapper .message-gift-name {
    font-weight: 600;
    color: var(--bs-white);
    margin-bottom: 0.25rem;
}

.message-wrapper .message-gift-cost {
    display: inline-flex;
    align-items: center;
    color: var(--bs-warning);
    font-size: 0.875rem;
}

.message-wrapper .message-gift-cost::before {
    content: '🪙';
    margin-right: 0.25rem;
}

/* Livestream link styles */
.message-wrapper .message-livestream-link {
    display: inline-block;
    background-color: var(--bs-primary);
    color: var(--bs-white);
    padding: 8px 16px;
    border-radius: 20px;
    text-decoration: none;
    font-weight: 500;
    transition: background-color 0.2s;
}

.message-wrapper .message-livestream-link:hover {
    background-color: color-mix(in srgb, var(--bs-primary) 80%, white);
    color: var(--bs-white);
    text-decoration: none;
}

.message-wrapper .message-read {
    color: var(--bs-info);
}


/* ---------- Added by chat-render.js ---------- */

/* Long words and links wrap inside bubbles and cards */
.message-wrapper .message-text,
.chat-card,
.chat-notice {
    overflow-wrap: anywhere;
    word-break: break-word;
}

.message-wrapper.notice {
    align-self: center;
    max-width: 90%;
}

.chat-notice {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    padding: 0.3rem 0.8rem;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.08);
    color: var(--bs-gray-400);
    font-size: 0.78rem;
    text-align: center;
}

.chat-notice-body {
    display: inline-flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
}

.chat-notice-translation {
    font-style: italic;
    opacity: 0.8;
}

.chat-notice-time {
    font-size: 0.65rem;
    opacity: 0.6;
}

.chat-notice-link {
    color: var(--bs-info);
    font-weight: 600;
}

.chat-notice-thumbs {
    display: inline-flex;
    gap: 4px;
}

.chat-notice-thumbs img {
    width: 40px;
    height: 40px;
    object-fit: cover;
    border-radius: 6px;
}

.message-wrapper .message-content.message-card {
    padding: 0;
    overflow: hidden;
}

.message-wrapper .message-content.message-card .message-time {
    padding: 0 0.7rem 0.5rem;
}

.chat-card {
    display: flex;
    flex-direction: column;
    width: min(260px, 70vw);
}

.chat-card-head {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.7rem;
}

.chat-card-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
    flex: none;
}

.chat-card-info {
    min-width: 0;
}

.chat-card-title {
    color: var(--bs-white);
    font-weight: 700;
}

.chat-card-sub {
    color: var(--bs-gray-400);
    font-size: 0.75rem;
}

.chat-card-body {
    padding: 0.5rem 0.7rem 0;
}

.chat-card-level {
    margin-right: 0.3rem;
    padding: 0 0.35rem;
    border-radius: 6px;
    background: var(--bs-secondary);
    color: var(--bs-white);
    font-size: 0.65rem;
}

.chat-card-level.bronze { background: #cd7f32; }
.chat-card-level.silver { background: #c0c0c0; color: #1a1a1a; }
.chat-card-level.gold { background: #ffd700; color: #1a1300; }
.chat-card-level.platinum { background: var(--bs-danger); }
.chat-card-level.millionaire { background: #000; }

.chat-card-button {
    display: block;
    margin: 0.6rem 0.7rem 0.7rem;
    padding: 0.4rem;
    border-radius: 999px;
    background: var(--bs-primary);
    color: var(--bs-white);
    font-size: 0.85rem;
    font-weight: 600;
    text-align: center;
    text-decoration: none;
}

.chat-card-button.is-disabled {
    opacity: 0.6;
    pointer-events: none;
}

.chat-live-thumb {
    position: relative;
    height: 140px;
    background: #343a40;
}

.chat-live-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.chat-live-badge {
    position: absolute;
    top: 8px;
    left: 8px;
    padding: 1px 6px;
    border-radius: 4px;
    background: #e03131;
    color: #fff;
    font-size: 0.65rem;
    font-weight: 700;
}

.chat-live-viewers {
    position: absolute;
    top: 8px;
    right: 8px;
    padding: 1px 6px;
    border-radius: 4px;
    background: rgba(0, 0, 0, 0.5);
    color: #fff;
    font-size: 0.7rem;
}

.chat-premium-media img,
.chat-premium-media video {
    display: block;
    width: 100%;
    max-height: 320px;
    object-fit: cover;
}

.chat-premium-tag {
    align-self: flex-start;
    margin: 0.5rem 0.7rem 0;
    padding: 0.15rem 0.6rem;
    border-radius: 999px;
    background: linear-gradient(135deg, #ffd24d, #f5a800);
    color: #1a1300;
    font-size: 0.72rem;
    font-weight: 700;
}

.chat-album-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 2px;
}

.chat-album-grid img,
.chat-album-grid video {
    display: block;
    width: 100%;
    height: 120px;
    object-fit: cover;
    border-radius: 0;
}

.chat-announcement-banner img,
.chat-offer-image {
    display: block;
    width: 100%;
    max-height: 160px;
    object-fit: cover;
}

.chat-offer-privileges {
    margin: 0;
    padding: 0.5rem 0.7rem 0;
    list-style: none;
    font-size: 0.8rem;
}

.chat-call {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.chat-call-coins {
    color: var(--bs-warning);
}

.chat-media-unavailable {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.75rem;
    border-radius: 12px;
    background: rgba(0, 0, 0, 0.25);
    color: var(--bs-gray-400);
    font-size: 0.8rem;
}

.chat-unsupported {
    color: var(--bs-gray-400);
    font-style: italic;
}

.chat-unsupported-text {
    margin-top: 0.25rem;
    font-style: normal;
}

.message-wrapper.is-pending {
    opacity: 0.6;
}

.message-wrapper.is-failed .message-content {
    outline: 1px solid var(--bs-danger);
}

.chat-message-retry {
    display: block;
    margin-top: 0.35rem;
    padding: 0;
    border: 0;
    background: none;
    color: var(--bs-danger);
    font-size: 0.75rem;
    font-weight: 600;
    cursor: pointer;
}

.chat-removed {
    align-self: center;
    padding: 0.4rem 0.9rem;
    border-radius: 999px;
    background: rgba(220, 53, 69, 0.15);
    color: #ffb3ba;
    font-size: 0.8rem;
}

/* The livestream inbox popup is narrower than the chat page */
.inbox-popup-messages .message-wrapper {
    max-width: 85%;
}

.inbox-popup-messages .message-wrapper .message-image,
.inbox-popup-messages .message-wrapper .message-video,
.inbox-popup-messages .message-wrapper .message-premium img,
.inbox-popup-messages .message-wrapper .message-premium video {
    max-width: 100%;
}

/* ---------- inbox-state.js toasts, socket-owner.js bar ---------- */

.inbox-toast {
    position: fixed;
    bottom: 1.5rem;
    left: 50%;
    z-index: 2000;
    transform: translateX(-50%);
    padding: 0.6rem 1rem;
    border-radius: 999px;
    background: rgba(33, 37, 41, 0.95);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
    color: #fff;
    font-size: 0.85rem;
}

.socket-status-bar {
    position: fixed;
    bottom: 1rem;
    left: 50%;
    z-index: 2001;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    transform: translateX(-50%);
    padding: 0.5rem 0.6rem 0.5rem 1rem;
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 999px;
    background: rgba(33, 37, 41, 0.97);
    color: #fff;
    font-size: 0.85rem;
}

.socket-status-bar[hidden] {
    display: none;
}

.socket-status-bar button,
.socket-status-bar a {
    padding: 0.3rem 0.8rem;
    border: 0;
    border-radius: 999px;
    background: var(--bs-primary);
    color: #fff;
    font-weight: 600;
    text-decoration: none;
}

/* ---------- chat-menu.js ---------- */

.chat-menu {
    position: fixed;
    z-index: 2100;
    min-width: 190px;
    padding: 0.35rem;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    background: #2b3035;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}

.chat-menu-item {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    width: 100%;
    padding: 0.5rem 0.65rem;
    border: 0;
    border-radius: 8px;
    background: none;
    color: var(--bs-gray-200);
    font-size: 0.875rem;
    text-align: left;
}

.chat-menu-item:hover,
.chat-menu-item:focus {
    background: rgba(255, 255, 255, 0.07);
    outline: none;
}

.chat-menu-item i {
    width: 1rem;
    color: var(--bs-gray-500);
    text-align: center;
}

.chat-menu-item.danger,
.chat-menu-item.danger i {
    color: #ff8787;
}

.chat-menu-text {
    display: flex;
    flex-direction: column;
}

.chat-menu-hint {
    font-size: 0.72rem;
    color: var(--bs-gray-500);
}

/* A paid choice (the private video call) */
.chat-menu-item.accent i,
.chat-menu-item.accent .chat-menu-hint {
    color: #fbbf24;
}

.chat-menu-separator {
    height: 1px;
    margin: 0.25rem 0;
    background: rgba(255, 255, 255, 0.08);
}

/* ---------- Sending gifts, voice and albums (E3) ---------- */

.chat-item-progress {
    margin-top: 0.35rem;
    color: var(--bs-gray-400);
    font-size: 0.75rem;
}

.chat-message-error {
    margin-top: 0.35rem;
    color: var(--bs-danger);
    font-size: 0.75rem;
}

.chat-panel {
    display: none;
}

.chat-panel.open {
    display: block;
    /* The chat page pins the messages area's height: a drawer allowed to shrink gets squeezed to a strip */
    flex-shrink: 0;
    max-height: 45vh;
    overflow-y: auto;
    padding: 0.6rem 0.9rem;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    background: var(--bs-dark);
}

/* On wide screens the gift grid and the composer stay a readable width, centered */
.chat-panel.open > * {
    max-width: 720px;
    margin-left: auto;
    margin-right: auto;
}

.chat-panel-header {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    margin-bottom: 0.5rem;
}

.chat-panel-title {
    flex: 1;
    font-weight: 600;
}

.chat-panel-close {
    border: 0;
    background: none;
    color: var(--bs-gray-400);
    font-size: 1rem;
}

/* ---------- Message types (E5) ---------- */

.chat-day-separator {
    align-self: center;
    margin: 0.4rem 0;
    padding: 0.1rem 0.75rem;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.06);
    color: var(--bs-gray-400);
    font-size: 0.72rem;
}

.chat-notice.chat-notice-system {
    border-radius: 0;
    background: none;
}

.chat-gate-gift {
    margin-left: 0.4rem;
    padding: 0.15rem 0.7rem;
    border: 0;
    border-radius: 999px;
    background: var(--bs-primary);
    color: #fff;
    font-size: 0.75rem;
}

.chat-auto-chip {
    display: inline-block;
    margin-bottom: 0.2rem;
    padding: 0 0.4rem;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.15);
    font-size: 0.65rem;
}

.message-wrapper .message-sender-role {
    margin-left: 0.3rem;
    padding: 0 0.3rem;
    border-radius: 4px;
    background: var(--bs-warning);
    color: #111;
    font-size: 0.6rem;
    font-weight: 600;
}

.chat-album-locked {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.2rem;
    min-height: 110px;
    background: repeating-linear-gradient(45deg, #343a40 0 10px, #2f3438 10px 20px);
    color: var(--bs-gray-200);
}

.chat-album-lock {
    font-size: 1.5rem;
}

.chat-unlock-btn {
    display: block;
    width: calc(100% - 1rem);
    margin: 0.4rem 0.5rem 0.5rem;
    border: 0;
    background: var(--bs-pink, #d63384);
    color: #fff;
}

.chat-unlock-btn:disabled {
    opacity: 0.6;
}

.chat-album-item {
    position: relative;
}

.chat-album-duration {
    position: absolute;
    right: 4px;
    bottom: 4px;
    padding: 0 4px;
    border-radius: 4px;
    background: rgba(0, 0, 0, 0.6);
    color: #fff;
    font-size: 0.65rem;
}
