html, body {
    font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}

h1:focus {
    outline: none;
}

a, .btn-link {
    color: #0071c1;
}

.btn-primary {
    color: #fff;
    background-color: #1b6ec2;
    border-color: #1861ac;
}

.btn:focus, .btn:active:focus, .btn-link.nav-link:focus, .form-control:focus, .form-check-input:focus {
  box-shadow: 0 0 0 0.1rem white, 0 0 0 0.25rem #258cfb;
}

.content {
    padding-top: 1.1rem;
}

.valid.modified:not([type=checkbox]) {
    outline: 1px solid #26b050;
}

.invalid {
    outline: 1px solid red;
}

.validation-message {
    color: red;
}

#blazor-error-ui {
    color-scheme: light only;
    background: lightyellow;
    bottom: 0;
    box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.2);
    box-sizing: border-box;
    display: none;
    left: 0;
    padding: 0.6rem 1.25rem 0.7rem 1.25rem;
    position: fixed;
    width: 100%;
    z-index: 1000;
}

    #blazor-error-ui .dismiss {
        cursor: pointer;
        position: absolute;
        right: 0.75rem;
        top: 0.5rem;
    }

.blazor-error-boundary {
    background: url(data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNTYiIGhlaWdodD0iNDkiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIG92ZXJmbG93PSJoaWRkZW4iPjxkZWZzPjxjbGlwUGF0aCBpZD0iY2xpcDAiPjxyZWN0IHg9IjIzNSIgeT0iNTEiIHdpZHRoPSI1NiIgaGVpZ2h0PSI0OSIvPjwvY2xpcFBhdGg+PC9kZWZzPjxnIGNsaXAtcGF0aD0idXJsKCNjbGlwMCkiIHRyYW5zZm9ybT0idHJhbnNsYXRlKC0yMzUgLTUxKSI+PHBhdGggZD0iTTI2My41MDYgNTFDMjY0LjcxNyA1MSAyNjUuODEzIDUxLjQ4MzcgMjY2LjYwNiA1Mi4yNjU4TDI2Ny4wNTIgNTIuNzk4NyAyNjcuNTM5IDUzLjYyODMgMjkwLjE4NSA5Mi4xODMxIDI5MC41NDUgOTIuNzk1IDI5MC42NTYgOTIuOTk2QzI5MC44NzcgOTMuNTEzIDI5MSA5NC4wODE1IDI5MSA5NC42NzgyIDI5MSA5Ny4wNjUxIDI4OS4wMzggOTkgMjg2LjYxNyA5OUwyNDAuMzgzIDk5QzIzNy45NjMgOTkgMjM2IDk3LjA2NTEgMjM2IDk0LjY3ODIgMjM2IDk0LjM3OTkgMjM2LjAzMSA5NC4wODg2IDIzNi4wODkgOTMuODA3MkwyMzYuMzM4IDkzLjAxNjIgMjM2Ljg1OCA5Mi4xMzE0IDI1OS40NzMgNTMuNjI5NCAyNTkuOTYxIDUyLjc5ODUgMjYwLjQwNyA1Mi4yNjU4QzI2MS4yIDUxLjQ4MzcgMjYyLjI5NiA1MSAyNjMuNTA2IDUxWk0yNjMuNTg2IDY2LjAxODNDMjYwLjczNyA2Ni4wMTgzIDI1OS4zMTMgNjcuMTI0NSAyNTkuMzEzIDY5LjMzNyAyNTkuMzEzIDY5LjYxMDIgMjU5LjMzMiA2OS44NjA4IDI1OS4zNzEgNzAuMDg4N0wyNjEuNzk1IDg0LjAxNjEgMjY1LjM4IDg0LjAxNjEgMjY3LjgyMSA2OS43NDc1QzI2Ny44NiA2OS43MzA5IDI2Ny44NzkgNjkuNTg3NyAyNjcuODc5IDY5LjMxNzkgMjY3Ljg3OSA2Ny4xMTgyIDI2Ni40NDggNjYuMDE4MyAyNjMuNTg2IDY2LjAxODNaTTI2My41NzYgODYuMDU0N0MyNjEuMDQ5IDg2LjA1NDcgMjU5Ljc4NiA4Ny4zMDA1IDI1OS43ODYgODkuNzkyMSAyNTkuNzg2IDkyLjI4MzcgMjYxLjA0OSA5My41Mjk1IDI2My41NzYgOTMuNTI5NSAyNjYuMTE2IDkzLjUyOTUgMjY3LjM4NyA5Mi4yODM3IDI2Ny4zODcgODkuNzkyMSAyNjcuMzg3IDg3LjMwMDUgMjY2LjExNiA4Ni4wNTQ3IDI2My41NzYgODYuMDU0N1oiIGZpbGw9IiNGRkU1MDAiIGZpbGwtcnVsZT0iZXZlbm9kZCIvPjwvZz48L3N2Zz4=) no-repeat 1rem/1.8rem, #b32121;
    padding: 1rem 1rem 1rem 3.7rem;
    color: white;
}

    .blazor-error-boundary::after {
        content: "An error has occurred."
    }

