/* =========================================
   PREMIUM CHAT UI (Starlight Link)
   ========================================= */

#chat {
    display: none;
    flex-direction: column;
    flex: 1;
    height: 100%;
    position: relative;
    overflow: hidden;
}

#chat.active {
    display: flex !important;
}

.chat-main-area {
    flex: 1;
    overflow-y: auto;
    padding: 10px 0 !important;
    display: flex;
    flex-direction: column;
    gap: 16px;
    scroll-behavior: smooth;
}

/* Scrollbar Styles */
.chat-main-area::-webkit-scrollbar {
    width: 6px;
}

.chat-main-area::-webkit-scrollbar-track {
    background: transparent;
}

.chat-main-area::-webkit-scrollbar-thumb {
    background: rgba(157, 141, 241, 0.2);
    border-radius: 10px;
}

/* Chat Rows & Animations */
.chat-row {
    display: flex;
    width: 100%;
    margin-bottom: 8px;
    animation: chatMessageFadeIn 0.4s cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes chatMessageFadeIn {
    from {
        opacity: 0;
        transform: translateY(12px) scale(0.98);
    }

    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.user-row {
    justify-content: flex-end;
}

/* AI Row: Vertical Stack (Icon Above Message) */
.ai-row {
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
    gap: 6px;
    margin-top: 8px;
}

.ai-row .chat-avatar {
    margin-bottom: 4px;
    /* Separation from bubble */
}

/* Base Avatar Style */
.chat-avatar {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background-color: white;
    background-image: var(--rui-icon-url, url('rui_dot.png'));
    background-size: cover;
    background-position: center;
    box-shadow: 0 4px 12px rgba(157, 141, 241, 0.2);
    border: 2px solid white;
    flex-shrink: 0;
    display: block !important;
}

/* Message Container */
.message-container {
    display: flex;
    flex-direction: column;
    max-width: 88%;
    position: relative;
}

.user-row .message-container {
    align-items: flex-end;
}

.ai-row .message-container {
    align-items: flex-start;
}

/* Chat Bubbles */
.chat-bubble {
    padding: 12px 18px;
    font-size: 0.8rem;
    line-height: 1.5;
    word-wrap: break-word;
    position: relative;
    transition: all 0.2s ease;
    white-space: pre-wrap;
    /* Preserve newlines */
}

.user-bubble {
    background: #ffffff;
    color: var(--ink);
    border-radius: 20px 20px 4px 20px;
    box-shadow: var(--neu-upper), var(--neu-lower);
    border: none;
}

.ai-bubble {
    background: #ffffff;
    color: var(--ink);
    border-radius: 4px 20px 20px 20px;
    box-shadow: var(--neu-upper), var(--neu-lower);
    border: none;
}

.chat-timestamp {
    font-size: 0.7rem;
    color: var(--muted);
    margin-top: 6px;
    padding: 0 8px;
    font-weight: 500;
}

/* Chat Message Actions */
.chat-actions {
    display: flex;
    gap: 4px;
    margin-top: 4px;
    opacity: 0;
    transition: opacity 0.2s;
    padding: 0 4px;
}

.chat-row:hover .chat-actions {
    opacity: 1;
}

.chat-action-btn {
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid rgba(157, 141, 241, 0.1);
    border-radius: 50%;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--rui-purple);
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.04);
    padding: 0;
}

.chat-action-btn:hover {
    background: white;
    color: var(--rui-purple-accent);
    border-color: var(--rui-purple-light);
    transform: scale(1.15) translateY(-2px);
    box-shadow: 0 6px 15px rgba(157, 141, 241, 0.2);
}

.chat-action-btn:active {
    transform: scale(0.95);
}

.user-row .chat-actions {
    justify-content: flex-end;
}

.ai-row .chat-actions {
    justify-content: flex-start;
}

#chat-typing-indicator {
    padding: 0;
    animation: none !important;
    /* Appear instantly */
    opacity: 1 !important;
}

/* Scroll to Bottom Button */
.chat-scroll-btn {
    position: absolute;
    bottom: 120px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid rgba(255, 255, 255, 0.3);
    color: var(--rui-purple);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    z-index: 100;
    transition: all 0.3s ease;
}

.chat-scroll-btn:hover {
    background: rgba(255, 255, 255, 0.4);
    transform: translateX(-50%) translateY(-2px);
}

