/* =========================================================================
   AI Pet — Frontend Styles (Animated Squirtle)
   ========================================================================= */

:root {
    --pet-primary: #4a90d9;
    --pet-primary-dark: #3a7bc8;
    --pet-bg: #ffffff;
    --pet-text: #333333;
    --pet-text-light: #888888;
    --pet-border: #f0f0f0;
    --pet-user-bg: #e8f5e9;
    --pet-pet-bg: #f0f8ff;
    --pet-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
    --pet-radius: 16px;
    --pet-size: 70px;
    --pet-transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* --- Pet Avatar --- */
.ai-pet-avatar {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: var(--pet-size);
    height: var(--pet-size);
    border-radius: 50%;
    background: transparent;
    box-shadow: none;
    cursor: grab;
    z-index: 999999;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform var(--pet-transition), box-shadow var(--pet-transition);
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
    animation: pet-breathe 3s ease-in-out infinite;
    overflow: visible;
}

.ai-pet-avatar:active {
    cursor: grabbing;
}

.ai-pet-avatar.ai-pet-dragging {
    animation: none;
    opacity: 0.85;
    transition: none;
    filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.2));
}

.ai-pet-avatar:hover {
    transform: scale(1.12);
    filter: drop-shadow(0 4px 16px rgba(74, 144, 217, 0.4));
}

.ai-pet-avatar:active:not(.ai-pet-dragging) {
    transform: scale(0.95);
}

.ai-pet-avatar.happy {
    animation: pet-happy 0.6s ease;
}

.ai-pet-avatar.sad {
    animation: pet-sad 0.8s ease;
}

/* --- Squirtle GIF --- */
.ai-pet-squirtle {
    width: 60px;
    height: 66px;
    object-fit: contain;
    pointer-events: none;
    image-rendering: pixelated;
    transition: filter 0.3s ease, transform 0.3s ease;
}

/* --- Expression Filters --- */
.ai-pet-avatar.expr-happy .ai-pet-squirtle {
    filter: brightness(1.2) saturate(1.3);
    transform: scale(1.1);
}

.ai-pet-avatar.expr-surprised .ai-pet-squirtle {
    filter: saturate(1.5) contrast(1.1);
    transform: scale(1.2);
    animation: expr-surprised-bounce 0.4s ease;
}

.ai-pet-avatar.expr-thinking .ai-pet-squirtle {
    filter: brightness(0.9);
    animation: expr-thinking-tilt 2s ease-in-out infinite;
}

.ai-pet-avatar.expr-sleep .ai-pet-squirtle {
    filter: brightness(0.65) blur(1px);
    transform: scale(0.9);
    opacity: 0.75;
}

.ai-pet-avatar.expr-normal .ai-pet-squirtle {
    filter: none;
    transform: scale(1);
    animation: none;
}

@keyframes expr-surprised-bounce {
    0% { transform: scale(1); }
    40% { transform: scale(1.25); }
    100% { transform: scale(1.1); }
}

@keyframes expr-thinking-tilt {
    0%, 100% { transform: rotate(0deg) translateY(0); }
    30% { transform: rotate(-3deg) translateY(-2px); }
    70% { transform: rotate(3deg) translateY(-1px); }
}

/* --- Bubble Particles --- */
.ai-pet-bubbles {
    position: absolute;
    inset: -20px;
    pointer-events: none;
    z-index: 1;
}

.ai-pet-particle {
    position: absolute;
    border-radius: 50%;
    background: radial-gradient(circle at 30% 30%, rgba(255,255,255,0.8), rgba(74,144,217,0.3));
    border: 1px solid rgba(74,144,217,0.2);
    animation: pet-bubble-float 2s ease-out forwards;
}

@keyframes pet-bubble-float {
    0% {
        opacity: 0.8;
        transform: translateY(0) scale(1);
    }
    100% {
        opacity: 0;
        transform: translateY(-50px) scale(0.3);
    }
}

