/* ============================================
   Chat Widget — AI Companions
   SwamiGPT / DharmaGPT / TapasAI
   Per-bot visual personality via CSS custom properties
   ============================================ */

/* ---- Overlay ---- */
.chat-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 99998;
    background: rgba(0,0,0,0.3);
    opacity: 0;
    transition: opacity 0.25s ease;
}
.chat-overlay.open {
    display: block;
    opacity: 1;
}

/* ---- Panel (base + bot theming) ---- */
.chat-panel {
    position: fixed;
    top: 0;
    right: -420px;
    width: 400px;
    max-width: 92vw;
    height: 100vh;
    height: 100dvh;
    z-index: 99999;
    display: flex;
    flex-direction: column;
    background: #faf8f5;
    box-shadow: -4px 0 24px rgba(0,0,0,0.15);
    transition: right 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    font-family: 'Inter', -apple-system, sans-serif;

    /* Default palette — overridden by data-bot */
    --chat-accent: #c8a87e;
    --chat-accent-soft: #d4b896;
    --chat-accent-bg: #f5f1eb;
    --chat-accent-glow: rgba(200,168,126,0.15);
    --chat-header-bg: #f5f1eb;
    --chat-user-bg: #c8a87e;
}
.chat-panel.open {
    right: 0;
}

/* SwamiGPT — warm amber/brown */
.chat-panel[data-bot="swami"] {
    --chat-accent: #c47a1a;
    --chat-accent-soft: #d4943e;
    --chat-accent-bg: #faf3e8;
    --chat-accent-glow: rgba(196,122,26,0.15);
    --chat-header-bg: #f8f0e4;
    --chat-user-bg: #c47a1a;
}

/* DharmaGPT — sage green */
.chat-panel[data-bot="dharma"] {
    --chat-accent: #5a8a5e;
    --chat-accent-soft: #7bab7f;
    --chat-accent-bg: #f0f5f0;
    --chat-accent-glow: rgba(90,138,94,0.15);
    --chat-header-bg: #edf3ed;
    --chat-user-bg: #5a8a5e;
}

/* TapasAI — journal blue */
.chat-panel[data-bot="tapas"] {
    --chat-accent: #1e88b4;
    --chat-accent-soft: #4da8cf;
    --chat-accent-bg: #e4f0f7;
    --chat-accent-glow: rgba(30,136,180,0.15);
    --chat-header-bg: #eaf3f8;
    --chat-user-bg: #1e88b4;
}

/* ---- Header ---- */
.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.85rem 1.25rem;
    border-bottom: 1px solid #e8e2da;
    background: var(--chat-header-bg);
    flex-shrink: 0;
}
.chat-header-left {
    display: flex;
    align-items: center;
    gap: 0.6rem;
}
.chat-bot-icon {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--chat-accent-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    flex-shrink: 0;
}
.chat-bot-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
}
.chat-bot-name {
    font-family: 'Cormorant Garamond', Georgia, serif;
    font-size: 1.15rem;
    font-weight: 600;
    color: #2c2a26;
}
.chat-bot-sub {
    font-size: 0.6rem;
    color: #9b9590;
    letter-spacing: 0.03em;
    text-transform: uppercase;
    margin-top: 1px;
}
.chat-header-actions {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}
.chat-new-btn {
    background: none;
    border: none;
    cursor: pointer;
    padding: 0.35rem;
    color: #9b9590;
    border-radius: 6px;
    transition: background 0.15s, color 0.15s;
    display: flex;
    align-items: center;
    justify-content: center;
}
.chat-new-btn:hover {
    background: var(--chat-accent-glow);
    color: var(--chat-accent);
}
.chat-close {
    background: none;
    border: none;
    cursor: pointer;
    padding: 0.35rem;
    color: #6b6560;
    border-radius: 6px;
    transition: background 0.15s;
    display: flex;
    align-items: center;
    justify-content: center;
}
.chat-close:hover {
    background: #e8e2da;
    color: #2c2a26;
}

/* ---- Messages ---- */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    scroll-behavior: smooth;
}

