/* ==========================================================================
   Boredom Arcade - shared styles (game pages + tokens used by the hub)
   Class prefix: ba-   Game code should prefix its own classes with the game id.

   Desktop first: every game page is one 16:9 stage, as large as the window
   allows, with a slim HUD bar above it or a HUD rail on its left (whichever
   gives the bigger stage). setupGame() computes the frame and publishes it as
   CSS variables on <html> (see shared/KIT.md "Layout contract").
   ========================================================================== */

:root {
  /* palette */
  --bg: #120d24;
  --bg-2: #1b1436;
  --panel: #251c49;
  --panel-2: #31265e;
  --panel-3: #3d3172;
  --line: rgba(255, 255, 255, 0.09);
  --text: #ffffff;
  --text-dim: #b8aee0;
  --text-mute: #8a80b5;
  --ink: #140b2b;          /* outline / dark text on bright fills */

  --yellow: #ffd23f;
  --yellow-deep: #d99a00;
  --pink: #ff3d8b;
  --pink-deep: #c01a62;
  --red: #ff4757;
  --red-deep: #b3202d;
  --green: #2ed573;
  --green-deep: #15924a;
  --blue: #3fa9f5;
  --blue-deep: #1b6fb3;
  --purple: #8e6cff;
  --purple-deep: #5a3fd1;
  --orange: #ff9f43;
  --coral: #ff7a59;
  --coral-deep: #c24a2c;

  --accent: var(--yellow);  /* setupGame() sets this to the game's accent */

  /* type */
  --font-display: ui-rounded, "SF Pro Rounded", "Nunito", "Varela Round", "Quicksand",
    "Arial Rounded MT Bold", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-body: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  /* shape */
  --radius-sm: 12px;
  --radius: 18px;
  --radius-lg: 26px;
  --hud-h: 52px;            /* HUD bar height (same as --hud-bar) */
  --chunk: 0 5px 0 rgba(0, 0, 0, 0.35);
  --ease-back: cubic-bezier(0.2, 1.6, 0.4, 1);

  /* frame, published by setupGame() on every resize (these are the defaults before it runs) */
  --stage-w: 100vw;         /* #stage width in CSS px (16:9 with --stage-h) */
  --stage-h: 56.25vw;
  --lu: calc(100vw / 1600); /* one logical unit (1/1600 of the stage width) in CSS px */
  --ui: 1;                  /* kit UI scale (cards, toasts): ~1 at 1920x1080, 0.82..1.35 */
  --hud-bar: 52px;
  --rail-w: 0px;
  --frame-r: 14px;

  /* safe areas */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);

  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; }

/* --------------------------------------------------------------------------
   Game page frame (every page that is not the hub)
   -------------------------------------------------------------------------- */
html:not(.ba-hub) {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}
html:not(.ba-hub) body {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  /* the letterbox backdrop: calm, tinted with the game's accent, never busy */
  background:
    radial-gradient(55% 65% at 50% 48%, color-mix(in srgb, var(--accent) 13%, transparent), transparent 72%),
    radial-gradient(1100px 700px at 0% 0%, rgba(142, 108, 255, 0.13), transparent 70%),
    radial-gradient(1100px 700px at 100% 100%, rgba(255, 61, 139, 0.09), transparent 70%),
    radial-gradient(rgba(255, 255, 255, 0.045) 1px, transparent 1.5px) 0 0 / 24px 24px,
    var(--bg);
}

/* The frame: HUD + stage, placed by setupGame() (pixel-exact, so the canvas stays crisp). */
.ba-app {
  position: absolute;
  left: var(--app-x, 0px);
  top: var(--app-y, 0px);
  width: var(--app-w, 100%);
  height: var(--app-h, 100%);
  display: grid;
  grid-template-columns: var(--stage-w);
  grid-template-rows: var(--hud-bar) var(--stage-h);
  border-radius: var(--frame-r);
  overflow: hidden;
  background: var(--bg-2);
  isolation: isolate;
  box-shadow:
    0 0 0 1px rgba(255, 255, 255, 0.09),
    0 24px 70px rgba(0, 0, 0, 0.55),
    0 0 140px -30px color-mix(in srgb, var(--accent) 55%, transparent);
}
.ba-app[data-hud="rail"] {
  grid-template-columns: var(--rail-w) var(--stage-w);
  grid-template-rows: var(--stage-h);
}

