/* Colour by Number — shared shell for the gallery and the player.
   Everything here is mobile-first: the page must never scroll, never bounce,
   and never let the browser steal a pinch gesture from the canvas. */

:root {
    --cbn-bg: #12101f;
    --cbn-panel: #1b1930;
    --cbn-panel-2: #232042;
    --cbn-line: rgba(255, 255, 255, 0.10);
    --cbn-text: #f4f3fb;
    --cbn-muted: rgba(244, 243, 251, 0.55);
    --cbn-accent: #7c6cff;
    --cbn-accent-2: #f093fb;
    --cbn-ok: #37d399;
    --safe-t: env(safe-area-inset-top, 0px);
    --safe-b: env(safe-area-inset-bottom, 0px);
    --safe-l: env(safe-area-inset-left, 0px);
    --safe-r: env(safe-area-inset-right, 0px);
}

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

html,
body {
    height: 100%;
    overscroll-behavior: none;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--cbn-bg);
    color: var(--cbn-text);
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    user-select: none;
    -webkit-user-select: none;
}

button {
    font: inherit;
    color: inherit;
    background: none;
    border: none;
    cursor: pointer;
}

.cbn-icon-btn {
    width: 40px;
    height: 40px;
    flex: 0 0 auto;
    display: grid;
    place-items: center;
    border-radius: 12px;
    font-size: 1.15rem;
    background: rgba(255, 255, 255, 0.07);
    border: 1px solid var(--cbn-line);
    transition: background .2s ease, transform .2s ease, opacity .2s ease;
}

.cbn-icon-btn:hover { background: rgba(255, 255, 255, 0.14); }
.cbn-icon-btn:active { transform: scale(.92); }
.cbn-icon-btn:disabled { opacity: .35; cursor: default; }

/* ── Gallery ─────────────────────────────────────────────────────────── */

