@import "./safe-area.css";
/* Barumba in-game layer — FLAT. Solid fills, soft single shadows, no bevels/gloss/ink outlines. */
:root {
  --paper: #F6F3EC;
  --surface: #FFFFFF;
  --line: #E2DDD2;
  /* Ink = a dark shade of the game colour (runtime sets it per game from `bg`); never near-black. */
  --ink: #61220C;
  --ink-2: #5A5A66;
  --ink-3: #8B8A94;
  --signal: #FF5A1F;
  --signal-ink: #B23A0A;
  --signal-soft: #FFE3D6;
  --down: #C8343A;
  --game-bg: var(--paper);
  --game-accent: var(--signal);
  --e1: 0 1px 2px #EDE7DF;
  --e2: 0 1px 2px color-mix(in srgb, var(--ink) 5%, transparent), 0 12px 28px -16px color-mix(in srgb, var(--ink) 28%, transparent);
  --f-display: "Bricolage Grotesque", ui-rounded, "Segoe UI", system-ui, sans-serif;
  --f-ui: "Atkinson Hyperlegible Next", "Atkinson Hyperlegible", system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: var(--barumba-viewport-height); overflow: hidden;
  background: var(--game-bg); color: var(--ink); font-family: var(--f-ui);
  -webkit-tap-highlight-color: transparent; overscroll-behavior: none;
}
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* Template: full-bleed stage, canvas behind, HUD + overlay on top. */
/* absolute (html/body are 100% + overflow hidden, so it still fills the viewport), not fixed:
   Chromium stops scrollIntoView / focus scrolling at a fixed subtree, so in an embed that
   sits partly below the host page's fold, a ready-card button reached by Tab or a scrolling
   modal could never be brought into view in the host page. */
.bg-game {
  position: absolute; inset: 0 0 var(--barumba-bottom-room);
  /* No long-press callout / text selection / browser gestures on the playfield (iOS Safari). */
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: none;
}
/* The portal has already reserved the inset and toolbar outside the iframe. */
html[data-safe-shell] .bg-game { bottom: 0; }
/* Text fields inside a game root stay selectable and receive normal touch gestures. */
.bg-game input, .bg-game textarea, .bg-game [contenteditable="true"] {
  user-select: text; -webkit-user-select: text; -webkit-touch-callout: default; touch-action: auto;
}
.bg-canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; user-select: none; -webkit-user-select: none; outline: none; }
.bg-canvas:focus-visible { box-shadow: inset 0 0 0 3px var(--signal); }

/* Organism: HUD */
.bg-hud {
  position: absolute; left: 0; right: 0; top: 0; z-index: 2;
  display: flex; justify-content: space-between; align-items: flex-start; gap: 8px;
  /* The right end stays clear for the shared pause + sound control (.bg-ctl, pinned top-right). */
  padding: max(10px, env(safe-area-inset-top)) calc(max(10px, env(safe-area-inset-right)) + var(--bg-ctl-w, 0px)) 0 max(10px, env(safe-area-inset-left)); pointer-events: none;
}
.bg-hud__group { display: flex; gap: 6px; align-items: center; pointer-events: auto; }

/* Molecule: stat pill */
.bg-pill {
  display: grid; gap: 1px; justify-items: start; min-width: 58px;
  padding: 6px 12px 7px; border-radius: 14px;
  background: #FFFFFF; box-shadow: var(--e1);
}
.bg-pill--warn .bg-value { color: var(--down); }
.bg-pop { animation: bg-pop 180ms cubic-bezier(0.2, 0.8, 0.2, 1); }
@keyframes bg-pop { 40% { transform: scale(1.06); } }

