/* Robot arm lab page. Builds on the site's styles.css tokens (--bg, --ink,
   --muted, --line, --accent, --focus, --gutter, fonts, dark mode). */

.page-robot {
  --ok: #1f9d5f;
  --ok-ink: #ffffff;
  --stage-bg: #fbfbf9;
  --stage-glow: #ffffff;
  --panel: var(--raised);
  --key: #ffffff;
  --key-ink: var(--ink);
  --key-press: #eef5ff;
  --shadow-soft: 0 1px 1px rgba(20, 20, 20, 0.04), 0 6px 18px -8px rgba(20, 20, 20, 0.18);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) .page-robot {
    --ok: #3ddc8b;
    --ok-ink: #0b0b0b;
    --stage-bg: #161618;
    --stage-glow: #1f1f23;
    --key: #222225;
    --key-press: #1c2a3a;
    --shadow-soft: 0 1px 1px rgba(0, 0, 0, 0.4), 0 8px 20px -10px rgba(0, 0, 0, 0.7);
  }
}

:root[data-theme='dark'] .page-robot {
  --ok: #3ddc8b;
  --ok-ink: #0b0b0b;
  --stage-bg: #161618;
  --stage-glow: #1f1f23;
  --key: #222225;
  --key-press: #1c2a3a;
  --shadow-soft: 0 1px 1px rgba(0, 0, 0, 0.4), 0 8px 20px -10px rgba(0, 0, 0, 0.7);
}

.ra {
  max-width: 76rem;
  margin: 0 auto;
  padding: 0 var(--gutter);
}

/* ---------- Header ---------- */
.ra-head {
  padding: 1.75rem 0 1.25rem;
  max-width: 46rem;
}

.ra-back {
  display: inline-block;
  margin-bottom: 0.9rem;
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 0.7rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  text-decoration: none;
}

.ra-back:hover {
  color: var(--ink);
}

.ra-head h1 {
  font-size: clamp(1.75rem, 4vw, 2.5rem);
  margin-bottom: 0.6rem;
}

.ra-intro {
  margin: 0;
  color: var(--muted);
  font-size: 0.975rem;
}

/* ---------- Game layout ---------- */
.ra-game {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 0.75rem;
}

.ra-game:focus {
  outline: none;
}

@media (min-width: 60rem) {
  .ra-game {
    grid-template-columns: minmax(0, 1fr) 18rem;
    align-items: start;
    gap: 1rem;
  }
}

.ra-stage {
  position: relative;
  height: clamp(22rem, calc(100vh - 19rem), 44rem);
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: radial-gradient(120% 90% at 50% 18%, var(--stage-glow), var(--stage-bg) 70%);
  isolation: isolate;
}

@media (max-width: 59.99rem) {
  .ra-stage {
    height: clamp(20rem, 58vh, 36rem);
  }
}

@media (max-width: 40rem) {
  .ra-stage {
    height: min(112vw, 54vh);
    min-height: 18rem;
    border-radius: 12px;
  }
}

#scene {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: grab;
}

#scene:active {
  cursor: grabbing;
}

/* ---------- HUD ---------- */
.ra-hud {
  position: absolute;
  top: 0.65rem;
  left: 0.65rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  pointer-events: none;
}

.ra-stat,
.ra-state {
  display: inline-flex;
  align-items: baseline;
  gap: 0.4rem;
  padding: 0.3rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  font-family: var(--font-mono);
  font-size: 0.7rem;
  line-height: 1.3;
  white-space: nowrap;
}

.ra-stat-k {
  color: var(--muted);
  font-size: 0.62rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.ra-stat-v {
  font-variant-numeric: tabular-nums;
}

.ra-state {
  position: absolute;
  top: 0.65rem;
  right: 0.65rem;
  color: var(--muted);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  font-size: 0.62rem;
  pointer-events: none;
}

.ra-state[data-busy='true'] {
  color: var(--ink);
  border-color: var(--accent);
}

/* ---------- Overlays ---------- */
.ra-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 1rem;
  pointer-events: none;
  z-index: 3;
}

.ra-overlay[hidden] {
  display: none;
}

.ra-card {
  pointer-events: auto;
  width: min(22rem, 100%);
  padding: 1.25rem 1.3rem 1.1rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: color-mix(in srgb, var(--raised) 92%, transparent);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  box-shadow: var(--shadow-soft);
  text-align: center;
  animation: ra-pop 0.35s var(--ease-out) both;
}

