/* === Reset & Base === */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    /* Atelier — warm paper & ink palette */
    --bg: #f5efe2;
    --surface: #fbf7ec;
    --surface-hover: #f0e8d2;
    --border: #c9bca0;
    --border-soft: #ddd0b2;
    --text: #2a211a;
    --text-muted: #8a7860;
    --text-soft: #5a4c3e;
    --accent: #9b2f2a;
    --accent-hover: #7a2220;
    --accent-soft: #f0dada;
    --selection: #6f8a5f;
    --selection-soft: #e1ead6;
    --danger: #9b2f2a;
    --cell-base: 22px;
    --zoom: 1;
    --cell-size: calc(var(--cell-base) * var(--zoom));
    --cell-gap: 1px;
    /* Fonts */
    --serif: 'Source Serif 4', 'Source Serif Pro', Georgia, serif;
    --sans: 'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
    --mono: 'JetBrains Mono', 'IBM Plex Mono', ui-monospace, monospace;
}

/* ── Global custom radio / checkbox accent ──────────────────────────
   Override the default browser blue with the app's warm accent colour
   so radio buttons and checkboxes feel cohesive across all overlays. */
input[type="radio"],
input[type="checkbox"] {
    accent-color: var(--accent);
}

body {
    font-family: var(--sans);
    background: var(--bg);
    color: var(--text);
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* === Masthead === */
.masthead {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 16px 32px 14px;
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 20px;
}

.masthead-brand {
    display: flex;
    align-items: center;
    gap: 14px;
}

.masthead-mark {
    flex-shrink: 0;
    display: block;
    width: 32px;
    height: 32px;
    object-fit: contain;
    /* The icon already has rounded corners baked in, but a tiny radius here
       smooths the few stray edge pixels that PNG decode/scale can leave. */
    border-radius: 4px;
}

.masthead h1 {
    font-family: var(--serif);
    font-size: 1.55rem;
    font-weight: 500;
    color: var(--text);
    letter-spacing: -0.4px;
    line-height: 1;
}

.masthead-subtitle {
    font-family: var(--mono);
    font-size: 0.68rem;
    letter-spacing: 2.5px;
    text-transform: uppercase;
    color: var(--text-muted);
    padding-left: 4px;
}

.masthead-meta {
    display: flex;
    align-items: baseline;
    gap: 14px;
}

.masthead-saved {
    font-family: var(--mono);
    font-size: 0.65rem;
    letter-spacing: 1.5px;
    text-transform: uppercase;
    color: var(--selection);
    white-space: nowrap;
}

.masthead-saved.unsaved {
    color: var(--text-muted);
}

/* Sits in the masthead alongside Saved/Unsaved. Reads as a quiet chrome
   action — small mono caps, hairline border, walnut on hover — so it
   doesn't compete with the workbench buttons in the panels below. */
.masthead-install {
    font-family: var(--mono);
    font-size: 0.65rem;
    letter-spacing: 1.5px;
    text-transform: uppercase;
    color: var(--text-muted);
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 3px;
    padding: 4px 10px;
    cursor: pointer;
    white-space: nowrap;
    align-self: center;
}
.masthead-install:hover {
    color: var(--text);
    border-color: var(--text-soft);
    background: var(--surface-hover);
}

/* === Status Bar (thin strip at bottom of workbench) === */
.status-bar {
    flex-shrink: 0;
    padding: 6px 32px;
    border-top: 1px solid var(--border);
    background: var(--surface);
    display: flex;
    align-items: center;
    gap: 14px;
    font-family: var(--mono);
    font-size: 0.65rem;
    color: var(--text-muted);
    letter-spacing: 0.5px;
}

.status-sep {
    color: var(--border);
}

.status-zoom {
    background: transparent;
    border: none;
    padding: 0 4px;
    font-family: var(--mono);
    font-size: 0.72rem;
    color: var(--text-muted);
    cursor: pointer;
    letter-spacing: 0.3px;
}
.status-zoom:hover {
    color: var(--text);
    background: transparent;
    border: none;
}

.status-autosave {
    margin-left: auto;
    color: var(--text-muted);
}

/* === Tab Strip (journey navigation) === */
.tab-strip {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 32px;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.tabs {
    display: flex;
}

.tab {
    display: flex;
    align-items: baseline;
    gap: 8px;
    padding: 12px 22px;
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    border-radius: 0;
    cursor: pointer;
    color: var(--text-muted);
    font-family: var(--serif);
    font-size: 0.95rem;
    transition: color 0.15s, border-color 0.15s;
}

.tab:hover {
    color: var(--text-soft);
    background: transparent;
    border-bottom-color: var(--border-soft);
}

.tab.active {
    color: var(--text);
    border-bottom-color: var(--selection);
}

.tab-num {
    font-family: var(--mono);
    font-size: 0.65rem;
    color: var(--text-muted);
    letter-spacing: 0.5px;
}

.tab.active .tab-num {
    color: var(--selection);
}

.tab-label {
    font-style: italic;
}

.tab-actions {
    display: flex;
    align-items: center;
    gap: 16px;
}

.tab-link-btn {
    background: transparent;
    border: none;
    border-bottom: 1px dotted var(--border);
    border-radius: 0;
    padding: 2px 2px 1px;
    font-family: var(--serif);
    font-size: 0.8rem;
    color: var(--text-soft);
    cursor: pointer;
}

.tab-link-btn:hover {
    color: var(--text);
    background: transparent;
    border-bottom-color: var(--text-soft);
    border-bottom-style: solid;
}

.pattern-name-input {
    background: transparent;
    color: var(--text);
    border: none;
    border-bottom: 1px dotted var(--border);
    padding: 4px 6px;
    font-family: var(--serif);
    font-size: 0.9rem;
    font-style: italic;
    width: 180px;
}

.pattern-name-input::placeholder {
    color: var(--text-muted);
    font-style: italic;
}

.pattern-name-input:focus {
    outline: none;
    border-bottom-color: var(--text-soft);
    border-bottom-style: solid;
}

/* === Buttons === */
button {
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 6px 14px;
    border-radius: 3px;
    cursor: pointer;
    font-family: var(--sans);
    font-size: 0.85rem;
    transition: background 0.15s, border-color 0.15s;
}

button:hover {
    background: var(--surface-hover);
    border-color: var(--text-soft);
}

button:active {
    transform: scale(0.97);
}

button.btn-primary {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
    font-weight: 600;
}

button.btn-primary:hover {
    background: var(--accent-hover);
    color: #fff;
}

button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* === Toolbar === */
.toolbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 10px 20px;
    display: flex;
    align-items: center;
    gap: 20px;
    flex-wrap: wrap;
}

.tool-group {
    display: flex;
    align-items: center;
    gap: 6px;
}

.tool-group label {
    font-size: 0.8rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    white-space: nowrap;
}

.input-label {
    font-size: 0.7rem;
    color: var(--text-muted);
    font-weight: 600;
}

input[type="number"], select {
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 5px 8px;
    border-radius: 3px;
    width: 60px;
    font-family: var(--sans);
    font-size: 0.85rem;
}

input[type="number"]:focus, select:focus {
    outline: none;
    border-color: var(--text-soft);
}

select {
    width: auto;
}

/* === Tool Buttons === */
.tool-btn {
    padding: 5px 12px;
    font-size: 0.8rem;
}

.tool-btn.active {
    background: var(--text);
    color: var(--surface);
    border-color: var(--text);
}

/* === Color Palette === */
.color-palette {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.color-swatch {
    width: 26px;
    height: 26px;
    border-radius: 3px;
    border: 1px solid var(--border);
    box-sizing: border-box;
    cursor: pointer;
    transition: transform 0.1s, box-shadow 0.15s;
    position: relative;
}

.color-swatch:hover {
    transform: scale(1.12);
}

.color-swatch.active {
    border-color: var(--border);
    box-shadow: inset 0 0 0 2px var(--bg), inset 0 0 0 3px var(--border);
}

.color-swatch-custom {
    width: 26px;
    height: 26px;
    border-radius: 3px;
    border: 1px dashed var(--border);
    cursor: pointer;
    background: transparent;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    font-size: 1.1rem;
    padding: 0;
}

.color-swatch-custom:hover {
    border-color: var(--accent);
    color: var(--accent);
}

#custom-color-input {
    position: absolute;
    width: 0;
    height: 0;
    opacity: 0;
    pointer-events: none;
}

/* === Workbench (three-column body) === */
.workbench {
    flex: 1;
    display: grid;
    grid-template-columns: 290px 1fr 300px;
    min-height: 0;
    overflow: hidden;
}

.panel {
    background: var(--surface);
    padding: 18px 16px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.panel-left {
    border-right: 1px solid var(--border);
}

.panel-right {
    border-left: 1px solid var(--border);
}

.panel-section {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.panel-section-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    margin-bottom: 2px;
}

.panel-section-title {
    font-family: var(--sans);
    font-size: 0.68rem;
    letter-spacing: 2px;
    text-transform: uppercase;
    font-weight: 600;
    color: var(--text-muted);
}

.panel-section-hint {
    font-family: var(--mono);
    font-size: 0.62rem;
    color: var(--text-muted);
}

.panel-row {
    display: flex;
    gap: 6px;
}

.panel-row-two {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
}

.panel-row-three {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 6px;
}

.panel-row button, .panel-btn-wide {
    width: 100%;
    padding: 8px 10px;
    font-size: 0.82rem;
    font-family: var(--serif);
    text-align: center;
}

/* (Erase Stitch is now a tool-btn in the Tool section — uses .tool-btn.active.) */

.panel-select {
    width: 100%;
}

/* === R1 RS/WS picker (flat mode only) === */
.first-row-picker {
    margin-top: 10px;
    padding: 10px;
    background: var(--bg);
    border: 1px solid var(--border-soft);
    border-radius: 3px;
}

.first-row-picker.hidden {
    display: none;
}

.first-row-label {
    font-family: var(--mono);
    font-size: 0.62rem;
    color: var(--text-muted);
    margin-bottom: 8px;
    text-transform: lowercase;
    letter-spacing: 0.3px;
}

.first-row-options {
    display: flex;
    gap: 8px;
}

.first-row-option {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 8px;
    border: 1px solid var(--border);
    border-radius: 3px;
    cursor: pointer;
    background: transparent;
    transition: background 0.15s, border-color 0.15s;
}

.first-row-option:has(input:checked) {
    background: var(--selection-soft);
    border-color: var(--selection);
}

.first-row-option input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.first-row-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    border: 1px solid var(--border);
    flex-shrink: 0;
}

.first-row-option:has(input:checked) .first-row-dot {
    border-color: var(--selection);
    background: var(--selection);
}

.first-row-name {
    font-family: var(--serif);
    font-size: 0.8rem;
    color: var(--text);
    flex: 1;
}

.first-row-code {
    font-family: var(--mono);
    font-size: 0.65rem;
    color: var(--text-muted);
    letter-spacing: 0.5px;
}

/* Dimensions grid */
.dimensions-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.dimension-field {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.dimension-label {
    font-family: var(--mono);
    font-size: 0.62rem;
    color: var(--text-muted);
}

.dimension-field input[type="number"] {
    width: 100%;
    font-family: var(--serif);
    font-size: 1.05rem;
    padding: 6px 8px;
}

.dimension-suffix {
    font-family: var(--mono);
    font-size: 0.6rem;
    color: var(--text-muted);
    text-align: right;
}

/* Canvas area */
.canvas-area {
    overflow: auto;
    padding: 28px 24px;
    display: flex;
    flex-direction: column;
    /* 'safe center' keeps the chart centred while NOT clipping when it's wider
       than the container — 'center' alone causes negative offset that hides
       scrollable content. */
    align-items: safe center;
    gap: 14px;
    background:
        repeating-linear-gradient(0deg, transparent 0 39px, rgba(42,33,26,0.025) 39px 40px);
    transition: padding-bottom 0.2s;
}

/* When knit mode is active, the fixed knit bar sits over the bottom edge of
   the workbench — which is where the canvas-area's horizontal scrollbar
   renders. Shrink the workbench so the scrollbar moves up above the overlay. */
body.knit-active .workbench {
    padding-bottom: 148px;
}

.grid-wrapper {
    display: inline-flex;
    gap: 0;
    user-select: none;
    background: #fff;
    padding: 16px;
    border: 1px solid var(--border);
    box-shadow: 0 1px 0 rgba(0,0,0,0.06), 0 16px 40px rgba(40,30,20,0.08);
    flex-shrink: 0;
    position: relative;
}

/* === Row/Column Numbers === */
/* Sticky label rails: stay pinned to the corresponding edge of the canvas
   viewport so the user can always read which row/stitch they're on, even
   when scrolled deep into a 1000×1000 chart. The scroll ancestor for sticky
   is .canvas-area; an opaque background and z-index keep the labels above
   the canvas they overlap when scrolled. */
.row-numbers {
    display: flex;
    flex-direction: column;
    padding-top: calc(18px + 4px + var(--cell-gap));
    gap: var(--cell-gap);
    flex-shrink: 0;
    position: sticky;
    z-index: 5;
    background: #fff;
}
/* Negative offsets reach past the .canvas-area's 28px×24px padding so the
   rails sit flush with the pane's BOX edge, not the padded content edge. */
.row-numbers-left { left: -24px; }
.row-numbers-right { right: -24px; }

.row-number {
    height: var(--cell-size);
    display: flex;
    align-items: center;
    font-family: var(--mono);
    font-size: 0.65rem;
    color: var(--text-muted);
    min-width: 46px;
    white-space: nowrap;
}

.row-numbers-left .row-number {
    justify-content: flex-end;
    padding-right: 4px;
}

.row-numbers-right .row-number {
    justify-content: flex-start;
    padding-left: 4px;
}

.row-number .row-arrow {
    font-size: 0.85rem;
    line-height: 1;
    margin-left: 1px;
}

.row-number.row-hidden {
    visibility: hidden;
}

.col-number.col-hidden {
    visibility: hidden;
}

.row-number.row-rs {
    color: var(--accent);
}

.row-number.row-ws {
    color: var(--text-muted);
}

.col-numbers {
    display: flex;
    gap: var(--cell-gap);
    padding-left: 0;
    position: sticky;
    z-index: 4; /* below row labels so corner overlap reads cleanly */
    background: #fff;
}

.col-numbers-top {
    margin-bottom: 4px;
    top: -28px;
}

.col-numbers-bottom {
    margin-top: 4px;
    bottom: -28px;
}

.col-number {
    width: var(--cell-size);
    text-align: center;
    font-size: 0.65rem;
    font-family: var(--mono);
    color: var(--text-muted);
    height: 18px;
    line-height: 18px;
}

/* === Grid === */
.grid-col-wrapper {
    position: relative;
}

.grid-canvas-wrapper {
    position: relative;
    display: inline-block;
    /* Clip the viewport canvases (base/overlay are inside #grid-container,
       stitch-overlay is a sibling here) so their transform translation
       doesn't extend .canvas-area's scroll overflow. See grid-view.js
       ensureLayers for the full rationale. */
    overflow: hidden;
}

.stitch-overlay {
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none;
    z-index: 5;
}

.grid-container {
    display: grid;
    gap: var(--cell-gap);
    background: var(--border-soft);
    border: 1px solid var(--border);
    line-height: 0;
}

.grid-cell {
    width: var(--cell-size);
    height: var(--cell-size);
    background: var(--surface);
    cursor: crosshair;
    transition: background 0.05s;
}

/* Canvas-backed grid: the cursor lives on the container now, since there
   are no per-cell divs to hover. */
#grid-container { cursor: crosshair; }
.grid-base-canvas, .grid-overlay-canvas {
    display: block;
    image-rendering: pixelated;
}

.grid-cell:hover {
    outline: 2px solid var(--selection);
    outline-offset: -2px;
    z-index: 1;
}

.grid-cell.painted {
    /* color set via inline style */
}

/* .selected class kept for JS logic only — no visual style on individual cells */

#selection-box {
    position: absolute;
    border: 2px dashed var(--selection);
    pointer-events: none;
    z-index: 6;
    border-radius: 2px;
    box-sizing: border-box;
}

.grid-cell.paste-ghost {
    opacity: 0.5;
    outline: 1px dashed #fff;
    outline-offset: -1px;
    z-index: 3;
}

/* === Selection Actions === */
.selection-actions {
    margin-top: 8px;
    padding: 10px;
    background: var(--bg);
    border: 1px dashed var(--border-soft);
    border-radius: 3px;
}

.selection-actions-label {
    font-family: var(--serif);
    font-style: italic;
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-bottom: 6px;
}

.selection-actions-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px;
}

