/* Client <-> Richie/Advisor chat. Two things share this file:
   1. The floating chat widget (chat.js) -- global, lives in base.html,
      reachable from every page via the FAB in the bottom-right corner.
   2. The advisor-side embedded chat panel on a client's detail page
      (advisor_client.js's .chat-shell/.chat-window) -- a single ADVISOR-
      channel thread embedded in that page, not a floating widget; the
      advisor's already deep in that specific client's context there.
   Bubbles/input/empty-state styling (.chat-messages, .chat-bubble*,
   .chat-input-row, .chat-empty) is shared by both. */

.chat-tab-badge {
    min-width: 16px;
    height: 16px;
    padding: 0 3px;
    border-radius: var(--radius-full);
    background: var(--danger);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    line-height: 16px;
    text-align: center;
}

.chat-tab-badge.hidden {
    display: none;
}

/* Advisor's embedded single-channel chat panel -------------------------- */

.chat-shell {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    overflow: hidden;
}

.chat-window {
    display: flex;
    flex-direction: column;
    height: 480px;
}

/* Richie/Advisor channel switcher -- a compact pill toggle, used inside
   the floating widget's header. */

.chat-channel-tab {
    display: flex;
    align-items: center;
    gap: 6px;
    background: none;
    border: 1px solid transparent;
    border-radius: var(--radius-full);
    padding: 6px 12px;
    font-size: 12.5px;
    font-weight: 700;
    color: var(--text-muted);
    cursor: pointer;
}

.chat-channel-tab.active {
    color: var(--accent);
    background: var(--accent-soft);
    border-color: var(--accent-border);
}

.chat-channel-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    flex-shrink: 0;
}

.chat-channel-dot-ai {
    background: linear-gradient(135deg, var(--accent), var(--accent-secondary));
}

.chat-channel-dot-human {
    background: var(--text-muted);
}

/* Shared bubble/input/empty-state styling -------------------------------- */

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 18px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.chat-bubble-row {
    display: flex;
}

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

.chat-bubble {
    max-width: 78%;
    padding: 9px 13px;
    border-radius: 14px;
    font-size: 13.5px;
    line-height: 1.5;
}

.chat-bubble-them {
    background: var(--bg-2);
    border: 1px solid var(--border);
    color: var(--text);
    border-bottom-left-radius: 4px;
}

.chat-bubble-mine {
    background: var(--accent);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.chat-bubble-sender {
    font-size: 10.5px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--text-faint);
    margin-bottom: 3px;
}

.chat-bubble-text {
    white-space: pre-wrap;
    word-break: break-word;
}

.chat-empty {
    text-align: center;
    color: var(--text-faint);
    font-size: 13px;
    padding: 24px 0;
}

.chat-error {
    color: var(--danger);
}

.chat-input-row {
    display: flex;
    gap: 8px;
    padding: 12px;
    border-top: 1px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
}

.chat-input-row input {
    flex: 1;
    border: 1px solid var(--border);
    border-radius: var(--radius-full);
    padding: 9px 16px;
    font-size: 13.5px;
    background: var(--bg-2);
    color: var(--text);
}

.chat-input-row input:focus {
    outline: none;
    border-color: var(--accent);
}

.chat-input-row button {
    border: none;
    border-radius: 50%;
    width: 38px;
    height: 38px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--accent);
    color: #fff;
    cursor: pointer;
}

.chat-input-row button svg {
    width: 16px;
    height: 16px;
}

.chat-input-row button:disabled {
    opacity: 0.6;
    cursor: default;
}

/* ==========================================================================
   Floating chat widget -- global, fixed-position, present on every
   authenticated page (see base.html). This is deliberately separate from
   the Richie insights page: a client can open it from Portfolio, Markets,
   Goals, wherever, without navigating anywhere first.
   ========================================================================== */

.chat-widget {
    position: fixed;
    right: 24px;
    bottom: 24px;
    z-index: 70;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 14px;
}

