/*
 * widget-htmleditor.css
 *
 * Reusable WYSIWYG HTML editor widget styles
 * Based on contenteditable with formatting toolbar
 */

.widget-htmleditor {
    position: relative;
    border: 1px solid #d0d0d0;
    border-radius: 6px;
    background: #f9f9f9;
    overflow: hidden;
    transition: border-color 0.2s ease, background 0.2s ease, box-shadow 0.2s ease;
}

/* Resizable editor */
.widget-htmleditor.widget-htmleditor-resizable {
    overflow: auto;
    display: flex;
    flex-direction: column;
}

.widget-htmleditor.widget-htmleditor-resizable .widget-htmleditor-toolbar {
    flex-shrink: 0;
}

.widget-htmleditor.widget-htmleditor-resizable .widget-htmleditor-statusbar {
    flex-shrink: 0;
}

.widget-htmleditor.widget-htmleditor-resizable .widget-htmleditor-content {
    flex: 1;
    max-height: none;
}

.widget-htmleditor.widget-htmleditor-resizable .widget-htmleditor-source {
    flex: 1;
    max-height: none;
}

.widget-htmleditor:focus-within {
    border-color: #667eea;
    background: white;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

/* Toolbar */
.widget-htmleditor-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    padding: 8px 10px;
    background: #f0f0f0;
    border-bottom: 1px solid #d0d0d0;
}

.widget-htmleditor-toolbar-group {
    display: flex;
    gap: 2px;
    padding-right: 8px;
    border-right: 1px solid #d0d0d0;
}

.widget-htmleditor-toolbar-group:last-child {
    border-right: none;
    padding-right: 0;
}

.widget-htmleditor-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: white;
    border: 1px solid #ccc;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    color: #333;
    transition: all 0.15s ease;
}

.widget-htmleditor-btn:hover {
    background: #e8e8e8;
    border-color: #999;
}

.widget-htmleditor-btn:active {
    background: #ddd;
}

.widget-htmleditor-btn.active {
    background: #667eea;
    border-color: #5a6fd6;
    color: white;
}

.widget-htmleditor-btn svg {
    width: 16px;
    height: 16px;
    fill: currentColor;
}

/* Color picker button */
.widget-htmleditor-color-btn {
    position: relative;
    overflow: visible;
}

.widget-htmleditor-color-indicator {
    position: absolute;
    bottom: 2px;
    left: 4px;
    right: 4px;
    height: 3px;
    background: #333;
    border-radius: 1px;
}

/* Dropdown select for headings */
.widget-htmleditor-select {
    height: 32px;
    padding: 0 8px;
    border: 1px solid #ccc;
    border-radius: 4px;
    background: white;
    font-size: 13px;
    cursor: pointer;
    min-width: 90px;
}

.widget-htmleditor-select:hover {
    border-color: #999;
}

.widget-htmleditor-select:focus {
    outline: none;
    border-color: #667eea;
}

.widget-htmleditor-select[data-command="fontName"] {
    min-width: 120px;
}

.widget-htmleditor-select[data-command="fontSize"] {
    min-width: 60px;
}

/* Editor content area */
.widget-htmleditor-content {
    min-height: 200px;
    max-height: 400px;
    padding: 12px 14px;
    overflow-y: auto;
    font-size: 1rem;
    line-height: 1.5;
    color: #333;
    background: inherit;
}

.widget-htmleditor-content:focus {
    outline: none;
}

/* Placeholder text */
.widget-htmleditor-content:empty::before {
    content: attr(data-placeholder);
    color: #999;
    pointer-events: none;
}

/* Content styling */
.widget-htmleditor-content h1 {
    font-size: 2em;
    margin: 0.67em 0;
}

.widget-htmleditor-content h2 {
    font-size: 1.5em;
    margin: 0.75em 0;
}

.widget-htmleditor-content h3 {
    font-size: 1.17em;
    margin: 0.83em 0;
}

.widget-htmleditor-content p {
    margin: 1em 0;
}

/* List reset - comprehensive to match email rendering */
.widget-htmleditor-content ul,
.widget-htmleditor-content ol {
    margin: 0.4em 0;
    margin-block-start: 0.4em;
    margin-block-end: 0.4em;
    padding-left: 2em;
    padding-inline-start: 2em;
}

/* Nested lists: browser may place them as children of li OR as siblings inside ul/ol */
.widget-htmleditor-content li > ul,
.widget-htmleditor-content li > ol,
.widget-htmleditor-content ul > ul,
.widget-htmleditor-content ul > ol,
.widget-htmleditor-content ol > ul,
.widget-htmleditor-content ol > ol {
    margin: 0;
    margin-block-start: 0;
    margin-block-end: 0;
}

.widget-htmleditor-content li {
    margin: 0;
    margin-block-start: 0;
    margin-block-end: 0;
    padding: 0;
}

/* Chrome wraps contenteditable content in div/p/br inside li - strip their margins */
.widget-htmleditor-content li > p,
.widget-htmleditor-content li > div,
.widget-htmleditor-content li > span {
    margin: 0;
    margin-block-start: 0;
    margin-block-end: 0;
}

.widget-htmleditor-content blockquote {
    margin: 1em 0;
    padding: 10px 20px;
    border-left: 4px solid #667eea;
    background: #f5f5f5;
}

.widget-htmleditor-content a {
    color: #667eea;
    text-decoration: underline;
}

.widget-htmleditor-content hr {
    border: none;
    border-top: 1px solid #ddd;
    margin: 1em 0;
}

/* Embedded images */
.widget-htmleditor-content img {
    max-width: 100%;
    height: auto;
    border-radius: 4px;
    margin: 4px 0;
    cursor: pointer;
    -webkit-user-drag: none;
}