.selection-actions button {
    font-size: 0.72rem;
    padding: 4px 6px;
    font-family: var(--mono);
}

/* === Padding Inputs === */
.padding-input {
    width: 45px !important;
}

.padding-label {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* === Modal === */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(42, 33, 26, 0.5);
    z-index: 100;
    justify-content: center;
    align-items: center;
}

/* Stitch editor can be opened ON TOP of the Stitch Gallery overlay (right-
   click → Edit, or the "Add Stitch Type" button inside the gallery footer).
   With identical z-index, painting order falls to DOM order — and the editor
   modal sits earlier in the markup, so it would render under the gallery.
   Bumping it one tier higher keeps it above any other modal that's open. */
#stitch-editor-modal {
    z-index: 110;
}

.modal-overlay.open {
    display: flex;
}

.modal {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    width: 90vw;
    max-width: 900px;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
}

.modal.modal-narrow {
    max-width: 500px;
}

.modal.random-modal {
    max-width: 640px;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}

/* Single heading recipe used by every overlay (stitch editor, gallery,
   confirm dialogs, preview/instructions/image/random modals). Mirrors the
   stitch editor's original look so all overlays read as siblings. */
.modal-header h2 {
    font-family: var(--serif);
    font-size: 1.2rem;
    font-weight: 500;
    letter-spacing: -0.2px;
    color: var(--text);
    margin: 0;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0 4px;
}

