/* Container and Preview */
.jigsaw-puzzle-container {
    max-width: 1400px; /* Modern web standard for content width */
    margin: 0 auto;
    text-align: center;
    font-family: Arial, sans-serif;
    touch-action: manipulation;
    padding: 0 20px; /* Side margins for breathing room */
    box-sizing: border-box;
}

/* Improve control wrapping on medium screens */
@media (max-width: 1024px) {
  .puzzle-controls-wrapper {
    flex-wrap: wrap;
  }

  /* For 108-piece pool, default to zero inset to avoid any perceived nip */
  .jigsaw-puzzle-container[data-piece-count="108"] .puzzle-area.split-mode .pool-pieces .puzzle-piece {
    --pool-inset: 0px;
  }
}

.puzzle-preview {
    margin-bottom: 10px;
    display: block;
    width: 50%;
    max-width: 768px;
    margin-left: auto;
    margin-right: auto;
    overflow: hidden;
    aspect-ratio: 8/5;
}

/* Mobile: Compact preview - FULL WIDTH with zero margin */
@media (max-width: 767px) {
    .jigsaw-puzzle-container .puzzle-preview {
        width: calc(100% + 20px) !important; /* bleed into container padding */
        max-width: 100% !important;
        margin-left: -10px !important;
        margin-right: -10px !important;
        margin-bottom: 4px !important;
        padding: 0 !important;
        /* Larger preview on mobile */
        max-height: 220px !important;
        min-height: 140px !important;
        aspect-ratio: auto !important; /* remove global constraint */
        height: auto !important;
    }
    /* Keep preview visible after start */
    .jigsaw-puzzle-container .puzzle-preview {
        display: block !important;
    }
    /* Make preview image truly full width */
    .jigsaw-puzzle-container .puzzle-preview img {
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        padding: 0 !important;
    }
}

.puzzle-preview img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
    margin: 0 auto;
}

/* Buttons */
.jigsaw-puzzle-container button,
.jigsaw-puzzle-container .puzzle-all-controls button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: 40px;
    padding: 0 20px;
    border: none;
    border-radius: 8px;
    background: #008080;
    color: #ffffff;
    font-size: 15px;
    font-weight: 500;
    line-height: 1;
    cursor: pointer;
    transition: background-color 0.2s ease, transform 0.2s ease, box-shadow 0.2s ease, opacity 0.2s ease;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    max-width: 200px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    margin: 4px;
}

/* START PUZZLE and SQUARE PIECES buttons - white text on teal */
.jigsaw-puzzle-container .start-button,
.jigsaw-puzzle-container .shape-toggle-button {
    background: #008080 !important;
    color: #ffffff !important;
    font-weight: 600;
}

