/**
 * VR Chat Widget — HTMX/Alpine.js Frontend Styles
 *
 * Extracted from inline <style> in ChatWidget.php to improve
 * cacheability, CSP compliance, and maintainability.
 *
 * @since 0.8.0
 */

/* ================================================================
   Core Widget Layout
   ================================================================ */

#vr-chat-widget {
    font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    z-index: 2147483647;
    position: fixed;
    bottom: 24px;
    right: 24px;
    pointer-events: none;
}

#vr-chat-widget * {
    box-sizing: border-box;
    pointer-events: auto;
}

/* ================================================================
   Toggle Button
   ================================================================ */

.vr-chat-toggle {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: linear-gradient(135deg, #0073aa 0%, #004a6e 100%);
    color: #ffffff;
    border: none;
    box-shadow: 0 8px 24px rgba(0, 115, 170, 0.3);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275), box-shadow 0.3s ease;
    z-index: 3;
}

.vr-chat-toggle:hover {
    transform: scale(1.08);
    box-shadow: 0 12px 28px rgba(0, 115, 170, 0.4);
}

.vr-chat-toggle-text {
    display: none;
    margin-left: 12px;
    font-weight: 600;
    font-size: 16px;
    white-space: nowrap;
    color: #ffffff;
}

/* ================================================================
   Chat Window
   ================================================================ */

.vr-chat-window {
    position: absolute;
    bottom: 84px;
    right: 0;
    width: 380px;
    max-width: calc(100vw - 48px);
    height: 600px;
    max-height: calc(100vh - 120px);
    background: #ffffff;
    border-radius: 20px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15), 0 1px 3px rgba(0, 0, 0, 0.05);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid rgba(0, 0, 0, 0.05);
    transform-origin: bottom right;
    z-index: 2;
}

/* ================================================================
   Layout Variants: Lightbox
   ================================================================ */

.vr-layout-lightbox.is-open {
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.5);
    pointer-events: auto !important;
    /* aria-modal support: prevent scroll on body */
    overflow: hidden;
}

.vr-layout-lightbox.is-open .vr-chat-window {
    position: relative;
    bottom: auto;
    right: auto;
    transform-origin: center;
    max-width: 90vw;
    max-height: 85vh;
}

.vr-layout-lightbox.is-open .vr-chat-toggle {
    display: none;
}

/* Lightbox backdrop click area for closing */
.vr-layout-lightbox.is-open::before {
    content: '';
    position: absolute;
    inset: 0;
    z-index: -1;
}

/* ================================================================
   Layout Variants: Bottom & Top Full Width
   ================================================================ */

.vr-layout-bottom .vr-chat-toggle,
.vr-layout-top .vr-chat-toggle {
    width: 100%;
    border-radius: 0;
    bottom: 0;
    right: 0;
    height: 64px;
    justify-content: flex-start;
    padding: 0 24px;
}

.vr-layout-top .vr-chat-toggle {
    bottom: auto;
    top: 0;
}

.vr-layout-bottom .vr-chat-toggle .vr-chat-toggle-text,
.vr-layout-top .vr-chat-toggle .vr-chat-toggle-text {
    display: block;
}

.vr-layout-bottom .vr-chat-window {
    bottom: 64px;
    right: auto;
    left: 50%;
    transform: translateX(-50%);
    transform-origin: bottom center;
}

.vr-layout-top .vr-chat-window {
    bottom: auto;
    top: 64px;
    right: auto;
    left: 50%;
    transform: translateX(-50%);
    transform-origin: top center;
}

/* ================================================================
   Layout Variants: Left & Right Full Height
   ================================================================ */

.vr-layout-left .vr-chat-toggle,
.vr-layout-right .vr-chat-toggle {
    width: 50px;
    height: 100%;
    border-radius: 0;
    bottom: 0;
    flex-direction: column;
    justify-content: center;
}

