/* src/OspVnWebsite.Web/wwwroot/css/chat-widget.css */

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


.osp-chat-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-z-index);
    font-family: inherit;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    transition: bottom 0.5s ease;
}

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

/* Focus Visible Styles for Accessibility */
.osp-chat-toggle:focus-visible,
.osp-chat-send:focus-visible,
.osp-chat-input:focus-visible,
.osp-chat-header-action:focus-visible,
.osp-chat-close:focus-visible,
.osp-chat-scroll-bottom:focus-visible,
.osp-chat-prompt-template:focus-visible,
.osp-chat-btn:focus-visible {
    outline: 2px solid var(--osp-primary);
    outline-offset: 2px;
}

/* Screen reader only utility */
.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;
}

/* Toggle Button */
.osp-chat-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-shadow);
    transition: all 0.3s ease;
    position: relative;
    margin-left: auto;
    display: flex;
    align-items: center;
    justify-content: center;
}

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

.osp-chat-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-z-index) + 1);
}

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

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

.osp-chat-toggle-text {
    display: none;
}

/* Chat Panel */
.osp-chat-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-bg);
    border-radius: 16px;
    box-shadow: var(--osp-chat-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: ospChatPanelSlide var(--anim-slow, 300ms) cubic-bezier(0.4, 0, 0.2, 1);
}

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

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

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

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

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

.osp-chat-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-header-action:hover {
    background: rgba(255, 255, 255, 0.3);
}

.osp-chat-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-close:hover {
    opacity: 1;
}

.osp-chat-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-clear:hover {
    background: rgba(255, 255, 255, 0.3);
}

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

/* Messages */
.osp-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    position: relative;
}

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

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

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

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

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

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

/* Chat Links */
.osp-chat-message a,
.osp-chat-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-message a:hover,
.osp-chat-link:hover {
    color: var(--theme-color-hover);
    text-decoration: underline;
}

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

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

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

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

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

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

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

@keyframes ospChatBounce {

    0%,
    80%,
    100% {
        transform: scale(0.6);
        opacity: 0.5;
    }

    40% {
        transform: scale(1);
        opacity: 1;
    }
}

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

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

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

.osp-chat-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-prompt-template:hover {
    background: #dbeafe;
    border-color: #93c5fd;
    transform: translateX(4px);
}

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

.osp-chat-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-prompt-template-primary:hover {
    background: var(--theme-color-hover);
    border-color: var(--theme-color-hover);
}

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

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

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

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

/* Inline links in chat messages */
.osp-chat-link {
    color: var(--osp-primary);
    text-decoration: underline;
    word-break: break-all;
}

.osp-chat-link:hover {
    opacity: 0.8;
}

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

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

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

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

.osp-chat-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-send:hover {
    background: var(--theme-color-hover);
}

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

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

/* Scroll to Bottom Button */
.osp-chat-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-scroll-bottom.visible {
    opacity: 1;
    visibility: visible;
    transform: scale(1);
}

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

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

.osp-chat-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-new-messages-badge.visible {
    opacity: 1;
    visibility: visible;
    transform: translateX(-50%) scale(1);
}

/* Navigation Modal */
.osp-chat-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-z-index) + 1);
}

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

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

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

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

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

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

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

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

/* Responsive - Tablet (768px - 1024px) */
@media (max-width: 1024px) and (min-width: 769px) {
    .osp-chat-panel {
        width: 400px;
        height: 600px;
    }

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

/* Responsive - Mobile (<=768px) */
@media (max-width: 768px) {
    .osp-chat-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-toggle {
        width: 56px;
        height: 56px;
        margin: 0 0 30px auto; /* margin-right: 0 vì container đã có right: 30px */
    }

    .osp-chat-toggle::after {
        content: none; /* Hide tooltip on mobile to prevent overlap */
    }

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

    /* Fullscreen chat panel trên mobile */
    .osp-chat-panel {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100vw;
        height: 100dvh; /* Dynamic viewport height - tự adjust khi có keyboard */
        max-width: 100vw;
        max-height: 100dvh;
        border-radius: 0;
    }

    /* Fallback cho browser chưa support dvh */
    @supports not (height: 100dvh) {
        .osp-chat-panel {
            height: 100vh;
            max-height: 100vh;
        }
    }

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

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

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

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

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

    .osp-chat-input-container {
        padding: 12px 16px;
        /* Quan trọng: đảm bảo input không bị keyboard che */
        padding-bottom: max(12px, env(safe-area-inset-bottom, 12px));
    }

    .osp-chat-input {
        font-size: 16px; /* 16px trở lên để iOS không zoom khi focus */
        padding: 10px 14px;
    }

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

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

    /* Scroll button inside messages container */
    .osp-chat-scroll-bottom {
        bottom: 12px;
        right: 12px;
        width: 44px;
        height: 44px;
    }

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

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

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

    /* Navigation modal trên mobile */
    .osp-chat-nav-modal-content {
        margin: 16px;
        max-width: calc(100vw - 32px);
    }

    /* Ensure all interactive elements meet 44x44px touch target */
    .osp-chat-clear {
        min-width: 44px;
        min-height: 44px;
    }

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

/* Responsive - Very Small Mobile (<=480px) */
@media (max-width: 480px) {
    .osp-chat-header {
        padding: 12px;
    }

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

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

    .osp-chat-input {
        font-size: 16px; /* Luôn 16px để tránh iOS zoom */
    }
}

/* Toast notification for copy feedback */
.osp-chat-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-toast.visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

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

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

/* Connection Status Dot */
.osp-chat-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: ospChatPulse 1.5s ease-in-out infinite;
}

.status-disconnected {
    background: #ef4444;
}

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

/* Skeleton Loader */
.osp-chat-skeleton-container {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 16px;
}

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

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

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

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

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

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