/* Pre-boot splash / crawlable content shown until Blazor WASM takes over #app. */
.app-prerender {
    max-width: 640px;
    margin: 7vh auto 0;
    padding: 0 1rem;
    text-align: center;
    font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}

    .app-prerender h1 {
        font-size: 2rem;
        margin: 0 0 0.6rem;
    }

    .app-prerender p {
        color: #5b626d;
        line-height: 1.5;
        margin: 0;
    }

/* Crawlable How-it-works/FAQ copy: present in the DOM for search engines and
   no-JS visitors, but kept out of the brief loading splash (Blazor replaces the
   whole #app on boot, so users see the live tool, not this text). */
.app-prerender .seo-content {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
}

.loading-progress {
    position: absolute;
    display: block;
    width: 8rem;
    height: 8rem;
    inset: 20vh 0 auto 0;
    margin: 0 auto 0 auto;
}

    .loading-progress circle {
        fill: none;
        stroke: #e0e0e0;
        stroke-width: 0.6rem;
        transform-origin: 50% 50%;
        transform: rotate(-90deg);
    }

        .loading-progress circle:last-child {
            stroke: #1b6ec2;
            stroke-dasharray: calc(3.141 * var(--blazor-load-percentage, 0%) * 0.8), 500%;
            transition: stroke-dasharray 0.05s ease-in-out;
        }

.loading-progress-text {
    position: absolute;
    text-align: center;
    font-weight: bold;
    inset: calc(20vh + 3.25rem) 0 auto 0.2rem;
}

    .loading-progress-text:after {
        content: var(--blazor-load-percentage-text, "Loading");
    }

code {
    color: #c02d76;
}

.form-floating > .form-control-plaintext::placeholder, .form-floating > .form-control::placeholder {
    color: var(--bs-secondary-color);
    text-align: end;
}

.form-floating > .form-control-plaintext:focus::placeholder, .form-floating > .form-control:focus::placeholder {
    text-align: start;
}

/* ---- Image2Brick app styles ---------------------------------------------- */

/* Theme tokens. Light is the default; dark is applied either automatically from
   the OS setting (when no explicit choice is stored) or via [data-theme] which
   the header toggle sets. The accent red stays constant across both themes. */
:root {
    --bg: #f3f4f6;
    --surface: #ffffff;
    --surface-2: #fafbfc;
    --text: #1f2530;
    --text-muted: #5b626d;
    --text-faint: #8a8f98;
    --border: #e4e7eb;
    --border-strong: #d0d5db;
    --border-subtle: #eef0f2;
    --border-dashed: #cdd2d8;
    --accent: #d11f2a;
    --accent-hover: #b3171f;
    --success: #237841;
    --danger: #b3171f;
    --canvas-bg: #ffffff;
    --overlay-bg: rgba(255, 255, 255, 0.55);
    --pill-bg: #1f2530;
    --pill-text: #ffffff;
    --btn-dark-bg: #1f2530;
    --btn-dark-text: #ffffff;
    --shadow: rgba(16, 24, 40, 0.08);
}

