/* ========================================
   TACTILE READER APP - STABLE LAYOUT
   Using CSS Grid + Flexbox architecture
   ======================================== */

/* === BASE LAYOUT === */
* {
    box-sizing: border-box;
}

:root {
    --navbar-size: 104px;
    /* Matches bottom bar: 80px buttons + 24px padding */
}

body {
    font-family: Verdana, sans-serif;
    background-color: #e0e0e0;
    margin: 0;
    padding: 0;
    min-height: 100vh;
    min-height: 100dvh;
    /* Dynamic viewport for mobile */
    overflow: hidden;

    /* CSS Grid Layout */
    display: grid;
    grid-template-rows: auto 1fr auto;
    grid-template-areas:
        "header"
        "main"
        "footer";
}

/* === TOP BAR (Header) === */
.top-bar {
    grid-area: header;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 20px;
    padding-top: max(8px, env(safe-area-inset-top));
    height: var(--navbar-size);
    z-index: 10;
}

.logo {
    display: flex;
    align-items: center;
    flex-shrink: 0;
}

.logo img {
    height: 50px;
    width: auto;
}

.bluetooth {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

.bluetooth button {
    background-color: transparent;
    border: none;
    padding: 0;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
}

.bluetooth img {
    height: 50px;
    width: auto;
}

.settings {
    display: flex;
    align-items: center;
    flex-shrink: 0;
}

.settings a {
    display: flex;
}

.settings img {
    height: 50px;
    width: auto;
    aspect-ratio: 1 / 1;
    object-fit: contain;
}

/* === MAIN CONTENT AREA === */
.main-area {
    grid-area: main;
    position: relative;
    margin: 0 20px;
    min-height: 0;
    /* Prevent grid blowout */
}

/* Camera Feed Container */
.canvas {
    position: absolute;
    inset: 0;
    border-radius: 28px;
    z-index: 5;
    overflow: hidden;
}

#camera-feed {
    display: none;
    width: 100%;
    height: 100%;
    object-fit: cover;
    background: #000;
}

body.camera-active #camera-feed {
    display: block;
}

#scan-overlay {
    position: absolute;
    inset: 0;
    display: none;
    z-index: 6;
    pointer-events: none;
}

body.camera-active #scan-overlay {
    display: block;
    pointer-events: auto;
}

#aruco-canvas {
    display: none;
}

/* Block Canvas Container */
.blocs {
    position: absolute;
    inset: 0;
    z-index: 10;
    display: flex;
    flex-direction: column;
}

#blocks-container {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    border-radius: 28px;
    background: #ffffff;
    box-sizing: border-box;
}

/* === BOTTOM BAR (Footer) === */
.bottom-bar {
    grid-area: footer;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 12px 0;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    height: var(--navbar-size);
    z-index: 10;
}

.buttons {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 20px;
}

.button {
    background-color: transparent;
    border: none;
    cursor: pointer;
}

.btn-circle {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    transition: transform 0.1s ease, background-color 0.3s ease;
}

.btn-circle:active {
    transform: scale(0.95);
}

.btn-yellow {
    background-color: #fcce41;
}

.btn-green {
    background-color: #28a745;
}

.btn-red {
    background-color: #ff5757;
}

.btn-blue {
    background-color: #4285f4;
}

.buttons img {
    width: 75%;
    height: 75%;
    object-fit: contain;
}

#trash img {
    transform: translateX(-1px);
}

/* === CAMERA ACTIVE MODE === */
body.camera-active .top-bar {
    display: none;
}

body.camera-active .main-area {
    margin: 0 5%;
}

body.camera-active .blocs {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: auto;
    height: var(--navbar-size);
    z-index: 20;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 0 10px;
    background: rgba(224, 224, 224, 0.3);
    border-bottom-left-radius: 10px;
    border-bottom-right-radius: 10px;
}

body.camera-active .canvas {
    top: var(--navbar-size);
    bottom: 0;
    left: 0;
    right: 0;
    border-radius: 28px;
}

body.camera-active #blocks-container {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    gap: 5px;
    padding: 5px 10px;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    background: transparent;
    height: 100%;
    border-radius: 0;
}

body.camera-active .block-wrapper {
    position: static !important;
    left: auto !important;
    top: auto !important;
    height: 90%;
    width: auto;
    min-width: 60px;
    margin: 0 5px;
    display: flex;
    align-items: center;
    flex-shrink: 0;
    touch-action: pan-x;
    cursor: pointer;
}

body.camera-active .solution-block {
    height: 100%;
    width: auto;
    max-height: 100%;
}

body.camera-active #selection-overlay {
    display: none !important;
}

body.camera-active #trash img {
    transform: translateX(-1px) scale(0.7);
}

body.camera-active #send img {
    transform: scale(0.7);
}

/* === BLOCK STYLING === */
.block-wrapper {
    position: absolute !important;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
    cursor: grab;
    padding: 4px;
    border-radius: 8px;
    transition: transform 0.15s ease, box-shadow 0.15s ease, opacity 0.3s ease;
    z-index: 10;
    will-change: transform, left, top;
    touch-action: none;
}

.block-wrapper img {
    pointer-events: none;
}

.block-wrapper.dragging {
    z-index: 1000;
    cursor: grabbing;
    filter: drop-shadow(0 10px 20px rgba(0, 0, 0, 0.3));
    transform: scale(1.05);
}

.block-wrapper.inactive {
    opacity: 0.4;
    filter: grayscale(0.5) opacity(0.5);
}

