/* CSS Pattern Builder - preview + code left, layer stack right */

.bld-app {
    position: relative;
    display: flex;
    height: 100%;
    overflow: hidden;
}

/* Stage (left): the live preview on top, the generated code below it */
.bld-stage {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

/* Grab cursor only while the selected layer actually has an offset to set -
   the grounds and the two blocks that carry their own centre have none, and a
   cursor promising a drag that does nothing is worse than no cursor. Scoped
   here rather than in common.css because this is the first tool to use the
   stage's pan gesture; promote it if a second one does. */
.bld-app .bld-can-pan .gn-studio-frame {
    cursor: grab;
}

.bld-app .gn-studio-panning .gn-studio-frame {
    cursor: grabbing;
}

/* Fills the studio frame, so the pattern follows the letterboxing when an
   aspect ratio is picked. The frame behind it carries the checkerboard in
   transparent mode (.gn-studio-transparent). */
.bld-preview {
    width: 100%;
    height: 100%;
}

/* Code panel: a fixed share of the stage rather than a height that follows
   the code, so the preview never jumps when a layer is added or the format
   changes. A stack of eight layers writes four long comma lists, so this one
   scrolls more than its sibling tool's does. */
.bld-code {
    flex: 0 0 34%;
    min-height: 200px;
    display: flex;
    flex-direction: column;
    border-top: 1px solid var(--gn-card-border);
    background-color: var(--gn-card-bg);
}

.bld-code-bar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid var(--gn-card-border);
}

.bld-code-text {
    flex: 1 1 auto;
    min-height: 0;
    margin: 0;
    padding: 0.75rem;
    overflow: auto;
    font-size: 0.8rem;
    line-height: 1.5;
    white-space: pre;
    color: var(--gn-body-color);
}

/* Controls panel (right); header/body/footer layout comes from the shared
   .app-side-* classes in css/common.css */
.bld-panel {
    flex: 0 0 320px;
    border-left: 1px solid var(--gn-card-border);
}

.bld-label {
    font-size: 0.8rem;
    color: var(--gn-muted);
    margin-bottom: 0;
    margin-top: 0.5rem;
    display: block;
}

.bld-app .form-range {
    margin-top: -2px;
}

.bld-val {
    float: right;
    color: var(--gn-accent);
    font-size: 0.75rem;
}

.bld-sep {
    margin: 0.75rem 0;
    border-color: var(--gn-card-border);
    opacity: 1;
}

.bld-empty {
    margin: 0;
    padding: 1rem 0;
    font-size: 0.8rem;
    color: var(--gn-muted);
    text-align: center;
}

/* Background row and layers heading share one rhythm in the panel header */
.bld-bg-row,
.bld-layers-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
}

.bld-layers-head {
    margin-top: 0.6rem;
}

/* Section heading, matching the selected layer's name below it - the two
   are the panel's only headings and should carry the same weight */
.bld-head-title {
    font-weight: 600;
    font-size: 0.9rem;
}

/* Compact icon buttons for the two headings. Smaller than a btn-sm: they
   sit beside a heading, not in a row of actions. */
.bld-head-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 26px;
    height: 26px;
    padding: 0;
    border: 1px solid var(--bs-border-color);
    border-radius: 0.25rem;
    background: none;
    color: var(--gn-body-color);
    font-size: 0.85rem;
    line-height: 1;
}

.bld-head-btn:hover:not(:disabled) {
    border-color: var(--gn-accent);
    color: var(--gn-accent);
}

.bld-head-btn:disabled {
    opacity: 0.35;
}

.bld-bg-row .gn-bg-options {
    padding: 0;
}

.bld-count {
    color: var(--gn-accent);
    font-size: 0.75rem;
}

/* --- Layer stack ------------------------------------------------------- */

/* The list caps its own height and scrolls: the panel header must not push
   the controls out of the panel when the stack grows to eight. A drag is
   clamped to whatever is visible here (see app.js), so the row being held
   can never end up somewhere off screen. */