/* Dark palette, shared by the auto and manual paths below. */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #14171c;
        --surface: #1e232b;
        --surface-2: #262c35;
        --text: #e6e9ee;
        --text-muted: #aab2bd;
        --text-faint: #7f8893;
        --border: #2c333d;
        --border-strong: #3a424e;
        --border-subtle: #272d36;
        --border-dashed: #3a424e;
        --accent: #ec3b45;
        --accent-hover: #ff5a63;
        --success: #5fcf86;
        --danger: #ff6b73;
        --canvas-bg: #0f1216;
        --overlay-bg: rgba(15, 18, 22, 0.6);
        --pill-bg: #e6e9ee;
        --pill-text: #14171c;
        --btn-dark-bg: #30363f;
        --btn-dark-text: #e6e9ee;
        --shadow: rgba(0, 0, 0, 0.4);
    }
}

:root[data-theme="dark"] {
    --bg: #14171c;
    --surface: #1e232b;
    --surface-2: #262c35;
    --text: #e6e9ee;
    --text-muted: #aab2bd;
    --text-faint: #7f8893;
    --border: #2c333d;
    --border-strong: #3a424e;
    --border-subtle: #272d36;
    --border-dashed: #3a424e;
    --accent: #ec3b45;
    --accent-hover: #ff5a63;
    --success: #5fcf86;
    --danger: #ff6b73;
    --canvas-bg: #0f1216;
    --overlay-bg: rgba(15, 18, 22, 0.6);
    --pill-bg: #e6e9ee;
    --pill-text: #14171c;
    --btn-dark-bg: #30363f;
    --btn-dark-text: #e6e9ee;
    --shadow: rgba(0, 0, 0, 0.4);
}

body {
    background: var(--bg);
    color: var(--text);
}

/* Themed scrollbars — replace the OS/browser default so scrolling panels match
   the surface in both light and dark. Tokens flip with the theme automatically. */
* {
    scrollbar-width: thin;
    scrollbar-color: var(--border-strong) transparent;
}

::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border-strong);
    border-radius: 999px;
    border: 2px solid var(--surface);   /* padding ring so the thumb reads as a slim pill */
    background-clip: padding-box;
}

    ::-webkit-scrollbar-thumb:hover {
        background: var(--text-faint);
        border: 2px solid var(--surface);
        background-clip: padding-box;
    }

::-webkit-scrollbar-corner {
    background: transparent;
}

