/* src/OspVnWebsite.Web/wwwroot/css/chat-widget-v2.css
   Rewrite: Scroll isolation architecture
   Key changes:
   - overscroll-behavior: contain để tách biệt scroll contexts
   - touch-action: pan-y cho phép scroll tự nhiên
   - Không dùng position:fixed trên body
   - Dùng class .osp-chat-v2-active để kiểm soát page scroll
*/

:root {
    --osp-chat-v2-theme-hover: #1d4ed8;
    --osp-chat-v2-bg: #ffffff;
    --osp-chat-v2-border: #e5e7eb;
    --osp-chat-v2-text: #1f2937;
    --osp-chat-v2-text-secondary: #6b7280;
    --osp-chat-v2-user-bubble: #00AEEF;
    --osp-chat-v2-assistant-bubble: #f3f4f6;
    --osp-chat-v2-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
    --osp-chat-v2-z-index: 9999;
    /* Animation timing */
    --osp-anim-fast: 150ms ease-out;
    --osp-anim-normal: 250ms ease-out;
    --osp-anim-slow: 300ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* ========================================
   SCROLL ISOLATION - Core fix
   ======================================== */

/* Khi chat mở, dùng overflow-hidden thay vì position:fixed */
body.osp-chat-v2-active {
    overflow: hidden;
    /* Ngăn bounce scroll trên iOS */
    position: relative;
}

/* ========================================
   Widget Container
   ======================================== */

.osp-chat-v2-widget {
    position: fixed;
    bottom: max(30px, env(safe-area-inset-bottom, 30px));
    right: max(30px, env(safe-area-inset-right, 30px));
    z-index: var(--osp-chat-v2-z-index);
    font-family: inherit;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    transition: bottom 0.5s ease;
    pointer-events: auto;
}

/* Khi button scroll-top xuất hiện, dời widget lên */
.osp-chat-v2-widget.has-scroll-top {
    bottom: 90px;
}

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

.osp-chat-v2-toggle {
    min-width: 44px;
    min-height: 44px;
    width: 50px;
    height: 50px;
    padding: 0;
    background: var(--osp-primary);
    color: white;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: var(--osp-chat-v2-shadow);
    transition: all 0.3s ease;
    position: relative;
    margin-left: auto;
    display: flex;
    align-items: center;
    justify-content: center;
}

.osp-chat-v2-toggle:hover {
    background: #1a4a8a;
    transform: scale(1.05);
}

.osp-chat-v2-toggle::after {
    content: attr(title);
    position: absolute;
    bottom: calc(100% + 8px);
    right: 0;
    background: #1f2937;
    color: white;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 500;
    white-space: nowrap;
    opacity: 0;
    visibility: hidden;
    transition: all 0.2s ease;
    pointer-events: none;
    z-index: calc(var(--osp-chat-v2-z-index) + 1);
}

.osp-chat-v2-toggle:hover::after {
    opacity: 1;
    visibility: visible;
}

.osp-chat-v2-icon {
    width: 24px;
    height: 24px;
}

/* ========================================
   Chat Panel
   ======================================== */

.osp-chat-v2-panel {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 470px;
    max-width: min(calc(100vw - 90px), 90vw);
    height: 750px;
    max-height: min(calc(100vh - 130px), 85vh);
    background: var(--osp-chat-v2-bg);
    border-radius: 16px;
    box-shadow: var(--osp-chat-v2-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: ospChatV2PanelSlide var(--osp-anim-slow, 300ms) cubic-bezier(0.4, 0, 0.2, 1);
    pointer-events: auto;
}

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

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

.osp-chat-v2-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    background: var(--osp-primary);
    color: white;
    flex-shrink: 0;
}

.osp-chat-v2-header-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    font-size: 15px;
}

.osp-chat-v2-avatar {
    font-size: 18px;
    display: flex;
    align-items: center;
}

