:root {
    color-scheme: light;
    --background: #f6f3ee;
    --surface: #ffffff;
    --text: #2b2622;
    --muted: #6f665e;
    --border: #d9d1c7;
    --accent: #c4501b;
    --accent-text: #ffffff;
    --success: #2e7d32;
    /* A cell of the grid; a piece image is 4/3 of a cell, which leaves a small gap between the pieces */
    --cell: min(calc((100vw - 48px) / 5.2), 76px);
    --border-band: calc(var(--cell) * 0.6);
    --piece: calc(var(--cell) * 4 / 3);
    /* A slot of the tray, 8 per row on small screens */
    --slot: min(calc((100vw - 40px) / 8), 76px);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0 16px 32px;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    background: var(--background);
    color: var(--text);
    -webkit-tap-highlight-color: transparent;
}

header {
    text-align: center;
}
h1 {
    margin: 16px 0 4px;
    font-size: 1.8rem;
}
.subtitle {
    margin: 0 0 12px;
    color: var(--muted);
}

/* Buttons and challenge bar */

button, select {
    font: inherit;
    min-height: 44px;
    padding: 8px 14px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    touch-action: manipulation;
}
button:hover {
    border-color: var(--muted);
}
button:disabled {
    opacity: 0.5;
    cursor: wait;
}
button.primary {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-text);
    font-weight: 600;
}
.challenge-bar, .controls {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 8px;
}
.challenge-bar label {
    font-weight: 600;
}

.status {
    min-height: 1.5em;
    margin: 12px 0;
    text-align: center;
    font-weight: 600;
}
.status.success {
    color: var(--success);
}

/* Board and tray */

.container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
}
.grid {
    display: grid;
    grid-template-columns: var(--border-band) repeat(4, var(--cell)) var(--border-band);
    grid-template-rows: var(--border-band) repeat(4, var(--cell)) var(--border-band);
    padding: 8px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    transition: box-shadow 0.3s;
}
.grid.solved {
    box-shadow: 0 0 0 4px var(--success);
}
.grid .cell, .grid .corner, .grid .horizontal-constraint, .grid .vertical-constraint {
    display: flex;
    align-items: center;
    justify-content: center;
}
.grid .cell {
    position: relative;
    outline: 1px dashed var(--border);
    outline-offset: -1px;
}
.grid .cell.drop-target {
    background: rgba(196, 80, 27, 0.15);
}
.grid .horizontal-constraint, .grid .vertical-constraint {
    cursor: pointer;
    touch-action: manipulation;
}
.grid .vertical-constraint img, .grid .horizontal-constraint img, .grid .corner img {
    width: 100%;
    height: 100%;
}
.grid .cell .piece, .preview .cell .mini-piece {
    position: absolute;
    left: calc((var(--cell) - var(--piece)) / 2);
    top: calc((var(--cell) - var(--piece)) / 2);
    width: var(--piece);
    height: var(--piece);
    z-index: 1;
}

.tray {
    display: grid;
    grid-template-columns: repeat(8, var(--slot));
    grid-auto-rows: var(--slot);
    padding: 6px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
}
.tray.drop-target {
    background: #fbeee6;
}
.tray .slot {
    display: flex;
    align-items: center;
    justify-content: center;
}
.tray .piece {
    width: 92%;
    height: 92%;
}

/* Compact layout on phones */
@media (max-width: 600px) {
    body {
        padding: 0 12px 24px;
        font-size: 15px;
    }
    h1 {
        margin: 10px 0 2px;
        font-size: 1.35rem;
    }
    .subtitle {
        margin-bottom: 8px;
        font-size: 0.85rem;
    }
    button, select {
        min-height: 38px;
        padding: 6px 10px;
        border-radius: 8px;
        font-size: 0.9rem;
    }
    .challenge-bar, .controls {
        gap: 6px;
    }
    .challenge-bar label {
        font-size: 0.9rem;
    }
    .status {
        margin: 8px 0;
        font-size: 0.85rem;
    }
    .container {
        gap: 10px;
    }
    .grid {
        padding: 4px;
    }
    .controls {
        margin-top: 10px;
    }
    .long-label {
        display: none;
    }
    .help {
        margin-top: 10px;
        font-size: 0.8rem;
    }
    .message {
        padding: 10px 16px;
        font-size: 0.9rem;
    }
}

@media (min-width: 820px) {
    :root {
        --slot: 84px;
    }
    .container {
        flex-direction: row;
        justify-content: center;
        align-items: flex-start;
    }
    .tray {
        grid-template-columns: repeat(4, var(--slot));
    }
}

/* Pieces */

.piece {
    cursor: grab;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-user-drag: none;
    -webkit-touch-callout: none;
    filter: url(#piece-normal);
}
.piece.locked {
    cursor: not-allowed;
}
/* The colors are SVG filters defined in index.html */
.piece.locked, .mini-piece.locked {
    filter: url(#piece-locked);
}
.piece.selected {
    outline: 3px solid var(--text);
    outline-offset: 2px;
    border-radius: 8px;
}
.piece.dragging {
    opacity: 0.25;
}
.piece.ghost {
    position: fixed;
    z-index: 100;
    pointer-events: none;
    transform: translate(-50%, -50%) scale(1.1);
}
.piece.red-filter {
    filter: url(#piece-error);
}
.piece.green-filter {
    filter: url(#piece-hint);
}
.mirror-horizontal {
    transform: scaleY(-1);
}
.mirror-vertical {
    transform: scaleX(-1);
}

/* Help and saved puzzles */

.controls {
    margin-top: 16px;
}
.help {
    max-width: 640px;
    margin: 16px auto 0;
    color: var(--muted);
    font-size: 0.9rem;
    text-align: center;
}
#saved-puzzles {
    max-width: 900px;
    margin: 16px auto 0;
    text-align: center;
}
.saved-list {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding-bottom: 8px;
}
.saved-puzzle {
    flex: 0 0 auto;
    padding: 6px;
    text-align: center;
    cursor: pointer;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 0.85rem;
}
.preview {
    --cell: 28px;
    --piece: calc(var(--cell) * 4 / 3);
    display: grid;
    grid-template-columns: repeat(4, var(--cell));
    grid-auto-rows: var(--cell);
    margin: 8px 8px 6px;
}
.preview .cell {
    position: relative;
}
.mini-piece {
    filter: url(#piece-normal);
}

footer {
    margin-top: 24px;
    text-align: center;
}
footer a {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    color: var(--muted);
    font-size: 0.9rem;
    text-decoration: none;
    border-radius: 8px;
}
footer a:hover {
    color: var(--text);
    background: var(--surface);
}

/* Message banner */

.message {
    position: fixed;
    left: 50%;
    bottom: calc(16px + env(safe-area-inset-bottom));
    z-index: 50;
    width: max-content;
    max-width: calc(100vw - 32px);
    padding: 14px 20px;
    border-radius: 12px;
    background: var(--text);
    color: #ffffff;
    font-weight: 600;
    text-align: center;
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
    opacity: 0;
    pointer-events: none;
    transform: translate(-50%, 20px);
    transition: opacity 0.2s, transform 0.2s;
}
.message.visible {
    opacity: 1;
    pointer-events: auto;
    transform: translate(-50%, 0);
}
.message.success {
    background: var(--success);
}
.message.warning {
    background: #b3261e;
}
.filters {
    position: absolute;
    width: 0;
    height: 0;
}