.jigsaw-puzzle-container button:hover,
.jigsaw-puzzle-container .puzzle-all-controls button:hover {
    background: #006666;
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.jigsaw-puzzle-container button:active,
.jigsaw-puzzle-container .puzzle-all-controls button:active {
    background: #005555;
    transform: translateY(1px);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.jigsaw-puzzle-container button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    background: #666666;
}

/* Icon Buttons */
.jigsaw-puzzle-container .icon-button {
    width: 40px;
    height: 40px;
    padding: 8px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: #008080;
    margin: 4px;
    transition: background-color 0.2s ease, transform 0.2s ease, box-shadow 0.2s ease;
}

.jigsaw-puzzle-container .icon-button:hover {
    background: #006666;
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.jigsaw-puzzle-container .icon-button:active {
    background: #005555;
    transform: translateY(1px);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

/* Accessible focus styles for all buttons */
.jigsaw-puzzle-container button:focus,
.jigsaw-puzzle-container .icon-button:focus,
.jigsaw-puzzle-container .shape-toggle-button:focus,
.jigsaw-puzzle-container .split-mode-button:focus {
  outline: 3px solid #3ba0a0;
  outline-offset: 2px;
}

.jigsaw-puzzle-container .icon-button img {
    width: 20px;
    height: 20px;
    filter: brightness(0) invert(1) saturate(0); /* Ensure icons are white */
    transition: transform 0.2s ease;
}

.jigsaw-puzzle-container .icon-button:hover img {
    transform: scale(1.1);
}

/* Music and Sound Buttons */
.jigsaw-puzzle-container .mute-music-button,
.jigsaw-puzzle-container .mute-sounds-button {
    width: 40px;
    padding: 8px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* Mobile Icon Buttons */
@media (max-width: 767px) {
    .jigsaw-puzzle-container .icon-button {
        width: 36px;
        height: 36px;
        padding: 6px;
        flex-shrink: 0;
    }

    .jigsaw-puzzle-container .icon-button img {
        width: 18px;
        height: 18px;
    }
    
    /* Make start button more compact but readable */
    .jigsaw-puzzle-container .start-button {
        font-size: 12px !important;
        padding: 0 10px !important;
        height: 36px !important;
        white-space: nowrap;
        flex-shrink: 0;
        min-width: 80px;
    }
}

/* Button Layout */
.jigsaw-puzzle-container .puzzle-all-controls {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 12px;
    margin: 15px 0;
    padding: 0 10px;
}

/* Button Groups */
.jigsaw-puzzle-container .puzzle-controls-main {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
    margin-bottom: 8px;
}

.jigsaw-puzzle-container .puzzle-controls {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
}

/* Controls Layout */
.puzzle-controls-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    margin: 10px 0;
    flex-wrap: nowrap;
}

/* Mobile: Ultra-compact controls */
@media (max-width: 767px) {
    .puzzle-controls-wrapper {
        margin: 4px 0 !important;
        gap: 4px !important;
        padding: 2px 4px !important;
    }
}

.puzzle-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 0;
}

.timer-wrapper {
    display: flex;
    align-items: center;
    gap: 5px;
    margin: 0;
    white-space: nowrap;
}

.timer-wrapper::before {
    content: "Time:";
    color: teal;
    font-size: 24px;
}

/* Area Labels */
.pool-label, .completion-label {
    margin-top: 10px;
    margin-bottom: 5px;
    color: #333;
    font-weight: bold;
    text-align: center;
}

/* Mobile: Minimal labels */
@media (max-width: 767px) {
    .pool-label, .completion-label,
    .puzzle-pool h3, .puzzle-completion h3 {
        font-size: 12px !important;
        margin: 2px 0 !important;
        padding: 0 !important;
        line-height: 1.2 !important;
    }
}

/* Puzzle Area - Normal Mode */
.puzzle-area {
    position: relative;
    width: 100%;
    max-width: 1280px;
    margin: 0 auto;
    aspect-ratio: 8/5;
    margin-bottom: 20px;
    z-index: 1000;
    overflow: visible; /* ensure extreme left/right bulges are visible */
}

.puzzle-area:not(.split-mode) .scrambled-pieces {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(5, 1fr);
    gap: 1px;
    background-color: #f0f0f0;
    overflow: visible; /* allow knob protrusions to render outside grid cells */
    /* Safari paint stability - prevent grey flashing on orientation change */
    will-change: transform, background-position, background-size;
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
}

/* When classic shapes are active, remove the grid gap and background to hide seams */
.puzzle-area:not(.split-mode) .scrambled-pieces.classic-mode {
    gap: 0;
    background: transparent;
}

/* Base puzzle piece styles (desktop) */
.puzzle-piece {
    width: 100%;
    height: 100%;
    background-size: 800% 500%;
    background-position: var(--bg-x) var(--bg-y);
    cursor: move;
    cursor: -webkit-grab;
    cursor: grab;
    border: 1px solid rgba(0, 0, 0, 0.1);
    transition: transform 0.2s ease, opacity 0.2s ease, border-color 0.2s ease;
    z-index: 1;
    overflow: visible;
    /* Performance optimizations */
    will-change: transform;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    transform: translateZ(0);
}

/* Ensure embedded SVG fills each tile (for classic shapes) */
.puzzle-piece svg {
    width: 100%;
    height: 100%;
    display: block;
}

/* Note: Classic shapes now use SVG <image> elements for proper clipping.
   CSS backgrounds are no longer needed for classic pieces. */

/* Classic shapes: transparent background by default (SVG provides the image) */
.puzzle-piece.classic-shape {
    background: transparent;
    border: none;
}

/* In pool, classic pieces need background images for visibility - set by JavaScript */

.puzzle-piece:hover {
    opacity: 0.8;
    transform: scale(1.05);
    z-index: 1002;
}

.puzzle-piece.dragging {
    cursor: -webkit-grabbing;
    cursor: grabbing;
    opacity: 0.8;
    transform: scale(1.05) translateZ(0); /* GPU acceleration */
    z-index: 1000;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    will-change: transform;
}

.puzzle-piece.highlight {
    border: 2px solid #4CAF50;
    box-shadow: 0 0 10px rgba(76,175,80,0.5);
}

.puzzle-piece.selected {
    opacity: 0.7;
    transform: scale(1.05);
    z-index: 999;
    border: 2px solid #2196F3;
    box-shadow: 0 0 10px rgba(33,150,243,0.5);
}

/* Mobile-specific styles */
@media (max-width: 768px) {
    .puzzle-piece {
        touch-action: none;
        user-select: none;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
    }

    .puzzle-piece:hover {
        opacity: 1;
        transform: none;
    }

    .puzzle-piece.dragging {
        opacity: 0.8;
        transform: scale(1.02);
        z-index: 1000;
        border: none;
        box-shadow: none;
    }

    .puzzle-piece.highlight {
        border: 3px solid #4CAF50;
    }

    .puzzle-piece.selected {
        opacity: 0.8;
        transform: scale(1.02);
        z-index: 999;
        border: 3px solid #2196F3;
    }
}

/* Split Mode */
.split-mode-wrapper {
    display: flex;
    flex-direction: row;
    gap: 20px;
    width: 100%;
    max-width: 1300px; /* Constrain split mode width */
    margin: 0 auto; /* Center the split mode */
}

/* Mobile: Ultra-tight split mode */
@media (max-width: 767px) {
    .split-mode-wrapper {
        gap: 4px !important;
        margin: 0 !important;
    }
}

.puzzle-pool {
    display: flex;
    flex-direction: column;
    gap: 0;
    background: #f5f5f5;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.puzzle-completion {
    display: flex;
    flex-direction: column;
    gap: 0;
    background: #f5f5f5; /* light grey background */
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* 40-piece vertical stack: pool above completion (same as 108-piece layout) */
.jigsaw-puzzle-container[data-piece-count="40"] .split-mode-wrapper {
    flex-direction: column;
    gap: 24px;
}

.jigsaw-puzzle-container[data-piece-count="40"] .puzzle-pool {
    min-height: 300px;
}

.jigsaw-puzzle-container[data-piece-count="40"] .puzzle-completion {
    min-height: 400px;
}

.puzzle-pool {
    border-radius: 8px;
    overflow: hidden; /* keep pool pieces contained */
    padding: 0;
}

.puzzle-completion {
    border-radius: 0;
    overflow: visible;
    padding: 12px;
}

.completion-pieces {
    width: 100%;
    aspect-ratio: 8/5;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(5, 1fr);
    gap: 0;
    background: transparent;
    padding: 20px;
    box-sizing: border-box;
    overflow: visible;
    position: relative;
}

.completion-pieces .puzzle-piece {
    width: 100%;  /* Fill grid cells completely, no gaps */
    height: 100%;   /* Fill grid cells completely */
    overflow: visible !important;
    z-index: 1;
    border: none;  /* Remove borders to eliminate gaps between classic shapes */
}

.completion-pieces .puzzle-piece svg {
    width: 100%;
    height: 100%;
}

.puzzle-area.split-mode .puzzle-completion {
    padding: 12px;
}

.puzzle-area.split-mode .puzzle-completion h3 {
    margin: 8px 12px;
    padding: 0;
}

.pool-pieces {
    display: grid;
    gap: 1px;
    padding: 0;
    background: white;
    border-radius: 4px;
    box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
    overflow: hidden; /* ensure no scrollbars */
}

/* Default 40-piece grid (non-split mode) */
.jigsaw-puzzle-container[data-piece-count="40"]:not(.split-mode) .pool-pieces,
.jigsaw-puzzle-container:not([data-piece-count]) .pool-pieces {
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(5, 1fr);
    aspect-ratio: 8/5;
}

/* Split Mode: keep pool contained and give a small inner gutter */
.puzzle-area.split-mode .pool-pieces {
    overflow: auto; /* Enable scrolling by default */
    padding: 20px;
    --pool-piece-scale: 1.0; /* NO SCALING */
    border-radius: 8px;
    background: #f5f5f5;
}

/* Desktop: 40-piece pool tray -> 2 rows with horizontal scroll (same as 108-piece) */
@media (min-width: 768px) {
  .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .pool-pieces {
    /* Override default grid to create many implicit columns with fixed track size */
    display: grid;
    grid-template-rows: repeat(2, var(--pool-tile-size, 72px));
    grid-template-columns: none; /* cancel earlier 8-col rule */
    grid-auto-rows: var(--pool-tile-size, 72px);
    grid-auto-flow: column;
    grid-auto-columns: var(--pool-tile-size, 72px);
    gap: 8px;
    padding: 12px;
    width: 100%;
    max-width: calc(100vw - 160px); /* prevent extending too far right */
    overflow-x: auto; /* enable horizontal scroll */
    overflow-y: hidden; /* prevent vertical scrollbar */
    aspect-ratio: auto; /* cancel any previous aspect ratio constraints */
    -webkit-overflow-scrolling: touch;
    scrollbar-gutter: stable both-edges;
    align-content: start;
    --pool-tile-size: 74px;
    padding-bottom: 64px; /* more slack to clear overlay scrollbar on Safari */
    align-items: start;
    justify-items: center;
  }

  /* Ensure tiles are square and not scaled further in the pool tray */
  .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .pool-pieces .puzzle-piece {
    width: var(--pool-tile-size, 72px);
    height: var(--pool-tile-size, 72px);
    transform: none; /* override pool scaling for clarity when scrolling */
    box-sizing: border-box; /* include border in the tile size to avoid clipping */
    overflow: visible; /* allow inner SVG bleed if any */
  }

  /* Styled scrollbar for 40-piece pool (desktop) */
  .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .pool-pieces::-webkit-scrollbar {
    height: 16px;
  }
  .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .pool-pieces::-webkit-scrollbar-track {
    background: #e0e0e0;
    border-radius: 8px;
  }
  .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .pool-pieces::-webkit-scrollbar-thumb {
    background: #008080;
    border-radius: 8px;
    border: 2px solid #e0e0e0;
  }
  .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .pool-pieces::-webkit-scrollbar-thumb:hover {
    background: #006666;
  }

  /* Remove excessive whitespace: override large min-heights for 40-piece desktop when using 2-row tray */
  .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .puzzle-pool {
    min-height: unset; /* cancel any min-height */
    overflow: visible; /* avoid clipping of inner content */
  }
  .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .pool-pieces {
    min-height: unset; /* cancel any min-height */
    height: auto; /* let rows determine height */
  }

  .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-completion {
    min-height: 700px; /* tall enough for all pieces */
    max-height: none;
    margin-bottom: 40px; /* prevent footer overlap */
    overflow: visible; /* allow pieces to overflow */
  }

  .jigsaw-puzzle-container[data-piece-count="40"] .completion-pieces {
    aspect-ratio: auto; /* let grid define the height */
    min-height: 650px;
    max-height: none;
    width: 100%;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(5, 1fr);
  }

  /* Removed grid overlay to prevent gaps between pieces */
}

.pool-pieces .puzzle-piece {
    aspect-ratio: 1;
    width: 100%;
    height: auto;
    /* In split mode, shrink pieces to keep them within the pool */
}

/* In split mode, never clip children in wrappers (global override) */
.puzzle-area.split-mode .puzzle-pool,
.puzzle-area.split-mode .puzzle-completion {
  overflow: visible;
}

.puzzle-area.split-mode .pool-pieces .puzzle-piece {
    transform: scale(var(--pool-piece-scale, 1.0));
    transform-origin: center center;
}

/* Ensure SVG sits comfortably inside pool tiles */
.puzzle-area.split-mode .pool-pieces .puzzle-piece svg {
    width: calc(100% - (var(--pool-inset, 5px) * 2));
    height: calc(100% - (var(--pool-inset, 5px) * 2));
    margin: var(--pool-inset, 5px);
    transform: none;
}

/* Mobile Styles */
@media (max-width: 767px) {
    .puzzle-all-controls {
        flex-direction: column;
        gap: 8px;
    }


    .puzzle-controls-main,
    .puzzle-controls {
        width: 100%;
        justify-content: center;
        gap: 8px;
    }

    .puzzle-controls-main button {
        flex: 1;
        max-width: 160px;
    }

    .jigsaw-puzzle-container button {
        height: 32px;
        padding: 0 12px;
        font-size: 13px;
    }

    .split-mode-wrapper {
        flex-direction: column;
    }

    .pool-pieces {
        grid-template-columns: repeat(8, 1fr);
        grid-template-rows: repeat(5, 1fr);
        min-height: 150px;
    }
}

/* Landscape Mode */
@media only screen and (max-width: 767px) and (orientation: landscape) {
    .puzzle-area {
        padding-bottom: 50%;
        max-height: 80vh;
    }

    .puzzle-preview img {
        max-width: 30%;
    }
}

/* Player Info */
.fastest-time {
    margin-top: 1em;
    color: teal;
    font-size: 150%;
    white-space: normal;
    word-wrap: break-word;
    max-width: 100%;
    padding: 0 10px;
}

.player-name {
    min-width: 200px;
    max-width: 300px;
    width: auto;
}

/* Timer */
.timer {
    font-size: 24px;
    padding: 5px 10px;
    margin: 5px;
    color: teal;
    background-color: rgba(255, 255, 255, 0.9);
    border-radius: 4px;
}

/* Alert */
.custom-alert-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.custom-alert-box {
    background-color: white;
    padding: 20px;
    border-radius: 5px;
    text-align: center;
    max-width: 80%;
}

.custom-alert-button {
    margin-top: 10px;
    padding: 5px 15px;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 3px;
    cursor: pointer;
}

/* Upload puzzle styles */
.jigsaw-puzzle-upload-container {
    text-align: center;
    margin: 20px 0;
}

.puzzle-upload-btn,
.puzzle-camera-btn,
#start-puzzle-btn {
    background-color: teal;
    color: white;
    padding: 10px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    margin: 5px;
}

.puzzle-upload-btn:hover,
.puzzle-camera-btn:hover,
#start-puzzle-btn:hover {
    background-color: #006666;
}

#image-preview {
    margin: 20px 0;
}

