* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    margin: 0;
    padding: 0;
}

/* Top navigation bar */
.navbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    width: 100%;
    z-index: 1500;
    background: rgba(255, 255, 255, 0.15);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(255, 255, 255, 0.15);
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    padding: 10px 16px;
}

.nav-title { display: flex; justify-content: center; }
.nav-title h1 { margin: 0; color: white; font-size: 1.5rem; }
.nav-actions { display: flex; justify-content: flex-end; }
.icon-btn {
    background: rgba(255, 255, 255, 0.15) !important;
    color: white !important;
    border: 1px solid rgba(255, 255, 255, 0.3) !important;
    border-radius: 50%;
    width: 32px;
    height: 32px;
    font-size: 16px;
    font-weight: 600;
    font-style: italic;
    font-family: serif;
    cursor: pointer;
    backdrop-filter: blur(10px);
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    /* Reset mobile button styles */
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    outline: none;
}
.icon-btn:hover { 
    background: rgba(255, 255, 255, 0.25) !important;
    border-color: rgba(255, 255, 255, 0.5) !important;
}

html, body {
    overscroll-behavior: none; /* prevent scroll chaining/bounce while dragging */
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
    padding-top: 80px; /* Account for fixed navbar height */
}

header {
    text-align: center;
    margin-bottom: 30px;
    color: white;
}

header h1 {
    font-size: 3rem;
    margin-bottom: 10px;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

header p {
    font-size: 1.2rem;
    opacity: 0.9;
    margin-bottom: 20px;
}

.game-actions {
    display: flex;
    gap: 12px;
    margin-bottom: 10px;
    justify-content: center;
    align-items: center;
}

.action-button {
    background: rgba(255, 255, 255, 0.9);
    color: #667eea;
    border: none;
    padding: 12px 24px;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    min-width: 140px;
    text-align: center;
}

.action-button:hover {
    background: white;
    transform: translateY(-2px);
    box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
}

#reset-btn {
    background: #ff6b6b;
    color: white;
}

#reset-btn:hover {
    background: #ee5a5a;
}

.attempts-display {
    text-align: center;
    margin-bottom: 10px;
}

.attempts-counter {
    color: white;
    font-size: 0.9rem;
    font-weight: 600;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
    opacity: 0.9;
}

/* Match timer style to attempts */
.timer-counter {
    color: white;
    font-size: 0.9rem;
    font-weight: 600;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
    opacity: 0.9;
    margin-left: 12px;
}

/* Start overlay */
.start-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.35);
    backdrop-filter: blur(2px);
    border-radius: 12px;
    z-index: 1200;
}
.start-button {
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: #fff;
    font-weight: 700;
    font-size: 1.1rem;
    border: 1px solid rgba(255,255,255,0.3);
    border-radius: 10px;
    padding: 10px 18px;
    cursor: pointer;
    box-shadow: 0 8px 24px rgba(0,0,0,0.25);
}
.start-button:hover {
    filter: brightness(1.05);
}
.hidden { display: none !important; }

main {
    display: flex;
    gap: 40px;
    align-items: flex-start;
    justify-content: center;
    flex-wrap: wrap;
}

/* Ensure each page section mirrors the original main layout */
.page {
    display: flex;
    gap: 40px;
    align-items: flex-start;
    justify-content: center;
    flex-wrap: nowrap; /* keep side-by-side on desktop */
}

/* Make Home section behave like direct children of main (restores original layout) */
#page-home.page {
    display: contents;
}

