/* --- Foundational & Root Styles --- */
:root {
    --bg-primary: #0a0a0b;
    --bg-secondary: #111115;
    --glass-bg: rgba(26, 26, 32, 0.7);
    --glass-border: rgba(255, 255, 255, 0.12);
    --text-primary: #ffffff;
    --text-secondary: #a1a1aa;
    --text-muted: #71717a;
    --accent-purple: #8b5cf6;
    --accent-cyan: #06b6d4;
    --accent-cyan-dark: #086d82;
    --gradient-primary: linear-gradient(135deg, var(--accent-purple) 0%, var(--accent-cyan) 100%);
    --radius-xl: 20px;
    --radius-lg: 16px;
    --radius-md: 12px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: 'Inter', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    padding: 1rem;
    position: relative;
    overflow-x: hidden;
    cursor: default;
}

/* --- Brush Cursor --- */
.brush-cursor {
    position: absolute;
    border: 2px dashed white;
    border-radius: 50%;
    pointer-events: none;
    z-index: 9999;
    transform: translate(0, 0);
    mix-blend-mode: difference;
    opacity: 0;
    transition: width 0.1s, height 0.1s, opacity 0.2s;
}

/* --- Background Decorations --- */
.background-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    pointer-events: none;
    background: radial-gradient(circle at 15% 20%, rgba(139, 92, 246, 0.15) 0%, transparent 40%),
                radial-gradient(circle at 85% 75%, rgba(6, 182, 212, 0.15) 0%, transparent 40%);
}

/* --- Main Layout & Card Styles --- */
.app-container {
    width: 100%;
    max-width: 900px;
    margin: 1.5rem auto 0;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.glass-card {
    background: var(--glass-bg);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-xl);
    padding: 2rem;
    transition: all 0.3s ease;
}

.glass-card h2 {
    text-align: center;
    margin-bottom: 0.5rem;
}

.glass-card p {
    text-align: center;
    color: var(--text-secondary);
    margin-bottom: 1.5rem;
    max-width: 600px;
    margin-left: auto;
    margin-right: auto;
}

.hidden {
    display: none !important;
}

/* --- Header --- */
.site-header {
    width: 100%;
    max-width: 900px;
    margin: 0 auto;
}

.site-header .header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
}

.site-header .logo {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
    text-decoration: none;
    flex-grow: 1;
    text-align: center;
}

@media (min-width: 480px) {
    .site-header .logo {
        text-align: left;
    }
}

/* --- Buttons & Controls --- */
.centered-content { text-align: center; }

.action-btn {
    display: inline-block;
    background: var(--gradient-primary);
    color: var(--text-primary);
    text-decoration: none;
    border: none;
    border-radius: var(--radius-md);
    padding: 0.8rem 1.5rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.2s ease;
    font-size: 1rem;
}

.action-btn.small-btn {
    padding: 0.5rem 1rem;
    font-size: 0.85rem;
    background: var(--bg-secondary);
    border: 1px solid var(--glass-border);
}

.action-btn:hover { transform: scale(1.05); }
.action-btn:disabled { background: var(--text-muted); cursor: not-allowed; transform: none; }

.status-message {
    color: var(--text-secondary);
    margin-top: 1rem;
    min-height: 1.2em;
}

.dual-base-summary {
    width: 100%;
    max-width: 430px;
    padding: 1rem;
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-lg);
    background: rgba(0, 0, 0, 0.16);
}

.base-upload-summary {
    margin: 0.6rem auto !important;
    font-size: 0.9rem;
}

.base-upload-summary.loaded {
    color: var(--accent-cyan) !important;
    font-weight: 600;
}

.base-pair-warning {
    margin: 0.75rem auto 0 !important;
    padding: 0.7rem;
    border: 1px solid rgba(251, 191, 36, 0.45);
    border-radius: var(--radius-md);
    background: rgba(251, 191, 36, 0.1);
    color: #fde68a !important;
    font-size: 0.85rem;
    line-height: 1.45;
}

.canvas-container {
    width: 100%;
    margin: 1rem 0;
    background-color: var(--bg-secondary);
    border-radius: var(--radius-lg);
    border: 1px solid var(--glass-border);
    overflow: hidden;
    position: relative; 
}

canvas {
    display: block;
    width: 100%;
    height: auto;
    cursor: crosshair;
}

.controls-bar {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    gap: 1.5rem;
    padding: 1rem;
    background: rgba(0,0,0,0.1);
    border-radius: var(--radius-lg);
}

