/* Quick Edit CSS - Layout & Overrides Only */

html,
body {
    overscroll-behavior: none;
}

/* Reusing Generator Styles:
   - .tg-sidebar (Left panel background/blur)
   - .tg-layout (Main container)
   - .btn-primary, .btn-secondary (Buttons)
   - .tg-sidebar-textarea, .tg-num-input (Inputs)
*/

/* --- MAIN LAYOUT OVERRIDE --- */
/* We need to override .tg-layout from flex (generator) to grid (quick-edit) on desktop */
.qe-layout {
    display: grid;
    grid-template-columns: 320px 1fr 1fr;
    /* Fixed Tools | Input | Output */
    background: var(--bg-black);
    padding: 0;
    /* Override generic padding if any */
    gap: 0;
    /* Remove gap */
}

/* Sidebar/center/right shared by editor-foundation.css */

/* --- COMPONENTS --- */

/* OPTIMIZATION: Remove 300ms tap delay on touch devices */
button,
.btn,
.qe-tool-btn,
.qe-action-btn,
.qe-m-tool,
.qe-m-text-btn,
#btn-m-undo,
#btn-m-redo {
    touch-action: manipulation;
}

/* Keep range sliders reliably draggable with finger/pencil */
.stencil-slider,
#stencil-contrast-slider,
#stencil-brightness-slider,
#stencil-contrast-slider-mobile,
#stencil-brightness-slider-mobile {
    touch-action: pan-x !important;
    pointer-events: auto !important;
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

/* Tools Row */
.qe-tools-row {
    display: flex;
}

.qe-tool-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    /* shared spacing/size in editor-shell.css */
}

.qe-tool-btn span {
    font-size: 0.7rem;
    font-weight: 500;
}

/* Active State Hook for btn-secondary */
.qe-tool-btn.active {
    background: var(--surface-hover);
    border-color: var(--neon-green) !important;
    color: var(--text-white);
    box-shadow: 0 0 15px rgba(34, 197, 94, 0.2);
}

/* Actions Grid */
.qe-actions-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.mt-2 {
    margin-top: 8px;
}

.qe-action-btn {
    /* shared spacing/size in editor-shell.css */
}

/* Canvas Wrap */
.qe-canvas-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

#qe-empty-msg {
    pointer-events: none;
    z-index: 1;
}

.qe-canvas-wrap canvas {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    max-width: 100%;
    max-height: 100%;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
    border-radius: 4px;
    /* Slight radius */
}

#canvas-mask {
    z-index: 10;
    opacity: 0.6;
}

#canvas-base {
    z-index: 5;
}

/* Output */
.qe-output-wrap {
    width: 100%;
    flex: 1 1 auto;
    /* Fill remaining height */
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.02);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-md);
    margin-bottom: 0;
    /* Removed previous margin */
    position: relative;
    overflow: hidden;
}

.qe-media-img {
    display: block;
    width: 100%;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

.qe-placeholder {
    color: var(--text-dim);
    text-align: center;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

.qe-download-btn {
    position: absolute;
    bottom: 20px;
    width: calc(100% - 40px);
}

/* Mobile Toggles */
.qe-mobile-sliders,
.qe-mobile-upload-btn,
.tg-composer {
    display: none !important;
    /* Hidden on desktop */
}

@media (min-width: 901px),
(orientation: landscape) and (min-width: 769px) {
    #qe-upload-desktop-btn {
        display: inline-flex !important;
        height: 36px !important;
    }

    /* Keep steps input compact */
    #qe-steps {
        width: 64px !important;
        flex: 0 0 64px !important;
        max-width: 64px !important;
        box-sizing: border-box !important;
        height: 36px !important;
    }

    /* Prevent res-box wrapper from stretching too wide */
    .qe-sidebar .res-box {
        flex: 0 0 auto !important;
        width: 80px !important;
        min-width: 80px !important;
        max-width: 80px !important;
    }

    /* Tighter vertical spacing for tools panel (no scroll) */
    .qe-sidebar {
        padding: 16px 16px !important;
        /* Increased from 12px (was 20px) */
        overflow-y: hidden !important;
        /* Disable scroll if fits */
        justify-content: center;
        /* Center vertically if space allows */
    }

    .qe-sidebar .sidebar-section {
        margin-bottom: 12px !important;
        /* Increased from 8px (was ~16px) */
    }

    .qe-sidebar .sidebar-section label {
        margin-bottom: 4px !important;
        font-size: 0.7rem !important;
        /* Slightly smaller label */
    }

    /* Compact Textarea */
    #qe-prompt-desktop.tg-sidebar-textarea {
        min-height: 150px !important;
        height: 150px !important;
        padding: 8px !important;
        font-size: 0.85rem !important;
        color: var(--text-white) !important;
    }

    /* Compact Buttons */
    .qe-tool-btn {
        height: 56px !important;
        /* Reduced from 64px */
        padding: 4px !important;
    }

    .qe-action-btn {
        height: 36px !important;
        /* Reduced from 40px */
    }

    /* Generate Button Spacing */
    .sidebar-footer {
        margin-top: 12px !important;
    }

    .tg-sidebar-generate-btn {
        padding: 10px !important;
    }

}