.osp-chat-v2-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.osp-chat-v2-header-action {
    min-width: 44px;
    min-height: 44px;
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    font-size: 14px;
    cursor: pointer;
    padding: 6px 10px;
    border-radius: 6px;
    transition: background 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.osp-chat-v2-header-action:hover {
    background: rgba(255, 255, 255, 0.3);
}

.osp-chat-v2-close {
    min-width: 44px;
    min-height: 44px;
    background: none;
    border: none;
    color: white;
    font-size: 24px;
    cursor: pointer;
    padding: 0;
    line-height: 1;
    opacity: 0.8;
    display: flex;
    align-items: center;
    justify-content: center;
}

.osp-chat-v2-close:hover {
    opacity: 1;
}

.osp-chat-v2-clear {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    font-size: 13px;
    cursor: pointer;
    padding: 4px 10px;
    border-radius: 6px;
    transition: background 0.2s;
    display: flex;
    align-items: center;
    gap: 4px;
}

.osp-chat-v2-clear:hover {
    background: rgba(255, 255, 255, 0.3);
}

.osp-chat-v2-clear svg {
    width: 14px;
    height: 14px;
}

/* ========================================
   Messages Container - SCROLL ISOLATION
   ======================================== */

.osp-chat-v2-messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    position: relative;
    /* Core scroll isolation */
    overscroll-behavior: contain;
    /* Cho phép scroll tự nhiên, ngăn conflict với touch handlers khác */
    touch-action: pan-y;
    /* Smooth scrolling trên iOS */
    -webkit-overflow-scrolling: touch;
    /* Always show scrollbar */
    scrollbar-width: auto;
    scrollbar-color: #c1c1c1 transparent;
}

.osp-chat-v2-messages::-webkit-scrollbar {
    width: 8px;
}

.osp-chat-v2-messages::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
}

.osp-chat-v2-messages::-webkit-scrollbar-thumb {
    background-color: #c1c1c1;
    border-radius: 4px;
}

.osp-chat-v2-messages::-webkit-scrollbar-thumb:hover {
    background-color: #a1a1a1;
}

.osp-chat-v2-welcome {
    color: var(--osp-chat-v2-text-secondary);
    font-size: 13px;
    line-height: 1.5;
    text-align: center;
    padding: 20px;
}

/* ========================================
   Message Bubbles
   ======================================== */

.osp-chat-v2-message {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 14px;
    line-height: 1.5;
    animation: ospChatV2SlideFade var(--osp-anim-normal, 250ms) ease-out;
    overflow-wrap: break-word;
    word-wrap: break-word;
    word-break: keep-all;
    white-space: pre-wrap;
}

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

.osp-chat-v2-message.user {
    align-self: flex-end;
    background: var(--osp-chat-v2-user-bubble);
    color: white;
    border-bottom-right-radius: 4px;
}

.osp-chat-v2-message.assistant {
    align-self: flex-start;
    background: var(--osp-chat-v2-assistant-bubble);
    color: var(--osp-chat-v2-text);
    border-bottom-left-radius: 4px;
}

.osp-chat-v2-message.error {
    background: #fef2f2;
    color: #dc2626;
    border: 1px solid #fecaca;
}

/* ========================================
   Chat Links
   ======================================== */

.osp-chat-v2-message a,
.osp-chat-v2-link {
    color: var(--osp-primary);
    text-decoration: none;
    font-weight: 500;
    transition: color 0.2s;
    word-break: keep-all;
    overflow-wrap: break-word;
}

.osp-chat-v2-message a:hover,
.osp-chat-v2-link:hover {
    color: var(--osp-chat-v2-theme-hover);
    text-decoration: underline;
}

.osp-chat-v2-message.user a {
    color: #fff;
    text-decoration: underline;
}

.osp-chat-v2-message.user a:hover {
    color: #dbeafe;
}

/* ========================================
   Typing Indicator
   ======================================== */

.osp-chat-v2-typing {
    display: flex;
    gap: 4px;
    padding: 10px 14px;
    align-self: flex-start;
}