/* hidden probe that lets the kit read env(safe-area-inset-*) */
.ba-safe-probe {
  position: fixed;
  left: 0;
  top: 0;
  width: 0;
  height: 0;
  visibility: hidden;
  pointer-events: none;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
}

/* Portrait (phones): the 16:9 stage is letterboxed, with a small hint underneath. */
.ba-rotate-hint {
  position: fixed;
  left: 50%;
  bottom: calc(10px + var(--safe-bottom));
  z-index: 5;
  display: none;
  align-items: center;
  gap: 12px;
  width: max-content;
  max-width: calc(100% - 24px);
  padding: 9px 16px 9px 12px;
  border-radius: 16px;
  color: var(--text-dim);
  background: rgba(255, 255, 255, 0.06);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
  transform: translateX(-50%);
  pointer-events: none;
}
.ba-rotate-hint svg { flex: none; width: 30px; height: 30px; color: var(--accent); }
.ba-rotate-hint b { display: block; font: 800 14px/1.2 var(--font-display); color: #fff; }
.ba-rotate-hint small { display: block; margin-top: 2px; font: 600 12px/1.25 var(--font-body); }
html.ba-portrait .ba-rotate-hint { display: flex; }
html.ba-compact .ba-app:has(.ba-overlay.is-open) ~ .ba-rotate-hint { display: none; }

/* --------------------------------------------------------------------------
   HUD: a slim bar above the stage ...
   -------------------------------------------------------------------------- */
.ba-hud {
  position: relative;
  z-index: 40;
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 12px;
  padding: 0 8px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.075), rgba(255, 255, 255, 0.015)),
    var(--bg-2);
  box-shadow: inset 0 -1px 0 var(--line), 0 6px 18px rgba(0, 0, 0, 0.25);
}
.ba-hud-left { display: flex; align-items: center; gap: 12px; min-width: 0; justify-self: start; }
.ba-hud-center { display: flex; align-items: center; justify-content: center; min-width: 0; }
.ba-hud-side { display: flex; align-items: center; gap: 6px; justify-self: end; }
.ba-hud-title {
  min-width: 0;
  font: 900 14px/1.1 var(--font-display);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ba-hud-row { display: flex; align-items: center; justify-content: center; gap: 8px; max-width: 100%; min-width: 0; }
.ba-hud-keys { display: none; }
.ba-back-label { display: none; }

/* ... or a rail on the stage's left, when the window is wider than 16:9 (a browser with its toolbars). */
.ba-app[data-hud="rail"] .ba-hud {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 14px;
  padding: 14px 12px 12px;
  overflow: hidden;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.015), rgba(255, 255, 255, 0.06)),
    var(--bg-2);
  box-shadow: inset -1px 0 0 var(--line), 6px 0 18px rgba(0, 0, 0, 0.25);
}
.ba-app[data-hud="rail"] .ba-hud-left { flex-direction: column; align-items: flex-start; gap: 12px; }
.ba-app[data-hud="rail"] .ba-back { width: auto; padding: 0 14px 0 9px; gap: 6px; }
.ba-app[data-hud="rail"] .ba-back-label { display: inline; font: 900 14px/1 var(--font-display); letter-spacing: 0.02em; }
.ba-app[data-hud="rail"] .ba-back svg { width: 18px; height: 18px; }
.ba-app[data-hud="rail"] .ba-hud-title {
  font-size: 17px;
  letter-spacing: 0.06em;
  line-height: 1.15;
  color: #fff;
  white-space: normal;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
.ba-app[data-hud="rail"] .ba-hud-center { justify-content: flex-start; }
.ba-app[data-hud="rail"] .ba-hud-row { flex-direction: column; align-items: flex-start; justify-content: flex-start; gap: 8px; }
.ba-app[data-hud="rail"] .ba-info { max-width: 100%; white-space: normal; line-height: 1.2; }
.ba-app[data-hud="rail"] .ba-hud-side { flex-wrap: wrap; justify-self: auto; gap: 8px; }
.ba-app[data-hud="rail"] .ba-hud-keys {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: auto;
  min-height: 0;
  overflow: hidden;
  font: 700 12px/1.2 var(--font-display);
  color: var(--text-mute);
}
.ba-hud-keys-title {
  margin-bottom: 2px;
  font: 900 11px/1 var(--font-display);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-mute);
}
.ba-key-row { display: flex; align-items: center; gap: 8px; min-width: 0; }
.ba-key-row span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-dim); }
.ba-key-row.is-off { opacity: 0.45; }
.ba-key-row kbd, .ba-kbd {
  flex: none;
  min-width: 24px;
  padding: 3px 6px 4px;
  border-radius: 6px;
  font: 800 11px/1 var(--font-display);
  text-align: center;
  color: var(--text);
  background: rgba(255, 255, 255, 0.1);
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.3), inset 0 0 0 1px rgba(255, 255, 255, 0.1);
}
/* arrow keys in the legend: drawn as SVG (many fonts lack the arrow glyphs) */
.ba-kbd-arrow { display: inline-block; width: 1.15em; height: 1.15em; margin: -0.2em 0.02em; vertical-align: -0.12em; }
@media (hover: none) {
  .ba-app[data-hud="rail"] .ba-hud-keys { display: none; }
}

