/* ============================================================
   FreyAI Website Chatbot Widget
   Floating chat widget — bottom right corner
   Brand: warm, professional, craft-oriented
   ============================================================ */

/* ── Variables ──────────────────────────────────────────── */
:root {
    --chat-primary: #c8956c;
    --chat-primary-dark: #b07a54;
    --chat-primary-light: #d4a882;
    --chat-primary-subtle: rgba(200, 149, 108, 0.12);
    --chat-bg: var(--bg-dark, #131516);
    --chat-surface: #1a1d27;
    --chat-surface-2: #2a2e3a;
    --chat-border: rgba(200, 149, 108, 0.15);
    --chat-border-strong: rgba(200, 149, 108, 0.3);
    --chat-text: #f0ece8;
    --chat-text-muted: #9a9490;
    --chat-user-bubble: #c8956c;
    --chat-bot-bubble: #1a1d27;
    --chat-shadow: 0 25px 60px -12px rgba(0, 0, 0, 0.7),
                   0 0 0 1px rgba(200, 149, 108, 0.08);
    --chat-radius: 20px;
    --chat-width: 390px;
    --chat-height: 560px;
    --chat-font: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
}

/* ── Toggle Button ──────────────────────────────────────── */
#freyai-chat-toggle {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: var(--z-overlay, 9998);
    width: 62px;
    height: 62px;
    border-radius: 50%;
    background: linear-gradient(145deg, var(--chat-primary), var(--chat-primary-dark));
    border: none;
    cursor: pointer;
    box-shadow: 0 6px 24px rgba(200, 149, 108, 0.4),
                0 2px 8px rgba(0, 0, 0, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1),
                box-shadow 0.3s ease;
    outline: none;
    -webkit-tap-highlight-color: transparent;
}

#freyai-chat-toggle:focus-visible {
    outline: 2px solid var(--chat-primary-light);
    outline-offset: 4px;
}

#freyai-chat-toggle:hover {
    transform: scale(1.1);
    box-shadow: 0 10px 36px rgba(200, 149, 108, 0.55),
                0 4px 12px rgba(0, 0, 0, 0.3);
}

#freyai-chat-toggle:active {
    transform: scale(0.95);
}

#freyai-chat-toggle svg {
    width: 26px;
    height: 26px;
    fill: #fff;
    transition: opacity 0.25s ease, transform 0.25s ease;
}

#freyai-chat-toggle .icon-chat { opacity: 1; transform: scale(1); }
#freyai-chat-toggle .icon-close { opacity: 0; transform: scale(0.6); position: absolute; }
#freyai-chat-toggle.open .icon-chat { opacity: 0; transform: scale(0.6); }
#freyai-chat-toggle.open .icon-close { opacity: 1; transform: scale(1); }

/* Pulse ring animation on toggle */
#freyai-chat-toggle::after {
    content: '';
    position: absolute;
    inset: -4px;
    border-radius: 50%;
    border: 2px solid rgba(200, 149, 108, 0.4);
    animation: toggle-ring-pulse 3s ease-in-out infinite;
    pointer-events: none;
}

#freyai-chat-toggle.open::after {
    animation: none;
    opacity: 0;
}

@keyframes toggle-ring-pulse {
    0%, 100% { transform: scale(1); opacity: 0; }
    50% { transform: scale(1.15); opacity: 1; }
}

/* Unread badge */
#freyai-chat-badge {
    position: absolute;
    top: -5px;
    right: -5px;
    background: #ef4444;
    color: #fff;
    border-radius: 50%;
    width: 22px;
    height: 22px;
    font-size: 11px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: var(--chat-font);
    border: 2.5px solid var(--chat-bg);
    animation: badge-pulse 2s ease-in-out infinite;
    box-shadow: 0 2px 8px rgba(239, 68, 68, 0.5);
}

@keyframes badge-pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.15); }
}