.osp-chat-v2-typing span {
    width: 8px;
    height: 8px;
    background: var(--osp-chat-v2-text-secondary);
    border-radius: 50%;
    animation: ospChatV2Bounce 1.4s ease-in-out infinite;
}

.osp-chat-v2-typing span:nth-child(1) {
    animation-delay: 0s;
}

.osp-chat-v2-typing span:nth-child(2) {
    animation-delay: 0.2s;
}

.osp-chat-v2-typing span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes ospChatV2Bounce {
    0%, 80%, 100% {
        transform: scale(0.6);
        opacity: 0.5;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

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

.osp-chat-v2-input-container {
    padding: 12px 16px;
    border-top: 1px solid var(--osp-chat-v2-border);
    flex-shrink: 0;
}

.osp-chat-v2-input-row {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
}

.osp-chat-v2-input {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid var(--osp-chat-v2-border);
    border-radius: 20px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s;
}

.osp-chat-v2-input::placeholder {
    color: #9ca3af;
    opacity: 1;
}

.osp-chat-v2-input:focus {
    border-color: var(--osp-primary);
}

.osp-chat-v2-send {
    min-width: 44px;
    min-height: 44px;
    width: 40px;
    height: 40px;
    border: none;
    background: var(--osp-primary);
    color: white;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    flex-shrink: 0;
}

.osp-chat-v2-send:hover {
    background: var(--osp-chat-v2-theme-hover);
}

.osp-chat-v2-send:active {
    transform: scale(0.9);
    transition: transform 100ms ease;
}

.osp-chat-v2-send svg {
    width: 18px;
    height: 18px;
}

/* ========================================
   Suggested Prompts
   ======================================== */

.osp-chat-v2-suggestions {
    padding: 8px 16px 0;
    border-top: 1px solid var(--osp-chat-v2-border);
    flex-shrink: 0;
}

.osp-chat-v2-prompt-templates {
    display: flex;
    flex-direction: column;
    gap: 6px;
    width: 100%;
    margin-bottom: 8px;
}

.osp-chat-v2-prompt-template {
    padding: 8px 14px;
    background: #f0f7ff;
    border: 1px solid #bfdbfe;
    border-radius: 12px;
    font-size: 13px;
    color: var(--osp-primary);
    cursor: pointer;
    transition: all 0.2s ease;
    text-align: left;
    line-height: 1.4;
}

.osp-chat-v2-prompt-template:hover {
    background: #dbeafe;
    border-color: #93c5fd;
    transform: translateX(4px);
}

.osp-chat-v2-prompt-template::before {
    content: "💡 ";
    margin-right: 4px;
}

.osp-chat-v2-prompt-template-primary {
    background: var(--osp-primary);
    color: white;
    border-color: var(--osp-primary);
    text-align: center;
    font-weight: 600;
    font-size: 14px;
}

.osp-chat-v2-prompt-template-primary:hover {
    background: var(--osp-chat-v2-theme-hover);
    border-color: var(--osp-chat-v2-theme-hover);
}

.osp-chat-v2-prompt-template-primary::before {
    content: "👔 ";
}

.osp-chat-v2-prompt-template-secondary {
    background: #fef3c7;
    color: #92400e;
    border-color: #fcd34d;
    text-align: center;
    font-weight: 600;
    font-size: 14px;
}

.osp-chat-v2-prompt-template-secondary:hover {
    background: #fde68a;
    border-color: #fbbf24;
}

.osp-chat-v2-prompt-template-secondary::before {
    content: "🎓 ";
}

/* ========================================
   Scroll to Bottom Button
   ======================================== */

.osp-chat-v2-scroll-bottom {
    position: absolute;
    bottom: 8px;
    right: 8px;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--osp-primary);
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    transition: all 0.2s ease;
    opacity: 0;
    visibility: hidden;
    transform: scale(0.8);
    z-index: 10;
}

.osp-chat-v2-scroll-bottom.visible {
    opacity: 1;
    visibility: visible;
    transform: scale(1);
}

.osp-chat-v2-scroll-bottom:hover {
    background: var(--osp-chat-v2-theme-hover);
    transform: scale(1.1);
}

.osp-chat-v2-scroll-bottom svg {
    width: 18px;
    height: 18px;
}

/* ========================================
   New Messages Badge
   ======================================== */

.osp-chat-v2-new-messages-badge {
    position: absolute;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%) scale(0.8);
    background: var(--osp-primary);
    color: white;
    border: none;
    padding: 6px 16px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    opacity: 0;
    visibility: hidden;
    transition: all 250ms cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 10;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.osp-chat-v2-new-messages-badge.visible {
    opacity: 1;
    visibility: visible;
    transform: translateX(-50%) scale(1);
}

/* ========================================
   Navigation Modal
   ======================================== */

.osp-chat-v2-nav-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: calc(var(--osp-chat-v2-z-index) + 1);
}

