/* ============================================================
   chat.css — largest standalone area. Legacy thread chat (.chat-*)
   + FamilyChat system (.family-*) + image lightbox.
   Mobile: 100dvh shell (avoids the iOS URL-bar jump) + safe-area
   on the composer.  (.family-chat-menu-badge lives in components.css
   because it decorates a nav element, not the chat surface.)
   ============================================================ */

/* ---------- legacy thread chat (older system, still in DB) ---------- */
.chat-box { background: #f8fafc; border: 1px solid var(--c-border); border-radius: 18px; padding: 16px; }
.chat-message { margin-bottom: 14px; max-width: 78%; }
.chat-parent { margin-right: auto; }
.chat-kid { margin-left: auto; }
.chat-meta { font-size: 0.78rem; color: var(--c-text-muted); margin-bottom: 4px; display: flex; justify-content: space-between; gap: 12px; }
.chat-bubble { border-radius: 16px; padding: 12px 14px; font-weight: 600; white-space: pre-wrap; }
.chat-parent .chat-bubble { background: #e0f2fe; color: #075985; }
.chat-kid .chat-bubble { background: #dcfce7; color: #166534; }

/* ============================================================
   FamilyChat — WeChat-style messenger
   List ⇄ thread navigation (mobile), conversation rows with
   last-message preview, day separators, grouped bubbles,
   read receipts. Own bubble = WeChat green, incoming = white.
   ============================================================ */
.family-chat-shell {
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: 18px;
    min-height: 620px;
}

.family-chat-sidebar,
.family-chat-main {
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: 22px;
    box-shadow: var(--shadow-card);
    overflow: hidden;
}

/* ---------- conversation list (sidebar) ---------- */
.family-chat-sidebar { display: flex; flex-direction: column; }
.family-chat-sidebar-header { padding: 16px 18px; border-bottom: 1px solid var(--c-border); }
.family-list-title-row { display: flex; align-items: center; justify-content: space-between; }
.family-chat-sidebar-header h2,
.family-chat-header h2 { margin: 0; font-weight: 900; font-size: 1.1rem; }

.current-mood-text { font-size: 0.82rem; color: var(--c-text-muted); cursor: pointer; }
.current-mood-text:hover { color: var(--c-text); text-decoration: underline; }
.mood-edit-panel { margin-top: 8px; }

.family-search-wrap { position: relative; margin-top: 12px; }
.family-search-icon { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); font-size: 0.85rem; opacity: 0.55; pointer-events: none; }
.family-search-input {
    width: 100%; border: 1px solid var(--c-border); background: var(--c-bg);
    border-radius: var(--radius-pill); padding: 9px 14px 9px 34px; font-size: 0.9rem;
    min-height: var(--tap-min);
}
.family-search-input:focus { outline: none; border-color: var(--c-brand-1); background: var(--c-surface); }

.family-member-list { padding: 8px; overflow-y: auto; flex: 1; }
.family-list-empty,
.family-list-empty.alert { margin: 16px; color: var(--c-text-muted); font-weight: 700; text-align: center; }

.family-member-card {
    display: flex; align-items: center; gap: 12px;
    text-decoration: none; color: var(--c-text-strong);
    padding: 10px 12px; border-radius: 14px;
    transition: background 0.14s ease-in-out;
}
.family-member-card:hover { background: var(--c-bg); }
.family-member-card.active { background: #eef2ff; }

.family-avatar {
    width: 48px; height: 48px; border-radius: 14px;
    background: linear-gradient(135deg, var(--c-brand-1), var(--c-brand-2));
    color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-weight: 800; position: relative; flex-shrink: 0; font-size: 1rem;
}
.family-avatar.large { width: 44px; height: 44px; border-radius: 12px; font-size: 0.95rem; }

.family-member-info { min-width: 0; flex: 1; }
.family-member-top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.family-member-name { font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.family-member-time { font-size: 0.72rem; color: var(--c-text-muted); flex-shrink: 0; }
.family-member-bottom { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 2px; }
.family-member-preview { font-size: 0.84rem; color: var(--c-text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }

.family-unread-dot {
    background: #ef4444; color: white; flex-shrink: 0;
    min-width: 20px; height: 20px; padding: 0 6px; border-radius: var(--radius-pill);
    font-size: 12px; display: flex; align-items: center; justify-content: center; font-weight: 800;
}

/* ---------- thread (main) ---------- */
.family-chat-main { display: flex; flex-direction: column; }
.family-chat-header { padding: 14px 18px; border-bottom: 1px solid var(--c-border); display: flex; align-items: center; gap: 12px; }
.family-chat-header-info { min-width: 0; }
.family-chat-header-sub { font-size: 0.8rem; color: var(--c-text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.family-back-btn {
    display: none; border: none; background: transparent; cursor: pointer;
    font-size: 1.8rem; line-height: 1; color: var(--c-brand-1); font-weight: 900;
    width: 36px; height: 36px; flex-shrink: 0; border-radius: 10px;
}
.family-back-btn:hover { background: var(--c-bg); }

.family-chat-messages { flex: 1; height: 390px; overflow-y: auto; padding: 16px 16px 8px; background: var(--c-bg); }
.family-loading { text-align: center; color: var(--c-text-muted); margin-top: 24px; font-weight: 600; }

/* day / time separator pill */
.family-day-sep { display: flex; justify-content: center; margin: 14px 0; }
.family-day-sep span {
    background: rgba(100, 116, 139, 0.16); color: var(--c-text-muted);
    font-size: 0.72rem; font-weight: 700; padding: 3px 12px; border-radius: var(--radius-pill);
}

/* message row: avatar + content; mine flips to the right */
.family-msg-row { display: flex; align-items: flex-end; gap: 8px; margin-bottom: 8px; }
.family-msg-row.mine { flex-direction: row-reverse; }
.family-msg-row.grouped { margin-bottom: 2px; }
.family-msg-row.grouped .family-msg-avatar { visibility: hidden; }

.family-msg-avatar {
    width: 36px; height: 36px; border-radius: 10px; flex-shrink: 0;
    background: linear-gradient(135deg, var(--c-brand-1), var(--c-brand-2)); color: #fff;
    display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 0.78rem;
}
.family-msg-row.mine .family-msg-avatar { background: linear-gradient(135deg, #16a34a, #22c55e); }

.family-msg-content { display: flex; flex-direction: column; max-width: 74%; }
.family-msg-row.mine .family-msg-content { align-items: flex-end; }

/* NOTE: keep white-space: pre-wrap — and never indent @message.Text in Razor (renders blank lines). */
.family-message-bubble {
    display: inline-block; max-width: 100%;
    padding: 8px 12px; border-radius: 14px;
    font-weight: 500; line-height: 1.35; font-size: 0.95rem;
    white-space: pre-wrap; overflow-wrap: anywhere; word-break: normal;
    box-shadow: 0 1px 1px rgba(15, 23, 42, 0.06);
}
.family-msg-row.theirs .family-message-bubble { background: #ffffff; color: var(--c-text-strong); border-top-left-radius: 4px; }
.family-msg-row.mine .family-message-bubble { background: #95ec69; color: #0b3d12; border-top-right-radius: 4px; }

.family-read-status { font-size: 0.66rem; color: var(--c-text-muted); margin-top: 2px; font-weight: 600; }

/* ---------- composer ---------- */
.family-chat-input-area { border-top: 1px solid var(--c-border); padding: 10px 14px; background: var(--c-surface); }
.family-emoji-row { display: flex; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.emoji-btn { border: 1px solid var(--c-border); background: var(--c-bg); border-radius: var(--radius-pill); padding: 6px 10px; font-size: 1.1rem; cursor: pointer; }

.family-input-row { display: flex; align-items: flex-end; gap: 8px; }
.family-icon-btn {
    min-width: var(--tap-min); height: var(--tap-min); flex-shrink: 0;
    border: 1px solid var(--c-border); background: var(--c-bg); border-radius: var(--radius-pill);
    display: flex; align-items: center; justify-content: center; font-size: 1.2rem; cursor: pointer;
}
.family-icon-btn:hover { background: var(--c-surface); }
.family-textarea {
    flex: 1; resize: none; border-radius: 20px; padding: 10px 14px;
    min-height: var(--tap-min); max-height: 120px; line-height: 1.3;
}
.family-send-btn {
    min-height: var(--tap-min); flex-shrink: 0;
    border: none; border-radius: var(--radius-pill); padding: 0 18px;
    background: linear-gradient(135deg, var(--c-brand-1), var(--c-brand-2)); color: #fff;
    font-weight: 800; cursor: pointer; display: flex; align-items: center; gap: 6px;
}
.family-send-btn:hover { filter: brightness(1.06); }
.family-send-icon { display: none; font-size: 1.1rem; }

.family-chat-empty { padding: 40px; text-align: center; color: var(--c-text-muted); font-weight: 800; display: flex; flex-direction: column; align-items: center; gap: 12px; height: 100%; justify-content: center; }
.family-chat-empty-emoji { font-size: 3rem; }

/* image support */
.family-image-btn { position: relative; overflow: hidden; }
.family-image-preview-wrap { background: var(--c-bg); border: 1px dashed #94a3b8; border-radius: 14px; padding: 8px; margin-bottom: 8px; display: inline-flex; align-items: flex-start; gap: 8px; position: relative; }
.family-image-preview { max-width: 140px; max-height: 100px; border-radius: 10px; border: 1px solid var(--c-border); object-fit: contain; background: white; }
.family-image-clear { border: none; background: rgba(15, 23, 42, 0.7); color: #fff; width: 24px; height: 24px; border-radius: var(--radius-pill); font-size: 16px; line-height: 1; cursor: pointer; }
.family-chat-image { max-width: 240px; max-height: 220px; border-radius: 12px; border: 1px solid var(--c-border); display: block; margin-top: 2px; object-fit: contain; background: white; }

/* online/offline dot */
.family-online-dot { position: absolute; bottom: -2px; right: -2px; width: 13px; height: 13px; border-radius: var(--radius-pill); border: 2px solid white; box-shadow: 0 2px 6px rgba(15, 23, 42, 0.2); }
.family-online-dot.online { background: #22c55e; }
.family-online-dot.offline { background: #94a3b8; }

/* lightbox */
.clickable-chat-image { cursor: zoom-in; }
.chat-image-lightbox { position: fixed; inset: 0; z-index: 9999; background: rgba(15, 23, 42, 0.86); display: flex; align-items: center; justify-content: center; padding: 24px; }
.chat-image-lightbox-img { max-width: 94vw; max-height: 90vh; border-radius: 16px; background: white; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45); }
.chat-image-lightbox-close { position: fixed; top: 18px; right: 24px; width: 42px; height: 42px; border: none; border-radius: var(--radius-pill); background: white; color: var(--c-text-strong); font-size: 28px; line-height: 1; font-weight: 900; cursor: pointer; }

/* ---------- Mobile: native list ⇄ thread navigation, full-height (100dvh) ---------- */
@media (max-width: 768px) {
    .chat-message { max-width: 100%; }

    .family-chat-shell {
        grid-template-columns: 1fr;
        gap: 0;
        /* fill the screen below the slim top bar; dvh avoids the iOS URL-bar jump */
        min-height: 0;
        height: calc(100dvh - var(--topbar-h) - var(--space-4));
    }

    .family-chat-sidebar,
    .family-chat-main { border-radius: 18px; }

    /* Default screen = the conversation LIST. Tapping a contact adds
       .viewing-thread (set in JS) to slide into the thread; the in-header
       back button removes it. This is the fix for "stuck on one chat". */
    .family-chat-shell .family-chat-sidebar { display: flex; }
    .family-chat-shell .family-chat-main { display: none; }
    .family-chat-shell.viewing-thread .family-chat-sidebar { display: none; }
    .family-chat-shell.viewing-thread .family-chat-main { display: flex; }

    .family-chat-sidebar { height: 100%; }
    .family-chat-main { height: 100%; }
    .family-chat-messages { height: auto; flex: 1; }

    .family-back-btn { display: flex; align-items: center; justify-content: center; }

    .family-msg-content { max-width: 80%; }

    /* icon-only send to save width */
    .family-send-btn { padding: 0; width: var(--tap-min); justify-content: center; }
    .family-send-icon { display: inline; }
    .family-send-text { display: none; }

    /* clear the iOS home indicator under the composer */
    .family-chat-input-area { padding-bottom: calc(10px + var(--safe-bottom)); }

    .family-chat-image { max-width: 78vw; }
}