body.cbn-gallery {
    /* The gallery is the one place a scroll is allowed, but only inside the list. */
    display: flex;
    flex-direction: column;
    height: 100dvh;
    overflow: hidden;
    background:
        radial-gradient(1200px 600px at 15% -10%, #2b2456 0%, transparent 60%),
        radial-gradient(900px 500px at 100% 0%, #3a1f4d 0%, transparent 55%),
        var(--cbn-bg);
}

.gallery-top {
    flex: 0 0 auto;
    padding: calc(var(--safe-t) + 14px) calc(var(--safe-r) + 16px) 10px calc(var(--safe-l) + 16px);
    display: flex;
    align-items: center;
    gap: 12px;
}

.gallery-top h1 {
    font-size: 1.35rem;
    font-weight: 800;
    letter-spacing: -.02em;
    flex: 1 1 auto;
    min-width: 0;
}

.gallery-sub {
    padding: 0 calc(var(--safe-r) + 16px) 12px calc(var(--safe-l) + 16px);
    color: var(--cbn-muted);
    font-size: .87rem;
}

/* Horizontally scrolling category filter — thumb-friendly, no wrap. */
.gallery-filters {
    flex: 0 0 auto;
    display: flex;
    gap: 8px;
    overflow-x: auto;
    scrollbar-width: none;
    padding: 2px calc(var(--safe-r) + 16px) 14px calc(var(--safe-l) + 16px);
    scroll-snap-type: x proximity;
}

.gallery-filters::-webkit-scrollbar { display: none; }

.filter-chip {
    flex: 0 0 auto;
    scroll-snap-align: start;
    padding: 9px 16px;
    border-radius: 999px;
    font-size: .85rem;
    font-weight: 600;
    white-space: nowrap;
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid var(--cbn-line);
    color: var(--cbn-muted);
    transition: all .2s ease;
}

.filter-chip[aria-pressed="true"] {
    background: linear-gradient(135deg, var(--cbn-accent), var(--cbn-accent-2));
    border-color: transparent;
    color: #fff;
}

.gallery-scroll {
    flex: 1 1 auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: 0 calc(var(--safe-r) + 16px) calc(var(--safe-b) + 28px) calc(var(--safe-l) + 16px);
}

.gallery-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 14px;
}

.puzzle-card {
    position: relative;
    display: block;
    border-radius: 18px;
    overflow: hidden;
    background: var(--cbn-panel);
    border: 1px solid var(--cbn-line);
    text-decoration: none;
    color: inherit;
    transition: transform .2s ease, box-shadow .2s ease;
}

.puzzle-card:active { transform: scale(.97); }

@media (hover: hover) {
    .puzzle-card:hover {
        transform: translateY(-4px);
        box-shadow: 0 16px 34px rgba(0, 0, 0, .45);
    }
}

.puzzle-card canvas {
    display: block;
    width: 100%;
    height: auto;
    aspect-ratio: 1 / 1;
    object-fit: contain;
    background: #fff;
    image-rendering: pixelated;
}

.puzzle-meta {
    padding: 10px 12px 12px;
}

.puzzle-meta strong {
    display: block;
    font-size: .9rem;
    font-weight: 700;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.puzzle-meta span {
    font-size: .75rem;
    color: var(--cbn-muted);
}

.puzzle-progress {
    margin-top: 8px;
    height: 4px;
    border-radius: 2px;
    background: rgba(255, 255, 255, .1);
    overflow: hidden;
}

.puzzle-progress i {
    display: block;
    height: 100%;
    border-radius: 2px;
    background: linear-gradient(90deg, var(--cbn-accent), var(--cbn-accent-2));
}

.puzzle-badge {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    font-size: .8rem;
    color: #04231a;
    background: var(--cbn-ok);
    box-shadow: 0 3px 10px rgba(0, 0, 0, .35);
}

.gallery-empty {
    padding: 60px 20px;
    text-align: center;
    color: var(--cbn-muted);
}

/* ── Player ──────────────────────────────────────────────────────────── */

body.cbn-player {
    /* Fixed positioning is what actually kills iOS Safari rubber-banding. */
    position: fixed;
    inset: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    background: var(--cbn-bg);
}

.player-top {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: calc(var(--safe-t) + 8px) calc(var(--safe-r) + 12px) 8px calc(var(--safe-l) + 12px);
    background: rgba(18, 16, 31, .92);
    border-bottom: 1px solid var(--cbn-line);
    z-index: 3;
}

.player-title {
    flex: 1 1 auto;
    min-width: 0;
}

.player-title strong {
    display: block;
    font-size: .95rem;
    font-weight: 700;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.player-title .bar {
    margin-top: 5px;
    height: 4px;
    border-radius: 2px;
    background: rgba(255, 255, 255, .12);
    overflow: hidden;
}

.player-title .bar i {
    display: block;
    height: 100%;
    width: 0;
    border-radius: 2px;
    background: linear-gradient(90deg, var(--cbn-accent), var(--cbn-accent-2));
    transition: width .25s ease;
}

.player-percent {
    flex: 0 0 auto;
    font-size: .8rem;
    font-weight: 700;
    color: var(--cbn-muted);
    font-variant-numeric: tabular-nums;
}

.player-stage {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    background: #ffffff;
}

#board {
    display: block;
    width: 100%;
    height: 100%;
    /* The engine owns every gesture; the browser must not pan, zoom or select. */
    touch-action: none;
}

.stage-hint {
    position: absolute;
    left: 50%;
    bottom: 14px;
    transform: translateX(-50%);
    padding: 8px 16px;
    border-radius: 999px;
    font-size: .78rem;
    font-weight: 600;
    color: #fff;
    background: rgba(18, 16, 31, .82);
    backdrop-filter: blur(8px);
    pointer-events: none;
    opacity: 0;
    transition: opacity .25s ease;
}

.stage-hint.show { opacity: 1; }

.zoom-cluster {
    position: absolute;
    right: 12px;
    bottom: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.zoom-cluster .cbn-icon-btn {
    background: rgba(18, 16, 31, .78);
    color: #fff;
    backdrop-filter: blur(8px);
}

/* Palette bar — the primary control, so it gets big targets and lives
   above the home indicator. */
.palette-bar {
    flex: 0 0 auto;
    background: rgba(18, 16, 31, .96);
    border-top: 1px solid var(--cbn-line);
    padding: 10px 0 calc(var(--safe-b) + 10px);
    z-index: 3;
}

.palette-scroll {
    display: flex;
    gap: 10px;
    overflow-x: auto;
    scrollbar-width: none;
    padding: 4px calc(var(--safe-r) + 14px) 4px calc(var(--safe-l) + 14px);
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
}

.palette-scroll::-webkit-scrollbar { display: none; }

.swatch {
    position: relative;
    flex: 0 0 auto;
    width: 54px;
    height: 54px;
    border-radius: 16px;
    display: grid;
    place-items: center;
    font-size: 1.05rem;
    font-weight: 800;
    border: 2px solid transparent;
    transition: transform .18s ease, border-color .18s ease, opacity .25s ease;
}

.swatch .n {
    text-shadow: 0 1px 2px rgba(0, 0, 0, .35);
    pointer-events: none;
}

.swatch .left {
    position: absolute;
    bottom: -2px;
    right: -2px;
    min-width: 20px;
    height: 20px;
    padding: 0 5px;
    border-radius: 999px;
    background: var(--cbn-panel-2);
    color: var(--cbn-text);
    border: 1px solid var(--cbn-line);
    font-size: .65rem;
    font-weight: 700;
    display: grid;
    place-items: center;
    font-variant-numeric: tabular-nums;
    pointer-events: none;
}

.swatch[aria-pressed="true"] {
    transform: translateY(-8px) scale(1.12);
    border-color: #fff;
    box-shadow: 0 8px 20px rgba(0, 0, 0, .5);
}

.swatch.done {
    opacity: .3;
    transform: none;
}

.swatch.done .left { display: none; }

/* ── Completion overlay ──────────────────────────────────────────────── */

/* Once solved the palette is dead weight — give the picture the room. */
body.cbn-player.solved .palette-bar,
body.cbn-player.solved .zoom-cluster,
body.cbn-player.solved .stage-hint {
    display: none;
}

/* Sits at the bottom so the finished picture stays fully visible. */
.finish-overlay {
    position: absolute;
    left: 12px;
    right: 12px;
    bottom: 12px;
    z-index: 5;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    padding: 18px 20px;
    border-radius: 22px;
    text-align: center;
    background: rgba(18, 16, 31, .92);
    border: 1px solid var(--cbn-line);
    box-shadow: 0 18px 50px rgba(0, 0, 0, .5);
    transform: translateY(140%);
    opacity: 0;
    pointer-events: none;
    transition: opacity .35s ease, transform .35s cubic-bezier(.2, .9, .3, 1.2);
}

.finish-overlay.show {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

.finish-overlay h2 {
    font-size: 1.2rem;
    font-weight: 800;
}

.finish-overlay p {
    color: var(--cbn-muted);
    font-size: .85rem;
}

.finish-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
}

.finish-actions button,
.finish-actions a {
    padding: 12px 22px;
    border-radius: 999px;
    font-size: .9rem;
    font-weight: 700;
    text-decoration: none;
    color: #fff;
    background: rgba(255, 255, 255, .12);
    border: 1px solid var(--cbn-line);
}

.finish-actions .primary {
    background: linear-gradient(135deg, var(--cbn-accent), var(--cbn-accent-2));
    border-color: transparent;
}

.cbn-loading {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    color: var(--cbn-muted);
    font-size: .9rem;
    background: var(--cbn-bg);
    z-index: 4;
}

.cbn-loading.hidden { display: none; }

/* Landscape phones: shrink the chrome so the canvas keeps most of the height. */
@media (max-height: 480px) and (orientation: landscape) {
    .swatch { width: 44px; height: 44px; border-radius: 13px; font-size: .9rem; }
    .palette-bar { padding: 6px 0 calc(var(--safe-b) + 6px); }
    .player-top { padding-top: calc(var(--safe-t) + 4px); padding-bottom: 4px; }
}