/* level pill + info chip */
.ba-level {
  appearance: none;
  border: 0;
  flex: 0 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 7px 14px 8px;
  border-radius: 999px;
  font: 900 15px/1 var(--font-display);
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--ink);
  background: linear-gradient(180deg, #ffe27a, var(--yellow));
  box-shadow: 0 3px 0 var(--yellow-deep), inset 0 1px 0 rgba(255, 255, 255, 0.6);
  white-space: nowrap;
  pointer-events: none;
}
.ba-level[hidden] { display: none; }
.ba-level.is-clickable { pointer-events: auto; cursor: pointer; transition: transform 0.08s, filter 0.15s; }
.ba-level.is-clickable::after { content: " \25BE"; font-size: 12px; }
.ba-level.is-clickable:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--yellow-deep); }
.ba-info {
  min-width: 0;
  padding: 7px 12px 8px;
  border-radius: 999px;
  font: 800 15px/1 var(--font-display);
  background: rgba(255, 255, 255, 0.1);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-variant-numeric: tabular-nums;
  flex: 0 20 auto; /* on narrow windows the info chip gives way before the level pill */
}
.ba-info:empty, .ba-info[hidden] { display: none; }

/* square chunky icon buttons (HUD) */
.ba-icon-btn {
  appearance: none;
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 0;
  border-radius: 12px;
  color: var(--text);
  background: linear-gradient(180deg, var(--panel-3), var(--panel-2));
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.14);
  font: 800 19px/1 var(--font-display);
  text-decoration: none;
  cursor: pointer;
  transition: transform 0.07s, box-shadow 0.07s, opacity 0.15s, filter 0.15s;
}
.ba-icon-btn svg { width: 21px; height: 21px; display: block; flex: none; }
.ba-icon-btn:active { transform: translateY(3px); box-shadow: 0 0 0 rgba(0, 0, 0, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.14); }
.ba-icon-btn[disabled] { opacity: 0.4; pointer-events: none; cursor: default; }
.ba-icon-btn[hidden] { display: none; }
.ba-icon-btn.is-off { color: var(--text-mute); }
.ba-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border-radius: 10px;
  font: 900 12px/20px var(--font-display);
  text-align: center;
  color: #fff;
  background: var(--red);
  box-shadow: 0 2px 0 var(--red-deep);
  pointer-events: none;
}
.ba-badge:empty { display: none; }

