.editor-stage {
    min-height: 0;
    height: clamp(350px, calc(100vh - 215px), 800px);
    overflow: hidden;
}

.editor-stage-wrap {
    height: clamp(450px, calc(100vh - 160px), 860px);
    min-height: clamp(390px, calc(100vh - 195px), 840px);
}

.editor-stage .canvas-container {
    max-width: 100%;
    max-height: 100%;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
}

.editor-stage.has-image .canvas-container {
    flex: 0 1 auto;
}

.editor-stage-wrap {
    min-width: 0;
    overflow: hidden;
}

.editor-sidebar {
    align-self: start;
    max-height: calc(100vh - 100px);
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-gutter: stable;
    scrollbar-width: thin;
    scrollbar-color: #475569 #101522;
}

.editor-page {
    overflow: hidden;
}

.editor-page main {
    height: calc(100vh - 82px);
    overflow: hidden;
}

.editor-layout {
    height: calc(100% - 52px);
    min-height: 0;
    align-items: stretch;
}

.editor-sidebar,
.editor-panel {
    min-height: 0;
}

.editor-panel {
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    scrollbar-color: #475569 #101522;
}

.editor-panel::-webkit-scrollbar {
    width: 6px;
}

.editor-panel::-webkit-scrollbar-track {
    background: #101522;
}

.editor-panel::-webkit-scrollbar-thumb {
    border-radius: 999px;
    background: #475569;
}

.editor-sidebar::-webkit-scrollbar {
    width: 6px;
}

.editor-sidebar::-webkit-scrollbar-track {
    background: #101522;
    border-radius: 999px;
}

.editor-sidebar::-webkit-scrollbar-thumb {
    border: 1px solid #101522;
    border-radius: 999px;
    background: #475569;
}

.editor-sidebar::-webkit-scrollbar-thumb:hover {
    background: #f97316;
}

.editor-stage.checkerboard-bg {
    background-color: #0b0f19;
    background-image: none;
}

.editor-stage .canvas-container .lower-canvas {
    background-color: #fff;
    background-image: linear-gradient(45deg, #e5e7eb 25%, transparent 25%), linear-gradient(-45deg, #e5e7eb 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #e5e7eb 75%), linear-gradient(-45deg, transparent 75%, #e5e7eb 75%);
    background-size: 20px 20px;
    background-position: 0 0, 0 10px, 10px -10px, -10px 0;
}

.editor-stage .canvas-container canvas {
    position: absolute;
    left: 50% !important;
    top: 50% !important;
    transform: translate(-50%, -50%);
    width: auto !important;
    height: auto !important;
    max-width: 100%;
    max-height: 100%;
}

@media (max-width: 900px) {
    .editor-page {
        overflow-x: hidden !important;
        overflow-y: hidden !important;
        height: 100vh !important;
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    .editor-header > div {
        max-width: 100% !important;
        width: 100% !important;
        margin: 0 !important;
        padding-left: 4px !important;
        padding-right: 4px !important;
        box-sizing: border-box !important;
    }

    .editor-page main {
        height: calc(100vh - 50px) !important;
        overflow: hidden !important;
        padding: 4px 8px !important;
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
        margin: 0 !important;
        max-width: none !important;
    }

    .editor-layout {
        display: flex !important;
        flex-direction: column !important;
        grid-template-columns: none !important;
        gap: 6px !important;
        height: 100% !important;
        min-height: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        padding: 0 !important;
        box-sizing: border-box !important;
        overflow: hidden !important;
    }

    /* Fixed top elements on Mobile/Tablet */
    .editor-stage-wrap {
        order: 1 !important;
        flex-shrink: 0 !important;
        height: auto !important;
        min-height: 200px !important;
        max-height: 290px !important;
        padding: 6px !important;
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
        margin: 0 !important;
    }

    .editor-stage {
        height: 100% !important;
        min-height: 180px !important;
        max-height: 270px !important;
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
    }

    .editor-sidebar {
        display: grid !important;
        grid-template-columns: repeat(4, 1fr) !important;
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
        margin: 0 !important;
        padding: 6px !important;
    }

    .editor-sidebar.hidden {
        display: none !important;
    }

    .layers-panel {
        order: 2 !important;
        flex-shrink: 0 !important;
    }

    .mobile-back-tools-btn {
        order: 3 !important;
        flex-shrink: 0 !important;
        margin-top: 2px !important;
        margin-bottom: 2px !important;
        padding: 8px 12px !important;
    }

    /* Only .editor-panel (Tool Settings) & .editor-sidebar (Menu Grid) scroll at the bottom! */
    .editor-panel {
        order: 4 !important;
        flex: 1 !important;
        min-height: 0 !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        -webkit-overflow-scrolling: touch !important;
        touch-action: pan-x pan-y !important;
        padding: 0 4px 10px 4px !important;
        background: transparent !important;
        border: none !important;
    }

    .editor-sidebar {
        order: 5 !important;
        flex: 1 !important;
        min-height: 0 !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
        touch-action: pan-x pan-y !important;
    }

    .tool-panel {
        max-height: none !important;
        overflow: visible !important;
    }
}

/* Fix mobile/tablet tool view: Up to Back to Tools remains fixed, only panel content below scrolls */
@media (max-width: 900px) {
    body.tool-active,
    body.tool-active html {
        overflow: hidden !important;
        height: 100vh !important;
        height: 100dvh !important;
    }

    body.tool-active.editor-page main {
        height: calc(100vh - 50px) !important;
        height: calc(100dvh - 50px) !important;
        overflow: hidden !important;
    }

    body.tool-active .editor-layout {
        display: flex !important;
        flex-direction: column !important;
        height: 100% !important;
        overflow: hidden !important;
    }

    body.tool-active .editor-panel {
        flex: 1 !important;
        min-height: 0 !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
    }
}

/* Mobile / Tablet Back to Tools Button */
.mobile-back-tools-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 8px 12px;
    margin-top: 8px;
    margin-bottom: 8px;
    border-radius: 12px;
    background: #182131;
    border: 1px solid #1e2738;
    color: #f472b6;
    font-size: 12px;
    font-weight: 800;
    cursor: pointer;
    transition: background 0.15s ease, transform 0.15s ease;
}
    border: 1px solid #1e2738;
    color: #f472b6;
    font-size: 12px;
    font-weight: 800;
    cursor: pointer;
    transition: background 0.15s ease, transform 0.15s ease;
}

.mobile-back-tools-btn:active {
    transform: scale(0.98);
    background: #26344b;
}

.mobile-back-tools-btn i {
    font-size: 14px;
    color: #f472b6;
}

@media (min-width: 901px) {
    .mobile-back-tools-btn {
        display: none !important;
    }
}

@media (max-width: 640px) {
    .editor-stage-wrap {
        height: auto !important;
        min-height: 200px !important;
        max-height: 300px !important;
        padding: 6px !important;
    }

    .editor-stage {
        height: 100% !important;
        min-height: 180px !important;
        max-height: 280px !important;
    }
}

    .editor-stage {
        height: min(72vh, 660px);
    }
}

@media (max-width: 640px) {
    .editor-stage-wrap {
        height: calc(min(68vh, 550px) + 70px);
        min-height: min(68vh, 590px);
    }

    .editor-stage {
        height: min(68vh, 550px);
    }
}
