/* ═══════════════════════════════════════════════════
   Teddy AI Chat Widget v3 — Old Design + New Features
   Visual: teddy-chat (Teddy bear FAB, navy gradient, orange send)
   Features: quick replies, rich text, session TTL, full history
   ═══════════════════════════════════════════════════ */

/* ── Floating Action Button ── */
.teddy-fab {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 64px;
    height: 64px;
    border-radius: 50%;
    border: 2px solid rgba(0, 212, 255, 0.4);
    background: linear-gradient(135deg, #0A2540, #1a3a5c);
    cursor: pointer;
    z-index: 9998;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3), 0 0 15px rgba(0, 212, 255, 0.2);
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    animation: teddyFloat 3s ease-in-out infinite;
    -webkit-tap-highlight-color: transparent;
}

.teddy-fab img {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    pointer-events: none;
}

.teddy-fab:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 25px rgba(0, 0, 0, 0.4), 0 0 25px rgba(0, 212, 255, 0.4);
    border-color: rgba(0, 212, 255, 0.7);
}

.teddy-fab.open {
    animation: none;
    transform: scale(0.9) rotate(15deg);
    opacity: 0.7;
}

@keyframes teddyFloat {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-6px); }
}

/* ── Notification pulse dot ── */
.teddy-fab::after {
    content: '';
    position: absolute;
    top: -2px;
    right: -2px;
    width: 16px;
    height: 16px;
    background: #e67e22;
    border-radius: 50%;
    border: 2px solid #0A2540;
    animation: teddyPulse 2s ease-in-out infinite;
}

.teddy-fab.open::after { display: none; }

@keyframes teddyPulse {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.3); opacity: 0.7; }
}

/* ── Chat Window ── */
.teddy-chat {
    position: fixed;
    bottom: 96px;
    right: 24px;
    width: 380px;
    max-width: calc(100vw - 32px);
    height: 520px;
    max-height: calc(100vh - 120px);
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.25);
    z-index: 9999;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    transform: translateY(20px) scale(0.95);
    pointer-events: none;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    border: 1px solid rgba(0, 0, 0, 0.08);
}

.teddy-chat.open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: all;
}

/* ── Chat Header ── */
.teddy-chat-header {
    background: linear-gradient(135deg, #0A2540, #1a4a6c);
    color: #fff;
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
    border-bottom: 1px solid rgba(0, 212, 255, 0.15);
}

.teddy-chat-header img {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 2px solid rgba(0, 212, 255, 0.4);
    flex-shrink: 0;
}

.teddy-chat-header .info {
    flex: 1;
    min-width: 0;
}

.teddy-chat-header .info h4 {
    font-size: 0.95rem;
    font-weight: 600;
    margin: 0;
    font-family: 'Orbitron', monospace;
    letter-spacing: 0.5px;
    line-height: 1.3;
}

.teddy-chat-header .info p {
    font-size: 0.7rem;
    color: rgba(255, 255, 255, 0.7);
    margin: 2px 0 0;
    display: flex;
    align-items: center;
    gap: 4px;
}

/* Online status dot */
.teddy-status-dot {
    width: 6px;
    height: 6px;
    background: #4ade80;
    border-radius: 50%;
    display: inline-block;
    animation: teddyOnline 2s infinite;
}

@keyframes teddyOnline {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

/* Close button — small, in header, no overlap with input */
.teddy-chat-close {
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: #fff;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 0.85rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
    line-height: 1;
}

.teddy-chat-close:hover {
    background: rgba(255, 255, 255, 0.25);
}

.teddy-chat-close:active {
    background: rgba(255, 255, 255, 0.35);
}

/* ── Messages Area ── */
.teddy-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: #fafafa;
    scroll-behavior: smooth;
    min-height: 0;
    -webkit-overflow-scrolling: touch;
}

.teddy-chat-messages::-webkit-scrollbar { width: 5px; }
.teddy-chat-messages::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }

/* ── Message Bubbles (simple — no avatar circle) ── */
.teddy-msg {
    max-width: 82%;
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 0.875rem;
    line-height: 1.55;
    animation: teddyMsgIn 0.3s ease-out;
    word-wrap: break-word;
    word-break: break-word;
}

.teddy-msg.no-anim {
    animation: none;
}

