/* ──────────── Chat split-view (index sub-sidebar + thread pane) ──────────── */
/* Mobile (<1024px): single column — sub-sidebar IS the page when no thread
   is selected; thread takes the viewport when one is (sub-sidebar hidden).
   Desktop (≥1024px): two-column grid — 240px sub-sidebar always visible,
   thread or empty-state on the right.

   The .chat-split fills .page-content (handled in layout.css where
   page-content drops its padding + becomes flex column when chat-split is
   present). */
.chat-split {
    display: grid;
    grid-template-columns: 1fr;
    flex: 1;
    min-height: 0;
}

@media (min-width: 1024px) {
    .chat-split {
        grid-template-columns: 240px 1fr;
    }
}

/* On <1024, when ChatThread is mounted (i.e., a conversation is selected),
   the sub-sidebar collapses so the thread fills the viewport. The thread is
   the page in that state. */
@media (max-width: 1023.98px) {
    .chat-split:has(.chat-page) .chat-side {
        display: none;
    }
}

.chat-side {
    display: flex;
    flex-direction: column;
    min-width: 0;
    border-right: 1px solid var(--color-border);
    overflow-y: auto;
}

/* Chat header layers chat-specific overrides on top of the global .page-header
   pattern (h1 + trailing action). Differences from .page-header: tighter gap
   for the narrow sub-sidebar at desktop, intrinsic padding (because
   .page-content has padding:0 for chat — edge-to-edge thread), no
   margin-bottom (the header sits in a flex column above the conversation
   list, gap is handled by padding-bottom + the list's own padding-top),
   flex-shrink:0 so the header doesn't collapse when the list overflows. */
.chat-side-header {
    gap: var(--space-2);
    padding: var(--space-4);
    margin-bottom: 0;
    flex-shrink: 0;
}

/* Match .page-content's padding scale so the Chat title aligns horizontally
   with the Tasks/Lists title on tablet + desktop. */
@media (min-width: 720px) {
    .chat-side-header {
        padding: var(--space-8) var(--space-8) var(--space-4);
    }
}

.chat-side-list {
    list-style: none;
    margin: 0;
    padding: var(--space-3) var(--space-2);
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
}

@media (min-width: 768px) {
    .chat-side-list {
        gap: 0.5rem;
    }
}

/* Each conversation row is its own white card — same card-row vocabulary as
   the Lists sidebar. The icon (squared mark for group, circular avatar for
   DM) carries the kind, so no section labels needed. */
.chat-side-row {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 10px;
    padding: 0.5rem 0.75rem;
    display: flex;
    align-items: center;
    gap: 0.625rem;
    color: var(--color-text);
    text-decoration: none;
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-medium);
    transition: border-color 140ms ease, background 140ms ease;
}

@media (min-width: 768px) {
    .chat-side-row {
        padding: 0.75rem;
    }
}

.chat-side-row:hover:not(.chat-side-row-active) {
    border-color: var(--color-text-muted);
    text-decoration: none;
}

.chat-side-row-active {
    background: var(--color-accent-soft);
    border-color: var(--color-accent);
    color: var(--color-accent);
    font-weight: var(--font-weight-semibold);
}

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

/* Empty state shown on desktop when no conversation is selected. */
.chat-empty {
    display: none;
}

@media (min-width: 1024px) {
    .chat-empty {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: var(--space-3);
        padding: var(--space-8);
        background: var(--color-surface-alt);
        color: var(--color-text-muted);
        font-size: var(--font-size-sm);
        text-align: center;
    }

    .chat-empty svg {
        width: 48px;
        height: 48px;
        opacity: 0.4;
    }

    .chat-empty p {
        margin: 0;
        max-width: 18rem;
    }
}

.chat-sub {
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
    margin: 0 0 1.1rem;
}

.chat-sub kbd {
    font-size: 0.71rem;
    font-weight: var(--font-weight-medium);
    padding: 0.05rem 0.32rem;
    border-radius: 4px;
    background: var(--color-surface-alt);
    color: var(--color-text);
    border: 1px solid var(--color-border);
    margin: 0 0.1rem;
}

/* ──────────── Chat page wrapper ──────────── */
/* Fills the page-content row so the chat-card can flex:1 inside it instead
   of relying on a 100vh-minus-magic-number fudge for the card's height.
   min-width: 0 breaks the flex/grid min-content chain so an unbreakably-wide
   child (long word in a message, send button) can't push chat-page wider
   than its column — otherwise the whole pane spills out under the sidebar
   at narrow widths. */
.chat-page {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    min-width: 0;
}

/* page-content drops its padding when chat-split is present (so the card +
   sub-sidebar abut the page edges), but the titlebar/intro line still want
   breathing room — mirror what .task-detail-panel does for its titlebar. */
.chat-page > .page-titlebar,
.chat-page > .chat-sub {
    padding-left: var(--space-4);
    padding-right: var(--space-4);
}

.chat-page > .page-titlebar {
    padding-top: var(--space-4);
}

