.pips-layout {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 2rem;
    max-width: 1400px;
    margin: 0 auto;
}

.pips-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2rem;
    padding: 2rem;
    background-color: #2a2a4a;
    border-radius: 15px;
    box-shadow: 0 8px 30px rgba(0,0,0,0.4);
    flex: 2; /* Takes up more space */
    min-width: 600px;
}

.legend-container {
    flex: 1;
    min-width: 250px;
    background-color: #2a2a4a;
    border-radius: 15px;
    padding: 1.5rem;
    box-shadow: 0 8px 30px rgba(0,0,0,0.4);
    color: #e0e0e0;
    max-height: 80vh;
    overflow-y: auto;
}

.legend-container h2 {
    margin-top: 0;
    color: #f0e8ff;
    border-bottom: 2px solid #5a3e8d;
    padding-bottom: 0.5rem;
    margin-bottom: 1rem;
}

.legend-item {
    display: flex;
    align-items: center;
    margin-bottom: 1rem;
    padding: 0.8rem;
    border-radius: 8px;
    background-color: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.legend-symbol {
    font-size: 1.5rem;
    font-weight: bold;
    margin-right: 1rem;
    width: 40px;
    height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 50%;
    background-color: #5a3e8d;
    color: white;
    flex-shrink: 0;
}

.legend-desc {
    font-size: 1rem;
    line-height: 1.4;
}

/* --- Existing Styles below --- */

.header-bar {
    display: flex;
    justify-content: space-between;
    width: 100%;
    align-items: center;
}

.button-group {
    display: flex;
    gap: 15px;
}

.check-btn {
    padding: 12px 25px;
    font-size: 1.1rem;
    color: white;
    background-color: #5a3e8d;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.3s;
}

.check-btn:hover {
    background-color: #7b58b4;
}

.clear-btn {
    padding: 12px 25px;
    font-size: 1.1rem;
    color: white;
    background-color: #d64545; 
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.3s;
}

.clear-btn:hover {
    background-color: #ff6b6b;
}

.rotate-btn {
    padding: 12px 20px;
    font-size: 1.4rem;
    color: white;
    background-color: #4a90d9;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.3s, transform 0.15s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 50px;
    /* Touch-friendly */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.rotate-btn:hover {
    background-color: #5aa0e9;
}

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

#game-board {
    display: grid;
    border: 3px solid #6b4fa8;
    background-color: #1a1a2e;
    position: relative;
    padding: 10px;
    border-radius: 10px;
    touch-action: none;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

:root {
    --cell-size: 80px;
}

.grid-cell {
    width: var(--cell-size);
    height: var(--cell-size);
    box-sizing: border-box;
    z-index: 2;
    transition: background-color 0.1s;
    background-color: transparent;
}

.grid-cell.empty {
    background-color: #0d0d1a;
    pointer-events: none;
}

.grid-cell.highlight-valid {
    background-color: rgba(100, 255, 100, 0.4);
}

.grid-cell.highlight-invalid {
    background-color: rgba(255, 100, 100, 0.4);
}

.region-cell-overlay {
    position: absolute;
    width: var(--cell-size);
    height: var(--cell-size);
    box-sizing: border-box;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    font-weight: bold;
    pointer-events: none;
    transition: opacity 0.2s;
}

.region-cell-overlay.occupied {
    opacity: 0.3;
}

.domino-tray {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
    padding: 1rem;
    background-color: #1a1a2e;
    border-radius: 10px;
    max-width: 100%;
}

.pips-container .domino, .domino-tray .domino {
    width: calc(var(--cell-size) * 2);
    height: var(--cell-size);
    display: flex;
    background-color: #f5f5dc;
    border: 3px solid #333;
    border-radius: 8px;
    cursor: grab;
    user-select: none;
    touch-action: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.15s ease-out, box-shadow 0.15s ease-out, opacity 0.15s ease-out;
    /* Minimum touch target size (44px recommended by Apple) */
    min-height: 44px;
}

.pips-container .domino:hover, .domino-tray .domino:hover {
    transform: scale(1.03);
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    border-color: #9d4edd;
}

.pips-container .domino:active, .domino-tray .domino:active {
    cursor: grabbing;
}

/* Picking up animation - fades out the original */
.pips-container .domino.picking-up, .domino-tray .domino.picking-up {
    opacity: 0.3;
    transform: scale(0.95);
    pointer-events: none;
}

/* Placed domino on board */
.pips-container .domino.placed {
    cursor: grab;
    z-index: 10;
}

.pips-container .domino.placed:hover {
    transform: rotate(var(--rotation, 0deg)) scale(1.05);
    box-shadow: 0 6px 20px rgba(0,0,0,0.4);
}

/* Drop animation */
.pips-container .domino.dropping {
    transition: left 0.2s cubic-bezier(0.34, 1.56, 0.64, 1),
                top 0.2s cubic-bezier(0.34, 1.56, 0.64, 1),
                transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1),
                opacity 0.15s ease-out;
}

/* Carried domino (being dragged) */
.domino.carried {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 9999;
    pointer-events: auto; /* Allow clicks for rotation */
    box-shadow: 0 15px 40px rgba(0,0,0,0.5);
    cursor: grabbing;
    /* Explicit dimensions since carried domino is appended to body, not container */
    width: calc(var(--cell-size) * 2);
    height: var(--cell-size);
    display: flex;
    background-color: #f5f5dc;
    border: 3px solid #333;
    border-radius: 8px;
    /* No transition - position is updated via JS game loop for smooth 60fps dragging */
    transition: none;
}

.domino-half {
    flex: 1 1 50%; /* Grow, shrink, but base at 50% - prevents blank half collapse */
    min-width: 0; /* Allow flex shrink to work properly */
    height: 100%;
    padding: 8px;
    box-sizing: border-box;
    display: grid;
    grid-template-rows: repeat(3, 1fr);
    grid-template-columns: repeat(3, 1fr);
    gap: 4px;
    color: #000000;
    position: relative; /* For pseudo-element positioning */
}

/* Separator using pseudo-element to avoid affecting width */
.domino-half:first-child::after {
    content: '';
    position: absolute;
    right: 0;
    top: 5px;   /* Small gap at top */
    bottom: 5px; /* Small gap at bottom */
    width: 1px;
    background-color: #ccc;
}

.domino-half .pip-dot {
    width: 14px;
    height: 14px;
    background-color: #000000;
    border-radius: 50%;
    align-self: center;
    justify-self: center;
    box-sizing: border-box;
    padding: 0 !important; /* Force override any Bootstrap padding */
}

.pip-dot.pos-1 { grid-row: 1; grid-column: 1; }
.pip-dot.pos-2 { grid-row: 1; grid-column: 3; }
.pip-dot.pos-3 { grid-row: 2; grid-column: 1; }
.pip-dot.pos-4 { grid-row: 2; grid-column: 2; }
.pip-dot.pos-5 { grid-row: 2; grid-column: 3; }
.pip-dot.pos-6 { grid-row: 3; grid-column: 1; }
.pip-dot.pos-7 { grid-row: 3; grid-column: 3; }

.modal {
    display: none;
    position: fixed;
    z-index: 100;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgba(0,0,0,0.6);
}

.modal-content {
    background-color: #2a2a4a;
    margin: 15% auto;
    padding: 30px;
    border: 1px solid #9d4edd;
    width: 80%;
    max-width: 400px;
    border-radius: 15px;
    text-align: center;
    color: #f0e8ff;
    box-shadow: 0 5px 25px rgba(157, 78, 221, 0.5);
}

.close-button {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
}

.close-button:hover {
    color: #fff;
}

/* Responsive styles */
@media (max-width: 768px) {
    .pips-layout {
        flex-direction: column;
        padding: 0.5rem;
        gap: 1rem;
    }

    .pips-container {
        min-width: auto;
        width: 100%;
        padding: 1rem;
    }

    .legend-container {
        min-width: auto;
        width: 100%;
        max-height: none;
    }

    .header-bar {
        flex-direction: column;
        gap: 1rem;
    }

    .header-bar h1 {
        margin: 0;
        font-size: 1.5rem;
    }

    .button-group {
        width: 100%;
        justify-content: center;
        flex-wrap: wrap;
    }

    /* Larger touch targets on mobile */
    .check-btn, .clear-btn, .rotate-btn {
        padding: 14px 20px;
        font-size: 1rem;
        min-height: 48px;
    }

    .domino-tray {
        padding: 0.75rem;
        gap: 8px;
    }

    /* Ensure minimum cell size for touch */
    :root {
        --cell-size: max(50px, min(80px, calc((100vw - 60px) / 6)));
    }
}

/* Extra small screens */
@media (max-width: 400px) {
    .button-group {
        gap: 8px;
    }

    .check-btn, .clear-btn, .rotate-btn {
        padding: 12px 14px;
        font-size: 0.9rem;
    }

    .header-bar h1 {
        font-size: 1.3rem;
    }
}