@keyframes teddyMsgIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.teddy-msg.ai {
    background: #fff;
    border: 1px solid #e0e0e0;
    align-self: flex-start;
    border-bottom-left-radius: 4px;
    color: #333;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}

.teddy-msg.user {
    background: linear-gradient(135deg, #0A2540, #1a4a6c);
    color: #fff;
    align-self: flex-end;
    border-bottom-right-radius: 4px;
}

.teddy-msg.typing {
    background: #f0f0f0;
    border: 1px solid #e0e0e0;
    align-self: flex-start;
    padding: 10px 16px;
    border-bottom-left-radius: 4px;
}

/* ── Rich text in AI bubbles ── */
.teddy-msg.ai ul {
    margin: 6px 0 6px 4px;
    padding: 0;
    list-style: none;
}

.teddy-msg.ai ul li {
    padding-left: 16px;
    position: relative;
    margin-bottom: 3px;
}

.teddy-msg.ai ul li::before {
    content: "•";
    position: absolute;
    left: 4px;
    color: #e67e22;
}

.teddy-msg.ai strong {
    font-weight: 600;
    color: #0A2540;
}

.teddy-msg.ai a {
    color: #1a4a6c;
    text-decoration: underline;
    word-break: break-all;
}

.teddy-msg.ai .teddy-book-link {
    display: inline-block;
    margin-top: 8px;
    padding: 8px 16px;
    background: linear-gradient(135deg, #e67e22, #d35400);
    color: #fff !important;
    text-decoration: none;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 600;
    letter-spacing: 0.3px;
    box-shadow: 0 2px 8px rgba(230, 126, 34, 0.35);
    transition: transform 0.15s, box-shadow 0.15s;
}

.teddy-msg.ai .teddy-book-link:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(230, 126, 34, 0.45);
}

/* ── Typing dots ── */
.teddy-typing-dots {
    display: flex;
    gap: 4px;
    align-items: center;
}

.teddy-typing-dots span {
    width: 7px;
    height: 7px;
    background: #0A2540;
    border-radius: 50%;
    animation: teddyTyping 1.2s ease-in-out infinite;
}

.teddy-typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.teddy-typing-dots span:nth-child(3) { animation-delay: 0.3s; }

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

/* ── Quick Reply Chips ── */
.teddy-quick-replies {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 0 16px 12px;
    background: #fafafa;
    flex-shrink: 0;
}

.teddy-quick-reply {
    padding: 6px 14px;
    border-radius: 20px;
    background: #fff;
    border: 1.5px solid #e0e0e0;
    color: #0A2540;
    font-size: 0.78rem;
    cursor: pointer;
    transition: border-color 0.2s, background 0.2s, color 0.2s;
    -webkit-tap-highlight-color: transparent;
    font-family: inherit;
    white-space: nowrap;
}

.teddy-quick-reply:hover {
    border-color: #e67e22;
    background: #fff8f3;
    color: #d35400;
}

/* ── Input Area ── */
.teddy-chat-input {
    display: flex;
    padding: 10px 12px;
    border-top: 1px solid #e0e0e0;
    gap: 8px;
    background: #fff;
    flex-shrink: 0;
    align-items: center;
}

.teddy-chat-input input {
    flex: 1;
    min-width: 0;
    padding: 10px 14px;
    background: #f5f5f5;
    border: 1px solid #ddd;
    border-radius: 10px;
    color: #333;
    font-size: 16px; /* prevents iOS zoom */
    font-family: inherit;
    outline: none;
    transition: border-color 0.2s, background 0.2s;
    -webkit-text-size-adjust: 100%;
}

.teddy-chat-input input:focus {
    border-color: #0A2540;
    background: #fff;
}

.teddy-chat-input input:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.teddy-chat-input input::placeholder {
    color: #aaa;
}

/* Orange "Send" button */
.teddy-chat-input button {
    padding: 10px 16px;
    flex-shrink: 0;
    background: linear-gradient(135deg, #e67e22, #d35400);
    border: none;
    border-radius: 10px;
    color: #fff;
    font-weight: 700;
    cursor: pointer;
    font-size: 0.9rem;
    transition: transform 0.15s, box-shadow 0.15s, opacity 0.15s;
    white-space: nowrap;
    -webkit-tap-highlight-color: transparent;
}

.teddy-chat-input button:hover {
    transform: scale(1.05);
    box-shadow: 0 2px 10px rgba(230, 126, 34, 0.4);
}

.teddy-chat-input button:active {
    transform: scale(0.97);
}

.teddy-chat-input button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

/* ── Footer: hidden ── */
.teddy-chat-footer {
    display: none;
}

/* ── Body scroll lock (fallback) ── */
body.teddy-chat-open {
    overflow: hidden;
}

/* ── Mobile: Full-screen takeover ── */
@media (max-width: 480px) {
    .teddy-chat {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        max-width: 100%;
        height: 100dvh;
        height: 100vh;
        max-height: none;
        border-radius: 0;
        border: none;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        /* Reset transform origin for full-screen slide */
        transform: translateY(20px) scale(0.98);
    }

    .teddy-chat.open {
        transform: translateY(0) scale(1);
    }

    .teddy-chat-header {
        flex-shrink: 0;
        /* Taller header on mobile for comfortable tap */
        padding: 14px 16px;
    }

    .teddy-chat-messages {
        flex: 1;
        min-height: 0;
        overflow-y: auto;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
    }

    .teddy-quick-replies {
        flex-shrink: 0;
    }

    .teddy-chat-input {
        flex-shrink: 0;
        padding-bottom: max(12px, env(safe-area-inset-bottom, 12px));
    }

    .teddy-fab {
        bottom: 16px;
        right: 16px;
        width: 56px;
        height: 56px;
    }

    .teddy-fab img {
        width: 40px;
        height: 40px;
    }
}

/* ── Tablet ── */
@media (min-width: 481px) and (max-width: 768px) {
    .teddy-chat {
        width: calc(100vw - 32px);
        max-height: calc(100vh - 120px);
        bottom: 100px;
        right: 16px;
    }
}