.osp-chat-v2-nav-modal-content {
    background: white;
    padding: 24px;
    border-radius: 12px;
    max-width: 400px;
    text-align: center;
    box-shadow: var(--osp-chat-v2-shadow);
}

.osp-chat-v2-nav-modal-content p {
    margin: 0 0 20px;
    font-size: 15px;
    color: var(--osp-chat-v2-text);
}

.osp-chat-v2-nav-actions {
    display: flex;
    gap: 12px;
    justify-content: center;
}

.osp-chat-v2-btn {
    padding: 10px 20px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    border: none;
    transition: all 0.2s;
}

.osp-chat-v2-btn-primary {
    background: var(--osp-primary);
    color: white;
}

.osp-chat-v2-btn-primary:hover {
    background: var(--osp-chat-v2-theme-hover);
}

.osp-chat-v2-btn-secondary {
    background: var(--osp-chat-v2-assistant-bubble);
    color: var(--osp-chat-v2-text);
}

.osp-chat-v2-btn-secondary:hover {
    background: #e5e7eb;
}

/* ========================================
   Toast Notification
   ======================================== */

.osp-chat-v2-toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: rgba(31, 41, 55, 0.9);
    color: white;
    padding: 10px 20px;
    border-radius: 8px;
    font-size: 14px;
    opacity: 0;
    transition: all 300ms ease;
    z-index: 10000;
    pointer-events: none;
}

.osp-chat-v2-toast.visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ========================================
   Retry Indicator
   ======================================== */

.osp-chat-v2-retry-indicator {
    text-align: center;
    padding: 8px;
    color: var(--osp-chat-v2-text-secondary);
    font-size: 12px;
}

.osp-chat-v2-retry-countdown {
    font-weight: 600;
    color: var(--osp-primary);
}

/* ========================================
   Connection Status Dot
   ======================================== */

.osp-chat-v2-status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    display: inline-block;
    margin-left: 4px;
    flex-shrink: 0;
}

.status-connected {
    background: #10b981;
}

.status-reconnecting {
    background: #f59e0b;
    animation: ospChatV2Pulse 1.5s ease-in-out infinite;
}

.status-disconnected {
    background: #ef4444;
}

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

/* ========================================
   Skeleton Loader
   ======================================== */

.osp-chat-v2-skeleton-container {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 16px;
}

.osp-chat-v2-skeleton {
    display: flex;
    gap: 8px;
    align-items: flex-start;
}

.osp-chat-v2-skeleton-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: ospChatV2Shimmer 1.5s infinite;
    flex-shrink: 0;
}

.osp-chat-v2-skeleton-line {
    height: 14px;
    border-radius: 7px;
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: ospChatV2Shimmer 1.5s infinite;
    margin-bottom: 6px;
}

.osp-chat-v2-skeleton-line:last-child {
    margin-bottom: 0;
}

