/**
 * Inline Editor Styles
 * CSS cho hệ thống chỉnh sửa inline
 */

/* Edit Mode Toolbar */
.edit-mode-toolbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    z-index: 9999;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
    display: none;
}

/* Page Loader Overlay (blocks UI until inline content finishes loading) */
.inline-page-loader {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: #ffffff;
    z-index: 100000;
    display: none;
    align-items: center;
    justify-content: center;
}
.inline-page-loader .loader-box { text-align: center; }
.inline-page-loader .spinner {
    width: 44px;
    height: 44px;
    border: 4px solid #e9ecef;
    border-top-color: #667eea;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin: 0 auto 12px;
}
.inline-page-loader .text { color: #6c757d; font-weight: 500; }

.edit-mode-toolbar.active {
    display: block;
}

.edit-mode-toolbar .form-check-input:checked {
    background-color: #28a745;
    border-color: #28a745;
}

.edit-mode-toolbar .form-check-label {
    color: white;
    cursor: pointer;
    user-select: none;
}

/* Add top padding to body when toolbar is active */
body:has(.edit-mode-toolbar.active) {
    padding-top: var(--edit-toolbar-height, 60px);
}

/* Editable Elements */
[inline-text].inline-editable {
    position: relative;
    cursor: pointer;
    transition: all 0.3s ease;
    outline: 2px dashed transparent;
    outline-offset: 4px;
    border-radius: 4px;
    z-index: 1 !important;
}

[inline-text].inline-editable:hover {
    outline-color: #667eea;
    background-color: rgba(102, 126, 234, 0.05);
}

/* Vô hiệu hóa các thẻ a và button bên trong khi ở chế độ edit */
[inline-text].inline-editable a,
[inline-text].inline-editable button {
    pointer-events: none;
    cursor: pointer !important;
    opacity: 0.8;
}

/* Không áp dụng cho các button trong editor toolbar */
[inline-text].inline-editable .inline-editor-toolbar a,
[inline-text].inline-editable .inline-editor-toolbar button,
[inline-text].inline-editing .inline-editor-toolbar a,
[inline-text].inline-editing .inline-editor-toolbar button {
    pointer-events: auto;
    opacity: 1;
}

/* Allow CKEditor UI inside editable blocks */
[inline-text].inline-editable .ck,
[inline-text].inline-editable .ck a,
[inline-text].inline-editable .ck button,
[inline-text].inline-editing .ck,
[inline-text].inline-editing .ck a,
[inline-text].inline-editing .ck button,
[inline-text].inline-editable .ck-toolbar,
[inline-text].inline-editing .ck-toolbar {
    pointer-events: auto !important;
    opacity: 1 !important;
    cursor: pointer !important;
}

/* Loading state cho inline-text elements */
[inline-text].inline-loading {
    position: relative;
    min-height: 1.5em;
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: loading-shimmer 1.5s infinite;
    border-radius: 4px;
    opacity: 0.8;
    pointer-events: none;
}

[inline-text].inline-loading::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 20px;
    height: 20px;
    border: 3px solid #f3f3f3;
    border-top: 3px solid #667eea;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

[inline-text].inline-loading::after {
    content: 'Đang tải...';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, calc(-50% + 30px));
    font-size: 0.85em;
    color: #999;
    white-space: nowrap;
}

/* Spinner animation */
@keyframes spin {
    0% { transform: translate(-50%, -50%) rotate(0deg); }
    100% { transform: translate(-50%, -50%) rotate(360deg); }
}

/* Shimmer loading effect */
@keyframes loading-shimmer {
    0% {
        background-position: 200% 0;
    }
    100% {
        background-position: -200% 0;
    }
}

/* Loading state cho image elements */
[inline-text].inline-loading img {
    opacity: 0.3;
    filter: blur(2px);
}

/* Inline Modal (lightweight, no Bootstrap JS dependency) */
.inline-modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100001;
}

.inline-modal {
    background: #fff;
    width: 520px;
    max-width: calc(100vw - 32px);
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.2);
    overflow: hidden;
}