/* Header tools (language selector + theme toggle), pinned to the top-right. */
.hero-tools {
    position: absolute;
    top: 0;
    right: 0;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* Theme toggle button in the header. */
.theme-toggle {
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 999px;
    height: 2.5rem;
    padding: 0 0.9rem 0 0.7rem;
    font-size: 0.9rem;
    font-weight: 600;
    line-height: 1;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    box-shadow: 0 1px 2px var(--shadow);
}

/* Language selector (custom flag dropdown beside the theme toggle). */
.lang-select {
    position: relative;
}

.lang-toggle {
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 999px;
    height: 2.5rem;
    padding: 0 0.7rem;
    font-size: 0.9rem;
    font-weight: 600;
    line-height: 1;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    box-shadow: 0 1px 2px var(--shadow);
}

    .lang-toggle:hover {
        border-color: var(--border-strong);
    }

.lang-flag {
    width: 1.35rem;
    height: 1rem;
    object-fit: cover;
    border-radius: 2px;
    flex: 0 0 auto;
    display: block;
}

.lang-caret {
    font-size: 0.7rem;
    opacity: 0.7;
    line-height: 1;
}

/* Invisible full-screen layer that closes the menu on an outside click. */
.lang-backdrop {
    position: fixed;
    inset: 0;
    z-index: 50;
}

.lang-menu {
    position: absolute;
    top: calc(100% + 0.4rem);
    right: 0;
    z-index: 51;
    margin: 0;
    padding: 0.3rem;
    list-style: none;
    min-width: 11rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.6rem;
    box-shadow: 0 6px 20px var(--shadow);
    max-height: 70vh;
    overflow-y: auto;
}

.lang-option {
    width: 100%;
    background: none;
    border: 0;
    color: var(--text);
    text-align: left;
    cursor: pointer;
    border-radius: 0.4rem;
    padding: 0.45rem 0.55rem;
    font-size: 0.92rem;
    display: flex;
    align-items: center;
    gap: 0.55rem;
}

    .lang-option:hover {
        background: var(--surface-alt, rgba(127, 127, 127, 0.12));
    }

    .lang-option.is-active {
        font-weight: 700;
    }

    .theme-toggle:hover {
        border-color: var(--border-strong);
    }

    .theme-toggle-icon {
        font-size: 1.1rem;
        line-height: 1;
    }

/* On narrow screens drop the text and fall back to the round icon-only button. */
@media (max-width: 520px) {
    .theme-toggle {
        width: 2.5rem;
        padding: 0;
        justify-content: center;
        gap: 0;
    }

    .theme-toggle-label {
        display: none;
    }

    /* Language selector collapses to flag + caret only. */
    .lang-name {
        display: none;
    }
}

.app-root {
    position: relative;
    min-height: 70vh;
}

.drop-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 50;
    background: rgba(209, 31, 42, 0.08);
    border: 4px dashed var(--accent);
    border-radius: 16px;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

    .drop-overlay span {
        background: var(--surface);
        padding: 1rem 1.6rem;
        border-radius: 12px;
        font-size: 1.3rem;
        font-weight: 700;
        color: var(--text);
        box-shadow: 0 6px 24px var(--shadow);
    }

.app-root.dragging .drop-overlay {
    display: flex;
}

.hero {
    position: sticky;
    top: 0;
    z-index: 40;
    background: var(--bg);
    text-align: center;
    padding: 1rem 0 0.8rem;
    box-shadow: 0 6px 14px -10px var(--shadow);
}

.muted-inline {
    color: var(--text-faint);
    font-weight: 400;
    font-size: 0.85em;
}

    .hero h1 {
        font-size: clamp(2.2rem, 6vw, 3.4rem);
        font-weight: 800;
        letter-spacing: -0.02em;
        margin: 0;
    }

/* Logo + wordmark, centred together. */
.brand {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.65rem;
}

.brand-logo {
    width: clamp(1.9rem, 4.5vw, 2.7rem);
    height: auto;
    flex: 0 0 auto;
    filter: drop-shadow(0 1px 2px var(--shadow));
}

    .hero .accent {
        color: var(--accent);
    }

    .hero .tagline {
        color: var(--text-muted);
        margin: 0.4rem 0 0;
        font-size: 1.05rem;
    }

.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 1.1rem 1.25rem;
    box-shadow: 0 1px 2px var(--shadow);
}

.controls {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-top: 0.6rem;
}

.controls-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 1rem;
}

/* Sliders sit on an equal-width grid so a control that wraps to the next row
   keeps the same width as the others rather than stretching to fill. */
.controls-row.sliders {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    align-items: end;
    gap: 0.5rem 1rem;
}

.canvas-wrap {
    position: relative;
    overflow: hidden;   /* clip the canvas while it's translated during a pan drag */
}

.canvas-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--overlay-bg);
    backdrop-filter: blur(1px);
    border-radius: 8px;
}

    .canvas-overlay span {
        background: var(--pill-bg);
        color: var(--pill-text);
        font-weight: 600;
        padding: 0.45rem 0.9rem;
        border-radius: 999px;
        box-shadow: 0 2px 8px var(--shadow);
    }

/* Matches .btn-ghost so the upload control looks like the other action buttons. */
.upload-btn {
    position: relative;
    overflow: hidden;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    background: var(--surface-2);
    border: 1px solid var(--border-strong);
    color: var(--text);
    padding: 0.6rem 1rem;
    border-radius: 10px;
    font-weight: 600;
    box-shadow: 0 1px 2px var(--shadow);
    transition: border-color 0.12s, box-shadow 0.12s, transform 0.06s;
}

    .upload-btn:hover:not(:disabled) {
        border-color: var(--text);
        box-shadow: 0 2px 6px var(--shadow);
    }

    .upload-btn:active:not(:disabled) {
        transform: translateY(1px);
        box-shadow: 0 1px 1px var(--shadow);
    }

