/**
 * ArobitEditor v1.0 — Standalone Rich Text Editor
 * Portable: works in any project (Laravel, Next.js, plain HTML)
 * Dependencies: Bootstrap 5 (for toolbar UI), Bootstrap Icons
 * Author: ArobitAI Platform
 */

/* ── Container ─────────────────────────────────────────────────── */
.arobit-editor-wrap {
    border: 1px solid #dee2e6;
    border-radius: 0.375rem;
    background: #fff;
    display: flex;
    flex-direction: column;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    position: relative;
}

.arobit-editor-wrap:focus-within {
    border-color: #86b7fe;
    box-shadow: 0 0 0 0.25rem rgba(13,110,253,.25);
}

.arobit-editor-wrap.ae-dark {
    background: #1e1e1e;
    border-color: #444;
    color: #d4d4d4;
}

/* ── Toolbar ────────────────────────────────────────────────────── */
.ae-toolbar {
    border-bottom: 1px solid #dee2e6;
    padding: 4px 6px;
    background: #f8f9fa;
    border-radius: 0.375rem 0.375rem 0 0;
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    align-items: center;
    user-select: none;
}

.ae-dark .ae-toolbar {
    background: #2d2d2d;
    border-color: #444;
}

.ae-toolbar-group {
    display: flex;
    align-items: center;
    gap: 1px;
}

.ae-toolbar-sep {
    width: 1px;
    height: 22px;
    background: #dee2e6;
    margin: 0 3px;
}

