/* === FLOATING CHAT WIDGET === */
/* Inspired by Intercom/Zendesk: bottom-right bubble that expands to a
 * draggable, resizable chat window. No new tabs. Two modes: model chat
 * (default) + support chat. State persisted in localStorage. */

:root {
    --widget-size-mini: 56px;
    --widget-size-default: 380px;
    --widget-size-default-height: 540px;
    --widget-z: 9998;
}

#chat-widget-bubble {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: var(--widget-size-mini);
    height: var(--widget-size-mini);
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), #8b5cf6);
    color: white;
    border: none;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
    cursor: pointer;
    z-index: var(--widget-z);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, box-shadow 0.2s;
}

#chat-widget-bubble:hover {
    transform: scale(1.05);
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.3);
}

#chat-widget-bubble.has-unread::after {
    content: "";
    position: absolute;
    top: 6px;
    right: 6px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: #ef4444;
    border: 2px solid var(--bg, #1a1a2e);
}

#chat-widget-window {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: var(--widget-size-default);
    height: var(--widget-size-default-height);
    max-width: calc(100vw - 40px);
    max-height: calc(100vh - 40px);
    background: var(--bg, #1a1a2e);
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.4);
    z-index: var(--widget-z);
    display: none;
    flex-direction: column;
    overflow: hidden;
    font-size: 14px;
}

#chat-widget-window.is-open { display: flex; }

.chat-widget-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    background: var(--surface, #16213e);
    border-bottom: 1px solid var(--border);
    cursor: move;
    user-select: none;
}

