/*
 * Blocks view (issue #19): the document body as a list of editable
 * Markdown blocks. Plain mode only; Write_On mode never shows it.
 * Colours, radii and fonts come from public/css/tokens.css (--te-*) and the
 * Shoelace theme (--sl-*) already used by the plain toolbar.
 * Design notes: docs/design/blocks-view.md
 */

/* The surface is hidden (not removed) while the Blocks view is showing. */
.markdown-input[hidden],
.te-blocks[hidden] {
    display: none !important;
}

.te-blocks {
    box-sizing: border-box;
    height: calc(100% - 50px);
    overflow-y: auto;
    padding: 1rem;
    background: var(--sl-color-neutral-50, #fafafa);
}

.te-blocks-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.te-block {
    position: relative;
    padding: 0.5rem 0.75rem 0.75rem;
    border: 1px solid var(--sl-color-neutral-200, #e4e4e7);
    border-left: 3px solid var(--sl-color-neutral-300, #d4d4d8);
    border-radius: var(--te-radius-popover);
    background: var(--sl-color-neutral-0, #fff);
    cursor: default;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.te-block:hover {
    border-left-color: var(--te-color-accent-soft);
}

/* Visible focus for keyboard users (and for the current block). */
.te-block:focus {
    outline: 2px solid var(--te-color-accent);
    outline-offset: 2px;
    border-left-color: var(--te-color-accent);
}

.te-block-editing {
    border-left-color: var(--te-color-accent);
    box-shadow: 0 0 0 1px var(--te-color-accent-soft);
}

.te-block-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    min-height: 1.5rem;
}

.te-block-type {
    font-family: var(--te-font-mono);
    font-size: 0.6875rem;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--te-color-text-dim);
}

.te-block-actions {
    display: inline-flex;
    gap: 0.125rem;
    opacity: 0;
    transition: opacity 0.15s ease;
}

.te-block:hover .te-block-actions,
.te-block:focus .te-block-actions,
.te-block:focus-within .te-block-actions {
    opacity: 1;
}

.te-block-action,
.te-blocks-add {
    font: inherit;
    border: 1px solid transparent;
    border-radius: 4px;
    background: transparent;
    color: var(--sl-color-neutral-600, #52525b);
    cursor: pointer;
}

.te-block-action {
    width: 1.75rem;
    height: 1.5rem;
    font-size: 0.75rem;
}

.te-block-action:hover,
.te-blocks-add:hover {
    background: var(--sl-color-neutral-100, #f4f4f5);
    color: var(--te-color-accent);
}

.te-block-action:focus-visible,
.te-blocks-add:focus-visible {
    outline: 2px solid var(--te-color-accent);
    outline-offset: 1px;
}

.te-block-body > :first-child {
    margin-top: 0.25rem;
}

.te-block-body > :last-child {
    margin-bottom: 0;
}

.te-block-body pre,
.te-block-source {
    overflow-x: auto;
    font-family: var(--te-font-mono);
    white-space: pre-wrap;
}

.te-block-body table {
    border-collapse: collapse;
}

.te-block-body th,
.te-block-body td {
    padding: 0.25rem 0.5rem;
    border: 1px solid var(--sl-color-neutral-200, #e4e4e7);
}

.te-block-code { border-left-color: var(--sl-color-neutral-500, #71717a); }
.te-block-heading { border-left-color: var(--te-color-accent-soft); }

.te-block-editor {
    box-sizing: border-box;
    display: block;
    width: 100%;
    margin-top: 0.25rem;
    padding: 0.5rem;
    border: 1px solid var(--te-color-accent-soft);
    border-radius: 4px;
    font-family: var(--te-font-mono);
    font-size: 0.875rem;
    line-height: 1.5;
    resize: vertical;
    caret-color: var(--te-color-accent);
}

.te-block-editor:focus {
    outline: 2px solid var(--te-color-accent);
    outline-offset: 0;
}

.te-blocks-add {
    margin-top: 0.75rem;
    padding: 0.375rem 0.75rem;
    border-color: var(--sl-color-neutral-300, #d4d4d8);
    border-style: dashed;
}

.te-blocks-empty {
    color: var(--te-color-text-dim);
}

/* Drafts kept after an outside change overlapped an open block editor.
   Non-blocking, above the surface and the blocks, in both views. */
.te-blocks-drafts[hidden] {
    display: none !important;
}

.te-blocks-drafts {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin: 0.5rem 1rem 0;
}

.te-blocks-drafts:focus-visible {
    outline: 2px solid var(--te-color-accent);
    outline-offset: 2px;
}

.te-blocks-draft {
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--te-color-accent-soft);
    border-left: 3px solid var(--te-color-accent);
    border-radius: var(--te-radius-popover);
    background: var(--sl-color-neutral-0, #fff);
}

.te-blocks-draft-message {
    margin: 0 0 0.25rem;
    font-size: 0.875rem;
}

.te-blocks-draft-message i {
    color: var(--te-color-accent);
}

.te-blocks-draft-text {
    max-height: 8rem;
    margin: 0 0 0.5rem;
    padding: 0.375rem 0.5rem;
    overflow: auto;
    font-family: var(--te-font-mono);
    font-size: 0.8125rem;
    white-space: pre-wrap;
    background: var(--sl-color-neutral-50, #fafafa);
    border-radius: 4px;
}

.te-blocks-draft-actions {
    display: flex;
    gap: 0.5rem;
}

.te-blocks-draft-actions button {
    font: inherit;
    font-size: 0.8125rem;
    padding: 0.25rem 0.625rem;
    border: 1px solid var(--sl-color-neutral-300, #d4d4d8);
    border-radius: 4px;
    background: transparent;
    cursor: pointer;
}

.te-blocks-draft-apply {
    border-color: var(--te-color-accent) !important;
    color: var(--te-color-accent);
}

.te-blocks-draft-actions button:focus-visible {
    outline: 2px solid var(--te-color-accent);
    outline-offset: 1px;
}

/* Move up / Move down (#58): no button where the block cannot move. */
.te-block-action[hidden] {
    display: none;
}

/* Messages about refused actions, such as a move the model would not make
   because it would split a span or ghost. Polite live region. */
.te-blocks-notice[hidden] {
    display: none !important;
}

.te-blocks-notice {
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
    margin: 0 0 0.75rem;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--te-color-accent-soft);
    border-left: 3px solid var(--te-color-accent);
    border-radius: var(--te-radius-popover);
    background: var(--sl-color-neutral-0, #fff);
}

.te-blocks-notice-text {
    flex: 1;
    margin: 0;
    font-size: 0.875rem;
}

.te-blocks-notice-dismiss {
    font: inherit;
    font-size: 0.75rem;
    width: 1.75rem;
    height: 1.5rem;
    border: 1px solid transparent;
    border-radius: 4px;
    background: transparent;
    color: var(--sl-color-neutral-600, #52525b);
    cursor: pointer;
}

.te-blocks-notice-dismiss:hover {
    background: var(--sl-color-neutral-100, #f4f4f5);
    color: var(--te-color-accent);
}

.te-blocks-notice-dismiss:focus-visible {
    outline: 2px solid var(--te-color-accent);
    outline-offset: 1px;
}

/* Blocks is not part of Write_On: never shown there. */
.te-app[data-mode="write-on"] .te-blocks,
.te-app[data-mode="write-on"] .te-view-toggle {
    display: none !important;
}

@media (prefers-reduced-motion: reduce) {
    .te-block,
    .te-block-actions {
        transition: none;
    }
}