/* hud button .nudge(): a short attention wiggle (e.g. "try the hint") */
.ba-icon-btn.is-nudging { animation: ba-nudge 0.95s ease-in-out 2; }
@keyframes ba-nudge {
  0%, 100% { transform: none; }
  12% { transform: rotate(-14deg) scale(1.1); }
  24% { transform: rotate(12deg) scale(1.12); }
  36% { transform: rotate(-9deg) scale(1.08); }
  48% { transform: rotate(6deg) scale(1.04); }
  60% { transform: none; }
}

@media (hover: hover) {
  .ba-icon-btn:hover { filter: brightness(1.18); transform: translateY(-1px); }
  .ba-icon-btn:active { transform: translateY(3px); }
  .ba-level.is-clickable:hover { filter: brightness(1.06); transform: translateY(-1px); }
}

/* Narrow windows (phones in portrait): back | level + info | buttons, no title, no full screen button. */
@media (max-width: 700px) {
  .ba-hud { grid-template-columns: auto minmax(0, 1fr) auto; gap: 8px; }
  .ba-hud-title { display: none; }
  .ba-hud-center { justify-content: flex-start; }
  .ba-app:not([data-hud="rail"]) .ba-fullscreen { display: none; }
  .ba-app[data-hud="rail"] .ba-hud-title { display: -webkit-box; font-size: 14px; }
}
@media (max-width: 400px) {
  .ba-hud { gap: 6px; padding: 0 6px; }
  .ba-hud-side { gap: 5px; }
  .ba-icon-btn { width: 36px; height: 36px; border-radius: 11px; font-size: 17px; }
  .ba-icon-btn svg { width: 19px; height: 19px; }
  .ba-level { padding: 6px 10px 7px; font-size: 14px; }
  .ba-info { padding: 6px 8px 7px; font-size: 13px; }
  /* the slim buttons keep a taller invisible hit area; sideways only half the gap, so neighbours never take a tap */
  .ba-icon-btn::before { content: ""; position: absolute; inset: -4px -2px; }
}
@media (max-width: 340px) {
  .ba-hud { gap: 4px; }
  .ba-hud-side { gap: 4px; }
  .ba-icon-btn { width: 33px; height: 33px; border-radius: 10px; }
  .ba-icon-btn::before { inset: -6px -2px; }
  .ba-icon-btn svg { width: 18px; height: 18px; }
  .ba-level { padding: 5px 8px 6px; font-size: 13px; }
}

:focus-visible { outline: 3px solid var(--yellow); outline-offset: 2px; }
:focus:not(:focus-visible) { outline: none; }

/* --------------------------------------------------------------------------
   Stage + layers
   -------------------------------------------------------------------------- */
.ba-stage-wrap {
  position: relative;
  width: var(--stage-w);
  height: var(--stage-h);
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: #0d0920;
}
.ba-stage {
  position: absolute;
  inset: 0;
  overflow: hidden;
  touch-action: none;
  container-type: size;     /* game CSS inside #stage may use cqw / cqh units */
  container-name: ba-stage;
}
.ba-canvas { position: absolute; left: 0; top: 0; display: block; touch-action: none; }
.ba-fx {
  position: absolute;
  inset: 0;
  z-index: 20;
  overflow: hidden;
  pointer-events: none;
  container-type: size;
}

/* --------------------------------------------------------------------------
   Big outlined caption text (arcade style)
   <span class="ba-caption ba-caption--hype">LEVEL 5</span>
   Modifiers: --info (white) | --hype (yellow) | --win | --lose
   -------------------------------------------------------------------------- */
