/* Conversation geometry: the log takes the space left by real content. */
#nodeCommentsPanel { container-type: size; container-name: chat-panel; }
#spaceChatTabContent, #directChatTabContent {
    position: absolute;
    inset: var(--chat-header-height, 66px) 0 0;
    min-height: 0;
}
#spaceChatTabContent[style*="block"], #dmActiveView[style*="block"] {
    display: flex !important;
    flex-direction: column;
    min-height: 0;
}
#dmActiveView { height: 100%; }
#spaceChatSearchBar { top: 0 !important; }
#spacePinnedPill { top: 8px !important; }
#spacePinnedPopover { top: 40px !important; }
#spaceChatMessages, #directChatMessages {
    position: relative !important;
    inset: auto !important;
    flex: 1;
    min-height: 0;
    overscroll-behavior: contain;
    overflow-anchor: auto;
}
#spaceChatTyping, #directChatTyping {
    position: relative !important;
    inset: auto !important;
    flex: none;
    height: 16px;
    margin: 0 12px 4px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}
#spaceChatTyping:empty, #directChatTyping:empty { display: none; }
#nodeCommentsPanel .chat-composer {
    position: relative !important;
    inset: auto !important;
    flex: none;
    min-width: 0;
    max-height: 70%;
    overflow-y: auto;
    padding: 8px 12px max(10px, env(safe-area-inset-bottom)) !important;
}
#nodeCommentsPanel .chat-composer-field {
    display: grid;
    grid-template-columns: repeat(4, 36px) 1fr;
    gap: 2px;
    padding: 4px;
    border-radius: 12px;
}
#nodeCommentsPanel .chat-composer-field:focus-within {
    border-color: var(--ui-accent);
    box-shadow: 0 0 0 2px rgba(var(--ui-accent-rgb), .12);
}
#nodeCommentsPanel .chat-composer-field textarea {
    grid-column: 1 / -1;
    width: 100%;
    box-sizing: border-box;
    font-size: 14px !important;
    line-height: 1.45;
    max-height: 120px !important;
}
#nodeCommentsPanel .chat-composer-field textarea:focus-visible { outline: none; }
#nodeCommentsPanel .chat-composer-field .chat-input-btn {
    width: 36px;
    height: 36px;
    color: var(--ui-text-muted);
}
#nodeCommentsPanel .chat-message-text:not(.chat-jumbo) { font-size: 14px !important; line-height: 1.55 !important; }
#nodeCommentsPanel .chat-message-text { overflow-wrap: anywhere; }
#nodeCommentsPanel .chat-message-text pre { max-width: 100%; overflow-x: auto; }
#nodeCommentsPanel .chat-attachments { min-width: 0; }
#nodeCommentsPanel .chat-attachments > div { max-width: 100% !important; }
#nodeCommentsPanel .chat-attachments img { max-width: 100% !important; }
#nodeCommentsPanel :is(button, textarea, [tabindex]):focus-visible, .message-actions button:focus-visible {
    outline: 2px solid var(--ui-accent);
    outline-offset: 2px;
}
.chat-history-state { padding: 24px 18px; color: var(--ui-text-muted); font-size: 14px; line-height: 1.5; text-align: center; }
.chat-history-state button { display: block; margin: 10px auto 0; }

