/**
 * Rich Text Editor Styles
 * Mobile-first design with dark theme support
 */

/* ===== EDITOR CONTAINER ===== */
.scrollnote-rich-editor {
    width: 100%;
    min-height: 100%;
    padding: 20px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    font-size: 16px;
    line-height: 1.6;
    color: #333;
    background: #fff;
    outline: none;
    box-sizing: border-box;
    transition: background-color 0.3s ease, color 0.3s ease;
}

.scrollnote-rich-editor:focus {
    outline: none;
}

/* ===== DARK THEME ===== */
.dark-theme .scrollnote-rich-editor {
    background: #1e1e1e;
    color: #e0e0e0;
}

.dark-theme .editor-paragraph {
    color: #e0e0e0;
}

/* Empty paragraph placeholder in dark theme */
.dark-theme .editor-paragraph:empty:before {
    color: #666;
}

/* Beacons in dark theme */
.dark-theme .editor-beacon {
    background: #2a2a2a;
    color: #e0e0e0;
}

.dark-theme .beacon-image .caption {
    color: #999;
}

/* ===== BLOCKS ===== */
.editor-block {
    margin: 0;
    padding: 0;
}

.editor-paragraph {
    margin: 0 0 1em 0;
    min-height: 1.6em;
}

.editor-paragraph:last-child {
    margin-bottom: 0;
}

/* Empty paragraph with only <br> */
.editor-paragraph:empty:before {
    content: attr(data-placeholder);
    color: #999;
    pointer-events: none;
}

/* ===== FORMATTING ===== */
.editor-paragraph span {
    white-space: pre-wrap;
}

/* Interactive Highlights */
.interactive-highlight {
    cursor: pointer;
    transition: background-color 0.2s;
    border-bottom: 2px dotted rgba(100, 200, 255, 0.8);
}

.interactive-highlight:hover {
    background-color: rgba(100, 200, 255, 0.6) !important;
}

.interactive-highlight.has-unread {
    border-bottom-color: #ff4444;
    border-bottom-width: 3px;
}

/* ===== BEACONS ===== */
.editor-beacon {
    margin: 20px 0;
    padding: 10px;
    background: #f5f5f5;
    border-radius: 8px;
    cursor: default;
}

.beacon-image {
    text-align: center;
}

.beacon-image img {
    max-width: 100%;
    height: auto;
    border-radius: 4px;
}

.beacon-image .caption {
    margin-top: 10px;
    font-size: 14px;
    color: #666;
    font-style: italic;
}

.beacon-audio {
    padding: 15px;
}

.beacon-audio audio {
    width: 100%;
    max-width: 500px;
}

.beacon-audio .title {
    margin-top: 10px;
    font-size: 14px;
    color: #666;
}

/* ===== MAIN TOOLBAR ===== */
.editor-toolbar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: #2a2a2a;
    border-top: 1px solid #444;
    padding: 10px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    z-index: 1000;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.3);
}

.toolbar-section {
    display: flex;
    gap: 5px;
}