.chat-fab {
    width: 56px;
    height: 56px;
    flex-shrink: 0;
    border-radius: 50%;
    border: none;
    background: linear-gradient(135deg, var(--accent), var(--accent-secondary));
    color: #fff;
    cursor: pointer;
    position: relative;
    box-shadow: 0 10px 28px -8px rgba(15, 23, 42, 0.45), var(--shadow-lg);
    transition: transform 0.15s var(--ease);
}

.chat-fab:hover {
    transform: scale(1.06);
}

.chat-fab:active {
    transform: scale(0.97);
}

.chat-fab-icon {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 24px;
    height: 24px;
    transform: translate(-50%, -50%);
    transition: opacity 0.15s var(--ease), transform 0.15s var(--ease);
}

.chat-fab-icon-close {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.5) rotate(-45deg);
}

.chat-widget.open .chat-fab-icon-chat {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.5) rotate(45deg);
}

.chat-widget.open .chat-fab-icon-close {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1) rotate(0deg);
}

.chat-fab-badge {
    position: absolute;
    top: -2px;
    right: -2px;
    min-width: 20px;
    height: 20px;
    padding: 0 5px;
    border-radius: var(--radius-full);
    background: var(--danger);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    line-height: 20px;
    text-align: center;
    border: 2px solid var(--bg);
}

.chat-fab-badge.hidden {
    display: none;
}

.chat-widget-panel {
    width: 372px;
    max-width: calc(100vw - 32px);
    height: 540px;
    max-height: calc(100vh - 140px);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 18px;
    box-shadow: 0 24px 60px -16px rgba(15, 23, 42, 0.45), var(--shadow-lg);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transform-origin: bottom right;
    animation: chat-panel-in 0.18s var(--ease);
}

.chat-widget-panel.hidden {
    display: none;
}

@keyframes chat-panel-in {
    from { opacity: 0; transform: translateY(14px) scale(0.95); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

.chat-widget-header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 10px 10px 10px 14px;
    background: var(--bg-2);
    border-bottom: 1px solid var(--border);
}

.chat-widget-tabs {
    display: flex;
    gap: 4px;
}

.chat-widget-close {
    flex-shrink: 0;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: none;
    background: none;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}

.chat-widget-close:hover {
    background: var(--surface);
    color: var(--text);
}

.chat-widget-close svg {
    width: 15px;
    height: 15px;
}

/* 860px, not 640px -- matches shell.css's own breakpoint for switching to
   the mobile layout (sidebar hidden, .mobile-tabbar shown). Below that
   width the tab bar is on screen, so the FAB/panel need to clear it and
   the viewport-height quirks below apply -- both true well before a
   phone-only 640px cutoff, e.g. most Android tablets in portrait. */
@media (max-width: 860px) {
    .chat-widget {
        right: 16px;
        /* clears .mobile-tabbar (shell.css) -- includes its own safe-area
           inset since that bar can be taller on devices with a home
           indicator. */
        bottom: calc(88px + env(safe-area-inset-bottom));
    }

    /* Pinned directly to the viewport with explicit top/bottom insets
       instead of a height derived from 100vh anchored above the FAB.
       100vh on mobile browsers is measured against the viewport with the
       address bar HIDDEN (its largest possible size) -- not the actual
       visible area whenever that bar is showing, which is the normal
       case. A height calc()'d from it, stacked bottom-up in a flex
       column above the FAB, pushed the panel's top (its header --
       Richie/Advisor tabs, close button) up underneath/behind that
       address bar instead of clearing it. Fixed top+bottom insets are
       resolved against the real visible viewport regardless, so this
       can't happen no matter how tall the browser's own chrome is at
       the moment the panel opens. */
    .chat-widget-panel {
        position: fixed;
        z-index: 70;
        left: 12px;
        right: 12px;
        top: calc(16px + env(safe-area-inset-top));
        bottom: calc(96px + env(safe-area-inset-bottom));
        width: auto;
        max-width: none;
        height: auto;
        max-height: none;
    }

    .chat-bubble {
        max-width: 88%;
    }
}