/* --- Notification Badge --- */
.ai-pet-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #ff4444;
    border: 2px solid #fff;
    font-size: 10px;
    color: #fff;
    display: none;
    align-items: center;
    justify-content: center;
    line-height: 1;
}

.ai-pet-badge.show {
    display: flex;
}

/* --- Speech Bubble (Hover hint) --- */
.ai-pet-speech {
    position: absolute;
    bottom: calc(100% + 12px);
    right: 0;
    background: #fff;
    border: 1px solid var(--pet-border);
    border-radius: 12px;
    padding: 8px 12px;
    font-size: 12px;
    color: var(--pet-text);
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease, transform 0.3s ease;
    transform: translateY(4px);
    z-index: 1000000;
}

.ai-pet-speech::after {
    content: '';
    position: absolute;
    bottom: -6px;
    right: 24px;
    width: 10px;
    height: 10px;
    background: #fff;
    border-right: 1px solid var(--pet-border);
    border-bottom: 1px solid var(--pet-border);
    transform: rotate(45deg);
}

.ai-pet-speech.show {
    opacity: 1;
    transform: translateY(0);
}

/* --- Chat Window --- */
.ai-pet-chat {
    position: fixed;
    bottom: 110px;
    right: 24px;
    width: 360px;
    max-width: calc(100vw - 48px);
    height: 520px;
    max-height: calc(100vh - 160px);
    background: var(--pet-bg);
    border-radius: var(--pet-radius);
    box-shadow: var(--pet-shadow);
    z-index: 999998;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    transform: translateY(20px) scale(0.95);
    pointer-events: none;
    transition: opacity var(--pet-transition), transform var(--pet-transition);
}

.ai-pet-chat.open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

/* --- Chat Header --- */
.ai-pet-header {
    display: flex;
    align-items: center;
    padding: 14px 16px;
    background: linear-gradient(135deg, var(--pet-primary), #6bb3f0);
    color: #fff;
    flex-shrink: 0;
    flex-grow: 0;
}

.ai-pet-header-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.25);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 10px;
    flex-shrink: 0;
    overflow: hidden;
}

.ai-pet-header-avatar img {
    width: 24px;
    height: 24px;
    object-fit: contain;
    image-rendering: pixelated;
}

.ai-pet-header-info {
    flex: 1;
    min-width: 0;
}

.ai-pet-header-name {
    font-weight: 600;
    font-size: 15px;
    line-height: 1.3;
}

.ai-pet-header-status {
    font-size: 12px;
    opacity: 0.85;
}

.ai-pet-close {
    width: 28px;
    height: 28px;
    border: none;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    color: #fff;
    font-size: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    flex-shrink: 0;
    line-height: 1;
}

.ai-pet-close:hover {
    background: rgba(255, 255, 255, 0.35);
}

/* --- Messages Area --- */
.ai-pet-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
}

.ai-pet-messages::-webkit-scrollbar {
    width: 4px;
}

.ai-pet-messages::-webkit-scrollbar-thumb {
    background: var(--pet-border);
    border-radius: 2px;
}

/* --- Message Bubble --- */
.ai-pet-msg {
    display: flex;
    gap: 8px;
    max-width: 85%;
    animation: pet-msg-in 0.3s ease-out;
}

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

.ai-pet-msg.user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.ai-pet-msg-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    background: linear-gradient(135deg, #7ec8e3, #4a90d9);
}

.ai-pet-msg-avatar img {
    width: 18px;
    height: 18px;
    object-fit: contain;
    image-rendering: pixelated;
}

.ai-pet-msg-avatar svg {
    width: 18px;
    height: 18px;
}

.ai-pet-msg-bubble {
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 14px;
    line-height: 1.5;
    word-break: break-word;
}

.ai-pet-msg.pet .ai-pet-msg-bubble {
    background: var(--pet-pet-bg);
    border: 1px solid var(--pet-border);
    border-top-left-radius: 4px;
    color: var(--pet-text);
}