.vr-layout-left .vr-chat-toggle {
    right: auto;
    left: 0;
}

.vr-layout-right .vr-chat-toggle {
    right: 0;
}

.vr-layout-left .vr-chat-toggle .vr-chat-toggle-text,
.vr-layout-right .vr-chat-toggle .vr-chat-toggle-text {
    display: block;
    writing-mode: vertical-lr;
    transform: rotate(180deg);
    margin-left: 0;
    margin-top: 12px;
}

.vr-layout-left .vr-chat-window,
.vr-layout-right .vr-chat-window {
    bottom: 0;
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
}

.vr-layout-left .vr-chat-window {
    left: 50px;
    right: auto;
    transform-origin: left center;
}

.vr-layout-right .vr-chat-window {
    right: 50px;
    transform-origin: right center;
}

.vr-layout-bottom {
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
}

.vr-layout-top {
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: auto !important;
}

.vr-layout-left {
    top: 0 !important;
    bottom: 0 !important;
    left: 0 !important;
    right: auto !important;
}

.vr-layout-right {
    top: 0 !important;
    bottom: 0 !important;
    right: 0 !important;
    left: auto !important;
}

/* ================================================================
   Header
   ================================================================ */

.vr-chat-header {
    background: #ffffff;
    color: #111827;
    padding: 14px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #e5e7eb;
    gap: 10px;
    z-index: 2;
}

.vr-chat-header-info {
    display: flex;
    align-items: center;
    gap: 10px;
    flex: 1;
    min-width: 0;
}

.vr-chat-header-icon {
    width: 34px;
    height: 34px;
    border-radius: 8px;
    background: #111827;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.vr-chat-header h3 {
    margin: 0;
    font-size: 14px;
    font-weight: 600;
    color: #111827;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.vr-chat-close {
    background: transparent !important;
    border: none !important;
    color: #6b7280 !important;
    width: 30px;
    height: 30px;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.vr-chat-close:hover {
    background: #f3f4f6 !important;
    color: #111827 !important;
}

/* ================================================================
   Messages
   ================================================================ */

.vr-chat-messages {
    flex: 1;
    padding: 16px 16px 16px 52px;
    overflow-y: auto;
    background: #f5f6f7;
    display: flex;
    flex-direction: column;
    gap: 10px;
    scroll-behavior: smooth;
}

.vr-starter-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-top: 2px;
    margin-left: -36px; /* align with message left edge */
}

.vr-starter-btn {
    background: #ffffff;
    border: 1px solid #e5e7eb;
    color: #374151;
    padding: 10px 12px;
    border-radius: 10px;
    font-size: 12.5px;
    cursor: pointer;
    transition: all 0.15s;
    text-align: left;
    line-height: 1.35;
    font-weight: 500;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.vr-starter-btn:hover {
    border-color: #0073aa;
    color: #0073aa;
    background: #f0f7fb;
    transform: translateY(-1px);
    box-shadow: 0 2px 6px rgba(0,115,170,0.12);
}

.vr-chat-messages::-webkit-scrollbar {
    width: 6px;
}

.vr-chat-messages::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 3px;
}

