:root {
    --wood: #B98257;
    --wood-dark: #9C6A43;
    --cream: #F4EFE3;
    --paper: #FFFDF8;
    --ink: #4B3A2F;
    --ink-soft: #8C7563;
    --red: #E5484D;
    --green: #5DBB63;
    --gold: #F2B544;
    --shadow: 0 6px 18px rgba(75, 58, 47, .14);
    --line: rgba(52, 36, 26, .85);
    --outline: 0 2px 0 var(--line), 0 -1.5px 0 var(--line), 1.5px 0 0 var(--line), -1.5px 0 0 var(--line),
        1.5px 1.5px 0 var(--line), -1.5px 1.5px 0 var(--line), 1.5px -1.5px 0 var(--line), -1.5px -1.5px 0 var(--line);
    color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { margin: 0; color: var(--ink); overflow-x: clip; }

/* a wooden picnic table, warm and soft enough for the evening */
body {
    min-height: 100dvh;
    background:
        repeating-linear-gradient(90deg, rgba(0,0,0,.08) 0 2px, transparent 2px 96px),
        repeating-linear-gradient(90deg, transparent 0 30px, rgba(255,255,255,.04) 30px 34px, transparent 34px 61px, rgba(0,0,0,.035) 61px 63px, transparent 63px 96px),
        linear-gradient(180deg, #C38C60, var(--wood) 40%, #B07A50);
    font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, sans-serif;
    -webkit-user-select: none;
    user-select: none;
}

button { font: inherit; color: inherit; cursor: pointer; border: 0; }

.wrap {
    margin: 0 auto; max-width: 520px;
    padding: max(8px, env(safe-area-inset-top)) 16px max(12px, env(safe-area-inset-bottom));
    display: flex; flex-direction: column; gap: 8px;
}

/* --- header ------------------------------------------------------------- */

.top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.logo { display: flex; align-items: center; gap: 8px; }
.logo svg { width: 44px; height: 44px; animation: march 2.4s ease-in-out infinite; }
.logo h1 { margin: 0; font-size: 26px; line-height: .95; font-weight: 900; letter-spacing: -.5px; color: #FFF8EC; text-shadow: 0 2px 0 rgba(75, 45, 25, .35); }
.logo h1 span { color: #FFD9A0; }
.level-label { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.1; }
.level-label strong { font-size: 18px; font-weight: 900; color: #FFF8EC; text-shadow: 0 2px 0 rgba(75, 45, 25, .35); }
.level-label span { margin-top: 2px; padding: 2px 8px; border-radius: 999px; background: var(--ink); color: var(--paper); font-size: 10px; font-weight: 900; text-transform: uppercase; letter-spacing: 1px; }

/* --- the plate ----------------------------------------------------------- */

.plate {
    /* a square cloth mat, as big as the screen's height leaves room for (--rest is
       everything else on the page, header to the bottom bar) */
    --rest: 404px;
    position: relative; align-self: center; width: min(100%, 440px, max(34dvh, calc(100dvh - var(--rest)))); aspect-ratio: 1;
    padding: 0; border-radius: 14px;
    background:
        repeating-linear-gradient(45deg, rgba(160, 200, 220, .18) 0 1.5px, transparent 1.5px 22px),
        repeating-linear-gradient(-45deg, rgba(160, 200, 220, .18) 0 1.5px, transparent 1.5px 22px),
        #FBF7EE;
    box-shadow: 0 12px 26px rgba(70, 40, 20, .35), inset 0 0 0 4px #F1E8D8;
    display: grid; place-items: center; margin-bottom: 18px;
}
/* Board and ant layer share one box, both sized against the mat itself (padding %
   would be measured against the page instead and put the ants off the grid). */
.board { width: 88%; aspect-ratio: 1; display: grid; gap: 1px; }   /* tracks set per picture in build() */
.ants { position: absolute; left: 6%; top: 6%; width: 88%; height: 88%; z-index: 5; pointer-events: none; }
.px {
    position: relative; min-width: 0; min-height: 0; overflow: hidden; border-radius: 22%; display: grid; place-items: center;
    /* a little cube: lit top, shaded bottom, in proportion at any cell size */
    background-image: linear-gradient(180deg, rgba(255,255,255,.38) 0 16%, rgba(255,255,255,0) 16% 80%, rgba(0,0,0,.2) 80%);
}
.px .psym { width: 72%; height: 72%; opacity: .85; pointer-events: none; }
.hole {
    position: absolute; left: 50%; bottom: -26px; transform: translateX(-50%); width: 56px; height: 26px; border-radius: 50%;
    background: radial-gradient(ellipse at 50% 40%, #2E1F16 0 45%, #5A3E2B 46% 62%, #8A6448 63% 100%);
    box-shadow: 0 2px 0 rgba(255,255,255,.18); z-index: 2;
}
.px.gone { opacity: 0; }
.board.quake { animation: quake .7s cubic-bezier(.36, .07, .19, .97); }
.board.won { animation: shine 1.2s ease-out; }
.board.wipe { animation: wipe .38s ease-in forwards; }
.board.serve { animation: serve .6s cubic-bezier(.3, 1.4, .5, 1); }

.yum {
    position: absolute; left: 50%; top: 50%; z-index: 6; padding: 8px 22px; border-radius: 999px;
    background: #fff; color: var(--red); font-size: 30px; font-weight: 900; white-space: nowrap;
    box-shadow: 0 8px 24px rgba(75, 58, 47, .25); animation: yum 2.2s cubic-bezier(.3, 1.5, .5, 1) both; pointer-events: none;
}
.dancer { position: absolute; z-index: 5; width: 28px; height: 28px; animation: dance 2.4s ease-in-out both; pointer-events: none; }
.dancer svg { width: 100%; height: 100%; }

.board-end {
    position: absolute; inset: 0; z-index: 20; border-radius: 14px; display: grid; place-items: center;
    background: rgba(255, 253, 248, .6); animation: end-in .5s ease-out both;
}
.board-end.lost { background: rgba(140, 117, 99, .25); }
.board-end .go {
    border-radius: 999px; padding: 14px 32px; font-size: 20px; font-weight: 900; color: #fff;
    background: var(--red); box-shadow: 0 5px 0 #B8303A, 0 12px 30px rgba(75, 58, 47, .3);
    animation: pop-in .55s .1s cubic-bezier(.3, 1.6, .5, 1) both;
}
.board-end .go:active { transform: translateY(3px); box-shadow: 0 2px 0 #B8303A; }

/* --- jars (seats) and the nest grid (lanes) --------------------------------------- */

.seats { display: flex; justify-content: center; gap: 8px; margin-top: 6px; }
.seat {
    position: relative; width: 50px; height: 52px; padding: 0; border-radius: 14px 14px 16px 16px;
    background: rgba(255, 248, 236, .18); border: 2px dashed rgba(255, 248, 236, .5);
}
.seat .group {
    position: absolute; inset: -2px; border-radius: 14px 14px 16px 16px; display: grid; place-items: center;
    background: linear-gradient(170deg, var(--c-light), var(--c) 60%);
    box-shadow: inset 0 -5px 0 var(--c-dark), 0 6px 10px rgba(60, 35, 20, .3);
}
.seat .group .sym { position: absolute; left: 6px; top: 8px; width: 13px; height: 13px; opacity: .85; }
.seat .group b { font-size: 21px; font-weight: 900; color: #fff; margin-top: 4px; text-shadow: var(--outline); }
.seat.pickable .group { box-shadow: 0 0 0 3px #6FA9D6, inset 0 -5px 0 var(--c-dark); animation: pulse 1s ease-in-out infinite; }

.lanes { display: grid; gap: 8px; padding: 0 6px; }
.lane { position: relative; height: 150px; }
/* Four rows per lane: the front one lit and tappable, the rest dimmed behind it. */
.token {
    position: absolute; left: 50%; top: 0; width: 56px; height: 42px; border-radius: 12px;
    background: linear-gradient(165deg, var(--c-light), var(--c) 60%);
    box-shadow: inset 0 -5px 0 var(--c-dark), 0 5px 10px rgba(60, 35, 20, .28);
    display: grid; place-items: center;
    transform: translate(-50%, calc(var(--d, 0) * 47px));
    opacity: calc(1 - var(--d, 0) * .18);
    z-index: calc(10 - var(--d, 0));
    transition: transform .42s cubic-bezier(.3, 1.35, .5, 1), opacity .42s;
}
.token b { font-size: 20px; font-weight: 900; color: #fff; margin-top: -3px; text-shadow: var(--outline); }
.token .sym { position: absolute; left: 6px; top: 5px; width: 12px; height: 12px; opacity: .85; }
.token.mystery { background: repeating-linear-gradient(45deg, #8C7BC8 0 8px, #8273BD 8px 16px); }
.token.mystery .q { color: #fff; font-size: 24px; }
.token.front { cursor: pointer; box-shadow: inset 0 -5px 0 var(--c-dark), 0 0 0 3px rgba(255, 248, 236, .85), 0 6px 12px rgba(60, 35, 20, .32); }
.token.front:active { filter: brightness(.95); }
.token.off { opacity: .45; cursor: default; }
.token.sent { pointer-events: none; }
.token.ghost { position: fixed; z-index: 15; opacity: 1; transition: none; }
.more { position: absolute; left: 50%; bottom: -2px; transform: translateX(-50%); font-size: 11px; font-weight: 900; color: rgba(255, 248, 236, .8); }
.rule-token { width: 40px; height: 36px; border-radius: 10px; background: var(--c); box-shadow: inset 0 -3px 0 var(--c-dark); display: grid; place-items: center; flex: none; }
.rule-token .sym { width: 18px; height: 18px; }
.toggle { display: flex; align-items: center; gap: 10px; width: 100%; margin-top: 14px; padding: 10px 12px; border-radius: 14px; background: var(--paper); box-shadow: var(--shadow); font-weight: 800; font-size: 14px; text-align: left; }
.toggle span { position: relative; width: 40px; height: 24px; flex: none; border-radius: 999px; background: #D9CBB8; transition: background .2s; }
.toggle span::after { content: ""; position: absolute; left: 3px; top: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform .2s; }
.toggle.on span { background: var(--green); }
.toggle.on span::after { transform: translateX(16px); }

.fly { position: fixed; inset: 0; z-index: 12; pointer-events: none; }
.fly .ghost { will-change: transform; }
/* Positioned by transform alone (see along()); the legs swing about the thorax. */
.walker { position: absolute; left: 0; top: 0; will-change: transform; }
.walker .rig { position: absolute; inset: 0; }
.walker img { position: absolute; inset: 0; width: 100%; height: 100%; }
/* The carried pixel: the same block as on the plate, lifted (it casts a shadow) and swaying a little. */
.walker .load { position: absolute; left: 50%; top: 50%; }
.walker .load .px { width: 100%; height: 100%; }
.walker.carrying .load .px { box-shadow: 0 2px 3px rgba(40, 20, 10, .35); }
.walker.carrying .load .px { animation: sway .3s ease-in-out infinite alternate; }
.walker .legs { transform-origin: 50% 57.5%; animation: gait .14s ease-in-out infinite alternate; }
.walker.waiting .legs { animation-play-state: paused; }     /* queued behind a pixel still to go */

.message { margin: 0; min-height: 20px; text-align: center; font-size: 15px; font-weight: 800; color: #FFF8EC; text-shadow: 0 1px 0 rgba(75, 45, 25, .35); text-wrap: balance; }

/* --- toolbar, power-ups, sheets (the house style) --------------------------- */

/* One row at the bottom: the toolbar, a divider, the power-ups. */
.bar { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 12px 10px; padding: 2px 0 6px; }
.toolbar { display: flex; justify-content: center; gap: 5px; padding-right: 10px; border-right: 2px solid rgba(255, 248, 236, .3); }
.tool {
    position: relative; width: 36px; height: 36px; border-radius: 50%;
    background: var(--paper); border: 1.5px solid #EADDD3; box-shadow: var(--shadow);
    display: grid; place-items: center; color: var(--ink); transition: transform .12s, background .2s, color .2s;
}
.tool svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
#levels-btn svg { fill: currentColor; stroke: none; }
.tool:active { transform: scale(.92); }
.tool::after {
    content: attr(data-label); position: absolute; bottom: calc(100% + 8px); left: 50%; transform: translate(-50%, 4px);
    padding: 5px 12px; border-radius: 999px; background: var(--ink); color: #fff; font-size: 13px; font-weight: 900;
    white-space: nowrap; opacity: 0; pointer-events: none; transition: opacity .15s, transform .15s; z-index: 5;
}
.tool:focus-visible { background: var(--ink); color: var(--paper); }
.tool:focus-visible::after { opacity: 1; transform: translate(-50%, 0); }
@media (hover: hover) {
    .tool:hover { background: var(--ink); color: var(--paper); border-color: var(--ink); }
    .tool:hover::after { opacity: 1; transform: translate(-50%, 0); }
}

.powerups { display: flex; justify-content: center; gap: 12px; }
.power {
    position: relative; width: 40px; height: 40px; padding: 0; border-radius: 50%; display: grid; place-items: center;
    background: radial-gradient(circle at 50% 35%, #fff, var(--paper) 60%, #F3E6D6);
    box-shadow: 0 0 0 3px var(--red), 0 3px 0 3px #B8303A, 0 6px 10px 3px rgba(60, 35, 20, .3);
    transition: transform .12s;
}
.power svg { width: 25px; height: 25px; }
.power span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.power:active { transform: translateY(3px) scale(.96); }
.power:disabled { opacity: .45; cursor: default; filter: grayscale(.6); }
.power b {
    position: absolute; right: -9px; bottom: -8px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 999px;
    background: var(--gold); color: #fff; font-size: 12px; font-weight: 900; display: grid; place-items: center;
    box-shadow: 0 2px 0 #C98A1E; text-shadow: 0 1px 0 rgba(120, 70, 10, .5);
}
.power.armed { background: #FFF3C9; box-shadow: 0 0 0 3px var(--gold), 0 3px 0 3px #C98A1E, 0 0 0 9px rgba(242, 181, 68, .3); animation: armed 1.2s ease-in-out infinite; }
.power.bump b { animation: bump .5s cubic-bezier(.3, 1.8, .5, 1); }

.sheet { position: fixed; inset: 0; z-index: 30; display: grid; align-items: end; justify-items: center; background: rgba(75, 58, 47, .35); animation: veil-in .3s ease-out both; }
.sheet[hidden] { display: none; }
.sheet.closing { animation: veil-out .28s ease-in both; }
.sheet-card {
    position: relative; width: min(520px, 100%); max-height: 82dvh; overflow: auto;
    padding: 22px 18px max(22px, env(safe-area-inset-bottom)); border-radius: 26px 26px 0 0;
    background: var(--cream); box-shadow: 0 -10px 40px rgba(75, 58, 47, .25);
    animation: slide-up .42s cubic-bezier(.2, .9, .3, 1.05) both;
}
.sheet.closing .sheet-card { animation: slide-down .28s cubic-bezier(.5, 0, .9, .5) both; }
.sheet-close { position: absolute; top: 12px; right: 14px; width: 36px; height: 36px; border-radius: 50%; background: var(--paper); font-size: 22px; font-weight: 900; box-shadow: var(--shadow); }
.sheet h2 { margin: 0 0 12px; font-size: 24px; font-weight: 900; }
.sheet h3 { margin: 18px 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: 1px; color: var(--ink-soft); }
.sheet p { margin: 0; font-size: 15px; font-weight: 700; line-height: 1.4; }
.list { display: grid; gap: 10px; }
.list div { display: flex; align-items: center; gap: 10px; }
.list svg { width: 40px; height: 36px; flex: none; }
.modes { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.mode { display: flex; flex-direction: column; align-items: flex-start; padding: 10px 12px; border-radius: 16px; background: var(--paper); box-shadow: var(--shadow); text-align: left; }
.mode:first-child { grid-column: 1 / -1; }
.mode b { font-size: 16px; font-weight: 900; }
.mode span { font-size: 12px; font-weight: 800; color: var(--ink-soft); }
.mode.on { background: var(--ink); color: var(--paper); }
.mode.on span { color: #F1D3D8; }
.mode:disabled { opacity: .5; cursor: default; }
.level-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; }
.level-grid button { aspect-ratio: 1; border-radius: 14px; background: var(--paper); box-shadow: var(--shadow); font-size: 16px; font-weight: 900; }
.level-grid button.solved { background: #E9F4E4; color: #3E7A36; }
.level-grid button.current { background: var(--ink); color: var(--paper); }
.level-grid button:disabled { opacity: .35; cursor: default; }

/* --- motion ----------------------------------------------------------------- */

@keyframes gait { from { transform: rotate(-12deg); } to { transform: rotate(12deg); } }
@keyframes march { 0%, 100% { transform: rotate(-6deg) translateY(0); } 50% { transform: rotate(6deg) translateY(-3px); } }
@keyframes sway { from { transform: rotate(-5deg); } to { transform: rotate(5deg); } }
@keyframes quake { 0%, 100% { transform: none; } 15% { transform: translate(-8px, 3px) rotate(-1.5deg); } 35% { transform: translate(8px, -3px) rotate(1.5deg); } 55% { transform: translate(-5px, 2px); } 75% { transform: translate(3px, -1px); } }
@keyframes shine { 0% { filter: none; } 40% { filter: saturate(1.3) brightness(1.05); transform: scale(1.04); } 100% { filter: none; transform: none; } }
@keyframes wipe { to { transform: scale(.6) rotate(-8deg); opacity: 0; } }
@keyframes serve { from { transform: scale(.6) rotate(8deg); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes yum { 0% { transform: translate(-50%, -50%) scale(0) rotate(-10deg); opacity: 0; } 20% { transform: translate(-50%, -50%) scale(1.15) rotate(3deg); opacity: 1; } 35%, 80% { transform: translate(-50%, -50%) scale(1); opacity: 1; } 100% { transform: translate(-50%, -80%) scale(.9); opacity: 0; } }
@keyframes dance { 0% { transform: scale(0); } 15% { transform: scale(1) rotate(-15deg); } 30% { transform: translateY(-10px) rotate(15deg); } 45% { transform: translateY(0) rotate(-15deg); } 60% { transform: translateY(-10px) rotate(15deg); } 85% { transform: rotate(0); opacity: 1; } 100% { transform: scale(0); opacity: 0; } }
@keyframes armed { 50% { transform: scale(1.06); } }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px rgba(242, 181, 68, .35), var(--shadow); } }
@keyframes bump { 50% { transform: scale(1.6); } }
@keyframes end-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop-in { from { opacity: 0; transform: scale(.4) translateY(20px); } to { opacity: 1; transform: none; } }
@keyframes slide-up { from { transform: translateY(100%); } to { transform: none; } }
@keyframes slide-down { from { transform: none; } to { transform: translateY(100%); } }
@keyframes veil-in { from { background: rgba(75, 58, 47, 0); } to { background: rgba(75, 58, 47, .35); } }
@keyframes veil-out { from { background: rgba(75, 58, 47, .35); } to { background: rgba(75, 58, 47, 0); } }

/* Short phones (iPhone 13 and alike): everything down to the power-ups fits one screen. */
@media (max-height: 720px) {
    .wrap { gap: 6px; }
    .logo svg { width: 36px; height: 36px; }
    .logo h1 { font-size: 21px; }
    .level-label strong { font-size: 16px; }
    .lane { height: 128px; }
    .token { height: 38px; transform: translate(-50%, calc(var(--d, 0) * 41px)); }
}

/* Very short screens (iPhone SE in Safari): smaller header and nests, so the plate keeps its size. */
@media (max-height: 620px) {
    .plate { --rest: 352px; margin-bottom: 14px; }
    .wrap { gap: 4px; }
    .logo svg { width: 28px; height: 28px; }
    .logo h1 { font-size: 17px; }
    .level-label strong { font-size: 14px; }
    .seat { height: 44px; }
    .lane { height: 104px; }
    .token { height: 32px; transform: translate(-50%, calc(var(--d, 0) * 34px)); }
    .token b { font-size: 17px; }
    .message { font-size: 13px; min-height: 17px; }
}

/* Narrow phones: the bottom bar still fits on one line. */
@media (max-width: 380px) {
    .bar { gap: 8px; }
    .toolbar { gap: 4px; padding-right: 8px; }
    .tool { width: 34px; height: 34px; }
    .powerups { gap: 7px; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