.ai-pet-msg.user .ai-pet-msg-bubble {
    background: var(--pet-user-bg);
    border-top-right-radius: 4px;
    color: var(--pet-text);
}

/* --- Streaming Bubble (receiving text) --- */
.ai-pet-msg-bubble.ai-pet-streaming::after {
    content: '▌';
    color: var(--pet-primary);
    animation: pet-cursor-blink 0.8s step-end infinite;
}

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

/* --- Typing Indicator --- */
.ai-pet-typing {
    display: none;
    align-self: flex-start;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
    padding: 0 16px 8px;
}

.ai-pet-typing.show {
    display: flex;
}

.ai-pet-typing-dots {
    display: flex;
    gap: 4px;
    padding: 10px 14px;
    background: var(--pet-pet-bg);
    border: 1px solid var(--pet-border);
    border-radius: 14px;
    border-top-left-radius: 4px;
}

.ai-pet-typing-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--pet-text-light);
    animation: pet-typing-bounce 1.4s infinite;
}

.ai-pet-typing-dot:nth-child(2) {
    animation-delay: 0.2s;
}

.ai-pet-typing-dot:nth-child(3) {
    animation-delay: 0.4s;
}

/* --- Input Area --- */
.ai-pet-input-area {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    border-top: 1px solid var(--pet-border);
    gap: 8px;
    flex-shrink: 0;
    flex-grow: 0;
}

.ai-pet-input {
    flex: 1;
    border: 1px solid var(--pet-border);
    border-radius: 20px;
    padding: 8px 14px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s;
    font-family: inherit;
    color: var(--pet-text);
}

.ai-pet-input:focus {
    border-color: var(--pet-primary);
}

.ai-pet-input::placeholder {
    color: var(--pet-text-light);
}

.ai-pet-send {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: none;
    background: var(--pet-primary);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.15s;
    flex-shrink: 0;
}

.ai-pet-send:hover {
    background: var(--pet-primary-dark);
}

.ai-pet-send:active {
    transform: scale(0.9);
}

.ai-pet-send:disabled {
    background: #ccc;
    cursor: not-allowed;
    transform: none;
}

.ai-pet-send svg {
    width: 18px;
    height: 18px;
}

/* --- Quick Actions --- */
.ai-pet-quick {
    display: flex;
    gap: 6px;
    padding: 0 16px 8px;
    flex-wrap: wrap;
    flex-shrink: 0;
    max-height: 60px;
    overflow: hidden;
}

.ai-pet-quick-btn {
    border: 1px solid var(--pet-border);
    border-radius: 14px;
    padding: 4px 10px;
    font-size: 12px;
    background: #fff;
    color: var(--pet-text);
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.ai-pet-quick-btn:hover {
    border-color: var(--pet-primary);
    color: var(--pet-primary);
    background: var(--pet-pet-bg);
}

/* --- Berserk Quick Button --- */
.ai-pet-quick-berserk {
    border: 1px solid #ff4444;
    border-radius: 14px;
    padding: 4px 10px;
    font-size: 12px;
    background: #fff;
    color: #ff4444;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
    font-weight: 600;
}

.ai-pet-quick-berserk:hover {
    border-color: #cc0000;
    color: #fff;
    background: #ff4444;
}

.ai-pet-quick-berserk.active {
    background: #ff4444;
    color: #fff;
    border-color: #cc0000;
    box-shadow: 0 0 8px rgba(255, 68, 68, 0.6);
    animation: pet-berserk-pulse 1s ease-in-out infinite;
}

@keyframes pet-berserk-pulse {
    0%, 100% { box-shadow: 0 0 8px rgba(255, 68, 68, 0.4); }
    50% { box-shadow: 0 0 16px rgba(255, 68, 68, 0.8); }
}

/* --- Berserk Chat Mode --- */
.ai-pet-chat.berserk {
    border: 2px solid #ff4444;
    box-shadow: 0 4px 20px rgba(255, 68, 68, 0.3);
}

.ai-pet-chat.berserk .ai-pet-header {
    background: linear-gradient(135deg, #cc0000, #ff4444);
}

/* --- Error Toast --- */
.ai-pet-error {
    display: none;
    position: absolute;
    top: 60px;
    left: 50%;
    transform: translateX(-50%);
    background: #fff3f3;
    border: 1px solid #ffcccc;
    color: #cc0000;
    padding: 6px 14px;
    border-radius: 8px;
    font-size: 12px;
    z-index: 1;
    white-space: nowrap;
    animation: pet-fade-in 0.3s ease;
}

.ai-pet-error.show {
    display: block;
}

/* --- Animations --- */
@keyframes pet-breathe {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-4px); }
}

