/* HTML Preview Container - Professional Document Viewer */

.html-preview-container {
    background: #ffffff;
    border: 2px solid #e3e6f0;
    border-radius: 12px;
    margin: 1rem 0;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    position: relative;
    max-width: 100%;
    width: 100%;
    /* Đảm bảo không tràn ra ngoài chat bubble */
    box-sizing: border-box;
}

.html-preview-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 0.75rem 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-weight: 500;
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.html-preview-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.9rem;
}

.html-preview-title i {
    font-size: 1.1rem;
}

.html-preview-actions {
    display: flex;
    gap: 0.5rem;
}

.html-preview-btn {
    background: rgba(255, 255, 255, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.3);
    color: white;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.2s ease;
}

.html-preview-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    border-color: rgba(255, 255, 255, 0.5);
}

.html-preview-content {
    max-height: 600px;
    overflow-y: auto;
    padding: 1.5rem;
    background: #fafbfc;
    position: relative;
    /* Contains floats */
    contain: paint layout;
}

.html-rendered-content {
    position: relative;
    max-width: 100%;
    overflow-x: auto;
    /* Optional constraints directly on wrapper */
}

/* Document-like styling for HTML content */
.html-preview-content .document-content {
    background: white;
    padding: 2rem;
    margin: 0 auto;
    max-width: 100% !important;
    width: 100% !important;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    font-family: 'Times New Roman', serif;
    line-height: 1.6;
    color: #333;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

/* Override inline styles trong document content */
.html-preview-content .document-content>* {
    max-width: 100% !important;
}

.html-preview-content .document-content div[style*="max-width"] {
    max-width: 100% !important;
}

/* Vietnamese document styling */
.html-preview-content .document-content h1,
.html-preview-content .document-content h2,
.html-preview-content .document-content h3 {
    text-align: center;
    font-weight: bold;
    margin: 1rem 0;
    color: #1a1a1a;
}

.html-preview-content .document-content h1 {
    font-size: 1.4rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 1.5rem;
}

.html-preview-content .document-content h2 {
    font-size: 1.2rem;
    margin: 1.5rem 0 1rem 0;
}

.html-preview-content .document-content h3 {
    font-size: 1.1rem;
    margin: 1rem 0 0.5rem 0;
}

.html-preview-content .document-content p {
    margin: 0.8rem 0;
    text-align: justify;
    text-indent: 1.5rem;
}

.html-preview-content .document-content .signature-section {
    margin-top: 3rem;
    text-align: right;
    font-style: italic;
}

.html-preview-content .document-content .form-field {
    display: inline-block;
    border-bottom: 1px solid #333;
    min-width: 200px;
    margin: 0 0.5rem;
    padding: 0 0.5rem;
}

.html-preview-content .document-content .center {
    text-align: center;
}

/* Code view styling - Textarea with word wrap */
.html-preview-code {
    padding: 1rem;
    background: #f8f9fa;
    border-top: 1px solid #e9ecef;
    max-width: 100% !important;
    box-sizing: border-box !important;
}

.html-preview-code textarea {
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
    border-radius: 6px;
    line-height: 1.5;
    padding: 1rem !important;
    margin: 0 !important;
    resize: vertical !important;
    min-height: 300px !important;
    max-height: 500px !important;
}

.html-preview-code textarea:focus {
    outline: none !important;
    box-shadow: 0 0 0 2px rgba(139, 69, 19, 0.3) !important;
}

/* Responsive improvements */
@media (max-width: 768px) {
    .html-preview-container {
        margin: 0.5rem 0;
        border-radius: 8px;
    }

    .html-preview-header {
        padding: 0.5rem 0.75rem;
        flex-direction: column;
        gap: 0.5rem;
        align-items: stretch;
    }

    .html-preview-actions {
        justify-content: center;
    }

    .html-preview-content {
        padding: 1rem;
        max-height: 300px;
        overflow-y: auto;
        contain: paint layout;
    }

    .html-preview-content .document-content {
        padding: 1rem;
        font-size: 0.9rem;
    }

    .html-preview-code {
        max-height: 250px;
        padding: 0.75rem;
    }

    .html-preview-code textarea {
        padding: 0.75rem !important;
        font-size: 0.8rem !important;
        min-height: 250px !important;
    }
}

/* Chat bubble integration - Force fit */
.chat-bubble-bot .html-preview-container {
    max-width: 100% !important;
    width: 100% !important;
    margin: 1rem 0;
    box-sizing: border-box !important;
}

.chat-bubble-bot .html-preview-content {
    max-width: 100% !important;
    box-sizing: border-box !important;
}

.chat-bubble-bot .html-preview-code {
    max-width: 100% !important;
    box-sizing: border-box !important;
}

.chat-bubble-bot .html-preview-code textarea {
    max-width: 100% !important;
    width: 100% !important;
    box-sizing: border-box !important;
    scrollbar-width: thin !important;
    scrollbar-color: #888 #f1f1f1 !important;
}

/* AGGRESSIVE override - Force all elements to fit */
.html-preview-container,
.html-preview-container *,
.html-preview-container div,
.html-preview-container .document-content,
.html-preview-container .document-content * {
    max-width: 100% !important;
    width: auto !important;
    box-sizing: border-box !important;
    overflow-wrap: break-word !important;
    word-wrap: break-word !important;
}

/* Target specific inline styles that cause overflow */
.html-preview-container div[style*="max-width"],
.html-preview-container div[style*="width"],
.html-preview-container *[style*="max-width"],
.html-preview-container *[style*="width"] {
    max-width: 100% !important;
    width: auto !important;
}

/* Chat message container constraints */
.chat-msg,
.chat-msg *,
.chat-bubble-bot,
.chat-bubble-bot * {
    max-width: 100% !important;
    box-sizing: border-box !important;
}

/* Ensure container fits in chat bubble - NUCLEAR OPTION */
.chat-msg .html-preview-container {
    max-width: 100% !important;
    width: 100% !important;
    contain: layout !important;
}

/* Scrollbar styling for code view */
.html-preview-code::-webkit-scrollbar,
.html-preview-code pre::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

.html-preview-code::-webkit-scrollbar-track,
.html-preview-code pre::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 4px;
}