.chat-msg {
    max-width: 88%;
    padding: 0.75rem 1rem;
    border-radius: 12px;
    font-size: 0.88rem;
    line-height: 1.65;
    color: #2c2a26;
    word-wrap: break-word;
}
.chat-msg.user {
    align-self: flex-end;
    background: var(--chat-user-bg);
    color: #fff;
    border-bottom-right-radius: 4px;
    white-space: pre-wrap;
}
.chat-msg.assistant {
    align-self: flex-start;
    background: var(--chat-accent-bg);
    border: 1px solid #e8e2da;
    border-bottom-left-radius: 4px;
}
/* Plain text during streaming */
.chat-msg.assistant.streaming {
    white-space: pre-wrap;
}

/* ---- Markdown content (rendered after streaming completes) ---- */
.chat-msg.assistant p {
    margin: 0 0 0.6rem 0;
}
.chat-msg.assistant p:last-child {
    margin-bottom: 0;
}
.chat-msg.assistant strong {
    font-weight: 600;
    color: #2c2a26;
}
.chat-msg.assistant em {
    font-style: italic;
    color: #4a4540;
}
.chat-msg.assistant code {
    font-family: 'SF Mono', 'Fira Code', monospace;
    font-size: 0.82em;
    background: rgba(0,0,0,0.05);
    padding: 0.15em 0.35em;
    border-radius: 3px;
}
.chat-msg.assistant pre {
    background: #f0ece6;
    border: 1px solid #e0dbd3;
    border-radius: 6px;
    padding: 0.6rem 0.8rem;
    overflow-x: auto;
    margin: 0.5rem 0;
    font-size: 0.82em;
    line-height: 1.5;
}
.chat-msg.assistant pre code {
    background: none;
    padding: 0;
}
.chat-msg.assistant h4 {
    font-family: 'Cormorant Garamond', Georgia, serif;
    font-size: 1rem;
    font-weight: 600;
    color: #2c2a26;
    margin: 0.8rem 0 0.4rem 0;
}
.chat-msg.assistant h4:first-child {
    margin-top: 0;
}
.chat-msg.assistant ul,
.chat-msg.assistant ol {
    margin: 0.4rem 0 0.6rem 0;
    padding-left: 1.2rem;
}
.chat-msg.assistant li {
    margin-bottom: 0.25rem;
}
.chat-msg.assistant blockquote {
    border-left: 3px solid var(--chat-accent);
    margin: 0.5rem 0;
    padding: 0.3rem 0 0.3rem 0.8rem;
    color: #6b6560;
    font-style: italic;
}
.chat-msg.assistant a {
    color: var(--chat-accent);
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: border-color 0.15s;
}
.chat-msg.assistant a:hover {
    border-bottom-color: var(--chat-accent);
}

/* ---- Welcome message ---- */
.chat-welcome {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: 2rem 1.5rem 1rem;
    gap: 0.6rem;
}
.chat-welcome-icon {
    width: 72px;
    height: 72px;
    border-radius: 50%;
    background: var(--chat-accent-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    margin-bottom: 0.3rem;
    box-shadow: 0 2px 12px var(--chat-accent-glow);
}
.chat-welcome-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
}
.chat-welcome-name {
    font-family: 'Cormorant Garamond', Georgia, serif;
    font-size: 1.35rem;
    font-weight: 600;
    color: #2c2a26;
}
.chat-welcome-role {
    font-size: 0.72rem;
    color: var(--chat-accent);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 500;
}
.chat-welcome-desc {
    font-size: 0.82rem;
    line-height: 1.6;
    color: #6b6560;
    max-width: 300px;
}

/* ---- Suggested prompts ---- */
.chat-suggestions {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    padding: 0 0.5rem;
    margin-top: 0.5rem;
}
.chat-suggestion {
    background: #fff;
    border: 1px solid #e8e2da;
    border-radius: 10px;
    padding: 0.65rem 0.9rem;
    font-size: 0.82rem;
    line-height: 1.45;
    color: #4a4540;
    cursor: pointer;
    transition: border-color 0.15s, background 0.15s, transform 0.1s;
    text-align: left;
}
.chat-suggestion:hover {
    border-color: var(--chat-accent-soft);
    background: var(--chat-accent-bg);
    transform: translateY(-1px);
}
.chat-suggestion:active {
    transform: translateY(0);
}

/* ---- Conversation restored notice ---- */
.chat-restored {
    text-align: center;
    font-size: 0.72rem;
    color: #9b9590;
    padding: 0.5rem 1rem;
    border-bottom: 1px solid #e8e2da;
    background: var(--chat-accent-bg);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}