.chat-widget-header-title {
    flex: 1;
    font-weight: 600;
    font-size: 0.95rem;
    display: flex;
    align-items: center;
    gap: 6px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-widget-header-actions {
    display: flex;
    gap: 4px;
    align-items: center;
}

.chat-widget-icon-btn {
    width: 28px;
    height: 28px;
    border: none;
    background: transparent;
    color: var(--text-muted, #888);
    cursor: pointer;
    border-radius: 6px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.chat-widget-icon-btn:hover {
    background: rgba(255, 255, 255, 0.08);
    color: var(--text, #fff);
}

.chat-widget-mode-tabs {
    display: flex;
    border-bottom: 1px solid var(--border);
    background: var(--surface, #16213e);
}

.chat-widget-mode-tab {
    flex: 1;
    padding: 8px 12px;
    background: transparent;
    border: none;
    color: var(--text-muted, #888);
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 500;
    border-bottom: 2px solid transparent;
    transition: color 0.15s, border-color 0.15s;
}

.chat-widget-mode-tab.active {
    color: var(--text, #fff);
    border-bottom-color: var(--primary);
}

.chat-widget-mode-tab:hover:not(.active) {
    color: var(--text, #fff);
}

.chat-widget-toolbar {
    display: flex;
    gap: 6px;
    padding: 6px 10px;
    background: var(--surface, #16213e);
    border-bottom: 1px solid var(--border);
    align-items: center;
}

.chat-widget-toolbar select {
    flex: 1;
    padding: 5px 8px;
    padding-right: 24px;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--bg, #1a1a2e);
    color: var(--text, #fff);
    font-size: 0.8rem;
    text-overflow: ellipsis;
    max-width: 100%;
}

.chat-widget-messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    scroll-behavior: smooth;
}

.chat-widget-footer {
    padding: 4px 10px;
    font-size: 0.7rem;
    color: var(--text-muted, #888);
    background: var(--surface, #16213e);
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    gap: 8px;
}

.chat-widget-footer span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-widget-msg {
    max-width: 88%;
    padding: 8px 12px;
    border-radius: 12px;
    line-height: 1.4;
    white-space: pre-wrap;
    word-break: break-word;
    font-size: 0.88rem;
}

.chat-widget-msg.user {
    align-self: flex-end;
    background: var(--primary);
    color: white;
    border-bottom-right-radius: 4px;
}

.chat-widget-msg.assistant {
    align-self: flex-start;
    background: var(--surface, #16213e);
    color: var(--text, #fff);
    border-bottom-left-radius: 4px;
}

.chat-widget-msg.system {
    align-self: center;
    background: rgba(239, 68, 68, 0.15);
    color: #fca5a5;
    font-size: 0.8rem;
    font-style: italic;
    text-align: center;
}

.chat-widget-msg .think {
    display: block;
    font-style: italic;
    font-size: 0.8em;
    color: var(--text-muted, #888);
    border-left: 2px solid var(--border);
    padding-left: 6px;
    margin-bottom: 6px;
}

/* lobehub-style <details> think block (collapsible reasoning) */
.chat-widget-think {
    background: rgba(99, 102, 241, 0.08);
    border-left: 3px solid var(--primary);
    border-radius: 6px;
    padding: 6px 10px;
    margin: 4px 0 8px;
    font-size: 0.82rem;
    color: var(--text-muted, #888);
}

.chat-widget-think summary {
    cursor: pointer;
    font-style: italic;
    font-size: 0.78rem;
    user-select: none;
    outline: none;
    list-style: none;
}

.chat-widget-think summary::-webkit-details-marker { display: none; }
.chat-widget-think summary::marker { content: ""; }
.chat-widget-think summary::before {
    content: "▸ ";
    display: inline-block;
    transition: transform 0.15s;
}
.chat-widget-think[open] summary::before {
    content: "▾ ";
}

.chat-widget-think-content {
    margin-top: 6px;
    padding: 4px 0 0 6px;
    line-height: 1.45;
    color: var(--text, #ddd);
}

/* Markdown code blocks (lobehub-style) */
.chat-widget-msg pre {
    background: rgba(0, 0, 0, 0.45);
    padding: 8px 10px;
    border-radius: 6px;
    overflow-x: auto;
    font-size: 0.78rem;
    margin: 4px 0;
    border: 1px solid var(--border);
}

.chat-widget-msg pre code {
    background: transparent;
    padding: 0;
    color: #e6e6e6;
    font-family: ui-monospace, "SF Mono", Menlo, monospace;
}

.chat-widget-msg code {
    background: rgba(0, 0, 0, 0.3);
    padding: 1px 5px;
    border-radius: 3px;
    font-family: ui-monospace, monospace;
    font-size: 0.85em;
    color: #f0a8d8;
}

.chat-widget-msg blockquote {
    border-left: 3px solid var(--primary);
    padding-left: 10px;
    margin: 4px 0;
    color: var(--text-muted, #aaa);
    font-style: italic;
}

.chat-widget-msg table {
    border-collapse: collapse;
    margin: 4px 0;
    font-size: 0.82rem;
}

.chat-widget-msg th, .chat-widget-msg td {
    border: 1px solid var(--border);
    padding: 4px 8px;
    text-align: left;
}

.chat-widget-msg th {
    background: rgba(99, 102, 241, 0.1);
    font-weight: 600;
}

.chat-widget-msg ul, .chat-widget-msg ol {
    margin: 4px 0;
    padding-left: 20px;
}

.chat-widget-msg a {
    color: var(--primary);
    text-decoration: underline;
}

.chat-widget-msg.streaming::after {
    content: "▊";
    animation: widget-blink 1s infinite;
    color: var(--primary);
}

@keyframes widget-blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}

.chat-widget-welcome {
    text-align: center;
    padding: 20px 8px;
    color: var(--text-muted, #888);
}

.chat-widget-welcome strong {
    color: var(--text, #fff);
    display: block;
    margin-bottom: 4px;
}

.chat-widget-starters {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    margin-top: 12px;
}

.chat-widget-starter {
    padding: 6px 10px;
    border: 1px solid var(--border);
    background: transparent;
    color: var(--text, #fff);
    border-radius: 6px;
    font-size: 0.78rem;
    cursor: pointer;
    text-align: left;
    transition: border-color 0.15s, background 0.15s;
}

.chat-widget-starter:hover {
    border-color: var(--primary);
    background: rgba(99, 102, 241, 0.08);
}

.chat-widget-input-area {
    display: flex;
    gap: 6px;
    padding: 8px;
    background: var(--surface, #16213e);
    border-top: 1px solid var(--border);
}

.chat-widget-input-area textarea {
    flex: 1;
    padding: 8px 10px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--bg, #1a1a2e);
    color: var(--text, #fff);
    resize: none;
    min-height: 36px;
    max-height: 120px;
    font-size: 0.85rem;
    font-family: inherit;
}

.chat-widget-input-area textarea:focus {
    outline: none;
    border-color: var(--primary);
}

.chat-widget-send-btn {
    width: 36px;
    height: 36px;
    border: none;
    background: var(--primary);
    color: white;
    border-radius: 8px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.chat-widget-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.chat-widget-stop-btn {
    width: 36px;
    height: 36px;
    border: none;
    background: var(--danger, #ef4444);
    color: white;
    border-radius: 8px;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
}

.chat-widget-footer {
    padding: 4px 10px;
    font-size: 0.7rem;
    color: var(--text-muted, #888);
    background: var(--surface, #16213e);
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
}

.chat-widget-resize-handle {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 16px;
    height: 16px;
    cursor: nwse-resize;
    background: linear-gradient(135deg, transparent 50%, var(--text-muted, #888) 50%);
    border-bottom-right-radius: 14px;
}

/* Mobile: full-screen on small viewports */
@media (max-width: 600px) {
    #chat-widget-window {
        bottom: 0;
        right: 0;
        left: 0;
        top: 0;
        width: 100vw;
        height: 100vh;
        max-width: 100vw;
        max-height: 100vh;
        border-radius: 0;
    }
    .chat-widget-resize-handle { display: none; }
}
/* ─── V172: Conversation-Dropdown im Header ─── */
.chat-widget-title-btn {
    background: none;
    border: none;
    color: inherit;
    font: inherit;
    font-weight: 600;
    padding: 4px 6px;
    border-radius: 4px;
    cursor: pointer;
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    text-align: left;
}
.chat-widget-title-btn:hover {
    background: rgba(255, 255, 255, 0.08);
}
.chat-widget-title-btn[aria-expanded="true"] {
    background: rgba(255, 255, 255, 0.12);
}

.chat-widget-conv-dropdown {
    position: absolute;
    top: 48px;
    left: 8px;
    right: 8px;
    background: var(--surface, #1a1d24);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.32);
    z-index: 10;
    max-height: 320px;
    overflow-y: auto;
    padding: 4px;
    min-width: 220px;
}
.chat-widget-conv-dropdown[hidden] { display: none; }

.chat-widget-conv-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.85rem;
    color: var(--text, #fff);
    background: none;
    border: none;
    width: 100%;
    text-align: left;
    transition: background-color 0.1s;
}
.chat-widget-conv-item:hover {
    background: rgba(255, 255, 255, 0.08);
}
.chat-widget-conv-item.active {
    background: rgba(91, 141, 239, 0.18);
    color: var(--primary, #5b8def);
    font-weight: 600;
}
.chat-widget-conv-item.chat-widget-conv-new {
    color: var(--primary, #5b8def);
    font-weight: 600;
    border-bottom: 1px solid var(--border);
    border-radius: 6px 6px 0 0;
}
.chat-widget-conv-item-title {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.chat-widget-conv-item-delete {
    flex-shrink: 0;
    background: none;
    border: none;
    color: var(--text-muted, #888);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    opacity: 0;
    transition: opacity 0.1s, color 0.1s;
    display: flex;
    align-items: center;
    justify-content: center;
}
.chat-widget-conv-item:hover .chat-widget-conv-item-delete {
    opacity: 1;
}
.chat-widget-conv-item-delete:hover {
    color: var(--danger, #ef4444);
    background: rgba(239, 68, 68, 0.1);
}
.chat-widget-conv-divider {
    height: 1px;
    background: var(--border);
    margin: 4px 6px;
}

/* V173-Fix: Hide the widget FAB on the native /chat page.
 * The user is already IN chat — the bubble overlaps the send
 * button and is redundant. JS-side init also skips, but this
 * CSS rule is defense-in-depth in case JS hasn't run yet.
 */
body:has(.chat-page) #chat-widget-bubble,
body:has(.chat-page) #chat-widget-window {
    display: none !important;
}