.bld-layers {
    position: relative;
    margin-top: 0.4rem;
    max-height: 302px;
    overflow-y: auto;
}

.bld-layer {
    display: flex;
    align-items: center;
    gap: 2px;
    padding: 3px 3px 3px 0;
    border: 1px solid transparent;
    border-radius: 0.375rem;
    background-color: var(--gn-card-bg);
}

.bld-layer:hover {
    background-color: var(--gn-sidebar-hover-bg);
}

.bld-layer.active {
    border-color: var(--gn-accent);
    background-color: var(--gn-sidebar-hover-bg);
}

/* A hidden layer stays legible but clearly out of play */
.bld-layer.bld-hidden .bld-layer-name,
.bld-layer.bld-hidden .bld-layer-swatch {
    opacity: 0.4;
}

/* The row being dragged rides above its neighbours and keeps its own
   background, so the rows it passes over stay readable underneath */
.bld-layer.bld-dragging {
    position: relative;
    z-index: 2;
    border-color: var(--gn-accent);
    box-shadow: 0 4px 12px rgb(0 0 0 / 25%);
}

.bld-layer-name {
    flex: 1 1 auto;
    min-width: 0;
    padding: 0.25rem 0.35rem;
    border: 0;
    background: none;
    text-align: left;
    font-size: 0.825rem;
    color: var(--gn-body-color);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* The layer's own motif, drawn on its own over white. The inner element is
   a whole tile that gets scaled down into this box (see swatchPaint), so the
   window here only frames and clips it. Keep the 30px in step with SWATCH in
   app.js. */
.bld-layer-swatch {
    position: relative;
    flex: 0 0 auto;
    width: 30px;
    height: 30px;
    overflow: hidden;
    border-radius: 0.25rem;
    border: 1px solid var(--gn-card-border);
}

.bld-layer-swatch > span {
    position: absolute;
    top: 0;
    left: 0;
    transform-origin: 0 0;
}

/* Full-strength icons rather than muted ones: these are the row's controls,
   not its labels, and at this size a muted glyph reads as disabled */
.bld-layer-grip,
.bld-layer-eye,
.bld-layer-act {
    flex: 0 0 auto;
    width: 32px;
    height: 34px;
    padding: 0;
    border: 0;
    background: none;
    font-size: 1.05rem;
    line-height: 1;
    color: var(--gn-body-color);
    border-radius: 0.25rem;
}

/* The handle claims the gesture: without touch-action none, a drag that
   starts here scrolls the list instead of moving the layer */
.bld-layer-grip {
    width: 22px;
    cursor: grab;
    touch-action: none;
}

.bld-layer.bld-dragging .bld-layer-grip {
    cursor: grabbing;
}

.bld-layer-grip:hover,
.bld-layer-eye:hover:not(:disabled),
.bld-layer-act:hover:not(:disabled) {
    color: var(--gn-accent);
    background-color: var(--gn-card-bg);
}

.bld-layer-act:disabled {
    opacity: 0.3;
}

/* --- Selected layer ---------------------------------------------------- */

.bld-sel-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    margin-bottom: 0.25rem;
}

/* The picker takes the row; Reset sits beside it as its own control,
   because it acts on the layer rather than on which block it draws */
.bld-sel-head .input-group {
    flex: 1 1 auto;
    min-width: 0;
}

.bld-sel-head .bld-head-btn {
    flex: 0 0 auto;
    width: 31px;
    height: 31px;
}

/* Previous/next/browse share the picker's border colour */
.bld-nav {
    border: 1px solid var(--bs-border-color);
    color: var(--gn-muted);
    background: none;
}

.bld-nav:hover {
    border-color: var(--bs-border-color);
    background-color: var(--gn-sidebar-hover-bg);
    color: var(--gn-accent);
}

.bld-color-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.25rem 0;
}

