/* ==========================================================================
   Chats: the Chats | Mail switch (MessagesCard), the chat list (ChatList), a chat (ChatThread, opened by ChatHost as a
   sheet at the app's root) and its composer (ChatComposer)
   ========================================================================== */

/* ---------- Chats | Mail ---------- */
.mailbox-switch {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px;
    padding: 4px;
    border: 1px solid var(--sc-line);
    border-radius: var(--sc-radius-pill);
    background: var(--sc-surface);
}

.mailbox-switch-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 40px;
    border: none;
    border-radius: var(--sc-radius-pill);
    background: transparent;
    color: var(--sc-text-muted);
    font-family: var(--sc-font-display);
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
}

.mailbox-switch-btn.is-on {
    background: var(--sc-gradient-primary);
    color: var(--sc-on-primary);
    box-shadow: 0 4px 14px rgba(var(--sc-primary-rgb), 0.3);
}

/* ---------- The chat list ---------- */
.chat-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.chat-list-head {
    display: flex;
    justify-content: flex-end;
}

.chat-new {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 14px;
    border: 1px solid rgba(var(--sc-primary-rgb), 0.5);
    border-radius: var(--sc-radius-pill);
    background: rgba(var(--sc-primary-rgb), 0.12);
    color: var(--sc-text);
    font-weight: 700;
    cursor: pointer;
}

.chat-new.is-on {
    background: var(--sc-surface-2);
}

.chat-rows {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.chat-row-btn {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--sc-line);
    border-radius: var(--sc-radius-lg);
    background: var(--sc-surface);
    color: var(--sc-text);
    text-align: left;
    cursor: pointer;
    transition: transform 0.15s, border-color 0.2s;
}

.chat-row-btn:active {
    transform: scale(0.985);
}

.chat-row-btn.is-unread {
    border-color: rgba(var(--sc-primary-rgb), 0.55);
    background: linear-gradient(135deg, rgba(var(--sc-primary-rgb), 0.12), var(--sc-surface));
}

.chat-row-avatar {
    position: relative;
    flex: none;
    width: 48px;
    height: 48px;
}

.chat-row-avatar img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
}