/* --- MOBILE LAYOUT (<= 768px) AND TABLET PORTRAIT (<= 1024px) --- */
@media (max-width: 768px),
(orientation: portrait) and (max-width: 1024px) {
    .desktop-only {
        display: none !important;
    }

    .mobile-only {
        display: flex !important;
    }

    /* Force placeholder behavior: Keep space but hide content */
    #btn-download-mobile {
        display: inline-flex !important;
        visibility: hidden !important;
    }

    /* Show only when class is added via JS */
    #btn-download-mobile.show-download {
        visibility: visible !important;
    }

    #btn-print-mobile {
        display: inline-flex !important;
        visibility: hidden !important;
    }

    #btn-print-mobile.show-print {
        visibility: visible !important;
    }

    #qe-mobile-upload-btn {
        display: inline-flex !important;
        width: 48px;
        height: 48px;
        flex: 0 0 48px;
    }

    /* LAYOUT: Hide Desktop Panels Visually */
    .qe-sidebar,
    .qe-panel-right {
        display: none !important;
    }

    /* Reset Main Grid to Block */
    .qe-layout {
        display: block;
        height: auto;
        min-height: 100vh;
        position: relative;
    }

    html,
    body {
        overflow: hidden;
        height: 100%;
        position: fixed;
        width: 100%;
        touch-action: none;
        overscroll-behavior: none;
    }

    /* Center Panel (Canvas) */
    .qe-panel-center {
        position: fixed;
        top: 64px;
        left: 60px;
        /* Space for Tool Strip */
        right: 0;
        bottom: 80px;
        /* Space for Prompt Bar */
        width: auto;
        height: auto;
        padding: 0;
        background: #000;
        z-index: 10;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    #qe-canvas-container {
        width: 100%;
        height: 100%;
    }

    /* Mobile Tool Strip (Left) */
    #qe-mobile-tool-strip {
        position: fixed;
        top: 64px;
        left: 0;
        bottom: 60px;
        /* Reduced to align perfectly above the composer */
        width: 60px;
        background: var(--surface-dark);
        border-right: 1px solid var(--glass-border);
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 5px 0;
        /* Reduced padding to fit tools */
        gap: 8px;
        /* Reduced gap to fit everything without scrolling */
        z-index: 50;
        overflow-y: hidden;
        /* Stop sliding/scrolling */
    }

    .qe-m-tool {
        width: 40px;
        /* Slightly smaller */
        height: 40px;
        border-radius: 8px;
        background: rgba(255, 255, 255, 0.03);
        border: 1px solid var(--glass-border);
        display: flex;
        align-items: center;
        justify-content: center;
        color: var(--text-dim);
        transition: all 0.2s;
        flex-shrink: 0;
    }

    /* Gray/Neutral Undo/Redo (Responsive) */
    #btn-m-undo,
    #btn-m-redo {
        color: var(--text-dim);
        border-color: var(--glass-border);
        pointer-events: auto;
        z-index: 60;
    }

    #btn-m-undo:active,
    #btn-m-redo:active {
        background: rgba(255, 255, 255, 0.1);
        color: white;
    }

    #btn-m-undo:disabled,
    #btn-m-redo:disabled {
        opacity: 0.3;
        pointer-events: auto !important;
        /* Always allow taps */
        cursor: default;
    }

    /* Text Buttons (Clear, Upload) */
    .qe-m-text-btn {
        flex-direction: column;
        gap: 2px;
        font-size: 8px;
        /* Slightly smaller font */
        text-transform: uppercase;
        font-weight: 700;
        line-height: 1;
        color: var(--neon-green);
        border-color: rgba(16, 185, 129, 0.3);
        height: auto;
        padding: 4px 2px;
        min-height: 40px;
    }

    /* Upload Button (Moved to Bottom) */
    #qe-mobile-upload-btn {
        display: flex !important;
        /* Ensure visible */
        position: static;
        /* Flow in tool strip */
        margin: 0;
        margin-top: auto;
        /* Push to bottom */
        margin-bottom: 5px;
        /* Adds a bit of breathing room at bottom */
        background: rgba(16, 185, 129, 0.1);
        cursor: pointer;
    }

    .stencil-page #qe-mobile-tool-strip {
        justify-content: flex-end !important;
        gap: 0 !important;
    }

    .stencil-page #btn-m-clear-image {
        margin-top: 0 !important;
        margin-bottom: 12px !important;
    }

    .stencil-page #qe-mobile-upload-btn {
        margin-top: 0 !important;
        margin-bottom: 5px !important;
    }

    .qe-m-upload-icon-small {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 2px;
    }

    .qe-m-tool.active {
        background: var(--surface-hover);
        color: var(--neon-green);
        border-color: var(--neon-green);
        box-shadow: 0 0 10px rgba(16, 185, 129, 0.2);
    }

    .qe-m-slider-container {
        height: 60px;
        /* Reduced slider height */
        width: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-top: 10px;
        margin-bottom: 10px;
    }

    /* Mobile Steps Input (Tiny) */
    .qe-steps-mini {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        margin-bottom: 4px;
        /* Reduced spacing */
    }

    .qe-steps-label {
        font-size: 8px;
        color: var(--text-dim);
        margin-bottom: 2px;
        text-transform: uppercase;
        font-weight: 700;
        letter-spacing: 0.5px;
    }

    #qe-steps-input {
        width: 36px;
        height: 24px;
        background: rgba(255, 255, 255, 0.05);
        border: 1px solid var(--glass-border);
        border-radius: 4px;
        color: white;
        text-align: center;
        font-size: 12px;
        padding: 0;
        appearance: none;
        -moz-appearance: textfield;
    }

    #qe-steps-input:focus {
        border-color: var(--neon-green);
        background: rgba(255, 255, 255, 0.1);
        outline: none;
    }

    #qe-steps-input::-webkit-inner-spin-button,
    #qe-steps-input::-webkit-outer-spin-button {
        -webkit-appearance: none;
        margin: 0;
    }

    .qe-m-slider {
        width: 60px;
        /* Shrink to match container */
        height: 20px;
        transform: rotate(-90deg);
        transform-origin: center;
        -webkit-appearance: none;
        appearance: none;
    }

    /* Prompt Bar (Mobile Composer) - Matches Generator Style */
    #qe-mobile-composer.tg-composer {
        display: block !important;
        position: fixed;
        bottom: 0px;
        left: 60px;
        right: 0;
        width: auto;
        padding: 10px;
        padding-bottom: 10px;
        /* Removed env(safe-area-inset-bottom) to kill black space */
        z-index: 9999;
        border-radius: 0;
        max-width: none;
        transform: translateY(calc(-1 * var(--kb-offset, 0px))) !important;
        transition: transform 0.2s ease-out;
    }

    #qe-mobile-composer .tg-input-row {
        display: grid;
        grid-template-columns: minmax(0, 1fr) 48px 22px;
        gap: 10px;
        align-items: center;
        padding: 0;
        width: 100%;
    }

    #qe-mobile-composer .tg-prompt-wrapper {
        min-width: 0;
        align-items: stretch;
        overflow: hidden;
    }

    #qe-mobile-composer .tg-num-input,
    #qe-mobile-composer textarea,
    #qe-mobile-composer select,
    #qe-mobile-composer input {
        scroll-margin: 0;
        scroll-padding: 0;
    }

    .qe-phone-spinner {
        width: 22px;
        height: 22px;
        border: 2.5px solid rgba(16, 185, 129, 0.18);
        border-top-color: var(--neon-green);
        border-radius: 50%;
        animation: qe-phone-spin 0.9s linear infinite;
        flex: 0 0 auto;
        margin-left: 0;
        justify-self: center;
        align-self: center;
    }

    @keyframes qe-phone-spin {
        from { transform: rotate(0deg); }
        to { transform: rotate(360deg); }
    }

    #qe-mobile-composer .tg-prompt-textarea {
        width: 100%;
        max-width: 100%;
        min-width: 0;
        box-sizing: border-box;
        overflow-wrap: anywhere;
        word-break: break-word;
        white-space: pre-wrap;
    }

    /* Generator Green Button Style */
    #btn-generate-mobile {
        background: var(--neon-green) !important;
        color: #020617 !important;
        /* Deep black for contrast */
        box-shadow: 0 0 20px rgba(16, 185, 129, 0.4);
        border: 1px solid var(--neon-green);
        border-radius: 50%;
        width: 48px;
        height: 48px;
        flex: 0 0 48px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* Header Note Truncation */
    /* Header Note Fix */
    .nav-container .brand+span {
        white-space: normal !important;
        overflow-wrap: anywhere !important;
        word-break: break-word !important;
        font-size: 0.72rem !important;
        max-width: 100% !important;
        display: inline-block !important;
        line-height: 1.2 !important;
        overflow: visible !important;
        text-overflow: clip !important;
    }
}