/* Who reads the room's chat (chat/SpaceAudience.js, Mikko 30.9.). */
/* A thin line under the header, a flex child of the pane above the log. */
.chat-here-strip {
    flex: none; align-items: center; gap: 0; width: 100%; min-height: 30px; padding: 4px 14px;
    border: 0; border-bottom: 1px solid var(--ui-border-faint); background: transparent;
    color: var(--ui-text-muted); font: inherit; font-size: 11.5px; text-align: left; cursor: pointer; white-space: nowrap;
}
.chat-here-strip:hover { background: rgba(var(--ui-accent-rgb), 0.06); color: var(--ui-text); }
.chat-here-count { flex: none; color: var(--ui-text); }
.chat-here-note { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; padding-left: 5px; color: var(--ui-text-dim); }
/* The pinned pill floats at the pane's top right: clear of the line. */
#spaceChatTabContent:has(> .chat-here-strip[style*="flex"]) #spacePinnedPill { top: 38px !important; }
#spaceChatTabContent:has(> .chat-here-strip[style*="flex"]) #spacePinnedPopover { top: 70px !important; }
.chat-here-face {
    display: inline-flex; align-items: center; justify-content: center; width: 20px; height: 20px; flex: none;
    border-radius: 50%; font-size: 16px; line-height: 1; background: rgba(var(--ui-surface-rgb), 1);
    box-shadow: 0 0 0 1.5px rgba(var(--ui-surface-rgb), 1); overflow: hidden;
}
.chat-here-strip .chat-here-face { width: 18px; height: 18px; font-size: 14px; }
.chat-here-strip .chat-here-face + .chat-here-face { margin-left: -5px; }
.chat-here-face + .chat-here-count { padding-left: 6px; }
.chat-here-menu { max-width: 300px; }
.chat-here-menu-note { padding: 9px 14px 8px; font-size: 11.5px; line-height: 1.45; color: var(--ui-text-muted); border-bottom: 1px solid var(--ui-border-faint); margin-bottom: 4px; }
.chat-here-menu-empty { padding: 8px 14px; font-size: 12px; color: var(--ui-text-dim); }
.chat-here-menu-row {
    display: flex; align-items: center; gap: 9px; width: 100%; min-height: 40px; padding: 6px 14px;
    border: 0; background: transparent; color: inherit; font: inherit; text-align: left; cursor: pointer;
}
.chat-here-menu-row:hover { background: rgba(var(--ui-accent-rgb), 0.1); }
.chat-here-menu-row .chat-here-face { width: 26px; height: 26px; font-size: 21px; }
.chat-here-menu-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-here-menu-go { display: inline-flex; color: var(--ui-text-dim); }
/* A name or face in the room's chat opens that person's menu. */
#spaceChatMessages .chat-msg:not([data-own]) :is(.chat-msg-sender, .chat-sender-avatar) { cursor: pointer; }
#spaceChatMessages .chat-msg:not([data-own]) .chat-msg-sender:hover { text-decoration: underline; text-underline-offset: 2px; }
/* The room's chat narrowed to two: the way to Direct, offered above the field. */
.chat-direct-hint { display: flex; align-items: center; gap: 4px; margin-bottom: 8px; }
.chat-direct-hint-go {
    flex: 1; min-width: 0; display: inline-flex; align-items: center; gap: 7px; min-height: 32px; padding: 5px 11px;
    border: 1px solid rgba(var(--ui-accent-rgb), 0.28); border-radius: 999px; background: rgba(var(--ui-accent-rgb), 0.08);
    color: var(--ui-text); font: inherit; font-size: 12px; text-align: left; cursor: pointer;
}
.chat-direct-hint-go > span:first-of-type { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-direct-hint-go:hover { background: rgba(var(--ui-accent-rgb), 0.16); }
.chat-direct-hint-x {
    flex: none; width: 32px; height: 32px; border: 0; border-radius: 50%; background: transparent;
    color: var(--ui-text-dim); font-size: 17px; line-height: 1; cursor: pointer;
}
.chat-direct-hint-x:hover { background: rgba(var(--ui-text-rgb), 0.08); color: var(--ui-text); }
.chat-outbox { max-height: 110px; overflow-y: auto; }
.chat-outbox-entry { display: flex; gap: 8px; align-items: center; padding: 7px 0; color: var(--ui-text); font-size: 12px; }
.chat-outbox-entry > span { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chat-outbox-entry button { flex: none; }
#directChatHeader { flex: none; gap: 8px; }
#directChatHeader > div:first-child, #dmUserChip { min-width: 0; }
#directChatUserName { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#nodeCommentsPanel .chat-emoji-popup {
    position: absolute !important;
    bottom: calc(var(--space-composer-height, 100px) + 8px) !important;
    left: 12px;
    right: auto !important;
    box-sizing: border-box;
    width: min(300px, calc(100% - 24px));
    max-height: min(300px, 60%) !important;
}
#nodeCommentsPanel #directChatEmojiPicker { bottom: calc(var(--dm-composer-height, 100px) + 8px) !important; }
#nodeCommentsPanel:has(#spaceChatTabContent[style*="none"]) #spaceChatEmojiPicker,
#nodeCommentsPanel:has(#directChatTabContent[style*="none"]) #directChatEmojiPicker { display: none !important; }
@media (max-width: 768px) {
    #nodeCommentsPanel {
        height: min(72dvh, calc(100dvh - var(--toolbar-height, 64px) - var(--kb-inset, 0px) - 8px));
        max-height: calc(100dvh - var(--toolbar-height, 64px) - var(--kb-inset, 0px) - 8px);
        padding-bottom: 0;
        box-sizing: border-box;
    }
    #nodeCommentsPanel.chat-fullscreen { height: auto; max-height: none; }
    #nodeCommentsPanel .chat-composer-field { grid-template-columns: repeat(4, 40px) 1fr; }
    #nodeCommentsPanel .chat-composer-field textarea { font-size: 16px !important; }
    #nodeCommentsPanel .chat-composer-field .chat-input-btn { width: 40px; height: 40px; }
    #nodeCommentsPanel .chat-send-btn--fab { width: 44px; height: 44px; }
    #conversationBackBtn { font-size: 0 !important; width: 40px; height: 40px; padding: 0 !important; }
    #conversationBackBtn::before { content: '←'; font-size: 24px; }
    #remoteChatVisitBtn { padding: 0 8px !important; }
    #chatHeaderContext { gap: 6px !important; }
    #chatHeaderContext > svg { display: none; }
    #nodeCommentsPanel > div:first-child { padding: 8px !important; gap: 4px !important; }
}
@media (pointer: coarse) {
    .message-actions button { min-width: 44px; min-height: 44px; }
    #spaceChatPending button, #dmChatPending button { min-width: 28px; min-height: 28px; top: 0 !important; right: 0 !important; }
}
@media (max-height: 650px) and (min-width: 769px) {
    #nodeCommentsPanel { top: 76px; bottom: 76px; }
}
@media (max-height: 500px) {
    #nodeCommentsPanel .chat-composer-field textarea { max-height: 64px !important; overflow-y: auto !important; }
    #nodeCommentsPanel .chat-composer { padding-top: 4px !important; padding-bottom: 4px !important; }
}
@media (max-width: 950px) and (max-height: 500px) {
    #nodeCommentsPanel {
        top: calc(var(--toolbar-height, 64px) + 8px);
        bottom: var(--kb-inset, 0px);
        height: auto;
        max-height: none;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
    }
    #nodeCommentsPanel.open { left: 0; }
}
@media (prefers-reduced-motion: reduce) {
    #nodeCommentsPanel, #nodeCommentsPanel *, .message-actions { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
/* A keyboard can leave a very short panel even on a tall layout viewport. */
@container chat-panel (max-height: 320px) {
    #nodeCommentsPanel #spaceChatEmojiPicker, #nodeCommentsPanel #directChatEmojiPicker { top: var(--chat-header-height, 56px); bottom: 4px !important; max-height: none !important; }
    #nodeCommentsPanel > div:first-child { padding: 2px 6px !important; }
    #directChatHeader { padding: 3px 8px !important; }
    #nodeCommentsPanel .chat-composer { padding: 4px 8px !important; }
    #nodeCommentsPanel .chat-composer-field { padding: 2px; grid-template-columns: repeat(4, 30px) 1fr; }
    #nodeCommentsPanel .chat-composer-field textarea { min-height: 32px !important; max-height: 44px !important; padding: 4px 6px !important; overflow-y: auto !important; }
    #nodeCommentsPanel .chat-composer-field .chat-input-btn { width: 30px; height: 30px; }
    #spaceChatReplyIndicator, #directReplyIndicator { padding: 2px 4px !important; margin-bottom: 2px !important; max-height: 20px; overflow: hidden; }
}
/* Canvas references have the same identity for every media type. Regular
   chat attachments deliberately do not carry this frame or navigation. */
.chat-canvas-card {
    display:inline-flex; flex-direction:column; width:300px; max-width:100%;
    margin:6px 0; overflow:hidden; vertical-align:top;
    border:1px solid var(--ui-border-accent); border-radius:10px;
    background:var(--ui-bg-panel, #10252d); text-align:left;
}
.chat-canvas-card-kind { padding:7px 10px; font-size:10px; font-weight:600; color:var(--ui-teal); letter-spacing:.03em; }
.chat-canvas-card > .chat-canvas-card-content { box-sizing:border-box; width:100%; max-width:100% !important; min-width:0 !important; margin:0 !important; border:0 !important; border-radius:0 !important; }
.chat-canvas-card-content > img, .chat-canvas-card-content > video { width:100%; max-width:100% !important; max-height:200px !important; object-fit:contain; border-radius:0 !important; }
.chat-canvas-card-action { display:flex; flex-direction:column; gap:4px; width:100%; padding:9px 10px; border:0; border-top:1px solid var(--ui-border); background:transparent; color:var(--ui-text); text-align:left; cursor:pointer; font:inherit; }
.chat-canvas-card-action strong { overflow:hidden; text-overflow:ellipsis; white-space:nowrap; max-width:100%; font-size:12px; }
.chat-canvas-card-action > span { font-size:11px; color:var(--ui-accent); }
.chat-canvas-card-action:hover { background:rgba(var(--ui-accent-rgb),.09); }
.chat-canvas-card-action:focus-visible { outline:2px solid var(--ui-accent); outline-offset:-3px; }
.chat-canvas-preview { display:block; background:rgba(var(--ui-text-rgb),.035); }
.chat-canvas-preview > img, .chat-canvas-preview > video { display:block; }
.chat-canvas-preview > audio { display:block; width:100%; height:40px; }
.chat-canvas-file-preview { display:flex; align-items:center; gap:12px; padding:14px; color:var(--ui-text-muted); font:600 14px monospace; }
.chat-canvas-file-preview > img { width:40px; height:40px; object-fit:contain; }
.chat-canvas-note-preview { padding:12px; font-size:12px; color:var(--ui-text-muted); max-height:100px; overflow:hidden; overflow-wrap:anywhere; }
.chat-history-nav { display:block; margin:8px auto; }
.chat-history-latest { position:sticky; bottom:6px; z-index:3; background:var(--ui-bg-panel, #10252d) !important; box-shadow:0 2px 8px #0004; }

.chat-history-latest[hidden] { display:none; }