#preview-image {
    max-width: 800px;
    margin: 10px auto;
    display: block;
}

/* Piece selector styling */
.piece-selector {
    display: flex;
    gap: 15px;
    margin-bottom: 15px;
    justify-content: center;
}

.piece-option {
    position: relative;
    cursor: pointer;
    flex: 1;
    max-width: 120px;
}

.piece-option input[type="radio"] {
    position: absolute;
    opacity: 0;
}

.piece-label {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 12px;
    border: 2px solid #ddd;
    border-radius: 8px;
    background: white;
    transition: border-color 0.3s ease, background-color 0.3s ease;
}

.piece-option input:checked + .piece-label {
    border-color: #008080;
    background: #f0ffff;
    transform: scale(1.05);
}

.piece-number {
    font-size: 24px;
    font-weight: bold;
    color: #333;
}

.piece-text {
    font-size: 13px;
    color: #666;
    margin-top: 4px;
}

/* Adjust layout for 108-piece mode */
.jigsaw-puzzle-container[data-piece-count="108"] .pool-area,
.jigsaw-puzzle-container[data-piece-count="108"] .puzzle-pool {
    min-height: 250px;
    height: auto;
    overflow-y: hidden; /* no vertical scroll bar in pool container */
}

.jigsaw-puzzle-container[data-piece-count="108"] .pool-pieces {
    grid-template-columns: repeat(12, 1fr);
    min-height: 200px;
    aspect-ratio: 12/9; /* match 108-piece grid ratio */
}

