/* ============================================================
   Document editor (js/doc-editor.js) — full-cover overlay above
   the canvas when a document tab is active. Pages on the left,
   formatting toolbar + WYSIWYG page on the right.
   ============================================================ */

.doc-editor {
    position: absolute;
    inset: 0;
    z-index: 9500;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    color: var(--text-primary);
}
.doc-editor.hidden { display: none; }

.doc-ico { display: inline-flex; width: 16px; height: 16px; flex-shrink: 0; }
.doc-ico svg { width: 100%; height: 100%; }

/* ---- header ---- */
.doc-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 20px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.07);
    flex-shrink: 0;
    min-width: 0;
}
.doc-header-icon { color: #c8a8e0; }
.doc-header-kicker {
    font-size: 0.68rem;
    font-weight: 600;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: #c8a8e0;
}
.doc-header-title {
    font-size: 0.9rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 320px;
}
.doc-header-title { cursor: text; border-radius: 4px; padding: 1px 4px; margin: -1px -4px; }
.doc-header-title:hover,
.doc-header-title:focus-visible { background: rgba(255, 255, 255, 0.06); outline: none; }
.doc-title-rename {
    font: inherit;
    font-size: 0.9rem;
    font-weight: 600;
    width: 260px;
    color: var(--text-primary);
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--accent);
    border-radius: 4px;
    padding: 1px 5px;
    outline: none;
}
.doc-header-meta { font-size: 0.72rem; color: var(--text-secondary); white-space: nowrap; }
.doc-header-hint {
    font-size: 0.72rem;
    color: var(--text-secondary);
    opacity: 0.75;
    margin-left: 8px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}
.doc-header-status {
    margin-left: auto;
    font-size: 0.72rem;
    color: var(--accent);
    white-space: nowrap;
}

/* ---- body ---- */
.doc-body { flex: 1; display: flex; min-height: 0; }

/* ---- pages ---- */
.doc-pages {
    width: 208px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 12px 8px;
    border-right: 1px solid rgba(255, 255, 255, 0.07);
    background: rgba(255, 255, 255, 0.015);
    overflow-y: auto;
}
.doc-pages-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 6px 6px;
}
.doc-pages-label {
    font-size: 0.68rem;
    font-weight: 600;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text-secondary);
}
.doc-pages-add {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    padding: 4px;
    border-radius: 6px;
    border: 1px solid transparent;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
}
.doc-pages-add:hover { background: rgba(255, 255, 255, 0.08); color: var(--text-primary); }
.doc-pages-list { display: flex; flex-direction: column; gap: 2px; }
.doc-page {
    position: relative;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 8px;
    border-radius: 6px;
    font-size: 0.8rem;
    color: var(--text-secondary);
    cursor: pointer;
    user-select: none;
    outline: none;
}
.doc-page .doc-ico { width: 14px; height: 14px; opacity: 0.7; }
.doc-page:hover { background: rgba(255, 255, 255, 0.05); color: var(--text-primary); }
.doc-page:focus-visible { box-shadow: 0 0 0 1px var(--accent) inset; }
.doc-page.is-active { background: rgba(91, 164, 207, 0.14); color: var(--text-primary); }
.doc-page.is-active .doc-ico { color: var(--accent); opacity: 1; }
.doc-page.is-dragging { opacity: 0.4; }
.doc-page.drop-before::before,
.doc-page.drop-after::after {
    content: '';
    position: absolute;
    left: 6px;
    right: 6px;
    height: 2px;
    border-radius: 1px;
    background: var(--accent);
}
.doc-page.drop-before::before { top: -2px; }
.doc-page.drop-after::after { bottom: -2px; }
.doc-page-title { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-page-del {
    display: none;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    padding: 3px;
    border: none;
    border-radius: 4px;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
}
.doc-page:hover .doc-page-del,
.doc-page:focus-within .doc-page-del { display: inline-flex; }
.doc-page-del:hover { background: rgba(239, 68, 68, 0.16); color: var(--danger); }
.doc-page-rename {
    flex: 1;
    min-width: 0;
    font: inherit;
    color: var(--text-primary);
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--accent);
    border-radius: 4px;
    padding: 1px 5px;
    outline: none;
}

/* ---- main column ---- */
.doc-main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }

.doc-toolbar {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 2px;
    padding: 6px 16px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.07);
    flex-shrink: 0;
}
.doc-tool {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-width: 28px;
    height: 28px;
    padding: 0 7px;
    font-size: 0.78rem;
    font-family: inherit;
    color: var(--text-secondary);
    background: transparent;
    border: 1px solid transparent;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.12s, color 0.12s;
}
.doc-tool .doc-ico { width: 15px; height: 15px; }
.doc-tool:hover:not(:disabled) { background: rgba(255, 255, 255, 0.08); color: var(--text-primary); }
.doc-tool.is-on { background: rgba(91, 164, 207, 0.18); color: var(--accent); }
.doc-tool:disabled { opacity: 0.35; cursor: default; }
.doc-tool b, .doc-tool i, .doc-tool u, .doc-tool s { font-size: 0.85rem; }
.doc-tool b { font-weight: 700; }
.doc-tool i { font-style: italic; font-family: Georgia, serif; }
.doc-tool-text { font-weight: 600; }
.doc-tool-wide { padding: 0 10px; border-color: rgba(91, 164, 207, 0.28); color: var(--accent); }
.doc-tool-wide .doc-tool-text { font-weight: 500; }
.doc-tool-sep { width: 1px; height: 16px; margin: 0 4px; background: rgba(255, 255, 255, 0.12); }

.doc-scroll { flex: 1; min-height: 0; overflow-y: auto; }
.doc-scroll-inner { position: relative; max-width: 780px; margin: 0 auto; padding: 32px 48px 40vh; }