.game-board-container {
    flex: 0 0 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.game-board {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    grid-template-rows: repeat(5, 1fr);
    gap: 3px;
    background: rgba(255, 255, 255, 0.15);
    padding: 15px;
    border-radius: 16px;
    backdrop-filter: blur(15px);
    box-shadow: 
        0 12px 24px rgba(0, 0, 0, 0.3),
        0 0 0 1px rgba(255, 255, 255, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.1);
    position: relative;
    touch-action: none; /* disable browser touch gestures inside board while dragging */
}

.board-cell {
    width: 60px;
    height: 60px;
    background: rgba(255, 255, 255, 0.95);
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.6rem;
    font-weight: 700;
    color: #2c3e50;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    box-shadow: 
        0 2px 8px rgba(0, 0, 0, 0.1),
        inset 0 1px 0 rgba(255, 255, 255, 0.6);
}

.board-cell.highlight-valid {
    background: rgba(76, 175, 80, 0.7);
    border-color: #4caf50;
    transform: scale(1.05);
}

.board-cell.highlight-invalid {
    background: rgba(244, 67, 54, 0.7);
    border-color: #f44336;
    transform: scale(1.05);
}

.board-cell.occupied {
    /* Background color will be set by JavaScript per piece */
    /* Border color remains default from .board-cell */
}

.board-cell.correct {
    box-shadow: 
        0 2px 8px rgba(0, 0, 0, 0.1),
        inset 0 1px 0 rgba(255, 255, 255, 0.6),
        0 0 0 3px #4caf50; /* Green outline for correct pieces */
}

.board-cell.incorrect {
    box-shadow: 
        0 2px 8px rgba(0, 0, 0, 0.1),
        inset 0 1px 0 rgba(255, 255, 255, 0.6),
        0 0 0 3px #f44336; /* Red outline for incorrect pieces */
}

.pieces-container {
    flex: 1;
    min-width: 300px;
}

.pieces-container h3 {
    color: white;
    text-align: center;
    margin-bottom: 20px;
    font-size: 1.5rem;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
}

.pieces-tray {
    background: rgba(255, 255, 255, 0.15);
    border-radius: 16px;
    padding: 25px;
    min-height: 300px;
    width: 100%;
    backdrop-filter: blur(15px);
    box-shadow: 
        0 12px 24px rgba(0, 0, 0, 0.3),
        0 0 0 1px rgba(255, 255, 255, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.1);
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    grid-auto-rows: 170px;
    gap: 24px;
    align-content: start;
    justify-content: center;   /* center grid tracks within tray */
    justify-items: center;     /* center each piece within its grid cell */
    align-items: center;       /* vertically center pieces within their cells */
    touch-action: pan-y;       /* allow vertical scroll but not pinch/browser gestures on pieces */
}

.piece {
    position: relative;
    cursor: grab;
    transition: transform 0.2s ease;
    user-select: none;
    margin: 0px;
    touch-action: none; /* ensure elements themselves don't trigger panning */
}

.piece:hover {
    transform: scale(1.05);
}

.piece.dragging {
    cursor: grabbing;
    transform: scale(1.1);
    z-index: 1000;
    opacity: 0.9;
}

.piece-cell {
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.98);
    border: 2px solid rgba(102, 126, 234, 0.4);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.3rem;
    font-weight: 700;
    color: #2c3e50;
    position: absolute;
    box-shadow: 
        0 4px 8px rgba(0, 0, 0, 0.15),
        inset 0 1px 0 rgba(255, 255, 255, 0.8);
    transition: all 0.2s ease;
}

.board-piece {
    position: absolute !important;
    z-index: 10;
}

.board-piece-cell {
    position: absolute;
    background: transparent;
    pointer-events: none;
    box-sizing: border-box;
    border-radius: 10px; /* match .board-cell radius exactly */
    /* Draw outline without affecting size to avoid any pixel drift */
    box-shadow: inset 0 0 0 2px rgba(102, 126, 234, 0.85);
}