/* Reset button under the sliders, since it acts on them. */
.controls-actions {
    display: flex;
    justify-content: center;
    margin-top: 0.55rem;
}

    .controls-actions .btn-ghost {
        flex: 1;
    }

.view-hint {
    font-size: 0.8rem;
    margin: 0.35rem 0 0;
}

/* Tighten the info lines above the canvas so the canvas gets more height. */
.preview > .muted.center {
    margin-bottom: 0.25rem;
}

/* Zoom / Pan controls live in the Controls panel but drive the canvas view,
   so their sliders are green to set them apart from the build sliders. */
/* Dark green so the zoom/pan sliders stand apart from the red build controls.
   The .control qualifier outranks the generic `.control input[type=range]` red rule. */
.control.view-control input[type=range] {
    accent-color: #1b5e20;
}

/* Action buttons under the canvas - kept compact so they fit on one row,
   leaving more vertical room for the canvas itself. */
.canvas-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.3rem;
    justify-content: center;
    margin-top: 0.5rem;
}

/* Bottom row of the preview panel: action buttons on the left, save-status pinned
   to the right on the same line. The buttons stay centred in the space they have. */
.canvas-actions-bar {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    margin-top: 0.5rem;
}

    .canvas-actions-bar .canvas-actions {
        flex: 1;
        min-width: 0;
        margin-top: 0;
    }

    .canvas-actions-bar .save-status {
        flex: 0 0 auto;
        margin: 0;
        text-align: right;
        white-space: nowrap;
    }

    .canvas-actions .btn-ghost,
    .canvas-actions .upload-btn {
        padding: 0.4rem 0.45rem;
        font-size: 0.76rem;
        white-space: nowrap;
    }

    .upload-btn input[type=file] {
        position: absolute;
        inset: 0;
        opacity: 0;
        cursor: pointer;
        width: 100%;
    }

/* Off-screen file input triggered after the "discard changes?" confirm. */
.hidden-file {
    display: none;
}

.control {
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
    min-width: 150px;
    flex: 1;
}

    .control > label {
        line-height: 1.25;
    }

    .control input[type=range] {
        width: 100%;
        accent-color: var(--accent);
    }

/* When no image is loaded the controls are inert - show that clearly. */
input[type=range]:disabled {
    accent-color: var(--border-strong);
    opacity: 0.55;
    cursor: not-allowed;
}

.controls.is-disabled label {
    color: var(--text-faint);
}

.controls-hint {
    font-weight: 600;
    color: var(--text-muted);
    margin: 0 0 1rem;
}