/* Tablet (720–1023.98px) — no sub-sidebar, but the main sidebar is still
   visible, so the chat-card would abut it with no gutter. Move the inset
   onto .chat-page itself at this breakpoint so the whole pane (titlebar,
   sub, card) clears the sidebar by one space step. Cancel the per-child
   horizontal padding to avoid double-stacking. */
@media (min-width: 720px) and (max-width: 1023.98px) {
    .chat-page {
        padding-left: var(--space-4);
        padding-right: var(--space-4);
    }

    .chat-page > .page-titlebar,
    .chat-page > .chat-sub {
        padding-left: 0;
        padding-right: 0;
    }
}

/* ──────────── Chat card ──────────── */
.chat-card {
    display: flex;
    flex-direction: column;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    flex: 1;
    min-height: 0;
    min-width: 0;
    position: relative;
}

.chat-history {
    flex: 1;
    overflow-y: auto;
    padding: 1rem 1.25rem 0.5rem;
    display: flex;
    flex-direction: column;
}

/* In-thread empty state — "No messages yet" inside an open conversation.
   Distinct from .chat-empty (desktop split-pane "pick a conversation"
   placeholder in ChatIndex). */
.chat-thread-empty {
    text-align: center;
    color: var(--color-text-muted);
    padding: 2rem 1rem;
    font-size: 0.875rem;
}

/* ──────────── Load-older pill ──────────── */
.chat-load-older-row {
    display: flex;
    justify-content: center;
    margin-bottom: 0.75rem;
}

.chat-load-older {
    background: transparent;
    color: var(--color-text-muted);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-full);
    padding: 0.25rem 0.75rem;
    font-size: 0.72rem;
    font-weight: 500;
    font-family: inherit;
    letter-spacing: 0.01em;
    cursor: pointer;
    transition: background 140ms ease, color 140ms ease;
}

.chat-load-older:hover:not(:disabled) {
    background: var(--color-surface-alt);
    color: var(--color-text);
}

/* ──────────── Day divider ──────────── */
.chat-day-divider {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    margin: 0.25rem 0 0.875rem;
}

.chat-day-line {
    flex: 1;
    height: 1px;
    background: var(--color-border);
}

.chat-day-label {
    font-size: 0.66rem;
    color: var(--color-text-faint);
    letter-spacing: 0.12em;
    font-weight: 500;
    text-transform: uppercase;
}

/* ──────────── Message row ──────────── */
.chat-msg {
    display: flex;
    gap: 0.625rem;
    padding: 0.5rem 0.375rem 0.0625rem;
    margin: 0 -0.375rem;
    border-radius: var(--radius-sm);
    background: transparent;
    position: relative;
    transition: background 120ms ease;
}

.chat-msg.grouped {
    padding-top: 0.0625rem;
}

@media (hover: hover) and (pointer: fine) {
    .chat-msg:hover {
        background: var(--color-bg-hover);
    }
}

.chat-msg.selected {
    background: var(--color-bg-hover);
}

.chat-msg-gutter {
    width: 32px;
    flex-shrink: 0;
    display: flex;
    justify-content: center;
    padding-top: 0.25rem;
}

.chat-msg.grouped .chat-msg-gutter {
    padding-top: 0.125rem;
}

.chat-msg-time-grouped {
    font-size: 0.625rem;
    color: var(--color-text-faint);
    font-variant-numeric: tabular-nums;
    opacity: 0;
    transition: opacity 120ms ease;
}

@media (hover: hover) and (pointer: fine) {
    .chat-msg:hover .chat-msg-time-grouped {
        opacity: 1;
    }
}

.chat-msg-body {
    flex: 1;
    min-width: 0;
}

.chat-msg-meta {
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
    margin-bottom: 0.125rem;
}

.chat-msg-author {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--color-text);
    letter-spacing: -0.05px;
}

.chat-msg-author.mine {
    color: var(--color-primary);
}

.chat-msg-time {
    font-size: 0.685rem;
    color: var(--color-text-muted);
    font-variant-numeric: tabular-nums;
}

.chat-msg-text {
    font-size: 0.85rem;
    color: var(--color-text);
    line-height: 1.5;
    white-space: pre-wrap;
    word-wrap: break-word;
}

.chat-msg-edited {
    margin-left: 0.375rem;
    font-size: 0.685rem;
    color: var(--color-text-faint);
}

.chat-msg-deleted {
    font-size: var(--font-size-sm);
    color: var(--color-text-faint);
    font-style: italic;
}

.chat-msg-edit {
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
    margin-top: 0.125rem;
}

/* Tighter padding + accent border than the send composer, since the edit
   composer sits inside a message bubble. Inherits the contenteditable
   plumbing (white-space / placeholder) from .chat-input. */
.chat-msg-edit-input {
    padding: 0.375rem 0.575rem;
    border-color: var(--color-primary);
    border-radius: 7px;
    min-height: 30px;
}

