/* ==========================================================================
   Jelly buttons — the one shared button material for the whole site.
   Loaded last so it is the single source of truth for every button surface:
   background, border, depth, gloss, shadows and interactive states.
   Component stylesheets only set shape/size (padding, radius, font) and,
   when needed, a colour through the --j-* tokens below.

   Tokens (all optional, set them on any selector):
     --j-base   main colour (secondary navy by default)
     --j-top    upper gradient stop   (derived from --j-base)
     --j-bot    lower gradient stop   (derived from --j-base)
     --j-rim    darker lower rim that gives the button thickness
     --j-edge   border colour
     --j-ink    text colour
     --j-glow   soft ambient glow
     --j-depth  rim thickness (default 4px)
     --j-ring / --j-ring-w   selection ring (e.g. the picked hero)
   ========================================================================== */
/* Tokens never inherit, so a coloured card can't recolour the buttons inside it. */
@property --j-base { syntax: '*'; inherits: false; }
@property --j-top { syntax: '*'; inherits: false; }
@property --j-bot { syntax: '*'; inherits: false; }
@property --j-rim { syntax: '*'; inherits: false; }
@property --j-edge { syntax: '*'; inherits: false; }
@property --j-ink { syntax: '*'; inherits: false; }
@property --j-ts { syntax: '*'; inherits: false; }
@property --j-glow { syntax: '*'; inherits: false; }
@property --j-depth { syntax: '*'; inherits: false; }
@property --j-press { syntax: '*'; inherits: false; }
@property --j-lift { syntax: '*'; inherits: false; }
@property --j-gloss { syntax: '*'; inherits: false; }
@property --j-shine { syntax: '*'; inherits: false; }
@property --j-bw { syntax: '*'; inherits: false; }
@property --j-ring { syntax: '*'; inherits: false; }
@property --j-ring-w { syntax: '*'; inherits: false; }

:root {
  --j-dur: 220ms;
  --j-ease: cubic-bezier(.25, .8, .35, 1);
  --j-focus: #fff;
}