.ra-card h2 {
  font-size: 1.3rem;
  margin-bottom: 0.5rem;
}

#won-time {
  font-size: 2.2rem;
  font-variant-numeric: tabular-nums;
}

.ra-card p {
  font-size: 0.9rem;
  color: var(--muted);
  margin-bottom: 0.9rem;
}

.ra-card p strong {
  color: var(--ink);
  font-weight: 600;
}

.ra-card-k {
  margin: 0 0 0.35rem !important;
  font-family: var(--font-mono);
  font-size: 0.65rem !important;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.ra-card .ra-card-note {
  margin: 0.65rem 0 0;
  font-size: 0.75rem;
}

@keyframes ra-pop {
  from {
    opacity: 0;
    transform: translateY(6px) scale(0.98);
  }
}

/* ---------- Toast ---------- */
.ra-toast {
  position: absolute;
  left: 50%;
  bottom: 0.75rem;
  z-index: 4;
  max-width: calc(100% - 1.5rem);
  margin: 0;
  padding: 0.45rem 0.8rem;
  border-radius: 10px;
  background: var(--ink);
  color: var(--bg);
  font-size: 0.8rem;
  line-height: 1.35;
  text-align: center;
  transform: translate(-50%, 0.5rem);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.25s var(--ease-out);
}

.ra-toast.is-on {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ---------- Hand preview ---------- */
.ra-hand {
  position: absolute;
  right: 0.65rem;
  bottom: 0.65rem;
  z-index: 2;
  width: clamp(8.5rem, 22%, 12rem);
  aspect-ratio: 4 / 3;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #000;
  box-shadow: var(--shadow-soft);
}

.ra-hand[hidden] {
  display: none;
}

.ra-hand video,
.ra-hand canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.ra-hand video {
  transform: scaleX(-1);
}

.ra-hand-status {
  position: absolute;
  left: 0.35rem;
  bottom: 0.3rem;
  right: 0.35rem;
  color: #fff;
  font-family: var(--font-mono);
  font-size: 0.58rem;
  letter-spacing: 0.03em;
  line-height: 1.3;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
}

.ra-hand-lost {
  position: absolute;
  top: 0.35rem;
  left: 50%;
  transform: translateX(-50%);
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  background: #e5484d;
  color: #fff;
  font-family: var(--font-mono);
  font-size: 0.6rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  white-space: nowrap;
}

.ra-hand-lost[hidden] {
  display: none;
}

/* ---------- Control panel ---------- */
.ra-panel {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  padding: 1rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
}

.ra-pad-row {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 1rem;
  align-items: center;
}

@media (min-width: 60rem) {
  .ra-pad-row {
    grid-template-columns: 1fr;
    justify-items: center;
    gap: 0.9rem;
  }

  .ra-grab {
    width: 100%;
  }
}

.ra-dpad {
  display: grid;
  grid-template-columns: repeat(3, 3.4rem);
  grid-template-rows: repeat(3, 3.4rem);
  gap: 0.3rem;
}

.ra-up { grid-area: 1 / 2; }
.ra-left { grid-area: 2 / 1; }
.ra-right { grid-area: 2 / 3; }
.ra-down { grid-area: 3 / 2; }

.ra-key,
.ra-grab,
.ra-btn,
.ra-chip {
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  cursor: pointer;
}

.ra-key {
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  background: var(--key);
  color: var(--key-ink);
  box-shadow: 0 1px 0 var(--line), 0 2px 6px -3px rgba(0, 0, 0, 0.18);
  touch-action: none;
  transition: transform 0.08s ease, background-color 0.12s ease, border-color 0.12s ease;
}

.ra-key svg {
  width: 1.35rem;
  height: 1.35rem;
  fill: currentColor;
}

.ra-key:hover {
  border-color: var(--ink);
}

.ra-key.is-down {
  background: var(--key-press);
  border-color: var(--focus);
  transform: translateY(1px) scale(0.97);
}

.ra-grab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  min-height: 6.5rem;
  padding: 0.75rem 1rem;
  border: 0;
  border-radius: 16px;
  background: var(--ink);
  color: var(--bg);
  font-family: var(--font-mono);
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  touch-action: manipulation;
  transition: transform 0.08s ease, background-color 0.2s ease, box-shadow 0.2s ease;
}

.ra-grab:active {
  transform: scale(0.98);
}

.ra-grab.is-ready {
  background: var(--ok);
  color: var(--ok-ink);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--ok) 25%, transparent);
}

.ra-grab[aria-pressed='true'] {
  background: var(--focus);
  color: #fff;
}

