/* ===========================================================================
   Piano Game (/ejercicios/piano-game).
   Built on the Music Lab tokens (music-lab.css), the Practice Lab pieces
   (practice-lab.css: modes, segments, transport, inputs; practice-scales.css:
   the exercise field) and Canta y afina's cards and fields (practice-sing.css).
   Nothing here defines a colour of its own beyond the tokens, so day and
   night both work. Phone first; the stage is touch-first (keys ≥ 44 px tall,
   several fingers at once) and the roll scales with the viewport.
   =========================================================================== */

.ml-pg {
  max-width: 1320px;
  margin: 0 auto;
  padding: 22px 16px calc(96px + env(safe-area-inset-bottom, 0px));
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.ml-pg [hidden] { display: none !important; }
.ml-pg .ml-sg-card h2 { font-size: 1.02rem; }
.ml-pg kbd { font: 600 12px/1 Inter, sans-serif; padding: 2px 5px; border: 1px solid var(--ml-line-strong); border-bottom-width: 2px; border-radius: 5px; background: var(--ml-surface-2); color: var(--ml-text); }

/* ── Header ──────────────────────────────────────────────────────────── */

.ml-pg__head { margin-bottom: 14px; }
.ml-pg__title { display: flex; flex-wrap: wrap; gap: 12px 24px; align-items: flex-start; justify-content: space-between; }
.ml-pg__title-main { min-width: 0; flex: 1 1 320px; }
.ml-pg__title h1 { font-size: clamp(1.7rem, 4.6vw, 2.5rem); margin: 0; }
.ml-pg__grad { background: var(--ml-grad-text); -webkit-background-clip: text; background-clip: text; color: transparent; }
.ml-pg__ex-name { margin: 6px 0 2px; font-size: 1.05rem; display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: baseline; }
.ml-pg__ex-name strong { font-family: "Space Grotesk", sans-serif; font-size: 1.2rem; }
.ml-pg__source { color: var(--ml-dim); font-size: 13px; }
.ml-pg__pick { display: grid; gap: 8px; flex: 0 1 440px; min-width: min(100%, 300px); }

/* ── Layout ──────────────────────────────────────────────────────────── */

.ml-pg__grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }
.ml-pg__grid > * { min-width: 0; }
.ml-pg__card { margin: 0; }

/* ── Mode and input ──────────────────────────────────────────────────── */

.ml-pg__controls { display: grid; gap: 12px; }
.ml-pg__ctl { display: grid; gap: 6px; }
.ml-pg__ctl-label { font-size: 12px; color: var(--ml-muted); font-weight: 600; }
.ml-pg__inputs { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); }
.ml-pg__inputs .ml-seg__b { min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 9px 6px; min-height: 44px; }
.ml-pg .ml-pl__mode { min-height: 46px; font-size: 13px; }

/* ── Input cards ─────────────────────────────────────────────────────── */

.ml-pg__card-actions { display: flex; gap: 8px; align-items: center; }
.ml-pg__card-actions .ml-btn { min-height: 46px; min-width: 120px; }
.ml-pg__octave { display: inline-flex; align-items: center; gap: 6px; }
.ml-pg__octave output { min-width: 72px; text-align: center; font: 600 14px "Space Grotesk", sans-serif; }
.ml-pg__midi[data-midi="on"] h2 svg { color: var(--ml-emerald); }
.ml-pg__midi[data-midi="unsupported"] .ml-sg__mic-status,
.ml-pg__midi[data-midi="denied"] .ml-sg__mic-status,
.ml-pg__midi[data-midi="error"] .ml-sg__mic-status,
.ml-pg__mic[data-mic="denied"] .ml-sg__mic-status,
.ml-pg__mic[data-mic="nodevice"] .ml-sg__mic-status,
.ml-pg__mic[data-mic="insecure"] .ml-sg__mic-status,
.ml-pg__mic[data-mic="unsupported"] .ml-sg__mic-status { color: var(--ml-rose); }
.ml-pg__midi-last { font-family: ui-monospace, "SFMono-Regular", Menlo, monospace; font-size: 13px; }
.ml-pg__mic .ml-sg__mic-tools output { font: 600 15px "Space Grotesk", sans-serif; }

/* ── Stage ───────────────────────────────────────────────────────────── */