/* ---------- Which elements are buttons ---------- */
:is(
  .btn, .icon-btn, .speak-btn, .option, .tile, .skip-link,
  .tabs button, button.chip, a.chip,
  .topnav a, .sidebar a,
  .bc-foot, .jc-card-go,
  .g-options button, .g-keys button, .g-touch button, .g-opt,
  .blk-pal, .blk-tool, .blk-into, .track-card, .simon-pad, .jc-hero-pick,
  .ln-big-play, .ln-tile, .ln-match-item, .math-table-btn, .st-dot
) {
  --_top: var(--j-top, color-mix(in oklab, var(--j-base, #183a7c), #fff 30%));
  --_bot: var(--j-bot, color-mix(in oklab, var(--j-base, #183a7c), #000 28%));
  --_rim: var(--j-rim, color-mix(in oklab, var(--j-base, #183a7c), #000 58%));
  --_depth: max(1px, calc(var(--j-depth, 4px) + var(--j-press, 0px)));
  --_gloss: var(--j-gloss, .5); /* plain variable so the ::before highlight can read it */
  color: var(--j-ink, #fff);
  text-shadow: var(--j-ts, 0 1px 1px rgba(0, 0, 0, .35));
  border: var(--j-bw, 1px) solid var(--j-edge, rgba(96, 165, 250, .7));
  background: linear-gradient(180deg, var(--_top) 0%, var(--j-base, #183a7c) 52%, var(--_bot) 100%);
  box-shadow:
    0 0 0 var(--j-ring-w, 0px) var(--j-ring, transparent),
    inset 0 1.5px 0 rgba(255, 255, 255, var(--j-shine, .5)),          /* upper-edge reflection */
    inset 0 -3px 6px rgba(0, 0, 0, .18),                             /* lower inner shading */
    0 var(--_depth) 0 var(--_rim),                                   /* thickness rim */
    0 calc(var(--_depth) + 4px) calc(10px + var(--j-lift, 0px)) -2px rgba(0, 0, 0, .45), /* contact shadow */
    0 0 calc(14px + var(--j-lift, 0px)) -3px var(--j-glow, rgba(59, 130, 246, .35));       /* ambient glow */
  text-decoration: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition:
    translate var(--j-dur) var(--j-ease), box-shadow var(--j-dur) var(--j-ease),
    filter var(--j-dur) var(--j-ease), border-color var(--j-dur) var(--j-ease),
    color var(--j-dur) var(--j-ease), opacity var(--j-dur) var(--j-ease);
}
/* Zero specificity so positioned buttons (skip link, drag ghosts) keep their own position. */
:where(
  .btn, .icon-btn, .speak-btn, .option, .tile, .skip-link,
  .tabs button, button.chip, a.chip, .topnav a, .sidebar a, .bc-foot, .jc-card-go,
  .g-options button, .g-keys button, .g-touch button, .g-opt,
  .blk-pal, .blk-tool, .blk-into, .track-card, .simon-pad, .jc-hero-pick,
  .ln-big-play, .ln-tile, .ln-match-item, .math-table-btn, .st-dot
) { position: relative; isolation: isolate; }

/* Layered gloss highlight across the upper half */
:is(
  .btn, .icon-btn, .speak-btn, .option, .tile, .skip-link,
  .tabs button, button.chip, a.chip, .topnav a, .sidebar a, .bc-foot, .jc-card-go,
  .g-options button, .g-keys button, .g-touch button, .g-opt,
  .blk-pal, .blk-tool, .blk-into, .track-card, .simon-pad, .jc-hero-pick,
  .ln-big-play, .ln-tile, .ln-match-item, .math-table-btn, .st-dot
)::before {
  content: ''; position: absolute; z-index: -1; pointer-events: none;
  left: 7%; right: 7%; top: 2px; height: 48%; border-radius: inherit;
  background:
    radial-gradient(60% 90% at 30% 0%, rgba(255, 255, 255, calc(var(--_gloss) + .15)), transparent 70%),
    linear-gradient(180deg, rgba(255, 255, 255, var(--_gloss)), rgba(255, 255, 255, 0) 96%);
  filter: blur(.4px);
}

/* ---------- Colour variants ---------- */
/* Primary — glossy golden yellow with warm orange-gold lower shading */
.btn-gold, .speak-btn, .ln-big-play, .skip-link, .ln-match-item.sel,
.tabs button[aria-pressed="true"], .chip.on, .chip[aria-pressed="true"],
.topnav a.active, .sidebar a.active, .jc-hero-pick.on, .st-dot.on, .blk-into.on {
  --j-base: #ffc72c; --j-top: #fff1a8; --j-bot: #f59e0b; --j-rim: #b4600a;
  --j-edge: #ffe17a; --j-ink: #2a1700; --j-glow: rgba(255, 184, 28, .5);
  --j-ts: 0 1px 0 rgba(255, 255, 255, .45);
}
/* Secondary — glossy navy with blue borders (also the default for every other button) */
.btn-ghost {
  --j-base: #183a7c; --j-edge: rgba(96, 165, 250, .8); --j-glow: rgba(59, 130, 246, .4);
}
.btn-blue, .ln-tile.in {
  --j-base: #2f7bf5; --j-top: #8cc2ff; --j-bot: #1b56c4; --j-rim: #0f347e;
  --j-edge: #93c5fd; --j-glow: rgba(47, 128, 255, .45);
}
/* Success — glossy green */
.btn-green, .icon-btn.plus, .option.right, .g-opt.ok, .ln-match-item.matched,
.g-options button.good, .g-keys button.good {
  --j-base: #1fae52; --j-top: #7eeea3; --j-bot: #118a3c; --j-rim: #0a5a26;
  --j-edge: #86efac; --j-glow: rgba(34, 197, 94, .45);
}
/* Destructive — glossy red */
.btn-red, .option.wrong, .g-opt.no, .ln-match-item.wrong,
.g-options button.bad, .g-keys button.bad, .chat-form .mic-on {
  --j-base: #e23b3b; --j-top: #ff9d9d; --j-bot: #b71c1c; --j-rim: #741010;
  --j-edge: #fca5a5; --j-glow: rgba(239, 68, 68, .45);
}
/* Game pieces that keep their own meaningful colour (block colour, course colour, Simon pad) */
.blk-pal, .track-card, .simon-pad {
  --j-edge: rgba(255, 255, 255, .6); --j-ink: #10142a; --j-ts: 0 1px 0 rgba(255, 255, 255, .4);
  --j-glow: rgba(0, 0, 0, 0); --j-rim: rgba(0, 0, 0, .38);
  --j-bot: color-mix(in oklab, var(--j-base, #183a7c), #000 12%);
}
.track-card.on, .ln-tile.in { --j-ring-w: 3px; --j-ring: var(--gold); }
.track-card.on { --j-edge: #fff; }
.ln-tile.in { --j-edge: var(--gold); }
/* Selected-but-not-primary picks keep the navy body with a gold ring */
.jc-hero-pick.on { --j-ring-w: 3px; --j-ring: rgba(255, 199, 44, .45); }
/* Lighter, quieter navigation items */
.topnav a:not(.active), .sidebar a:not(.active) {
  --j-base: #12295c; --j-edge: rgba(96, 165, 250, .35); --j-glow: rgba(59, 130, 246, .18);
  --j-depth: 3px; --j-gloss: .28; --j-shine: .3;
}
/* Small controls get a thinner rim */
.btn-sm, .speak-btn, .icon-btn.sm, .blk-tool, .blk-into, .bc-foot, .jc-card-go, button.chip, a.chip { --j-depth: 3px; }
.st-dot { --j-depth: 2px; --j-gloss: .6; }
.btn-lg { --j-depth: 5px; }
/* Locked card: clearly unavailable but still readable */
.big-card.is-locked .bc-foot {
  --j-base: #26335a; --j-edge: #3a4b7a; --j-ink: var(--muted); --j-glow: transparent; --j-gloss: .18;
}

/* ---------- Interactive states ---------- */
@media (hover: hover) {
  :is(
    .btn, .icon-btn, .speak-btn, .option, .tile, .tabs button, button.chip, a.chip, .topnav a, .sidebar a,
    .g-options button, .g-keys button, .g-touch button, .g-opt,
    .blk-pal, .blk-tool, .blk-into, .track-card, .simon-pad, .jc-hero-pick,
    .ln-big-play, .ln-tile, .ln-match-item, .math-table-btn, .st-dot
  ):hover:not(:disabled, [aria-disabled="true"]),
  :is(.big-card, .jc-card):hover :is(.bc-foot, .jc-card-go) {
    translate: 0 -2px; --j-press: 2px; --j-lift: 8px; filter: brightness(1.08) saturate(1.05);
  }
}
:is(
  .btn, .icon-btn, .speak-btn, .option, .tile, .tabs button, button.chip, a.chip, .topnav a, .sidebar a,
  .g-options button, .g-keys button, .g-touch button, .g-opt,
  .blk-pal, .blk-tool, .blk-into, .track-card, .simon-pad, .jc-hero-pick,
  .ln-big-play, .ln-tile, .ln-match-item, .math-table-btn, .st-dot
):active:not(:disabled, [aria-disabled="true"]),
:is(.big-card, .jc-card):active :is(.bc-foot, .jc-card-go) {
  translate: 0 2px; --j-press: -3px; --j-lift: -4px; filter: brightness(.97);
  transition-duration: 120ms;
}
:is(
  .btn, .icon-btn, .speak-btn, .option, .tile, .skip-link, .tabs button, button.chip, a.chip, .topnav a, .sidebar a,
  .g-options button, .g-keys button, .g-touch button, .g-opt,
  .blk-pal, .blk-tool, .blk-into, .track-card, .simon-pad, .jc-hero-pick,
  .ln-big-play, .ln-tile, .ln-match-item, .math-table-btn, .st-dot
):focus-visible {
  outline: 3px solid var(--j-focus); outline-offset: 3px;
}
/* Disabled: flatter and quieter, still legible. Answer pieces that are only locked after
   answering (right/wrong/placed) keep their colour so the result stays visible. */
:is(
  .btn, .icon-btn, .speak-btn, .option, .tile, .tabs button, button.chip,
  .g-options button, .g-keys button, .g-touch button, .g-opt,
  .blk-pal, .blk-tool, .blk-into, .track-card, .simon-pad, .jc-hero-pick,
  .ln-big-play, .ln-tile, .ln-match-item, .st-dot
):is(:disabled, [aria-disabled="true"]):not(.right, .wrong, .good, .bad, .ok, .no, .matched, .in) {
  cursor: not-allowed; opacity: .6; filter: saturate(.45) brightness(.9);
  --j-depth: 1px; --j-glow: transparent; --j-gloss: .2;
}
[aria-busy="true"]:is(.btn, button) { cursor: progress; }

/* Light surfaces (reward pop-up) need a dark focus ring */
.reward { --j-focus: #10142a; }

/* Gentle "ping" ring for live controls (listen button, recording mic) — uses a pseudo-element so
   it never fights the button's own shadows or the keyboard focus ring. */
.ln-big-play::after, .chat-form .mic-on::after {
  content: ''; position: absolute; inset: -2px; border-radius: inherit; pointer-events: none;
  animation: jelly-ping 1.8s var(--j-ease) infinite;
}
.chat-form .mic-on::after { animation-duration: 1.2s; --_ping: rgba(229, 72, 77, .6); }
@keyframes jelly-ping {
  0% { box-shadow: 0 0 0 0 var(--_ping, rgba(255, 199, 44, .55)); }
  100% { box-shadow: 0 0 0 12px transparent; }
}

@media (prefers-reduced-motion: reduce) {
  :is(.btn, .icon-btn, .speak-btn, .option, .tile, .tabs button, button.chip, a.chip, .topnav a, .sidebar a,
    .bc-foot, .jc-card-go, .g-options button, .g-keys button, .g-touch button, .g-opt,
    .blk-pal, .blk-tool, .blk-into, .track-card, .simon-pad, .jc-hero-pick,
    .ln-big-play, .ln-tile, .ln-match-item, .math-table-btn, .st-dot) { translate: none !important; }
}