.control-group {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
}

.control-group label {
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

select {
    background: var(--bg-secondary);
    border: 1px solid var(--glass-border);
    color: var(--text-primary);
    border-radius: var(--radius-md);
    padding: 0.5rem;
    text-align: center;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    cursor: pointer;
}

input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    width: 150px;
    height: 4px;
    background: var(--glass-border);
    border-radius: 5px;
    outline: none;
    cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--gradient-primary);
    border: 2px solid white;
}

/* Color Picker Styling */
input[type="color"]::-webkit-color-swatch-wrapper {
    padding: 0;
}
input[type="color"]::-webkit-color-swatch {
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-md);
}

.label-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    margin-bottom: 0.2rem;
}

.reset-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 1rem;
    padding: 2px 6px;
    transition: all 0.2s ease;
    line-height: 1;
    border-radius: 4px;
}

.reset-btn:hover {
    color: var(--accent-cyan);
    background: rgba(255,255,255,0.05);
    transform: rotate(-90deg);
}

.tool-select {
    display: flex;
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-md);
    overflow: hidden;
}

.tool-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    padding: 0.5rem 1rem;
    cursor: pointer;
    transition: background-color 0.2s;
}

.tool-btn.active {
    background-color: var(--accent-purple);
    color: white;
}

.tool-btn:not(.active):hover {
    background-color: var(--bg-secondary);
}

.final-export { margin-top: 1.5rem; }

/* --- Preview Section Styles --- */
.preview-container {
    display: flex;
    flex-wrap: wrap;
    gap: 1.5rem;
    margin-bottom: 2rem;
    align-items: flex-start;
}
.preview-box {
    flex: 1;
    min-width: 250px;
    text-align: center;
}
.preview-box h3 {
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
    font-weight: 600;
}
.preview-box .canvas-container {
    margin-top: 0;
}
.preview-box .action-btn {
    margin-top: 1rem;
}

/* --- Final base selection --- */
.base-choice-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
    gap: 1rem;
    max-width: 650px;
    margin: 1.5rem auto 0;
}

.base-choice-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.55rem;
    padding: 1.4rem;
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-lg);
    background: rgba(255, 255, 255, 0.04);
    color: var(--text-primary);
    cursor: pointer;
    transition: border-color 0.2s ease, background 0.2s ease, transform 0.2s ease;
}

.base-choice-card:hover {
    border-color: var(--accent-cyan);
    background: rgba(6, 182, 212, 0.08);
    transform: translateY(-2px);
}

.base-choice-title {
    font-size: 1rem;
    font-weight: 700;
}

.base-choice-size {
    color: var(--accent-cyan);
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.base-choice-crop {
    color: var(--text-secondary);
    font-size: 0.85rem;
    font-variant-numeric: tabular-nums;
}

/* --- Scale Base Step --- */
.scale-calc-card {
    background: rgba(0, 0, 0, 0.25);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-lg);
    padding: 1.5rem;
    max-width: 640px;
    margin: 0 auto 0.5rem;
}

.scale-calc-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 1.5rem;
}

.scale-calc-col {
    flex: 1;
    min-width: 200px;
}

.scale-calc-col h3 {
    font-size: 0.95rem;
    color: var(--text-secondary);
    margin-bottom: 0.75rem;
    text-align: center;
    font-weight: 600;
}

.scale-res-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 1rem;
    padding: 0.55rem 0.75rem;
    margin-bottom: 0.4rem;
    background: rgba(255, 255, 255, 0.03);
    border-radius: var(--radius-md);
    border: 1px solid rgba(255, 255, 255, 0.06);
}

.scale-res-label {
    color: var(--text-muted);
    font-size: 0.85rem;
}

.scale-res-value {
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--text-primary);
    font-size: 0.95rem;
}

.scale-res-value.accent {
    color: var(--accent-cyan);
}

.scale-formula {
    margin-top: 1.25rem;
    padding: 1rem;
    border-radius: var(--radius-md);
    border: 1px dashed rgba(6, 182, 212, 0.35);
    background: rgba(6, 182, 212, 0.06);
    text-align: center;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 0.85rem;
    line-height: 1.7;
    color: var(--text-secondary);
}

.scale-formula strong {
    color: var(--accent-cyan);
    font-weight: 700;
}

.scale-factor-summary {
    margin: 1rem 0 0 !important;
    color: var(--text-primary) !important;
    font-weight: 600;
}