/* Atoms */
.bg-eyebrow { font: 700 10px/1 var(--f-ui); letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-2); }
.bg-value { font: 800 17px/1.1 var(--f-display); letter-spacing: -0.01em; font-variant-numeric: tabular-nums; }
.bg-value--big { font-size: 19px; }
.bg-pips { display: flex; gap: 4px; height: 20px; align-items: center; }
.bg-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--signal); }
.bg-dot--off { background: transparent; box-shadow: inset 0 0 0 1.5px var(--line); }
.bg-kbd {
  display: inline-block; margin-left: 8px; padding: 2px 6px; border-radius: 6px;
  background: #EDE7DF; font: 700 11px/1.3 var(--f-ui); color: var(--ink-2);
}
/* Key hints (kbd chips, controls lists, "Press …"): keyboard players only. kbd-hints.js sets
   <html class="kbd-hints"> for a fine/hover pointer or after a real keydown; never on touch. */
html:not(.kbd-hints) [data-kbd-hint],
html:not(.kbd-hints) .kbd,
html:not(.kbd-hints) .bg-kbd { display: none !important; }
.bg-btn {
  appearance: none; border: 0; cursor: pointer; min-height: 48px; padding: 0 22px; border-radius: 14px;
  font: 800 17px/1 var(--f-display); letter-spacing: -0.01em; color: var(--ink);
  transition: transform 120ms cubic-bezier(0.2, 0.8, 0.2, 1), box-shadow 120ms;
}
.bg-btn:active { transform: translateY(1px); }
.bg-btn:focus-visible, .bg-ibtn:focus-visible { outline: 2px solid var(--signal-ink); outline-offset: 3px; }
.bg-btn--primary { background: var(--signal); box-shadow: 0 6px 16px -8px rgba(178, 58, 10, 0.55); }
.bg-btn--primary:active { box-shadow: 0 2px 6px -2px rgba(178, 58, 10, 0.5); }
/* Border: opaque 2px, ~30% darker than the white fill (a shade of the game ink, never black). */
.bg-btn--secondary { background: var(--surface); box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--ink) 30%, #FFFFFF); }
.bg-btn--ghost { background: transparent; color: var(--ink-2); min-height: 40px; }
.bg-btn--primary .bg-kbd { background: #DEB49D; color: var(--ink); }
.bg-ibtn {
  appearance: none; border: 0; cursor: pointer; width: 44px; height: 44px; border-radius: 14px;
  display: grid; place-items: center; color: var(--ink);
  background: #FFFFFF; box-shadow: var(--e1);
}
.bg-ibtn svg { width: 18px; height: 18px; }

/* Organism: shared pause + sound control (controls.js). Pinned top-right, clear of the safe
   areas, above the overlay scrim so sound stays reachable on the ready/pause/results screens.
   Games choose a tone only (data-tone="light" | "dark"); no per-game CSS for this chrome.
   Flat: solid fill, an opaque 2px border ~30% darker than that fill (never black), no bevel. */
.bg-game { --bg-ctl-w: 100px; }
[data-chrome="off"] .bg-game { --bg-ctl-w: 50px; }
.bg-ctl {
  position: absolute; z-index: 4; display: flex; gap: 6px; align-items: center;
  top: max(10px, env(safe-area-inset-top)); right: max(10px, env(safe-area-inset-right));
  --ctl-fill: #FFFFFF; --ctl-edge: color-mix(in srgb, var(--ink) 30%, #FFFFFF); --ctl-fg: var(--ink);
}
.bg-ctl[data-tone="dark"] {
  --ctl-fill: color-mix(in srgb, var(--ink) 84%, var(--game-bg)); --ctl-edge: var(--ink); --ctl-fg: var(--paper);
}
.bg-ctl__btn {
  appearance: none; margin: 0; padding: 0; cursor: pointer; width: 44px; height: 44px; border-radius: 14px;
  display: grid; place-items: center; color: var(--ctl-fg);
  background: var(--ctl-fill); border: 2px solid var(--ctl-edge); box-shadow: var(--e1);
  touch-action: manipulation; transition: transform 120ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
.bg-ctl__btn:active { transform: translateY(1px); }
.bg-ctl__btn svg { width: 20px; height: 20px; }
.bg-ctl__pause svg { width: 18px; height: 18px; }
.bg-ctl__btn:focus-visible { outline: 2px solid var(--signal-ink); outline-offset: 3px; }
.bg-ctl[data-tone="dark"] .bg-ctl__btn:focus-visible { outline-color: var(--paper); }
/* No run to pause (ready / results): the slot stays so the row never shifts. */
.bg-ctl__pause[data-idle="true"] { visibility: hidden; }
/* Embedded in the portal (chrome=off): the portal owns Pause; sound only. */
[data-chrome="off"] .bg-ctl__pause { display: none; }
/* Narrow phones (< 360px): the two buttons stack (pause in the corner, sound under it), so the
   stats row keeps the same one-button-wide corner it always had. */
@media (max-width: 359.98px) {
  .bg-game { --bg-ctl-w: 50px; }
  .bg-ctl { flex-direction: column-reverse; }
}

/* Organism: overlay / modal */
.bg-overlay {
  position: absolute; inset: 0; z-index: 3; display: grid; place-items: center; padding: 20px;
  background: color-mix(in srgb, var(--ink) 28%, transparent);
}
.bg-overlay[hidden] { display: none; }
.bg-modal {
  width: min(320px, 100%); padding: 22px 20px 18px; border-radius: 20px;
  background: var(--paper); box-shadow: var(--e2); text-align: center;
  display: grid; gap: 10px; animation: bg-in 200ms cubic-bezier(0.2, 0.8, 0.2, 1);
  max-height: 100%; overflow-y: auto; overscroll-behavior: contain;
}
@keyframes bg-in { from { transform: translateY(8px); opacity: 0; } }
.bg-modal__eyebrow { margin: 0; font: 700 11px/1 var(--f-ui); letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-2); }
.bg-modal__title { margin: 0; font: 800 28px/1.05 var(--f-display); letter-spacing: -0.02em; }
.bg-modal__body { margin: 0; color: var(--ink-2); font-size: 15px; line-height: 1.4; }
.bg-modal__stat { display: grid; gap: 4px; padding: 10px; border-radius: 14px; background: var(--surface); }
.bg-modal__stat strong { font: 800 40px/1 var(--f-display); letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.bg-modal__actions { display: grid; gap: 6px; margin-top: 4px; }

/* D1 return hook (return-hook.js). Flat + opaque: fills are the game ink mixed into white
   (never alpha), borders 2px and ~40% darker than their light fill, in the game's own hue.
   Results row (.res-cta): the row itself has no handler; each button is a >= 48px target. */
.res-cta .bg-btn { min-height: 48px; }
.return-frame, .return-frame body {
  height: auto; background: var(--paper); touch-action: pan-y; overscroll-behavior-y: auto;
}
/* This results panel belongs to the host's scrolling sheet, so vertical swipes must
   pass through the frame instead of keeping the playfield's gesture lock. */
.bg-return-frame { position: static; display: grid; align-content: start; gap: 10px; padding: 8px 2px; text-align: center; touch-action: pan-y; }
.bg-streak, .bg-beat {
  justify-self: center; margin: 0; padding: 6px 14px; border-radius: 999px;
  background: color-mix(in srgb, var(--ink) 8%, #FFFFFF);
  border: 2px solid color-mix(in srgb, var(--ink) 40%, #FFFFFF);
  color: var(--ink); font: 800 14px/1.2 var(--f-display); letter-spacing: -0.01em;
  font-variant-numeric: tabular-nums;
}
.bg-beat { padding: 8px 18px; font-size: 22px; }
.bg-practice-note, .bg-comparison { margin: 0 0 8px; font-size: 13px; text-align: center; }
.bg-reset { display: grid; gap: 2px; margin-top: 2px; color: var(--ink); }
.bg-reset__in { display: block; font: 700 15px/1.25 var(--f-ui); font-variant-numeric: tabular-nums; }
.bg-reset__note { margin: 0; font: 600 12px/1.25 var(--f-ui); }
.bg-reset__local { font: 400 11px/1.25 var(--f-ui); }
.bg-toast {
  position: absolute; left: 50%; bottom: max(18px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  z-index: 5; margin: 0; padding: 10px 16px; border-radius: 14px; max-width: calc(100% - 32px);
  background: #FFFFFF; border: 2px solid color-mix(in srgb, var(--ink) 40%, #FFFFFF); box-shadow: var(--e1);
  color: var(--ink); font: 700 14px/1.3 var(--f-ui); text-align: center; overflow-wrap: anywhere;
  user-select: text; -webkit-user-select: text;
}
.bg-toast[hidden] { display: none; }
.bg-steps { margin: 0; padding: 0 0 0 20px; display: grid; gap: 4px; text-align: left; font-size: 14px; line-height: 1.35; color: var(--ink); }
.bg-controls { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; text-align: left; font-size: 13px; color: var(--ink-2); }
.bg-controls li { display: flex; justify-content: space-between; align-items: center; }

/* Molecule: coach hint (first-run prompt) */
.bg-coach {
  position: absolute; left: 50%; bottom: max(18px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 2;
  margin: 0; padding: 10px 16px; border-radius: 999px; white-space: nowrap;
  background: var(--surface); box-shadow: var(--e1); font-weight: 700; font-size: 14px;
  pointer-events: none; transition: opacity 300ms;
}
.bg-coach[data-hide="true"] { opacity: 0; }
.bg-overlay:not([hidden]) ~ .bg-coach, .bg-coach:has(~ .bg-overlay:not([hidden])) { opacity: 0; }

/* Organism: game-over transition. The root carries the phase: data-go = freeze | dim | stamp.
   freeze: the canvas keeps its last frame (the runtime stops drawing), nothing else changes.
   dim: the stage desaturates and a light ink scrim fades in. stamp: the flat stamp pops in.
   Flat: solid fill, a crisp 2px border in a darker shade of the game colour, no shadow/bevel. */
.bg-go {
  position: absolute; inset: 0; z-index: 3; display: grid; place-items: center; padding: 20px;
  pointer-events: none; background: transparent; transition: background-color 200ms ease-out;
}
.bg-go[hidden] { display: none; }
[data-go="dim"] .bg-go, [data-go="stamp"] .bg-go { background: color-mix(in srgb, var(--ink) 20%, transparent); }

.bg-go__stamp {
  display: grid; gap: 6px; justify-items: center; min-width: min(240px, 100%); padding: 16px 26px 14px;
  border-radius: 18px; background: var(--paper); border: 2px solid var(--go-ink, var(--signal-ink));
  color: var(--go-ink, var(--signal-ink)); text-align: center; opacity: 0; transform: rotate(-3deg);
}
[data-go="stamp"] .bg-go__stamp { opacity: 1; animation: bg-go-pop 220ms cubic-bezier(0.2, 0.9, 0.3, 1.25) both; }
@keyframes bg-go-pop {
  0% { opacity: 0; transform: rotate(-3deg) scale(0.6); }
  60% { opacity: 1; transform: rotate(-3deg) scale(1.05); }
  100% { opacity: 1; transform: rotate(-3deg) scale(1); }
}
.bg-go__word { font: 800 40px/1 var(--f-display); letter-spacing: -0.02em; white-space: nowrap; }
.bg-go__row { display: flex; gap: 8px; align-items: baseline; }
.bg-go__label { color: var(--go-ink, var(--ink-2)); }
.bg-go__score { font: 800 24px/1 var(--f-display); letter-spacing: -0.01em; color: var(--ink); font-variant-numeric: tabular-nums; }
/* Reduced motion: a simple fade only (no pop, scale or tilt). The runtime sets data-motion. */
[data-motion="reduce"] .bg-go__stamp { transform: none; transition: opacity 200ms ease-out; }
[data-motion="reduce"][data-go="stamp"] .bg-go__stamp { animation: none; }

/* Embedded / render mode: hide the corner HUD and coach so only the playfield shows
   (the shared .bg-ctl keeps its sound button; see above). */
[data-chrome="off"] .bg-hud, [data-chrome="off"] .bg-coach { display: none; }

@media (prefers-reduced-motion: reduce) {
  .bg-pop, .bg-modal { animation: none; }
  .bg-ctl__btn { transition: none; }
  .bg-go__stamp { transform: none; transition: opacity 200ms ease-out; }
  [data-go="stamp"] .bg-go__stamp { animation: none; }
}
