:root{
  --w: 1418;
  --h: 908;
  --ratio: calc(var(--w) / var(--h));
  --blue: #2d5bff;

  /* JS sets these */
  --uiScale: 1;
  --uiH: 64px;

  /* toolbar */
  --ui-gap: 10px;
  --ui-pad-x: 12px;
}

html, body{
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #fff;
  font-family: system-ui, -apple-system, sans-serif;
}

/* ---------- TOOLBAR (always one row, shrinks to fit) ---------- */
#ui{
  position: fixed;
  top: 12px;
  left: 50%;

  transform: translateX(-50%) scale(var(--uiScale));
  transform-origin: top center;

  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;

  gap: var(--ui-gap);
  padding: 0 var(--ui-pad-x);

  /* KEY FIXES (no clipping) */
  width: max-content;     /* size to contents */
  max-width: none;        /* remove the cap that caused clipping */
  overflow: visible;      /* don't cut off buttons */
  white-space: nowrap;
  box-sizing: border-box;

  z-index: 10000;
}


.btn{
  width: 44px;
  height: 44px;
  font-size: 20px;

  border: 2px solid var(--blue);
  background:#000;
  color:#fff;

  display:grid;
  place-items:center;
  cursor:pointer;
  flex: 0 0 auto;

  padding:0;
  line-height: 1;
  user-select:none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

@media (max-width: 420px){
  .btn{ border-width: 1.5px; }
}

.btn.on{
  box-shadow: 0 0 0 2px rgba(45,91,255,0.22);
}

/* ---------- STAGE (fits width AND height) ---------- */
.wrap{
  height: 100%;
  display: grid;
  place-items: center;
  padding-top: calc(var(--uiH) + 10px);
  box-sizing: border-box;
}

#stage{
  position: relative;

  /* Fit by width AND height */
  width: min(
    1418px,
    calc(100vw - 24px),
    calc((100vh - var(--uiH) - 24px) * var(--ratio))
  );

  aspect-ratio: var(--w) / var(--h);
  height: auto;
}

/* Layers */
.layer-img{
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;

  image-orientation: none;
  transform: none !important;
}

.layer-vid{
  position: absolute;
  transform-origin: center;
  pointer-events: none;
}

.hidden{ display:none !important; }

/* Z order */
#doodle{ z-index: 1; }
#lines{ z-index: 10; }
#math{ z-index: 11; }
#labels{ z-index: 12; }

#v55{ z-index: 20; }
#vipad{ z-index: 21; }
#v65{ z-index: 22; }

#speakers{ z-index: 100; }

/* Instruction overlay */
.instruction{
  position: absolute;
  z-index: 200;
  right: clamp(12px, 3.5vw, 50px);
  bottom: clamp(12px, 3.5vw, 50px);

  background: rgba(0,0,0,0.5);
  color: rgba(255,255,255,0.95);

  padding: clamp(10px, 2.2vw, 16px) clamp(12px, 2.4vw, 18px);
  border-radius: 2px;

  width: min(360px, 55%);
  font-size: clamp(11px, 2.2vw, 14px);
  line-height: 1.25;

  pointer-events: none;
  user-select: none;
}

@media (max-width: 520px){
  .instruction{
    left: 6%;
    right: 6%;
    bottom: 6%;
    width: auto;
  }
}

.instruction-title{ font-weight: 600; margin-bottom: 10px; }
.instruction-body{ margin-bottom: 10px; }
.instruction-name{ font-weight: 500; }
.instruction-year{ opacity: 0.9; }