.chat-restored-dismiss {
    background: none;
    border: none;
    color: var(--chat-accent);
    font-size: 0.72rem;
    cursor: pointer;
    padding: 0;
    font-weight: 500;
}
.chat-restored-dismiss:hover {
    text-decoration: underline;
}

/* ---- Typing indicator ---- */
.chat-typing {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 0.75rem 1rem;
    background: var(--chat-accent-bg);
    border: 1px solid #e8e2da;
    border-radius: 12px;
    border-bottom-left-radius: 4px;
    align-self: flex-start;
}
.chat-typing span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--chat-accent);
    animation: chatBounce 1.4s ease-in-out infinite;
}
.chat-typing span:nth-child(2) { animation-delay: 0.2s; }
.chat-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes chatBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-4px); opacity: 1; }
}

/* ---- Input area ---- */
.chat-input-area {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.85rem 1.25rem;
    border-top: 1px solid #e8e2da;
    background: var(--chat-header-bg);
    flex-shrink: 0;
}
.chat-input {
    flex: 1;
    padding: 0.6rem 0.85rem;
    border: 1px solid #e0d9cf;
    border-radius: 8px;
    font-size: 0.85rem;
    font-family: inherit;
    background: #fff;
    color: #2c2a26;
    outline: none;
    resize: none;
    min-height: 38px;
    max-height: 100px;
    line-height: 1.4;
}
.chat-input:focus {
    border-color: var(--chat-accent);
    box-shadow: 0 0 0 2px var(--chat-accent-glow);
}
.chat-input::placeholder {
    color: #b0a89e;
}
.chat-send {
    background: var(--chat-accent);
    border: none;
    color: #fff;
    width: 36px;
    height: 36px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, transform 0.1s;
    flex-shrink: 0;
}
.chat-send:hover {
    filter: brightness(0.9);
    transform: translateY(-1px);
}
.chat-send:active {
    transform: translateY(0);
}
.chat-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}
.chat-send svg {
    width: 16px;
    height: 16px;
}

/* ---- Footer ---- */
.chat-footer {
    text-align: center;
    padding: 0.4rem;
    font-size: 0.6rem;
    color: #b0a89e;
    border-top: 1px solid #e8e2da;
    background: var(--chat-header-bg);
    flex-shrink: 0;
}

/* ---- Rate limit message ---- */
.chat-limit-msg {
    background: #f5edd8 !important;
    border: 1px solid #d4a843 !important;
    text-align: center;
    font-size: 0.82rem;
    line-height: 1.6;
}
.chat-limit-msg strong {
    color: #b8860b;
}
.chat-limit-msg em {
    color: #6b6560;
    font-style: normal;
    font-size: 0.78rem;
}

/* ---- Auth CTA ---- */
.chat-auth-cta {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 2.5rem 2rem;
    text-align: center;
}
.chat-auth-icon-large {
    font-size: 3rem;
    margin-bottom: 1rem;
    line-height: 1;
}
.chat-auth-title {
    font-family: 'Cormorant Garamond', Georgia, serif;
    font-size: 1.5rem;
    font-weight: 600;
    color: #2c2a26;
    margin: 0 0 0.75rem 0;
}
.chat-auth-desc {
    font-size: 0.88rem;
    line-height: 1.6;
    color: #6b6560;
    margin: 0 0 0.5rem 0;
    max-width: 300px;
}
.chat-auth-free {
    font-size: 0.82rem;
    color: #9b9590;
    margin: 0 0 1.5rem 0;
}
.chat-auth-btn {
    display: block;
    width: 100%;
    max-width: 260px;
    padding: 0.75rem 1.5rem;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 600;
    text-align: center;
    text-decoration: none;
    transition: background 0.15s, transform 0.1s;
    margin-bottom: 0.6rem;
}
.chat-auth-btn.primary {
    background: var(--chat-accent);
    color: #fff;
}
.chat-auth-btn.primary:hover {
    filter: brightness(0.9);
    transform: translateY(-1px);
}
.chat-auth-btn.secondary {
    background: transparent;
    color: #9b9590;
    font-size: 0.78rem;
    font-weight: 400;
}
.chat-auth-btn.secondary:hover {
    color: #6b6560;
}

/* ---- Mobile ---- */
@media (max-width: 480px) {
    .chat-panel {
        width: 100vw;
        right: -100vw;
    }
    .chat-welcome {
        padding: 1.5rem 1rem 0.5rem;
    }
    .chat-welcome-desc {
        max-width: 100%;
    }
}