.scale-warnings {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    margin-top: 1rem;
    text-align: left;
}

.scale-warnings.hidden {
    display: none !important;
}

.scale-warning {
    padding: 0.75rem 0.9rem;
    border-radius: var(--radius-md);
    font-size: 0.88rem;
    line-height: 1.45;
    border: 1px solid transparent;
}

.scale-warning strong {
    display: block;
    margin-bottom: 0.2rem;
    font-size: 0.9rem;
}

.scale-warning.caution {
    background: rgba(6, 182, 212, 0.1);
    border-color: rgba(6, 182, 212, 0.35);
    color: #a5f3fc;
}

.scale-warning.warning {
    background: rgba(251, 191, 36, 0.12);
    border-color: rgba(251, 191, 36, 0.45);
    color: #fde68a;
}

.scale-warning.danger {
    background: rgba(239, 68, 68, 0.14);
    border-color: rgba(239, 68, 68, 0.45);
    color: #fecaca;
}

.scale-memory-meta {
    margin-top: 0.85rem;
    font-size: 0.82rem;
    color: var(--text-muted);
    text-align: center;
    line-height: 1.5;
}

.scale-memory-meta.hidden {
    display: none !important;
}

.scale-memory-meta strong {
    color: var(--text-secondary);
}

/* Toggle Switch Style */
.toggle-switch { display: inline-flex; align-items: center; gap: 0.75rem; cursor: pointer; font-weight: 600; color: var(--text-secondary); }
.toggle-switch input[type="checkbox"] { display: none; }
.toggle-switch .toggle-knob { position: relative; width: 44px; height: 24px; background-color: var(--bg-secondary); border: 1px solid var(--glass-border); border-radius: 12px; transition: background-color 0.3s ease; }
.toggle-switch .toggle-knob::before { content: ''; position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; background-color: var(--text-secondary); border-radius: 50%; transition: transform 0.3s ease, background-color 0.3s ease; }
.toggle-switch input[type="checkbox"]:checked + .toggle-knob { background-color: var(--accent-cyan); }
.toggle-switch input[type="checkbox"]:checked + .toggle-knob::before { transform: translateX(20px); background-color: white; }

/* --- New Button Groups --- */
.btn-group {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    justify-content: center;
}
.btn-group-vertical {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    align-items: center;
}

/* --- Responsive Styles --- */
@media (max-width: 768px) {
    body { padding: 0.5rem; }
    .app-container { gap: 1rem; }
    .glass-card { padding: 1.5rem; }
    .controls-bar { flex-direction: column; gap: 1rem; }
}

/* --- Editor Layout (Side-by-Side) --- */
.app-container:has(.editor-layout) {
    max-width: 1400px;
}

.editor-layout {
    display: flex;
    gap: 2rem;
    align-items: flex-start;
}

.editor-sidebar {
    flex: 0 0 350px;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    position: sticky;
    top: 1rem;
    max-height: calc(100vh - 2rem);
    overflow-y: auto;
    padding-right: 0.5rem; /* Space for scrollbar */
    scrollbar-width: thin;
    scrollbar-color: var(--glass-border) transparent;
}

.editor-sidebar::-webkit-scrollbar {
    width: 6px;
}
.editor-sidebar::-webkit-scrollbar-thumb {
    background: var(--glass-border);
    border-radius: 3px;
}

.editor-workspace {
    flex: 1;
    min-width: 0;
    position: sticky;
    top: 1rem;
}

.editor-sidebar .controls-bar {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 1rem;
    padding: 1.25rem;
    background: rgba(0, 0, 0, 0.2);
}

.editor-sidebar .control-group {
    align-items: flex-start;
    width: 100%;
}

.editor-sidebar .control-group input[type="range"] {
    width: 100%;
}

.editor-sidebar h2 {
    text-align: left;
    margin-bottom: 0.5rem;
}

.editor-sidebar p {
    text-align: left;
    margin-left: 0;
    margin-right: 0;
    margin-bottom: 1rem;
}

/* Mask Preview Thumbnail */
.mask-preview-card {
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-md);
    padding: 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
}

.mask-preview-card label {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.mask-thumb-container {
    width: 100%;
    aspect-ratio: 16 / 9; /* Default aspect ratio, will be updated by JS */
    background: #000;
    border-radius: var(--radius-md);
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid rgba(255, 255, 255, 0.05);
}

#mask-preview-canvas {
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
    display: block;
    image-rendering: pixelated; /* Keep it sharp */
}