.jigsaw-puzzle-container[data-piece-count="108"] .completion-area,
.jigsaw-puzzle-container[data-piece-count="108"] .puzzle-completion {
    min-height: 400px;
    overflow-y: hidden; /* avoid scrollbars; grid should always fit */
}

.jigsaw-puzzle-container[data-piece-count="108"] .completion-pieces {
    grid-template-columns: repeat(12, 1fr);
    grid-template-rows: repeat(9, 1fr);
    min-height: 350px;
    aspect-ratio: 12/9;
}

/* 108-piece vertical stack: pool above completion and larger sizes */
.jigsaw-puzzle-container[data-piece-count="108"] .split-mode-wrapper {
  flex-direction: column;
  gap: 24px;
}

.jigsaw-puzzle-container[data-piece-count="108"] .puzzle-area.split-mode .pool-pieces {
  --pool-piece-scale: 0.95; /* larger tiles in the pool */
}

@media (min-width: 768px) {
  .jigsaw-puzzle-container[data-piece-count="108"] .puzzle-pool {
    min-height: 420px;
  }
  .jigsaw-puzzle-container[data-piece-count="108"] .pool-pieces {
    min-height: 380px;
  }
  .jigsaw-puzzle-container[data-piece-count="108"] .puzzle-completion {
    min-height: 520px;
  }
  .jigsaw-puzzle-container[data-piece-count="108"] .completion-pieces {
    min-height: 460px;
  }
}

