/* ================================================
   Cosmic AI Chat — chat.css
   Matches cosmic-ai-saas-pro theme palette.
   NO backdrop-filter on interactive elements (prevents flicker).
================================================ */

/* ---- Page background isolation ---- */
.cosmic-chat-wrapper *,
.cosmic-chat-wrapper *::before,
.cosmic-chat-wrapper *::after {
    box-sizing: border-box;
}

/* ================================================
   GATE (logged-out state)
================================================ */

.cosmic-chat-gate {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 300px;
    padding: 40px 20px;
}

.cosmic-chat-gate-inner {
    text-align: center;
    background: rgba(15, 8, 40, 0.97);
    border: 1px solid rgba(123, 47, 247, 0.3);
    border-radius: 20px;
    padding: 50px 40px;
    max-width: 400px;
}

.cosmic-chat-gate-icon {
    font-size: 48px;
    display: block;
    margin-bottom: 15px;
}

.cosmic-chat-gate-inner h3 {
    color: #c77dff;
    font-size: 22px;
    margin: 0 0 12px;
}

.cosmic-chat-gate-inner p {
    color: #aaa;
    font-size: 15px;
}

.cosmic-chat-gate-inner a {
    color: #c77dff;
    font-weight: 600;
}

/* ================================================
   WRAPPER
================================================ */

.cosmic-chat-wrapper {
    width: 100%;
    margin: 0 auto;
    background: #0d0826;
    border: 1px solid rgba(123, 47, 247, 0.35);
    border-radius: 20px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    min-height: 580px;
    max-height: 720px;
    font-family: 'Poppins', sans-serif;
    box-shadow: 0 0 60px rgba(123, 47, 247, 0.2);
    /* Promote to own GPU layer — isolates all child repaints */
    transform: translateZ(0);
    isolation: isolate;
}

/* ================================================
   HEADER
================================================ */

.cosmic-chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 22px;
    background: linear-gradient(90deg, #130830, #1a0d40);
    border-bottom: 1px solid rgba(123, 47, 247, 0.25);
    flex-shrink: 0;
}

.cosmic-chat-header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.cosmic-chat-avatar {
    width: 42px;
    height: 42px;
    background: linear-gradient(135deg, #7b2ff7, #f107a3);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    flex-shrink: 0;
}

.cosmic-chat-title {
    font-size: 15px;
    font-weight: 700;
    color: #fff;
}

.cosmic-chat-subtitle {
    font-size: 12px;
    color: #a970ff;
    margin-top: 1px;
}

.cosmic-chat-header-right {
    display: flex;
    align-items: center;
    gap: 14px;
}

.cosmic-chat-credits {
    display: flex;
    align-items: center;
    gap: 5px;
    background: rgba(123, 47, 247, 0.15);
    border: 1px solid rgba(123, 47, 247, 0.3);
    border-radius: 20px;
    padding: 5px 13px;
    font-size: 13px;
    color: #c77dff;
    font-weight: 600;
}

.credit-icon {
    color: #f107a3;
    font-size: 11px;
}

.credit-label {
    color: #888;
    font-weight: 400;
    font-size: 12px;
}

.cosmic-chat-q-counter {
    font-size: 12px;
    color: #888;
    background: rgba(255,255,255,0.05);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 20px;
    padding: 4px 12px;
}

.cosmic-chat-q-counter span {
    color: #c77dff;
    font-weight: 700;
}

/* ================================================
   MESSAGES AREA
================================================ */

.cosmic-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px 18px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    scroll-behavior: smooth;
}

.cosmic-chat-messages::-webkit-scrollbar {
    width: 4px;
}

.cosmic-chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.cosmic-chat-messages::-webkit-scrollbar-thumb {
    background: rgba(123, 47, 247, 0.4);
    border-radius: 4px;
}

/* ---- Message rows ---- */

.cosmic-chat-msg {
    display: flex;
    gap: 10px;
    max-width: 88%;
}

.cosmic-chat-msg.user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.cosmic-chat-msg.ai {
    align-self: flex-start;
}

/* ---- Bubbles ---- */

.cosmic-chat-bubble {
    padding: 13px 17px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.65;
}