.ae-dark .ae-toolbar-sep { background: #555; }

/* Toolbar buttons */
.ae-btn {
    border: none;
    background: transparent;
    border-radius: 3px;
    padding: 3px 6px;
    cursor: pointer;
    color: #495057;
    font-size: 13px;
    line-height: 1.4;
    white-space: nowrap;
    display: inline-flex;
    align-items: center;
    gap: 3px;
    transition: background 0.1s;
    min-width: 28px;
    justify-content: center;
}

.ae-btn:hover { background: #e9ecef; }
.ae-btn.active { background: #d0e4ff; color: #0d6efd; }
.ae-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.ae-dark .ae-btn { color: #ccc; }
.ae-dark .ae-btn:hover { background: #3a3a3a; }
.ae-dark .ae-btn.active { background: #1a3a5c; color: #74b5ff; }

/* Dropdowns */
.ae-dropdown {
    position: relative;
}

.ae-dropdown-menu {
    position: absolute;
    top: calc(100% + 2px);
    left: 0;
    z-index: 1055;
    background: #fff;
    border: 1px solid #dee2e6;
    border-radius: 4px;
    box-shadow: 0 4px 12px rgba(0,0,0,.15);
    min-width: 140px;
    padding: 4px 0;
    display: none;
}

.ae-dropdown-menu.show { display: block; }

.ae-dark .ae-dropdown-menu {
    background: #2d2d2d;
    border-color: #555;
    color: #ddd;
}

.ae-dropdown-item {
    display: block;
    width: 100%;
    padding: 5px 12px;
    background: none;
    border: none;
    text-align: left;
    cursor: pointer;
    font-size: 13px;
    color: #212529;
    white-space: nowrap;
}
.ae-dropdown-item:hover { background: #f0f4ff; color: #0d6efd; }
.ae-dropdown-item.active { background: #d0e4ff; color: #0d6efd; font-weight: 600; }

.ae-dark .ae-dropdown-item { color: #ddd; }
.ae-dark .ae-dropdown-item:hover { background: #3a3a3a; color: #74b5ff; }

/* Heading dropdown items */
.ae-heading-h1 { font-size: 18px; font-weight: 700; }
.ae-heading-h2 { font-size: 16px; font-weight: 700; }
.ae-heading-h3 { font-size: 14px; font-weight: 700; }
.ae-heading-h4 { font-size: 13px; font-weight: 700; }
.ae-heading-h5 { font-size: 12px; font-weight: 700; }
.ae-heading-h6 { font-size: 11px; font-weight: 700; }

/* Color pickers */
.ae-color-grid {
    display: grid;
    grid-template-columns: repeat(8, 20px);
    gap: 2px;
    padding: 8px;
    min-width: 190px;
}

.ae-color-swatch {
    width: 20px;
    height: 20px;
    border-radius: 3px;
    cursor: pointer;
    border: 1px solid rgba(0,0,0,.1);
    transition: transform 0.1s;
}
.ae-color-swatch:hover { transform: scale(1.2); border-color: #333; }

.ae-color-custom {
    grid-column: span 8;
    margin-top: 4px;
    width: 100%;
    padding: 2px 4px;
    font-size: 12px;
    border: 1px solid #dee2e6;
    border-radius: 3px;
}

/* Table picker */
.ae-table-grid {
    padding: 8px;
    display: grid;
    grid-template-columns: repeat(10, 18px);
    gap: 2px;
    min-width: 210px;
}
.ae-table-cell {
    width: 18px;
    height: 18px;
    border: 1px solid #dee2e6;
    border-radius: 2px;
    cursor: pointer;
    background: #fff;
}
.ae-table-cell.highlighted { background: #cfe2ff; border-color: #0d6efd; }
.ae-table-label {
    grid-column: span 10;
    font-size: 11px;
    color: #6c757d;
    text-align: center;
    margin-top: 4px;
}

/* ── Editing Area ───────────────────────────────────────────────── */
.ae-body {
    flex: 1;
    overflow-y: auto;
    position: relative;
}

.ae-content {
    min-height: 600px; /* overridden by inline style from ArobitEditor.create({height}) */
    padding: 16px 20px;
    outline: none;
    line-height: 1.7;
    color: #212529;
    word-break: break-word;
    white-space: pre-wrap;
    white-space: normal;
}

.ae-dark .ae-content { color: #d4d4d4; background: #1e1e1e; }

/* Editor content typography */
.ae-content h1 { font-size: 2em; font-weight: 700; margin: .67em 0; }
.ae-content h2 { font-size: 1.5em; font-weight: 700; margin: .75em 0; }
.ae-content h3 { font-size: 1.17em; font-weight: 700; margin: .83em 0; }
.ae-content h4 { font-size: 1em; font-weight: 700; margin: 1.12em 0; }
.ae-content h5 { font-size: .83em; font-weight: 700; margin: 1.5em 0; }
.ae-content h6 { font-size: .75em; font-weight: 700; margin: 1.67em 0; }
.ae-content p { margin: 0 0 .75em; }
.ae-content blockquote {
    border-left: 4px solid #0d6efd;
    margin: 1em 0;
    padding: .5em 1em;
    color: #6c757d;
    background: #f8f9fa;
}
.ae-dark .ae-content blockquote { background: #2a2a2a; color: #aaa; }
.ae-content pre {
    background: #f4f4f4;
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 12px 14px;
    overflow-x: auto;
    font-family: 'Fira Code', 'Consolas', monospace;
    font-size: 13px;
    margin: 1em 0;
}
.ae-dark .ae-content pre { background: #1a1a1a; border-color: #444; color: #e0e0e0; }
.ae-content code {
    background: #f0f0f0;
    border-radius: 3px;
    padding: 1px 5px;
    font-family: 'Fira Code', 'Consolas', monospace;
    font-size: .875em;
}
.ae-dark .ae-content code { background: #2a2a2a; color: #e0e0e0; }
.ae-content table {
    border-collapse: collapse;
    width: 100%;
    margin: 1em 0;
}
.ae-content table th,
.ae-content table td {
    border: 1px solid #dee2e6;
    padding: 6px 10px;
    vertical-align: top;
}
.ae-content table th { background: #f8f9fa; font-weight: 600; }
.ae-dark .ae-content table th { background: #2d2d2d; }
.ae-dark .ae-content table td { border-color: #444; }
.ae-content a { color: #0d6efd; text-decoration: underline; }
.ae-content hr { border: none; border-top: 2px solid #dee2e6; margin: 1.5em 0; }
.ae-content ul, .ae-content ol { padding-left: 2em; margin: .75em 0; }
.ae-content li { margin-bottom: .25em; }
.ae-content img { max-width: 100%; height: auto; display: block; margin: .5em auto; }

/* Placeholder */
.ae-content:empty:before {
    content: attr(data-placeholder);
    color: #adb5bd;
    pointer-events: none;
}

/* ── Source Panel ───────────────────────────────────────────────── */
.ae-source-wrap {
    display: none;
    flex-direction: row;
    flex: 1;
}
.ae-source-wrap.show { display: flex; }

.ae-source-editor {
    flex: 1;
    border: none;
    border-left: 1px solid #dee2e6;
    padding: 14px;
    font-family: 'Fira Code', 'Consolas', monospace;
    font-size: 13px;
    resize: none;
    outline: none;
    background: #fafafa;
    color: #212529;
    line-height: 1.6;
}
.ae-dark .ae-source-editor { background: #1a1a1a; color: #e0e0e0; border-color: #444; }

/* ── Status Bar ─────────────────────────────────────────────────── */
.ae-statusbar {
    border-top: 1px solid #dee2e6;
    padding: 3px 12px;
    font-size: 11px;
    color: #6c757d;
    background: #f8f9fa;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-radius: 0 0 0.375rem 0.375rem;
    gap: 12px;
    flex-wrap: wrap;
}
.ae-dark .ae-statusbar { background: #2a2a2a; border-color: #444; color: #888; }

.ae-statusbar-left { display: flex; gap: 12px; align-items: center; }
.ae-statusbar-right { display: flex; gap: 8px; align-items: center; }

.ae-paste-select {
    font-size: 11px;
    border: 1px solid #dee2e6;
    border-radius: 3px;
    padding: 1px 4px;
    background: #fff;
    color: #495057;
    cursor: pointer;
}
.ae-dark .ae-paste-select { background: #2d2d2d; border-color: #555; color: #ccc; }

/* ── Find & Replace Panel ───────────────────────────────────────── */
.ae-find-panel {
    display: none;
    border-bottom: 1px solid #dee2e6;
    padding: 8px 12px;
    background: #fff8e1;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
}
.ae-find-panel.show { display: flex; }
.ae-dark .ae-find-panel { background: #2a2500; border-color: #554; }

.ae-find-panel input {
    font-size: 13px;
    border: 1px solid #dee2e6;
    border-radius: 3px;
    padding: 3px 8px;
    outline: none;
    width: 180px;
}
.ae-find-panel input:focus { border-color: #86b7fe; }

.ae-find-count { font-size: 12px; color: #6c757d; min-width: 50px; }

.ae-find-panel label {
    font-size: 12px;
    color: #495057;
    display: flex;
    align-items: center;
    gap: 4px;
    cursor: pointer;
}

/* Highlight finds */
.ae-highlight { background: #fff176; border-radius: 2px; }
.ae-highlight.ae-current { background: #ffab40; }

/* ── Link Modal ─────────────────────────────────────────────────── */
.ae-modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.4);
    z-index: 2000;
    align-items: center;
    justify-content: center;
}
.ae-modal-overlay.show { display: flex; }

.ae-modal {
    background: #fff;
    border-radius: 6px;
    padding: 20px 24px;
    width: 420px;
    max-width: 95vw;
    box-shadow: 0 8px 32px rgba(0,0,0,.2);
}
.ae-dark .ae-modal { background: #2d2d2d; color: #ddd; }

.ae-modal h6 {
    font-weight: 600;
    margin: 0 0 14px;
    font-size: 15px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.ae-modal label { font-size: 12px; color: #6c757d; margin-bottom: 3px; display: block; }
.ae-modal input, .ae-modal select {
    width: 100%;
    padding: 6px 10px;
    border: 1px solid #dee2e6;
    border-radius: 4px;
    font-size: 13px;
    margin-bottom: 10px;
    outline: none;
    background: #fff;
    color: #212529;
}
.ae-dark .ae-modal input, .ae-dark .ae-modal select {
    background: #1e1e1e; border-color: #555; color: #ddd;
}
.ae-modal input:focus, .ae-modal select:focus { border-color: #86b7fe; }

.ae-modal-footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

/* ── Emoji Picker ───────────────────────────────────────────────── */
.ae-emoji-grid {
    display: grid;
    grid-template-columns: repeat(8, 30px);
    gap: 2px;
    padding: 8px;
    max-height: 200px;
    overflow-y: auto;
    min-width: 270px;
}
.ae-emoji-btn {
    width: 30px;
    height: 30px;
    border: none;
    background: none;
    font-size: 18px;
    cursor: pointer;
    border-radius: 3px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.ae-emoji-btn:hover { background: #f0f4ff; }

/* ── Fullscreen ─────────────────────────────────────────────────── */
.arobit-editor-wrap.ae-fullscreen {
    position: fixed;
    inset: 0;
    z-index: 1999;
    border-radius: 0;
    border: none;
    display: flex;
    flex-direction: column;
    /* height/overflow fully controlled by fixed inset:0 */
}

/* ae-body: flex child that fills all remaining space and scrolls */
.arobit-editor-wrap.ae-fullscreen .ae-body {
    flex: 1 1 0;
    min-height: 0;          /* critical — allows shrinking below content height */
    overflow-y: scroll;     /* always show scrollbar so layout doesn't jump */
    display: flex;
    flex-direction: column;
}

/* ae-content: grows with text, never constrained — parent ae-body scrolls it */
.arobit-editor-wrap.ae-fullscreen .ae-content {
    flex: 1 0 auto;         /* grow but never shrink below natural size */
    min-height: 100%;
}

/* Source view in fullscreen */
.arobit-editor-wrap.ae-fullscreen .ae-source-wrap.show {
    flex: 1 0 auto;
    display: flex;
    flex-direction: column;
    min-height: 100%;
}
.arobit-editor-wrap.ae-fullscreen .ae-source-editor {
    flex: 1 1 auto;
    min-height: 100%;
    resize: none;
}

/* ── Image selected state ────────────────────────────────────────── */
.ae-content img {
    cursor: pointer;
    transition: outline 0.1s;
}
.ae-content img.ae-img-selected {
    outline: 2px solid #0d6efd;
    outline-offset: 2px;
}

/* ── Image toolbar (floating) ────────────────────────────────────── */
.ae-img-toolbar {
    position: fixed;
    z-index: 2100;
    background: #fff;
    border: 1px solid #dee2e6;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0,0,0,.18);
    display: flex;
    gap: 2px;
    padding: 3px 4px;
}
.ae-img-toolbar button {
    border: none;
    background: transparent;
    cursor: pointer;
    border-radius: 3px;
    padding: 3px 7px;
    font-size: 12px;
    color: #495057;
    white-space: nowrap;
}
.ae-img-toolbar button:hover { background: #f0f4ff; color: #0d6efd; }
.ae-img-toolbar button.ae-img-remove { color: #dc3545; }
.ae-img-toolbar button.ae-img-remove:hover { background: #fff0f0; }

/* ── Inline confirm popover (replaces browser confirm dialog) ─────── */
.ae-confirm-popover {
    position: fixed;
    z-index: 2200;
    background: #fff;
    border: 1px solid #dee2e6;
    border-radius: 6px;
    box-shadow: 0 4px 16px rgba(0,0,0,.18);
    padding: 12px 14px;
    min-width: 200px;
    font-size: 13px;
    color: #212529;
}
.ae-confirm-popover p {
    margin: 0 0 10px 0;
    font-weight: 500;
}
.ae-confirm-popover .ae-confirm-btns {
    display: flex;
    gap: 6px;
    justify-content: flex-end;
}
.ae-confirm-popover .ae-confirm-yes {
    background: #dc3545;
    color: #fff;
    border: none;
    border-radius: 4px;
    padding: 4px 12px;
    font-size: 12px;
    cursor: pointer;
}
.ae-confirm-popover .ae-confirm-yes:hover { background: #c82333; }
.ae-confirm-popover .ae-confirm-no {
    background: #f8f9fa;
    color: #495057;
    border: 1px solid #dee2e6;
    border-radius: 4px;
    padding: 4px 12px;
    font-size: 12px;
    cursor: pointer;
}
.ae-confirm-popover .ae-confirm-no:hover { background: #e9ecef; }

/* ── Responsive ─────────────────────────────────────────────────── */
@media (max-width: 768px) {
    .ae-toolbar { gap: 1px; padding: 3px 4px; }
    .ae-btn { padding: 2px 5px; min-width: 24px; font-size: 12px; }
    .ae-content { padding: 12px; }
}