.rotate-button {
    position: absolute;
    top: -10px;
    right: -10px;
    width: 28px;
    height: 28px;
    background: #2196f3;
    color: white;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    z-index: 20;
    transition: transform 0.2s ease, background 0.3s ease;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.rotate-button:hover {
    background: #1976d2;
    transform: scale(1.1);
}

/* Hide rotate button when piece is on the board */
.board-piece .rotate-button {
    display: none !important;
}

.win-message {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 50px;
    border-radius: 20px;
    text-align: center;
    box-shadow: 
        0 20px 40px rgba(0, 0, 0, 0.3),
        0 0 0 1px rgba(255, 255, 255, 0.1);
    z-index: 2000;
    max-width: 450px;
    backdrop-filter: blur(20px);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.win-close {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 24px;
    height: 24px;
    line-height: 24px;
    border: none;
    border-radius: 50%;
    background: rgba(255,255,255,0.25);
    color: rgba(255,255,255,0.95);
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.win-close:hover {
    background: rgba(255,255,255,0.35);
}

/* Board complete outline */
.game-board.complete {
    outline: 3px solid #4caf50 !important; /* clear, visible ring */
    outline-offset: 0;
    box-shadow:
        0 12px 24px rgba(0, 0, 0, 0.3),
        0 0 0 1px rgba(255, 255, 255, 0.2);
}

/* Info modal */
.modal {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    width: min(480px, 90vw);
    max-height: 80vh;
    overflow: auto;
    border-radius: 16px;
    padding: 20px 20px 24px;
    backdrop-filter: blur(20px);
    box-shadow: 
        0 20px 40px rgba(0,0,0,0.3),
        0 0 0 1px rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.1);
    z-index: 3000;
}
.modal.hidden { display: none !important; }
.modal-close {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 28px;
    height: 28px;
    border: none;
    border-radius: 50%;
    background: rgba(255,255,255,0.15);
    color: white;
    font-size: 18px;
    cursor: pointer;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.2);
}
.modal-close:hover {
    background: rgba(255,255,255,0.25);
}
.info-image {
    display: block;
    width: 70%;
    height: auto;
    margin: 12px auto 0;
    border-radius: 12px;
    box-shadow: 0 6px 16px rgba(0,0,0,0.2);
}

.win-message h2 {
    color: #ffffff;
    margin-bottom: 20px;
    font-size: 3rem;
    font-weight: 800;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
    letter-spacing: 2px;
}

.win-message p {
    color: rgba(255, 255, 255, 0.9);
    margin-bottom: 30px;
    font-size: 1.2rem;
    font-weight: 300;
}

.share-blurb {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 10px;
    align-items: start;
    margin-bottom: 18px;
}

#share-blurb {
    background: rgba(255,255,255,0.15);
    color: #fff;
    padding: 12px 14px;
    border-radius: 10px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    white-space: pre-wrap;
    min-width: 220px;
}

.copy-btn {
    background: rgba(255,255,255,0.9);
    color: #667eea;
    border: none;
    padding: 10px 14px;
    border-radius: 10px;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 4px 10px rgba(0,0,0,0.2);
}

.copy-btn:hover {
    background: #ffffff;
}

.win-message button {
    background: rgba(255, 255, 255, 0.9);
    color: #667eea;
    border: none;
    padding: 15px 30px;
    border-radius: 12px;
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}

.win-message button:hover {
    background: white;
    transform: translateY(-2px);
    box-shadow: 0 12px 20px rgba(0, 0, 0, 0.3);
}

.win-actions {
    display: grid;
    grid-template-columns: 1fr auto 1fr; /* center column auto-sizes to the button */
    align-items: center;
    gap: 12px;
    width: 100%;
}

.win-actions > * {
    grid-column: 2;            /* place any child (Close button) in the center column */
    justify-self: center;      /* ensure it's centered in its grid cell */
}

#win-dismiss {
    background: rgba(255,255,255,0.5);
    color: #333;
}

#win-dismiss:hover {
    background: rgba(255,255,255,0.8);
}

.hidden {
    display: none !important;
}

/* Side panel */
.side-panel {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    width: min(300px, 85vw);
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: #fff;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    z-index: 2600;
    box-shadow: 4px 0 20px rgba(0,0,0,0.35);
    border-right: 1px solid rgba(255,255,255,0.12);
    display: flex;
    flex-direction: column;
}

.side-panel.open {
    transform: translateX(0);
}

.side-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px;
    border-bottom: 1px solid rgba(255,255,255,0.15);
    background: rgba(255,255,255,0.06);
    backdrop-filter: blur(10px);
}

.side-panel-title {
    font-size: 1.1rem;
    font-weight: 700;
}

.side-panel-nav {
    display: flex;
    flex-direction: column;
    padding: 10px;
    gap: 8px;
}