.editor-sidebar .tool-select {
    width: 100%;
}

.editor-sidebar .tool-btn {
    flex: 1;
    text-align: center;
}

.editor-sidebar .toggle-switch {
    justify-content: space-between;
    width: 100%;
    padding: 0.5rem 0;
}

.editor-sidebar .final-export {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.editor-sidebar .final-export .action-btn {
    width: 100%;
}

@media (max-width: 1100px) {
    .editor-layout {
        flex-direction: column-reverse;
        gap: 1.5rem;
    }
    
    .editor-sidebar {
        flex: none;
        width: 100%;
        position: static;
        max-height: none;
        overflow-y: visible;
    }

    .editor-workspace {
        position: static;
    }
    
    .editor-sidebar h2, .editor-sidebar p {
        text-align: center;
    }
    
    .editor-sidebar .controls-bar {
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
        align-items: center;
    }
    
    .editor-sidebar .control-group {
        align-items: center;
        width: auto;
    }
    
    .editor-sidebar .control-group input[type="range"] {
        width: 150px;
    }

    .editor-sidebar .final-export {
        flex-direction: row;
        justify-content: center;
    }
    
    .editor-sidebar .final-export .action-btn {
        width: auto;
    }
}

/* --- Custom Confirmation Modal --- */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 10000;
    opacity: 1;
    transition: opacity 0.3s ease;
}

.modal-overlay.hidden {
    display: none;
    opacity: 0;
}

.modal-card {
    background: var(--glass-bg);
    backdrop-filter: blur(25px);
    -webkit-backdrop-filter: blur(25px);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-xl);
    padding: 2rem;
    width: 90%;
    max-width: 450px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.4);
    transform: scale(1);
    transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    text-align: center;
}

.modal-overlay.hidden .modal-card {
    transform: scale(0.9);
}

.modal-card h3 {
    font-size: 1.5rem;
    margin-bottom: 1rem;
    color: var(--text-primary);
}

.modal-card p {
    color: var(--text-secondary);
    line-height: 1.6;
    margin-bottom: 2rem;
}

.modal-actions {
    display: flex;
    justify-content: center;
    gap: 1rem;
}

.modal-actions .action-btn {
    min-width: 120px;
}

.modal-actions .small-btn {
    background: rgba(255, 255, 255, 0.05);
    border-color: var(--glass-border);
    color: var(--text-muted);
}

.modal-actions .small-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
}

.final-replace-controls {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.6rem;
    width: 100%;
}

/* --- Curve Editor Styles --- */
.curve-container {
    position: relative;
    width: 200px;
    height: 200px;
    max-width: 100%;
    background: #000;
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-md);
    margin-top: 0.5rem;
    overflow: hidden;
    cursor: crosshair;
}

.curve-container canvas {
    width: 100%;
    height: 100%;
    display: block;
}

.mini-reset {
    position: absolute;
    top: 5px;
    right: 5px;
    background: rgba(0, 0, 0, 0.5) !important;
    font-size: 0.8rem;
    padding: 2px 4px;
    z-index: 10;
}

/* --- Adjustment Tabs --- */
.adjustment-tabs {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
    border-bottom: 1px solid var(--glass-border);
    padding-bottom: 0.5rem;
}

.adj-tab-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 0.8rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    cursor: pointer;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    transition: all 0.2s;
}

.adj-tab-btn.active {
    color: var(--accent-cyan);
    background: rgba(6, 182, 212, 0.1);
}

.adj-tab-btn:hover:not(.active) {
    color: var(--text-secondary);
    background: rgba(255, 255, 255, 0.05);
}

.adj-pane {
    display: none;
}

.adj-pane.active {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* --- Batch Export Modal Styles --- */
.batch-inputs-container {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-top: 1.5rem;
    text-align: left;
}

.batch-input-group {
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--glass-border);
    padding: 1rem;
    border-radius: var(--radius-md);
}

.batch-input-group label {
    display: block;
    font-weight: 600;
    color: var(--accent-cyan);
    margin-bottom: 0.5rem;
}

.batch-input-group input[type="file"] {
    width: 100%;
    color: var(--text-secondary);
    cursor: pointer;
}

.batch-input-group input[type="file"]::file-selector-button {
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-md);
    padding: 0.5rem 1rem;
    margin-right: 1rem;
    cursor: pointer;
    transition: background 0.2s;
}

.batch-input-group input[type="file"]::file-selector-button:hover {
    background: rgba(255, 255, 255, 0.05);
}

