/* ==================== Composer ==================== */
/*
 * Layout:
 *   ┌──────────────────────────────────────────────┐
 *   │  Toolbar                                      │
 *   ├──────┬───────────────────────────────────────┤
 *   │ Rail │  Active section's editor (full canvas │
 *   │      │  editor mounted via initEditor)       │
 *   └──────┴───────────────────────────────────────┘
 */

.composer-layout {
    position: fixed;
    /* Sits to the right of the persistent sidebar. Tied to
     * var(--sidebar-width) so any nav-width change propagates here
     * automatically (was hardcoded 56px). */
    inset: 0 0 0 var(--sidebar-width, 80px);
    transition: left 0.16s ease;
    display: flex;
    flex-direction: column;
    background: var(--bg-secondary, #f7f7fa);
    z-index: 100;
    overflow: hidden;
}

.composer-toolbar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 16px;
    background: #fff;
    border-bottom: 1px solid var(--border-subtle, #e6e6ec);
    flex-shrink: 0;
}
.composer-back {
    padding: 6px;
    line-height: 0;
}
.composer-doc-name {
    font-size: 14px;
    font-weight: 600;
    border: 1px solid transparent;
    background: transparent;
    padding: 6px 10px;
    border-radius: 6px;
    width: 280px;
    color: var(--text-primary, #1a1a2e);
}
.composer-doc-name:hover { border-color: var(--border-subtle, #e6e6ec); }
.composer-doc-name:focus {
    outline: none;
    border-color: #5B53E8;
    background: #fff;
}
.composer-toolbar-right {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 8px;
}
.composer-save-status {
    font-size: 11px;
    color: var(--text-muted, #888);
    padding: 3px 8px;
    border-radius: 4px;
    background: var(--bg-tertiary, #f0f1f5);
    min-width: 50px;
    text-align: center;
}
/* "+ Add section" — the rail's free growth path. */
.composer-rail-add {
    display: block;
    width: calc(100% - 16px);
    margin: 8px;
    padding: 7px 10px;
    font-size: 12px;
    font-weight: 600;
    color: var(--accent, #5B53E8);
    background: transparent;
    border: 1px dashed var(--accent, #5B53E8);
    border-radius: 8px;
    cursor: pointer;
}
.composer-rail-add:hover { background: rgba(91, 83, 232, 0.06); }

/* Add-section dialog (compact form modal; X/Cancel/Escape close only). */
.composer-addsec-modal { max-width: 380px; }
.composer-addsec-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.composer-addsec-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin: 10px 0;
}
.composer-addsec-chip {
    font-size: 11.5px;
    padding: 4px 9px;
    border: 1px solid var(--border, #e0e0e8);
    border-radius: 999px;
    background: var(--bg, #fff);
    cursor: pointer;
    color: var(--text, #1a1a2e);
}
.composer-addsec-chip:hover { border-color: var(--accent, #5B53E8); color: var(--accent, #5B53E8); }
.composer-addsec-dims {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
}
.composer-addsec-num {
    width: 90px;
    padding: 6px 8px;
    font-size: 13px;
    border: 1px solid var(--border, #e0e0e8);
    border-radius: 6px;
}
.composer-addsec-x { color: var(--text-muted, #888); }
.composer-addsec-name {
    display: block;
    width: 100%;
    padding: 6px 8px;
    font-size: 13px;
    border: 1px solid var(--border, #e0e0e8);
    border-radius: 6px;
    margin-bottom: 14px;
}
.composer-addsec-foot {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

/* Rail section filter (shown at ≥6 sections). */
.composer-rail-filter {
    display: block;
    width: calc(100% - 16px);
    margin: 0 8px 8px;
    padding: 5px 8px;
    font-size: 12px;
    border: 1px solid var(--border, #e0e0e8);
    border-radius: 6px;
    background: var(--bg, #fff);
    color: var(--text, #1a1a2e);
}
.composer-rail-filter:focus {
    outline: none;
    border-color: var(--accent, #5B53E8);
}
.composer-rail-nomatch {
    padding: 14px 10px;
    font-size: 12px;
    color: var(--text-muted, #888);
    text-align: center;
}

/* Load-failure card (network blip / deploy restart / deleted composition). */
.composer-load-error { gap: 6px; }
.composer-load-error-title {
    font-size: 15px;
    font-weight: 700;
    color: var(--text, #1a1a2e);
}
.composer-load-error-msg {
    font-size: 13px;
    color: var(--text-muted, #666);
    max-width: 420px;
    text-align: center;
}
.composer-load-error-actions {
    display: flex;
    gap: 8px;
    margin-top: 10px;
}

/* Export progress toast — the shared loading state for every export trigger
   (toolbar buttons, format menu, rail download menu). pointer-events none so
   it never blocks the canvas while a long ZIP renders. */
.composer-export-toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(17, 17, 27, 0.92);
    color: #fff;
    font-size: 12.5px;
    font-weight: 600;
    padding: 8px 16px;
    border-radius: 999px;
    z-index: 10000;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
    pointer-events: none;
}
/* Slim caret next to "Export current PNG" — opens the format menu. */
.composer-export-caret {
    padding: 4px 6px;
    margin-left: -4px;
    display: inline-flex;
    align-items: center;
}
/* Failed save → the pill becomes a retry button (setStatus toggles is-error). */
.composer-save-status.is-error {
    color: #b91c1c;
    background: #fee2e2;
    cursor: pointer;
    font-weight: 600;
}
.composer-save-status.is-error:hover { background: #fecaca; }
.composer-save-status.is-error:focus-visible {
    outline: 2px solid #b91c1c;
    outline-offset: 1px;
}

.composer-main {
    flex: 1;
    display: grid;
    grid-template-columns: 220px 1fr;
    overflow: hidden;
}

/* ---- Section rail (left) ---- */
.composer-rail {
    background: #fff;
    border-right: 1px solid var(--border-subtle, #e6e6ec);
    overflow-y: auto;
    padding: 12px 10px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.composer-rail-title {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted, #aaa);
    margin: 4px 4px 4px;
}
.composer-rail-item {
    position: relative;
    display: flex;
    gap: 10px;
    padding: 8px;
    border-radius: 8px;
    border: 1.5px solid transparent;
    cursor: pointer;
    transition: all 0.12s;
    background: #fafafd;
}
.composer-rail-item:hover {
    background: #f0f0ff;
}
.composer-rail-item.active {
    background: #f0f0ff;
    border-color: #5B53E8;
    box-shadow: var(--focus-ring);
}
.composer-rail-thumb {
    flex-shrink: 0;
    width: 56px;
    height: 56px;
    border-radius: 6px;
    overflow: hidden;
    background: var(--bg-tertiary, #f0f1f5);
    border: 1px solid var(--border-subtle, #e6e6ec);
}
.composer-rail-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}
.composer-rail-meta {
    flex: 1;
    min-width: 0;
}
.composer-rail-name {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-primary, #1a1a2e);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
/* Inline rename input — swaps in for the name on double-click. */
.composer-rail-rename {
    width: 100%;
    font-size: 12px;
    font-weight: 600;
    font-family: inherit;
    color: var(--text-primary, #1a1a2e);
    border: 1px solid #5B53E8;
    border-radius: 4px;
    padding: 1px 4px;
    outline: none;
    background: #fff;
}
.composer-rail-dim {
    font-size: 10px;
    color: var(--text-muted, #888);
    margin-top: 2px;
}
/* Action cluster — Download + Remove buttons stacked at the top-right of
   each rail item. Hidden by default, revealed on hover so the rail stays
   visually quiet when scanning sections. */
.composer-rail-actions {
    position: absolute;
    top: 6px;
    right: 6px;
    display: none;
    flex-direction: row;
    gap: 4px;
}
.composer-rail-item:hover .composer-rail-actions { display: flex; }
/* Keyboard path: tabbing into the item (or any of its action buttons) reveals
   the cluster — hover-only would make the buttons unreachable by keyboard. */
.composer-rail-item:focus-within .composer-rail-actions { display: flex; }
.composer-rail-item:focus-visible {
    outline: 2px solid #5B53E8;
    outline-offset: -2px;
}

.composer-rail-move,
.composer-rail-duplicate,
.composer-rail-download,
.composer-rail-remove {
    position: static;        /* parent .composer-rail-actions handles position */
    width: 22px;
    height: 22px;
    border: none;
    border-radius: 4px;
    background: rgba(255, 255, 255, 0.9);
    color: var(--text-muted, #555);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: all 0.12s;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}
.composer-rail-move:hover:not(:disabled),
.composer-rail-duplicate:hover,
.composer-rail-download:hover {
    background: #5B53E8;
    color: #fff;
}
.composer-rail-move:disabled { opacity: 0.35; cursor: default; }
.composer-rail-remove:hover {
    background: #fee2e2;
    color: #b91c1c;
}

/* Subsection rendering — proper tree-view layout with continuous vertical
   trunk + horizontal branch stubs at each child. The trunk runs through
   every middle child (├) and stops at the center of the last child (└).

   Layout:
       Parent
       │
       ├── First child
       ├── Middle child
       └── Last child

   Implementation:
   - Each subsection gets margin-left for indentation.
   - ::before draws the vertical trunk on the left of the card.
   - ::after draws the horizontal branch from trunk to card.
   - The .is-last-subsection class (added in JS) caps the trunk at the
     row's vertical center so it doesn't extend below the final child.
   - Slightly muted background + softer border on subsections so the
     hierarchy reads at a glance even before you trace the lines.
*/
.composer-rail-item.is-subsection {
    --tree-indent: 22px;
    --tree-stub: 14px;
    --tree-color: #c4c8d4;
    margin-left: var(--tree-indent);
    position: relative;
    background: var(--bg-secondary, #fafafd);
    border-color: #ebecef;
}

/* Vertical trunk — full height by default (├ shape). */
.composer-rail-item.is-subsection::before {
    content: '';
    position: absolute;
    left: calc(var(--tree-stub) * -1);
    top: -7px;            /* extend up into the gap so siblings' trunks join cleanly */
    bottom: -7px;
    width: 1.5px;
    background: var(--tree-color);
    border-radius: 1px;
    pointer-events: none;
}

/* Last subsection — trunk stops at center (└ shape) so the line
   doesn't suggest there are more siblings below. */
.composer-rail-item.is-subsection.is-last-subsection::before {
    top: -7px;
    bottom: auto;
    height: calc(50% + 7px);
}

/* Horizontal branch — stub from trunk to card edge, terminates
   with a small rounded "knee" at the card. */
.composer-rail-item.is-subsection::after {
    content: '';
    position: absolute;
    left: calc(var(--tree-stub) * -1);
    top: 50%;
    width: var(--tree-stub);
    height: 1.5px;
    background: var(--tree-color);
    border-radius: 1px;
    pointer-events: none;
}

/* Active state — the colored highlight on a selected subsection should
   tint the tree lines too, so the eye traces back to the parent more
   easily. Composer's active state uses --accent (indigo); we mirror it. */
.composer-rail-item.is-subsection.active {
    --tree-color: #5B53E8;
}
.composer-rail-item.is-subsection.active::before,
.composer-rail-item.is-subsection.active::after {
    background: var(--tree-color);
}

/* Download popover — 3-option menu anchored next to the download icon. */
.composer-rail-dl-popover {
    background: #fff;
    border: 1px solid var(--border, #e6e6ec);
    border-radius: 10px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.14);
    padding: 6px;
    min-width: 280px;
    max-width: 360px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.composer-rail-dl-title {
    font-size: 11px;
    font-weight: 600;
    color: var(--text-muted, #888);
    text-transform: uppercase;
    letter-spacing: 0.4px;
    padding: 6px 10px 4px;
}
.composer-rail-dl-option {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    background: transparent;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 12px;
    color: var(--text-primary, #1a1a2e);
    text-align: left;
    width: 100%;
}
.composer-rail-dl-option:hover {
    background: rgba(99, 102, 241, 0.08);
    color: #5B53E8;
}
.composer-rail-dl-cancel {
    padding: 6px 10px;
    background: transparent;
    border: none;
    border-top: 1px solid var(--border, #e6e6ec);
    border-radius: 0 0 8px 8px;
    margin-top: 4px;
    cursor: pointer;
    font-size: 11px;
    color: var(--text-muted, #888);
    text-align: center;
}
.composer-rail-dl-cancel:hover { color: var(--text-primary, #1a1a2e); }

/* ---- Editor mount (right) ---- */
.composer-editor-mount {
    overflow: hidden;
    position: relative;
    background: var(--bg-secondary, #f7f7fa);
}
/* The editor mounts an `.editor-layout` inside this slot. Make it fill. */
.composer-editor-mount > .editor-layout {
    height: 100%;
}

/* ---- Studio tray multi-select bits (kept here so single CSS file owns it) ---- */
.studio-winner {
    position: relative;
}
.studio-winner-select-btn {
    position: absolute;
    top: 8px;
    left: 8px;
    width: 22px;
    height: 22px;
    border-radius: 6px;
    border: 1.5px solid #fff;
    background: rgba(255,255,255,0.7);
    cursor: pointer;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    color: transparent;
    backdrop-filter: blur(4px);
    transition: all 0.12s;
}
.studio-winner-select-btn:hover {
    background: rgba(255,255,255,0.95);
}
.studio-winner-select-btn.on {
    background: #5B53E8;
    border-color: #5B53E8;
    color: #fff;
}
.studio-winner.selected {
    border-color: #5B53E8;
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

/* Remove (×) corner button — mirrors the selection checkbox on the right.
   Always visible so removing a winner is one click from anywhere on the
   card. Red hover so the destructive action is obvious before commit. */
.studio-winner-remove-corner {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 22px;
    height: 22px;
    border-radius: 6px;
    border: 1.5px solid #fff;
    background: rgba(255, 255, 255, 0.7);
    cursor: pointer;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #555;
    backdrop-filter: blur(4px);
    transition: all 0.12s;
    padding: 0;
}
.studio-winner-remove-corner:hover {
    background: #ef4444;
    border-color: #ef4444;
    color: #fff;
}
.studio-tray-composer-btn {
    width: 100%;
    margin-bottom: 6px;
}
.studio-tray-composer-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}
/* In-flight state — slightly different shade so the disabled+spinner
   combo reads as "working" rather than "blocked". Generic across any
   .btn (footer composer button + per-winner action buttons + anywhere
   else _withLock() is used). */
.btn.is-loading,
.btn.is-loading:disabled {
    opacity: 0.85;
    cursor: progress;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}
.btn-spinner {
    display: inline-block;
    width: 12px;
    height: 12px;
    border: 2px solid currentColor;
    border-top-color: transparent;
    border-radius: 50%;
    animation: btn-spinner-spin 0.7s linear infinite;
    flex-shrink: 0;
}
@keyframes btn-spinner-spin {
    to { transform: rotate(360deg); }
}
.studio-tray-composer-hint {
    font-size: 10px;
    color: var(--text-muted, #aaa);
    text-align: center;
    margin-bottom: 8px;
    min-height: 14px;
    line-height: 1.4;
}

@media (max-width: 800px) {
    .composer-main {
        grid-template-columns: 1fr;
        grid-template-rows: 200px 1fr;
    }
    .composer-rail {
        flex-direction: row;
        overflow-x: auto;
        overflow-y: hidden;
        border-right: none;
        border-bottom: 1px solid var(--border-subtle, #e6e6ec);
    }
    .composer-rail-item {
        flex-shrink: 0;
        flex-direction: column;
        width: 110px;
    }
    .composer-rail-thumb {
        width: 100%;
        aspect-ratio: 1;
        height: auto;
    }
}