.menu-item {
    width: 100%;
    text-align: left;
    background: rgba(255,255,255,0.15);
    color: #fff;
    border: 1px solid rgba(255,255,255,0.25);
    border-radius: 10px;
    padding: 10px 12px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s ease, transform 0.15s ease;
}

.menu-item:hover {
    background: rgba(255,255,255,0.25);
    transform: translateX(2px);
}

.menu-item.active {
    background: rgba(255,255,255,0.35);
}

.side-panel-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 2550;
}

@media (max-width: 768px) {
    /* Fill viewport and keep board + tray visible; tray scrolls internally */
    html, body {
        height: auto;
        overflow: auto; /* allow page scroll */
    }

    /* Navbar mobile styles */
    .navbar {
        padding: 8px 12px;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        width: 100%;
        z-index: 1500;
    }

    .nav-title h1 {
        font-size: 1.3rem;
    }

    .icon-btn {
        width: 28px;
        height: 28px;
        font-size: 14px;
        min-width: 28px; /* Ensure button doesn't shrink */
        text-align: center;
        -webkit-text-size-adjust: 100%; /* Prevent iOS text scaling */
        background: rgba(102, 126, 234, 0.7) !important; /* More solid purple background for mobile */
        color: white !important;
        border: 1px solid rgba(255, 255, 255, 0.5) !important;
        backdrop-filter: none !important; /* Remove backdrop filter on mobile */
        -webkit-appearance: none !important;
        -moz-appearance: none !important;
        appearance: none !important;
    }

    .container {
        height: auto; /* allow content to define height so page can scroll */
        display: flex;
        flex-direction: column;
        padding: 10px;
        padding-top: 60px; /* Account for smaller mobile navbar */
    }

    header {
        margin-bottom: 10px;
    }

    /* Modal mobile styles */
    .modal {
        width: 95vw;
        max-height: 90vh;
        padding: 16px;
    }

    .modal h2 {
        font-size: 1.5rem;
        margin-bottom: 12px;
    }

    .info-image {
        width: 85%;
    }

    main {
        flex: 1 1 auto;
        min-height: 0; /* allow children to shrink and enable internal scroll */
        display: flex;
        flex-direction: column;
        gap: 12px;
        align-items: center;
        justify-content: flex-start;
    }
    .page {
        display: flex;
        flex-direction: column;
        gap: 12px;
        align-items: center;
        justify-content: flex-start;
    }

    .game-board-container {
        flex: 0 0 auto;
        align-self: center;
    }

    .game-actions {
        margin-bottom: 15px;
    }

    .action-button {
        padding: 10px 20px;
        font-size: 0.9rem;
    }

    .pieces-container {
        flex: 1 1 auto;
        min-height: 0; /* critical for nested overflow */
        width: 100%;
    }

    .pieces-tray {
        height: 100%;
        overflow-y: auto;            /* scroll only the tray */
        -webkit-overflow-scrolling: touch;
        gap: 16px;                   /* increased spacing between pieces */
        grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
        grid-auto-rows: 160px;       /* increased height to prevent vertical overlap */
    }

    .board-cell {
        width: 50px;
        height: 50px;
        font-size: 1.3rem;
    }
    
    .piece-cell {
        width: 36px;                 /* slightly larger cells for better touch */
        height: 36px;
        font-size: 1rem;
    }
}

/* Ensure tray sits below the board on most handheld/tablet layouts */
@media (max-width: 1024px), (orientation: portrait) {
    main {
        display: flex;
        flex-direction: column;
        flex-wrap: nowrap; /* prevent side-by-side wrapping */
        align-items: center;
        justify-content: flex-start;
        gap: 12px;
    }
    .page {
        display: flex;
        flex-direction: column;
        flex-wrap: nowrap;
        align-items: center;
        justify-content: flex-start;
        gap: 12px;
    }

    .game-board-container { order: 0; }
    .pieces-container { order: 1; width: 100%; }

    .pieces-tray {
        max-height: 40vh;   /* visible below board */
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        touch-action: pan-y; /* allow vertical scrolling in tray */
    }
}