/* Image wrapped in a link - eliminate baseline gap */
.widget-htmleditor-content a > img {
    vertical-align: bottom;
    margin: 0;
}

.widget-htmleditor-content img.widget-htmleditor-img-selected {
    outline: 2px solid #667eea;
    outline-offset: 1px;
}

/* Image resize overlay - absolutely positioned over the image, child of .widget-htmleditor */
.widget-htmleditor-resize-overlay {
    position: absolute;
    pointer-events: none;
    z-index: 10;
    box-sizing: border-box;
    border: 2px solid #667eea;
}

.widget-htmleditor-resize-handle {
    position: absolute;
    width: 10px;
    height: 10px;
    background: #667eea;
    border: 1px solid white;
    border-radius: 2px;
    pointer-events: all;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.widget-htmleditor-resize-handle-nw {
    top: -5px;
    left: -5px;
    cursor: nw-resize;
}

.widget-htmleditor-resize-handle-ne {
    top: -5px;
    right: -5px;
    cursor: ne-resize;
}

.widget-htmleditor-resize-handle-sw {
    bottom: -5px;
    left: -5px;
    cursor: sw-resize;
}

.widget-htmleditor-resize-handle-se {
    bottom: -5px;
    right: -5px;
    cursor: se-resize;
}

/* Status bar */
.widget-htmleditor-statusbar {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding: 4px 10px;
    background: #f0f0f0;
    border-top: 1px solid #d0d0d0;
    min-height: 24px;
}

.widget-htmleditor-statusbar-flash {
    background: #f8d7da;
    transition: background 0.3s ease;
}

.widget-htmleditor-size-indicator {
    font-size: 11px;
    color: #888;
    font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
}

.widget-htmleditor-size-indicator.near-limit {
    color: #b8860b;
    font-weight: 600;
}

.widget-htmleditor-size-indicator.over-limit {
    color: #c0392b;
    font-weight: 700;
}

/* Source code view toggle */
.widget-htmleditor-source {
    display: none;
    width: 100%;
    min-height: 200px;
    max-height: 400px;
    padding: 12px 14px;
    border: none;
    background: #1e1e1e;
    color: #d4d4d4;
    font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
    font-size: 13px;
    line-height: 1.4;
    resize: vertical;
    box-sizing: border-box;
}

.widget-htmleditor-source:focus {
    outline: none;
}

.widget-htmleditor.source-mode .widget-htmleditor-content {
    display: none;
}

.widget-htmleditor.source-mode .widget-htmleditor-source {
    display: block;
}

.widget-htmleditor.source-mode .widget-htmleditor-btn:not([data-command="source"]) {
    opacity: 0.5;
    pointer-events: none;
}

/* Color picker popup */
.widget-htmleditor-color-popup {
    position: absolute;
    top: 100%;
    left: 0;
    margin-top: 4px;
    padding: 8px;
    background: white;
    border: 1px solid #ccc;
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 1000;
    display: none;
}

.widget-htmleditor-color-popup.visible {
    display: block;
}

.widget-htmleditor-color-grid {
    display: grid;
    grid-template-columns: repeat(8, 24px);
    gap: 4px;
}

.widget-htmleditor-color-swatch {
    width: 24px;
    height: 24px;
    border: 1px solid #ddd;
    border-radius: 3px;
    cursor: pointer;
    transition: transform 0.1s ease;
}

.widget-htmleditor-color-swatch:hover {
    transform: scale(1.1);
    border-color: #333;
}

/* Link dialog */
.widget-htmleditor-link-dialog {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 20px;
    background: white;
    border: 1px solid #ccc;
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
    z-index: 2001;
    display: none;
    min-width: 300px;
}

.widget-htmleditor-link-dialog.visible {
    display: block;
}

.widget-htmleditor-link-dialog-title {
    font-size: 1.1rem;
    font-weight: 600;
    margin-bottom: 15px;
    color: #333;
}

.widget-htmleditor-link-dialog-field {
    margin-bottom: 15px;
}

.widget-htmleditor-link-dialog-field label {
    display: block;
    margin-bottom: 5px;
    font-size: 0.9rem;
    color: #666;
}

.widget-htmleditor-link-dialog-field input {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 0.95rem;
    box-sizing: border-box;
}

.widget-htmleditor-link-dialog-field input:focus {
    outline: none;
    border-color: #667eea;
}

.widget-htmleditor-link-dialog-buttons {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
}

.widget-htmleditor-link-dialog-btn {
    padding: 8px 16px;
    border-radius: 4px;
    border: none;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s ease;
}

.widget-htmleditor-link-dialog-btn-primary {
    background: #667eea;
    color: white;
}

.widget-htmleditor-link-dialog-btn-primary:hover {
    background: #5a6fd6;
}

.widget-htmleditor-link-dialog-btn-secondary {
    background: #f0f0f0;
    color: #333;
}

.widget-htmleditor-link-dialog-btn-secondary:hover {
    background: #e0e0e0;
}

/* Dialog backdrop */
.widget-htmleditor-dialog-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
    z-index: 2000;
    display: none;
}

.widget-htmleditor-dialog-backdrop.visible {
    display: block;
}

/* Responsive adjustments */
@media (max-width: 768px) {
    .widget-htmleditor-toolbar {
        padding: 6px 8px;
    }

    .widget-htmleditor-btn {
        width: 28px;
        height: 28px;
    }

    .widget-htmleditor-select {
        height: 28px;
        min-width: 80px;
        font-size: 12px;
    }

    .widget-htmleditor-content {
        min-height: 150px;
        padding: 10px 12px;
    }

    .widget-htmleditor-color-grid {
        grid-template-columns: repeat(6, 24px);
    }
}