.vr-message {
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 14.5px;
    line-height: 1.5;
    max-width: 85%;
    word-wrap: break-word;
    animation: vrChatPopIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes vrChatPopIn {
    from {
        opacity: 0;
        transform: translateY(10px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.vr-message-bot {
    background: #ffffff;
    color: #1e293b;
    align-self: flex-start;
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
    border: 1px solid #e5e7eb;
    position: relative;
}

/* Bot avatar — dark rounded square to the left of each bot message */
.vr-message-bot::before {
    content: '';
    position: absolute;
    left: -40px;
    top: 4px;
    width: 28px;
    height: 28px;
    border-radius: 7px;
    background: #111827 url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z'%3E%3C/path%3E%3C/svg%3E") center/60% no-repeat;
    flex-shrink: 0;
}

/* ---------- Bot message HTML normalization ----------
   LLM responses may contain headings, lists, links, and
   paragraphs. These reset rules ensure consistent sizing
   and spacing within the chat bubble.
   ---------------------------------------------------- */

.vr-message-bot h1,
.vr-message-bot h2,
.vr-message-bot h3,
.vr-message-bot h4,
.vr-message-bot h5,
.vr-message-bot h6 {
    font-size: 14.5px;
    font-weight: 700;
    margin: 8px 0 4px;
    line-height: 1.4;
}

.vr-message-bot p {
    margin: 0 0 8px;
    font-size: inherit;
    line-height: inherit;
}

.vr-message-bot p:last-child {
    margin-bottom: 0;
}

.vr-message-bot ul,
.vr-message-bot ol {
    margin: 4px 0 8px;
    padding-left: 20px;
    font-size: inherit;
    line-height: inherit;
}

.vr-message-bot li {
    margin-bottom: 4px;
}

.vr-message-bot a {
    color: #0073aa;
    text-decoration: underline;
    word-break: break-word;
}

.vr-message-bot a:hover {
    color: #005177;
}

.vr-message-bot strong,
.vr-message-bot b {
    font-weight: 700;
}

.vr-message-bot br + br {
    display: none;  /* collapse double line breaks */
}

.vr-message-user {
    background: #0073aa;
    color: #ffffff;
    align-self: flex-end;
    border-bottom-right-radius: 4px;
    box-shadow: 0 1px 4px rgba(0, 115, 170, 0.2);
    border: none;
}

.vr-message-error {
    background: #fef2f2;
    color: #991b1b;
    border: 1px solid #fca5a5;
    align-self: flex-start;
}

/* ================================================================
   Input Area
   ================================================================ */

.vr-chat-input-area {
    padding: 12px 16px;
    background: #ffffff;
    border-top: 1px solid #e5e7eb;
    position: relative;
}

#vr-chat-form {
    display: flex;
    gap: 8px;
    align-items: center;
}

#vr-chat-form input[type="text"] {
    flex-grow: 1;
    padding: 11px 16px;
    border: 1.5px solid #e5e7eb;
    border-radius: 24px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
    background: #ffffff;
    color: #111827;
}

#vr-chat-form input[type="text"]::placeholder {
    color: #9ca3af;
}

#vr-chat-form input[type="text"]:focus {
    border-color: #0073aa;
    background: #ffffff;
    box-shadow: 0 0 0 3px rgba(0, 115, 170, 0.1);
}

#vr-chat-form button {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: #0073aa;
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.15s;
    flex-shrink: 0;
}

#vr-chat-form button:hover {
    background: #005177;
    transform: scale(1.06);
}

.vr-chat-loading {
    font-size: 12px;
    color: #475569; /* WCAG AA: 6.4:1 on #f8fafc (was #64748b = 3.9:1) */
    margin-top: 8px;
    text-align: center;
    font-style: italic;
}

/* ================================================================
   Accessibility (WCAG 2.2 AA)
   ================================================================ */

/* Screen-reader-only utility */
.vr-sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Focus-visible indicators (SC 2.4.7 / 2.4.13) */
.vr-chat-toggle:focus-visible,
.vr-chat-close:focus-visible,
#vr-chat-form button:focus-visible,
.vr-starter-btn:focus-visible {
    outline: 2px solid #0073aa;
    outline-offset: 2px;
}

#vr-chat-input:focus-visible {
    border-color: #0073aa;
    box-shadow: 0 0 0 3px rgba(0, 115, 170, 0.15);
    outline: 2px solid transparent;
}

/* Reduced motion support (SC 2.3.3) */
@media (prefers-reduced-motion: reduce) {
    .vr-chat-toggle,
    .vr-chat-window,
    .vr-message,
    .vr-chat-loading span {
        animation: none !important;
        transition-duration: 0.01ms !important;
    }
}