/* ── Chat Window ────────────────────────────────────────── */
#freyai-chat-window {
    position: fixed;
    bottom: 100px;
    right: 24px;
    z-index: var(--z-modal, 9999);
    width: var(--chat-width);
    height: var(--chat-height);
    background: var(--chat-bg);
    border-radius: var(--chat-radius);
    border: 1px solid var(--chat-border);
    box-shadow: var(--chat-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    font-family: var(--chat-font);
    font-size: 14px;
    color: var(--chat-text);
    line-height: 1.5;
    opacity: 0;
    transform: translateY(20px) scale(0.95);
    pointer-events: none;
    transition: opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1),
                transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

#freyai-chat-window.open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

/* ── Header ─────────────────────────────────────────────── */
#freyai-chat-header {
    background: linear-gradient(145deg, var(--chat-primary), var(--chat-primary-dark));
    padding: 18px 20px;
    display: flex;
    align-items: center;
    gap: 14px;
    flex-shrink: 0;
    position: relative;
    overflow: hidden;
}

/* Subtle pattern overlay for header depth */
#freyai-chat-header::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(180deg, rgba(255,255,255,0.08) 0%, transparent 100%);
    pointer-events: none;
}

#freyai-chat-avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.18);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 20px;
    position: relative;
    z-index: 1;
}

#freyai-chat-header-info {
    flex: 1;
    position: relative;
    z-index: 1;
}

#freyai-chat-name {
    font-weight: 700;
    font-size: 15px;
    color: #fff;
    line-height: 1.3;
    letter-spacing: -0.01em;
}

#freyai-chat-status {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.8);
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 3px;
    font-weight: 500;
}

#freyai-chat-status::before {
    content: '';
    width: 7px;
    height: 7px;
    background: #4ade80;
    border-radius: 50%;
    display: inline-block;
    box-shadow: 0 0 8px rgba(74, 222, 128, 0.6);
    animation: status-glow 2.5s ease-in-out infinite;
}

@keyframes status-glow {
    0%, 100% { box-shadow: 0 0 4px rgba(74, 222, 128, 0.4); }
    50% { box-shadow: 0 0 10px rgba(74, 222, 128, 0.8); }
}

/* ── Messages Area ──────────────────────────────────────── */
#freyai-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px 16px 10px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    scroll-behavior: smooth;
    overscroll-behavior: contain;
}

/* Custom scrollbar */
#freyai-chat-messages::-webkit-scrollbar {
    width: 5px;
}

#freyai-chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

#freyai-chat-messages::-webkit-scrollbar-thumb {
    background: var(--chat-surface-2);
    border-radius: 3px;
}

#freyai-chat-messages::-webkit-scrollbar-thumb:hover {
    background: var(--chat-primary);
}

/* Firefox scrollbar */
#freyai-chat-messages {
    scrollbar-width: thin;
    scrollbar-color: var(--chat-surface-2) transparent;
}

/* Individual message */
.chat-msg {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    max-width: 100%;
    animation: msg-appear 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes msg-appear {
    from { opacity: 0; transform: translateY(10px); }
    to   { opacity: 1; transform: translateY(0); }
}

.chat-msg.user {
    flex-direction: row-reverse;
}

.chat-msg-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: var(--chat-surface-2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    flex-shrink: 0;
    border: 1px solid var(--chat-border);
}

.chat-msg.user .chat-msg-avatar {
    background: var(--chat-primary-subtle);
    border-color: rgba(200, 149, 108, 0.3);
}

.chat-bubble {
    max-width: 78%;
    padding: 11px 15px;
    border-radius: 16px;
    line-height: 1.55;
    word-break: break-word;
    white-space: pre-wrap;
    font-size: 13.5px;
    letter-spacing: 0.01em;
}

.chat-msg.bot .chat-bubble {
    background: var(--chat-bot-bubble);
    border: 1px solid var(--chat-border);
    border-bottom-left-radius: 4px;
    color: var(--chat-text);
}

.chat-msg.user .chat-bubble {
    background: linear-gradient(145deg, var(--chat-primary), var(--chat-primary-dark));
    border-bottom-right-radius: 4px;
    color: #fff;
    box-shadow: 0 2px 12px rgba(200, 149, 108, 0.25);
}

/* Typing indicator */
.chat-typing {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 12px 16px;
    background: var(--chat-bot-bubble);
    border: 1px solid var(--chat-border);
    border-radius: 16px;
    border-bottom-left-radius: 4px;
    width: fit-content;
}

.chat-typing span {
    width: 7px;
    height: 7px;
    background: var(--chat-primary-light);
    border-radius: 50%;
    animation: typing-dot 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 typing-dot {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.3; }
    30% { transform: translateY(-5px); opacity: 1; }
}

/* Timestamp */
.chat-msg-time {
    font-size: 10px;
    color: var(--chat-text-muted);
    margin-top: 4px;
    padding: 0 4px;
    text-align: right;
    opacity: 0.7;
}

.chat-msg.bot .chat-msg-time {
    text-align: left;
}

/* ── Quick Replies ───────────────────────────────────────── */
#freyai-quick-replies {
    padding: 4px 14px 10px;
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    flex-shrink: 0;
}

