html, body {
    margin: 0;
    padding: 0;
    overflow-x: hidden; /* Hide horizontal scroll */
}
body {
    background-color: black;
    color: white;
    margin: 0;  
}

.window-custom {
    display: flex;
    flex-direction: column;
    padding: 0;
    margin: 0;
}

.title-bar {
    background:linear-gradient(90deg,#00007B,#1085D2);
    padding: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: white;
}

.title-bar-controls {
    display: flex;
}

.tab-bar {
    height: 24px;
    background-color: #c0c0c0;
}

.window-body-custom {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 20px;
    padding: 20px;
    flex-grow: 1; 
    background-color: black;
    color: black;
    margin: 0;
}

.controls-column1 {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    width: 15%;
}

.controls-column2 {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    width: 15%;
}

.input-control {
    background-color: #c0c0c0;
    display: flex;
    flex-direction: column;
}

.pieces-checkbox {
    background-color: #c0c0c0;
    display: flex;
    flex-direction: column;
}

.footer {
    padding: 0 20px;
    text-align: center;
}

.footer a {
    color: white;
}


.welcome-screen {
    position: fixed;
    top: 50%;
    left: 50%;
    width: min(90vw, 420px);
    transform: translate(-50%, -50%);
    z-index: 1000;
    display: none;
}
#tetris-grid-container {
    position: relative;
    width: min(600px, 60vw, calc(100dvh - 140px));
    aspect-ratio: 1;
    flex: 0 1 auto;
    margin: 0 auto;
}

#tetris-canvas, #three-container {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    border: 2px solid rgba(255, 255, 255, 0.8);

}

#three-container {
    display: none; /* Initially hidden */
}

#toggle-container {
    margin-bottom: 10px;
}

#threed-helper{
    display: none;
}

@media (min-width: 769px) {
    html,
    body,
    .window-custom {
        height: 100dvh;
        overflow: hidden;
    }

    .window-body-custom {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
        flex: 1 1 auto;
        min-height: 0;
        padding: 12px 20px;
    }

    .controls-column1 {
        width: min(220px, 100%);
        justify-self: start;
    }

    .controls-column2 {
        width: min(220px, 100%);
        justify-self: end;
    }

    .controls-column1,
    .controls-column2 {
        max-height: 100%;
        overflow-y: auto;
    }

    #tetris-grid-container {
        width: min(600px, 60vw, calc(100dvh - 105px));
        justify-self: center;
    }

    .footer p {
        margin: 6px 0;
    }
}