progress {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 10px;
    border-radius: 5px;
    overflow: hidden;
    background: var(--bg-secondary);
    border: 1px solid var(--glass-border);
}

progress::-webkit-progress-bar {
    background: var(--bg-secondary);
}

progress::-webkit-progress-value {
    background: var(--gradient-primary);
    border-radius: 5px;
}

progress::-moz-progress-bar {
    background: var(--gradient-primary);
    border-radius: 5px;
}
/* Keep painting view controls beside the preview on desktop and mobile. */
.mask-view-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.mask-view-controls .tool-btn {
    flex: 1 1 auto;
}

/* Desktop keeps the ordinary expanded sidebar, with no disclosure UI. */
#mask-controls-panel,
.mask-controls-content {
    display: contents;
}

#mask-controls-panel > summary {
    display: none;
}

@media (max-width: 768px) {
    #mask-controls-panel {
        display: block;
        width: 100%;
        border: 1px solid var(--glass-border);
        border-radius: var(--radius-md);
    }

    #mask-controls-panel > summary {
        display: list-item;
        padding: 0.85rem 1rem;
        min-height: 48px;
        cursor: pointer;
        font-weight: 600;
        list-style-position: inside;
    }

    #mask-controls-panel > summary:focus-visible {
        outline: 2px solid var(--accent-cyan);
        outline-offset: -3px;
    }

    #mask-controls-panel > .mask-controls-content {
        display: flex;
        flex-direction: column;
        gap: 0.65rem;
        padding: 0.5rem;
    }

    #mask-controls-panel:not([open]) > .mask-controls-content {
        display: none;
    }
}

@media (max-width: 1100px) {
    #mask-step {
        padding: 0.5rem;
    }

    #mask-step .editor-layout {
        gap: 0.75rem;
        align-items: stretch;
    }

    #mask-step .editor-workspace {
        flex: none;
        width: 100%;
    }

    #mask-step .canvas-container {
        display: flex;
        justify-content: center;
        margin: 0.5rem 0 0;
        border-radius: 8px;
    }

    #mask-canvas {
        /* Preserve the actual canvas aspect ratio: no letterboxing inside
           its pointer target, so touch-to-mask coordinate mapping stays exact. */
        width: min(100%, calc(65svh * var(--mask-view-aspect, 1)));
        height: auto;
        align-self: flex-start;
    }

    #mask-step .editor-sidebar {
        padding: 0;
        gap: 0.65rem;
    }

    #mask-step .mask-controls-content > h2,
    #mask-step .mask-controls-content > p {
        display: none;
    }

    #mask-step .mask-brush-controls {
        display: grid;
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
        gap: 0.5rem 1rem;
        padding: 0.75rem;
    }

    #mask-step .mask-brush-controls .tool-select {
        grid-column: 1 / -1;
    }

    #mask-step .control-group {
        width: 100%;
        min-width: 0;
        align-items: stretch;
    }

    #mask-step .control-group input[type="range"] {
        width: 100%;
        min-height: 32px;
    }

    #mask-step .label-row {
        flex-wrap: wrap;
        gap: 0.25rem;
    }

    #mask-step .tool-btn,
    #mask-step .action-btn,
    #mask-step .adj-tab-btn {
        min-height: 44px;
        white-space: normal;
    }

    #mask-step .final-export,
    #mask-step .final-export .btn-group {
        display: flex;
        flex-direction: column;
        align-items: stretch;
        width: 100%;
    }

    #mask-step .final-export .action-btn {
        width: 100%;
    }
}

/* The shared navigation stylesheet adds main-container padding. Keep the
   final painting workspace close to the screen edges on slim phones. */
@media (max-width: 768px) {
    main.app-container:has(#mask-step:not(.hidden)) {
        padding-inline: 4px;
    }
}

/* Fluid layout for every stage, scoped against the shared site stylesheet. */
.replace-region-app .app-container > .glass-card,
.replace-region-app .preview-box,
.replace-region-app .scale-calc-col,
.replace-region-app .control-group {
    min-width: 0;
}

.replace-region-app .preview-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 280px), 1fr));
}

.replace-region-app .base-choice-grid {
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 230px), 1fr));
}

.replace-region-app .scale-calc-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr));
}

.replace-region-app .action-btn,
.replace-region-app .base-choice-card,
.replace-region-app .status-message,
.replace-region-app .base-upload-summary,
.replace-region-app .scale-formula {
    max-width: 100%;
    white-space: normal;
    overflow-wrap: anywhere;
}