.modal-close:hover {
    color: var(--text);
}

.modal-controls {
    padding: 12px 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    gap: 16px;
    align-items: center;
    flex-wrap: wrap;
}

.print-icons-toggle {
    font-family: var(--serif);
    font-size: 0.85rem;
    color: var(--ink);
    display: inline-flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    user-select: none;
}
.print-icons-toggle input {
    margin: 0;
}

.preview-canvas-wrapper {
    flex: 1;
    overflow: auto;
    padding: 20px;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    min-height: 300px;
}

#preview-canvas {
    border: 1px solid var(--border);
    image-rendering: pixelated;
}

/* === Stitch Palette (now inside left panel) === */
.stitch-palette {
    display: grid;
    /* minmax(0, 1fr) ignores the columns' intrinsic min-content widths so a
       wider label on one side (e.g. "Erase / Stitch type") can't pull its
       column wider than the other. */
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
}

.stitch-tile-row {
    display: flex;
    gap: 4px;
}

.stitch-tile {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 8px;
    padding: 6px 10px 6px 6px;
    border: 1px solid var(--border);
    border-radius: 3px;
    background: var(--bg);
    cursor: pointer;
    transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
    min-height: 48px;
}

/* Icon (canvas or erase glyph) sits left, bordered to read as a chip */
.stitch-tile-canvas,
.stitch-tile .stitch-erase-icon {
    flex-shrink: 0;
    border: 1px solid var(--border);
    border-radius: 3px;
    background: var(--surface);
}
.stitch-tile .stitch-erase-icon {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
}

/* Label column — main label + optional muted sub-label, all left-aligned */
.stitch-labels {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    line-height: 1.15;
    gap: 1px;
}
.stitch-label-main {
    text-align: left;
    /* Truncate codes wider than the tile (e.g. a custom "ALongStitchCode")
       instead of letting them bleed into adjacent tiles or the chart. */
    max-width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.stitch-tile .stitch-label-sub {
    font-family: var(--serif);
    font-size: 0.72rem;
    font-weight: 500;
    /* Italic on the sub-name (e.g. "knit" under "K") to match the convention
       in the editor's auto-fill prompts and the printed pattern legend. */
    font-style: italic;
    color: var(--text-muted);
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    text-align: left;
    white-space: nowrap;
    font-style: italic;
}

/* === No-Stitch tile: two rows — [icon | labels] on top, [checkbox] below === */
/* Slightly tighter horizontal padding than the other tiles so the mono-font
   "Select Cells" label fits on a single line. */
.stitch-tile[data-stitch="no-stitch"] {
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    padding: 6px 4px;
}
.stitch-tile[data-stitch="no-stitch"] .no-st-top {
    display: flex;
    align-items: center;
    gap: 8px;
}
.stitch-tile[data-stitch="no-stitch"] > .no-st-checkbox {
    /* Left-align the checkbox with the No St icon (which sits at the tile's
       content-box left edge) and give a comfortable gap to "All BG". */
    margin: 0;
    padding: 0;
    justify-content: flex-start;
    gap: 8px;
}

.stitch-tile:hover {
    background: var(--surface-hover);
    border-color: var(--text-soft);
}

.stitch-tile.active {
    border-color: var(--selection);
    background: var(--selection-soft);
    box-shadow: inset 0 0 0 1px var(--selection);
}

.stitch-tile span {
    font-size: 0.8rem;
    color: var(--text);
    font-weight: 600;
    font-family: var(--serif);
}

.stitch-tile.active span {
    color: var(--selection-deep, #4f6a42);
}

.stitch-tile-canvas {
    border-radius: 3px;
}

.no-st-checkbox {
    font-family: var(--mono);
    font-size: 0.7rem !important;
    color: var(--text-muted) !important;
    display: flex;
    align-items: center;
    gap: 3px;
    cursor: pointer;
    margin-top: 1px;
}

.no-st-checkbox input {
    width: 12px;
    height: 12px;
    accent-color: var(--accent);
    cursor: pointer;
}

.stitch-erase-icon {
    font-size: 1.2rem;
    color: var(--danger);
}

.grid-cell.stitch-purl {
    background: #243352 !important;
}

.grid-cell.cable-ghost {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
    z-index: 4;
}

/* === Image Import Modal === */
.drop-zone {
    border: 2px dashed var(--border);
    border-radius: 8px;
    padding: 40px 20px;
    text-align: center;
    cursor: pointer;
    transition: border-color 0.2s, background 0.2s;
    margin-bottom: 16px;
}

.drop-zone:hover,
.drop-zone.drag-over {
    border-color: var(--text-soft);
    background: var(--surface-hover);
}

.drop-zone p {
    color: var(--text-muted);
    font-size: 0.95rem;
    margin: 0;
}

.drop-zone .drop-zone-sub {
    font-size: 0.8rem;
    margin-top: 6px;
}

.checkbox-input {
    width: 16px;
    height: 16px;
    accent-color: var(--accent);
    cursor: pointer;
}

.modal.modal-image {
    max-width: 640px;
}

.image-layout {
    display: flex;
    gap: 20px;
}

.image-form-col {
    flex: 1;
    min-width: 0;
}

.image-preview-col {
    display: flex;
    flex-direction: column;
    gap: 12px;
    align-items: center;
    width: 220px;
    flex-shrink: 0;
}

.image-preview-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.image-preview-box canvas {
    border: 1px solid var(--border);
    image-rendering: pixelated;
    width: 200px;
    height: 200px;
    object-fit: contain;
}

.preview-label {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* === Random Pattern Modal === */
.modal-body {
    padding: 20px;
    overflow: auto;
}

.form-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 14px;
}

.form-row > label {
    font-size: 0.85rem;
    color: var(--text-muted);
    min-width: 70px;
    text-align: right;
}

.form-row select,
.form-row input[type="number"] {
    flex: 1;
    max-width: 200px;
}

.form-actions {
    justify-content: flex-start;
    padding-left: 80px;
    gap: 8px;
    margin-bottom: 16px;
}

.density-slider {
    flex: 1;
    max-width: 160px;
    accent-color: var(--accent);
}

#random-density-value {
    font-family: var(--mono);
    font-size: 0.78rem;
    color: var(--text-muted);
    min-width: 32px;
}

.random-color-picks {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.random-swatch {
    width: 24px;
    height: 24px;
    border-radius: 3px;
    border: 2px solid transparent;
    cursor: pointer;
    transition: border-color 0.15s, transform 0.1s;
    opacity: 0.4;
}

.random-swatch:hover {
    transform: scale(1.15);
    opacity: 0.7;
}

.random-swatch.picked {
    border-color: #fff;
    opacity: 1;
    box-shadow: 0 0 4px rgba(255, 255, 255, 0.3);
}

.random-layout {
    display: flex;
    gap: 20px;
}

.random-form-col {
    flex: 1;
    min-width: 0;
}

.random-preview-col {
    display: flex;
    flex-direction: column;
    gap: 12px;
    align-items: center;
    flex-shrink: 0;
}

#random-preview-canvas {
    border: 1px solid var(--border);
    image-rendering: pixelated;
    width: 200px;
    height: 200px;
    object-fit: contain;
}

/* === Instructions Modal === */
.instructions-content {
    flex: 1;
    overflow: auto;
    padding: 20px;
    min-height: 300px;
}

.instructions-content pre {
    font-family: 'Consolas', 'Monaco', monospace;
    font-size: 0.85rem;
    color: var(--text);
    white-space: pre-wrap;
    word-wrap: break-word;
    line-height: 1.6;
    background: var(--bg);
    padding: 16px;
    border-radius: 4px;
    border: 1px solid var(--border);
    outline: none;
    cursor: text;
}

.instructions-content pre:focus {
    border-color: var(--accent);
}

.instructions-edit-hint {
    font-family: var(--mono);
    font-size: 0.7rem;
    color: var(--text-muted);
    font-style: italic;
}

/* Instructions tab buttons: match the workbench's serif button look so
   they don't read as a different visual register from the rest of the
   panel buttons. */
#instructions-modal .modal-controls button {
    font-family: var(--serif);
    font-size: 0.82rem;
}