/* Tablet Landscape: Prevent Scroll on Canvas Interaction Only */
@media (hover: none) and (pointer: coarse) and (orientation: landscape) {

    #qe-canvas-container,
    .qe-canvas-wrap,
    #canvas-mask,
    #canvas-base {
        touch-action: none !important;
        overscroll-behavior: contain;
    }
}

/* Desktop header text truncation */
@media (min-width: 1024px) {
    .nav-container .brand+span {
        white-space: normal !important;
        overflow: visible !important;
        text-overflow: clip !important;
        line-height: 1.2;
    }
}

/* Tablet portrait ghost image and misalignment (address bar resize) */
@media (min-width: 768px) and (max-width: 1024px) and (orientation: portrait) {

    /* 1. Force container constraints and stability */
    .qe-canvas-wrap {
        position: relative !important;
        overflow: hidden !important;
        /* Ensure it hasn't collapsed */
        height: 100%;
        width: 100%;
    }

    /* 2. Fix canvas positioning (remove transform jitter) — skip Quick Edit: QEViewportManager sets left/top/transform with !important */
    body:not(.quick-edit-page) .qe-canvas-wrap canvas {
        position: absolute !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        margin: auto !important;
        transform: none !important;
        /* CRITICAL: Stops ghosting/shifting */
        max-width: 100% !important;
        max-height: 100% !important;
    }

    /* 3. Hide any unintended image layers */
    body:not(.quick-edit-page) .qe-canvas-wrap img {
        display: none !important;
        visibility: hidden !important;
    }

    /* Mobile Back Button (Portrait Only) */
    .btn-mobile-back {
        display: flex;
        /* Show on Tablet Portrait */
        /* Hidden by default, shown in media query or via JS injection */
        align-items: center;
        justify-content: center;
        background: transparent;
        border: none;
        color: var(--text-white);
        padding: 4px;
        cursor: pointer;
        z-index: 100;
        transition: opacity 0.2s;
    }

    .btn-mobile-back svg {
        width: 20px;
        height: 20px;
    }
}

