:root {
  --wall: #EEF3F5;
  --bench: #CFD9DF;
  --bench-dark: #B7C4CC;
  --ink: #1B2A33;
  --ink-soft: #5A6B75;
  --paper: #FFFFFF;
  --amber: #F2A33A;
  --mint: #3BB58A;
  --coral: #E4573D;
  --class: #4F7BE0;
  --radius: 14px;
  --shadow: 0 2px 0 rgba(27, 42, 51, 0.08), 0 8px 20px rgba(27, 42, 51, 0.08);
  --font: 'Rubik', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  font-weight: 500;
  color: var(--ink);
  background: var(--wall);
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
#app { position: relative; width: 100%; height: 100dvh; overflow: hidden; }

/* ---------- screens ---------- */
.screen {
  position: absolute; inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: calc(16px + var(--sat)) 16px calc(16px + var(--sab));
  opacity: 0;
  transition: opacity 240ms ease;
}
.screen.active { display: flex; opacity: 1; }
.screen.overlay { background: rgba(27, 42, 51, 0.45); z-index: 20; }
#screen-play { padding: var(--sat) 0 var(--sab); justify-content: stretch; }

.card {
  background: var(--paper);
  border-radius: 22px;
  padding: 28px 24px;
  width: min(100%, 420px);
  max-height: 100%;
  overflow: auto;
  box-shadow: var(--shadow);
  text-align: center;
  display: flex; flex-direction: column; gap: 14px; align-items: center;
}
.card.wide { width: min(100%, 520px); text-align: left; align-items: stretch; }
h1 { font-size: 40px; line-height: 1; margin: 0; letter-spacing: -0.02em; font-weight: 700; }
h2 { font-size: 26px; margin: 0; font-weight: 700; }
p { margin: 0; line-height: 1.45; }
.pitch { color: var(--ink-soft); max-width: 34ch; }
.hint { color: var(--ink-soft); font-size: 13px; max-width: 36ch; }
.best { color: var(--ink-soft); }
.row { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }

.btn {
  font: inherit; font-weight: 700; font-size: 17px;
  border: 0; border-radius: 999px; cursor: pointer;
  padding: 14px 26px; color: var(--ink); background: var(--bench);
  transition: transform 120ms ease, background 120ms ease;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(1px) scale(0.98); }
.btn:focus-visible { outline: 3px solid var(--amber); outline-offset: 2px; }
.btn.primary { background: var(--ink); color: #fff; min-width: 200px; }
.btn.ghost { background: transparent; color: var(--ink-soft); font-size: 15px; padding: 10px 16px; }
.btn.small { font-size: 14px; padding: 8px 16px; }

/* Title logo: one capsule with the stem lit */
.logo .pill {
  display: inline-block; padding: 10px 22px; border-radius: 999px;
  background: linear-gradient(90deg, #fff 0 50%, #E9EEF1 50% 100%);
  border: 2px solid var(--bench-dark); font-size: 22px; font-weight: 700;
  box-shadow: var(--shadow);
}
.logo i { font-style: normal; } .logo b { color: var(--amber); }

/* ---------- play layout ---------- */
.hud { width: 100%; max-width: 900px; margin: 0 auto; padding: 6px 12px 4px; }
.timer { height: 6px; background: var(--bench); border-radius: 3px; overflow: hidden; }
.timer-fill { height: 100%; width: 100%; background: var(--ink); border-radius: 3px; transform-origin: left; }
.hud-row { display: flex; justify-content: space-between; align-items: baseline; padding-top: 6px; }
.level-tag { color: var(--ink-soft); font-size: 14px; }
.score { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }
.score.bump { animation: bump 220ms ease-out; }

.bench {
  position: relative; flex: 1; width: 100%; min-height: 0;
  display: grid; grid-template-columns: minmax(96px, 1fr) minmax(150px, 2fr) minmax(96px, 1fr);
  gap: 6px; padding: 4px 6px; touch-action: none; user-select: none; -webkit-user-select: none;
}
@media (min-width: 700px) { .bench { grid-template-columns: 1fr 2.4fr 1fr; max-width: 900px; margin: 0 auto; } }

.wall { display: flex; flex-direction: column; gap: 6px; min-height: 0; }
#fx { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 6; }

/* Bins: a lidded dispensing bin. Lid strip carries the class colour and snaps shut on a swallow. */
.bin {
  --class: #4F7BE0;
  position: relative; flex: 1; min-height: 44px;
  background: var(--paper); border-radius: var(--radius);
  box-shadow: inset 0 0 0 2px rgba(27, 42, 51, 0.08), 0 2px 0 rgba(27, 42, 51, 0.06);
  display: flex; flex-direction: column; justify-content: center; align-items: center;
  padding: 14px 6px 8px; text-align: center; overflow: hidden;
  transition: box-shadow 160ms ease, transform 160ms ease;
}
.bin .lid {
  position: absolute; left: 0; right: 0; top: 0; height: 10px;
  background: var(--class); transform-origin: top; transform: rotateX(0deg);
}
.bin .lid::after { content: ''; position: absolute; left: 50%; top: 3px; width: 22px; height: 4px; margin-left: -11px; border-radius: 2px; background: rgba(255,255,255,0.55); }
.bin .icon { width: 22px; height: 22px; color: var(--class); margin-bottom: 2px; }
.bin .name { font-size: 12.5px; font-weight: 700; line-height: 1.15; }
.bin .stem { font-size: 12px; color: var(--amber); font-weight: 700; margin-top: 2px; transition: opacity 600ms ease; }
.bin .stem.faded { opacity: 0; }
.bin .key {
  position: absolute; bottom: 5px; font-size: 10px; font-weight: 700; color: var(--ink-soft);
  border: 1.5px solid var(--bench-dark); border-radius: 5px; padding: 0 5px; line-height: 15px; min-width: 16px;
}
.wall.left .bin .key { left: 6px; } .wall.right .bin .key { right: 6px; }
.bin.open { box-shadow: inset 0 0 0 3px var(--class), 0 2px 0 rgba(27, 42, 51, 0.06); transform: scale(1.03); }
.bin.hit { animation: binPop 360ms cubic-bezier(.34,1.56,.64,1); }
.bin.hit .lid { animation: lidSnap 360ms ease-out; }
.bin.hit::before { content: ''; position: absolute; inset: 0; background: var(--class); opacity: 0.22; animation: flash 360ms ease-out forwards; }
.bin.wrong { animation: shake 180ms linear; box-shadow: inset 0 0 0 3px var(--coral), 0 2px 0 rgba(27, 42, 51, 0.06); }
.bin.pulse { animation: pulse 900ms ease-in-out infinite; box-shadow: inset 0 0 0 3px var(--amber), 0 2px 0 rgba(27,42,51,0.06); }
.bin.pair-a { border-left: 4px solid var(--class); } .bin.pair-b { border-right: 4px solid var(--class); }
.bin .quick { position: absolute; top: 12px; right: 6px; background: var(--amber); color: #fff; font-size: 10px; font-weight: 700; border-radius: 999px; padding: 1px 6px; animation: floatUp 700ms ease-out forwards; }

/* Belt */
.belt-col { display: flex; gap: 4px; min-height: 0; }
.streak { position: relative; width: 14px; border-radius: 7px; background: var(--paper); box-shadow: inset 0 0 0 2px rgba(27,42,51,0.08); overflow: visible; flex: none; }
.streak-fill { position: absolute; left: 2px; right: 2px; bottom: 2px; height: 0%; border-radius: 6px; background: var(--amber); transition: height 200ms ease, background 200ms ease; }
.streak.tier2 .streak-fill { background: var(--mint); } .streak.tier3 .streak-fill { background: var(--class, #4F7BE0); } .streak.tier4 .streak-fill { background: var(--coral); }
.mult { position: absolute; top: -2px; left: 50%; transform: translate(-50%, -100%); font-size: 11px; font-weight: 700; color: var(--ink-soft); white-space: nowrap; }
.mult.pop { animation: bump 300ms cubic-bezier(.34,1.56,.64,1); color: var(--amber); }

.belt {
  position: relative; flex: 1; border-radius: 16px; overflow: hidden;
  background: var(--bench);
  box-shadow: inset 0 0 0 3px var(--bench-dark), inset 0 10px 24px rgba(27,42,51,0.08);
}
.belt.tier2 { box-shadow: inset 0 0 0 3px var(--mint), inset 0 10px 24px rgba(27,42,51,0.08); }
.belt.tier3 { box-shadow: inset 0 0 0 3px var(--amber), inset 0 10px 24px rgba(27,42,51,0.08); }
.belt.tier4 { box-shadow: inset 0 0 0 3px var(--coral), inset 0 10px 24px rgba(27,42,51,0.08); }
.rollers {
  position: absolute; inset: 0;
  background-image: repeating-linear-gradient(180deg, transparent 0 22px, rgba(27,42,51,0.07) 22px 26px);
  animation: roll 1.2s linear infinite;
}
.belt.stopped .rollers { animation-play-state: paused; }
.drain { position: absolute; left: 10%; right: 10%; bottom: 0; height: 10px; border-radius: 10px 10px 0 0; background: var(--ink); opacity: 0.25; }
.capsules { position: absolute; inset: 0; }

/* Capsule: neutral two-tone pill; class colour is only revealed after sorting. */
.capsule {
  position: absolute; left: 50%; top: 0;
  width: 118px; height: 36px; margin-left: -59px;
  border-radius: 999px;
  background: linear-gradient(90deg, #fff 0 50%, #E6ECEF 50% 100%);
  border: 2px solid var(--bench-dark);
  box-shadow: 0 3px 0 rgba(27,42,51,0.12), 0 6px 12px rgba(27,42,51,0.12);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700; letter-spacing: 0.01em;
  cursor: grab; will-change: transform; white-space: nowrap;
}
.capsule.long { font-size: 11.5px; width: 134px; margin-left: -67px; }
.capsule.dragging { cursor: grabbing; z-index: 5; box-shadow: 0 8px 0 rgba(27,42,51,0.08), 0 16px 24px rgba(27,42,51,0.18); }
.capsule .st { color: inherit; }
.capsule.lit .st { color: var(--amber); text-decoration: underline; text-decoration-thickness: 2px; text-underline-offset: 2px; }
.capsule.front { border-color: var(--ink-soft); }

.float { position: absolute; left: 50%; font-weight: 700; color: var(--mint); font-size: 16px; pointer-events: none; animation: floatUp 700ms ease-out forwards; z-index: 7; white-space: nowrap; }

/* Level 4 stickers */
.stickers { position: absolute; left: 6px; right: 6px; top: 4px; display: none; gap: 6px; padding: 6px 6px 12px; z-index: 8; background: rgba(238,243,245,0.96); border-radius: 14px; box-shadow: var(--shadow); max-width: 900px; margin: 0 auto; }
.stickers.show { display: flex; }
.stickers::after { content: ''; position: absolute; left: 8px; right: 8px; bottom: 0; height: 4px; border-radius: 2px; background: var(--amber); transform-origin: left; transform: scaleX(var(--left, 1)); }
.stickers { padding-bottom: 10px; }
.sticker {
  flex: 1; min-width: 0; background: var(--paper); border-radius: 10px; padding: 8px 6px 8px;
  font-size: 11.5px; line-height: 1.2; text-align: center; border: 2px dashed var(--bench-dark);
  position: relative; cursor: pointer; animation: slideDown 260ms cubic-bezier(.34,1.56,.64,1) both;
}
.sticker:nth-child(2) { animation-delay: 60ms; } .sticker:nth-child(3) { animation-delay: 120ms; }
.sticker .key { display: block; font-size: 10px; color: var(--ink-soft); margin-bottom: 3px; }
.sticker.right { border-style: solid; border-color: var(--mint); background: #E8F7F0; animation: stamp 260ms ease-out; }
.sticker.wrongpick { border-color: var(--coral); animation: peel 300ms ease-in forwards; }
.sticker.pulse { animation: pulse 900ms ease-in-out infinite; }

/* Guide (intro) banner */
.guide { width: min(100%, 520px); margin: 0 auto; padding: 8px 12px; display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.guide[hidden] { display: none; }
.guide p { font-size: 14px; } .guide .btn { white-space: nowrap; flex: none; } .guide b { color: var(--amber); }

.tray-row { display: flex; justify-content: space-between; align-items: center; padding: 6px 12px 8px; width: 100%; max-width: 900px; margin: 0 auto; }
.tray { display: flex; gap: 6px; align-items: baseline; color: var(--ink-soft); font-size: 13px; }
.tray-count { font-weight: 700; color: var(--ink); font-size: 16px; }
.tray.bump .tray-count { animation: bump 240ms ease-out; }

/* Debrief and end */
.stats { color: var(--ink-soft); }
.miss-list, .level-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; max-height: 40dvh; overflow: auto; }
.miss-list li { background: var(--wall); border-radius: 10px; padding: 8px 12px; font-size: 14px; line-height: 1.35; }
.miss-list li u { color: var(--amber); text-decoration-thickness: 2px; }
.miss-list li .arrow { color: var(--ink-soft); margin: 0 6px; }
.miss-list li small { display: block; color: var(--ink-soft); font-size: 12px; }
.level-list li { display: flex; justify-content: space-between; font-size: 14px; color: var(--ink-soft); }
.final { font-size: 48px; font-weight: 700; line-height: 1; text-align: center; }
.glossary summary { cursor: pointer; font-weight: 700; }
.glossary div { margin-top: 8px; max-height: 40dvh; overflow: auto; font-size: 13px; line-height: 1.4; }
.glossary .g { padding: 6px 0; border-top: 1px solid var(--wall); }
.glossary .g b { color: var(--amber); }
.sources { font-size: 14px; color: var(--ink-soft); display: flex; flex-direction: column; gap: 8px; }

/* ---------- animations ---------- */
@keyframes roll { to { background-position: 0 26px; } }
@keyframes bump { 0% { transform: scale(1); } 40% { transform: scale(1.25); } 100% { transform: scale(1); } }
@keyframes binPop { 0% { transform: scale(1); } 35% { transform: scale(1.08); } 100% { transform: scale(1); } }
@keyframes lidSnap { 0% { transform: rotateX(0); } 30% { transform: rotateX(-70deg); } 70% { transform: rotateX(8deg); } 100% { transform: rotateX(0); } }
@keyframes flash { to { opacity: 0; } }
@keyframes shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }
@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.04); } }
@keyframes floatUp { 0% { opacity: 1; transform: translate(-50%, 0); } 100% { opacity: 0; transform: translate(-50%, -40px); } }
@keyframes slideDown { from { transform: translateY(-60px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes stamp { 0% { transform: scale(1.2); } 100% { transform: scale(1); } }
@keyframes peel { to { transform: rotate(-8deg) translateY(30px); opacity: 0; } }
@keyframes breathe { 0%,100% { opacity: 0.85; } 50% { opacity: 1; } }
.bin .lid { animation: breathe 3.3s ease-in-out infinite; }

@media (prefers-reduced-motion: reduce) {
  .screen { transition: none; }
  .rollers, .bin .lid { animation: none !important; }
  .bin.wrong, .bin.hit, .mult.pop, .sticker, .float, .bin.pulse, .sticker.pulse { animation: none !important; }
  .bin.hit::before { display: none; }
}