/* −/+ steppers flanking each slider. */
.slider-row {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

    .slider-row input[type=range] {
        flex: 1;
        min-width: 0;
    }

.step {
    flex: 0 0 auto;
    width: 1.55rem;
    height: 1.55rem;
    padding: 0;
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--text);
    border-radius: 8px;
    font-size: 1rem;
    line-height: 1;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

    .step:hover:not(:disabled) {
        border-color: var(--text);
    }

    .step:disabled {
        opacity: 0.4;
        cursor: default;
    }

/* The three buttons sit below the sliders, in one centered horizontal row. */
.controls-row.actions {
    margin-top: 0.5rem;
    gap: 0.4rem;
    justify-content: center;
}

    .controls-row.actions > * {
        flex: 1 1 0;
        min-width: 0;
        justify-content: center;
        text-align: center;
        white-space: nowrap;
        padding: 0.5rem 0.35rem;
        font-size: 0.82rem;
    }

.check {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    user-select: none;
}

.btn-primary {
    background: var(--accent);
    border: none;
    border-radius: 10px;
    padding: 0.6rem 1rem;
    font-weight: 600;
    color: #fff;
}

    .btn-primary:hover:not(:disabled) {
        background: var(--accent-hover);
    }

    .btn-primary:disabled {
        opacity: 0.5;
    }

/* Dark-green confirm button (the dialog's Save). */
.btn-save {
    background: #15692f;
    border: none;
    border-radius: 10px;
    padding: 0.6rem 1rem;
    font-weight: 600;
    color: #fff;
}

    .btn-save:hover:not(:disabled) {
        background: #0f4f24;
    }

    .btn-save:disabled {
        opacity: 0.5;
    }

.status {
    text-align: center;
    font-weight: 600;
    color: var(--text-muted);
}

    .status.error {
        color: var(--danger);
    }

.hint {
    text-align: center;
    color: var(--text-faint);
    margin-top: 2rem;
}

.result {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1.25rem;
    margin-top: 0.5rem;
}

@media (min-width: 820px) {
    .result {
        grid-template-columns: 1.4fr 1fr;
        align-items: start;
    }

    /* The multi-panel layout stays stacked until there's room for the columns. */
    .result.four-col {
        grid-template-columns: 1fr;
    }
}

/* Controls | Canvas | BOM | Saved - the canvas (1fr) takes the slack.
   The whole workspace fits the viewport; each column scrolls on its own
   instead of growing the page. */
@media (min-width: 1280px) {
    html, body { overflow: hidden; }           /* no page scroll */

    .app-shell { height: 100vh; }
    .content {
        display: flex;
        flex-direction: column;
        min-height: 0;
        overflow: hidden;
    }
    .app-root {
        display: flex;
        flex-direction: column;
        flex: 1;
        min-height: 0;
    }

    /* Trim the three side columns so the preview (1fr) claims the slack width. */
    .result.four-col {
        grid-template-columns: minmax(205px, 235px) minmax(0, 1fr) minmax(225px, 280px) minmax(225px, 260px);
        align-items: stretch;
        flex: 1;
        min-height: 0;
    }

    /* Each column is its own scroll area, full row height. */
    .result.four-col > .panel {
        max-height: 100%;
        overflow-y: auto;
    }

    /* BOM: pin the header/buttons/save, scroll only the colour list. */
    .result.four-col > .panel.bom {
        display: flex;
        flex-direction: column;
        overflow: hidden;
    }
    .panel.bom .bom-table {
        flex: 1;
        min-height: 0;
        max-height: none;
    }

    /* Canvas scales to fit its panel rather than overflowing/scrolling. */
    .preview {
        display: flex;
        flex-direction: column;
        overflow: hidden;
    }
    /* The canvas-wrap takes all the slack height in the preview column. The
       canvas hugs the top (right under the header) so any leftover space from a
       non-filling aspect ratio falls below it rather than as a gap up top. */
    .preview .canvas-wrap {
        flex: 1;
        min-width: 0;
        min-height: 0;
        aspect-ratio: auto !important;
        display: flex;
        align-items: flex-start;
        justify-content: center;
    }
    .preview .canvas-wrap canvas {
        max-width: 100%;
        max-height: 100%;
        width: auto;
        height: auto;
    }
    /* Empty state: the drop zone fills the whole panel so it's large and centred
       (its own content stays centred), instead of a small box pinned to the top. */
    .preview .canvas-wrap .ph-canvas {
        align-self: stretch;
        width: 100%;
        height: 100%;
    }
}

/* In the narrow controls column the sliders should stack one per row. */
.controls-panel .controls-row.sliders {
    grid-template-columns: 1fr;
}

/* Panel headings: centered, bold, slightly larger. */
.controls-panel > h2,
.preview > h2,
.bom h2,
.saved-panel .saved-head > h2 {
    text-align: center;
    font-weight: 800;
    font-size: 1.3rem;
    margin: 0 0 0.6rem;
}

/* Saved layouts as a column: heading centered above its tools, cards stack. */
.saved-panel .saved-grid {
    grid-template-columns: 1fr;
}

.saved-panel .saved-head {
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
}

    .saved-panel .saved-head > h2 {
        width: 100%;
    }

.preview canvas {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
    image-rendering: pixelated;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--canvas-bg);
}

/* ---- Empty-state placeholder (shown before an image is added) ------------ */