/* Stencil page only: keep left controls grouped and centered in landscape desktop/tablet */
@media (min-width: 901px) {
    .stencil-page .qe-sidebar {
        display: flex !important;
        flex-direction: column !important;
        justify-content: center !important;
        gap: 10px !important;
        padding: 14px 12px !important;
    }

    .stencil-page .qe-sidebar .sidebar-section,
    .stencil-page .qe-sidebar .sidebar-footer,
    .stencil-page .qe-sidebar #qe-credits {
        margin: 0 !important;
    }

    .stencil-page .qe-sidebar .sidebar-section {
        margin-bottom: 0 !important;
    }

    .stencil-page .qe-sidebar .sidebar-section label {
        margin-bottom: 6px !important;
    }

    .stencil-page .qe-sidebar .sidebar-footer {
        margin-top: 2px !important;
    }

    .stencil-page .qe-sidebar #qe-credits {
        margin-top: 2px !important;
        text-align: center !important;
    }
}

@media (orientation: landscape) and (pointer: coarse) and (min-width: 901px) and (max-width: 1366px) {
    .stencil-page .qe-sidebar {
        justify-content: center !important;
        gap: 8px !important;
        padding: 12px 10px !important;
    }
}

/* Toast Notification (Global) */
.qe-toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 0.8rem;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s, transform 0.3s;
    z-index: 9999;
    white-space: nowrap;
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(4px);
}

.qe-toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* Quick Edit phone portrait only: extend left strip to bottom edge.
   Prevents the blank bottom-left box where canvas shows through. */
@media (max-width: 600px) and (orientation: portrait) {
    .quick-edit-page #qe-mobile-tool-strip {
        bottom: 0 !important;
        padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 6px) !important;
    }

    .stencil-page #qe-mobile-tool-strip {
        bottom: 0 !important;
        padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 6px) !important;
    }
}