/* Thinking Bubble (Dots) Specifics */
.thinking-bubble {
    padding: 10px 18px !important;
    min-width: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(255, 255, 255, 0.9) !important;
    border-radius: 20px !important;
    box-shadow: var(--neu-upper), var(--neu-lower);
    margin-top: 4px;
}

.typing-dots-group {
    display: flex;
    gap: 6px;
    align-items: center;
}

.typing-dot {
    width: 10px;
    height: 10px;
    background: var(--rui-purple-accent);
    /* Darker purple for visibility */
    border-radius: 50%;
    opacity: 0.6;
    animation: typingBounce 1.4s infinite ease-in-out;
}

.typing-dot:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-dot:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typingBounce {

    0%,
    80%,
    100% {
        transform: scale(0.6);
        opacity: 0.4;
    }

    40% {
        transform: scale(1.2);
        opacity: 1;
    }
}

/* Inline Edit Styles */
.edit-bubble-textarea {
    width: 100%;
    min-width: 200px;
    background: rgba(0, 0, 0, 0.05);
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    padding: 8px;
    font-size: 0.8rem;
    font-family: inherit;
    color: inherit;
    resize: none;
    outline: none;
}

.edit-bubble-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 8px;
}

.edit-bubble-actions button {
    padding: 4px 12px;
    border-radius: 6px;
    font-size: 0.75rem;
    cursor: pointer;
    border: 1px solid transparent;
    transition: all 0.2s;
}

.edit-bubble-actions .save-btn {
    background: var(--rui-purple);
    color: white;
}

.edit-bubble-actions .cancel-btn {
    background: #eee;
    color: #666;
}

.edit-bubble-actions button:hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* Input Panel */
.chat-input-panel {
    padding: 12px 20px 24px;
    background: rgba(255, 255, 255, 0.6);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-top: 1px solid rgba(0, 0, 0, 0.05);
    position: relative;
    z-index: 10;
}

/* Plus Menu Styles */
.plus-menu-container {
    position: relative;
}

#btn-plus {
    width: 48px;
    height: 48px;
    border-radius: 50% !important;
    padding: 0 !important;
    background: white !important;
    color: var(--rui-purple);
    box-shadow: var(--shadow-soft);
}

.plus-dropdown {
    position: absolute;
    bottom: 60px;
    left: 0;
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(20px);
    border-radius: 20px;
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    border: 1px solid rgba(0, 0, 0, 0.05);
    z-index: 100;
    min-width: 140px;
}

.plus-dropdown button {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border: none;
    background: transparent;
    color: var(--ink);
    border-radius: 12px;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s;
}

/* Chat Input Area */
#chat-input {
    flex: 1;
    min-height: 48px;
    max-height: 150px;
    resize: none;
    border-radius: 24px !important;
    padding: 12px 20px !important;
    font-size: 0.95rem !important;
    line-height: 1.5 !important;
    background: white !important;
    box-shadow: var(--shadow-soft) !important;
    border: none !important;
    outline: none !important;
}

#btn-send-chat {
    width: 48px;
    height: 48px;
    min-width: 48px;
    border-radius: 50% !important;
    padding: 0 !important;
    background: var(--rui-purple) !important;
    color: white !important;
    box-shadow: 0 4px 15px rgba(157, 141, 241, 0.3) !important;
    border: none !important;
}

@media (max-width: 768px) {
    .message-container {
        max-width: 92%;
    }

    .chat-input-panel {
        padding: 12px 12px calc(12px + env(safe-area-inset-bottom, 20px)) !important;
        position: fixed !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        width: 100% !important;
        z-index: 2000 !important;
        background: rgba(255, 255, 255, 0.98);
        box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.05);
    }

    .chat-main-area {
        padding-bottom: 180px !important;
    }
}

/* Sidebar History List Styles */
.chat-history-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    margin: 4px 10px;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    color: var(--ink-sub);
    font-size: 0.85rem;
    position: relative;
}

.chat-history-item.active {
    background: white;
    color: var(--rui-purple);
    font-weight: 700;
    box-shadow: var(--shadow-soft);
}

.chat-history-item-actions {
    position: absolute;
    right: 12px;
    display: flex;
    gap: 6px;
    opacity: 0;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    transform: translateX(10px);
    pointer-events: none;
}