.ph-canvas {
    position: relative;
    min-height: 420px;
    border: 2px dashed var(--border-dashed);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    background: var(--surface-2);
    padding: 2rem;
    cursor: pointer;
}

    /* The whole placeholder opens the file dialog. */
    .ph-canvas input[type=file] {
        position: absolute;
        inset: 0;
        width: 100%;
        height: 100%;
        opacity: 0;
        cursor: pointer;
    }

    .ph-canvas:hover {
        border-color: var(--text-faint);
    }

.app-root.dragging .ph-canvas {
    border-color: var(--accent);
    background: rgba(209, 31, 42, 0.05);
}

.ph-icon {
    font-size: 3.4rem;
    line-height: 1;
}

.ph-mosaic {
    width: 3.4rem;
    height: auto;
    display: block;
    margin: 0 auto;
}

.ph-title {
    font-weight: 700;
    color: var(--text-muted);
    margin: 0.9rem 0 0.4rem;
    font-size: 1.3rem;
}

.ph-sub {
    font-size: 0.95rem;
    color: var(--text-faint);
    margin: 0;
}

.bom-head {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 0.25rem;
}

    .bom-head h2 {
        flex: 1 1 100%;   /* title on its own line; buttons wrap below within the panel */
    }

    .bom-head h2 {
        margin: 0;
    }

.muted {
    color: var(--text-faint);
    font-size: 0.85rem;
    margin: 0 0 0.75rem;
}

    .muted.center {
        text-align: center;
    }

/* The colour list scrolls within the panel instead of growing the page.
   Right padding keeps the Qty column clear of the scrollbar. */
.bom-table {
    max-height: 60vh;
    overflow-y: auto;
    padding-right: 0.6rem;
    scrollbar-gutter: stable;
}

/* Export buttons pinned to the bottom of the BOM panel. */
.bom-actions {
    display: flex;
    gap: 0.5rem;
    position: sticky;
    bottom: 0;
    margin-top: auto;
    padding-top: 0.75rem;
    background: var(--surface);
    border-top: 1px solid var(--border-subtle);
}

    .bom-actions > * {
        flex: 1 1 auto;
        justify-content: center;
        text-align: center;
    }

.bom table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.92rem;
}

.bom-table thead th {
    position: sticky;
    top: 0;
    background: var(--surface);
    z-index: 1;
}

.bom th, .bom td {
    text-align: left;
    padding: 0.4rem 0.25rem;
    border-bottom: 1px solid var(--border-subtle);
}