.cosmic-chat-msg.ai .cosmic-chat-bubble {
    background: #1a1040;
    color: #ddd;
    border-bottom-left-radius: 4px;
    border: 1px solid rgba(123, 47, 247, 0.2);
}

.cosmic-chat-msg.user .cosmic-chat-bubble {
    background: linear-gradient(135deg, #7b2ff7, #9d4edd);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.cosmic-chat-bubble p {
    margin: 0 0 8px;
}

.cosmic-chat-bubble p:last-child {
    margin: 0;
}

.cosmic-chat-bubble ul {
    margin: 8px 0;
    padding-left: 20px;
}

.cosmic-chat-bubble ul li {
    margin-bottom: 4px;
    font-size: 13px;
    color: #ccc;
}

.cosmic-chat-session-note {
    margin-top: 10px !important;
    font-size: 12px !important;
    color: #a970ff !important;
    background: rgba(123, 47, 247, 0.08);
    border-radius: 8px;
    padding: 7px 10px;
}

.cosmic-chat-no-credits {
    color: #ff9090 !important;
    background: rgba(255, 60, 60, 0.08) !important;
}

/* ---- Typing indicator ---- */

.cosmic-chat-typing {
    padding: 0 18px 10px;
}

.cosmic-chat-bubble.typing {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 12px 18px;
    background: #1a1040;
    border: 1px solid rgba(123, 47, 247, 0.2);
    border-bottom-left-radius: 4px;
}

.cosmic-chat-bubble.typing span {
    width: 7px;
    height: 7px;
    background: #a970ff;
    border-radius: 50%;
    display: inline-block;
    /* Each dot on its own GPU layer via will-change */
    will-change: transform;
    animation: cosmicTypingDot 1.2s infinite ease-in-out;
}

.cosmic-chat-bubble.typing span:nth-child(2) { animation-delay: 0.2s; }
.cosmic-chat-bubble.typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes cosmicTypingDot {
    0%, 60%, 100% { transform: translateY(0); }
    30%           { transform: translateY(-6px); }
}

/* ================================================
   SESSION END + NO CREDITS NOTICES
================================================ */

.cosmic-chat-session-end,
.cosmic-chat-no-credit-notice {
    margin: 0 18px 12px;
    padding: 14px 18px;
    border-radius: 12px;
    text-align: center;
    font-size: 13px;
    color: #ccc;
    background: rgba(123, 47, 247, 0.08);
    border: 1px solid rgba(123, 47, 247, 0.2);
}

.cosmic-chat-no-credit-notice {
    background: rgba(255, 60, 60, 0.06);
    border-color: rgba(255, 60, 60, 0.2);
    color: #ff9090;
}

.cosmic-chat-session-end p,
.cosmic-chat-no-credit-notice p {
    margin: 0 0 10px;
}

.cosmic-chat-new-session-btn {
    background: linear-gradient(90deg, #7b2ff7, #9d4edd);
    color: #fff;
    border: none;
    border-radius: 30px;
    padding: 10px 22px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    font-family: 'Poppins', sans-serif;
    /* Only transition what we need — never 'all' */
    transition: opacity 0.2s ease;
}

.cosmic-chat-new-session-btn:hover {
    opacity: 0.85;
}

.cosmic-chat-buy-btn {
    display: inline-block;
    background: linear-gradient(90deg, #f107a3, #7b2ff7);
    color: #fff;
    text-decoration: none;
    border-radius: 30px;
    padding: 10px 22px;
    font-size: 13px;
    font-weight: 600;
    transition: opacity 0.2s ease;
}

.cosmic-chat-buy-btn:hover {
    opacity: 0.85;
    color: #fff;
}

/* ================================================
   INPUT AREA
   Critical: solid bg, specific transition only — no 'all'
================================================ */

.cosmic-chat-input-area {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    padding: 14px 18px;
    background: #110930;
    border-top: 1px solid rgba(123, 47, 247, 0.2);
    flex-shrink: 0;
}

.cosmic-chat-input {
    flex: 1;
    resize: none;
    background: #1a1040;       /* solid — no rgba transparent */
    border: 1px solid rgba(123, 47, 247, 0.3);
    border-radius: 12px;
    color: #fff;
    font-size: 14px;
    font-family: 'Poppins', sans-serif;
    padding: 11px 15px;
    line-height: 1.5;
    max-height: 120px;
    overflow-y: auto;
    /* Only transition border-color — not 'all' — prevents repaint cascade */
    transition: border-color 0.2s ease;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
}

.cosmic-chat-input:focus {
    border-color: #a970ff;
    /* inset shadow stays within the element's own paint layer */
    box-shadow: inset 0 0 0 1px rgba(169, 112, 255, 0.3);
    background: #1e1550;
}

.cosmic-chat-input::placeholder {
    color: rgba(255, 255, 255, 0.25);
}

.cosmic-chat-input:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.cosmic-chat-send-btn {
  
    height: 44px;
    border-radius: 12px;
    background: linear-gradient(135deg, #7b2ff7, #9d4edd);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    flex-shrink: 0;
    transition: opacity 0.2s ease;
}

.cosmic-chat-send-btn:hover:not(:disabled) {
    opacity: 0.85;
}

.cosmic-chat-send-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

/* ================================================
   FOOTER
================================================ */

.cosmic-chat-footer {
    display: flex;
    justify-content: center;
    gap: 8px;
    padding: 8px;
    font-size: 11px;
    color: #444;
    background: #0d0826;
    border-top: 1px solid rgba(255,255,255,0.04);
}

/* ================================================
   RESPONSIVE
================================================ */

@media (max-width: 600px) {

    .cosmic-chat-wrapper {
        border-radius: 12px;
        max-height: 90vh;
        min-height: 500px;
    }

    .cosmic-chat-header {
        padding: 12px 15px;
    }

    .cosmic-chat-title { font-size: 14px; }

    .cosmic-chat-messages {
        padding: 14px 12px;
    }

    .cosmic-chat-msg {
        max-width: 95%;
    }

    .cosmic-chat-input-area {
        padding: 10px 12px;
    }

    .credit-label { display: none; }
}

/* ================================================
   TAB NAV — lives OUTSIDE both panels, always visible
================================================ */

.cosmic-chat-tabs-nav {
    display: flex;
    gap: 8px;
    margin-bottom: 10px;
    max-width: 780px;
    margin-left: auto;
    margin-right: auto;
}

.cosmic-tab-btn {
    background: rgba(13, 8, 38, 0.9);
    border: 1px solid rgba(123, 47, 247, 0.25);
    border-radius: 20px;
    color: #666;
    font-size: 13px;
    font-family: 'Poppins', sans-serif;
    padding: 8px 20px;
    cursor: pointer;
    transition: border-color 0.2s ease, color 0.2s ease, background 0.2s ease;
    margin: 0;
    font-weight: 500;
}

.cosmic-tab-btn:hover {
    color: #c77dff;
    border-color: rgba(123, 47, 247, 0.5);
    background: rgba(123, 47, 247, 0.1);
}

.cosmic-tab-btn.active {
    background: rgba(123, 47, 247, 0.2);
    border-color: #a970ff;
    color: #c77dff;
    font-weight: 600;
}

/* ================================================
   CHAT FOOTER (simple — no tabs inside)
================================================ */

.cosmic-chat-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 8px 14px;
    font-size: 11px;
    color: #444;
    background: #0d0826;
    border-top: 1px solid rgba(255,255,255,0.04);
    flex-shrink: 0;
}

.cosmic-chat-footer-rule {
    color: #333;
    font-size: 11px;
}

/* ================================================
   HISTORY WRAPPER (standalone + inline)
================================================ */

.cosmic-history-wrapper {
    width: 100%;
    margin: 0 auto;
    background: #0d0826;
    border: 1px solid rgba(123, 47, 247, 0.35);
    border-radius: 20px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    min-height: 580px;
    max-height: 720px;
    font-family: 'Poppins', sans-serif;
    box-shadow: 0 0 60px rgba(123, 47, 247, 0.2);
    transform: translateZ(0);
    isolation: isolate;
}

/* Inline variant sits right below the chat widget */
.cosmic-history-inline {
    margin-top: 12px;
    min-height: 540px;
}

/* ================================================
   HISTORY HEADER
================================================ */

.cosmic-history-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 22px;
    background: linear-gradient(90deg, #130830, #1a0d40);
    border-bottom: 1px solid rgba(123, 47, 247, 0.25);
    flex-shrink: 0;
}

.cosmic-history-header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.cosmic-history-icon {
    font-size: 24px;
}

.cosmic-history-title {
    font-size: 15px;
    font-weight: 700;
    color: #fff;
}

.cosmic-history-subtitle {
    font-size: 12px;
    color: #a970ff;
    margin-top: 1px;
}

.cosmic-history-back-btn {
    font-size: 13px;
    color: #a970ff;
    text-decoration: none;
    border: 1px solid rgba(123, 47, 247, 0.3);
    border-radius: 20px;
    padding: 6px 14px;
    transition: border-color 0.2s ease, color 0.2s ease;
}

.cosmic-history-back-btn:hover {
    color: #c77dff;
    border-color: rgba(123, 47, 247, 0.6);
}

/* ================================================
   HISTORY BODY
================================================ */

.cosmic-history-body {
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    position: relative;
}

/* ================================================
   SESSION LIST
================================================ */

.cosmic-history-sessions {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.cosmic-history-sessions::-webkit-scrollbar { width: 4px; }
.cosmic-history-sessions::-webkit-scrollbar-track { background: transparent; }
.cosmic-history-sessions::-webkit-scrollbar-thumb {
    background: rgba(123, 47, 247, 0.4);
    border-radius: 4px;
}

/* ---- Session card ---- */

.cosmic-history-session-card {
    background: #1a1040;
    border: 1px solid rgba(123, 47, 247, 0.18);
    border-radius: 14px;
    padding: 14px 16px;
    cursor: pointer;
    transition: border-color 0.2s ease, background 0.2s ease;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 14px;
}

.cosmic-history-session-card:hover {
    border-color: rgba(123, 47, 247, 0.5);
    background: #1e1550;
}

.cosmic-history-session-left {
    flex: 1;
    min-width: 0;
}

.cosmic-history-session-num {
    font-size: 11px;
    color: #a970ff;
    font-weight: 600;
    margin-bottom: 4px;
    letter-spacing: 0.3px;
}

.cosmic-history-session-preview {
    font-size: 14px;
    color: #ddd;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-bottom: 6px;
}

.cosmic-history-session-preview.empty {
    color: #555;
    font-style: italic;
}

.cosmic-history-session-meta {
    display: flex;
    gap: 12px;
    font-size: 11px;
    color: #666;
}

.cosmic-history-session-right {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 6px;
    flex-shrink: 0;
}

.cosmic-history-q-badge {
    font-size: 12px;
    font-weight: 600;
    color: #fff;
    background: rgba(123, 47, 247, 0.3);
    border: 1px solid rgba(123, 47, 247, 0.4);
    border-radius: 20px;
    padding: 3px 10px;
    white-space: nowrap;
}

.cosmic-history-q-badge.full {
    background: rgba(60, 255, 150, 0.08);
    border-color: rgba(60, 255, 150, 0.25);
    color: #7fffc4;
}

.cosmic-history-arrow {
    color: #555;
    font-size: 16px;
}

/* ================================================
   LOADING + EMPTY STATE
================================================ */

.cosmic-history-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    color: #666;
    font-size: 14px;
    gap: 14px;
}

.cosmic-history-spinner {
    width: 32px;
    height: 32px;
    border: 3px solid rgba(123, 47, 247, 0.2);
    border-top-color: #a970ff;
    border-radius: 50%;
    will-change: transform;
    animation: cosmicHistorySpin 0.8s linear infinite;
}

@keyframes cosmicHistorySpin {
    to { transform: rotate(360deg); }
}

.cosmic-history-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    text-align: center;
    gap: 8px;
}

.cosmic-history-empty span {
    font-size: 40px;
    margin-bottom: 8px;
}

.cosmic-history-empty p {
    color: #aaa;
    font-size: 15px;
    margin: 0;
}

.cosmic-history-empty small {
    color: #555;
    font-size: 12px;
}

/* ================================================
   PAGINATION
================================================ */

.cosmic-history-pagination {
    padding: 10px 16px;
    text-align: center;
    flex-shrink: 0;
}

.cosmic-history-load-more {
    background: transparent;
    border: 1px solid rgba(123, 47, 247, 0.3);
    border-radius: 20px;
    color: #a970ff;
    font-size: 13px;
    font-family: 'Poppins', sans-serif;
    padding: 8px 22px;
    cursor: pointer;
    transition: border-color 0.2s ease, background 0.2s ease;
    margin: 0;
}

.cosmic-history-load-more:hover {
    background: rgba(123, 47, 247, 0.1);
    border-color: rgba(123, 47, 247, 0.6);
}

/* ================================================
   MESSAGE VIEWER
================================================ */

.cosmic-history-viewer {
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.cosmic-history-viewer-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: #130830;
    border-bottom: 1px solid rgba(123, 47, 247, 0.15);
    flex-shrink: 0;
    gap: 12px;
}

.cosmic-history-viewer-back {
    background: transparent;
    border: 1px solid rgba(123, 47, 247, 0.3);
    border-radius: 20px;
    color: #a970ff;
    font-size: 12px;
    font-family: 'Poppins', sans-serif;
    padding: 5px 14px;
    cursor: pointer;
    transition: border-color 0.2s ease;
    margin: 0;
    flex-shrink: 0;
}

.cosmic-history-viewer-back:hover {
    border-color: rgba(123, 47, 247, 0.6);
    background: rgba(123, 47, 247, 0.08);
}

.cosmic-history-viewer-meta {
    font-size: 12px;
    color: #666;
    text-align: right;
}

.cosmic-history-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.cosmic-history-messages::-webkit-scrollbar { width: 4px; }
.cosmic-history-messages::-webkit-scrollbar-track { background: transparent; }
.cosmic-history-messages::-webkit-scrollbar-thumb {
    background: rgba(123, 47, 247, 0.4);
    border-radius: 4px;
}

/* ---- History message bubbles ---- */

.cosmic-history-msg {
    display: flex;
    max-width: 88%;
}

.cosmic-history-msg.user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.cosmic-history-msg.ai {
    align-self: flex-start;
}

.cosmic-history-bubble {
    padding: 12px 16px;
    border-radius: 14px;
    font-size: 13.5px;
    line-height: 1.6;
}

.cosmic-history-msg.ai .cosmic-history-bubble {
    background: #1a1040;
    color: #ccc;
    border-bottom-left-radius: 4px;
    border: 1px solid rgba(123, 47, 247, 0.15);
}

.cosmic-history-msg.user .cosmic-history-bubble {
    background: linear-gradient(135deg, #5a1fbf, #7b2ff7);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.cosmic-history-bubble p {
    margin: 0 0 6px;
}

.cosmic-history-bubble p:last-child {
    margin: 0;
}

.cosmic-history-msg-time {
    font-size: 10px;
    color: #444;
    margin-top: 4px;
    padding: 0 4px;
}

.cosmic-history-msg.user .cosmic-history-msg-time {
    text-align: right;
}

/* Q/A pair grouping */
.cosmic-history-qa-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding-bottom: 16px;
    border-bottom: 1px solid rgba(255,255,255,0.04);
}

.cosmic-history-qa-group:last-child {
    border-bottom: none;
    padding-bottom: 0;
}

.cosmic-history-q-label {
    font-size: 10px;
    color: #555;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    padding: 0 4px;
}

/* ================================================
   RESPONSIVE
================================================ */

@media (max-width: 600px) {

    .cosmic-history-wrapper {
        border-radius: 12px;
        max-height: 90vh;
        min-height: 500px;
    }

    .cosmic-history-session-card {
        flex-direction: column;
        gap: 8px;
    }

    .cosmic-history-session-right {
        flex-direction: row;
        align-items: center;
    }

    .cosmic-history-msg {
        max-width: 95%;
    }

    .cosmic-chat-footer-rule {
        display: none;
    }
}