/* Quick Edit iPad portrait: force mobile shell only (avoid desktop sidebar stacking). */
@media (min-width: 601px) and (max-width: 1024px) and (orientation: portrait) {
    .quick-edit-page .qe-sidebar {
        display: none !important;
    }

    .quick-edit-page #qe-mobile-tool-strip {
        display: flex !important;
    }
}

/* QUICK EDIT ONLY: desktop + tablet-landscape controls alignment */
@media (min-width: 901px) {
    .quick-edit-page .qe-layout {
        grid-template-columns: 300px 1fr;
        gap: 10px;
        padding: 10px;
    }

    .quick-edit-page .qe-panel-right {
        display: none !important;
    }

    .quick-edit-page .qe-sidebar {
        display: flex !important;
        flex-direction: column !important;
        justify-content: flex-start !important;
        padding: 10px 12px !important;
    }

    .quick-edit-page .qe-bottom-group {
        margin-top: auto !important;
        display: flex !important;
        flex-direction: column !important;
        gap: 8px !important;
    }

    .quick-edit-page .qe-sidebar .sidebar-section {
        margin-bottom: 8px !important;
    }

    /* Override generic widescreen distribution rules and keep controls grouped */
    .quick-edit-page .qe-sidebar > .sidebar-section:first-of-type {
        margin-bottom: 10px !important;
    }

    .quick-edit-page .qe-bottom-group .sidebar-section {
        margin: 0 !important;
    }

    .quick-edit-page .qe-sidebar .sidebar-section label {
        font-size: 0.66rem !important;
        margin-bottom: 3px !important;
        letter-spacing: 0.05em;
    }

    .quick-edit-page #qe-prompt-desktop.tg-sidebar-textarea {
        min-height: 150px !important;
        height: 150px !important;
        padding: 8px 10px !important;
        border-radius: 14px !important;
    }

    .quick-edit-page .qe-actions-grid {
        grid-template-columns: 1fr 1fr !important;
        column-gap: 8px !important;
        row-gap: 6px !important;
    }

    .quick-edit-page .qe-actions-grid .qe-action-btn {
        height: 38px !important;
        border-radius: 14px !important;
    }

    .quick-edit-page .qe-actions-grid .qe-action-btn:last-child {
        grid-column: 1 / span 2 !important;
        width: 56% !important;
        justify-self: center !important;
        margin-top: -1px !important;
    }

    .quick-edit-page #qe-steps {
        width: 64px !important;
        flex: 0 0 64px !important;
        max-width: 64px !important;
        height: 38px !important;
        border-radius: 12px !important;
    }

    .quick-edit-page .qe-sidebar .res-box {
        width: 64px !important;
        min-width: 64px !important;
        max-width: 64px !important;
    }

    .quick-edit-page #qe-upload-desktop-btn {
        display: flex !important;
        height: 38px !important;
        padding: 0 12px !important;
        border-radius: 12px !important;
        font-size: 0.8rem !important;
        width: 56% !important;
        justify-self: center !important;
        margin: -2px auto 0 !important;
    }

    .quick-edit-page .qe-sidebar .sidebar-section:nth-of-type(3) {
        margin-bottom: 12px !important;
    }

    .quick-edit-page .sidebar-footer {
        margin-top: 0 !important;
    }

    .quick-edit-page .tg-sidebar-generate-btn {
        min-height: 46px !important;
        padding: 10px 12px !important;
        border-radius: 999px !important;
    }

    .quick-edit-page #qe-credits {
        margin-top: 8px !important;
        font-size: 0.76rem !important;
    }
}