/* --- Block browser overlay --------------------------------------------- */

/* The dialog paints its own backdrop. Bootstrap's is appended to <body>,
   which is outside the app panel and so is not rendered while the panel is
   the fullscreen element - the dialog would float over the pattern with
   nothing behind it. */
.bld-browser.modal {
    background-color: rgba(0, 0, 0, 0.5);
}

/* A little taller than the default inset allows: this is a grid to browse,
   and the height is what decides how many rows are in view. Through the
   variable rather than the margin, because modal-dialog-scrollable works its
   own height out from it - set the two apart and the dialog is pushed off
   the top of a short screen. */
.bld-browser.modal {
    --bs-modal-margin: 1rem;
}

.bld-browser-search {
    max-width: 260px;
}

.bld-browser-grid {
    display: flex;
    flex-wrap: wrap;
    align-content: flex-start;
    gap: 8px;
    padding: 1rem;
}

.bld-browser-group {
    width: 100%;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--gn-accent);
    border-bottom: 1px solid var(--gn-card-border);
    padding-bottom: 4px;
    margin-top: 8px;
}

.bld-browser-empty {
    width: 100%;
    padding: 2rem 0;
    text-align: center;
    color: var(--gn-muted);
}

.bld-block-card {
    width: 128px;
    padding: 8px;
    text-align: center;
    background: none;
    border: 2px solid transparent;
    border-radius: 0.5rem;
    color: var(--gn-body-color);
    transition: border-color 0.15s, background-color 0.15s;
}

.bld-block-card:hover,
.bld-block-card.active {
    border-color: var(--gn-accent);
    background-color: var(--gn-sidebar-hover-bg);
}

/* A real element carrying the block's own CSS - the same code the layer
   would contribute, so the card cannot drift from what it adds */
.bld-swatch {
    display: block;
    width: 108px;
    height: 72px;
    margin: 0 auto 4px;
    border-radius: 0.375rem;
    border: 1px solid var(--gn-card-border);
}

.bld-block-card span:last-child {
    display: block;
    font-size: 0.7rem;
    color: var(--gn-muted);
    line-height: 1.15;
}

/* Small screens: stack, with the preview pinned at the top so the pattern
   stays visible while the controls below are being dragged */
@media (max-width: 767px) {
    .bld-app {
        flex-direction: column;
        overflow-y: auto;
    }

    .bld-stage {
        position: sticky;
        top: 0;
        z-index: 4;
        flex: 0 0 auto;
        height: 46vh;
    }

    .bld-panel {
        flex: 0 0 auto;
        border-left: 0;
        border-top: 1px solid var(--gn-card-border);
    }

    /* The whole app container scrolls instead of the panel body; the layer
       stack sticks below the preview and the action footer to the bottom */
    .bld-panel .app-side-body {
        overflow-y: visible;
    }

    .bld-panel .app-side-header {
        position: sticky;
        top: 46vh;
        z-index: 3;
        background-color: var(--gn-card-bg);
    }

    .bld-layers {
        max-height: 218px;
    }

    .bld-panel .app-side-footer {
        position: sticky;
        bottom: 0;
        z-index: 3;
        background-color: var(--gn-card-bg);
    }
}

/* Heading, reset button and live value on one row, so a slider can be put
   back on its own without disturbing the rest of the layer */
.bld-labelrow {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    margin-top: 0.5rem;
}

.bld-labelrow .bld-val {
    float: none;
    margin-left: auto;
}

/* Quiet until wanted: one of these sits on every slider, and at full
   strength eight of them would read as the loudest thing in the panel */
.bld-reset {
    display: inline-flex;
    align-items: center;
    padding: 0;
    border: 0;
    background: none;
    color: var(--gn-muted);
    line-height: 1;
    opacity: 0.45;
}

.bld-reset:hover,
.bld-reset:focus-visible {
    color: var(--gn-accent);
    opacity: 1;
}