.instructions-edited {
    color: #f1c40f;
    font-size: 0.7rem;
    font-style: italic;
}

/* === Active Knitting Mode === */
.knit-overlay {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 90;
    pointer-events: none;
}

.knit-bar {
    background: var(--surface);
    border-top: 2px solid var(--accent);
    padding: 8px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    pointer-events: all;
}

.knit-bar-left, .knit-bar-right {
    display: flex;
    align-items: center;
    gap: 8px;
}

.knit-bar-center {
    flex: 1;
    text-align: center;
}

.knit-row-label {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--accent);
    min-width: 80px;
    text-align: center;
}

/* Top bar — shows the row instructions, centred */
.knit-direction {
    display: inline-block;
    font-family: var(--mono);
    font-size: 0.95rem;
    color: var(--text);
    line-height: 1.4;
    padding: 0 8px;
    text-align: center;
    max-width: 100%;
}

/* Bottom strip — shows the working direction, left-aligned */
.knit-instruction {
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 6px 16px;
    font-family: var(--sans);
    font-size: 0.82rem;
    color: var(--text-muted);
    line-height: 1.4;
    text-align: left;
    pointer-events: all;
}

.grid-cell.knit-active-row {
    outline: 2px solid var(--accent) !important;
    outline-offset: -1px;
    position: relative;
    z-index: 6;
}

/* Red highlight bar that extends past the grid edges for visibility */
.knit-row-bar {
    position: absolute;
    left: -28px;
    right: -28px;
    pointer-events: none;
    border-top: 2px solid var(--accent);
    border-bottom: 2px solid var(--accent);
    background: rgba(155, 47, 42, 0.10);
    display: none;
    z-index: 5;
}
.knit-row-bar.visible {
    display: block;
}
/* Diamond tabs on each end so the bar reads even at the far edges */
.knit-row-bar::before,
.knit-row-bar::after {
    content: '';
    position: absolute;
    top: 50%;
    width: 12px;
    height: 12px;
    transform: translateY(-50%) rotate(45deg);
    background: var(--accent);
}
.knit-row-bar::before { left: -8px; }
.knit-row-bar::after  { right: -8px; }

/* === Fullscreen Knitting View === */
.knit-fullscreen-view {
    position: fixed;
    inset: 0;
    background: var(--bg);
    z-index: 200;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px;
}

.knit-fs-header {
    display: flex;
    align-items: center;
    gap: 30px;
    margin-bottom: 40px;
}

.knit-fs-nav {
    background: var(--surface);
    color: var(--accent);
    border: 2px solid var(--accent);
    font-size: 2rem;
    padding: 10px 24px;
    border-radius: 8px;
    cursor: pointer;
}

.knit-fs-nav:hover {
    background: var(--accent);
    color: #fff;
}

.knit-fs-info {
    text-align: center;
}

.knit-fs-row {
    display: block;
    font-size: 2.5rem;
    font-weight: 700;
    color: var(--accent);
}

.knit-fs-direction {
    display: block;
    font-size: 1.2rem;
    color: var(--text-muted);
    margin-top: 4px;
}

.knit-fs-instruction {
    font-family: 'Consolas', 'Monaco', monospace;
    font-size: 1.5rem;
    color: var(--text);
    line-height: 2;
    text-align: center;
    max-width: 90vw;
    word-wrap: break-word;
}

.knit-fs-progress {
    margin-top: 30px;
    font-size: 0.9rem;
    color: var(--text-muted);
}

.knit-fs-footer {
    margin-top: 40px;
    display: flex;
    gap: 12px;
}

.knit-fs-footer button {
    padding: 10px 24px;
    font-size: 1rem;
}

/* === Print View === */
.print-view {
    display: none;
}