.chat-history-item:hover .chat-history-item-actions {
    opacity: 1;
    transform: translateX(0);
    pointer-events: auto;
}

.history-action-btn {
    width: 28px;
    height: 28px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid rgba(0, 0, 0, 0.05);
    background: white;
    color: var(--muted);
    cursor: pointer;
    transition: all 0.2s;
    font-size: 0.85rem;
}

.history-action-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.history-action-btn.edit:hover {
    color: var(--rui-purple);
    border-color: var(--rui-purple-light);
}

.history-action-btn.delete:hover {
    color: #ff5f5f;
    border-color: #ffcfcf;
    background: #fffcfc;
    transform: translateY(-2px) scale(1.05);
    /* Extra pop for delete */
}

/* Ensure premium font/rendering for emojis if used */
.history-action-btn {
    font-family: inherit;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
}

.history-title {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    margin-right: 40px;
    /* Space for hidden actions */
}

.chat-input-wrapper {
    display: flex;
    gap: 8px;
    align-items: flex-end;
    max-width: 800px;
    margin: 0 auto;
    width: 100%;
}

/* ===== COMPREHENSIVE DARK MODE FOR CHAT ===== */

body.dark-theme .ai-bubble {
    background: #1a1a1a !important;
    color: #ffffff !important;
    border-color: rgba(157, 141, 241, 0.2) !important;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3) !important;
}

body.dark-theme .user-bubble {
    background: var(--rui-purple) !important;
    color: white !important;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3) !important;
}

body.dark-theme #chat-input {
    background: #1a1a1a !important;
    color: #ffffff !important;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3) !important;
}

body.dark-theme #btn-plus {
    background: #1a1a1a !important;
    color: var(--rui-purple) !important;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3) !important;
}

body.dark-theme .plus-dropdown {
    background: rgba(20, 20, 20, 0.95) !important;
    border-color: rgba(255, 255, 255, 0.1) !important;
}

body.dark-theme .plus-dropdown button {
    color: #ffffff !important;
}

body.dark-theme .plus-dropdown button:hover {
    background: rgba(157, 141, 241, 0.15) !important;
}

body.dark-theme .chat-history-item.active {
    background: rgba(157, 141, 241, 0.15) !important;
    color: var(--rui-purple) !important;
}

body.dark-theme .chat-history-item:hover {
    background: rgba(255, 255, 255, 0.05) !important;
}

body.dark-theme .chat-input-area {
    background: #0a0a0a !important;
    border-top-color: rgba(255, 255, 255, 0.06) !important;
}

body.dark-theme .chat-input-panel {
    background: rgba(0, 0, 0, 0.7) !important;
    border-top-color: rgba(255, 255, 255, 0.06) !important;
}

body.dark-theme #btn-plus {
    background: #1a1a1a !important;
    color: var(--rui-purple) !important;
}

body.dark-theme .plus-dropdown {
    background: #1a1a1a !important;
    border-color: rgba(255, 255, 255, 0.08) !important;
}

body.dark-theme .plus-dropdown button {
    color: var(--ink) !important;
}

body.dark-theme .chat-avatar {
    background-color: #111 !important;
    border-color: #222 !important;
}

body.dark-theme .chat-typing-indicator .thinking-bubble {
    background: #1a1a1a !important;
    border-color: rgba(157, 141, 241, 0.2) !important;
}

body.dark-theme .chat-action-btn {
    background: #1a1a1a !important;
    border-color: rgba(255, 255, 255, 0.08) !important;
    color: #888888 !important;
}

body.dark-theme .chat-action-btn:hover {
    background: #222222 !important;
    color: var(--rui-purple) !important;
}

body.dark-theme .history-action-btn {
    background: #1a1a1a !important;
    border-color: rgba(255, 255, 255, 0.08) !important;
    color: #888888 !important;
}

body.dark-theme .history-action-btn.delete:hover {
    background: #1a1a1a !important;
    color: #ff5f5f !important;
    border-color: rgba(255, 95, 95, 0.3) !important;
}

body.dark-theme #btn-send-chat {
    background: var(--rui-purple) !important;
}

body.dark-theme .chat-timestamp {
    color: #666666 !important;
}

body.dark-theme #chat-search-input {
    background: #1a1a1a !important;
    color: #ffffff !important;
    border-color: rgba(255, 255, 255, 0.1) !important;
}

/* ===== END CHAT DARK MODE ===== */