/* ---- the page ---- */
.doc-surface {
    outline: none;
    font-size: 0.95rem;
    line-height: 1.65;
    color: var(--text-primary);
    caret-color: var(--accent);
    word-wrap: break-word;
    overflow-wrap: anywhere;
    white-space: pre-wrap;
}
.doc-surface > * { margin: 0; }
.doc-surface p { min-height: 1.65em; }
.doc-surface h1, .doc-surface h2, .doc-surface h3 { position: relative; line-height: 1.3; }
.doc-surface h1 { font-size: 1.75rem; font-weight: 700; margin: 0.9em 0 0.3em; }
.doc-surface h2 { font-size: 1.35rem; font-weight: 700; margin: 0.8em 0 0.25em; }
.doc-surface h3 { font-size: 1.1rem; font-weight: 600; margin: 0.7em 0 0.2em; }
.doc-surface > :first-child { margin-top: 0; }
.doc-surface blockquote {
    border-left: 3px solid rgba(200, 168, 224, 0.5);
    padding: 1px 0 1px 14px;
    color: var(--text-secondary);
    min-height: 1.65em;
}
.doc-surface hr {
    border: none;
    border-top: 1px solid rgba(255, 255, 255, 0.14);
    margin: 14px 0;
}
.doc-surface pre.doc-code {
    margin: 6px 0;
    padding: 10px 14px;
    background: rgba(0, 0, 0, 0.32);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-family: 'SF Mono', 'Consolas', 'Monaco', monospace;
    font-size: 0.84rem;
    line-height: 1.55;
    white-space: pre-wrap;
    tab-size: 2;
}
.doc-surface code {
    padding: 1px 5px;
    border-radius: 4px;
    background: rgba(255, 255, 255, 0.08);
    font-family: 'SF Mono', 'Consolas', 'Monaco', monospace;
    font-size: 0.86em;
}
.doc-surface strong { font-weight: 700; }
.doc-surface em { font-style: italic; }
.doc-surface a.doc-link { color: #8ab4f8; text-decoration: underline; text-underline-offset: 2px; cursor: text; }
.doc-surface a.doc-link.is-unsafe { color: var(--text-secondary); text-decoration-style: dashed; }
.doc-surface a.doc-pwlink { color: var(--accent); text-decoration-style: dotted; cursor: pointer; }

/* lists: flat blocks, one per item, indented by data-indent */
.doc-surface .doc-li { position: relative; padding-left: 26px; min-height: 1.65em; }
.doc-surface .doc-li[data-indent="1"] { padding-left: 50px; }
.doc-surface .doc-li[data-indent="2"] { padding-left: 74px; }
.doc-surface .doc-li[data-indent="3"] { padding-left: 98px; }
.doc-surface .doc-li::before {
    position: absolute;
    top: 0;
    width: 20px;
    text-align: center;
    color: var(--text-secondary);
    user-select: none;
    pointer-events: none;
}
.doc-surface .doc-li::before { left: 2px; }
.doc-surface .doc-li[data-indent="1"]::before { left: 26px; }
.doc-surface .doc-li[data-indent="2"]::before { left: 50px; }
.doc-surface .doc-li[data-indent="3"]::before { left: 74px; }
.doc-surface .doc-li[data-list="ul"]::before { content: '\2022'; font-weight: 700; }
.doc-surface .doc-li[data-list="ul"][data-indent="1"]::before { content: '\25E6'; }
.doc-surface .doc-li[data-list="ul"][data-indent="2"]::before { content: '\25AA'; font-size: 0.8em; }
.doc-surface .doc-li[data-list="ol"]::before { content: attr(data-num); text-align: right; width: 20px; font-variant-numeric: tabular-nums; }
.doc-surface .doc-li[data-list="task"]::before {
    content: '';
    top: 0.33em;
    left: 5px;
    width: 13px;
    height: 13px;
    border: 1.5px solid rgba(255, 255, 255, 0.35);
    border-radius: 4px;
    box-sizing: border-box;
    pointer-events: auto;
    cursor: pointer;
}
.doc-surface .doc-li[data-list="task"][data-indent="1"]::before { left: 29px; }
.doc-surface .doc-li[data-list="task"][data-indent="2"]::before { left: 53px; }
.doc-surface .doc-li[data-list="task"][data-indent="3"]::before { left: 77px; }
.doc-surface .doc-li[data-list="task"][data-done="1"]::before {
    background: var(--accent) no-repeat center / 9px 9px url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpolyline points='2 6.5 5 9 10 3' fill='none' stroke='%2318181b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    border-color: var(--accent);
}
.doc-surface .doc-li[data-list="task"][data-done="1"] { color: var(--text-secondary); text-decoration: line-through; text-decoration-color: rgba(255, 255, 255, 0.3); }

/* empty page placeholder */
.doc-surface.is-empty > p:first-child { position: relative; }
.doc-surface.is-empty > p:first-child::before {
    content: 'Start writing…  # heading, - list, [] task, @ to link a node';
    position: absolute;
    left: 0;
    top: 0;
    color: var(--text-secondary);
    opacity: 0.5;
    pointer-events: none;
    white-space: nowrap;
}

/* a heading holding a node/group chip is that item's section */
.doc-surface .doc-section-head::before {
    content: '';
    position: absolute;
    left: -14px;
    top: 0.2em;
    bottom: 0.2em;
    width: 3px;
    border-radius: 2px;
    background: var(--accent);
    opacity: 0.75;
}

/* ---- workflow chips ---- */
.doc-chip {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    max-width: 100%;
    padding: 1px 7px 1px 2px;
    margin: 0 1px;
    border: 1px solid rgba(91, 164, 207, 0.32);
    border-radius: 7px;
    background: rgba(91, 164, 207, 0.12);
    color: var(--text-primary);
    font-size: 0.86em;
    line-height: 1.5;
    vertical-align: baseline;
    white-space: nowrap;
    cursor: pointer;
    user-select: none;
    text-decoration: none;
    font-weight: 500;
    font-style: normal;
}
.doc-chip:hover { background: rgba(91, 164, 207, 0.22); border-color: rgba(91, 164, 207, 0.55); }
/* In a heading the chip IS the heading (a node's section): heading-sized, heading weight. */
.doc-surface h1 .doc-chip, .doc-surface h2 .doc-chip, .doc-surface h3 .doc-chip {
    font-size: 0.92em;
    font-weight: inherit;
    padding: 2px 10px 2px 3px;
    border-radius: 9px;
    vertical-align: middle;
    position: relative;
    top: -0.05em;
}
.doc-surface h1 .doc-chip-thumb, .doc-surface h2 .doc-chip-thumb, .doc-surface h3 .doc-chip-thumb {
    width: 1.35em;
    height: 1.35em;
}
.doc-chip-thumb {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    border-radius: 5px;
    overflow: hidden;
    flex-shrink: 0;
    color: var(--accent);
    background: rgba(0, 0, 0, 0.25);
}
.doc-chip-thumb .doc-ico { width: 12px; height: 12px; }
.doc-chip-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.doc-chip-label { overflow: hidden; text-overflow: ellipsis; max-width: 260px; }
.doc-chip-dot { width: 6px; height: 6px; border-radius: 50%; background: #71717a; flex-shrink: 0; }
.doc-chip-dot[data-state=""] { display: none; }
.doc-chip-dot[data-state="generating"] { background: var(--warning); animation: doc-pulse 1.2s ease-in-out infinite; }
.doc-chip-dot[data-state="done"] { background: var(--success); }
.doc-chip-dot[data-state="sent"] { background: var(--accent); }
.doc-chip-dot[data-state="error"] { background: var(--danger); }
.doc-chip-dot[data-state="disabled"] { background: #52525b; }
.doc-chip.is-missing {
    border-style: dashed;
    border-color: rgba(239, 68, 68, 0.4);
    background: rgba(239, 68, 68, 0.06);
    color: var(--text-secondary);
}
.doc-chip.is-missing .doc-chip-label { text-decoration: line-through; }
.doc-chip.is-missing .doc-chip-thumb { color: var(--text-secondary); }
@keyframes doc-pulse { 50% { opacity: 0.35; } }

/* ---- outside edits + jumps ---- */
.doc-surface .doc-ext-changed {
    background: rgba(91, 164, 207, 0.13);
    box-shadow: -10px 0 0 rgba(91, 164, 207, 0.13), 10px 0 0 rgba(91, 164, 207, 0.13);
    border-radius: 2px;
    transition: background 0.6s, box-shadow 0.6s;
}
.doc-ext-label {
    position: absolute;
    right: 8px;
    padding: 2px 8px;
    border-radius: 999px;
    background: var(--accent);
    color: #0b1620;
    font-size: 0.68rem;
    font-weight: 600;
    pointer-events: none;
    z-index: 2;
}
.doc-surface .doc-flash { animation: doc-flash 1.6s ease-out; }
@keyframes doc-flash {
    0%, 30% { background: rgba(251, 191, 36, 0.22); box-shadow: -10px 0 0 rgba(251, 191, 36, 0.22), 10px 0 0 rgba(251, 191, 36, 0.22); }
    100% { background: transparent; box-shadow: none; }
}

/* ---- workflow-link picker ---- */
.doc-picker {
    position: absolute;
    z-index: 20;
    width: 340px;
    max-height: 300px;
    display: flex;
    flex-direction: column;
    background: rgba(39, 39, 42, 0.96);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
    overflow: hidden;
}
.doc-picker-input {
    margin: 8px;
    padding: 7px 10px;
    font: inherit;
    font-size: 0.82rem;
    color: var(--text-primary);
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    outline: none;
}
.doc-picker-input:focus { border-color: var(--accent); }
.doc-picker-list { overflow-y: auto; padding: 0 6px 6px; }
.doc-picker-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 8px;
    border-radius: 6px;
    font-size: 0.8rem;
    cursor: pointer;
}
.doc-picker-row.is-active, .doc-picker-row:hover { background: rgba(91, 164, 207, 0.16); }
.doc-picker-kind {
    flex-shrink: 0;
    min-width: 42px;
    font-size: 0.64rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--accent);
}
.doc-picker-label { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-picker-where { flex-shrink: 0; max-width: 110px; font-size: 0.7rem; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-picker-empty { padding: 10px; font-size: 0.78rem; color: var(--text-secondary); }

/* ---- narrow screens: pages become a strip ---- */
@media (max-width: 760px) {
    .doc-header-hint { display: none; }
    .doc-body { flex-direction: column; }
    .doc-pages {
        width: auto;
        flex-direction: row;
        align-items: center;
        padding: 6px 8px;
        border-right: none;
        border-bottom: 1px solid rgba(255, 255, 255, 0.07);
        overflow-x: auto;
        overflow-y: hidden;
    }
    .doc-pages-head { padding: 0 4px 0 0; }
    .doc-pages-label { display: none; }
    .doc-pages-list { flex-direction: row; }
    .doc-page { white-space: nowrap; }
    .doc-scroll-inner { padding: 20px 16px 40vh; }
}
