html, body {
    height: 100%;
    margin: 0;
    overflow: hidden;
    overscroll-behavior: none;
}

#designer-app {
    display: flex;
    height: 100dvh;
    max-height: 100dvh;
    min-height: 0;
    position: relative;
}

.designer-panel {
    display: flex;
    flex-direction: column;
    width: 17rem;
    min-height: 0;
}

.designer-panel-scroll {
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
}

.component-section {
    margin-bottom: 0.5rem;
}

.component-section h3,
.component-section h4 {
    margin-bottom: 0.25rem;
}

.component-grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.375rem;
}

.component-grid-2 select,
.component-grid-2 input[type="color"] {
    width: 100%;
}

#part-list {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.25rem;
    max-height: 5.5rem;
    overflow-y: auto;
}

.part-btn {
    padding: 0.25rem 0.375rem !important;
    font-size: 0.65rem !important;
    line-height: 1.1;
}

/* Fabric grid */
#fabric-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.5rem;
    align-content: start;
    grid-auto-rows: auto;
}

.fabric-card {
    display: block;
    width: 100%;
    min-width: 0;
    aspect-ratio: 1 / 1;
    padding: 0;
    margin: 0;
    background: #f1f5f9;
    cursor: pointer;
    line-height: 0;
}

.fabric-card img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.fabric-card-fallback {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.65rem;
    color: #64748b;
    padding: 0.25rem;
    text-align: center;
    line-height: 1.2;
    aspect-ratio: 1 / 1;
}

#category-filters {
    max-height: 3.25rem;
    overflow-y: auto;
    flex-shrink: 0;
}

#uv-controls {
    flex-shrink: 0;
    padding: 0 !important;
}

.viewer-toolbar {
    flex-shrink: 0;
    padding: 0.25rem 0.5rem;
}

.viewer-btn {
    padding: 0.25rem;
    border-radius: 0.375rem;
    color: #475569;
    transition: background-color 0.15s;
    font-size: 0.75rem;
    font-weight: 500;
    min-width: 28px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.viewer-btn:hover {
    background-color: #f1f5f9;
}

.fabric-card.border-blue-500 {
    border-color: #3b82f6;
}

#viewer-container {
    min-height: 0;
}

#viewer-container canvas {
    display: block;
    width: 100% !important;
    height: 100% !important;
    touch-action: none;
}

.component-section select:focus,
.component-section input:focus {
    outline: none;
    box-shadow: 0 0 0 2px #3b82f6;
}

.panel-backdrop,
.mobile-tabbar {
    display: none;
}

/* ============ MOBILE / TABLET ( < 768px ) ============ */
@media (max-width: 767px) {
    #designer-app {
        display: block;
    }

    .viewer-main {
        position: absolute;
        inset: 0;
        bottom: 3.75rem;
        display: flex;
        flex-direction: column;
        width: 100%;
    }

    .designer-panel {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 3.75rem;
        width: 100%;
        height: 70vh;
        max-height: 70vh;
        z-index: 50;
        border-radius: 1rem 1rem 0 0;
        box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.18);
        transform: translateY(110%);
        transition: transform 0.28s ease;
        border: none;
    }

    .designer-panel.panel-open {
        transform: translateY(0);
    }

    /* Fabrics sheet: header + filters fixed, grid scrolls, UV pinned at bottom */
    #panel-fabrics .panel-header,
    #panel-fabrics #category-filters {
        flex-shrink: 0;
    }

    #panel-fabrics #fabric-grid {
        flex: 1 1 0;
        min-height: 0;
        overflow-y: auto;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.5rem;
        padding: 0.5rem;
        align-content: start;
    }

    #panel-fabrics #uv-controls {
        flex-shrink: 0;
        border-top: 1px solid #e2e8f0;
        background: #fff;
    }

    .panel-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        bottom: 3.75rem;
        background: rgba(15, 23, 42, 0.4);
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.25s ease;
        z-index: 40;
    }

    .panel-backdrop.show {
        opacity: 1;
        pointer-events: auto;
    }

    .component-grid-2 select,
    #fabric-search,
    #collar-select,
    #cuff-select,
    #pocket-select,
    #button-select {
        padding: 0.5rem;
        font-size: 0.8rem;
    }

    .part-btn {
        padding: 0.5rem !important;
        font-size: 0.75rem !important;
    }

    #part-list {
        max-height: none;
        grid-template-columns: repeat(2, 1fr);
    }

    .viewer-toolbar {
        flex-wrap: wrap;
        gap: 0.375rem;
    }

    .viewer-btn {
        min-width: 34px;
        padding: 0.375rem;
    }

    .mobile-tabbar {
        display: flex;
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        height: 3.75rem;
        padding-bottom: env(safe-area-inset-bottom, 0);
        background: #ffffff;
        border-top: 1px solid #e2e8f0;
        z-index: 60;
    }

    .tab-btn {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 0.125rem;
        font-size: 0.65rem;
        color: #64748b;
        background: none;
        border: none;
    }

    .tab-btn.active {
        color: #2563eb;
    }
}

@media (max-width: 900px) and (orientation: landscape) {
    .designer-panel {
        height: 85vh;
        max-height: 85vh;
    }
}