@media (orientation: landscape) and (pointer: coarse) and (min-width: 901px) and (max-width: 1366px) {
    .quick-edit-page .qe-layout {
        height: calc(100dvh - 64px) !important;
        min-height: calc(100dvh - 64px) !important;
        grid-template-columns: 286px 1fr;
        gap: 8px;
        padding: 8px;
    }

    .quick-edit-page .qe-sidebar {
        display: flex !important;
        flex-direction: column !important;
        justify-content: flex-start !important;
        padding: 8px 10px !important;
    }

    .quick-edit-page .qe-sidebar > .sidebar-section:first-of-type {
        margin-bottom: 8px !important;
    }

    .quick-edit-page .qe-bottom-group {
        margin-top: auto !important;
        gap: 7px !important;
    }

    .quick-edit-page .qe-bottom-group .sidebar-section {
        margin: 0 !important;
    }

    .quick-edit-page #qe-prompt-desktop.tg-sidebar-textarea {
        min-height: 150px !important;
        height: 150px !important;
    }

    .quick-edit-page .qe-actions-grid .qe-action-btn {
        height: 34px !important;
        border-radius: 12px !important;
    }

    .quick-edit-page #qe-upload-desktop-btn {
        display: flex !important;
        height: 34px !important;
        width: 56% !important;
        justify-self: center !important;
        margin: -2px auto 0 !important;
    }

    .quick-edit-page .tg-sidebar-generate-btn {
        min-height: 42px !important;
        padding: 8px 10px !important;
    }

    .quick-edit-page .res-box {
        position: relative !important;
        overflow: visible !important;
    }

    .quick-edit-page #qe-steps {
        -webkit-appearance: auto !important;
        appearance: auto !important;
        width: 64px !important;
        padding: 0 30px 0 8px !important;
        text-align: left !important;
        background: linear-gradient(180deg, rgba(15, 23, 42, 0.96), rgba(8, 15, 32, 0.96)) !important;
        border: 1px solid rgba(148, 163, 184, 0.22) !important;
        box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), 0 10px 24px rgba(2, 6, 23, 0.28) !important;
        color: var(--text-white) !important;
        font-weight: 700 !important;
        font-size: 0.9rem !important;
        line-height: 1 !important;
        transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.2s ease !important;
    }

    .quick-edit-page #qe-steps:hover {
        border-color: rgba(16, 185, 129, 0.48) !important;
        box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 12px 28px rgba(16, 185, 129, 0.14) !important;
    }

    .quick-edit-page #qe-steps:focus {
        border-color: rgba(16, 185, 129, 0.72) !important;
        box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.16), 0 14px 30px rgba(16, 185, 129, 0.18) !important;
        outline: none !important;
    }

    .quick-edit-page #qe-steps::-webkit-outer-spin-button,
    .quick-edit-page #qe-steps::-webkit-inner-spin-button {
        -webkit-appearance: inner-spin-button !important;
        opacity: 1 !important;
        min-width: 30px !important;
        width: 30px !important;
        height: 34px !important;
        margin: 0 !important;
        cursor: pointer !important;
        filter: brightness(1.55) saturate(1.3) contrast(1.12);
    }

    .quick-edit-page #qe-steps:hover::-webkit-inner-spin-button,
    .quick-edit-page #qe-steps:hover::-webkit-outer-spin-button,
    .quick-edit-page #qe-steps:focus::-webkit-inner-spin-button,
    .quick-edit-page #qe-steps:focus::-webkit-outer-spin-button {
        filter: brightness(1.85) saturate(1.45) contrast(1.15);
    }
}


/* Quick Edit desktop only: add a bit more space above EDIT button */
@media (min-width: 901px) and (hover: hover) and (pointer: fine) {
    .quick-edit-page .qe-bottom-group .sidebar-section:nth-of-type(2) {
        margin-bottom: 8px !important;
    }

    /* Stencil desktop: keep recents anchored next to title for consistent header layout. */
    body.stencil-page .main-header .nav-container {
        display: flex !important;
        align-items: center !important;
    }

    body.stencil-page .app-brand-wrap {
        flex: 0 0 auto !important;
    }

    body.stencil-page #qe-recents-strip {
        flex: 1 1 auto !important;
        min-width: 0 !important;
        margin-left: 14px !important;
        justify-content: flex-start !important;
    }
}

/* Quick Edit: header thumbnail strip only on desktop-width viewports */
@media (max-width: 1024px) {
    .quick-edit-page #qe-recents-strip {
        display: flex !important;
    }
}

/* Quick Edit iPad/Tablet only: unhide recents strip and force left-to-right flow, new items added to the right. */
@media (min-width: 768px) and (max-width: 1024px) {
    .quick-edit-page #qe-recents-strip {
        display: flex !important;
        flex-direction: row;
        justify-content: flex-start !important;
        padding: 0 12px !important;
    }
}

/* Quick Edit iPad/tablet portrait only: match Magic Blend strip anchor and append direction. */
@media (min-width: 601px) and (max-width: 1024px) and (orientation: portrait) {
    .quick-edit-page #qe-recents-strip {
        display: flex !important;
        flex-direction: row !important;
        justify-content: flex-start !important;
        padding: 0 12px !important;
    }

    .quick-edit-page #qe-btn-download-desktop {
        display: none !important;
    }

    .quick-edit-page #qe-btn-download-desktop.show-download {
        display: inline-flex !important;
    }

    .quick-edit-page #btn-download-mobile {
        display: none !important;
    }
}