:root[data-theme='dark'] .ra-grab[aria-pressed='true'] {
  color: #0b0b0b;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) .ra-grab[aria-pressed='true'] {
    color: #0b0b0b;
  }
}

.ra-grab.is-ready[aria-pressed='true'] {
  background: var(--ok);
  color: var(--ok-ink);
}

.ra-grab[aria-busy='true'] {
  opacity: 0.75;
}

/* Gripper glyph: two jaws that close when holding. */
.ra-grab-icon {
  position: relative;
  width: 2rem;
  height: 1.4rem;
}

.ra-grab-icon::before,
.ra-grab-icon::after {
  content: '';
  position: absolute;
  top: 0;
  width: 0.45rem;
  height: 1.4rem;
  border: 2px solid currentColor;
  border-top-width: 5px;
  border-radius: 2px 2px 4px 4px;
  transition: transform 0.25s var(--ease-out);
}

.ra-grab-icon::before {
  left: 0;
}

.ra-grab-icon::after {
  right: 0;
}

.ra-grab[aria-pressed='true'] .ra-grab-icon::before {
  transform: translateX(0.45rem);
}

.ra-grab[aria-pressed='true'] .ra-grab-icon::after {
  transform: translateX(-0.45rem);
}

.ra-views {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 0;
  padding: 0.2rem;
  border: 1px solid var(--line);
  border-radius: 999px;
}

.ra-chip {
  min-height: 2.2rem;
  padding: 0.2rem 0.25rem;
  border: 0;
  border-radius: 999px;
  background: none;
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 0.66rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  white-space: nowrap;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.ra-chip:hover {
  color: var(--ink);
}

.ra-chip[aria-pressed='true'] {
  background: var(--ink);
  color: var(--bg);
}

.ra-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.ra-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  min-height: 2.75rem;
  padding: 0.45rem 0.8rem;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: none;
  color: var(--ink);
  font-size: 0.85rem;
  line-height: 1.2;
  white-space: nowrap;
  transition: border-color 0.15s ease, background-color 0.15s ease;
}

.ra-btn svg {
  width: 1.1rem;
  height: 1.1rem;
  flex: none;
}

.ra-btn:hover {
  border-color: var(--ink);
}

.ra-btn[aria-pressed='true'] {
  border-color: var(--focus);
  background: color-mix(in srgb, var(--accent) 22%, transparent);
}

.ra-btn-primary {
  min-width: 9rem;
  border-color: var(--ink);
  background: var(--ink);
  color: var(--bg);
  font-weight: 600;
}

.ra-btn-primary:hover {
  background: color-mix(in srgb, var(--ink) 86%, var(--accent));
}

.ra-keys {
  margin: 0;
  color: var(--muted);
  font-size: 0.72rem;
  line-height: 1.9;
}

.ra-keys kbd {
  display: inline-block;
  min-width: 1.35rem;
  margin: 0 0.08rem;
  padding: 0 0.3rem;
  border: 1px solid var(--line-strong);
  border-bottom-width: 2px;
  border-radius: 5px;
  font-family: var(--font-mono);
  font-size: 0.62rem;
  line-height: 1.45;
  text-align: center;
  color: var(--ink);
}

/* Touch devices: shortcuts don't apply. */
@media (hover: none) and (pointer: coarse), (max-width: 40rem) {
  .ra-keys {
    display: none;
  }
}

/* ---------- Notes ---------- */
.ra-notes {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 1.5rem;
  margin-top: 2rem;
}

@media (min-width: 48rem) {
  .ra-notes {
    grid-template-columns: 1fr 1fr;
    gap: 2.5rem;
  }
}

.ra-how h2 {
  margin-bottom: 0.6rem;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 400;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.ra-how ol {
  margin: 0;
  padding-left: 1.2rem;
}

.ra-how li {
  margin-bottom: 0.4rem;
  font-size: 0.92rem;
}

.ra-how p {
  font-size: 0.92rem;
}

.ra-muted {
  color: var(--muted);
}

/* Phones: d-pad on the left, a big Grab button on the right. */
@media (max-width: 26rem) {
  .ra-panel {
    padding: 0.85rem;
  }

  .ra-dpad {
    grid-template-columns: repeat(3, 3.25rem);
    grid-template-rows: repeat(3, 3.25rem);
  }

  .ra-pad-row {
    gap: 0.75rem;
  }

  .ra-grab {
    min-height: 10.25rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .ra-card {
    animation: none;
  }
}