.inline-modal-header, .inline-modal-footer {
    padding: 12px 16px;
    border-bottom: 1px solid #e9ecef;
}
.inline-modal-header { display: flex; align-items: center; justify-content: space-between; }
.inline-modal-title { margin: 0; font-size: 16px; }
.inline-modal-body { padding: 16px; }
.inline-modal-footer { border-top: 1px solid #e9ecef; border-bottom: 0; display: flex; gap: 8px; justify-content: flex-end; }

.inline-modal .btn-close {
    background: transparent;
    border: 0;
    width: 32px; height: 32px;
    border-radius: 4px;
    position: relative;
}
.inline-modal .btn-close::before, .inline-modal .btn-close::after {
    content: '';
    position: absolute; left: 8px; right: 8px; top: 15px; height: 2px; background: #6c757d;
}
.inline-modal .btn-close::before { transform: rotate(45deg); }
.inline-modal .btn-close::after { transform: rotate(-45deg); }

.inline-modal .form-control { width: 100%; }

/* Loading state cho empty elements */
[inline-text].inline-loading:empty {
    min-height: 40px;
    display: block;
}

[inline-text].inline-editable::before {
    content: '✎';
    position: absolute;
    top: -10px;
    right: -10px;
    background: #667eea;
    color: white;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
    z-index: 10;
}

[inline-text].inline-editable:hover::before {
    opacity: 1;
}

/* Editing State */
[inline-text].inline-editing {
    outline: 2px solid #28a745;
    background-color: rgba(40, 167, 69, 0.05);
}

[inline-text].inline-editing::before {
    content: '⚡';
    background: #28a745;
    opacity: 1;
}

/* Editor Wrapper */
.inline-editor-wrapper {
    position: relative;
    padding: 10px;
    background: white;
    border: 2px solid #28a745;
    border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.inline-editor-container {
    min-height: 100px;
    margin-bottom: 10px;
}

/* Image Editor */
.inline-image-editor {
    min-height: auto;
}

.inline-image-editor .image-preview {
    text-align: center;
    margin-bottom: 15px;
    padding: 10px;
    background: #f8f9fa;
    border-radius: 4px;
}

.inline-image-editor .image-preview img {
    max-width: 100%;
    max-height: 400px;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.inline-image-editor .image-upload-controls {
    padding: 10px;
}

/* Visually hidden input - KHÔNG dùng display:none vì browser security chặn .click() */
.inline-image-editor .image-upload-controls input[type="file"],
.visually-hidden-input {
    position: absolute !important;
    left: -9999px !important;
    width: 1px !important;
    height: 1px !important;
    opacity: 0 !important;
    /* KHÔNG dùng display:none, visibility:hidden, hoặc pointer-events:none */
}

/* Label button cho file upload - Native label với for="" */
.inline-image-editor .image-upload-controls label[for] {
    cursor: pointer;
    display: inline-block;
    margin-bottom: 5px;
    transition: all 0.2s ease;
    user-select: none;
}

.inline-image-editor .image-upload-controls label[for]:hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.inline-image-editor .image-upload-controls label[for]:active {
    transform: translateY(0);
}

.inline-image-editor .image-upload-controls .file-name {
    margin-top: 5px;
    font-size: 12px;
    color: #666;
    font-style: italic;
    min-height: 16px;
}

/* CKEditor Customization */
.inline-editor-container .ck-editor {
    border: 1px solid #ddd;
    border-radius: 4px;
}

.inline-editor-wrapper .ck-editor__editable,
.inline-editor-container .ck-editor__editable {
    min-height: 150px;
    max-height: 500px;
    overflow-y: auto;
    /* Ensure readable text inside editor */
    color: #212529 !important;
    background-color: #ffffff !important;
    caret-color: #212529;
}

/* CKEditor content base color (extra safety) */
.inline-editor-wrapper .ck-content,
.inline-editor-wrapper .ck-editor__editable.ck-content,
.inline-editor-wrapper .ck-content *,
.inline-editor-wrapper .ck-editor__editable *,
.inline-editor-wrapper .ck-blurred.ck-content *,
.inline-editor-wrapper .ck-focused.ck-content *,
.inline-editor-container .ck-content,
.inline-editor-container .ck-editor__editable.ck-content,
.inline-editor-container .ck-content *,
.inline-editor-container .ck-editor__editable *,
.inline-editor-container .ck-blurred.ck-content *,
.inline-editor-container .ck-focused.ck-content * {
    color: #212529 !important;
}
.inline-editor-wrapper .ck-content *[style*="color:"],
.inline-editor-container .ck-content *[style*="color:"] {
    /* Override inline color coming from stored HTML */
    color: #212529 !important;
}
.inline-editor-wrapper .ck-content,
.inline-editor-container .ck-content { background-color: #ffffff !important; }

/* Ensure common text elements are readable inside CKEditor */
.inline-editor-wrapper .ck-content p,
.inline-editor-wrapper .ck-content span,
.inline-editor-wrapper .ck-content li,
.inline-editor-wrapper .ck-content h1,
.inline-editor-wrapper .ck-content h2,
.inline-editor-wrapper .ck-content h3,
.inline-editor-wrapper .ck-content h4,
.inline-editor-wrapper .ck-content h5,
.inline-editor-wrapper .ck-content h6,
.inline-editor-wrapper .ck-content strong,
.inline-editor-wrapper .ck-content em {
    color: #212529 !important;
}

/* Force CKEditor theme variables to dark text on light background */
.inline-editor-wrapper .ck,
.inline-editor-container .ck {
    --ck-color-text: #212529 !important;
    --ck-color-base-text: #212529 !important;
    --ck-color-base-foreground: #212529 !important;
    --ck-color-base-background: #ffffff !important;
    --ck-color-editor-background: #ffffff !important;
}

/* Toolbar dropdowns and lists use dark text */
.inline-editor-wrapper .ck,
.inline-editor-wrapper .ck .ck-list__item,
.inline-editor-wrapper .ck .ck-button,
.inline-editor-wrapper .ck .ck-input,
.inline-editor-wrapper .ck .ck-input-text,
.inline-editor-container .ck,
.inline-editor-container .ck .ck-list__item,
.inline-editor-container .ck .ck-button,
.inline-editor-container .ck .ck-input,
.inline-editor-container .ck .ck-input-text {
    color: #212529;
}

/* Links inside editor should be visible */
.inline-editor-wrapper .ck-content a, .inline-editor-container .ck-content a { color: #0d6efd; }

/* Placeholder text contrast */
.inline-editor-wrapper .ck-placeholder, .inline-editor-container .ck-placeholder { color: #6c757d !important; }

.inline-editor-wrapper .ck-toolbar, .inline-editor-container .ck-toolbar {
    border-top-left-radius: 4px;
    border-top-right-radius: 4px;
}

/* Editor Toolbar */
.inline-editor-toolbar {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    padding-top: 10px;
    border-top: 1px solid #e9ecef;
}

.inline-editor-toolbar .btn {
    padding: 6px 16px;
    font-size: 14px;
    border-radius: 4px;
    font-weight: 500;
    transition: all 0.3s ease;
}

.inline-editor-toolbar .btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.inline-editor-toolbar .btn i {
    margin-right: 4px;
}

/* Loading Indicator */
.inline-editor-loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(255, 255, 255, 0.95);
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 1000;
}

.inline-editor-loading i {
    font-size: 24px;
    color: #667eea;
}

/* Notification */
.inline-editor-notification {
    position: fixed;
    top: 80px;
    right: 20px;
    z-index: 10000;
    min-width: 300px;
    animation: slideInRight 0.3s ease-out;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

@keyframes slideInRight {
    from {
        transform: translateX(100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

/* Save All Button */
#saveAllChanges {
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% {
        box-shadow: 0 0 0 0 rgba(40, 167, 69, 0.7);
    }
    50% {
        box-shadow: 0 0 0 10px rgba(40, 167, 69, 0);
    }
}

/* Responsive */
@media (max-width: 768px) {
    .edit-mode-toolbar .container-fluid {
        padding: 10px;
    }
    
    .edit-mode-toolbar .d-flex {
        flex-direction: column;
        gap: 10px;
    }
    
    .inline-editor-notification {
        right: 10px;
        left: 10px;
        min-width: auto;
    }
    
    [inline-text].inline-editable::before {
        top: -8px;
        right: -8px;
        width: 20px;
        height: 20px;
        font-size: 10px;
    }
}

/* Print Mode - Hide edit indicators */
@media print {
    .edit-mode-toolbar,
    [inline-text].inline-editable::before,
    .inline-editor-toolbar,
    #saveAllChanges {
        display: none !important;
    }
    
    [inline-text].inline-editable {
        outline: none !important;
        background: transparent !important;
    }
}