.toolbar-btn {
    min-width: 44px;
    min-height: 44px;
    padding: 10px;
    border: 1px solid #444;
    background: #333;
    color: #e0e0e0;
    border-radius: 6px;
    font-size: 16px;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.toolbar-btn:hover {
    background: #3a3a3a;
    border-color: #555;
}

.toolbar-btn:active {
    background: #444;
    transform: scale(0.95);
}

/* Active state when formatting is applied to selection */
.toolbar-btn.active {
    background: #3498db;
    border-color: #2980b9;
    color: #fff;
}

.toolbar-btn.active:hover {
    background: #2980b9;
}

.toolbar-btn strong,
.toolbar-btn em,
.toolbar-btn u {
    font-size: 18px;
    pointer-events: none;
}

/* ===== SELECTION TOOLBAR ===== */
.selection-toolbar {
    position: absolute;
    background: #333;
    border-radius: 8px;
    padding: 5px;
    display: flex;
    gap: 3px;
    z-index: 2000;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    transition: opacity 0.2s;
}

.selection-toolbar.hidden {
    display: none;
    opacity: 0;
    pointer-events: none;
}

.toolbar-btn-small {
    min-width: 40px;
    min-height: 40px;
    padding: 8px;
    border: none;
    background: transparent;
    color: #fff;
    border-radius: 4px;
    font-size: 14px;
    cursor: pointer;
    transition: background 0.2s;
}

.toolbar-btn-small:hover {
    background: rgba(255, 255, 255, 0.1);
}

.toolbar-btn-small:active {
    background: rgba(255, 255, 255, 0.2);
}

/* Active state when formatting is applied to selection */
.toolbar-btn-small.active {
    background: rgba(52, 152, 219, 0.3);
    border: 1px solid rgba(52, 152, 219, 0.5);
}

.toolbar-btn-small.active:hover {
    background: rgba(52, 152, 219, 0.4);
}

.toolbar-btn-small strong,
.toolbar-btn-small em,
.toolbar-btn-small u {
    font-size: 16px;
    color: #fff;
    pointer-events: none;
}

/* ===== DESKTOP ADJUSTMENTS ===== */
@media (min-width: 768px) {
    .editor-toolbar {
        position: static;
        border-top: none;
        border-bottom: 1px solid #ddd;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    }

    .scrollnote-rich-editor {
        min-height: 100%;
    }
}

/* ===== MOBILE OPTIMIZATIONS ===== */
@media (max-width: 767px) {
    .toolbar-section {
        gap: 3px;
    }

    .toolbar-btn {
        min-width: 40px;
        min-height: 40px;
        padding: 8px;
        font-size: 14px;
    }

    .toolbar-btn strong,
    .toolbar-btn em,
    .toolbar-btn u {
        font-size: 16px;
    }

    /* Add bottom padding to editor to prevent toolbar overlap */
    .scrollnote-rich-editor {
        padding-bottom: 80px;
    }
}

/* ===== KEYBOARD VISIBLE (iOS) ===== */
@supports (-webkit-touch-callout: none) {
    /* iOS specific: adjust for keyboard */
    .scrollnote-rich-editor {
        padding-bottom: 100px;
    }
}

/* ===== LOADING STATE ===== */
.scrollnote-rich-editor.loading {
    opacity: 0.6;
    pointer-events: none;
}

.scrollnote-rich-editor.loading:after {
    content: 'Loading...';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 18px;
    color: #999;
}

/* ===== UNIFIED COLLABORATION OVERLAY ===== */
.unified-collab-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    z-index: 100;
    overflow: visible;
}

/* ===== REMOTE CURSORS ===== */
.cursor-layer {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    z-index: 100;
}

.remote-cursor {
    position: absolute;
    pointer-events: none;
    z-index: 101;
    transition: left 0.1s ease, top 0.1s ease;
}

.cursor-caret {
    width: 2px;
    height: 20px;
    position: absolute;
    animation: blink 1s infinite;
}

@keyframes blink {
    0%, 49% { opacity: 1; }
    50%, 100% { opacity: 0.3; }
}

.cursor-label {
    position: absolute;
    top: -22px;
    left: 2px;
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 11px;
    color: #fff;
    white-space: nowrap;
    font-weight: 500;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

/* ===== RICH EDITOR WRAPPER ===== */
#rich-editor-wrapper {
    position: relative;
}

/* ===== VIEWPORT OVERLAY (Collaboration) ===== */
#viewport-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    z-index: 50;
    overflow: visible; /* Changed from hidden to allow username labels to show */
}

.viewport-rect {
    position: absolute;
    left: 0;
    right: 0;
    border: 2px solid;
    border-radius: 4px;
    transition: top 0.1s ease, height 0.1s ease;
    pointer-events: none;
}

.viewport-username {
    position: absolute;
    top: -22px;
    left: 4px;
    padding: 2px 8px;
    border-radius: 3px;
    font-size: 11px;
    font-weight: 600;
    white-space: nowrap;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.selection-highlight {
    position: absolute;
    pointer-events: none;
    border-radius: 2px;
    transition: opacity 0.2s ease;
}

/* ===== THEME TOGGLE ===== */
.theme-toggle {
    padding: 6px 12px;
    background: #f0f0f0;
    border: 1px solid #ccc;
    border-radius: 4px;
    cursor: pointer;
    font-size: 18px;
    margin-left: 10px;
    transition: background 0.2s ease;
}

.theme-toggle:hover {
    background: #e0e0e0;
}

/* ===== DEBUG VIEW ===== */
.debug-toggle {
    padding: 6px 12px;
    background: #f0f0f0;
    border: 1px solid #ccc;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    margin-left: 10px;
}

.debug-toggle:hover {
    background: #e0e0e0;
}

#debug-container {
    padding: 20px;
    background: #1e1e1e;
    color: #d4d4d4;
    height: 100%;
    overflow: auto;
}