.quick-reply-btn {
    padding: 7px 14px;
    border-radius: 20px;
    border: 1px solid var(--chat-border-strong);
    background: var(--chat-primary-subtle);
    color: var(--chat-primary-light);
    font-size: 12.5px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
    font-family: inherit;
    letter-spacing: 0.01em;
    -webkit-tap-highlight-color: transparent;
}

.quick-reply-btn:hover {
    background: var(--chat-primary);
    color: #fff;
    border-color: var(--chat-primary);
    box-shadow: 0 2px 12px rgba(200, 149, 108, 0.35);
    transform: translateY(-1px);
}

.quick-reply-btn:active {
    transform: translateY(0) scale(0.97);
}

.quick-reply-btn:focus-visible {
    outline: 2px solid var(--chat-primary-light);
    outline-offset: 2px;
}

/* ── Input Area ─────────────────────────────────────────── */
#freyai-chat-input-area {
    padding: 12px 16px 14px;
    border-top: 1px solid var(--chat-border);
    display: flex;
    gap: 10px;
    align-items: flex-end;
    background: var(--chat-surface);
    flex-shrink: 0;
}

#freyai-chat-input {
    flex: 1;
    background: var(--chat-bg);
    border: 1px solid var(--chat-border);
    border-radius: 14px;
    padding: 10px 16px;
    color: var(--chat-text);
    font-size: 14px;
    font-family: inherit;
    resize: none;
    min-height: 44px;
    max-height: 100px;
    outline: none;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
    line-height: 1.45;
    -webkit-appearance: none;
}

#freyai-chat-input::placeholder {
    color: var(--chat-text-muted);
    opacity: 0.8;
}

#freyai-chat-input:focus {
    border-color: var(--chat-primary);
    box-shadow: 0 0 0 3px rgba(200, 149, 108, 0.15);
}

#freyai-chat-send {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    background: linear-gradient(145deg, var(--chat-primary), var(--chat-primary-dark));
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(200, 149, 108, 0.3);
    -webkit-tap-highlight-color: transparent;
}

#freyai-chat-send:hover {
    background: linear-gradient(145deg, var(--chat-primary-dark), #9a6943);
    box-shadow: 0 4px 16px rgba(200, 149, 108, 0.4);
    transform: translateY(-1px);
}

#freyai-chat-send:active {
    transform: scale(0.93) translateY(0);
}

#freyai-chat-send:focus-visible {
    outline: 2px solid var(--chat-primary-light);
    outline-offset: 2px;
}

#freyai-chat-send:disabled {
    background: var(--chat-surface-2);
    cursor: not-allowed;
    box-shadow: none;
    transform: none;
}

#freyai-chat-send svg {
    width: 18px;
    height: 18px;
    fill: #fff;
}

/* ── Footer branding ────────────────────────────────────── */
#freyai-chat-footer {
    text-align: center;
    padding: 0 16px 10px;
    font-size: 10px;
    color: var(--chat-text-muted);
    background: var(--chat-surface);
    flex-shrink: 0;
    letter-spacing: 0.02em;
    opacity: 0.7;
}