/* ──────────── Floating action menu (top-right of message) ──────────── */
.chat-msg-actions {
    position: absolute;
    top: -10px;
    right: 0.5rem;
    display: none;
    gap: 2px;
    padding: 2px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 7px;
    box-shadow: var(--shadow-sm);
    z-index: 1;
}

.chat-msg.selected .chat-msg-actions,
.chat-msg:focus-within .chat-msg-actions {
    display: flex;
}

@media (hover: hover) and (pointer: fine) {
    .chat-msg:hover .chat-msg-actions {
        display: flex;
    }
}

/* ──────────── Composer ──────────── */
.chat-composer {
    display: flex;
    gap: 0.625rem;
    align-items: flex-end;
    padding: 0.75rem 0.875rem;
    background: var(--color-surface-alt);
    border-top: 1px solid var(--color-border);
}

/* Read-only-archive banner — replaces the composer when a DM's scoping
   group has lost a participant. Same border + surface as the composer so
   the chat-side layout doesn't shift; muted type + centred to read as
   informational, not a control. */
.chat-archived {
    padding: var(--space-4) var(--space-3);
    background: var(--color-surface-alt);
    border-top: 1px solid var(--color-border);
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
    text-align: center;
}

/* Wraps the composer + floating @mention picker so the picker can position
   relative to the input without being thrown off by the parent flexbox. */
.chat-composer-input-wrap {
    position: relative;
    flex: 1;
    min-width: 0;
}

/* Contenteditable composer. Mention pills are inline children that the
   user can't type inside (contenteditable=false), so the rest of the
   editable space behaves like a single-line / wrapping plain text input. */
.chat-input {
    width: 100%;
    min-width: 0;
    padding: 0.55rem 0.75rem;
    font-size: 0.85rem;
    font-family: inherit;
    line-height: 1.45;
    color: var(--color-text);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 9px;
    outline: none;
    min-height: 38px;
    max-height: 120px;
    overflow-y: auto;
    white-space: pre-wrap;
    word-break: break-word;
    transition: border-color 140ms ease, box-shadow 140ms ease;
}

/* Placeholder via data-placeholder + .is-empty class managed in JS — :empty
   alone is unreliable because browsers leave a stray <br> after backspace. */
.chat-input.is-empty::before {
    content: attr(data-placeholder);
    color: var(--color-text-muted);
    pointer-events: none;
}

/* Floating dropdown anchored above the textarea — Slack-style positioning
   (above the input, not at the caret) so we don't need a JS mirror-element
   trick to compute caret pixel coords. */
.mention-autocomplete {
    position: absolute;
    bottom: calc(100% + 0.375rem);
    left: 0;
    right: 0;
    max-height: 14rem;
    overflow-y: auto;
    list-style: none;
    margin: 0;
    padding: 0.25rem;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 9px;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
    z-index: 20;
}

.mention-autocomplete-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.375rem 0.5rem;
    border-radius: 6px;
    cursor: pointer;
    color: var(--color-text);
}

.mention-autocomplete-row.active,
.mention-autocomplete-row:hover {
    background: var(--color-surface-alt);
}

.mention-autocomplete-name {
    font-size: 0.85rem;
}

/* Inline mention pill rendered in message bodies. .chat-mention-self is the
   "you were mentioned" highlight — fuller accent fill so it pops out of a
   busy thread. */
.chat-mention {
    display: inline-block;
    padding: 0 0.25em;
    border-radius: 0.3em;
    background: var(--color-primary-ring, rgba(0, 102, 204, 0.12));
    color: var(--color-primary);
    font-weight: 500;
    text-decoration: none;
}

.chat-mention-self {
    background: var(--color-primary);
    color: var(--color-on-primary);
}

.chat-input:focus {
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px var(--color-primary-ring);
}

.chat-send {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.55rem 1rem;
    font-size: 0.825rem;
    font-weight: 500;
    font-family: inherit;
    background: var(--color-primary);
    color: var(--color-on-primary);
    border: none;
    border-radius: 9px;
    cursor: pointer;
    transition: background 140ms ease, opacity 140ms ease;
}

.chat-send:hover:not(:disabled) {
    background: var(--color-primary-hover);
}

.chat-send:disabled {
    background: var(--color-surface);
    color: var(--color-text-faint);
    border: 1px solid var(--color-border);
    cursor: default;
}

.chat-send:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
}

.chat-send svg {
    width: 13px;
    height: 13px;
}

/* ──────────── New-messages pill ──────────── */
.chat-new-pill {
    position: absolute;
    left: 50%;
    bottom: 4.75rem;
    transform: translateX(-50%);
    background: var(--color-primary);
    color: var(--color-on-primary);
    border: none;
    border-radius: var(--radius-full);
    padding: 0.4rem 0.85rem;
    font-size: 0.78rem;
    font-weight: 500;
    cursor: pointer;
    box-shadow: var(--shadow-md);
    z-index: 2;
    transition: transform 160ms ease, box-shadow 160ms ease;
}

.chat-new-pill:hover {
    transform: translateX(-50%) translateY(-1px);
    box-shadow: var(--shadow-lg);
}

.chat-new-pill:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 3px;
}