#debug-container h3 {
    color: #4ec9b0;
    margin-top: 0;
}

#debug-json {
    font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
    font-size: 13px;
    line-height: 1.5;
    white-space: pre;
    overflow-x: auto;
    padding: 15px;
    background: #252526;
    border-radius: 4px;
    border: 1px solid #3e3e3e;
}

/* ===== LAYOUT FIX - INDEPENDENT SCROLLING ===== */
/* Use flex instead of fixed 100vh to work within the app layout */
#main-content {
    display: flex;
    height: 100%;
    overflow: hidden;
}

#sidebar {
    width: 250px;
    min-width: 250px;
    height: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    background: #f5f5f5;
    border-right: 1px solid #ddd;
    padding: 20px;
    box-sizing: border-box;
}

#editor-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

#editor-header {
    flex-shrink: 0;
    height: 60px;
    padding: 10px 20px;
    background: #fff;
    border-bottom: 1px solid #ddd;
    display: flex;
    align-items: center;
    gap: 10px;
}

#rich-editor-wrapper {
    flex: 1;
    overflow-y: auto;
    overflow-x: visible; /* Allow viewport rects to show outside */
    background: #fff;
    position: relative;
}

#toolbar-container {
    flex-shrink: 0;
    background: #2a2a2a;
    border-top: 1px solid #444;
    padding: 10px;
}

/* Mobile responsive */
@media (max-width: 768px) {
    #sidebar {
        position: fixed;
        left: -250px;
        top: 0;
        z-index: 1000;
        transition: left 0.3s ease;
    }

    #sidebar.active {
        left: 0;
    }
}

/* ===== CUSTOM MODAL (Alert/Confirm) ===== */
.custom-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10001;
    animation: fadeIn 0.2s ease;
}

.custom-modal {
    background: #2a2a2a;
    border-radius: 12px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
    max-width: 450px;
    width: 90%;
    animation: slideUp 0.3s ease;
    color: #e0e0e0;
}

.custom-modal-header {
    padding: 24px 24px 16px 24px;
    border-bottom: 1px solid #444;
}

.custom-modal-header h3 {
    margin: 0;
    font-size: 1.4rem;
    color: #e0e0e0;
    display: flex;
    align-items: center;
    gap: 8px;
}

.custom-modal-body {
    padding: 24px;
    line-height: 1.6;
}

.custom-modal-body p {
    margin: 0;
    color: #ccc;
    font-size: 15px;
}

.custom-modal-footer {
    padding: 16px 24px 24px 24px;
    display: flex;
    gap: 12px;
    justify-content: flex-end;
}