.ba-caption {
  --fill: #fff;
  --o: var(--ink);
  --ow: 3px;
  font-family: var(--font-display);
  font-weight: 900;
  letter-spacing: 0.01em;
  line-height: 1.02;
  text-transform: uppercase;
  color: var(--fill);
  text-shadow:
    var(--ow) 0 0 var(--o), calc(var(--ow) * -1) 0 0 var(--o),
    0 var(--ow) 0 var(--o), 0 calc(var(--ow) * -1) 0 var(--o),
    calc(var(--ow) * 0.72) calc(var(--ow) * 0.72) 0 var(--o),
    calc(var(--ow) * -0.72) calc(var(--ow) * -0.72) 0 var(--o),
    calc(var(--ow) * 0.72) calc(var(--ow) * -0.72) 0 var(--o),
    calc(var(--ow) * -0.72) calc(var(--ow) * 0.72) 0 var(--o),
    0 calc(var(--ow) * 2.2) 0 rgba(0, 0, 0, 0.35);
}
.ba-caption--hype { --fill: var(--yellow); --o: var(--ink); }
.ba-caption--info { --fill: #fff; --o: var(--ink); }
.ba-caption--win { --fill: var(--yellow); --o: #7a3d00; }
.ba-caption--lose { --fill: #ffb38a; --o: #5a1e10; }

/* --------------------------------------------------------------------------
   Banner (banner()) - big bouncy caption over the stage, non-blocking.
   Sized from the stage height (cqh), so it looks the same at 1280x720 and 2560x1440.
   -------------------------------------------------------------------------- */
.ba-banner {
  position: absolute;
  left: 50%;
  top: 8%;
  width: max-content;
  max-width: 90%;
  text-align: center;
  transform: translate(-50%, 0);
  animation: ba-banner-in 0.42s var(--ease-back) both;
}
.ba-banner--center { top: 50%; transform: translate(-50%, -50%); animation-name: ba-banner-in-c; }
.ba-banner--bottom { top: auto; bottom: 9%; }
.ba-banner-text {
  display: block;
  --ow: clamp(2.5px, 0.55cqh, 6px);
  font-size: clamp(26px, 7.4cqh, 110px);
  white-space: pre-line;
  transform: rotate(-4deg);
  animation: ba-wobble 1.1s ease-in-out 0.42s infinite;
}
.ba-banner--md .ba-banner-text { --ow: clamp(2px, 0.42cqh, 5px); font-size: clamp(21px, 5.4cqh, 80px); }
.ba-banner--sm .ba-banner-text { --ow: clamp(2px, 0.3cqh, 3.5px); font-size: clamp(16px, 3.6cqh, 54px); }
.ba-banner-sub {
  display: block;
  margin-top: 0.25em;
  --ow: clamp(1.5px, 0.26cqh, 3px);
  --fill: #fff;
  font-size: 0.46em;
  letter-spacing: 0.02em;
}
.ba-banner--hype .ba-banner-text { --fill: var(--yellow); --o: var(--ink); }
.ba-banner--info .ba-banner-text { --fill: #fff; --o: var(--ink); }
.ba-banner.is-leaving { animation: ba-banner-out 0.25s ease-in both; }
.ba-banner--center.is-leaving { animation-name: ba-banner-out-c; }

@keyframes ba-banner-in { from { transform: translate(-50%, 0) scale(0.2); opacity: 0; } to { transform: translate(-50%, 0) scale(1); opacity: 1; } }
@keyframes ba-banner-in-c { from { transform: translate(-50%, -50%) scale(0.2); opacity: 0; } to { transform: translate(-50%, -50%) scale(1); opacity: 1; } }
@keyframes ba-banner-out { to { transform: translate(-50%, -30%) scale(0.6); opacity: 0; } }
@keyframes ba-banner-out-c { to { transform: translate(-50%, -50%) scale(0.6); opacity: 0; } }
@keyframes ba-wobble {
  0%, 100% { transform: rotate(-4deg) scale(1); }
  25% { transform: rotate(3deg) scale(1.06); }
  50% { transform: rotate(-2deg) scale(0.98); }
  75% { transform: rotate(4deg) scale(1.04); }
}

/* --------------------------------------------------------------------------
   Toast (toast())
   -------------------------------------------------------------------------- */
.ba-toast {
  position: absolute;
  left: 50%;
  bottom: max(16px, 4cqh);
  width: max-content;
  max-width: min(88%, 44em);
  padding: 0.7em 1.2em 0.75em;
  border-radius: 999px;
  font: 800 calc(16px * var(--ui)) / 1.25 var(--font-display);
  text-align: center;
  color: #fff;
  background: rgba(12, 7, 28, 0.9);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.12), 0 8px 24px rgba(0, 0, 0, 0.4);
  transform: translateX(-50%);
  animation: ba-toast-in 0.28s var(--ease-back) both;
}
.ba-toast.is-leaving { animation: ba-toast-out 0.2s ease-in both; }
/* toast(text, ms, { at: 'top' }) / { y }: the kit's other placements (translate composes with the animation) */
.ba-toast--top { top: max(16px, 4cqh); bottom: auto; }
.ba-toast--y { bottom: auto; translate: 0 -50%; }
@keyframes ba-toast-in { from { transform: translate(-50%, 16px) scale(0.9); opacity: 0; } }
@keyframes ba-toast-out { to { transform: translate(-50%, 10px); opacity: 0; } }

/* --------------------------------------------------------------------------
   Overlay / modal (showWin, showLose, showMessage, showLevelPicker)
   Everything in the card is sized in em from --ui, so it scales with the stage.
   -------------------------------------------------------------------------- */
.ba-overlay {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: none;
  align-items: center;
  justify-content: center;
  padding: calc(16px * var(--ui)) 18px;
  overflow: hidden;
  background: rgba(8, 4, 22, 0.66);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  touch-action: none;
  font-size: calc(17px * var(--ui));
}
.ba-overlay.is-open { display: flex; animation: ba-fade 0.2s ease-out both; }
.ba-overlay-host { position: fixed; inset: 0; z-index: 90; pointer-events: none; }
.ba-overlay-host > .ba-overlay.is-open { pointer-events: auto; }
@keyframes ba-fade { from { opacity: 0; } }
/* Small stages (phones): cards use the whole window instead of the tiny letterboxed stage. */
html.ba-compact .ba-overlay { position: fixed; z-index: 45; padding: calc(10px + var(--safe-top)) 12px calc(10px + var(--safe-bottom)); }

/* sun rays behind the win card */
.ba-overlay[data-kind="win"]::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 46em;
  height: 46em;
  margin: -23em 0 0 -23em;
  border-radius: 50%;
  background: repeating-conic-gradient(from 0deg, rgba(255, 210, 63, 0.16) 0deg 9deg, transparent 9deg 18deg);
  -webkit-mask: radial-gradient(closest-side, #000 25%, transparent 100%);
  mask: radial-gradient(closest-side, #000 25%, transparent 100%);
  /* the rays turn, slow down and settle after 12 s: a still card costs the browser nothing per frame */
  animation: ba-rays 12s cubic-bezier(0.3, 0.5, 0.45, 1) both;
  pointer-events: none;
}
@keyframes ba-rays { to { transform: rotate(200deg); } }
@keyframes ba-spin { to { transform: rotate(360deg); } }

.ba-card {
  position: relative;
  width: min(100%, 25em);
  max-height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 1.5em 1.3em 1.25em;
  border-radius: 1.6em;
  text-align: center;
  background: linear-gradient(180deg, var(--panel-3), var(--panel) 70%);
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.14),
    0 0 0 3px rgba(0, 0, 0, 0.25),
    0 10px 0 rgba(0, 0, 0, 0.35),
    0 30px 60px rgba(0, 0, 0, 0.5);
  animation: ba-pop 0.45s var(--ease-back) both;
}
.ba-card--win { background: linear-gradient(180deg, #5a3fd1, #33237a 75%); }
.ba-card--lose { background: linear-gradient(180deg, #4d3a86, #2a2056 75%); }
.ba-card--levels { width: min(100%, max(22em, calc(var(--cols, 8) * 4.15em + 3.2em))); }
@keyframes ba-pop { from { transform: scale(0.6) translateY(20px); opacity: 0; } }

.ba-card-title {
  flex: none;
  margin: 0;
  --ow: 0.085em;
  font-size: 2.45em;
}
.ba-card-body {
  flex: 0 1 auto;
  min-height: 0;
  overflow: auto;
  margin: 0.7em 0.25em 1.2em;
  font: 700 1em/1.45 var(--font-display);
  color: var(--text-dim);
  white-space: pre-line;
  overflow-wrap: anywhere;
}
.ba-card-actions { display: flex; flex-direction: column; gap: 0.7em; flex: none; }
.ba-card-actions .ba-btn { width: 100%; font-size: 1.12em; }
.ba-card-actions .ba-btn--ghost { font-size: 1em; }

.ba-stars { flex: none; display: flex; justify-content: center; align-items: flex-end; gap: 0.25em; margin: 0.8em 0 0.1em; }
.ba-star { width: 3.5em; height: 3.5em; filter: drop-shadow(0 4px 0 rgba(0, 0, 0, 0.3)); }
.ba-star:nth-child(2) { width: 4.4em; height: 4.4em; margin-bottom: 0.6em; }
.ba-star .ba-star-fill { fill: #3a2f66; }
.ba-star .ba-star-edge { fill: none; stroke: #1b1238; stroke-width: 3; stroke-linejoin: round; }
.ba-star.is-on .ba-star-fill { fill: #ffd23f; }
.ba-star .ba-star-shine { fill: rgba(255, 255, 255, 0); }
.ba-star.is-on .ba-star-shine { fill: rgba(255, 255, 255, 0.55); }
.ba-star.is-on .ba-star-edge { stroke: #a55a00; }
.ba-star.is-on { animation: ba-star-pop 0.5s var(--ease-back) both; }
.ba-star.is-on:nth-child(1) { animation-delay: 0.25s; }
.ba-star.is-on:nth-child(2) { animation-delay: 0.45s; }
.ba-star.is-on:nth-child(3) { animation-delay: 0.65s; }
@keyframes ba-star-pop { from { transform: scale(0) rotate(-40deg); opacity: 0; } }

/* level picker */
.ba-levels {
  position: relative;
  flex: 0 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(var(--cols, 8), minmax(0, 3.6em));
  justify-content: center;
  gap: 0.55em;
  margin: 1em 0 1.1em;
  padding: 0.3em 0.3em 0.5em;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  scrollbar-width: thin;
  scrollbar-color: var(--panel-3) transparent;
}
.ba-lvl {
  appearance: none;
  aspect-ratio: 1;
  border: 0;
  border-radius: 0.8em;
  font: 900 1.1em/1 var(--font-display);
  color: #fff;
  background: linear-gradient(180deg, #7b61ff, var(--purple-deep));
  box-shadow: 0 4px 0 #3b2596, inset 0 1px 0 rgba(255, 255, 255, 0.3);
  cursor: pointer;
  transition: transform 0.07s, filter 0.15s;
}
.ba-lvl:active { transform: translateY(3px); box-shadow: 0 1px 0 #3b2596; }
.ba-lvl.is-current { color: var(--ink); background: linear-gradient(180deg, #ffe27a, var(--yellow)); box-shadow: 0 4px 0 var(--yellow-deep); }
.ba-lvl.has-stars { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0.2em; padding: 0; }
.ba-lvl-stars { font: 900 0.58em/1 var(--font-body); letter-spacing: -0.5px; color: rgba(255, 255, 255, 0.28); }
.ba-lvl-stars b { font-weight: inherit; color: var(--yellow); }
.ba-lvl.is-current .ba-lvl-stars { color: rgba(20, 11, 43, 0.25); }
.ba-lvl.is-current .ba-lvl-stars b { color: #b86b00; }
.ba-lvl[disabled] { color: rgba(255, 255, 255, 0.35); background: #2a2150; box-shadow: 0 4px 0 #1a1436; cursor: default; font-size: 0.9em; }
.ba-lvl:focus-visible { outline-offset: 3px; }

/* --------------------------------------------------------------------------
   Buttons  <button class="ba-btn ba-btn--primary">
   Variants: --primary (green) --yellow --danger --blue --ghost
   Sized in em: set font-size on the button (or a parent card) to scale it.
   -------------------------------------------------------------------------- */
.ba-btn {
  --btn-bg: linear-gradient(180deg, #8f7bff, var(--purple-deep));
  --btn-edge: #3b2596;
  position: relative;
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45em;
  min-height: 2.75em;
  padding: 0.7em 1.25em 0.75em;
  border: 0;
  border-radius: 0.9em;
  font: 900 19px/1 var(--font-display);
  letter-spacing: 0.03em;
  text-transform: uppercase;
  text-decoration: none;
  color: #fff;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.25);
  background: var(--btn-bg);
  box-shadow: 0 0.27em 0 var(--btn-edge), inset 0 2px 0 rgba(255, 255, 255, 0.3), 0 0.5em 0.9em rgba(0, 0, 0, 0.25);
  cursor: pointer;
  transition: transform 0.07s, box-shadow 0.07s, filter 0.15s;
}
.ba-btn:active { transform: translateY(0.22em); box-shadow: 0 0.05em 0 var(--btn-edge), inset 0 2px 0 rgba(255, 255, 255, 0.3); }
.ba-btn[disabled] { filter: grayscale(0.8) brightness(0.7); pointer-events: none; cursor: default; }
.ba-btn--primary { --btn-bg: linear-gradient(180deg, #62f29a, #1fbf5f); --btn-edge: var(--green-deep); }
.ba-btn--yellow { --btn-bg: linear-gradient(180deg, #ffe27a, var(--yellow)); --btn-edge: var(--yellow-deep); color: var(--ink); text-shadow: none; }
.ba-btn--danger { --btn-bg: linear-gradient(180deg, #ff7282, var(--red)); --btn-edge: var(--red-deep); }
.ba-btn--blue { --btn-bg: linear-gradient(180deg, #74c3ff, var(--blue)); --btn-edge: var(--blue-deep); }
.ba-btn--ghost {
  --btn-bg: rgba(255, 255, 255, 0.08);
  --btn-edge: rgba(0, 0, 0, 0.35);
  min-height: 2.6em;
  color: var(--text-dim);
  text-shadow: none;
  box-shadow: 0 0.22em 0 var(--btn-edge), inset 0 0 0 2px rgba(255, 255, 255, 0.1);
}

/* Desktop: hover lift, and a key cap on the button Enter presses (and on "Close" = Esc). */
@media (hover: hover) {
  .ba-btn:hover { filter: brightness(1.08); transform: translateY(-1px); }
  .ba-btn:active { transform: translateY(0.22em); }
  .ba-btn--ghost:hover { color: #fff; }
  .ba-lvl:not([disabled]):hover { filter: brightness(1.12); transform: translateY(-2px); }
  .ba-lvl:not([disabled]):active { transform: translateY(3px); }
}
@media (hover: hover) and (pointer: fine) {
  .ba-card-actions .ba-btn[data-key]::after {
    content: attr(data-key);
    position: absolute;
    right: 0.8em;
    top: 50%;
    padding: 0.28em 0.45em 0.32em;
    border-radius: 0.4em;
    font: 800 0.55em/1 var(--font-display);
    letter-spacing: 0.04em;
    text-transform: none;
    text-shadow: none;
    color: currentColor;
    background: rgba(0, 0, 0, 0.16);
    box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.18);
    opacity: 0.8;
    transform: translateY(-50%);
    pointer-events: none;
  }
}

/* --------------------------------------------------------------------------
   Confetti canvas (confetti())
   -------------------------------------------------------------------------- */
.ba-confetti {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 100;
  pointer-events: none;
}

/* --------------------------------------------------------------------------
   Motion preferences: decorative animation off
   -------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  .ba-banner, .ba-banner-text, .ba-banner.is-leaving,
  .ba-toast, .ba-toast.is-leaving,
  .ba-card, .ba-star.is-on, .ba-overlay.is-open,
  .ba-overlay[data-kind="win"]::before, .ba-icon-btn.is-nudging {
    animation: none !important;
  }
  .ba-banner-text { transform: rotate(-3deg); }
  .ba-btn, .ba-icon-btn, .ba-lvl, .ba-level { transition: none; }
}