.bom .num {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

.bom .key {
    text-align: center;
    font-weight: 700;
    color: var(--text);
    width: 2.5rem;
}

.swatch {
    display: inline-block;
    width: 14px;
    height: 14px;
    border-radius: 3px;
    border: 1px solid rgba(127, 127, 127, 0.5);
    margin-right: 0.5rem;
    vertical-align: -2px;
}

/* ---- Save bar + buttons -------------------------------------------------- */

.btn-ghost {
    background: var(--surface-2);
    border: 1px solid var(--border-strong);
    border-radius: 9px;
    padding: 0.45rem 0.8rem;
    font-weight: 600;
    color: var(--text);
    cursor: pointer;
    box-shadow: 0 1px 2px var(--shadow);
    transition: border-color 0.12s, box-shadow 0.12s, transform 0.06s;
}

    .btn-ghost:hover:not(:disabled) {
        border-color: var(--text);
        box-shadow: 0 2px 6px var(--shadow);
    }

    .btn-ghost:active:not(:disabled) {
        transform: translateY(1px);
        box-shadow: 0 1px 1px var(--shadow);
    }

    .btn-ghost:disabled {
        opacity: 0.5;
    }

.btn-link-danger {
    background: none;
    border: none;
    color: var(--danger);
    cursor: pointer;
    font-weight: 600;
    padding: 0.45rem 0.4rem;
}

    .btn-link-danger:hover {
        text-decoration: underline;
    }

/* Stacked confirm for a saved card: "Delete?" label above a Yes/Cancel row. */
.confirm-stack {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.4rem;
    width: 100%;
    font-weight: 600;
    color: var(--text-muted);
}

    .confirm-stack .confirm-row {
        display: flex;
        gap: 0.5rem;
    }

.btn-danger-solid {
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 7px;
    padding: 0.45rem 0.9rem;
    font-weight: 600;
    cursor: pointer;
}

    .btn-danger-solid:hover {
        background: var(--accent-hover);
    }

.save-bar {
    display: flex;
    gap: 0.5rem;
    margin: 0.25rem 0 0.5rem;
}

    .save-bar input {
        flex: 1;
        background: var(--surface);
        color: var(--text);
        border: 1px solid var(--border-strong);
        border-radius: 9px;
        padding: 0.45rem 0.7rem;
        font-size: 0.95rem;
    }

        .save-bar input::placeholder {
            color: var(--text-faint);
        }

.save-msg {
    margin: 0 0 0.5rem;
    font-size: 0.85rem;
    color: var(--success);
}

    .save-msg.error {
        color: var(--danger);
    }

/* Always-visible save state line - fixed height so it never shifts the panel. */
.save-status {
    margin: 0.35rem 0 0;
    font-size: 0.85rem;
    font-weight: 600;
    min-height: 1.2em;
}

    .save-status.saved {
        color: var(--success);
    }

    .save-status.unsaved {
        color: #e08a1e;
    }

    .save-status.error {
        color: var(--danger);
    }

/* "Name this layout" overlay shown on Save click.
   (Custom class names - not Bootstrap's .modal, which is display:none by default.) */
.dlg-backdrop {
    position: fixed;
    inset: 0;
    z-index: 60;
    background: rgba(0, 0, 0, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.dlg {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 1.25rem;
    width: 100%;
    max-width: 360px;
    box-shadow: 0 12px 40px var(--shadow);
}

    .dlg h3 {
        margin: 0 0 0.75rem;
        font-size: 1.25rem;
        font-weight: 700;
        text-align: center;
    }

.dlg-text {
    margin: 0;
    color: var(--text-muted);
    text-align: center;
    line-height: 1.4;
}

.dlg-input {
    width: 100%;
    border: 1px solid var(--border-strong);
    border-radius: 9px;
    padding: 0.5rem 0.7rem;
    background: var(--surface);
    color: var(--text);
    font-size: 0.95rem;
}

.dlg-actions {
    display: flex;
    justify-content: center;
    gap: 0.5rem;
    margin-top: 1rem;
}

/* ---- Saved layouts gallery ----------------------------------------------- */

.saved {
    margin-top: 0;   /* now a grid column; top aligns with the other panels */
}

.saved-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    flex-wrap: wrap;
    margin-bottom: 0.9rem;
}

    .saved-head h2 {
        font-size: 1.2rem;
        margin: 0;
    }

.saved-tools {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    width: 100%;
}

    .saved-tools > .btn-ghost,
    .saved-tools > .import-btn {
        flex: 1;
        min-width: 0;            /* allow equal shrink instead of overflow/clipping */
        justify-content: center;
        text-align: center;
        white-space: nowrap;
        padding-left: 0.5rem;    /* trim side padding so labels fit the narrow column */
        padding-right: 0.5rem;
    }

.import-btn {
    position: relative;
    overflow: hidden;
    display: inline-flex;
    align-items: center;
    cursor: pointer;
}

    .import-btn input[type=file] {
        position: absolute;
        inset: 0;
        opacity: 0;
        cursor: pointer;
        width: 100%;
    }

.saved-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 1rem;
}

.saved-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

    .saved-card img {
        width: 100%;
        aspect-ratio: 1 / 1;
        object-fit: contain;
        background: var(--surface-2);
        image-rendering: pixelated;
        cursor: pointer;
        border-bottom: 1px solid var(--border-subtle);
    }

.saved-meta {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    padding: 0.6rem 0.7rem 0.2rem;
}

    .saved-meta strong {
        font-size: 0.95rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .saved-meta span {
        color: var(--text-faint);
        font-size: 0.78rem;
    }

.saved-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.2rem 0.5rem 0.5rem;
}