@keyframes pet-glow {
    0%, 100% { transform: scale(1); opacity: 0; }
    50% { transform: scale(1.05); opacity: 0.2; }
}

@keyframes pet-happy {
    0% { transform: rotate(0deg) scale(1); }
    25% { transform: rotate(-10deg) scale(1.1); }
    50% { transform: rotate(10deg) scale(1.1); }
    75% { transform: rotate(-5deg) scale(1.05); }
    100% { transform: rotate(0deg) scale(1); }
}

@keyframes pet-sad {
    0% { transform: translateY(0); }
    30% { transform: translateY(3px); }
    60% { transform: translateY(-2px); }
    100% { transform: translateY(0); }
}

@keyframes pet-msg-in {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

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

@keyframes pet-fade-in {
    from { opacity: 0; transform: translateX(-50%) translateY(-4px); }
    to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

@keyframes pet-idle-bounce {
    0%, 100% { transform: translateY(0) rotate(0deg); }
    20% { transform: translateY(-6px) rotate(-3deg); }
    40% { transform: translateY(0) rotate(0deg); }
    60% { transform: translateY(-3px) rotate(3deg); }
    80% { transform: translateY(0) rotate(0deg); }
}

.ai-pet-avatar.idle-bounce {
    animation: pet-idle-bounce 2s ease-in-out;
}

/* --- Global Touch Optimizations --- */
.ai-pet-chat,
.ai-pet-avatar,
.ai-pet-send,
.ai-pet-close,
.ai-pet-quick-btn {
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
}

.ai-pet-chat {
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

/* --- Swipe Pull Indicator (Mobile) --- */
.ai-pet-pull-indicator {
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%) translateY(-100%);
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.2s;
    pointer-events: none;
    z-index: 1;
}

.ai-pet-pull-indicator.visible {
    opacity: 1;
}

.ai-pet-pull-indicator svg {
    width: 20px;
    height: 20px;
    color: rgba(0, 0, 0, 0.3);
}

.ai-pet-pull-progress {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(90deg, transparent, var(--pet-primary), transparent);
    transform: scaleX(0);
    transition: transform 0.1s linear;
    pointer-events: none;
}

/* --- Tablet (max-width: 768px) --- */
@media (max-width: 768px) {
    :root {
        --pet-size: 60px;
    }

    .ai-pet-avatar {
        bottom: calc(16px + env(safe-area-inset-bottom, 0px));
        right: calc(16px + env(safe-area-inset-right, 0px));
    }

    .ai-pet-squirtle {
        width: 50px;
        height: 55px;
    }

    .ai-pet-speech {
        display: none;
    }

    .ai-pet-chat {
        bottom: calc(8px + env(safe-area-inset-bottom, 0px));
        right: 8px;
        left: 8px;
        width: auto;
        height: calc(100vh - 90px - env(safe-area-inset-bottom, 0px));
        max-height: calc(100vh - 90px - env(safe-area-inset-bottom, 0px));
        border-radius: 16px;
    }

    .ai-pet-chat.open {
        right: 8px;
        left: 8px;
    }

    .ai-pet-close {
        width: 44px;
        height: 44px;
        font-size: 20px;
    }

    .ai-pet-send {
        width: 44px;
        height: 44px;
    }

    .ai-pet-send svg {
        width: 20px;
        height: 20px;
    }

    .ai-pet-input {
        font-size: 16px;
        padding: 10px 14px;
        min-height: 44px;
    }

    .ai-pet-quick-btn {
        padding: 8px 14px;
        font-size: 13px;
        min-height: 36px;
    }

    .ai-pet-messages {
        padding: 12px;
    }

    .ai-pet-msg-bubble {
        font-size: 15px;
        padding: 12px 16px;
    }

    .ai-pet-header {
        padding: 12px 16px;
    }

    .ai-pet-input-area {
        padding: 12px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    }

    .ai-pet-msg {
        max-width: 90%;
    }

    /* Larger touch targets on tablet */
    .ai-pet-quick-btn {
        min-height: 40px;
    }
}

/* --- Phone (max-width: 480px) --- */
@media (max-width: 480px) {
    :root {
        --pet-size: 52px;
    }

    .ai-pet-avatar {
        bottom: calc(12px + env(safe-area-inset-bottom, 0px));
        right: calc(12px + env(safe-area-inset-right, 0px));
    }

    .ai-pet-squirtle {
        width: 44px;
        height: 48px;
    }

    .ai-pet-chat {
        bottom: 0;
        left: 0;
        right: 0;
        width: 100vw;
        height: calc(100vh - 60px);
        max-height: calc(100vh - 60px);
        border-radius: 16px 16px 0 0;
    }

    .ai-pet-chat.open {
        left: 0;
        right: 0;
    }

    .ai-pet-header-name {
        font-size: 16px;
    }

    .ai-pet-header-status {
        font-size: 13px;
    }

    /* Full-screen sheet animation on phones */
    .ai-pet-chat {
        transform: translateY(100%);
        transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1);
    }

    .ai-pet-chat.open {
        transform: translateY(0);
    }

    /* Increase message bubble size for readability */
    .ai-pet-msg-bubble {
        font-size: 16px;
        padding: 14px 18px;
        line-height: 1.6;
    }

    .ai-pet-msg {
        max-width: 92%;
    }

    /* Pull indicator hidden on full-screen phones */
    .ai-pet-pull-indicator {
        display: none;
    }
}

/* --- Landscape Mode --- */
@media (max-height: 500px) and (orientation: landscape) {
    .ai-pet-chat {
        bottom: 8px;
        right: 8px;
        left: 8px;
        width: auto;
        height: calc(100vh - 16px);
        max-height: calc(100vh - 16px);
    }

    .ai-pet-messages {
        padding: 8px;
    }

    .ai-pet-header {
        padding: 8px 16px;
    }

    .ai-pet-input-area {
        padding: 8px 16px;
    }

    .ai-pet-quick {
        display: none;
    }

    .ai-pet-msg-bubble {
        padding: 8px 12px;
        font-size: 13px;
    }
}

/* --- Large Tablet (769px - 1024px) --- */
@media (min-width: 769px) and (max-width: 1024px) {
    .ai-pet-chat {
        width: 380px;
        max-width: calc(100vw - 32px);
        height: 500px;
        max-height: calc(100vh - 100px);
    }
}

/* --- Reduced Motion --- */
@media (prefers-reduced-motion: reduce) {
    .ai-pet-avatar,
    .ai-pet-squirtle,
    .ai-pet-particle,
    .ai-pet-typing-dot,
    .ai-pet-msg,
    .ai-pet-error,
    .ai-pet-speech {
        animation: none !important;
    }

    .ai-pet-avatar,
    .ai-pet-chat,
    .ai-pet-send,
    .ai-pet-close,
    .ai-pet-quick-btn,
    .ai-pet-speech {
        transition: none !important;
    }

    .ai-pet-messages {
        scroll-behavior: auto;
    }
}