.replace-region-app .modal-card {
    max-height: calc(100dvh - 2rem);
    overflow-y: auto;
    overscroll-behavior: contain;
}

.replace-region-app .modal-card input[type="file"] {
    width: 100%;
    min-width: 0;
}

.mobile-editor-panel,
.editor-controls-content {
    display: contents;
}

.mobile-editor-panel > summary {
    display: none;
}

@media (max-width: 1100px) {
    .replace-region-app .editor-workspace {
        width: 100%;
        flex: none;
    }
}

@media (max-width: 768px) {
    .replace-region-app main.app-container {
        padding-inline: 4px;
        gap: 0.75rem;
    }

    .replace-region-app .app-container > .glass-card {
        padding: clamp(0.5rem, 2vw, 1rem);
    }

    .replace-region-app > .site-header:not([data-site-header]) {
        padding-inline: 0.5rem;
    }

    .replace-region-app .glass-card h2 {
        font-size: clamp(1.15rem, 5vw, 1.5rem);
        overflow-wrap: anywhere;
    }

    .replace-region-app .glass-card h3 {
        font-size: clamp(1rem, 4vw, 1.2rem);
    }

    .replace-region-app .controls-bar {
        padding: 0.75rem;
        gap: 0.75rem;
        align-items: stretch;
    }

    .replace-region-app .btn-group,
    .replace-region-app .btn-group-vertical,
    .replace-region-app .modal-actions {
        width: 100%;
        flex-direction: column;
        align-items: stretch;
    }

    .replace-region-app .action-btn,
    .replace-region-app select,
    .replace-region-app .tool-btn {
        min-height: 44px;
    }

    .replace-region-app .btn-group > .action-btn,
    .replace-region-app .btn-group-vertical > .action-btn,
    .replace-region-app .control-group,
    .replace-region-app .control-group select,
    .replace-region-app .editor-sidebar .control-group input[type="range"] {
        width: 100%;
    }

    .replace-region-app .action-btn:hover {
        transform: none;
    }

    .replace-region-app .preview-container {
        gap: 1rem;
        margin-bottom: 1rem;
    }

    .replace-region-app .preview-box .action-btn {
        margin-top: 0;
    }

    .replace-region-app .scale-calc-card,
    .replace-region-app .dual-base-summary,
    .replace-region-app .modal-card {
        padding: clamp(0.75rem, 3vw, 1.25rem);
    }

    .replace-region-app .scale-res-row {
        flex-wrap: wrap;
        gap: 0.25rem 0.75rem;
    }

    .replace-region-app .scale-res-value {
        margin-left: auto;
    }

    .replace-region-app .scale-formula {
        padding: 0.5rem;
    }

    .replace-region-app :is(#crop-step, #re-upload-step, #paint-editor-step) .canvas-container {
        display: flex;
        justify-content: center;
        border-radius: 8px;
    }

    .replace-region-app :is(#crop-canvas, #paint-editor-canvas) {
        width: min(100%, calc(65svh * var(--canvas-aspect, 1)));
        height: auto;
        align-self: flex-start;
    }

    .replace-region-app :is(#original-preview-canvas, #cropped-preview-canvas) {
        width: min(100%, calc(42svh * var(--canvas-aspect, 1)));
        height: auto;
        align-self: flex-start;
    }

    .replace-region-app #paint-editor-step .editor-layout {
        gap: 0.75rem;
        align-items: stretch;
    }

    .replace-region-app #paint-editor-step .editor-sidebar {
        padding: 0;
    }

    .mobile-editor-panel {
        display: block;
        width: 100%;
        border: 1px solid var(--glass-border);
        border-radius: var(--radius-md);
    }

    .mobile-editor-panel > summary {
        display: list-item;
        list-style-position: inside;
        padding: 0.85rem 1rem;
        min-height: 48px;
        cursor: pointer;
        font-weight: 600;
    }

    .mobile-editor-panel > summary:focus-visible {
        outline: 2px solid var(--accent-cyan);
        outline-offset: -3px;
    }

    .mobile-editor-panel > .editor-controls-content {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
        padding: 0.5rem;
    }

    .mobile-editor-panel:not([open]) > .editor-controls-content {
        display: none;
    }

    #paint-editor-step .final-export {
        flex-direction: column;
        align-items: stretch;
    }

    #paint-editor-step .final-export .action-btn {
        width: 100%;
    }
}