/* Desktop: 108-piece pool tray -> 2 rows with horizontal scroll */
@media (min-width: 768px) {
  .jigsaw-puzzle-container[data-piece-count="108"] .puzzle-area.split-mode .pool-pieces {
    /* Override default grid to create many implicit columns with fixed track size */
    display: grid;
    grid-template-rows: repeat(2, var(--pool-tile-size, 72px));
    grid-template-columns: none; /* cancel earlier 12-col rule */
    grid-auto-rows: var(--pool-tile-size, 72px);
    grid-auto-flow: column;
    grid-auto-columns: var(--pool-tile-size, 72px);
    gap: 8px;
    padding: 12px;
    width: 100%;
    max-width: 100%;
    overflow-x: auto; /* enable horizontal scroll */
    overflow-y: visible; /* don't clip second row */
    aspect-ratio: auto; /* cancel any previous aspect ratio constraints */
    -webkit-overflow-scrolling: touch;
    scrollbar-gutter: stable both-edges;
    align-content: start;
    /* Bump default tile size slightly to reduce rounding artifacts at DPRs */
    --pool-tile-size: 74px;
    padding-bottom: 64px; /* more slack to clear overlay scrollbar on Safari */
    align-items: start;
    justify-items: center;
  }

  /* Ensure tiles are square and not scaled further in the pool tray */
  .jigsaw-puzzle-container[data-piece-count="108"] .puzzle-area.split-mode .pool-pieces .puzzle-piece {
    width: var(--pool-tile-size, 72px);
    height: var(--pool-tile-size, 72px);
    transform: none; /* override pool scaling for clarity when scrolling */
    box-sizing: border-box; /* include border in the tile size to avoid clipping */
    overflow: visible; /* allow inner SVG bleed if any */
  }

  /* Inline instruction hint styling - on same line as title */
  .pool-title {
    margin: 8px 12px;
    text-align: center;
    font-size: 20px;
    font-weight: bold;
    color: #333;
  }
  
  .pool-instructions-inline {
    font-size: 16px;
    color: #555;
    font-weight: 400;
    margin-left: 12px;
  }

  /* Remove excessive whitespace: override large min-heights for 108-piece desktop when using 2-row tray */
  .jigsaw-puzzle-container[data-piece-count="108"] .puzzle-area.split-mode .puzzle-pool {
    min-height: unset; /* cancel 420px min-height */
    overflow: visible; /* avoid clipping of inner content */
  }
  .jigsaw-puzzle-container[data-piece-count="108"] .puzzle-area.split-mode .pool-pieces {
    min-height: unset; /* cancel 380px min-height */
    height: auto; /* let rows determine height */
  }

  /* Avoid clipping enlarged classic shapes in completion area wrapper */
  .puzzle-area.split-mode .puzzle-completion { overflow: visible; }

  /* Scroll navigation arrows for pool (40-piece and 108-piece) - white on teal like mobile */
  .pool-scroll-arrow {
    position: absolute !important;
    top: 50% !important;
    transform: translateY(-50%) !important;
    width: 60px !important;
    height: 60px !important;
    background: #008080 !important;
    background-color: #008080 !important;
    color: white !important;
    border: 2px solid white !important;
    border-radius: 50% !important;
    cursor: pointer;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    font-size: 32px !important;
    font-weight: bold !important;
    line-height: 1 !important;
    z-index: 10 !important;
    transition: background 0.2s ease !important;
    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.3) !important;
  }
  .pool-scroll-arrow:hover {
    background: #006666 !important;
    background-color: #006666 !important;
    transform: translateY(-50%) !important;
  }
  .pool-scroll-arrow:active {
    background: #005555 !important;
    background-color: #005555 !important;
    transform: translateY(-50%) scale(0.95) !important;
  }
  .pool-scroll-arrow.left {
    left: -65px !important;
  }
  .pool-scroll-arrow.right {
    right: -65px !important;
  }
  .pool-scroll-arrow:disabled {
    opacity: 0.3 !important;
    cursor: not-allowed;
  }
}