/* --- RECENTS STRIP --- */
.sketch-recents-strip {
    display: flex;
    flex-direction: row;
    flex: 1;
    align-items: center;
    justify-content: flex-start;
    gap: 8px;
    padding: 0 12px;
    height: 100%;
    overflow: hidden;
}

.sketch-recent-thumb {
    width: 40px;
    height: 48px;
    border-radius: 4px;
    object-fit: cover;
    background: var(--bg-card);
    border: 1px solid var(--glass-border);
    cursor: pointer;
    transition: all 0.2s ease;
    flex-shrink: 0;
    opacity: 0;
}

.sketch-recent-thumb.is-rendered {
    opacity: 1;
}

.sketch-recent-thumb:hover {
    border-color: var(--neon-green);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(16, 185, 129, 0.2);
}

/* Limit visible items - hide anything past 10 on desktop */
.sketch-recent-thumb:nth-child(n+11) {
    display: none !important;
}

@media (max-width: 1024px) {
    .sketch-recents-strip {
        padding-left: 48px;
        justify-content: flex-start;
    }
}

/* Phone only: limit to 5 thumbnails */
@media (max-width: 767px) {
    .sketch-recent-thumb:nth-child(n+6) {
        display: none !important;
    }
}

/* Android phone portrait: fit 5 thumbnails */
@media (max-width: 480px) and (orientation: portrait) {
    .sketch-recents-strip {
        padding-left: 32px;
    }

    .stencil-page #btn-download-mobile {
        right: 18px !important;
        top: 6px !important;
    }

    .stencil-page #btn-print-mobile {
        right: 18px !important;
        top: 6px !important;
    }

    .sketch-recent-thumb {
        width: 36px;
        height: 42px;
        border-radius: 3px;
    }
}

/* Phone only: normalize Quick Edit recents geometry for clean, uncropped 5-up header */
@media (max-width: 600px) and (orientation: portrait) {
    body.quick-edit-page .main-header .nav-container {
        position: relative !important;
        display: flex !important;
        align-items: center !important;
    }

    body.quick-edit-page .app-brand-wrap,
    body.quick-edit-page .nav-actions.app-nav-actions-shrink {
        position: relative !important;
        z-index: 2 !important;
        flex: 0 0 auto !important;
    }

    body.quick-edit-page #qe-recents-strip {
        position: absolute !important;
        left: 111px !important;
        right: 58px !important;
        top: 50% !important;
        display: flex !important;
        width: auto !important;
        min-width: 0 !important;
        max-width: none !important;
        height: 48px !important;
        padding: 0 !important;
        gap: 4px !important;
        justify-content: flex-start !important;
        align-items: center !important;
        overflow: hidden !important;
        box-sizing: border-box !important;
        transform: translateY(-50%) !important;
        z-index: 1 !important;
    }

    body.quick-edit-page .nav-actions.app-nav-actions-shrink {
        width: 56px !important;
        min-width: 56px !important;
        justify-content: flex-end !important;
        margin-left: auto !important;
    }

    body.quick-edit-page #qe-recents-strip .sketch-recent-thumb {
        width: auto !important;
        height: 42px !important;
        object-fit: cover !important;
        object-position: center !important;
        border-radius: 4px !important;
        /* Keep five visible while allowing slight shrink on narrow widths. */
        flex: 0 1 calc((100% - 16px) / 5) !important;
        max-width: 34px !important;
        min-width: 0 !important;
    }

    body.quick-edit-page #qe-recents-strip .sketch-recent-thumb:nth-child(n+6) {
        display: none !important;
    }
}

/* iPad portrait: modal buttons safe spacing */
@media (min-width: 768px) and (max-width: 1024px) and (orientation: portrait) {
    .sketch-preview-modal {
        padding: 10px;
        padding-bottom: 0;
        box-sizing: border-box;
    }

    .sketch-preview-content {
        padding-top: 20px;
        max-height: calc(100vh - 30px);
    }

    .sketch-preview-img-wrap {
        flex: 1 1 auto;
        min-height: 0;
        max-height: calc(100vh - 200px);
        margin-bottom: 8px;
    }

    .sketch-preview-actions {
        padding-bottom: calc(32px + env(safe-area-inset-bottom, 0px));
        flex-shrink: 0;
    }
}

/* --- PREVIEW MODAL --- */
.sketch-preview-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 100000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.sketch-preview-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(2, 6, 23, 0.95);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
}