/* === Print Styles === */
@media print {
    /* Consistent margin on every page (the .print-view padding only applies
       to the first page in browser pagination). */
    @page {
        margin: 10mm;
    }

    body > *:not(.print-view) {
        display: none !important;
    }

    .print-view {
        display: block !important;
        color: #000;
        background: #fff;
    }

    /* One panel per page when the chart is split for size or pagination.
       The first panel sits directly under the title; subsequent panels start
       a new page. */
    .print-panel {
        margin-bottom: 8mm;
    }
    .print-panel-break {
        break-before: page;
        page-break-before: always;
    }
    .print-panel-label {
        font-size: 9pt;
        color: #555;
        margin-bottom: 2mm;
        text-align: center;
    }

    /* Rasterised user-stitch icons inside a print td. The td has fixed
       width/height set inline; the img fills it. */
    .print-grid td.icon-cell {
        padding: 0;
        line-height: 0;
    }

    /* Small corner marker showing the colour symbol on stitched + colored
       cells, so B&W prints don't lose the colour identifier when the centre
       is occupied by a stitch glyph or icon. */
    .print-grid td.has-color-mark {
        position: relative;
    }
    .print-color-mark {
        position: absolute;
        top: 0;
        right: 1px;
        font-size: 5pt;
        font-weight: bold;
        line-height: 1;
        pointer-events: none;
        text-shadow:
             0  0   1px #fff,
             0  0   1px #fff;
    }
    .print-stitch-icon {
        display: block;
        width: 100%;
        height: 100%;
        object-fit: contain;
        image-rendering: -webkit-optimize-contrast;
    }

    .print-title {
        font-size: 18pt;
        text-align: center;
        margin-bottom: 4mm;
        color: #000;
    }

    .print-info {
        text-align: center;
        font-size: 10pt;
        color: #555;
        margin-bottom: 6mm;
    }

    .print-grid-wrapper {
        /* Multiple panels need to stack vertically (one per page); flex-row
           would lay them side by side and overflow the page width. */
        display: flex;
        flex-direction: column;
        align-items: center;
        margin-bottom: 8mm;
    }

    .print-grid {
        border-collapse: collapse;
        /* Fixed layout = column widths come from the declared td width, not
           the widest content. Without this a long user-stitch code stretches
           its column wider than the chart's other columns. */
        table-layout: fixed;
    }

    .print-grid th {
        font-size: 7pt;
        font-weight: normal;
        color: #888;
        padding: 1px 3px;
        text-align: center;
    }

    .print-grid td {
        width: 7mm;
        height: 7mm;
        border: 0.5px solid #ccc;
        text-align: center;
        font-size: 8pt;
        font-weight: bold;
        vertical-align: middle;
        overflow: hidden;
    }

    .print-grid td.painted-cell {
        border-color: #999;
    }

    .print-grid td.cable-cell {
        font-size: 7pt;
        font-weight: bold;
        background: #e8f4f8 !important;
        color: #000 !important;
        border-color: #666;
    }

    .print-grid td.stitch-cell {
        font-size: 10pt;
    }

    .print-grid td.stitch-purl-cell {
        background: #f0f0f0 !important;
        color: #555 !important;
    }

    /* No-stitch cells fill with a hatched ::before that prints reliably —
       print-color-adjust: exact tells modern browsers to honor the background
       on print rather than stripping it. */
    .print-grid td.no-stitch-cell,
    .print-legend-swatch.no-stitch-cell {
        position: relative;
        border-color: #999 !important;
    }
    .print-grid td.no-stitch-cell::before,
    .print-legend-swatch.no-stitch-cell::before {
        content: "";
        position: absolute;
        inset: 0;
        background: repeating-linear-gradient(
            45deg,
            #aaa,
            #aaa 0.6mm,
            #e8e8e8 0.6mm,
            #e8e8e8 1.2mm
        );
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    .print-grid .row-header {
        font-size: 7pt;
        font-weight: normal;
        color: #888;
        border: none;
        padding-right: 3px;
        text-align: right;
        width: auto;
        white-space: nowrap;
    }

    .print-legend {
        margin-top: 6mm;
    }

    .print-legend h3 {
        font-size: 11pt;
        margin-bottom: 3mm;
        color: #000;
    }

    .print-legend-grid {
        display: flex;
        flex-wrap: wrap;
        gap: 4mm;
    }

    .print-legend-item {
        display: flex;
        align-items: center;
        gap: 2mm;
        font-size: 9pt;
    }

    .print-legend-swatch {
        width: 6mm;
        height: 6mm;
        border: 0.5px solid #999;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        font-size: 8pt;
        font-weight: bold;
    }

    .print-instructions {
        margin-top: 8mm;
        page-break-before: auto;
    }

    .print-instructions pre {
        font-family: 'Consolas', monospace;
        font-size: 8pt;
        line-height: 1.4;
        white-space: pre-wrap;
        color: #000;
    }
}

/* === Add Stitch Type editor === */
/* Typography matches the main workbench: Source Serif for the modal title,
   Inter for form labels/inputs, uppercase-sans for small headings. */

.stitch-editor-modal {
    max-width: 1040px;
    width: 95vw;
}
/* Stitch editor heading style now lives on the shared .modal-header h2 rule. */
.stitch-editor-body {
    display: grid;
    grid-template-columns: minmax(360px, 1fr) 380px;
    gap: 14px;
    padding: 18px;
    /* `clip` (vs `hidden`) prevents the browser from scrolling this element
       at all — including the auto-scroll-into-view that fires when typing
       into the live text overlay at large font sizes, which used to drag
       the toolstrip off the top of the modal. */
    overflow: clip;
}
.stitch-editor-left {
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.stitch-editor-right {
    min-width: 0;
    display: grid;
    grid-template-columns: 140px 1fr;
    gap: 10px 14px;
    align-content: start;
    align-items: center;
}
.stitch-editor-toolstrip {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 8px 10px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 4px;
}
.stitch-editor-tool-row {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: center;
}
.stitch-editor-tool-group {
    display: flex;
    gap: 4px;
}
.st-tool-btn {
    padding: 5px 12px;
    /* Match the main workbench panel buttons: Source Serif at 0.82rem */
    font-family: var(--serif);
    font-size: 0.82rem;
    font-weight: 400;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    border-radius: 3px;
    cursor: pointer;
}
.st-tool-btn:hover { background: var(--surface-hover); }
.st-tool-btn.active {
    background: var(--text);
    color: var(--surface);
    border-color: var(--text);
}
.stitch-editor-colors {
    display: flex;
    gap: 5px;
    align-items: center;
}
.st-swatch {
    width: 24px; height: 24px;
    border-radius: 50%;
    border: 1px solid rgba(42,33,26,0.2);
    cursor: pointer;
    padding: 0;
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.st-swatch.active {
    outline: 2px solid var(--text);
    outline-offset: 1px;
}
.st-swatch.st-swatch-eraser {
    background: var(--surface);
    border: 1px dashed var(--text-muted);
    color: var(--text-muted);
    font-size: 0.85rem;
    font-family: var(--sans);
}
.st-swatch.st-swatch-eraser.active {
    color: var(--text);
    border-color: var(--text);
}
.stitch-editor-stroke {
    display: flex;
    align-items: center;
    gap: 6px;
    padding-left: 8px;
    border-left: 1px solid var(--border);
    font-family: var(--sans);
    font-size: 0.78rem;
    color: var(--text-muted);
}
.stitch-editor-stroke input[type=range] { width: 100px; }
.stitch-editor-fill-group {
    display: flex;
    align-items: center;
    gap: 6px;
    padding-left: 8px;
    border-left: 1px solid var(--border);
    font-family: var(--sans);
    font-size: 0.78rem;
    color: var(--text-muted);
}
.stitch-editor-fill-label {
    text-transform: uppercase;
    letter-spacing: 1px;
    font-weight: 600;
}
.stitch-editor-fill {
    display: flex;
    gap: 4px;
    align-items: center;
}
/* Reuse .st-swatch sizing/active rules for fill swatches; the "None" tile
   shows a diagonal slash on the paper colour. */
.st-swatch.st-swatch-none {
    background: var(--surface);
    color: var(--text-muted);
    position: relative;
}
.st-swatch.st-swatch-none::before {
    content: '';
    position: absolute;
    inset: 4px;
    background: linear-gradient(
        to top right,
        transparent calc(50% - 1px),
        var(--text-muted) calc(50% - 0.5px),
        var(--text-muted) calc(50% + 0.5px),
        transparent calc(50% + 1px)
    );
    border-radius: 50%;
}
.stitch-editor-actions {
    display: flex;
    gap: 4px;
    margin-left: auto;
}
.stitch-editor-actions button {
    padding: 5px 12px;
    font-family: var(--serif);
    font-size: 0.82rem;
    font-weight: 400;
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 3px;
    cursor: pointer;
}
.stitch-editor-actions button:hover { background: var(--surface-hover); }
.stitch-editor-text-row {
    padding-top: 2px;
    border-top: 1px dashed var(--border-soft);
    align-items: center;
}
.stitch-editor-text-row-hint {
    flex: 1;
    font-family: var(--sans);
    font-size: 0.75rem;
    color: var(--text-muted);
    line-height: 1.35;
}
.stitch-editor-text-size {
    display: flex;
    align-items: center;
    gap: 6px;
    padding-left: 8px;
    border-left: 1px solid var(--border);
    font-family: var(--sans);
    font-size: 0.78rem;
    color: var(--text-muted);
}
.stitch-editor-text-size input[type=range] { width: 110px; }
#st-text-commit {
    padding: 4px 14px;
    font-family: var(--sans);
    font-size: 0.78rem;
    border: 1px solid var(--text);
    background: var(--text);
    color: var(--surface);
    border-radius: 3px;
    cursor: pointer;
}
#st-text-commit:hover { background: #1a1410; }

/* Live text-entry overlay sits over the drawing canvas until committed */
.st-text-overlay {
    position: absolute;
    left: 5%;
    top: 5%;
    /* Max bounds match the drawing board itself (with a small margin) so a
       character at the slider's max font size has room to render without
       being clipped. The box's natural height tracks the text height. */
    max-width: 90%;
    max-height: 90%;
    min-height: 80px;
    min-width: 80px;
    display: flex;
    flex-direction: column;
    border: 1.5px dashed var(--accent);
    border-radius: 4px;
    background: rgba(251, 247, 236, 0.55);
    box-shadow: 0 0 0 1px rgba(155, 47, 42, 0.15);
    z-index: 2;
    user-select: none;
    /* `clip` (not `hidden`) so the browser can't scroll this element to
       keep the caret in view when typing into a contenteditable that's
       taller than the overlay — which would scroll the drag handle off
       the top and persist across reopens. */
    overflow: clip;
}
.st-text-overlay-handle {
    /* Pin the drag bar at its natural height — without this the flex layout
       lets it shrink to zero when the contenteditable is taller than the
       overlay's max-height (i.e. whenever the slider's font size + a few
       characters exceed the drawing-board height). */
    flex: 0 0 auto;
    padding: 3px 8px;
    font-family: var(--sans);
    font-size: 0.7rem;
    letter-spacing: 1.5px;
    text-transform: uppercase;
    color: var(--accent);
    background: rgba(155, 47, 42, 0.08);
    border-bottom: 1px dashed var(--accent);
    cursor: move;
    user-select: none;
    border-top-left-radius: 3px;
    border-top-right-radius: 3px;
    flex-shrink: 0;
}
.st-text-overlay-content {
    /* Sized by its own content (text size) so the overlay grows naturally
       up to the parent's max-height. text-align: center handles horizontal
       centering; multi-line text stacks naturally because this is a normal
       block (NOT flex — Chromium/Webkit have known contenteditable+flex
       bugs that swallowed the Enter key entirely). position:relative
       anchors the empty-state placeholder (::before below) so it can sit
       out of layout flow. */
    position: relative;
    flex: 0 0 auto;
    padding: 8px 10px;
    text-align: center;
    outline: none;
    font-family: var(--serif);
    font-size: 72px;
    line-height: 1;
    color: var(--text);
    user-select: text;
    cursor: text;
    overflow: hidden;
    word-break: break-word;
    display: block;
}
.st-text-overlay-content:empty::before {
    content: 'Type here…';
    color: var(--text-muted);
    opacity: 0.6;
    /* Float the placeholder out of layout flow at a sane fixed size so it
       can't balloon the content box when the slider sets a huge font-size
       (was forcing the textbox to overflow the wrapper and triggering a
       focus-scroll that pushed the editor toolstrip off the modal). */
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    white-space: nowrap;
    font-size: 14px;
    line-height: 1;
}
.stitch-editor-canvas-wrapper {
    position: relative;
    /* Paper + faint quarter grid drawn as CSS backgrounds. Keeping the grid
       out of the canvas means the eraser tool (destination-out) can punch
       pixels back to transparent without hiding the grid. */
    background-color: #fbf7ec;
    background-image:
        linear-gradient(to right, rgba(42,33,26,0.08) 1px, transparent 1px),
        linear-gradient(to bottom, rgba(42,33,26,0.08) 1px, transparent 1px);
    background-size: 25% 25%;
    background-position: 0 0;
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 4px;
    align-self: stretch;
}
#stitch-editor-canvas {
    display: block;
    width: 100%;
    height: auto;
    aspect-ratio: 1 / 1;
    cursor: crosshair;
    touch-action: none;
    background: transparent;
}
/* Right-side form: two-column grid, labels right-aligned against inputs */
.form-grid-row {
    display: contents;
}
.stitch-editor-right label,
.stitch-editor-right .form-grid-label {
    justify-self: end;
    align-self: center;
    font-family: var(--sans);
    font-size: 0.78rem;
    font-weight: 600;
    letter-spacing: 1.2px;
    text-transform: uppercase;
    color: var(--text-muted);
    /* Long labels like "Detailed instructions (optional)" wrap across
       multiple lines so they don't bleed into the drawing column. */
    white-space: normal;
    text-align: right;
    line-height: 1.3;
}
.form-grid-row-align-top > label,
.form-grid-row-align-top > .form-grid-label {
    align-self: start;
    padding-top: 6px;
}
.form-label-opt {
    color: var(--text-muted);
    font-weight: 400;
    text-transform: none;
    letter-spacing: 0;
}
.stitch-editor-right input[type=text],
.stitch-editor-right textarea {
    font-family: var(--sans);
    font-size: 0.9rem;
    padding: 7px 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    border-radius: 3px;
    width: 100%;
}
.stitch-editor-right textarea {
    resize: vertical;
    line-height: 1.45;
}
.stitch-editor-right input[type=text]:focus,
.stitch-editor-right textarea:focus {
    outline: none;
    border-color: var(--text);
}
.stitch-editor-preview {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 4px;
}
/* Override the right-side form's right-aligned uppercase label rules so the
   multi-cell toggle reads as body copy, in the same serif/size as the
   workbench buttons, left-aligned within the input column. Selector is
   double-classed so it beats the .stitch-editor-right label rule. */
.stitch-editor-right .st-multicell-toggle {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    font-family: var(--sans);
    font-size: 0.75rem;
    font-weight: 400;
    line-height: 1.4;
    text-transform: none;
    letter-spacing: 0;
    text-align: left;
    color: var(--text-muted);
    cursor: pointer;
    justify-self: stretch;
}
.st-multicell-text {
    flex: 1;
    text-align: left;
}
.st-multicell-toggle input { margin: 0; flex: 0 0 auto; }
.st-row-effect {
    border: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    justify-self: stretch;
    min-width: 0;
    width: 100%;
}
.stitch-editor-right .st-re-option {
    display: flex;
    align-items: center;
    gap: 6px;
    font-family: var(--sans);
    font-size: 0.75rem;
    font-weight: 400;
    line-height: 1.4;
    text-transform: none;
    letter-spacing: 0;
    text-align: left;
    color: var(--text-muted);
    flex-wrap: wrap;
    cursor: pointer;
    align-self: stretch;
    width: 100%;
}
.st-re-option input[type=radio] { margin: 0; flex: 0 0 auto; }
.st-re-n {
    width: 32px;
    padding: 1px 2px;
    font-family: var(--sans);
    font-size: 0.8rem;
    border: 1px solid var(--border);
    border-radius: 3px;
    background: #fff;
    text-align: center;
    -moz-appearance: textfield;
}
.st-re-n::-webkit-outer-spin-button,
.st-re-n::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.st-re-n:disabled { opacity: 0.4; }
#st-preview-canvas {
    width: 40px;
    height: 40px;
    background: #fbf7ec;
    border: 1px solid var(--border);
    border-radius: 3px;
}
.stitch-editor-preview-hint {
    font-family: var(--sans);
    font-size: 0.75rem;
    color: var(--text-muted);
    line-height: 1.4;
}
.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 12px 20px;
    border-top: 1px solid var(--border);
}
.modal-footer button {
    /* Exact match for the workbench .panel-btn-wide typography (+ Add Stitch Type, Preview pattern, etc.) */
    padding: 8px 16px;
    font-family: var(--serif);
    font-size: 0.82rem;
    font-weight: 400;
    text-align: center;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    border-radius: 3px;
    cursor: pointer;
}
.modal-footer button.btn-primary {
    background: var(--text);
    color: var(--surface);
    border-color: var(--text);
}
.modal-footer button:hover { background: var(--surface-hover); }
.modal-footer button.btn-primary:hover { background: #1a1410; }

/* === Stitch Gallery overlay === */
.modal.gallery-modal { width: 720px; max-width: 92vw; }
/* Heading style now shared with all other overlays via .modal-header h2. */
.gallery-body { padding: 18px 20px; }
/* Body copy mirrors the .dimension-label / .panel-section-hint style — same
   mono family the workbench uses for "Height" / "Width" / row hints. */
.gallery-hint {
    margin: 0 0 12px;
    font-family: var(--mono);
    font-size: 0.72rem;
    line-height: 1.6;
    color: var(--text-muted);
}
.gallery-search-row {
    margin: 0 0 10px;
}
.gallery-search {
    width: 100%;
    padding: 7px 10px;
    font-family: var(--mono);
    font-size: 0.78rem;
    color: var(--ink);
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 4px;
    box-sizing: border-box;
}
.gallery-search:focus {
    outline: none;
    border-color: var(--ink);
}
.gallery-empty {
    grid-column: 1 / -1;
    margin: 0;
    padding: 24px 8px;
    text-align: center;
    font-family: var(--mono);
    font-size: 0.78rem;
    color: var(--text-muted);
}

/* === Sizing calculator modal === */
.sizing-body { padding: 18px 20px; }
.sizing-hint {
    margin: 0 0 14px;
    font-family: var(--mono);
    font-size: 0.72rem;
    line-height: 1.6;
    color: var(--text-muted);
}
.sizing-fieldset {
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 10px 14px 14px;
    margin: 0 0 12px;
}
.sizing-fieldset legend {
    padding: 0 6px;
    font-family: var(--mono);
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
}
.sizing-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 6px 0;
}
.sizing-label {
    flex: 1;
    font-family: var(--mono);
    font-size: 0.78rem;
    color: var(--ink);
}
.sizing-row input[type="number"] {
    width: 80px;
    padding: 5px 8px;
    font-family: var(--mono);
    font-size: 0.82rem;
    color: var(--ink);
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 4px;
    box-sizing: border-box;
    text-align: right;
}
.sizing-select {
    padding: 5px 8px;
    font-family: var(--mono);
    font-size: 0.78rem;
    color: var(--ink);
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 4px;
    box-sizing: border-box;
}
.sizing-unit {
    width: auto;
    min-width: 60px;
}
.sizing-result {
    margin-top: 14px;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-left: 3px solid var(--accent);
    border-radius: 4px;
    background: var(--bg);
    font-family: var(--mono);
    font-size: 0.82rem;
    line-height: 1.6;
    color: var(--ink);
}
.sizing-result-row strong { font-weight: 600; }
.sizing-result-meta {
    color: var(--text-muted);
    font-size: 0.74rem;
    margin-left: 6px;
}
.sizing-result-warn { border-left-color: var(--accent-warn, #c8392d); }
.sizing-result-warning {
    margin-top: 8px;
    color: #c8392d;
    font-size: 0.74rem;
}
.sizing-footer {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    padding: 12px 20px;
    border-top: 1px solid var(--border);
}

/* === Garment pattern generator modal === */
.modal-garment { max-width: 720px; }
.garment-body {
    padding: 18px 20px;
    max-height: 72vh;
    overflow-y: auto;
}
.garment-hint {
    margin: 0 0 14px;
    font-family: var(--mono);
    font-size: 0.76rem;
    color: var(--text-muted);
    line-height: 1.55;
}
.garment-layout {
    display: flex;
    gap: 20px;
}
.garment-form {
    flex: 1;
    min-width: 0;
}
.garment-preview-col {
    width: 220px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 12px;
    position: sticky;
    top: 0;
    align-self: flex-start;
}
.garment-preview-canvas-wrap {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 8px;
    display: flex;
    justify-content: center;
}
#garment-preview-canvas { display: block; }
.garment-result {
    font-size: 0.82rem;
    line-height: 1.55;
    color: var(--text-muted);
}
.garment-result-title {
    font-weight: 600;
    color: var(--text);
    margin-bottom: 4px;
    font-size: 0.88rem;
}
.garment-radio-group {
    display: flex;
    gap: 12px;
    align-items: center;
    flex-wrap: wrap;
}
.garment-radio-group label {
    display: flex;
    align-items: center;
    gap: 4px;
    font-family: var(--mono);
    font-size: 0.78rem;
    color: var(--ink, var(--text));
    cursor: pointer;
    white-space: nowrap;
}
.garment-footer {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    padding: 12px 20px;
    border-top: 1px solid var(--border);
}

/* 4-column tile grid. Each tile mirrors the palette's .stitch-tile look so
   the gallery feels like a richer view of the same component. */
.gallery-list {
    display: grid;
    /* minmax(0, 1fr) lets columns shrink below intrinsic content width so
       long stitch codes (P4into1, K5into1) ellipsis-truncate inside the tile
       instead of forcing the row to overflow horizontally. */
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 6px;
    max-height: 56vh;
    overflow-y: auto;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--bg);
}
.gallery-item {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 8px;
    padding: 6px 10px 6px 6px;
    border: 1px solid var(--border);
    border-radius: 3px;
    background: var(--bg);
    cursor: pointer;
    transition: border-color 0.15s, background 0.15s, box-shadow 0.15s, opacity 0.15s;
    min-height: 48px;
    /* Inactive tiles read as "available but not in use" — muted, but still
       legible. Active tiles override this below. */
    opacity: 0.55;
}
.gallery-item:hover {
    background: var(--surface-hover);
    border-color: var(--text-soft);
    opacity: 0.85;
}
.gallery-item.is-active {
    opacity: 1;
    border-color: var(--selection);
    background: var(--selection-soft);
    box-shadow: inset 0 0 0 1px var(--selection);
}
.gallery-item.is-active:hover {
    background: var(--selection-soft);
}
.gallery-item.is-locked {
    cursor: not-allowed;
}
.gallery-item.is-locked:hover {
    /* Locked stays put — no hover lift, since clicking does nothing. */
    background: var(--selection-soft);
    border-color: var(--selection);
}
.gallery-item-icon,
.gallery-item-glyph {
    flex: 0 0 40px;
    width: 40px;
    height: 40px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 3px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: var(--serif);
    color: var(--text);
}
.gallery-item-text {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    line-height: 1.15;
    gap: 1px;
    overflow: hidden;
}
.gallery-item-code {
    font-family: var(--serif);
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}
.gallery-item.is-active .gallery-item-code {
    color: var(--selection-deep, #4f6a42);
}
.gallery-item-sub {
    font-family: var(--serif);
    font-size: 0.72rem;
    font-weight: 500;
    font-style: italic;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}
.gallery-footer { justify-content: flex-end; }

/* === Toast notifications === */
.toast-stack {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 200;
    pointer-events: none; /* let toasts opt back in below */
    max-width: 90vw;
}
.toast {
    pointer-events: auto;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 6px 8px 16px;
    border-radius: 4px;
    color: var(--text);
    font-family: var(--sans);
    font-size: 0.88rem;
    line-height: 1.4;
    box-shadow: 0 4px 18px rgba(42,33,26,0.15);
    /* Tone-specific background + border supplied below. */
    border: 1px solid var(--border);
    background: var(--surface);
}
/* Default / neutral status — soft sage green, matching the workbench's
   "Right side" indicator. */
.toast.toast-info {
    background: var(--selection-soft);
    border-color: var(--selection);
}
/* Errors and validation failures — soft red, matching the masthead accent. */
.toast.toast-error {
    background: var(--accent-soft);
    border-color: #e5b8b3;
}
.toast-text {
    flex: 1;
}
.toast-close {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 1.25rem;
    line-height: 1;
    padding: 0 6px;
    cursor: pointer;
    border-radius: 3px;
}
.toast-close:hover {
    color: var(--text);
    background: rgba(42,33,26,0.08);
}
.toast {
    transition: opacity 0.4s ease, transform 0.4s ease;
}
.toast.toast-fading {
    opacity: 0;
    transform: translateY(8px);
}

/* === Styled confirm dialog (replaces window.confirm) === */
.modal.confirm-modal { width: 460px; max-width: 92vw; }
.confirm-body { padding: 18px 20px; }
.confirm-message {
    margin: 0;
    /* Match the workbench's small-text style (mono, muted). The bullet
       list of stitch ids inside still wraps nicely. */
    font-family: var(--mono);
    font-size: 0.72rem;
    line-height: 1.6;
    color: var(--text-muted);
    /* Newlines and bullet lists in the message render as written. */
    white-space: pre-line;
}
.confirm-footer { padding: 12px 20px; }
.modal-footer button.btn-danger {
    background: #9b2f2a;
    color: #fbf7ec;
    border-color: #9b2f2a;
}
.modal-footer button.btn-danger:hover {
    background: #7d2521;
    border-color: #7d2521;
}

/* Right-click Edit/Delete menu on user-stitch tiles */
.stitch-context-menu {
    position: fixed;
    z-index: 200;
    display: flex;
    flex-direction: column;
    min-width: 120px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 4px;
    box-shadow: 0 4px 16px rgba(42,33,26,0.15);
    padding: 4px 0;
    font-family: var(--sans);
}
.stitch-context-menu button {
    padding: 6px 14px;
    background: transparent;
    border: none;
    text-align: left;
    font: inherit;
    font-size: 0.85rem;
    color: var(--text);
    cursor: pointer;
}
.stitch-context-menu button:hover { background: var(--surface-hover); }
.stitch-context-menu button.danger { color: var(--danger); }
.stitch-context-menu button.danger:hover { background: var(--accent-soft); }
.rail-context-target {
    padding: 6px 14px 4px;
    font-family: var(--sans);
    font-size: 0.7rem;
    font-weight: 600;
    letter-spacing: 1.2px;
    text-transform: uppercase;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
}

/* === Responsive === */
@media (max-width: 768px) {
    header {
        flex-direction: column;
        align-items: flex-start;
    }

    .toolbar {
        flex-direction: column;
        align-items: flex-start;
    }

    :root {
        --cell-base: 18px;
    }

    .stitch-editor-body {
        flex-direction: column;
    }
    #stitch-editor-canvas {
        width: 100%;
        height: auto;
        max-width: 320px;
    }
}

/* === Pattern-import conflict banner === */
.import-conflict-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 8px 16px;
    background: #f4ead6;
    border-bottom: 1px solid #d6c9a4;
    font-family: var(--serif);
    font-size: 0.86rem;
    color: var(--text);
}
.import-conflict-banner[hidden] { display: none; }
.import-conflict-text { flex: 1; }
.import-conflict-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}
.import-conflict-actions button {
    font-family: var(--serif);
    font-size: 0.78rem;
    padding: 4px 12px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    border-radius: 3px;
    cursor: pointer;
}
.import-conflict-actions button.btn-primary {
    background: var(--text);
    color: var(--surface);
    border-color: var(--text);
}
.import-conflict-actions button.btn-primary:hover { background: #1a1410; }
.import-conflict-actions button:hover { background: var(--surface-hover); }
.import-conflict-actions .modal-close {
    width: 26px; height: 26px;
    padding: 0;
    font-size: 1.1rem;
    line-height: 1;
}

/* === Pattern-import review modal === */
.modal.review-modal { width: 640px; max-width: 92vw; }
.review-hint {
    margin: 0 0 14px;
    font-family: var(--mono);
    font-size: 0.72rem;
    line-height: 1.6;
    color: var(--text-muted);
}
.review-hint strong { color: var(--text); font-weight: 600; }
.review-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-height: 60vh;
    overflow-y: auto;
}
.review-row {
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 12px 14px;
    background: var(--surface);
}
.review-row-code {
    font-family: var(--mono);
    font-size: 0.86rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 10px;
    letter-spacing: 0.02em;
    display: flex;
    align-items: center;
    gap: 10px;
}
.review-row-tag {
    font-family: var(--mono);
    font-size: 0.62rem;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: #8a5a1a;
    background: #f4ead6;
    border: 1px solid #d6c9a4;
    padding: 1px 6px;
    border-radius: 2px;
}
.review-row-warning {
    font-family: var(--mono);
    font-size: 0.7rem;
    line-height: 1.5;
    color: #6b4a1a;
    background: #f9f1de;
    border-left: 3px solid #c9a441;
    padding: 6px 10px;
    margin-bottom: 10px;
    border-radius: 2px;
}
.review-row.has-type-mismatch {
    border-color: #d6c9a4;
}
.review-pair {
    display: flex;
    gap: 16px;
    justify-content: stretch;
}
.review-cell {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 10px;
    border: 1px solid var(--border);
    border-radius: 3px;
    background: var(--bg);
    cursor: pointer;
    transition: background 120ms ease, border-color 120ms ease;
}
/* Selected side of the toggle — light-switch style. The unselected side
   stays subdued so the user can see at a glance which version will commit
   when they click Done. */
.review-cell.is-selected {
    background: #ede1c2;
    border-color: var(--text);
}
.review-cell:not(.is-selected) {
    opacity: 0.72;
}
.review-cell:not(.is-selected):hover {
    opacity: 1;
}
.review-cell-label {
    font-family: var(--mono);
    font-size: 0.68rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
}
.review-cell-icon {
    width: 64px;
    height: 64px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 3px;
}
.review-cell button {
    font-family: var(--serif);
    font-size: 0.78rem;
    padding: 4px 12px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    border-radius: 3px;
    cursor: pointer;
}
.review-cell button.btn-primary {
    background: var(--text);
    color: var(--surface);
    border-color: var(--text);
}
.review-cell button:hover { background: var(--surface-hover); }
.review-cell button.btn-primary:hover { background: #1a1410; }
.review-empty {
    text-align: center;
    padding: 20px;
    font-family: var(--mono);
    font-size: 0.78rem;
    color: var(--text-muted);
}