/* Mobile Portrait */
@media (max-width: 768px) {
    /* Make title bar text smaller on mobile */
    .title-bar-text {
        font-size: 12px;
    }
    
    .title-bar-controls img {
        width: 14px;
        height: 12px;
    }
    
    /* Stack layout vertically on mobile */
    .window-body-custom {
        flex-direction: column;
        padding: 10px;
        gap: 15px;
    }
    
    /* Make controls full width and reorder */
    .controls-column1,
    .controls-column2 {
        width: min(100%, 400px);
        order: 2; /* Controls come after canvas */
        margin: 0 auto;
        font-size: 12px;
    }
    
    /* Canvas container comes first and takes available space */
    #tetris-grid-container {
        position: relative;
        order: 1;
        width: min(90vw, calc(100dvh - 100px), 400px);
        height: auto;
        margin: 0 auto;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    
    /* Make canvas responsive */
    #tetris-canvas, 
    #three-container {
        position: absolute !important;
        top: 0 !important;
        left: 0 !important;
        width: 100% !important;
        height: 100% !important;
        max-width: 100% !important;
        max-height: 100% !important;
        border: 1px solid rgba(255, 255, 255, 0.8);
    }

    /* Keep each control panel aligned as a single readable menu. */
    .controls-column1,
    .controls-column2 {
        display: flex;
        flex-direction: column;
        gap: 10px;
    }
    
    .controls-column1 > .window,
    .controls-column2 > .window {
        flex: 1;
        min-width: 0;
        width: 100%;
        box-sizing: border-box;
    }
    
    /* Make checkbox grid more mobile-friendly */
    .pieces-checkbox {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 8px;
        padding: 8px;
    }
    
    .pieces-checkbox .field-row {
        flex-direction: column;
        align-items: center;
        justify-content: center;
        text-align: center;
        margin: 0;
        min-height: 48px;
    }

    .pieces-checkbox img {
        width: auto;
        height: auto;
        max-width: 53px;
        max-height: 40px;
        display: block;
        margin: 0 auto;
    }
    
    /* Adjust form controls for mobile */
    .input-control .field-row-stacked {
        margin-bottom: 8px;
    }
    
    .input-control label {
        font-size: 12px;
        margin-bottom: 2px;
    }
    
    .input-control input[type="range"] {
        width: 100%;
        margin: 5px 0;
    }

    .input-control input[type="text"],
    .input-control input[type="file"],
    .input-control select,
    .input-control button {
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        font-family: inherit;
        font-size: 16px;
    }

    .input-control input[type="text"],
    .input-control select,
    .input-control button {
        height: 44px;
        min-height: 44px;
        line-height: 1.25;
        padding: 8px 10px;
    }

    .input-control select {
        padding-right: 32px;
        background-position: right 6px center;
    }

    .input-control input[type="file"] {
        min-height: 44px;
        padding: 6px;
        line-height: 1.75;
    }

    .input-control input[type="file"]::file-selector-button {
        font: inherit;
    }
    
    .input-control button {
        min-width: auto;
        margin: 2px 0;
    }
    
    /* Welcome screen responsive */
    .welcome-screen {
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 90%;
        max-width: 350px;
        z-index: 1000;
    }
    
    .welcome-screen .window-body {
        padding: 15px;
    }
    
    .welcome-screen p {
        font-size: 11px;
        line-height: 1.4;
        margin: 8px 0;
    }
    
    /* Footer adjustments */
    .footer {
        flex-direction: column;
        text-align: center;
        padding: 15px 10px;
        gap: 8px;
    }
    
    .footer p {
        margin: 0;
        font-size: 11px;
    }
    
    /* Touch-friendly sizes */
    button {
        min-height: 40px;
        touch-action: manipulation;
    }
    
    input[type="range"] {
        height: 40px;
        margin: 8px 0;
    }
    
    input[type="checkbox"] + label {
        min-height: 40px;
        display: flex;
        align-items: center;
    }
    
    /* Prevent zoom on form inputs */
    input[type="text"],
    input[type="number"],
    select,
    textarea {
        font-size: 16px;
    }

        /* Ensure the Three.js canvas inside the container is properly sized */
    #three-container canvas {
        width: 100% !important;
        height: 100% !important;
        display: block;
    }
    
}

/* Mobile landscape keeps the same readable stack instead of narrow sidebars. */
@media (max-width: 768px) and (orientation: landscape) {
    .window-body-custom {
        gap: 10px;
        padding: 8px;
    }

    #tetris-grid-container {
        width: min(90vw, calc(100dvh - 80px), 400px);
        height: auto;
    }

    .pieces-checkbox {
        grid-template-columns: repeat(4, 1fr);
    }
}

/* Small phones (320px and below) */
@media (max-width: 480px) {
    .window-body-custom {
        padding: 5px;
        gap: 10px;
    }
    
    #tetris-grid-container {
        width: min(95vw, calc(100dvh - 80px), 320px);
        height: auto;
    }
    
    .pieces-checkbox {
        grid-template-columns: repeat(3, 1fr);
        gap: 6px;
    }
    
    .input-control button {
        padding: 6px 10px;
    }
    
    .footer {
        padding: 10px 5px;
    }
    
}

/* Very small phones */
@media (max-width: 360px) {
    .title-bar-text {
        font-size: 10px;
    }
    
    .pieces-checkbox {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .controls-column1,
    .controls-column2 {
        flex-direction: column;
        gap: 5px;
    }
    
    .controls-column1 > .window,
    .controls-column2 > .window {
        margin-bottom: 8px;
    }
}
