/* Reset & base */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; }

.hidden { display: none !important; }

/* ── Login page ── */
.login-body {
    display: flex; justify-content: center; align-items: center;
    height: 100vh; background: #f5f5f5;
}
.login-container {
    background: #fff; padding: 2.5rem; border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.1); width: 360px;
}
.login-title { text-align: center; font-size: 1.8rem; color: #333; display: flex; align-items: center; justify-content: center; gap: 0.4rem; }
.login-subtitle { text-align: center; color: #888; margin-bottom: 1.5rem; }

.tab-bar { display: flex; gap: 0; margin-bottom: 1rem; }
.tab {
    flex: 1; padding: 0.6rem; border: none; background: #eee;
    cursor: pointer; font-size: 0.95rem; transition: background 0.2s;
}
.tab:first-child { border-radius: 4px 0 0 4px; }
.tab:last-child { border-radius: 0 4px 4px 0; }
.tab.active { background: #4a90d9; color: #fff; }

.flash-error {
    background: #fee; color: #c00; padding: 0.6rem; border-radius: 4px;
    margin-bottom: 1rem; font-size: 0.9rem;
}

.auth-form { display: flex; flex-direction: column; gap: 0.8rem; }
.auth-form input {
    padding: 0.7rem; border: 1px solid #ddd; border-radius: 4px; font-size: 0.95rem;
}
.auth-form button {
    padding: 0.7rem; border: none; border-radius: 4px;
    background: #4a90d9; color: #fff; font-size: 1rem; cursor: pointer;
}
.auth-form button:hover { background: #3a7bc8; }

/* ── App layout ── */
.app-body { height: 100vh; overflow: hidden; }
.app-container { display: flex; height: 100%; }

/* Sidebar */
.sidebar {
    width: 220px; min-width: 160px; max-width: 500px; background: #2c2c2c; color: #ddd;
    display: flex; flex-direction: column;
}
.sidebar-resize-handle {
    width: 5px; cursor: col-resize; background: transparent;
    flex-shrink: 0; transition: background 0.15s;
}
.sidebar-resize-handle:hover, .sidebar-resize-handle.dragging {
    background: #4a90d9;
}
.sidebar-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 1rem; border-bottom: 1px solid #444;
}
.sidebar-title {
    display: flex; align-items: center; gap: 0.3rem;
    font-family: 'Charter', 'Georgia', 'Times New Roman', serif;
    font-weight: 700; font-size: 1.0rem; user-select: none;
}
.logo-icon {
    display: inline-flex; align-items: center; justify-content: center;
    width: 18px; height: 18px; color: #ddd; flex-shrink: 0;
}
.logo-text {
    font-family: 'Charter', 'Georgia', 'Times New Roman', serif;
    font-size: 1.0rem; letter-spacing: -0.01em;
}

/* Sidebar main — sliding viewport */
.sidebar-main {
    position: relative; overflow: hidden; flex: 1;
}
.sidebar-panels-track {
    display: flex; width: 300%; height: 100%;
    transition: transform 0.3s ease;
    transform: translateX(0);
}
.sidebar-panels-track.show-level1 {
    transform: translateX(-33.333%);
}
.sidebar-panels-track.show-level2 {
    transform: translateX(-66.666%);
}
.sidebar-panel {
    width: 33.333%; display: flex; flex-direction: column; overflow: hidden;
}
.panel-scroll {
    flex: 1; overflow-y: auto; padding: 0.5rem 0;
}
.panel-add-btn {
    padding: 0.5rem 0.75rem; font-size: 0.75rem; color: #6a9fd9;
    cursor: pointer; transition: color 0.15s;
    border-top: none;
}
.panel-add-btn:hover { color: #8bb8e8; }
.panel-add-btn-inline { padding: 0.3rem 0.75rem; }

/* Tool items in tools panel (Level 0) */
.tool-item {
    display: flex; align-items: center; gap: 0.5rem;
    padding: 0.55rem 0.75rem; cursor: pointer;
    font-size: 0.85rem; font-weight: 500; color: #ccc;
    border-left: 3px solid transparent;
    transition: background 0.15s;
}
.tool-item:hover { background: #3a3a3a; }
.tool-item.active { background: #3a3a3a; border-left-color: #4a90d9; }
.tool-icon { display: flex; align-items: center; flex-shrink: 0; color: #999; }
.tool-label { flex: 1; }

/* Tool sub-views in Level 1 panel */
.tool-views-panel { position: relative; }
.tool-view {
    display: flex; flex-direction: column;
    position: absolute; inset: 0;
}
.tool-view.hidden { display: none; }
.tool-view-header {
    display: flex; align-items: center; gap: 0.5rem;
    padding: 0.6rem 0.75rem; border-bottom: 1px solid #3a3a3a;
}
.tool-view-title {
    font-weight: 600; font-size: 0.88rem; overflow: hidden;
    text-overflow: ellipsis; white-space: nowrap; flex: 1;
}
.tool-view-placeholder {
    padding: 1rem 0.75rem; font-size: 0.8rem; color: #777;
    font-style: italic;
}

/* Main area placeholder for non-notebook tools */
.tool-main-placeholder {
    flex: 1; display: flex; flex-direction: column;
    justify-content: center; align-items: center;
    color: #aaa; font-size: 1.05rem; gap: 0.6rem;
}

/* Section headers for notebooks list */
.section-header {
    padding: 0.5rem 0.75rem 0.25rem; font-size: 0.65rem;
    text-transform: uppercase; letter-spacing: 0.05em;
    color: #777; font-weight: 600; user-select: none;
}

/* Back button & notebook header in open-notebook panel */
.notebook-panel-header {
    display: flex; align-items: center; gap: 0.5rem;
    padding: 0.6rem 0.75rem; border-bottom: 1px solid #3a3a3a;
}
.back-btn {
    border: none; background: none; color: #bbb; cursor: pointer;
    font-size: 1.1rem; padding: 0.1rem 0.3rem; border-radius: 4px;
    line-height: 1;
}
.back-btn:hover { background: #3a3a3a; color: #fff; }
.notebook-name-header {
    font-weight: 600; font-size: 0.88rem; overflow: hidden;
    text-overflow: ellipsis; white-space: nowrap; flex: 1;
}

/* Gear button for notebook settings */
.gear-btn {
    border: none; background: none; color: #777; cursor: pointer;
    font-size: 1rem; padding: 0.1rem 0.3rem; border-radius: 4px;
    line-height: 1; flex-shrink: 0;
}
.gear-btn:hover { background: #3a3a3a; color: #fff; }

/* Notebook download dropdown */
.notebook-download-dropdown { position: relative; flex-shrink: 0; }
.notebook-download-menu {
    position: absolute; top: 100%; right: 0; z-index: 100;
    background: #2a2a2a; border: 1px solid #444; border-radius: 6px;
    padding: 4px 0; min-width: 160px; box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
.notebook-download-menu button {
    display: block; width: 100%; text-align: left;
    padding: 6px 12px; border: none; background: none;
    color: #ccc; font-size: 0.8rem; cursor: pointer;
}
.notebook-download-menu button:hover { background: #3a3a3a; color: #fff; }

/* Notebook list items */
.notebook-item {
    padding: 0.5rem 0.75rem; cursor: pointer; white-space: nowrap;
    overflow: hidden; font-size: 0.82rem;
    border-left: 3px solid transparent; transition: background 0.15s;
    display: flex; align-items: flex-start; gap: 0.3rem;
    font-weight: 600;
}
.notebook-item:hover { background: #3a3a3a; }
.notebook-item.active { background: #3a3a3a; border-left-color: #4a90d9; }
.notebook-item.dragging { opacity: 0.4; }

.notebook-title {
    flex: 1; overflow: hidden; text-overflow: ellipsis;
}

.notebook-meta-owner {
    font-size: 0.65rem; color: #777; font-weight: 400;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* Page items */
.page-item {
    padding: 0.35rem 0.75rem; cursor: pointer; white-space: nowrap;
    overflow: hidden; font-size: 0.78rem;
    border-left: 3px solid transparent; transition: background 0.15s;
    color: #bbb;
    display: flex; align-items: center; gap: 0.3rem;
}
.page-item-title {
    flex: 1; overflow: hidden; text-overflow: ellipsis;
}
.page-item:hover { background: #3a3a3a; }
.page-item.active { background: #444; border-left-color: #6ab0f3; color: #fff; }
.page-item.dragging { opacity: 0.4; }

/* More button (context menu trigger) */
.more-btn {
    flex-shrink: 0; border: none; background: none; color: #777;
    cursor: pointer; font-size: 0.95rem; padding: 0 0.2rem;
    line-height: 1; opacity: 0; transition: opacity 0.15s;
}
.notebook-item:hover .more-btn,
.page-item:hover .more-btn,
.more-btn.active { opacity: 1; }
.more-btn:hover { color: #fff; }

/* Sidebar footer */
.sidebar-footer {
    padding: 0.5rem 0.75rem; border-top: 1px solid #444;
    display: flex; gap: 0.4rem;
}
.sidebar-icon-btn {
    width: 36px; height: 30px; border: none; border-radius: 6px;
    background: transparent; color: #bbb; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: background 0.15s, color 0.15s;
}
.sidebar-icon-btn:hover { background: #3a3a3a; color: #fff; }
.sidebar-icon-btn.active { background: #3a3a3a; color: #fff; }

/* Context menu */
.context-menu {
    position: fixed; background: #fff; border: 1px solid #ddd;
    border-radius: 4px; box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    z-index: 1000; min-width: 140px;
}
.context-menu button {
    display: block; width: 100%; padding: 0.5rem 1rem; border: none;
    background: none; text-align: left; cursor: pointer; font-size: 0.9rem;
}
.context-menu button:hover { background: #f0f0f0; }
.context-menu-label {
    padding: 0.4rem 1rem 0.35rem; font-size: 0.85rem; color: #888;
    border-bottom: 1px solid #eee; user-select: text; cursor: default;
}

/* Editor area */
.editor-area { flex: 1; display: flex; flex-direction: column; overflow: hidden; position: relative; }

.empty-state {
    flex: 1; display: flex; flex-direction: column;
    justify-content: center; align-items: center;
    color: #999; font-size: 1.1rem; gap: 0.3rem;
}

.editor-panel { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

.editor-header {
    display: flex; align-items: center; gap: 0.5rem;
    padding: 0.5rem 1rem; border-bottom: 1px solid #eee; background: #fafafa;
}
.toolbar-left, .toolbar-right {
    display: flex; align-items: center; gap: 0.2rem; flex-shrink: 0;
}
.doc-title-input {
    flex: 1; min-width: 0; border: none; background: transparent;
    font-size: 1.1rem; font-weight: 600; outline: none; text-align: center;
}
.save-error-indicator {
    font-size: 0.75rem; font-weight: 600; color: #d32f2f;
    white-space: nowrap; cursor: default; padding: 2px 6px;
    border-radius: 3px; background: #ffebee; margin-left: 0.3rem;
}
.save-error-indicator.hidden { display: none; }
.toolbar-btn {
    border: none; background: none; color: #888; cursor: pointer;
    font-size: 0.85rem; width: 28px; height: 28px; border-radius: 4px;
    display: flex; align-items: center; justify-content: center;
    transition: background 0.15s, color 0.15s; flex-shrink: 0;
}
.toolbar-btn:hover { background: #eee; color: #333; }
.toolbar-btn.active { background: #e0e0e0; color: #333; }
.toolbar-label { font-size: 0.7rem; color: #aaa; user-select: none; }
.toolbar-gap { width: 0.5rem; }

/* Scroll wrapper for unified scrolling of line numbers + textarea */
.editor-scroll-wrapper {
    flex: 1;
    overflow-y: auto;
    display: flex;
    align-items: flex-start;
    position: relative;
    min-height: 0;
}

/* Line numbers */
.line-numbers {
    display: none;
    width: 3rem; padding: 1rem 0.5rem 1rem 0;
    text-align: right;
    font-family: 'SFMono-Regular', Consolas, monospace;
    font-size: inherit; line-height: 1.6;
    color: #bbb; background: #fafafa;
    border-right: 1px solid #eee;
    pointer-events: none; user-select: none;
    flex-shrink: 0;
}
.editor-input-pane.show-line-numbers .line-numbers { display: block; }

/* Hidden mirror div for measuring wrapped line heights */
.line-mirror {
    position: absolute;
    visibility: hidden;
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow: hidden;
    pointer-events: none;
}

.editor-split { flex: 1; display: flex; overflow: hidden; }
.editor-input-pane {
    flex: 1 1 50%; position: relative; display: flex; flex-direction: column; overflow: hidden;
}
.editor-input-pane .textarea-wrapper {
    flex: 1; min-height: 100%; min-width: 0;
    position: relative;
}
.editor-input-pane .editor-textarea {
    overflow: hidden; min-width: 0; width: 100%;
}
.editor-textarea {
    border: none;
    padding: 1rem; font-family: 'SFMono-Regular', Consolas, monospace;
    font-size: 0.9rem; resize: none; outline: none; line-height: 1.6;
}
.editor-textarea.read-only {
    background: #f8f8f8; color: #888; cursor: default;
}
.editor-textarea.drop-target {
    outline: 2px dashed #4a90d9;
    outline-offset: -2px;
    background: rgba(74, 144, 217, 0.04);
}

/* ── Syntax highlight backdrop ── */
.highlight-backdrop {
    position: absolute;
    inset: 0;
    overflow: hidden;
    pointer-events: none;
    white-space: pre-wrap;
    word-wrap: break-word;
    margin: 0;
    border: none;
    font-family: 'SFMono-Regular', Consolas, monospace;
    font-size: 0.9rem;
    line-height: 1.6;
    padding: 1rem;
    z-index: 0;
    tab-size: 4;
    color: #333;
}

/* Textarea with syntax highlighting: text invisible, caret visible */
.editor-textarea.syntax-highlight {
    position: relative;
    z-index: 1;
    color: transparent;
    caret-color: #333;
    background: transparent;
}
.editor-textarea.syntax-highlight::placeholder { color: #aaa; }
.editor-textarea.syntax-highlight::selection {
    background: rgba(74, 144, 217, 0.3);
}

/* Read-only: background on backdrop, not textarea */
.editor-textarea.syntax-highlight.read-only {
    background: transparent;
    color: transparent;
    caret-color: transparent;
}
.highlight-backdrop.read-only-bg { background: #f8f8f8; }

/* ── Syntax token colors ── */
.syn-header       { color: #1a5276; font-weight: 700; }
.syn-bold-marker  { color: #888; }
.syn-bold-text    { color: #333; font-weight: 700; }
.syn-italic-marker { color: #888; }
.syn-italic-text  { color: #333; font-style: italic; }
.syn-code-fence   { color: #6a7a3a; }
.syn-code-block   { color: #4a7a2a; }
.syn-inline-code  { color: #4a7a2a; }
.syn-math-marker  { color: #c51b1b; }
.syn-math         { color: #c51b1b; font-weight: 600; }
.syn-link-bracket { color: #2980b9; }
.syn-link-text    { color: #2471a3; }
.syn-link-url     { color: #1abc9c; }
.syn-image-marker { color: #2980b9; }
.syn-blockquote   { color: #888; }
.syn-list-marker  { color: #a0522d; }
.syn-hr           { color: #bbb; }
.syn-plain        { color: #333; }

.split-handle { width: 4px; cursor: col-resize; background: #eee; flex-shrink: 0; }
.split-handle:hover, .split-handle.dragging { background: #ccc; }
.preview-pane {
    flex: 1 1 50%; padding: 1rem; overflow-y: auto; line-height: 1.7; font-size: 0.95rem;
    font-family: 'Charter', 'Georgia', 'Times New Roman', serif;
}
.preview-pane h1, .preview-pane h2, .preview-pane h3 { margin-top: 1rem; margin-bottom: 0.5rem; }
.preview-pane p { margin-bottom: 0.6rem; }
.preview-pane ul, .preview-pane ol { margin-left: 1.5rem; margin-bottom: 0.6rem; }
.preview-pane pre { background: #f5f5f5; padding: 0.8rem; border-radius: 4px; overflow-x: auto; }
.preview-pane code { background: #f0f0f0; padding: 0.15rem 0.3rem; border-radius: 3px; font-size: 0.9em; }
.preview-pane pre code { background: none; padding: 0; }
.preview-pane blockquote { border-left: 3px solid #ddd; padding-left: 1rem; color: #666; }

/* Modal */
.modal-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,0.4);
    display: flex; justify-content: center; align-items: center; z-index: 2000;
}
.modal {
    background: #fff; padding: 2rem; border-radius: 8px;
    width: 380px; box-shadow: 0 4px 20px rgba(0,0,0,0.2);
}
.modal.modal-wide { width: 640px; }
.modal h2 { margin-bottom: 1.2rem; }
.modal h3 { margin: 1rem 0 0.6rem; font-size: 0.95rem; color: #333; }
.modal label { display: block; margin-bottom: 1rem; font-size: 0.9rem; color: #555; }
.modal label input {
    display: block; width: 100%; margin-top: 0.3rem;
    padding: 0.6rem; border: 1px solid #ddd; border-radius: 4px; font-size: 0.95rem;
}
.modal-actions { display: flex; gap: 0.5rem; margin-top: 0.5rem; }
.btn-primary {
    padding: 0.6rem 1.2rem; border: none; border-radius: 4px;
    background: #4a90d9; color: #fff; cursor: pointer;
}
.btn-primary:hover { background: #3a7bc8; }
.btn-primary.btn-small { padding: 0.4rem 0.8rem; font-size: 0.85rem; }
.btn-secondary {
    padding: 0.6rem 1.2rem; border: 1px solid #ddd; border-radius: 4px;
    background: #fff; cursor: pointer;
}
.btn-secondary:hover { background: #f5f5f5; }
.btn-danger {
    padding: 0.6rem 1.2rem; border: none; border-radius: 4px;
    background: #d9534f; color: #fff; cursor: pointer; margin-top: 0.5rem;
}
.btn-danger:hover { background: #c9302c; }
.modal hr { margin: 1rem 0; border: none; border-top: 1px solid #eee; }

/* Notebook settings info */
.nb-settings-info {
    margin: 0.5rem 0 1rem; font-size: 0.85rem; color: #666;
    display: flex; flex-direction: column; gap: 0.25rem;
}
.nb-settings-info .info-label { font-weight: 600; color: #555; }

/* Notebook settings divider */
.nb-settings-divider { margin: 1rem 0 0.5rem; border: none; border-top: 1px solid #ddd; }

/* Collaborators list */
.collaborators-list {
    display: grid;
    grid-template-columns: 1fr auto auto 1.5rem;
    gap: 0.25rem 0.5rem;
    align-items: center;
    margin-bottom: 0.5rem;
    font-size: 0.85rem;
}
.collaborator-row { display: contents; }
.collaborators-header { display: contents; }
.collaborators-header span { font-size: 0.75rem; color: #999; font-weight: 600; padding-bottom: 0.2rem; }
.collab-username { color: #999; font-size: 0.8rem; }
.collab-perm {
    font-size: 0.75rem; padding: 0.15rem 0.4rem; border-radius: 3px;
    background: #e8f0fe; color: #4a90d9;
}
.collab-perm-owner {
    background: #fef3e0; color: #b37700;
}
.btn-remove-collab {
    border: none; background: none; color: #c00; cursor: pointer;
    font-size: 0.85rem; padding: 0.1rem 0.3rem;
}
.btn-remove-collab:hover { color: #900; }

/* Notebook title row (input + buttons below) */
.nb-title-row {
    display: flex; flex-direction: column; gap: 0.4rem; margin-bottom: 1rem;
}
.nb-title-row input {
    width: 100%; padding: 0.6rem; border: 1px solid #ddd;
    border-radius: 4px; font-size: 0.95rem; box-sizing: border-box;
}
.nb-title-actions {
    display: flex; justify-content: space-between;
}
.nb-title-row input[readonly] {
    background: #f5f5f5; color: #666; cursor: default;
}
.btn-secondary.btn-small { padding: 0.4rem 0.8rem; font-size: 0.85rem; }

/* Disabled button styling */
button:disabled { opacity: 0.4; cursor: default; }

/* Invite section (multi-row) */
.invite-section { margin-top: 0.5rem; }
.invite-field-row {
    display: flex; gap: 0.4rem; align-items: center; margin-top: 0.4rem;
}
.invite-label { font-size: 0.85rem; color: #555; white-space: nowrap; }
.invite-field-row input {
    flex: 1; padding: 0.4rem 0.6rem; border: 1px solid #ddd;
    border-radius: 4px; font-size: 0.85rem;
}
.invite-field-row select {
    padding: 0.4rem; border: 1px solid #ddd; border-radius: 4px;
    font-size: 0.85rem; background: #fff;
}

/* Invite feedback messages */
.invite-message {
    font-size: 0.8rem; margin-top: 0.3rem; min-height: 1.2em;
}
.invite-message.error { color: #c00; }
.invite-message.success { color: #2a7; }

/* Settings error message */
.settings-error {
    font-size: 0.85rem; color: #c00; min-height: 1.2em;
    margin-top: 0.3rem;
}

/* ── Chat pane (integrated third pane) ── */
.chats-pane {
    flex: 0 0 300px; min-width: 200px;
    display: flex; flex-direction: column; overflow: hidden;
    background: #fff; border-left: 1px solid #eee;
}
.split-handle-chat {
    width: 4px; cursor: col-resize; background: #eee; flex-shrink: 0;
}
.split-handle-chat:hover, .split-handle-chat.dragging { background: #ccc; }

/* Hide panes via toggle */
.editor-split.hide-edit > .editor-input-pane,
.editor-split.hide-edit > .split-handle:first-of-type { display: none; }
.editor-split.hide-chat > .chats-pane,
.editor-split.hide-chat > .split-handle-chat { display: none; }

/* Tab bar for switching between Dora and Collaborators */
.chats-tab-bar {
    display: flex; border-bottom: 1px solid #eee; flex-shrink: 0;
}
.chats-tab {
    flex: 1; padding: 0.45rem 0.5rem; border: none; background: none;
    font-size: 0.8rem; font-weight: 500; color: #888; cursor: pointer;
    border-bottom: 2px solid transparent; transition: color 0.15s, border-color 0.15s;
}
.chats-tab:hover { color: #555; }
.chats-tab.active { color: #333; border-bottom-color: #4a90d9; }

/* Chat view (fill parent chats-pane) */
.chat-view {
    flex: 1; display: flex; flex-direction: column; overflow: hidden;
    position: relative;
}
.chat-view.hidden { display: none; }

.chat-view-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 0.5rem 0.75rem; border-bottom: 1px solid #eee;
}
.chat-title { font-weight: 600; font-size: 0.9rem; }
.chat-page-name { font-size: 0.78rem; font-weight: 400; color: #999; }
.chat-dismiss {
    border: none; background: none; font-size: 1.3rem;
    cursor: pointer; color: #999; line-height: 1;
}
.chat-dismiss:hover { color: #333; }

.chat-messages {
    flex: 1; overflow-y: auto; padding: 0.5rem 0.75rem;
    display: flex; flex-direction: column; gap: 0.4rem;
}

.chat-bubble {
    max-width: 80%; padding: 0.4rem 0.7rem; border-radius: 12px;
    font-size: 0.85rem; line-height: 1.4; word-wrap: break-word;
    position: relative;
}

/* Per-message context menu */
.chat-msg-menu-btn {
    position: absolute; top: 50%; transform: translateY(-50%); right: -32px;
    border: none; background: none; color: #888;
    cursor: pointer; font-size: 1rem; line-height: 1;
    padding: 4px 6px; border-radius: 50%;
    opacity: 0; transition: opacity 0.15s;
    letter-spacing: -1px;
}
.chat-mine .chat-msg-menu-btn { right: auto; left: -32px; }
.chat-bubble:hover .chat-msg-menu-btn { opacity: 1; }
.chat-msg-menu-btn:hover { background: rgba(0,0,0,0.08); color: #333; }

.chat-msg-menu {
    position: fixed;
    background: #fff; border: 1px solid #ddd; border-radius: 6px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15); z-index: 110;
    min-width: 130px;
}

/* Copy success flash */
@keyframes chat-copied-flash-mine {
    0% { background: #4a90d9; }
    40% { background: #6fb3f2; }
    100% { background: #4a90d9; }
}
@keyframes chat-copied-flash-theirs {
    0% { background: #f0f0f0; }
    40% { background: #fff; }
    100% { background: #f0f0f0; }
}
.chat-mine.chat-copied { animation: chat-copied-flash-mine 0.5s ease; }
.chat-theirs.chat-copied { animation: chat-copied-flash-theirs 0.5s ease; }
.chat-msg-menu button {
    display: flex; align-items: center; gap: 0.4rem;
    width: 100%; padding: 0.4rem 0.7rem; border: none;
    background: none; text-align: left; cursor: pointer;
    font-size: 0.8rem; color: #333; white-space: nowrap;
}
.chat-msg-menu button:hover { background: #f0f0f0; }
.chat-mine {
    background: #4a90d9; color: #fff; align-self: flex-end;
    border-bottom-right-radius: 4px;
}
.chat-theirs {
    background: #f0f0f0; color: #333; align-self: flex-start;
    border-bottom-left-radius: 4px;
}
.chat-sender {
    display: flex; align-items: baseline; gap: 0.4rem;
    font-size: 0.7rem; margin-bottom: 0.1rem;
}
.chat-sender-name { font-weight: 600; }
.chat-thinking-time { font-size: 0.65rem; color: #aaa; font-weight: normal; }
.chat-timestamp { margin-left: auto; font-size: 0.65rem; color: #aaa; font-weight: normal; }
.chat-mine .chat-timestamp { color: rgba(255,255,255,0.6); }
.chat-preview {
    margin: 0 0.75rem 0.25rem; background: #f8f4e8;
    border: 1px dashed #d4c98a; align-self: flex-end;
}

.chat-input-row {
    display: flex; gap: 0.4rem; padding: 0.5rem 0.75rem;
    border-top: 1px solid #eee;
}
.chat-input {
    flex: 1; border: 1px solid #ddd; border-radius: 4px;
    padding: 0.4rem 0.6rem; font-size: 0.85rem; resize: none;
    font-family: inherit; outline: none;
}

/* KaTeX in chat */
.chat-mine .katex { color: #fff; }
.chat-bubble .katex { font-size: 0.9em; }

/* ── First-user welcome banner ── */
.first-user-banner {
    background: #e8f4fd; color: #1a5276; padding: 0.7rem;
    border-radius: 4px; font-size: 0.85rem; line-height: 1.4;
    border: 1px solid #aed6f1; margin-bottom: 0.2rem;
}

/* ── Admin panel ── */
.admin-users-table {
    width: 100%; border-collapse: collapse; font-size: 0.85rem; margin-bottom: 1rem;
}
.admin-users-table th {
    text-align: left; padding: 0.4rem 0.5rem; border-bottom: 2px solid #eee;
    font-size: 0.8rem; color: #777; font-weight: 600;
}
.admin-users-table td {
    padding: 0.4rem 0.5rem; border-bottom: 1px solid #f0f0f0; white-space: nowrap;
}
.role-tag {
    display: inline-block; font-size: 0.72rem; font-weight: 600; padding: 0.15rem 0.5rem;
    border-radius: 999px; text-transform: capitalize; letter-spacing: 0.02em;
}
.role-admin { background: #e8f0fe; color: #3b78c4; }
.role-user { background: #e9f7ef; color: #2e8b57; }
.admin-menu-btn {
    border: none; background: none; cursor: pointer; font-size: 1.1rem;
    padding: 2px 6px; border-radius: 4px; color: #888; letter-spacing: -1px;
}
.admin-menu-btn:hover { background: rgba(0,0,0,0.08); color: #333; }
.admin-row-menu {
    position: fixed; background: #fff; border: 1px solid #ddd; border-radius: 6px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15); z-index: 2100; min-width: 120px;
}
.admin-row-menu button {
    display: flex; align-items: center; gap: 0.4rem;
    width: 100%; padding: 0.4rem 0.7rem; border: none;
    background: none; text-align: left; cursor: pointer;
    font-size: 0.8rem; color: #333; white-space: nowrap;
}
.admin-row-menu button:hover { background: #f0f0f0; }
.admin-action-cell {
    white-space: nowrap; display: flex; align-items: center; gap: 0.3rem;
}
.admin-confirm-msg {
    font-size: 0.8rem; color: #c00; margin-right: 0.3rem;
}

/* ── AI Chat (integrated pane) ── */

/* AI Chat window menu (dropdown inside chat view) */
.ai-chat-win-menu {
    position: absolute; top: 36px; right: 8px;
    background: #fff; border: 1px solid #ddd; border-radius: 6px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15); z-index: 110;
    min-width: 130px;
}
.ai-chat-win-menu button {
    display: flex; align-items: center; gap: 0.4rem;
    width: 100%; padding: 0.4rem 0.7rem; border: none;
    background: none; text-align: left; cursor: pointer;
    font-size: 0.8rem; color: #333; white-space: nowrap;
}
.ai-chat-win-menu button:hover { background: #f0f0f0; }

/* Assistant message bubbles */
.chat-assistant {
    background: #f3f0ff; color: #333; align-self: flex-start;
    border-bottom-left-radius: 4px;
}
.chat-assistant .chat-sender-name { color: #6c5ce7; }
.chat-assistant .chat-timestamp { color: #aaa; }
.chat-assistant .katex { color: #333; }

/* Copy flash for assistant bubbles */
@keyframes chat-copied-flash-assistant {
    0% { background: #f3f0ff; }
    40% { background: #fff; }
    100% { background: #f3f0ff; }
}
.chat-assistant.chat-copied { animation: chat-copied-flash-assistant 0.5s ease; }

/* AI Chat loading indicator */
.ai-chat-loading {
    font-style: italic; color: #999; font-size: 0.8rem;
    padding: 0.3rem 0; align-self: flex-start;
}

/* ── Edit proposal chat bubble ── */
.chat-edit-proposal {
    border-left: 3px solid #6c5ce7;
}
.proposal-summary {
    font-style: italic;
    margin: 0.3rem 0;
    font-size: 0.85rem;
}
.proposal-action-area {
    margin-top: 0.4rem;
}
.proposal-view-btn {
    background: none;
    border: 1px solid #6c5ce7;
    color: #6c5ce7;
    padding: 0.2rem 0.6rem;
    border-radius: 4px;
    font-size: 0.78rem;
    cursor: pointer;
    transition: background 0.15s;
}
.proposal-view-btn:hover {
    background: rgba(108, 92, 231, 0.1);
}
.proposal-status {
    font-size: 0.78rem;
    font-weight: 600;
    padding: 0.15rem 0.5rem;
    border-radius: 3px;
}
.proposal-status-accepted {
    color: #27ae60;
    background: rgba(39, 174, 96, 0.1);
}
.proposal-status-rejected {
    color: #e74c3c;
    background: rgba(231, 76, 60, 0.1);
}

/* ── Diff panel overlay ── */
.diff-panel-overlay {
    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: 2000;
}
.diff-panel-overlay.hidden { display: none; }
.diff-panel {
    width: 80vw;
    height: 80vh;
    background: #fff;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}
.diff-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.8rem 1.2rem;
    border-bottom: 1px solid #e0e0e0;
    flex-shrink: 0;
}
.diff-panel-title {
    font-weight: 600;
    font-size: 1rem;
    color: #333;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.diff-panel-close {
    background: none;
    border: none;
    font-size: 1.4rem;
    cursor: pointer;
    color: #888;
    padding: 0 0.3rem;
    line-height: 1;
}
.diff-panel-close:hover { color: #333; }
.diff-panel-body {
    flex: 1;
    overflow-y: auto;
    padding: 0;
    font-family: 'SF Mono', 'Menlo', 'Consolas', monospace;
    font-size: 0.82rem;
    line-height: 1.5;
}
.diff-panel-footer {
    display: flex;
    justify-content: flex-end;
    gap: 0.6rem;
    padding: 0.8rem 1.2rem;
    border-top: 1px solid #e0e0e0;
    flex-shrink: 0;
}
.diff-panel-accept {
    background: #27ae60;
    color: #fff;
    border: none;
    padding: 0.45rem 1.2rem;
    border-radius: 4px;
    font-size: 0.85rem;
    cursor: pointer;
}
.diff-panel-accept:hover { background: #219a52; }
.diff-panel-reject {
    background: #e74c3c;
    color: #fff;
    border: none;
    padding: 0.45rem 1.2rem;
    border-radius: 4px;
    font-size: 0.85rem;
    cursor: pointer;
}
.diff-panel-reject:hover { background: #c0392b; }

/* Diff lines */
.diff-line {
    padding: 0 1rem;
    white-space: pre-wrap;
    word-break: break-all;
    min-height: 1.5em;
}
.diff-marker {
    display: inline-block;
    width: 1.2em;
    text-align: center;
    color: #888;
    user-select: none;
    font-weight: 600;
}
.diff-add {
    background: #e6ffed;
}
.diff-add .diff-marker { color: #27ae60; }
.diff-remove {
    background: #ffeef0;
}
.diff-remove .diff-marker { color: #e74c3c; }
.diff-equal {
    background: #fff;
}

/* ── Resource items ── */
.resource-item {
    padding: 0.35rem 0.75rem; white-space: nowrap;
    overflow: hidden; font-size: 0.75rem;
    border-left: 3px solid transparent; transition: background 0.15s;
    color: #bbb;
    display: flex; align-items: center; gap: 0.4rem;
}
.resource-item:hover { background: #3a3a3a; }
.resource-icon { flex-shrink: 0; color: #888; display: flex; align-items: center; }
.resource-name {
    flex: 1; overflow: hidden; text-overflow: ellipsis;
}
.resource-size {
    flex-shrink: 0; font-size: 0.65rem; color: #666;
}
.resource-item[draggable="true"] { user-select: none; cursor: grab; }
.resource-item.dragging { opacity: 0.4; }
.resource-drag-image {
    position: fixed;
    top: -1000px;
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.3rem 0.6rem;
    background: #3a3a3a;
    color: #eee;
    font-size: 0.78rem;
    border-radius: 4px;
    white-space: nowrap;
    pointer-events: none;
}
.resource-item:hover .more-btn { opacity: 1; }

/* Sidebar section gap spacer */
.sidebar-section-gap { height: 1rem; }

/* Page size column */
.page-size {
    flex-shrink: 0; font-size: 0.65rem; color: #666;
}

/* Notebook metadata labels */
.notebook-meta {
    font-size: 0.65rem; color: #888; font-weight: 400;
    display: flex; gap: 0.5rem; align-items: center;
    margin-top: 0.1rem;
}
.notebook-meta-icon {
    display: inline-flex; align-items: center; gap: 0.15rem;
}

/* ── Asset proposal chat bubble ── */
.chat-asset-proposal {
    border-left: 3px solid #6c5ce7;
}
.proposal-response-text {
    margin: 0.3rem 0 0.5rem;
    font-size: 0.85rem;
    line-height: 1.4;
}
.asset-file-info {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.4rem 0.6rem;
    background: rgba(108, 92, 231, 0.06);
    border: 1px solid rgba(108, 92, 231, 0.15);
    border-radius: 6px;
    margin: 0.3rem 0;
}
.asset-file-icon {
    display: flex;
    align-items: center;
    flex-shrink: 0;
}
.asset-filename {
    font-size: 0.8rem;
    font-weight: 500;
    color: #333;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
}
.asset-filesize {
    font-size: 0.72rem;
    color: #888;
    flex-shrink: 0;
}

/* Asset preview in diff panel */
.asset-preview-section {
    padding: 1rem;
    border-bottom: 1px solid #e0e0e0;
    display: flex;
    justify-content: center;
}
.asset-preview-iframe {
    width: 100%;
    height: 400px;
    border: 1px solid #e0e0e0;
    border-radius: 4px;
    background: #fafafa;
}
.asset-preview-model {
    width: 100%;
    height: 400px;
    border: 1px solid #e0e0e0;
    border-radius: 4px;
    background: #f9f9f9;
}
.asset-diff-section {
    padding: 0;
}
.asset-diff-label {
    font-size: 0.8rem;
    font-weight: 600;
    color: #555;
    padding: 0.6rem 1rem 0.3rem;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* Diff line numbers */
.diff-line-num {
    display: inline-block;
    width: 3em;
    text-align: right;
    color: #aaa;
    user-select: none;
    margin-right: 0.5em;
    font-size: 0.78em;
}

/* Contextual diff skip indicator */
.diff-context-skip {
    font-style: italic;
    color: #aaa;
    text-align: center;
    padding: 0.15rem 1rem;
    font-size: 0.78rem;
    background: #f8f8f8;
    user-select: none;
}

/* ── Inline SVG containers in preview ── */
.inline-svg-container {
    display: block;
    margin: 0.5rem auto;
    text-align: center;
    max-width: 100%;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    background: #f9f9f9;
    overflow: visible;
    padding: 8px;
    position: relative;
}
.inline-svg-container svg {
    width: 100%;
    height: 100%;
}

/* Math overlay system — positions KaTeX HTML via CSS absolute positioning
   instead of relying on SVG foreignObject transforms (broken in Safari) */
.math-overlay-wrap {
    position: absolute;
    overflow: hidden;
    pointer-events: none;
    z-index: 5;
}
.math-overlay {
    position: absolute;
    white-space: nowrap;
    display: inline-block;
    line-height: 1.2;
    pointer-events: auto;
    cursor: pointer;
}

/* SVG edit toggle button */
.svg-edit-toggle {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 24px;
    height: 24px;
    border: 1px solid #ccc;
    border-radius: 4px;
    background: #fff;
    cursor: pointer;
    font-size: 14px;
    line-height: 22px;
    text-align: center;
    z-index: 10;
    color: #666;
    padding: 0;
}
.svg-edit-toggle:hover {
    border-color: #999;
    color: #333;
}
.svg-edit-toggle.active {
    background: #4a90d9;
    border-color: #4a90d9;
    color: #fff;
}

/* SVG edit handles */
.svg-edit-handles circle {
    pointer-events: all;
    cursor: grab;
}

/* Pointer events for edit mode primitives */
.inline-svg-container.svg-editing polygon,
.inline-svg-container.svg-editing polyline,
.inline-svg-container.svg-editing line,
.inline-svg-container.svg-editing text {
    pointer-events: all;
    cursor: pointer;
}

.svg-edit-text-input {
    position: fixed;
    z-index: 9999;
    font-family: inherit;
    font-size: 14px;
    padding: 2px 4px;
    border: 1px solid #4a90d9;
    border-radius: 3px;
    background: rgba(255,255,255,0.95);
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
    outline: none;
    min-width: 60px;
}

/* Selection overlay rect in handles group */
.svg-edit-handles .svg-edit-selection-rect {
    pointer-events: none;
}

/* Bottom toolbar */
.svg-edit-toolbar {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 8px;
    background: #f0f0f0;
    border-top: 1px solid #ddd;
    border-radius: 0 0 6px 6px;
    margin: 0 -8px -8px -8px;
}

.svg-edit-toolbar label {
    display: flex; align-items: center; gap: 2px;
    font-size: 12px; color: #555; cursor: pointer;
    margin: 0;
}

.svg-edit-toolbar input[type="color"] {
    width: 24px; height: 24px; border: 1px solid #ccc;
    border-radius: 3px; padding: 1px; cursor: pointer;
}

.svg-edit-toolbar .svg-edit-none-toggle {
    font-size: 11px; display: flex; align-items: center; gap: 2px;
}

.svg-edit-toolbar .svg-edit-add-btn {
    padding: 2px 8px; font-size: 12px;
    border: 1px solid #ccc; border-radius: 4px;
    background: #fff; cursor: pointer; color: #444;
    display: flex; align-items: center; gap: 3px;
}
.svg-edit-toolbar .svg-edit-add-btn:hover {
    background: #e8e8e8; border-color: #999;
}
.svg-edit-toolbar .svg-edit-add-btn svg {
    width: 14px; height: 14px;
}

/* Mode segmented control (select | draw) */
.svg-mode-seg-group {
    display: flex;
}
.svg-mode-seg-group .svg-edit-add-btn {
    border-radius: 0;
    border-right-width: 0;
}
.svg-mode-seg-group .svg-mode-seg-select {
    border-radius: 4px 0 0 4px;
}
.svg-mode-seg-group .svg-mode-seg-draw {
    border-radius: 0 4px 4px 0;
    border-right-width: 1px;
}

/* Text segmented control (T | ↑) */
.svg-text-seg-group {
    display: flex;
}
.svg-text-seg-group .svg-edit-add-btn {
    border-radius: 0;
    border-right-width: 0;
}
.svg-text-seg-group .svg-text-seg-t {
    border-radius: 4px 0 0 4px;
}
.svg-text-seg-group .svg-text-seg-size {
    border-radius: 0 4px 4px 0;
    border-right-width: 1px;
    padding: 2px 4px;
    font-size: 11px;
}

/* Font size popup menu */
.svg-fontsize-menu {
    position: fixed;
    z-index: 10000;
    display: flex;
    align-items: center;
    gap: 4px;
    background: #fff;
    border: 1px solid #ccc;
    border-radius: 6px;
    padding: 4px 6px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    font-size: 12px;
}
.svg-fontsize-btn {
    width: 22px; height: 22px;
    border: 1px solid #ccc; border-radius: 4px;
    background: #f5f5f5; cursor: pointer;
    font-size: 14px; line-height: 1;
    display: flex; align-items: center; justify-content: center;
    padding: 0;
}
.svg-fontsize-btn:hover { background: #e0e0e0; border-color: #999; }
.svg-fontsize-val {
    min-width: 36px; text-align: center;
    font-size: 12px; color: #333; font-variant-numeric: tabular-nums;
}

/* SVG point dragging cursor override */
body.svg-point-dragging, body.svg-point-dragging * {
    cursor: grabbing !important;
}

/* SVG freehand draw mode cursor */
.inline-svg-container.svg-draw-mode,
.inline-svg-container.svg-draw-mode * {
    cursor: crosshair !important;
}

/* Active state for draw mode button */
.svg-edit-toolbar .svg-edit-add-btn.active {
    background: #4a90d9;
    color: #fff;
    border-color: #3a7bc8;
}

/* ── SVG syntax highlight ── */
.syn-svg-inline { color: #6a7a3a; opacity: 0.7; }
.svg-capsule {
    display: inline;
    background: #6c7a89;
    color: #fff;
    padding: 1px 8px;
    border-radius: 10px;
    font-size: 0.85em;
    font-family: -apple-system, BlinkMacSystemFont, sans-serif;
    cursor: default;
    user-select: all;
}

/* ── SVG proposal chat bubble ── */
.chat-svg-proposal {
    border-left: 3px solid #6c5ce7;
}
.svg-preview-container {
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    padding: 0.5rem;
    margin: 0.3rem 0;
    max-height: 300px;
    overflow: auto;
    background: #fff;
    text-align: center;
}
.svg-preview-container svg {
    max-width: 100%;
    max-height: 280px;
}
.svg-preview-caption {
    font-size: 0.75rem;
    color: #888;
    text-align: center;
    font-style: italic;
    margin-bottom: 0.3rem;
}

/* SVG panel preview in diff overlay */
.svg-panel-preview {
    padding: 1rem;
    border-bottom: 1px solid #e0e0e0;
    display: flex;
    justify-content: center;
    background: #fff;
}
.svg-panel-preview svg {
    max-width: 100%;
    max-height: 50vh;
}

/* ── Image resize drag ── */
body.img-resizing, body.img-resizing * {
    cursor: var(--resize-cursor) !important;
}
body.img-resizing iframe {
    pointer-events: none;
}
body.img-resizing model-viewer {
    pointer-events: none;
}
body.img-resizing .inline-svg-container {
    pointer-events: none;
}

/* Size label tooltip during drag */
.resize-size-label {
    position: fixed;
    pointer-events: none;
    z-index: 10000;
    background: #fff;
    border: 1px solid #999;
    border-radius: 3px;
    padding: 2px 6px;
    font-size: 11px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    color: #333;
    white-space: nowrap;
}

/* ── Preview pane images ── */
.preview-pane img { max-width: 100%; height: auto; border-radius: 4px; margin: 0.5rem 0; }
.preview-pane img.missing-image { max-width: 200px; opacity: 0.5; }
.preview-pane iframe { pointer-events: none; }

/* ── 3D model viewer ── */
.model-viewer-container {
    margin: 0.5rem 0; border: 1px solid #e0e0e0; border-radius: 6px;
    background: #f9f9f9; overflow: hidden; padding: 8px;
}
.model-loading {
    display: flex; align-items: center; justify-content: center;
    height: 100%; font-style: italic; color: #999;
}
.model-error {
    text-align: center; padding: 2rem; color: #c00;
    background: #fee; border-radius: 6px;
}

/* ── Download dropdown ── */
.toolbar-dropdown {
    position: relative; display: inline-flex;
}
.toolbar-dropdown-menu {
    position: absolute; top: 100%; right: 0; margin-top: 4px;
    background: #fff; border: 1px solid #ddd; border-radius: 6px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15); z-index: 200;
    min-width: 160px;
}
.toolbar-dropdown-menu button {
    display: block; width: 100%; padding: 0.45rem 0.75rem; border: none;
    background: none; text-align: left; cursor: pointer;
    font-size: 0.82rem; color: #333; white-space: nowrap;
}
.toolbar-dropdown-menu button:hover { background: #f0f0f0; }