.html-preview-code::-webkit-scrollbar-thumb,
.html-preview-code pre::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 4px;
}

.html-preview-code::-webkit-scrollbar-thumb:hover,
.html-preview-code pre::-webkit-scrollbar-thumb:hover {
    background: #a8a8a8;
}

.html-preview-content .document-content .right {
    text-align: right;
}

.html-preview-content .document-content .bold {
    font-weight: bold;
}

.html-preview-content .document-content .italic {
    font-style: italic;
}

.html-preview-content .document-content .underline {
    text-decoration: underline;
}

/* Code view styling */
.html-code-view {
    background: #f8f9fa;
    border: 1px solid #e9ecef;
    border-radius: 6px;
    padding: 1rem;
    font-family: 'Courier New', monospace;
    font-size: 0.85rem;
    color: #495057;
    white-space: pre-wrap;
    word-wrap: break-word;
    word-break: break-all;
    max-height: 400px;
    overflow-y: auto;
    overflow-x: auto;
    display: none;
}

.html-code-view.active {
    display: block;
}

/* Toggle buttons */
.preview-mode-toggle {
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid #dee2e6;
    border-radius: 20px;
    padding: 0.25rem;
    display: flex;
    gap: 0.25rem;
    position: absolute;
    top: 1rem;
    right: 1rem;
    z-index: 10;
}

.preview-mode-btn {
    background: transparent;
    border: none;
    padding: 0.4rem 0.8rem;
    border-radius: 16px;
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.2s ease;
    color: #6c757d;
}

.preview-mode-btn.active {
    background: #007bff;
    color: white;
}

.preview-mode-btn:hover:not(.active) {
    background: #e9ecef;
    color: #495057;
}

/* Responsive */
@media (max-width: 768px) {
    .html-preview-content .document-content {
        padding: 1rem;
        margin: 0;
        font-size: 0.9rem;
    }

    .html-preview-header {
        flex-direction: column;
        gap: 0.5rem;
        align-items: flex-start;
        padding: 0.5rem 1rem;
    }

    .preview-mode-toggle {
        position: relative;
        top: auto;
        right: auto;
        margin-bottom: 1rem;
    }

    .html-code-view {
        font-size: 0.75rem;
        padding: 0.75rem;
    }

    .html-preview-actions {
        flex-wrap: wrap;
        gap: 0.25rem;
    }

    .html-preview-btn {
        font-size: 0.75rem;
        padding: 0.25rem 0.5rem;
    }
}

/* Print styles */
@media print {
    .html-preview-container {
        border: none;
        box-shadow: none;
        margin: 0;
    }

    .html-preview-header,
    .html-preview-actions,
    .preview-mode-toggle {
        display: none !important;
    }

    .html-preview-content {
        padding: 0;
        background: white;
        max-height: none;
    }

    .html-preview-content .document-content {
        box-shadow: none;
        margin: 0;
        max-width: none;
    }
}

/* Animation */
.html-preview-container {
    animation: slideInUp 0.3s ease-out;
}

@keyframes slideInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Loading state */
.html-preview-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    color: #6c757d;
}

.html-preview-loading i {
    animation: spin 1s linear infinite;
    margin-right: 0.5rem;
}

@keyframes spin {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(360deg);
    }
}

/* Error state */
.html-preview-error {
    background: #f8d7da;
    border: 1px solid #f5c6cb;
    color: #721c24;
    padding: 1rem;
    border-radius: 6px;
    margin: 1rem;
}