.ml-pg__stage { border: 1px solid var(--ml-line); border-radius: 16px; background: var(--ml-surface); padding: 10px; display: grid; gap: 8px; }
.ml-pg__strip { display: grid; grid-template-columns: repeat(auto-fit, minmax(92px, 1fr)); gap: 6px; }
.ml-pg__stat { display: flex; flex-direction: column; gap: 1px; padding: 6px 10px; border-radius: 10px; background: var(--ml-bar-bg); min-width: 0; }
.ml-pg__stat span { font-size: 11px; color: var(--ml-dim); text-transform: uppercase; letter-spacing: .06em; font-weight: 700; }
.ml-pg__stat strong { font: 700 15px "Space Grotesk", sans-serif; color: var(--ml-text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ml-pg__stat--score strong { font-size: 18px; color: var(--ml-coral); }
.ml-pg[data-state="waiting"] #ml-pg-state { color: var(--ml-amber); }
.ml-pg[data-state="playing"] #ml-pg-state { color: var(--ml-emerald); }
.ml-pg[data-state="done"] #ml-pg-state { color: var(--ml-cyan); }

.ml-pg__roll { position: relative; height: clamp(230px, 40vh, 440px); border-radius: 12px 12px 0 0; overflow: hidden; background: var(--ml-bar-bg); border: 1px solid var(--ml-line); border-bottom: 0; }
.ml-pg__roll canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.ml-pg__roll-msg {
  position: absolute; left: 50%; top: 42%; transform: translate(-50%, -50%);
  max-width: min(92%, 420px); text-align: center; padding: 10px 16px; border-radius: 12px;
  background: color-mix(in srgb, var(--ml-bg-2) 88%, transparent); border: 1px solid var(--ml-line); color: var(--ml-muted); font-size: 14px; line-height: 1.4;
  pointer-events: none;
}
.ml-pg__feedback {
  position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%);
  padding: 4px 12px; border-radius: 999px; font: 700 13px "Space Grotesk", sans-serif;
  background: var(--ml-surface-2); color: var(--ml-text); border: 1px solid var(--ml-line-strong);
  opacity: 0; transition: opacity .18s; pointer-events: none; white-space: nowrap;
}
.ml-pg__feedback.is-on { opacity: 1; }
.ml-pg__feedback[data-kind="perfect"] { color: var(--ml-emerald); border-color: var(--ml-emerald); }
.ml-pg__feedback[data-kind="good"] { color: var(--ml-cyan); border-color: var(--ml-cyan); }
.ml-pg__feedback[data-kind="ok"] { color: var(--ml-amber); border-color: var(--ml-amber); }
.ml-pg__feedback[data-kind="miss"], .ml-pg__feedback[data-kind="extra"] { color: var(--ml-rose); border-color: var(--ml-rose); }

/* The keys: absolutely placed from the server's percentages, so the roll
   can read their boxes and draw its lanes over them. */
.ml-pg__keys { position: relative; height: clamp(112px, 17vw, 172px); border-radius: 0 0 12px 12px; overflow: hidden; background: var(--ml-key-black); border: 1px solid var(--ml-line); border-top: 2px solid var(--ml-coral); touch-action: none; user-select: none; -webkit-user-select: none; }
.ml-pg__key {
  position: absolute; top: 0; margin: 0; padding: 0; border: 0; cursor: pointer; touch-action: none;
  display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: 2px;
  font: 600 11px Inter, sans-serif; -webkit-tap-highlight-color: transparent;
}
.ml-pg__key--white { height: 100%; background: #f7f1f5; color: #3a2a3a; border-right: 1px solid rgba(0, 0, 0, .28); border-radius: 0 0 5px 5px; z-index: 1; box-shadow: inset 0 -6px 0 rgba(0, 0, 0, .08); }
.ml-pg__key--black { height: 62%; background: linear-gradient(#2a1f30, #130c17); color: #e8dce6; border-radius: 0 0 4px 4px; z-index: 2; box-shadow: 0 3px 0 rgba(0, 0, 0, .5); }
.ml-pg__key-name { display: none; padding-bottom: 6px; pointer-events: none; }
.ml-pg__key--black .ml-pg__key-name { font-size: 9.5px; padding-bottom: 4px; }
.ml-pg--names .ml-pg__key--c .ml-pg__key-name, .ml-pg--names.ml-pg--all-names .ml-pg__key-name { display: block; }
.ml-pg__cap { display: none; position: absolute; top: 5px; left: 50%; transform: translateX(-50%); min-width: 16px; padding: 1px 3px; border-radius: 4px; background: rgba(0, 0, 0, .55); color: #fff; font: 700 10px Inter, sans-serif; pointer-events: none; }
.ml-pg__key--black .ml-pg__cap { background: rgba(255, 255, 255, .22); }
.ml-pg--caps .ml-pg__cap:not(:empty) { display: block; }
.ml-pg__key.is-hint { box-shadow: inset 0 0 0 3px var(--ml-cyan); }
.ml-pg__key.is-hint::before { content: ""; position: absolute; left: 50%; top: 22%; width: 8px; height: 8px; border-radius: 50%; background: var(--ml-cyan); transform: translateX(-50%); }
.ml-pg__key--black.is-hint::before { top: 30%; }
.ml-pg__key.is-down { background: color-mix(in srgb, var(--ml-coral) 55%, #f7f1f5); color: #fff; transform: translateY(1px); }
.ml-pg__key--black.is-down { background: color-mix(in srgb, var(--ml-coral) 55%, #130c17); }
.ml-pg__key.is-hit { background: color-mix(in srgb, var(--ml-emerald) 60%, #f7f1f5); color: #0b2a1c; }
.ml-pg__key--black.is-hit { background: color-mix(in srgb, var(--ml-emerald) 55%, #130c17); color: #eafff4; }
.ml-pg__key.is-wrong { background: color-mix(in srgb, var(--ml-rose) 60%, #f7f1f5); color: #fff; }
.ml-pg__key--black.is-wrong { background: color-mix(in srgb, var(--ml-rose) 60%, #130c17); }
.ml-pg__key.is-hit::after, .ml-pg__key.is-wrong::after { position: absolute; left: 50%; top: 40%; transform: translate(-50%, -50%); font: 800 14px/1 Inter, sans-serif; }
.ml-pg__key.is-hit::after { content: "✓"; }
.ml-pg__key.is-wrong::after { content: "✕"; }
.ml-pg__key:focus-visible { outline: 3px solid var(--ml-coral); outline-offset: -3px; }
.ml-pg__keys--hands .ml-pg__key.is-hint.is-left { box-shadow: inset 0 0 0 3px var(--ml-violet); }
.ml-pg__keys--hands .ml-pg__key.is-hint.is-left::before { background: var(--ml-violet); }

/* ── Transport ───────────────────────────────────────────────────────── */

.ml-pg__transport-card { display: grid; gap: 10px; padding: 14px 16px; border: 1px solid var(--ml-line); border-radius: 14px; background: var(--ml-surface); }
.ml-pg__transport-card .ml-pl__transport { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; }
.ml-pg__transport-card .ml-pl__tempo-label small { color: var(--ml-dim); font-weight: 500; }
.ml-pg__options { display: flex; flex-wrap: wrap; gap: 10px 18px; align-items: flex-end; padding-top: 10px; border-top: 1px solid var(--ml-line); }
.ml-pg__options .ml-pl__field { min-width: 150px; }
.ml-pg__latency { display: inline-flex; align-items: center; gap: 6px; color: var(--ml-muted); font-size: 13px; }
.ml-pg__check { align-self: center; }

/* ── Results and history ─────────────────────────────────────────────── */

.ml-pg__score-row { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 12px 20px; align-items: center; }
.ml-pg__score-main { display: flex; flex-direction: column; align-items: center; justify-content: center; min-width: 120px; padding: 10px 14px; border-radius: 14px; background: var(--ml-bar-bg); }
.ml-pg__score-main strong { font: 800 44px/1 "Space Grotesk", sans-serif; background: var(--ml-grad-text); -webkit-background-clip: text; background-clip: text; color: transparent; }
.ml-pg__score-main span { margin-top: 4px; font-size: 13px; color: var(--ml-muted); font-weight: 600; }
.ml-pg__stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 6px 14px; margin: 0; }
.ml-pg__stats div { display: flex; flex-direction: column; gap: 1px; }
.ml-pg__stats dt { font-size: 11px; color: var(--ml-dim); text-transform: uppercase; letter-spacing: .06em; font-weight: 700; }
.ml-pg__stats dd { margin: 0; font: 600 15px "Space Grotesk", sans-serif; }
.ml-pg__results-note { margin: 10px 0 0; color: var(--ml-muted); font-size: 13.5px; }
.ml-pg__notes { list-style: none; margin: 12px 0 0; padding: 0; display: flex; flex-wrap: wrap; gap: 5px; }
.ml-pg__notes li { display: inline-flex; align-items: center; gap: 5px; padding: 3px 8px; border-radius: 999px; border: 1px solid var(--ml-line); background: var(--ml-surface-2); font-size: 12px; font-weight: 600; }
.ml-pg__notes li::before { font-size: 11px; }
.ml-pg__notes li[data-status="perfect"] { color: var(--ml-emerald); }
.ml-pg__notes li[data-status="perfect"]::before { content: "✓"; }
.ml-pg__notes li[data-status="good"] { color: var(--ml-cyan); }
.ml-pg__notes li[data-status="good"]::before { content: "✓"; }
.ml-pg__notes li[data-status="ok"] { color: var(--ml-amber); }
.ml-pg__notes li[data-status="ok"]::before { content: "~"; }
.ml-pg__notes li[data-status="miss"] { color: var(--ml-rose); }
.ml-pg__notes li[data-status="miss"]::before { content: "✕"; }
.ml-pg__notes li[data-status="mono"] { color: var(--ml-dim); }
.ml-pg__notes li[data-status="mono"]::before { content: "·"; }
.ml-pg__notes li small { color: var(--ml-dim); font-weight: 500; }
.ml-pg__results-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.ml-pg__hist { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.ml-pg__hist li { display: grid; grid-template-columns: 52px minmax(0, 1fr) auto; gap: 4px 12px; align-items: center; padding: 8px 10px; border-radius: 10px; background: var(--ml-bar-bg); font-size: 13px; }
.ml-pg__hist strong { font: 800 20px "Space Grotesk", sans-serif; color: var(--ml-coral); }
.ml-pg__hist span { color: var(--ml-muted); min-width: 0; }
.ml-pg__hist time { color: var(--ml-dim); font-size: 12px; white-space: nowrap; }
.ml-pg__help ul { margin: 0; padding-left: 18px; color: var(--ml-muted); font-size: 14px; display: flex; flex-direction: column; gap: 6px; }
.ml-pg__help strong { color: var(--ml-text); }

/* ── Tablet ──────────────────────────────────────────────────────────── */

@media (min-width: 760px) {
  .ml-pg__controls { grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); align-items: start; }
  .ml-pg__grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-areas:
      "controls controls"
      "keyshelp keyshelp"
      "midi midi"
      "mic mic"
      "stage stage"
      "transport transport"
      "results results"
      "history help";
  }
  .ml-pg__controls { grid-area: controls; }
  .ml-pg__keys-help { grid-area: keyshelp; }
  .ml-pg__midi { grid-area: midi; }
  .ml-pg__mic { grid-area: mic; }
  .ml-pg__stage { grid-area: stage; }
  .ml-pg__transport-card { grid-area: transport; }
  .ml-pg__results { grid-area: results; }
  .ml-pg__history { grid-area: history; }
  .ml-pg__help { grid-area: help; }
  .ml-pg__roll { height: clamp(260px, 38vh, 460px); }
}

/* ── Desktop ─────────────────────────────────────────────────────────── */

@media (min-width: 1100px) {
  .ml-pg__grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-template-areas:
      "controls controls controls"
      "keyshelp keyshelp keyshelp"
      "midi midi midi"
      "mic mic mic"
      "stage stage stage"
      "transport transport transport"
      "results history help";
  }
  .ml-pg__results .ml-pg__score-row { grid-template-columns: 1fr; }
  .ml-pg__roll { height: clamp(300px, 42vh, 520px); }
  .ml-pg__keys { height: 180px; }
}

/* ── Small phones ────────────────────────────────────────────────────── */

@media (max-width: 480px) {
  .ml-pg { padding-left: 10px; padding-right: 10px; }
  .ml-pg__stage { padding: 8px; }
  .ml-pg__strip { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .ml-pg__stat--score { grid-column: span 1; }
  .ml-pg__inputs .ml-seg__b { font-size: 11.5px; padding: 8px 4px; }
  .ml-pg .ml-pl__mode { font-size: 12px; }
  .ml-pg__keys { height: 128px; }
  .ml-pg__key-name { font-size: 10px; }
  .ml-pg__key--black .ml-pg__key-name { display: none; } /* 15 px wide: the white keys carry the names */
  .ml-pg__options .ml-pl__field { min-width: 0; flex: 1 1 45%; }
  .ml-pg__hist li { grid-template-columns: 44px minmax(0, 1fr); }
  .ml-pg__hist time { grid-column: 2; }
}

@media (prefers-reduced-motion: reduce) {
  .ml-pg__feedback { transition: none; }
}