.sketch-preview-content {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 20px;
    width: 100%;
    max-width: 800px;
    height: 100%;
    max-height: 90vh;
    z-index: 1;
}

.sketch-preview-close {
    position: absolute;
    top: -10px;
    right: 0;
    background: rgba(255, 255, 255, 0.1);
    color: white;
    border: none;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    font-size: 24px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    z-index: 2;
}

.sketch-preview-close:hover {
    background: var(--surface-hover);
    color: var(--neon-green);
}

.sketch-preview-img-wrap {
    flex: 1;
    width: 100%;
    min-height: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 12px;
    border: 1px solid var(--glass-border);
    padding: 10px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.8), inset 0 0 20px rgba(0, 0, 0, 0.5);
}

#qe-preview-img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    border-radius: 8px;
}

.sketch-preview-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    justify-content: center;
    width: 100%;
    flex-shrink: 0;
}

.sp-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-size: 14px;
    padding: 12px 20px;
    background: rgba(255, 255, 255, 0.05);
    min-width: 120px;
}

@media (max-width: 768px) {
    .sketch-preview-modal {
        padding: 10px;
        padding-bottom: 0;
        box-sizing: border-box;
    }

    .sketch-preview-content {
        justify-content: flex-start;
        padding-top: 30px;
        height: 100%;
        max-height: calc(100vh - 40px);
    }

    .sketch-preview-img-wrap {
        flex: 1 1 auto;
        min-height: 0;
        max-height: calc(100vh - 240px);
        margin-bottom: 8px;
    }

    #qe-preview-img {
        max-height: 100%;
        width: auto;
    }

    .sketch-preview-actions {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
        padding-bottom: calc(48px + env(safe-area-inset-bottom, 0px));
        flex-shrink: 0;
        width: 100%;
    }

    .sp-btn {
        padding: 10px 5px;
        font-size: 12px;
        min-width: 0;
        width: 100%;
    }
}

@media (max-width: 600px) {
    .sketch-preview-modal {
        height: 100dvh;
        align-items: flex-start;
        padding: 8px 8px calc(84px + env(safe-area-inset-bottom, 0px));
        overflow: hidden;
    }

    .sketch-preview-content {
        gap: 10px;
        padding-top: 22px;
        height: 100%;
        max-height: calc(100dvh - 16px);
        justify-content: flex-start;
        overflow-y: auto;
        overscroll-behavior: contain;
    }

    .sketch-preview-close {
        top: 0;
        right: 0;
    }

    .sketch-preview-img-wrap {
        flex: 0 0 auto;
        max-height: calc(100dvh - 300px);
        margin-bottom: 0;
        padding: 8px;
    }

    #qe-preview-img {
        max-height: 100%;
    }

    .sketch-preview-actions {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 6px;
        padding-bottom: 0;
        margin-top: auto;
        align-self: stretch;
    }

    .sp-btn {
        min-height: 58px;
        padding: 8px 4px;
        font-size: 11px;
        gap: 4px;
        flex-direction: column;
        line-height: 1.1;
    }

    .sp-btn svg {
        width: 16px;
        height: 16px;
    }
}

@media (max-width: 600px) and (pointer: coarse) and (orientation: portrait) {
    .sketch-preview-modal {
        /* Reduce unused dark area under actions on phones. */
        padding: 8px 8px calc(8px + env(safe-area-inset-bottom, 0px));
    }

    .sketch-preview-content {
        max-height: calc(100dvh - 8px);
        padding-bottom: calc(2px + env(safe-area-inset-bottom, 0px));
        overflow: hidden;
    }

    .sketch-preview-img-wrap {
        flex: 1 1 auto;
        min-height: 0;
        max-height: calc(100dvh - 238px);
    }

    .sketch-preview-actions {
        gap: 5px;
        margin-top: auto;
        padding-bottom: calc(2px + env(safe-area-inset-bottom, 0px));
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .sp-btn {
        min-height: 46px;
        padding: 7px 4px;
        font-size: 10px;
        gap: 3px;
    }

    .sp-btn svg {
        width: 15px;
        height: 15px;
    }

    .sketch-preview-actions .sp-btn:last-child {
        grid-column: 2;
    }
}

/* FINAL GUARDRAIL — Quick Edit iPad portrait must use mobile shell only. */
@media (min-width: 601px) and (max-width: 1024px) and (orientation: portrait) {
    body.quick-edit-page .qe-sidebar {
        display: none !important;
    }

    body.quick-edit-page #qe-mobile-tool-strip {
        display: flex !important;
    }
}