.custom-modal-btn {
    height: 44px;
    padding: 0 24px;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    font-size: 15px;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.custom-modal-btn-primary {
    background: #3498db;
    color: white;
}

.custom-modal-btn-primary:hover {
    background: #2980b9;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(52, 152, 219, 0.4);
}

.custom-modal-btn-secondary {
    background: #555;
    color: #e0e0e0;
}

.custom-modal-btn-secondary:hover {
    background: #666;
    transform: translateY(-1px);
}

.custom-modal-btn-danger {
    background: #e74c3c;
    color: white;
}

.custom-modal-btn-danger:hover {
    background: #c0392b;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(231, 76, 60, 0.4);
}

.custom-modal-btn:active {
    transform: translateY(0);
}

.custom-modal-btn:focus {
    outline: 2px solid #3498db;
    outline-offset: 2px;
}

/* ===== COLOR PICKER MODAL ===== */
.color-picker-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: 10000;
    animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.color-picker-content {
    background: #2a2a2a;
    border-radius: 8px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
    max-width: 500px;
    width: 90%;
    max-height: 90vh;
    overflow-y: auto;
    animation: slideUp 0.3s ease;
    color: #e0e0e0;
}

@keyframes slideUp {
    from {
        transform: translateY(20px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.color-picker-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    border-bottom: 1px solid #444;
}

.color-picker-header h3 {
    margin: 0;
    font-size: 1.3rem;
    color: #e0e0e0;
}

.color-picker-close {
    background: none;
    border: none;
    font-size: 2rem;
    cursor: pointer;
    color: #888;
    line-height: 1;
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: all 0.2s;
}

.color-picker-close:hover {
    background: #3a3a3a;
    color: #e0e0e0;
}

.color-picker-body {
    padding: 20px;
}

.color-palette-section,
.custom-color-section,
.remove-color-section {
    margin-bottom: 24px;
}

.color-palette-section:last-child,
.custom-color-section:last-child,
.remove-color-section:last-child {
    margin-bottom: 0;
}

.color-palette-section h4,
.custom-color-section h4 {
    margin: 0 0 12px 0;
    font-size: 0.9rem;
    font-weight: 600;
    color: #999;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Color palette grid */
.color-palette {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    gap: 8px;
}

.color-swatch {
    width: 100%;
    aspect-ratio: 1;
    border: 2px solid #555;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s;
    position: relative;
}

.color-swatch:hover {
    transform: scale(1.1);
    border-color: #888;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.5);
    z-index: 1;
}

.color-swatch:active {
    transform: scale(0.95);
}

/* Highlight palette - larger with text labels */
.highlight-palette {
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
}

.highlight-swatch {
    height: 60px;
    aspect-ratio: unset;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    color: #333;
    border: 2px solid rgba(0, 0, 0, 0.2);
    position: relative;
}

/* Add background layer to show how highlight looks in editor */
.highlight-swatch::before {
    content: '';
    position: absolute;
    inset: 0;
    background: #fff; /* Light background by default */
    z-index: -1;
    border-radius: 4px;
}

/* When editor is in dark mode, show highlight on dark background */
.dark-theme .highlight-swatch::before {
    background: #1e1e1e; /* Match editor dark background */
}

/* Text color adapts to editor theme */
.dark-theme .highlight-swatch {
    color: #e0e0e0;
}

.highlight-swatch .swatch-text {
    text-shadow: 0 1px 2px rgba(255, 255, 255, 0.8);
    position: relative;
    z-index: 1;
}

.dark-theme .highlight-swatch .swatch-text {
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
}

/* Custom color inputs */
.custom-color-inputs {
    display: flex;
    gap: 10px;
    align-items: center;
}

#custom-color-picker {
    width: 60px;
    height: 40px;
    border: 2px solid #555;
    border-radius: 6px;
    cursor: pointer;
    transition: border-color 0.2s;
    background: #333;
}

#custom-color-picker:hover {
    border-color: #888;
}

#custom-color-text {
    flex: 1;
    height: 40px;
    padding: 0 12px;
    border: 2px solid #555;
    border-radius: 6px;
    font-size: 14px;
    font-family: monospace;
    transition: border-color 0.2s;
    background: #333;
    color: #e0e0e0;
}

#custom-color-text:focus {
    outline: none;
    border-color: #3498db;
}

.btn-primary,
.btn-secondary {
    height: 40px;
    padding: 0 20px;
    border: none;
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.btn-primary {
    background: #3498db;
    color: white;
}

.btn-primary:hover {
    background: #2980b9;
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(52, 152, 219, 0.3);
}

.btn-primary:active {
    transform: translateY(0);
}

.btn-secondary {
    background: #e74c3c;
    color: white;
    width: 100%;
}

.btn-secondary:hover {
    background: #c0392b;
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(231, 76, 60, 0.3);
}

.btn-secondary:active {
    transform: translateY(0);
}

/* Mobile responsive */
@media (max-width: 600px) {
    .color-palette {
        grid-template-columns: repeat(6, 1fr);
        gap: 6px;
    }

    .highlight-palette {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }

    .custom-color-inputs {
        flex-wrap: wrap;
    }

    #custom-color-picker {
        width: 50px;
        height: 36px;
    }

    #custom-color-text {
        flex: 1;
        min-width: 150px;
        height: 36px;
    }

    .btn-primary {
        height: 36px;
        padding: 0 16px;
        font-size: 14px;
    }

    .btn-secondary {
        height: 36px;
        font-size: 14px;
    }
}