/* ========================================
   MOBILE OVERRIDES - MUST BE LAST!
   ======================================== */
@media (max-width: 767px) {
    /* Mobile: Keep 2-row horizontal scroll for pool (40 pieces) - SMALLER TILES + TOUCH SCROLL */
    .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .pool-pieces {
        display: grid;
        --pool-tile-size: 38px; /* Show ≥8 pieces across on narrow phones */
        grid-template-rows: repeat(2, var(--pool-tile-size, 40px)) !important;
        grid-auto-flow: column;
        grid-auto-columns: var(--pool-tile-size, 40px);
        overflow-x: auto !important; /* Allow touch scrolling */
        overflow-y: hidden !important;
        gap: 2px;
        padding: 4px !important; /* Minimal padding */
        max-height: 95px !important; /* Reduced for smaller tiles */
        min-height: unset !important;
        padding-bottom: 8px !important; /* Minimal space for scrollbar */
        --pool-inset: 1px; /* Even tighter for smaller tiles */
        /* Enable smooth touch scrolling */
        scroll-behavior: smooth;
        -webkit-overflow-scrolling: touch;
        /* Allow horizontal scroll with touch */
        touch-action: pan-x;
    }
    /* Reduce pool wrapper min-height on mobile - FULL WIDTH */
    .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-pool { 
        min-height: unset !important;
        padding: 0 !important;
        margin: 2px 0 !important;
        width: 100% !important; /* CRITICAL: Full width for pool */
    }
    
    /* Ensure pool pieces container is full width */
    .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .pool-pieces {
        width: 100% !important;
    }
    
    /* Force scrollbar visibility on mobile iOS */
    .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .pool-pieces::-webkit-scrollbar {
        -webkit-appearance: none;
        height: 8px;
    }
    
    .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .pool-pieces::-webkit-scrollbar-thumb {
        border-radius: 4px;
        background-color: rgba(0, 128, 128, 0.8);
        box-shadow: 0 0 1px rgba(255, 255, 255, .5);
    }
    
    /* Ensure pool pieces are properly sized on mobile - FORCE 40PX */
    .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .pool-pieces .puzzle-piece {
        width: 40px !important;
        height: 40px !important;
        min-width: 40px !important;
        min-height: 40px !important;
        max-width: 40px !important;
        max-height: 40px !important;
        flex-shrink: 0 !important;
    }
    
    /* Completion area on mobile: keep grid fixed height but allow SVG bleed */
    .puzzle-area.split-mode .completion-pieces {
        overflow: visible !important;
        grid-template-columns: repeat(8, 1fr) !important;
        grid-template-rows: repeat(5, 1fr) !important;
        min-height: 190px !important;
        max-height: 230px !important;
        height: 210px !important;
        padding: 2px !important;
        width: 100% !important;
        background-image: none !important;
        background: transparent !important;
    }
    
    /* Mobile: pieces fill grid cells; allow SVG to bleed via inline styles */
    .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .completion-pieces .puzzle-piece {
        width: 100%;
        height: 100%;
        max-width: 100%;
        max-height: 100%;
        min-width: 0;
        min-height: 0;
        position: relative;
        transform: none;
        transform-origin: center center;
    }
    
    /* Allow inline margins/size from JS to remove seams */
    .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .completion-pieces .puzzle-piece svg {
        width: 100%;
        height: 100%;
        max-width: none;
        max-height: none;
    }
    
    /* Ensure raster elements also respect inline sizing if present */
    .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .completion-pieces .puzzle-piece canvas,
    .jigsaw-puzzle-container[data-piece-count="40"] .puzzle-area.split-mode .completion-pieces .puzzle-piece img {
        width: 100%;
        height: 100%;
        max-width: none;
        max-height: none;
    }
    /* Compact completion wrapper - full width, minimal padding */
    .puzzle-area.split-mode .puzzle-completion {
        background: transparent !important;
        padding: 0 !important;
        margin: 2px 0 !important;
        min-height: unset !important;
    }
    
    /* Pool pieces can overflow for SVG rendering, but completion must not */
    .pool-pieces .puzzle-piece {
        overflow: visible !important;
    }
    
    .completion-pieces .puzzle-piece {
        overflow: visible !important; /* Allow SVG bleed across seams */
    }
    
    /* Fix button layout on mobile - ULTRA COMPACT */
    .puzzle-controls-wrapper {
        display: flex;
        flex-wrap: wrap; /* allow wrapping for better fit */
        justify-content: center;
        align-items: center;
        gap: 3px !important;
        padding: 3px 2px !important;
        margin: 2px 0 !important;
        overflow-x: visible;
    }
    
    /* Piece selector on mobile - more compact */
    .piece-selector {
        display: flex;
        gap: 8px;
        margin-bottom: 6px;
        width: 100%;
        justify-content: center;
        order: -1; /* Move to top */
    }
    
    .piece-option {
        flex: 0 0 auto;
        max-width: 70px;
    }
    
    .piece-label {
        padding: 6px 8px;
        font-size: 12px;
    }
    
    .piece-number {
        font-size: 18px;
    }
    
    .piece-text {
        font-size: 10px;
    }

    /* Smaller timer in compact toolbar - teal color, better visibility */
    .timer { 
        font-size: 16px; 
        padding: 4px 8px; 
        margin: 0;
        background: white;
        border: 2px solid #008080;
        border-radius: 4px;
        color: #008080;
        font-weight: bold;
        min-width: 50px;
        text-align: center;
    }
    .timer-wrapper::before { 
        content: ""; /* Remove stopwatch icon on mobile */
        font-size: 0;
        display: none !important;
    }
    .timer-wrapper {
        flex-shrink: 0;
    }

    /* Ultra-compact container padding on mobile */
    .jigsaw-puzzle-container {
        padding: 0 !important; /* allow full-bleed preview */
    }
    
    /* Make "Beat the record" text fit on 2 lines - COMPACT */
    .fastest-time {
        font-size: 16px !important;
        line-height: 1.3 !important;
        max-width: 100% !important;
        text-align: center !important;
        margin: 8px 0 !important;
        padding: 0 10px !important;
        word-wrap: break-word !important;
    }
    
    /* Minimize puzzle area margins */
    .puzzle-area.split-mode {
        margin: 0 !important;
    }

    /* Mobile scroll arrows for pool - teal, larger, more visible */
    .pool-scroll-arrow {
        position: absolute !important;
        top: 50% !important;
        transform: translateY(-50%) !important;
        width: 48px !important;
        height: 48px !important;
        background: #008080 !important;
        background-color: #008080 !important;
        color: white !important;
        border: 2px solid white !important;
        border-radius: 50% !important;
        cursor: pointer;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        font-size: 28px !important;
        font-weight: bold !important;
        line-height: 1 !important;
        z-index: 10 !important;
        box-shadow: 0 3px 8px rgba(0,0,0,0.3) !important;
    }
    .pool-scroll-arrow.left { left: 2px !important; }
    .pool-scroll-arrow.right { right: 2px !important; }
    .pool-scroll-arrow:active {
        transform: translateY(-50%) scale(0.95) !important;
    }
    .pool-scroll-arrow:hover {
        background: #006666 !important;
        background-color: #006666 !important;
    }
}
    
    /* Ensure icons display properly in buttons */
    .jigsaw-puzzle-container .icon-button {
        display: inline-flex;
        align-items: center;
        min-width: 32px;
        min-height: 32px;
    }
    
    .jigsaw-puzzle-container .icon-button img {
        /* Do not force display; allow JS to toggle via inline style */
        width: 16px !important;
        height: 16px !important;
        display: inline !important; /* Default to inline */
    }
    
    /* Ensure icon visibility is controlled by JS - use class for mobile compatibility */
    .jigsaw-puzzle-container .icon-button .hidden-icon {
        display: none !important;
        opacity: 0 !important;
        visibility: hidden !important;
    }
    
    /* Ensure music icons toggle properly */
    .jigsaw-puzzle-container .mute-music-button .music-on-icon {
        display: inline !important;
    }
    
    .jigsaw-puzzle-container .mute-music-button .music-off-icon {
        display: none !important;
    }
    
    /* When music is muted (added by JS) */
    .jigsaw-puzzle-container .mute-music-button.muted .music-on-icon {
        display: none !important;
    }
    
    .jigsaw-puzzle-container .mute-music-button.muted .music-off-icon {
        display: inline !important;
    }

    /* Ensure sound icons toggle properly (match music) */
    .jigsaw-puzzle-container .mute-sounds-button .sound-on-icon {
        display: inline !important;
    }
    .jigsaw-puzzle-container .mute-sounds-button .sound-off-icon {
        display: none !important;
    }
    .jigsaw-puzzle-container .mute-sounds-button.muted .sound-on-icon {
        display: none !important;
    }
    .jigsaw-puzzle-container .mute-sounds-button.muted .sound-off-icon {
        display: inline !important;
    }