.block-wrapper[data-id="flag"] {
    filter: drop-shadow(0 0 10px rgba(255, 215, 0, 0.5));
}

.solution-block {
    height: 60px;
    width: auto;
    max-width: 80px;
    object-fit: contain;
}

.repeat-block {
    height: 40px;
    margin-top: -8px;
}

.repeat-block.selected {
    filter: drop-shadow(0 0 6px #fcce41);
}

/* === SELECTION STYLES === */
/* Single block selected */
.block-wrapper.selected {
    box-shadow: 0 0 0 3px #e91e63, 0 0 15px rgba(233, 30, 99, 0.4);
    border-radius: 12px;
}

/* Sequence selection overlay - single rectangle around entire sequence */
#selection-overlay {
    position: absolute;
    pointer-events: none;
    border: 3px solid #e91e63;
    border-radius: 16px;
    box-shadow: 0 0 20px rgba(233, 30, 99, 0.4);
    z-index: 5;
    display: none;
}

#selection-overlay.active {
    display: block;
}

#blocs-canvas {
    display: none;
}

/* Snap Highlighting */
.snap-target-right::after {
    content: '';
    position: absolute;
    top: 10%;
    right: -10px;
    width: 10px;
    height: 80%;
    background: rgba(66, 133, 244, 0.5);
    border-radius: 0 4px 4px 0;
    pointer-events: none;
}

.snap-target-bottom::after {
    content: '';
    position: absolute;
    bottom: -10px;
    left: 5%;
    width: 40%;
    height: 10px;
    background: rgba(50, 168, 82, 0.5);
    border-radius: 0 0 4px 4px;
    pointer-events: none;
}

/* === SWEETALERT2 POPUP STYLING === */
.swal2-popup {
    border-radius: 28px !important;
    font-family: Verdana, sans-serif !important;
    padding: 1.5em !important;
}

.swal2-title {
    font-size: 1.4em !important;
}

.swal2-html-container {
    font-size: 1em !important;
}

.swal2-styled.swal2-confirm {
    border-radius: 50px !important;
    padding: 12px 35px !important;
    font-family: Verdana, sans-serif !important;
}

/* Bluetooth Popup Styles */
.popup-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    margin: 20px 0;
    justify-items: center;
    align-items: center;
}

.popup-item {
    text-align: center;
    padding: 10px;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.popup-img {
    height: auto;
    min-height: 80px;
    max-height: 20vh;
    max-width: 100%;
    object-fit: contain;
}

.robot-select-btn {
    margin-top: 8px;
    padding: 8px 20px;
    border: none;
    border-radius: 20px;
    background-color: #4285f4;
    color: white;
    font-family: Verdana, sans-serif;
    font-size: 0.9em;
    font-weight: bold;
    cursor: pointer;
    transition: background-color 0.2s, transform 0.1s;
}

.robot-select-btn:hover {
    background-color: #3367d6;
}

.robot-select-btn:active {
    transform: scale(0.95);
}

.robot-label {
    font-weight: bold;
    font-size: 1em;
    color: #333;
    margin-bottom: 8px;
    text-transform: capitalize;
}

@media (orientation: landscape) {
    body {
        grid-template-columns: var(--navbar-size) 1fr var(--navbar-size);
        grid-template-rows: 1fr;
        grid-template-areas: "header main footer";
    }

    .top-bar {
        flex-direction: column;
        justify-content: space-between;
        align-items: center;
        padding: 20px 10px;
        width: var(--navbar-size);
        height: auto;
    }

    /* Larger icons in landscape for better visibility */
    .logo img,
    .bluetooth img,
    .settings img {
        height: 60px;
    }

    .bluetooth {
        position: static;
        transform: none;
        margin: auto 0;
    }

    .main-area {
        margin: 20px 0;
    }

    #blocks-container {
        border-radius: 20px;
    }

    .bottom-bar {
        flex-direction: column;
        padding: 0 12px;
        padding-right: max(12px, env(safe-area-inset-right));
        width: var(--navbar-size);
        height: auto;
    }

    /* Ensure buttons stay at full size in landscape */
    .btn-circle {
        width: 80px;
        height: 80px;
    }

    .buttons {
        flex-direction: column;
        gap: 15px;
    }

    /* Camera Active in Landscape */
    body.camera-active .top-bar {
        display: none;
    }

    body.camera-active .main-area {
        margin: 0;
    }

    body.camera-active .blocs {
        top: 0;
        left: 0;
        right: 0;
        height: var(--navbar-size);
        border-radius: 0 0 10px 10px;
    }

    body.camera-active .canvas {
        top: var(--navbar-size);
        border-radius: 20px;
    }

    .popup-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .popup-img {
        max-height: 30vh;
    }
}

/* === SMALL LANDSCAPE SCREENS (phones) === */
/* Only adjust spacing, NOT sizes - margins stay consistent */
@media (orientation: landscape) and (max-height: 500px) {
    .buttons {
        gap: 10px;
    }
}

/* === VERY SMALL LANDSCAPE (height < 350px) === */
/* Only reduce button gaps, NOT margins - margins stay consistent */
@media (orientation: landscape) and (max-height: 350px) {
    .buttons {
        gap: 6px;
    }
}

/* === DESKTOP ADJUSTMENTS === */
@media (min-width: 768px) and (orientation: portrait) {
    .main-area {
        margin: 0 2%;
    }
}