@keyframes ospChatV2Shimmer {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* ========================================
   Accessibility
   ======================================== */

/* Screen reader only */
.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 styles */
.osp-chat-v2-toggle:focus-visible,
.osp-chat-v2-send:focus-visible,
.osp-chat-v2-input:focus-visible,
.osp-chat-v2-header-action:focus-visible,
.osp-chat-v2-close:focus-visible,
.osp-chat-v2-scroll-bottom:focus-visible,
.osp-chat-v2-prompt-template:focus-visible,
.osp-chat-v2-btn:focus-visible {
    outline: 2px solid var(--osp-primary);
    outline-offset: 2px;
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
    .osp-chat-v2-widget *,
    .osp-chat-v2-widget *::before,
    .osp-chat-v2-widget *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* ========================================
   Responsive - Tablet (768px - 1024px)
   ======================================== */

@media (max-width: 1024px) and (min-width: 769px) {
    .osp-chat-v2-panel {
        width: 400px;
        height: 600px;
    }

    .osp-chat-v2-toggle::after {
        right: -20px;
    }
}

/* ========================================
   Responsive - Mobile (<=768px)
   ======================================== */

@media (max-width: 768px) {
    .osp-chat-v2-widget {
        bottom: max(30px, env(safe-area-inset-bottom, 30px));
        right: max(30px, env(safe-area-inset-right, 30px));
        left: auto;
        width: auto;
    }

    .osp-chat-v2-toggle {
        width: 56px;
        height: 56px;
        margin: 0 0 30px auto;
    }

    .osp-chat-v2-toggle::after {
        content: none;
    }

    .osp-chat-v2-toggle::before {
        content: "";
        position: absolute;
        top: -4px;
        right: -4px;
        width: 12px;
        height: 12px;
        background: #ef4444;
        border-radius: 50%;
        border: 2px solid white;
    }

    /* Fullscreen panel trên mobile */
    .osp-chat-v2-panel {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100vw;
        height: 100dvh;
        max-width: 100vw;
        max-height: 100dvh;
        border-radius: 0;
    }

    @supports not (height: 100dvh) {
        .osp-chat-v2-panel {
            height: 100vh;
            max-height: 100vh;
        }
    }

    .osp-chat-v2-header {
        padding: 14px 16px;
    }

    .osp-chat-v2-header-title {
        font-size: 16px;
    }

    .osp-chat-v2-avatar {
        font-size: 18px;
    }

    .osp-chat-v2-messages {
        padding: 16px;
        gap: 12px;
    }

    .osp-chat-v2-message {
        font-size: 14px;
        padding: 10px 14px;
        max-width: 85%;
    }

    .osp-chat-v2-input-container {
        padding: 12px 16px;
        padding-bottom: max(12px, env(safe-area-inset-bottom, 12px));
    }

    .osp-chat-v2-input {
        font-size: 16px; /* iOS: 16px+ prevents zoom on focus */
        padding: 10px 14px;
    }

    .osp-chat-v2-send {
        width: 44px;
        height: 44px;
    }

    .osp-chat-v2-send svg {
        width: 20px;
        height: 20px;
    }

    .osp-chat-v2-scroll-bottom {
        bottom: 12px;
        right: 12px;
        width: 44px;
        height: 44px;
    }

    .osp-chat-v2-scroll-bottom svg {
        width: 18px;
        height: 18px;
    }

    .osp-chat-v2-suggestions {
        padding: 8px 16px 0;
    }

    .osp-chat-v2-prompt-template {
        font-size: 13px;
        padding: 8px 14px;
    }

    .osp-chat-v2-nav-modal-content {
        margin: 16px;
        max-width: calc(100vw - 32px);
    }

    .osp-chat-v2-clear,
    .osp-chat-v2-btn {
        min-width: 44px;
        min-height: 44px;
        padding: 10px 20px;
    }
}

/* ========================================
   Responsive - Very Small Mobile (<=480px)
   ======================================== */

@media (max-width: 480px) {
    .osp-chat-v2-header {
        padding: 12px;
    }

    .osp-chat-v2-header-title {
        font-size: 15px;
    }

    .osp-chat-v2-message {
        font-size: 13px;
        padding: 8px 12px;
    }

    .osp-chat-v2-input {
        font-size: 16px;
    }
}