.chat-row-text {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.chat-row-top,
.chat-row-bottom {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

.chat-row-top strong {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-row-top small {
    flex: none;
    color: var(--sc-text-muted);
    font-size: 12px;
}

.chat-row-preview,
.chat-row-typing {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    color: var(--sc-text-muted);
    font-size: 13px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-row-btn.is-unread .chat-row-preview {
    color: var(--sc-text);
    font-weight: 600;
}

.chat-row-typing {
    color: var(--sc-success, #3ddc97);
    font-style: italic;
}

.chat-row-gift {
    flex: none;
    color: var(--sc-gold);
    font-size: 16px;
}

.chat-row-count {
    display: grid;
    flex: none;
    place-items: center;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: var(--sc-radius-pill);
    background: var(--sc-danger, #ff4d6d);
    color: #fff;
    font-size: 11px;
    font-weight: 800;
}

.chat-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    margin: auto;
    padding: 28px 12px;
    color: var(--sc-text-muted);
    text-align: center;
}

.chat-empty ion-icon {
    font-size: 40px;
    opacity: 0.6;
}

.chat-empty strong {
    color: var(--sc-text);
    font-family: var(--sc-font-display);
    font-size: 17px;
}

/* ---------- A chat (a sheet at the app's root) ---------- */
.popup-content.chat-sheet {
    height: 92vh;
    height: 92dvh;
    padding-bottom: 0;
    overflow: hidden;
}

.popup-content.chat-sheet > .chat {
    flex: 1 1 auto;
    min-height: 0;
}

.chat {
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-height: 0;
}

.chat-head {
    display: flex;
    flex: none;
    align-items: center;
    gap: 10px;
    /* Room on the right for the sheet's close button */
    padding: 0 44px 8px 0;
    border-bottom: 1px solid var(--sc-line);
}

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

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

.chat-head-name strong {
    overflow: hidden;
    font-family: var(--sc-font-display);
    font-size: 17px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-head-name small {
    color: var(--sc-text-muted);
    font-size: 12px;
}

.chat-head-name small.is-typing {
    color: var(--sc-success, #3ddc97);
    font-style: italic;
}

.chat-head-btn {
    display: grid;
    flex: none;
    place-items: center;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 50%;
    background: var(--sc-surface-2);
    color: var(--sc-text);
    font-size: 20px;
    cursor: pointer;
}

.chat-confirm {
    display: flex;
    flex: none;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    border: 1px solid rgba(255, 77, 109, 0.5);
    border-radius: var(--sc-radius);
    background: rgba(255, 77, 109, 0.1);
    font-size: 13px;
}

.chat-confirm span {
    flex: 1 1 100%;
}

.chat-confirm .btn {
    flex: 1;
}

.chat-scroll {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    gap: 3px;
    min-height: 0;
    margin: 0 -18px;
    padding: 6px 14px 10px;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

.chat-more {
    align-self: center;
    margin-bottom: 8px;
    padding: 6px 14px;
    border: 1px solid var(--sc-line);
    border-radius: var(--sc-radius-pill);
    background: var(--sc-surface-2);
    color: var(--sc-text-muted);
    font-size: 13px;
    cursor: pointer;
}

.chat-day {
    display: flex;
    justify-content: center;
    margin: 10px 0 6px;
}

.chat-day span {
    padding: 2px 10px;
    border-radius: var(--sc-radius-pill);
    background: var(--sc-surface-2);
    color: var(--sc-text-muted);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.chat-row {
    display: flex;
}

.chat-row.is-mine {
    justify-content: flex-end;
}

.chat-row.has-tail {
    margin-bottom: 6px;
}

.chat-bubble {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 4px;
    max-width: min(80%, 420px);
    padding: 8px 12px 6px;
    border-radius: 18px;
    background: var(--sc-surface-2);
    color: var(--sc-text);
    animation: chat-in 0.22s var(--sc-ease-out) backwards;
}

.chat-row.is-mine .chat-bubble {
    background: var(--sc-gradient-primary);
    color: var(--sc-on-primary);
}

.chat-row.is-theirs.has-tail .chat-bubble {
    border-bottom-left-radius: 6px;
}

.chat-row.is-mine.has-tail .chat-bubble {
    border-bottom-right-radius: 6px;
}

.chat-bubble.is-note {
    font-style: italic;
}

.chat-text {
    margin: 0;
    font-size: 15px;
    line-height: 1.35;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.chat-time {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    align-self: flex-end;
    font-size: 10px;
    opacity: 0.7;
}

.chat-seen {
    font-size: 13px;
}

/* A present in the thread */
.chat-bubble.is-gift {
    min-width: min(240px, 80%);
}

.chat-gift {
    display: flex;
    align-items: center;
    gap: 10px;
}

.chat-gift-art {
    display: grid;
    flex: none;
    place-items: center;
    width: 40px;
    height: 40px;
    font-size: 30px;
}

.chat-gift-art .mail-giftbox-art {
    transform: scale(0.55);
}

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

.chat-gift-text b {
    font-size: 14px;
    line-height: 1.2;
}

.chat-gift-text small {
    font-size: 11px;
    opacity: 0.8;
}

.chat-gift-open {
    flex: none;
    border-radius: var(--sc-radius-pill);
    font-weight: 700;
}

.chat-gift-back {
    flex: none;
    padding: 0 4px;
    color: inherit;
    font-size: 12px;
    text-decoration: underline;
    opacity: 0.85;
}

/* They're typing */
.chat-typing {
    flex-direction: row;
    gap: 4px;
    padding: 12px 14px;
}

.chat-typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--sc-text-muted);
    animation: chat-dot 1.2s ease-in-out infinite;
}

.chat-typing span:nth-child(2) { animation-delay: 0.15s; }
.chat-typing span:nth-child(3) { animation-delay: 0.3s; }

/* ---------- The composer ---------- */
.chat-composer {
    display: flex;
    flex: none;
    flex-direction: column;
    gap: 6px;
    margin: 0 -18px;
    padding: 8px 12px calc(10px + var(--sc-safe-bottom));
    border-top: 1px solid var(--sc-line);
    background: var(--sc-surface);
}

.chat-compose-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}

.chat-input {
    flex: 1;
    min-width: 0;
    min-height: 42px;
    max-height: 140px;
    padding: 10px 14px;
    border: 1px solid var(--sc-line-strong);
    border-radius: 21px;
    background: var(--sc-surface-2);
    color: var(--sc-text);
    /* 16px stops iPhone Safari zooming in on focus */
    font-size: 16px;
    line-height: 1.3;
    resize: none;
    outline: none;
}

.chat-input:focus {
    border-color: rgba(var(--sc-primary-rgb), 0.7);
}

.chat-gift-btn,
.chat-send {
    display: grid;
    flex: none;
    place-items: center;
    width: 42px;
    height: 42px;
    border: none;
    border-radius: 50%;
    font-size: 20px;
    cursor: pointer;
}

.chat-gift-btn {
    background: var(--sc-surface-2);
    color: var(--sc-gold);
}

.chat-gift-btn.is-on {
    background: rgba(255, 200, 61, 0.2);
    box-shadow: 0 0 0 2px var(--sc-gold);
}

.chat-send {
    background: var(--sc-gradient-primary);
    color: var(--sc-on-primary);
    transition: opacity 0.2s, transform 0.15s;
}

.chat-send:disabled {
    opacity: 0.4;
}

.chat-send:not(:disabled):active {
    transform: scale(0.92);
}

.chat-attached {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 10px;
    border: 1px solid rgba(255, 200, 61, 0.5);
    border-radius: var(--sc-radius);
    background: rgba(255, 200, 61, 0.1);
}

.chat-attached-art {
    display: grid;
    flex: none;
    place-items: center;
    width: 28px;
}

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

.chat-attached-text small {
    color: var(--sc-text-muted);
    font-size: 11px;
}

.chat-attached-text b {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-attached-remove {
    flex: none;
    border: none;
    background: none;
    color: var(--sc-text-muted);
    font-size: 22px;
    cursor: pointer;
}

.chat-error {
    margin: 0;
    color: var(--sc-danger, #ff4d6d);
    font-size: 13px;
}

/* The gift picker */
.chat-attach {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.chat-attach-tabs {
    display: flex;
    gap: 8px;
}

.chat-attach-tabs .compose-attach-option {
    flex: 1;
}

.chat-attach-tabs .compose-attach-option.is-on {
    box-shadow: 0 0 0 2px currentColor;
}

.chat-attach-note {
    margin: 0;
    color: var(--sc-text-muted);
}

@keyframes chat-in {
    from {
        opacity: 0;
        transform: translateY(6px) scale(0.98);
    }
}

@keyframes chat-dot {
    0%, 60%, 100% { transform: none; opacity: 0.5; }
    30% { transform: translateY(-4px); opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
    .chat-bubble,
    .chat-typing span {
        animation: none;
    }
}