#freyai-chat-footer a {
    color: var(--chat-primary-light);
    text-decoration: none;
    transition: color 0.15s ease;
}

#freyai-chat-footer a:hover {
    color: var(--chat-primary);
    text-decoration: underline;
}

/* ── Privacy notice ────────────────────────────────────── */
.freyai-chat-privacy {
    font-size: 11px;
    color: var(--chat-text-muted);
    text-align: center;
    padding: 8px 14px;
    margin: 0 4px 6px;
    line-height: 1.5;
    background: var(--chat-primary-subtle);
    border-radius: 10px;
    border: 1px solid var(--chat-border);
}

.freyai-chat-privacy a {
    color: var(--chat-primary-light);
    text-decoration: underline;
    text-decoration-color: rgba(200, 149, 108, 0.4);
    text-underline-offset: 2px;
    transition: text-decoration-color 0.15s ease;
}

.freyai-chat-privacy a:hover {
    text-decoration-color: var(--chat-primary);
}

/* ── Responsive — Mobile ───────────────────────────────── */
@media (max-width: 480px) {
    #freyai-chat-window {
        width: calc(100vw - 16px);
        right: 8px;
        bottom: 80px;
        height: calc(100dvh - 96px);
        max-height: 600px;
        border-radius: 16px;
    }

    #freyai-chat-toggle {
        right: 16px;
        bottom: 16px;
        width: 56px;
        height: 56px;
    }

    #freyai-chat-toggle svg {
        width: 24px;
        height: 24px;
    }

    #freyai-chat-header {
        padding: 14px 16px;
    }

    .chat-bubble {
        max-width: 85%;
    }

    .quick-reply-btn {
        font-size: 12px;
        padding: 6px 12px;
    }
}

/* Tablets and small desktops */
@media (min-width: 481px) and (max-width: 768px) {
    #freyai-chat-window {
        width: 370px;
        height: 500px;
    }
}

/* ── Light mode / system preference ───────────────────── */
@media (prefers-color-scheme: light) {
    :root {
        --chat-bg: #faf8f6;
        --chat-surface: #f0ece8;
        --chat-surface-2: #e4ddd6;
        --chat-border: rgba(200, 149, 108, 0.2);
        --chat-border-strong: rgba(200, 149, 108, 0.35);
        --chat-text: #2c2520;
        --chat-text-muted: #8a7e76;
        --chat-bot-bubble: #f0ece8;
        --chat-primary-subtle: rgba(200, 149, 108, 0.1);
        --chat-shadow: 0 25px 60px -12px rgba(0, 0, 0, 0.15),
                       0 0 0 1px rgba(200, 149, 108, 0.12);
    }

    #freyai-chat-badge {
        border-color: var(--chat-bg);
    }

    .chat-msg.bot .chat-bubble {
        border-color: rgba(200, 149, 108, 0.18);
    }

    #freyai-chat-messages::-webkit-scrollbar-thumb {
        background: var(--chat-surface-2);
    }
}

/* ── Print ────────────────────────────────────────────── */
@media print {
    #freyai-chatbot-container,
    #freyai-chat-toggle,
    #freyai-chat-window {
        display: none !important;
    }
}

/* ── Reduced motion ──────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    #freyai-chat-toggle,
    #freyai-chat-window,
    .chat-msg,
    .quick-reply-btn,
    #freyai-chat-send,
    #freyai-chat-toggle svg {
        transition: none !important;
        animation: none !important;
    }

    .chat-typing span {
        animation: none !important;
        opacity: 0.6;
    }

    #freyai-chat-toggle::after {
        animation: none !important;
        display: none;
    }
}

/* ── High contrast mode ─────────────────────────────── */
@media (forced-colors: active) {
    #freyai-chat-toggle {
        border: 2px solid ButtonText;
    }

    #freyai-chat-window {
        border: 2px solid ButtonText;
    }

    .chat-bubble {
        border: 1px solid ButtonText;
    }

    #freyai-chat-input {
        border: 1px solid ButtonText;
    }
}
